package shop.alien.storeplatform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.*;
import shop.alien.entity.store.dto.LifeGroupBuyDto;
import shop.alien.entity.store.vo.LifeGroupBuyNameThaliVo;
import shop.alien.entity.store.vo.LifeGroupBuyThaliVo;
import shop.alien.entity.store.vo.StoreMenuVo;
import shop.alien.entity.storePlatform.LifeGroupBuyMainPlatform;
import shop.alien.entity.storePlatform.vo.LifeGroupBuyPlatformDto;
import shop.alien.entity.storePlatform.vo.LifeGroupThalisPlatformDto;
import shop.alien.mapper.*;
import shop.alien.mapper.storePlantform.LifeGroupBuyMainPlantformMapper;
import shop.alien.storeplatform.service.LifeGroupBuyPlatformService;
import shop.alien.util.common.JwtUtil;
import shop.alien.util.type.LifeCollectIdentityQuery;
import shop.alien.util.type.TypeUtil;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 团购服务实现
 * @date 2025-11-11
 */
@Service
@RequiredArgsConstructor
public class LifeGroupBuyPlatformServiceImpl extends ServiceImpl<LifeGroupBuyMainMapper, LifeGroupBuyMain> implements LifeGroupBuyPlatformService {

    /**
     * 团购主表 Mapper
     */
    private final LifeGroupBuyMainMapper lifeGroupBuyMainMapper;

    /**
     * 团购套餐 Mapper
     */
    private final LifeGroupBuyThaliMapper lifeGroupBuyThaliMapper;

    /**
     * 门店图片 Mapper
     */
    private final StoreImgMapper storeImgMapper;

    /**
     * 节假日对照 Mapper
     */
    private final EssentialHolidayComparisonMapper essentialHolidayComparisonMapper;

    /**
     * 门店菜品 Mapper
     */
    private final StoreMenuMapper storeMenuMapper;

    /**
     * 收藏 Mapper
     */
    private final LifeCollectMapper lifeCollectMapper;

    private final TypeUtil typeUtil;

    /**
     * 审核记录 Mapper
     */
    private final WebAuditMapper webAuditMapper;

    /**
     * 订单 Mapper
     */
    private final LifeUserOrderMapper lifeUserOrderMapper;

    /**
     * 团购主表 Mapper
     */
    private final LifeGroupBuyMainPlantformMapper lifeGroupBuyMainPlantformMapper;


