package shop.alien.job.store;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.EssentialModuleInformation;
import shop.alien.entity.store.LifeCoupon;
import shop.alien.entity.store.StoreBusinessInfo;
import shop.alien.entity.store.StoreLabelType;
import shop.alien.mapper.EssentialModuleInformationMapper;
import shop.alien.mapper.LifeCouponMapper;
import shop.alien.mapper.StoreBusinessInfoMapper;
import shop.alien.mapper.StoreLabelTypeMapper;

import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: fcw
 * @CreateTime: 2025-04-25
 * @Description: 按摩足浴
 */

@Slf4j
@Component
@RequiredArgsConstructor
public class MassageFootBathJob {

    // 零点
    private final static String ZERO_TIME = "00:00";

    // 23:59
    private final static String TWENTY_FOUR_TIME = "23:59";

    private final StoreBusinessInfoMapper storeBusinessInfoMapper;

    private final EssentialModuleInformationMapper informationMapper;

    private final LifeCouponMapper lifeCouponMapper;

    private final BaseRedisService baseRedisService;

    private final StoreLabelTypeMapper storeLabelTypeMapper;

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

        // 获取所有足浴按摩筛选
        List<EssentialModuleInformation> essentialModuleInformations = informationMapper.selectList(new LambdaQueryWrapper<EssentialModuleInformation>().eq(EssentialModuleInformation::getParentId, 1360));

        // 获取所有商家的营业时间
        List<StoreBusinessInfo> storeBusinessInfos = storeBusinessInfoMapper.selectBusinessInfo("按摩足浴");

        // 获取所有的代金券价格
        List<LifeCoupon> lifeCoupons = lifeCouponMapper.selectCoupon("按摩足浴");

        // 获取所有标签
        List<StoreLabelType> storeLabelTypes = storeLabelTypeMapper.selectList(new LambdaQueryWrapper<StoreLabelType>().eq(StoreLabelType::getStoreType, "按摩足浴"));

