package shop.alien.job.store;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import shop.alien.config.redis.BaseRedisService;
import shop.alien.entity.store.*;
import shop.alien.mapper.EssentialModuleInformationMapper;
import shop.alien.mapper.LifeCouponMapper;
import shop.alien.mapper.StoreInfoMapper;
import shop.alien.mapper.StoreLabelTypeMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: fcw
 * @CreateTime: 2025-04-25
 * @Description:
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class KTVJob {

    private final EssentialModuleInformationMapper informationMapper;

    private final StoreInfoMapper storeInfoMapper;

    private final BaseRedisService baseRedisService;

    private final LifeCouponMapper lifeCouponMapper;

    private final StoreLabelTypeMapper storeLabelTypeMapper;

    @XxlJob("ktvJob")
    public void ktvJob() {

        // 获取所有的商家信息
        List<StoreInfo> storeInfos = storeInfoMapper.selectList(new QueryWrapper<>());
        // 获取所有的代金券信息
        List<LifeCoupon> lifeCoupons = lifeCouponMapper.selectCoupon("KTV");

        // 获取所有KTV门店标签
        List<StoreLabelType> storeLabelTypes = storeLabelTypeMapper.selectList(new LambdaQueryWrapper<StoreLabelType>().eq(StoreLabelType::getStoreType, "KTV"));
        // 获取所有KTV筛选
        List<EssentialModuleInformation> essentialModuleInformations = informationMapper.selectList(
                new LambdaQueryWrapper<EssentialModuleInformation>().eq(EssentialModuleInformation::getParentId, 2940));

        for (EssentialModuleInformation essentialModuleInformation : essentialModuleInformations) {
            Integer informationId = essentialModuleInformation.getId();
            // 获取所有数据值
            List<EssentialModuleInformation> businessHours = informationMapper.selectList(
                    new LambdaQueryWrapper<EssentialModuleInformation>().eq(EssentialModuleInformation::getParentId, informationId));

            switch (informationId) {
                case 2941: // 人数
                    for (EssentialModuleInformation businessHour : businessHours) {
                        Integer businessHourId = businessHour.getId();
                        // 获取符合的商家
                        List<LifeCoupon> list = new ArrayList<>();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 2944:  // 1-2人
                                storeIds = getLifeCoupons(lifeCoupons, 1, 2);
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("2944", storeIds);
                                break;
                            case 2945: // 3-4人
                                storeIds = getLifeCoupons(lifeCoupons, 3, 4);
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("2945", storeIds);
                                break;
                            case 2946: // 4-6人
                                storeIds = getLifeCoupons(lifeCoupons, 4, 6);
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("2946", storeIds);
                                break;
                            case 2947: // 6-10人
                                storeIds = getLifeCoupons(lifeCoupons, 6, 10);
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("2947", storeIds);
                                break;
                            case 2948: // 10人+
                                storeIds = getLifeCoupons(lifeCoupons, 10, Integer.MAX_VALUE);
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("2948", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                case 2942:
                    for (EssentialModuleInformation businessHour : businessHours) {
                        Integer businessHourId = businessHour.getId();
                        // 获取符合的商家
                        List<StoreLabel> list = new ArrayList<>();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 2949: // 大包间
                                storeIds = getStoreLabelTypes(storeLabelTypes, "2949");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("2949", storeIds);
                                break;
                            case 2950: // 小包间
                                storeIds = getStoreLabelTypes(storeLabelTypes, "2950");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("2950", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                case 2943:
                    for (EssentialModuleInformation businessHour : businessHours) {
                        Integer businessHourId = businessHour.getId();
                        // 获取符合的商家
                        List<StoreLabel> list = new ArrayList<>();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 2951: // 麦克风
                                storeIds = getStoreLabelTypes(storeLabelTypes, "2951");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("2951", storeIds);
                                break;
                            case 2952: // 立式麦克风
                                storeIds = getStoreLabelTypes(storeLabelTypes, "2952");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("2952", storeIds);
                                break;
                            case 2953: // 可手机点歌
                                storeIds = getStoreLabelTypes(storeLabelTypes, "2953");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("2953", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;

            }
        }
    }

    private String getLifeCoupons(List<LifeCoupon> lifeCoupons, int i, int i1) {
        List<LifeCoupon> list = lifeCoupons.stream()
                .filter(item -> Integer.parseInt(item.getMinUsePeoples()) <= i && Integer.parseInt(item.getMaxUsePeoples()) >= i1)
                .collect(Collectors.toList());
        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
        return list.stream()
                .map(item -> String.valueOf(item.getStoreId()))
                .collect(Collectors.joining(","));
    }


    /**
     * 处理标签
     *
     * @param storeLabelTypes
     * @param label
     * @return
     */
    private String getStoreLabelTypes(List<StoreLabelType> storeLabelTypes, String label) {
        List<StoreLabelType> list = storeLabelTypes.stream()
                .filter(item -> item.getLabel().contains(label))
                .collect(Collectors.toList());
        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
        return list.stream()
                .map(item -> String.valueOf(item.getStoreId()))
                .collect(Collectors.joining(","));
    }


}