    /**
     * 保存团购草稿
     *
     * @param lifeGroupBuyDto 团购数据
     * @return 是否保存成功
     * @date 2025-11-11
     */
    @Override
    public boolean saveDraft(LifeGroupBuyPlatformDto lifeGroupBuyDto) {
        LifeGroupBuyMainPlatform lifeGroupBuyMain = lifeGroupBuyDto.getLifeGroupBuyMain();
        List<LifeGroupBuyThali> lifeGroupBuyThalis = new ArrayList<>();

        // 套餐菜品拼接
        if (lifeGroupBuyDto.getLifeGroupBuyThalis().size() > 0 ) {
            lifeGroupBuyDto.getLifeGroupBuyThalis().forEach(lifeGroupBuyThali -> {
                lifeGroupBuyThali.getDishes().forEach( lifeGroupBuyDish -> {
                    LifeGroupBuyThali row = new LifeGroupBuyThali();
                    row.setGroupName(lifeGroupBuyThali.getGroupName());
                    row.setDetailId(lifeGroupBuyDish.getDetailId());
                    row.setDishName(lifeGroupBuyDish.getDishName());
                    row.setDishesUnit(lifeGroupBuyDish.getDishesUnit());
                    row.setQty(lifeGroupBuyDish.getQty());
                    lifeGroupBuyThalis.add(row);
                });
            });
        }


        // 拼接有效期
        if (lifeGroupBuyMain.getEffectiveDateType() != null && lifeGroupBuyMain.getEffectiveDateType() == 1) {
            List<String> expirationDateList = lifeGroupBuyMain.getExpirationDateList();
            if (ObjectUtils.isNotEmpty(expirationDateList)) {
                lifeGroupBuyMain.setEffectiveDateValue(String.join(",", expirationDateList));
            } else {
                lifeGroupBuyMain.setEffectiveDateValue("");
            }
        } else {
            lifeGroupBuyMain.setEffectiveDateValue(lifeGroupBuyMain.getExpirationDate());
        }

        // 拼接不可用日期（自定义不可用日期）
        if (lifeGroupBuyMain.getDisableDateType() != null && lifeGroupBuyMain.getDisableDateType() == 2) {
            // 二维数组拼接成String类型
            String row = lifeGroupBuyMain.getDisableDateList().stream()
                    .map(range -> String.join(",", range))
                    .collect(Collectors.joining(";"));
            // 添加不可用日期
            lifeGroupBuyMain.setDisableDateValue(row);
        }

        lifeGroupBuyMain.setWriteOff("手动核销");

        JSONObject currentUserInfo = JwtUtil.getCurrentUserInfo();
        Integer userId = null;
        if (!ObjectUtils.isEmpty(currentUserInfo)) {
            userId = currentUserInfo.getInteger("userId");
        }
        if (ObjectUtils.isEmpty(lifeGroupBuyMain.getId())) {
            Integer count = lifeGroupBuyMainMapper.selectCount(new LambdaQueryWrapper<LifeGroupBuyMain>().like(LifeGroupBuyMain::getCreatedTime, DateUtils.formatDate(new Date(), "yyyy-MM-dd")).eq(LifeGroupBuyMain::getStoreId, lifeGroupBuyMain.getStoreId()));
            lifeGroupBuyMain.setGroupNo("G" + DateUtils.formatDate(new Date(), "yyyyMMdd") + lifeGroupBuyMain.getStoreId() + org.apache.commons.lang3.StringUtils.leftPad(String.valueOf(count + 1), 5, "0"));
            lifeGroupBuyMain.setCreatedUserId(userId);
            lifeGroupBuyMainMapper.insert(lifeGroupBuyMain);
            if (ObjectUtils.isNotEmpty(lifeGroupBuyThalis)) {
                for (int i = 0; i < lifeGroupBuyThalis.size(); i++) {
                    LifeGroupBuyThali lifeGroupBuyThali = lifeGroupBuyThalis.get(i);
                    lifeGroupBuyThali.setParentId(lifeGroupBuyMain.getId().toString());
                    lifeGroupBuyThali.setDetailSort(i + 1);
                    lifeGroupBuyThali.setCreatedUserId(userId);
                    lifeGroupBuyThaliMapper.insert(lifeGroupBuyThali);
                }
            }
            return true;
        } else {
            lifeGroupBuyMain.setUpdatedUserId(userId);
            lifeGroupBuyMainMapper.updateById(lifeGroupBuyMain);
            lifeGroupBuyThaliMapper.update(null, new LambdaUpdateWrapper<LifeGroupBuyThali>().eq(LifeGroupBuyThali::getParentId, lifeGroupBuyMain.getId()).set(LifeGroupBuyThali::getDeleteFlag, 1));
            if (ObjectUtils.isNotEmpty(lifeGroupBuyThalis)) {
                for (int i = 0; i < lifeGroupBuyThalis.size(); i++) {
                    LifeGroupBuyThali lifeGroupBuyThali = lifeGroupBuyThalis.get(i);
                    lifeGroupBuyThali.setParentId(lifeGroupBuyMain.getId().toString());
                    lifeGroupBuyThali.setDetailSort(i + 1);
                    lifeGroupBuyThali.setCreatedUserId(userId);
                    lifeGroupBuyThaliMapper.insert(lifeGroupBuyThali);
                }
            }
            return true;
        }
    }