        for (EssentialModuleInformation essentialModuleInformation : essentialModuleInformations) {
            Integer informationId = essentialModuleInformation.getId();
            // 获取所有数据值
            List<EssentialModuleInformation> businessHours = informationMapper.selectList(new LambdaQueryWrapper<EssentialModuleInformation>().eq(EssentialModuleInformation::getParentId, informationId));
            switch (informationId) {
                case 1587:
                    for (EssentialModuleInformation businessHour : businessHours) {
                        Integer businessHourId = businessHour.getId();
                        // 获取符合的商家
                        List<StoreBusinessInfo> list = new ArrayList<>();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 1599:  // 营业中
                                //获取当前时间
                                Date date = new Date();
                                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                                String time = sdf.format(date);
                                // 将字符串解析为 LocalTime 对象
                                LocalTime localTime = LocalTime.parse(time);
                                // 获取当前在营业的商家
                                list = storeBusinessInfos.stream().filter(item -> localTime.isBefore(LocalTime.parse(item.getEndTime()))).collect(Collectors.toList());
                                storeIds = list.stream().map(item -> String.valueOf(item.getStoreId())).collect(Collectors.joining(","));
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1599", storeIds);
                                break;
                            case 1600: // 24小时营业
                                list = storeBusinessInfos.stream().filter(item -> ZERO_TIME.equals(item.getStartTime()) && TWENTY_FOUR_TIME.equals(item.getEndTime())).collect(Collectors.toList());
                                // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                                storeIds = list.stream().map(item -> String.valueOf(item.getStoreId())).collect(Collectors.joining(","));
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1600", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                // 一小时可订 todo

                // 价格区间
                case 1589:
                    for (EssentialModuleInformation businessHour : businessHours) {
                        Integer businessHourId = businessHour.getId();
                        // 获取符合的商家
                        List<LifeCoupon> list = new ArrayList<>();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 1602:  // 0-100
                                storeIds = getLifeCoupons(lifeCoupons, 0, 100);
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1602", storeIds);
                                break;
                            case 1603: // 100-200
                                storeIds = getLifeCoupons(lifeCoupons, 100, 200);
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1603", storeIds);
                                break;
                            case 1604: // 200-300
                                storeIds = getLifeCoupons(lifeCoupons, 200, 300);
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1604", storeIds);
                                break;
                            case 1605: // 300以上
                                storeIds = getLifeCoupons(lifeCoupons, 300, Integer.MAX_VALUE);
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1605", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                // 服务项目
                case 1590:
                    for (EssentialModuleInformation information : businessHours) {
                        Integer businessHourId = information.getId();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 1608: // 足浴
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1608");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1608", storeIds);
                                break;
                            case 1609: // 推拿
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1609");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1609", storeIds);
                                break;
                            case 1610: // SPA
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1610");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1610", storeIds);
                                break;
                            case 1611: // 采耳
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1611");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1611", storeIds);
                                break;
                            case 1612: // 正骨
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1612");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1612", storeIds);
                                break;
                            case 1613: // 盲人按摩
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1613");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1613", storeIds);
                                break;
                            case 1614: // 艾灸
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1614");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1614", storeIds);
                                break;
                            case 1615: // 拔罐
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1615");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1615", storeIds);
                                break;
                            case 1616: // 刮痧
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1616");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1616", storeIds);
                                break;
                            case 1617: // 修脚
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1617");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1617", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                // 按摩手法
                case 1591:
                    for (EssentialModuleInformation information : businessHours) {
                        Integer businessHourId = information.getId();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 1618: // 中式
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1618");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1618", storeIds);
                                break;
                            case 1619: // 泰式
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1619");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1619", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                // 按摩部位
                case 1592:
                    for (EssentialModuleInformation information : businessHours) {
                        Integer businessHourId = information.getId();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 1620: //   全身
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1620");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1620", storeIds);
                                break;
                            case 1621: //   腰背
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1621");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1621", storeIds);
                                break;
                            case 1622: //   肩颈
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1622");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1622", storeIds);
                                break;
                            case 1623: //   头部
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1623");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1623", storeIds);
                                break;
                            case 1624: //  背部
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1624");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1624", storeIds);
                                break;
                            case 1625: //  腰部
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1625");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1625", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                // 餐饮信息
                case 1593:
                    for (EssentialModuleInformation information : businessHours) {
                        Integer businessHourId = information.getId();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 1626: //   自助餐
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1626");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1626", storeIds);
                                break;
                            case 1627: //   小吃简餐
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1627");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1627", storeIds);
                                break;
                            case 1628: //   零食
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1628");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1628", storeIds);
                                break;
                            case 1629: //   水果
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1629");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1629", storeIds);
                                break;
                            case 1630: //  饮料
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1630");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1630", storeIds);
                                break;
                            case 1631: //  茶水
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1631");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1631", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                // 休闲设施
                case 1594:
                    for (EssentialModuleInformation information : businessHours) {
                        Integer businessHourId = information.getId();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 1632: //   电影院
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1632");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1632", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                // 按摩工具
                case 1595:
                    for (EssentialModuleInformation information : businessHours) {
                        Integer businessHourId = information.getId();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 1633: //   筋膜刀
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1633");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1633", storeIds);
                                break;
                            case 1634: //   筋膜枪
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1634");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1634", storeIds);
                                break;
                            case 1635: //   点穴棒
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1635");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1635", storeIds);
                                break;
                            case 1636: //   经络梳
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1636");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1636", storeIds);
                                break;
                            case 1637: //  砭石按摩锥
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1637");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1637", storeIds);
                                break;
                            case 1638: //  擀筋棒
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1638");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1638", storeIds);
                                break;
                            case 1639: //  罗汉锤
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1639");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1639", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                // 热敷工具
                case 1596:
                    for (EssentialModuleInformation information : businessHours) {
                        Integer businessHourId = information.getId();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 1640: //   铜壶
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1640");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1640", storeIds);
                                break;
                            case 1641: //   热敷包
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1641");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1641", storeIds);
                                break;
                            case 1642: //   热石
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1642");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1642", storeIds);
                                break;
                            case 1643: //   砭石
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1643");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1643", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                //特色设施
                case 1597:
                    for (EssentialModuleInformation information : businessHours) {
                        Integer businessHourId = information.getId();
                        // 获取过滤后列表中每个对象的 storeId 字段，并用逗号连接成字符串
                        String storeIds = "";
                        switch (businessHourId) {
                            case 1644: //   充电宝
                                storeIds = getStoreLabelTypes(storeLabelTypes, "1644");
                                // 将商家storeId添加到redis中
                                baseRedisService.setString("1644", storeIds);
                                break;
                            default:
                                break;
                        }
                    }
                    break;
            }
        }


    }

    /**
     * 处理商家价格区间
     *
     * @param lifeCoupons
     * @param startNum
     * @param endNum
     * @return
     */
    private String getLifeCoupons(List<LifeCoupon> lifeCoupons, Integer startNum, Integer endNum) {
        List<LifeCoupon> list = lifeCoupons.stream().filter(item -> Integer.parseInt(item.getPrice()) >= startNum && Integer.parseInt(item.getPrice()) < endNum).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(","));
    }
}