    /**
     * 保存团购并返回套餐信息
     *
     * @param lifeGroupBuyDto 团购数据
     * @return 最新套餐记录
     * @date 2025-11-11
     */
    @Override
    public LifeGroupBuyThali saveThali(LifeGroupBuyPlatformDto lifeGroupBuyDto) {

        LifeGroupBuyMainPlatform lifeGroupBuyMain = lifeGroupBuyDto.getLifeGroupBuyMain();
        List<LifeGroupBuyThali> lifeGroupBuyThalis = new ArrayList<>();


        if (lifeGroupBuyDto.getLifeGroupBuyThalis().size() > 0 ) {
            lifeGroupBuyDto.getLifeGroupBuyThalis().forEach(lifeGroupBuyThali -> {
                lifeGroupBuyThali.getDishes().forEach( lifeGroupBuyDish -> {
                    LifeGroupBuyThali row = new LifeGroupBuyThali();
                    row.setGroupName(lifeGroupBuyThali.getGroupName());
                    row.setDetailId(lifeGroupBuyDish.getDetailId());
                    row.setDishName(lifeGroupBuyDish.getDishName());
                    row.setDishesUnit(lifeGroupBuyDish.getDishesUnit());
                    row.setQty(lifeGroupBuyDish.getQty());
                    lifeGroupBuyThalis.add(row);
                });
            });
        }


        // 拼接有效期
        if (lifeGroupBuyMain.getEffectiveDateType() != null && lifeGroupBuyMain.getEffectiveDateType() == 1) {
            List<String> expirationDateList = lifeGroupBuyMain.getExpirationDateList();
            if (ObjectUtils.isNotEmpty(expirationDateList)) {
                lifeGroupBuyMain.setEffectiveDateValue(String.join(",", expirationDateList));
            } else {
                lifeGroupBuyMain.setEffectiveDateValue("");
            }
        } else {
            lifeGroupBuyMain.setEffectiveDateValue(lifeGroupBuyMain.getExpirationDate());
        }

        // 拼接不可用日期（自定义不可用日期）
        if (lifeGroupBuyMain.getDisableDateType() != null && lifeGroupBuyMain.getDisableDateType() == 2) {
            // 二维数组拼接成String类型
            String row = lifeGroupBuyMain.getDisableDateList().stream()
                    .map(range -> String.join(",", range))
                    .collect(Collectors.joining(";"));
            // 添加不可用日期
            lifeGroupBuyMain.setDisableDateValue(row);
        }

        lifeGroupBuyMain.setWriteOff("手动核销");

        JSONObject currentUserInfo = JwtUtil.getCurrentUserInfo();
        Integer userId = null;
        if (!ObjectUtils.isEmpty(currentUserInfo)) {
            userId = currentUserInfo.getInteger("userId");
        }

        LifeGroupBuyThali lifeGroupBuyThali = null;
        if (ObjectUtils.isEmpty(lifeGroupBuyMain.getId())) {
            Integer count = lifeGroupBuyMainMapper.selectCount(new LambdaQueryWrapper<LifeGroupBuyMain>().like(LifeGroupBuyMain::getCreatedTime, DateUtils.formatDate(new Date(), "yyyy-MM-dd")).eq(LifeGroupBuyMain::getStoreId, lifeGroupBuyMain.getStoreId()));
            lifeGroupBuyMain.setGroupNo("G" + DateUtils.formatDate(new Date(), "yyyyMMdd") + lifeGroupBuyMain.getStoreId() + org.apache.commons.lang3.StringUtils.leftPad(String.valueOf(count + 1), 5, "0"));
            lifeGroupBuyMain.setCreatedUserId(userId);
            lifeGroupBuyMain.setCreatedTime(new Date());
            lifeGroupBuyMainMapper.insert(lifeGroupBuyMain);
            if (ObjectUtils.isNotEmpty(lifeGroupBuyThalis)) {
                for (int i = 0; i < lifeGroupBuyThalis.size(); i++) {
                    lifeGroupBuyThali = lifeGroupBuyThalis.get(i);
                    lifeGroupBuyThali.setParentId(lifeGroupBuyMain.getId().toString());
                    lifeGroupBuyThali.setDetailSort(i + 1);
                    lifeGroupBuyThali.setCreatedUserId(userId);
                    lifeGroupBuyThali.setCreatedTime(new Date());
                    lifeGroupBuyThaliMapper.insert(lifeGroupBuyThali);
                }
            }
            return lifeGroupBuyThali;
        } else {
            lifeGroupBuyMain.setUpdatedUserId(userId);
            lifeGroupBuyMain.setCreatedTime(new Date());
            lifeGroupBuyMainMapper.updateById(lifeGroupBuyMain);
            lifeGroupBuyThaliMapper.update(null, new LambdaUpdateWrapper<LifeGroupBuyThali>().eq(LifeGroupBuyThali::getParentId, lifeGroupBuyMain.getId()).set(LifeGroupBuyThali::getDeleteFlag, 1).set(LifeGroupBuyThali::getCreatedTime, new Date()));
            if (ObjectUtils.isNotEmpty(lifeGroupBuyThalis)) {
                for (int i = 0; i < lifeGroupBuyThalis.size(); i++) {
                    lifeGroupBuyThali = lifeGroupBuyThalis.get(i);
                    lifeGroupBuyThali.setParentId(lifeGroupBuyMain.getId().toString());
                    lifeGroupBuyThali.setDetailSort(i + 1);
                    lifeGroupBuyThali.setCreatedUserId(userId);
                    lifeGroupBuyThaliMapper.insert(lifeGroupBuyThali);
                }
            }
            return lifeGroupBuyThali;
        }
    }

    /**
     * 查询团购套餐列表
     *
     * @param page      页码
     * @param size      每页数量
     * @param storeId   门店编号
     * @param status    状态
     * @param groupName 团购名称
     * @param groupType 团购类型
     * @return 套餐分页数据
     * @date 2025-11-11
     */
    @Override
    public IPage<LifeGroupBuyThaliVo> getThaliList(int page, int size, String storeId, String status, String groupName, String groupType, String reviewType, String groupNo) {
        QueryWrapper<LifeGroupBuyThaliVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(storeId), "store_id", storeId);
        if (StringUtils.isNotEmpty(reviewType)) {
            switch (reviewType) {
                case "0":
                    queryWrapper.eq("status", "1");
                    break;
                case "1":
                    queryWrapper.in("status", Arrays.asList("2", "4", "5", "6", "7", "8"));
                    break;
                case "2":
                    queryWrapper.eq("status", "3");
                    break;
                default:
                    queryWrapper.eq(StringUtils.isNotEmpty(status), "status", status);
                    break;
            }
        } else {
            if ("8".equals(status)) {
                queryWrapper.in(StringUtils.isNotEmpty(status), "status", Arrays.asList("2", "6"));
            } else {
                queryWrapper.eq(StringUtils.isNotEmpty(status), "status", status);
            }
        }
        queryWrapper.eq(StringUtils.isNotEmpty(groupType), "group_type", groupType);
        queryWrapper.like(StringUtils.isNotEmpty(groupName), "group_name", groupName);
        queryWrapper.like(StringUtils.isNotEmpty(groupNo), "group_no", groupNo);
        queryWrapper.eq("delete_flag", 0);
        queryWrapper.orderByDesc("created_time");
        IPage<LifeGroupBuyThaliVo> lifeGroupBuyThaliVoIPage = new Page<>(page, size);
        IPage<LifeGroupBuyThaliVo> voIPage = lifeGroupBuyMainMapper.selectPageByThaliVo(lifeGroupBuyThaliVoIPage, queryWrapper);
        voIPage.getRecords().forEach(this::getMainImgAndDisableDate);
        return voIPage;
    }

    /**
     * 根据编号查询团购详情
     *
     * @param id     团购编号
     * @param userId 用户编号
     * @return 团购详情
     * @date 2025-11-11
     */
    @Override
    public LifeGroupBuyPlatformDto getThaliById(String id, String userId) {

        LifeGroupBuyPlatformDto dto = new LifeGroupBuyPlatformDto();

        // 获取团购信息
        LifeGroupBuyMain lifeGroupBuyMain = lifeGroupBuyMainMapper.selectById(id);

        // 创建 LifeGroupBuyMainPlatform 对象并复制属性
        LifeGroupBuyMainPlatform platform = new LifeGroupBuyMainPlatform();
        BeanUtils.copyProperties(lifeGroupBuyMain, platform);

        // 是否收藏了该团购
        LambdaQueryWrapper<LifeCollect> collectQuery = new LambdaQueryWrapper<>();
        LifeCollectIdentityQuery.applyUserSide(collectQuery, LifeCollectIdentityQuery.resolve(userId, typeUtil));
        collectQuery.eq(LifeCollect::getCouponId, id);
        LifeCollect lifeCollect = lifeCollectMapper.selectOne(collectQuery);
        dto.setIsCollect("0");
        if (!Objects.isNull(lifeCollect)) {
            dto.setIsCollect("1");
        }

        // 获取团购菜品信息
        List<LifeGroupBuyThali> lifeGroupBuyThalis = lifeGroupBuyThaliMapper.selectList(new LambdaQueryWrapper<LifeGroupBuyThali>().eq(LifeGroupBuyThali::getParentId, id).eq(LifeGroupBuyThali::getDeleteFlag, 0));
        // 菜品拼接
        if (lifeGroupBuyMain.getGroupType() == 1) {
            for (LifeGroupBuyThali lifeGroupBuyThali : lifeGroupBuyThalis) {
                if (StringUtils.isNotEmpty(lifeGroupBuyThali.getDetailId())) {
                    StoreMenuVo menuInfo = storeMenuMapper.getMenuInfo(Integer.parseInt(lifeGroupBuyThali.getDetailId()));
                    if (ObjectUtils.isNotEmpty(menuInfo)) {
                        lifeGroupBuyThali.setDishName(menuInfo.getDishName());
                        lifeGroupBuyThali.setDishImg(menuInfo.getImgUrl());
                        lifeGroupBuyThali.setDishPrice(menuInfo.getDishPrice().toString());
                        lifeGroupBuyThali.setDishesUnit(menuInfo.getDishesUnit());
                    }
                }
            }
        }

        Map<String, List<LifeGroupBuyThali>> collect = lifeGroupBuyThalis.stream().collect(Collectors.groupingBy(LifeGroupBuyThali::getGroupName));


        List<LifeGroupThalisPlatformDto> thalis = new ArrayList<>();
        List<String> keys = collect.entrySet().stream().sorted(Comparator.comparingInt(i -> i.getValue().get(0).getDetailSort())).map(Map.Entry::getKey).collect(Collectors.toList());
        for (String key : keys) {
            LifeGroupThalisPlatformDto lifeGroupThalisPlatformDto = new LifeGroupThalisPlatformDto();
            lifeGroupThalisPlatformDto.setGroupName(key);
            lifeGroupThalisPlatformDto.setDishes(collect.get(key).stream().sorted(Comparator.comparingInt(LifeGroupBuyThali::getDetailSort)).collect(Collectors.toList()));
            thalis.add(lifeGroupThalisPlatformDto);
        }
        dto.setLifeGroupBuyThalis(thalis);

        //图片
        if (StringUtils.isNotEmpty(platform.getImageId())) {
            List<String> imgIds = Arrays.asList(platform.getImageId().split(","));
            List<StoreImg> storeImgs = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().in(StoreImg::getId, imgIds));
            if (ObjectUtils.isNotEmpty(storeImgs)) {
                platform.setImageValueStr(storeImgs.stream().map(StoreImg::getImgUrl).collect(Collectors.toList()));
            }
        }

        // 拼接有效期
        if (platform.getEffectiveDateType() != null && platform.getEffectiveDateType() == 1) {
            if (ObjectUtils.isNotEmpty(platform.getEffectiveDateValue())) {
                platform.setExpirationDateList(Arrays.asList(platform.getEffectiveDateValue().split(",")));
            }
        }

        // 拼接不可用日期（自定义不可用日期）
        if (platform.getDisableDateType() != null && platform.getDisableDateType() == 2) {
            // String拼接成二维数组类型
            List<List<String>> disableDateList = Arrays.stream(platform.getDisableDateValue().split(";"))
                    .map(range -> Arrays.asList(range.split(",")))
                    .collect(Collectors.toList());
            platform.setDisableDateList(disableDateList);
        }

        dto.setLifeGroupBuyMain(platform);
        return dto;
    }

    /**
     * 更新团购状态
     *
     * @param id               团购编号
     * @param status           状态值
     * @param approvalComments 审核意见
     * @return 是否更新成功
     * @date 2025-11-11
     */
    @Override
    public boolean updateStatus(String id, String status, String approvalComments) {
        LambdaUpdateWrapper<LifeGroupBuyMain> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(LifeGroupBuyMain::getId, id);
        lambdaUpdateWrapper.set(LifeGroupBuyMain::getStatus, status);
        lambdaUpdateWrapper.set(LifeGroupBuyMain::getApprovalComments, approvalComments);
        if(status.equals("5")){
            LifeGroupBuyMain lifeGroupBuyMain = lifeGroupBuyMainMapper.selectById(id);
            if(lifeGroupBuyMain !=null && lifeGroupBuyMain.getStatus()==2){
                lambdaUpdateWrapper.set(LifeGroupBuyMain::getStartTimeValue, new Date());
            }
        }
            lifeGroupBuyMainMapper.update(null,lambdaUpdateWrapper);
        //处理中台审核记录
        if (StringUtils.isNotEmpty(id) && StringUtils.isNotEmpty(status) && (status.equals("2") || status.equals("3"))) {
            LambdaUpdateWrapper<WebAudit> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(WebAudit::getLifeGroupPackageId, id);
            wrapper.set(WebAudit::getStatus, "1");
            webAuditMapper.update(null, wrapper);
        }
        return true;
    }

    /**
     * 删除团购
     *
     * @param id        团购编号
     * @param groupType 团购类型
     * @return 是否删除成功
     * @date 2025-11-11
     */
    @Override
    public boolean delThaliById(String id, String groupType) {
        if ("1".equals(groupType)) {
            lifeGroupBuyThaliMapper.update(null, new LambdaUpdateWrapper<LifeGroupBuyThali>().eq(LifeGroupBuyThali::getParentId, id).set(LifeGroupBuyThali::getDeleteFlag, 1));
        }
        lifeGroupBuyMainMapper.update(null, new LambdaUpdateWrapper<LifeGroupBuyMain>().eq(LifeGroupBuyMain::getId, id).set(LifeGroupBuyMain::getDeleteFlag, 1));
        return true;
    }

    /**
     * 更新团购库存
     *
     * @param id  团购编号
     * @param num 库存数量
     * @return 是否更新成功
     * @date 2025-11-11
     */
    @Override
    public boolean updateNum(String id, String num) {
        //判断库存数为0 增加库存数修改状态为进行中
        LambdaQueryWrapper<LifeGroupBuyMain> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LifeGroupBuyMain :: getId, id);
        LifeGroupBuyMain lifeGroupBuyMain = lifeGroupBuyMainMapper.selectOne(lambdaQueryWrapper);

        if(lifeGroupBuyMain.getInventoryNum() == 0){
            LambdaUpdateWrapper<LifeGroupBuyMain> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(LifeGroupBuyMain :: getId, id);
            lambdaUpdateWrapper.set(LifeGroupBuyMain :: getStatus, 5);
            lambdaUpdateWrapper.set(LifeGroupBuyMain :: getInventoryNum, num);
            lifeGroupBuyMainMapper.update(null, lambdaUpdateWrapper);
        }
        else {
            LambdaUpdateWrapper<LifeGroupBuyMain> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(LifeGroupBuyMain::getId, id);
            lambdaUpdateWrapper.set(LifeGroupBuyMain::getInventoryNum, num);
            lifeGroupBuyMainMapper.update(null, lambdaUpdateWrapper);
        }

        return true;
    }

    /**
     * 填充主图与不可用日期信息
     *
     * @param record 团购套餐数据
     * @date 2025-11-11
     */
    private void getMainImgAndDisableDate(LifeGroupBuyThaliVo record) {
        //图片
        if (StringUtils.isNotEmpty(record.getImageId())) {
            List<String> imgIds = Arrays.asList(record.getImageId().split(","));
            List<StoreImg> storeImgs = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().in(StoreImg::getId, imgIds));
            if (ObjectUtils.isNotEmpty(storeImgs)) {
                /*String imgs = storeImgs.stream()
                        .map(StoreImg::getImgUrl)
                        .collect(Collectors.joining("*"));
                record.setImageValueStr(imgs);*/
                record.setImageValueStr(storeImgs.stream().map(StoreImg::getImgUrl).collect(Collectors.toList()));
            }
        }
        //不可用日期
        if (ObjectUtils.isNotEmpty(record.getDisableDateType())) {
            if (record.getDisableDateType() == 1) {
                List<String> valueList = Arrays.asList(record.getDisableDateValue().split(";"));
                String holiday = "";
                if (valueList.size() > 1) {
                    List<String> list = Arrays.asList(valueList.get(1).split(","));
                    List<EssentialHolidayComparison> essentialHolidayComparisons = essentialHolidayComparisonMapper.selectList(new LambdaQueryWrapper<EssentialHolidayComparison>().in(EssentialHolidayComparison::getId, list));
                    holiday = essentialHolidayComparisons.stream().map(EssentialHolidayComparison::getFestivalName).collect(Collectors.joining(","));
                }
                if (StringUtils.isNotEmpty(valueList.get(0)) && StringUtils.isNotEmpty(holiday)) {
                    record.setDisableDateValueStr(valueList.get(0) + ";" + holiday);
                }
                if (StringUtils.isEmpty(valueList.get(0))) {
                    record.setDisableDateValueStr(holiday);
                }
                if (StringUtils.isEmpty(holiday)) {
                    record.setDisableDateValueStr(valueList.get(0));
                }
            }
        }

        //已售数量
        record.setSaleNum(lifeUserOrderMapper.countBuyGroup(record.getId().toString()));

        BigDecimal costPrice = lifeGroupBuyMainPlantformMapper.selectSumCostPrice(record.getId());
        if (ObjectUtils.isEmpty(costPrice)) {
            costPrice = BigDecimal.ZERO;;
        }
        record.setCostPrice(costPrice);

        if (ObjectUtils.isNotEmpty(record.getPreferentialPrice())) {
            record.setProfit(record.getPreferentialPrice().subtract(costPrice));
        }

        if (record.getStatus() == 1) {
            record.setReviewType("待审核");
        } else if (record.getStatus() == 2 || record.getStatus() == 4 || record.getStatus() == 5 || record.getStatus() == 6 || record.getStatus() == 7 ) {
            record.setReviewType("审核通过");
        } else if (record.getStatus() == 3) {
            record.setReviewType("审核拒绝");
        }

        if (Objects.nonNull(record.getStatus())) {
            switch (record.getStatus()) {
                case 0:
                    record.setStatusName("草稿");
                    break;
                case 1:
                    record.setStatusName("待审核");
                    break;
                case 2:
                    record.setStatusName("未开始");
                    break;
                case 3:
                    record.setStatusName("审核拒绝");
                    break;
                case 4:
                    record.setStatusName("已售罄");
                    break;
                case 5:
                    record.setStatusName("进行中");
                    break;
                case 6:
                    record.setStatusName("已下架");
                    break;
                case 7:
                    record.setStatusName("已结束");
                    break;
                default:
                    record.setStatusName("未知状态");
                    break;
            }
        }

    }

}
