package shop.alien.store.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.aliyun.tea.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.*;
import shop.alien.entity.store.dto.LifeDiscountCouponDto;
import shop.alien.entity.store.vo.LifeCouponStatusVo;
import shop.alien.entity.store.vo.LifeDiscountCouponStoreFriendVo;
import shop.alien.entity.store.vo.LifeDiscountCouponUserWebVo;
import shop.alien.entity.store.vo.LifeDiscountCouponVo;
import shop.alien.mapper.*;
import shop.alien.store.service.LifeDiscountCouponQuantumRulesService;
import shop.alien.store.service.LifeDiscountCouponService;
import shop.alien.store.service.LifeDiscountCouponUserService;
import shop.alien.util.common.constant.DiscountCouponEnum;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 优惠券表 服务实现类
 * </p>
 *
 * @author ssk
 * @since 2025-02-07
 */
@Service
@RequiredArgsConstructor
public class LifeDiscountCouponServiceImpl extends ServiceImpl<LifeDiscountCouponMapper, LifeDiscountCoupon> implements LifeDiscountCouponService {

    final static String HOLIDAY_RULE_TITLE = "节假日规则：";
    final static String WEEK_RULE_TITLE = "周中规则：";
    final static String MINIMUM_SPENDING_AMOUNT = "最低限制消费金额：";
    final static String NOT_USE = "不可用;";

    private final LifeDiscountCouponMapper lifeDiscountCouponMapper;

    private final LifeDiscountCouponUnavailableRulesMapper lifeDiscountCouponUnavailableRulesMapper;

    private final LifeDiscountCouponQuantumRulesMapper lifeDiscountCouponQuantumRulesMapper;

    private final LifeDiscountCouponUserMapper lifeDiscountCouponUserMapper;

    private final EssentialHolidayComparisonMapper essentialHolidayComparisonMapper;

    private final LifeDiscountCouponStoreFriendMapper lifeDiscountCouponStoreFriendMapper;

    private final StoreInfoMapper storeInfoMapper;

    private final LifeDiscountCouponQuantumRulesService lifeDiscountCouponQuantumRulesService;

    private final LifeDiscountCouponUserService lifeDiscountCouponUserService;

    @Override
    public boolean addDiscountCoupon(LifeDiscountCouponDto lifeDiscountCouponDto) {
        try {
            //发布优惠券表信息
            LifeDiscountCoupon lifeDiscountCoupon = new LifeDiscountCoupon();
            BeanUtils.copyProperties(lifeDiscountCouponDto, lifeDiscountCoupon);
            // 根据开始领取时间判断可领取状态
            // 判断是否在领取时间内

            Date now = new Date();
            Instant instant = now.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDate localNow = instant.atZone(zoneId).toLocalDate();
            if (lifeDiscountCouponDto.getCouponStatus() == 1 && !StringUtils.isEmpty(lifeDiscountCoupon.getBeginGetDate()) || !StringUtils.isEmpty(lifeDiscountCoupon.getEndGetDate())) {
                int startResult = localNow.compareTo(lifeDiscountCoupon.getBeginGetDate());
                int endResult = localNow.compareTo(lifeDiscountCoupon.getEndGetDate());
                if ((lifeDiscountCouponDto.getCouponStatus() != null && lifeDiscountCouponDto.getCouponStatus() == 0) || (startResult < 0 || endResult > 0)) {
                    lifeDiscountCoupon.setGetStatus(0);
                } else {
                    lifeDiscountCoupon.setGetStatus(1);
                }
            }

            // 设置有效期
            String specifiedDay = lifeDiscountCoupon.getSpecifiedDay();
            if (!StringUtils.isEmpty(specifiedDay)) {
                int sDay = Integer.parseInt(specifiedDay);
                if (sDay > 0 && lifeDiscountCouponDto.getEndGetDate() != null) {
                    Date endGetDate = Date.from(lifeDiscountCouponDto.getEndGetDate().atStartOfDay(ZoneId.systemDefault()).toInstant());
                    Date validDate = addDaysToDateJava8(endGetDate, sDay);
                    LocalDate validDateLocalDate = validDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                    lifeDiscountCoupon.setValidDate(validDateLocalDate);
                }
            }
            lifeDiscountCouponMapper.insert(lifeDiscountCoupon);
            //发布优惠券规则信息
            //周中规则保存
            LifeDiscountCouponUnavailableRules lifeDiscountCouponUnavailableRules = new LifeDiscountCouponUnavailableRules();
            lifeDiscountCouponUnavailableRules.setDiscountCouponId(lifeDiscountCoupon.getId());
            lifeDiscountCouponUnavailableRules.setUnavailableRuleType(DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue());
            if (CollectionUtils.isNotEmpty(lifeDiscountCouponDto.getWeeklyDisabledList())) {
                lifeDiscountCouponDto.getWeeklyDisabledList().forEach(weeklyDisabled -> {
                    lifeDiscountCouponUnavailableRules.setUnavailableRuleValue(weeklyDisabled);
                    lifeDiscountCouponUnavailableRulesMapper.insert(lifeDiscountCouponUnavailableRules);
                });
            }

            //节假日规则保存
            lifeDiscountCouponUnavailableRules.setUnavailableRuleType(DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue());
            if (CollectionUtils.isNotEmpty(lifeDiscountCouponDto.getHolidayDisabledList())) {
                lifeDiscountCouponDto.getHolidayDisabledList().forEach(holidayDisabled -> {
                    lifeDiscountCouponUnavailableRules.setUnavailableRuleValue(holidayDisabled);
                    lifeDiscountCouponUnavailableRulesMapper.insert(lifeDiscountCouponUnavailableRules);
                });
            }

            //领取规则
            lifeDiscountCouponUnavailableRules.setUnavailableRuleType(DiscountCouponEnum.CLAIM_RULE.getValue());
            String claimRule = lifeDiscountCouponDto.getClaimRule();
            //领取规则为自定义时，获取自定义数量
            if (claimRule != null && claimRule.equals(DiscountCouponEnum.CUSTOMIZE.getValue())) {
                lifeDiscountCouponUnavailableRules.setCustomizeValue(lifeDiscountCouponDto.getClaimRuleCustomizeValue());
            }
            lifeDiscountCouponUnavailableRules.setUnavailableRuleValue(lifeDiscountCouponDto.getClaimRule());
            lifeDiscountCouponUnavailableRulesMapper.insert(lifeDiscountCouponUnavailableRules);

            //指定时间段可用规则
            List<LifeDiscountCouponQuantumRules> availableTimeQuantumList = lifeDiscountCouponDto.getAvailableTimeQuantum();
            if (CollectionUtils.isNotEmpty(availableTimeQuantumList)) {
                availableTimeQuantumList.forEach(availableTimeQuantum -> {
                    availableTimeQuantum.setDiscountCouponId(lifeDiscountCoupon.getId());
                });
                lifeDiscountCouponQuantumRulesService.saveBatch(availableTimeQuantumList);
            }

            //自定义时间段不可用规则
            List<LifeDiscountCouponQuantumRules> customizeUnavailableTimeQuantumList = lifeDiscountCouponDto.getCustomizeUnavailableTimeQuantum();
            if (CollectionUtils.isNotEmpty(customizeUnavailableTimeQuantumList)) {
                customizeUnavailableTimeQuantumList.forEach(customizeUnavailableTimeQuantum -> {
                    customizeUnavailableTimeQuantum.setDiscountCouponId(lifeDiscountCoupon.getId());
                });
                lifeDiscountCouponQuantumRulesService.saveBatch(customizeUnavailableTimeQuantumList);
            }
        } catch (BeansException e) {
            log.error("LifeDiscountCouponServiceImpl.receiveCoupon ERROR Msg=" + e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public boolean editDiscountCoupon(LifeDiscountCouponDto lifeDiscountCouponDto) {
        try {
            //发布优惠券表信息
            LifeDiscountCoupon lifeDiscountCoupon = new LifeDiscountCoupon();
            lifeDiscountCoupon.setId(Integer.parseInt(lifeDiscountCouponDto.getCouponId()));
            BeanUtils.copyProperties(lifeDiscountCouponDto, lifeDiscountCoupon);

            // 根据开始领取时间判断可领取状态
            // 判断是否在领取时间内
            Date now = new Date();
            Instant instant = now.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDate localNow = instant.atZone(zoneId).toLocalDate();
            if (lifeDiscountCouponDto.getCouponStatus() == 1 && !StringUtils.isEmpty(lifeDiscountCoupon.getBeginGetDate()) || !StringUtils.isEmpty(lifeDiscountCoupon.getEndGetDate())) {
                int startResult = localNow.compareTo(lifeDiscountCoupon.getBeginGetDate());
                int endResult = localNow.compareTo(lifeDiscountCoupon.getEndGetDate());
                if ((lifeDiscountCouponDto.getCouponStatus() != null && lifeDiscountCouponDto.getCouponStatus() == 0) || (startResult < 0 || endResult > 0)) {
                    lifeDiscountCoupon.setGetStatus(0);
                } else {
                    lifeDiscountCoupon.setGetStatus(1);
                }
            } else if (lifeDiscountCouponDto.getCouponStatus() == 0) {
                lifeDiscountCoupon.setGetStatus(0);
            }

            // 设置有效期
            String specifiedDay = lifeDiscountCouponDto.getSpecifiedDay();
            if (!StringUtils.isEmpty(specifiedDay)) {
                int sDay = Integer.parseInt(specifiedDay);
                if (sDay > 0 && lifeDiscountCouponDto.getEndGetDate() != null) {
                    Date endGetDate = Date.from(lifeDiscountCouponDto.getEndGetDate().atStartOfDay(ZoneId.systemDefault()).toInstant());
                    Date validDate = addDaysToDateJava8(endGetDate, sDay);
                    LocalDate validDateLocalDate = validDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                    lifeDiscountCoupon.setValidDate(validDateLocalDate);
                }
            }

            lifeDiscountCouponMapper.updateById(lifeDiscountCoupon);
            //发布优惠券规则信息
            //先删除之前所有规则
            LambdaQueryWrapper<LifeDiscountCouponUnavailableRules> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(LifeDiscountCouponUnavailableRules::getDiscountCouponId, lifeDiscountCoupon.getId());
            lifeDiscountCouponUnavailableRulesMapper.delete(queryWrapper);

            //周中规则保存
            LifeDiscountCouponUnavailableRules lifeDiscountCouponUnavailableRules = new LifeDiscountCouponUnavailableRules();
            lifeDiscountCouponUnavailableRules.setDiscountCouponId(lifeDiscountCoupon.getId());
            lifeDiscountCouponUnavailableRules.setUnavailableRuleType(DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue());
            if (CollectionUtils.isNotEmpty(lifeDiscountCouponDto.getWeeklyDisabledList())) {
                lifeDiscountCouponDto.getWeeklyDisabledList().forEach(weeklyDisabled -> {
                    lifeDiscountCouponUnavailableRules.setUnavailableRuleValue(weeklyDisabled);
                    lifeDiscountCouponUnavailableRulesMapper.insert(lifeDiscountCouponUnavailableRules);
                });
            }

            //节假日规则保存
            lifeDiscountCouponUnavailableRules.setUnavailableRuleType(DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue());
            if (CollectionUtils.isNotEmpty(lifeDiscountCouponDto.getHolidayDisabledList())) {
                lifeDiscountCouponDto.getHolidayDisabledList().forEach(holidayDisabled -> {
                    lifeDiscountCouponUnavailableRules.setUnavailableRuleValue(holidayDisabled);
                    lifeDiscountCouponUnavailableRulesMapper.insert(lifeDiscountCouponUnavailableRules);
                });
            }

            //领取规则
            lifeDiscountCouponUnavailableRules.setUnavailableRuleType(DiscountCouponEnum.CLAIM_RULE.getValue());
            lifeDiscountCouponUnavailableRules.setUnavailableRuleValue(lifeDiscountCouponDto.getClaimRule());
            lifeDiscountCouponUnavailableRulesMapper.insert(lifeDiscountCouponUnavailableRules);

            // 删除优惠券时间段规则信息
            LambdaQueryWrapper<LifeDiscountCouponQuantumRules> lifeDiscountCouponQuantumRulesLambdaQueryWrapper = new LambdaQueryWrapper<>();
            lifeDiscountCouponQuantumRulesLambdaQueryWrapper.eq(LifeDiscountCouponQuantumRules::getDiscountCouponId, lifeDiscountCoupon.getId());
            lifeDiscountCouponQuantumRulesMapper.delete(lifeDiscountCouponQuantumRulesLambdaQueryWrapper);

            // 保存指定时间段可用规则
            List<LifeDiscountCouponQuantumRules> availableTimeQuantumList = lifeDiscountCouponDto.getAvailableTimeQuantum();
            if (CollectionUtils.isNotEmpty(availableTimeQuantumList)) {
                availableTimeQuantumList.forEach(availableTimeQuantum -> {
                    availableTimeQuantum.setDiscountCouponId(lifeDiscountCoupon.getId());
                });
                lifeDiscountCouponQuantumRulesService.saveBatch(availableTimeQuantumList);
            }
            // 保存自定义时间段不可用规则
            List<LifeDiscountCouponQuantumRules> customizeUnavailableTimeQuantumList = lifeDiscountCouponDto.getCustomizeUnavailableTimeQuantum();
            if (CollectionUtils.isNotEmpty(customizeUnavailableTimeQuantumList)) {
                customizeUnavailableTimeQuantumList.forEach(customizeUnavailableTimeQuantum -> {
                    customizeUnavailableTimeQuantum.setDiscountCouponId(lifeDiscountCoupon.getId());
                });
                lifeDiscountCouponQuantumRulesService.saveBatch(customizeUnavailableTimeQuantumList);
            }

        } catch (BeansException e) {
            log.error("LifeDiscountCouponServiceImpl.editDiscountCoupon ERROR Msg=" + e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteDiscountCoupon(Integer id) {
        //删除优惠券表信息
        lifeDiscountCouponMapper.deleteById(id);
        //删除优惠券规则信息
        lifeDiscountCouponUnavailableRulesMapper.delete(new LambdaQueryWrapper<LifeDiscountCouponUnavailableRules>().eq(LifeDiscountCouponUnavailableRules::getDiscountCouponId, id));
        return true;
    }

    @Override
    public boolean cutDiscountCouponStatus(String counponId) {
        LifeDiscountCoupon lifeDiscountCoupon = lifeDiscountCouponMapper.selectById(counponId);
        if (lifeDiscountCoupon.getGetStatus() == Integer.parseInt(DiscountCouponEnum.NO_GET.getValue())) {
            Date now = new Date();
            Instant instant = now.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDate localNow = instant.atZone(zoneId).toLocalDate();
            lifeDiscountCoupon.setBeginGetDate(localNow);
        }
        lifeDiscountCoupon.setGetStatus(lifeDiscountCoupon.getGetStatus() == Integer.parseInt(DiscountCouponEnum.NO_GET.getValue()) ? Integer.parseInt(DiscountCouponEnum.CAN_GET.getValue()) : Integer.parseInt(DiscountCouponEnum.NO_GET.getValue()));
        lifeDiscountCouponMapper.updateById(lifeDiscountCoupon);
        return true;
    }

    @Override
    public LifeDiscountCouponVo getCounponDetailById(String counponId, UserLoginInfo userLoginInfo) {
        LifeDiscountCoupon lifeDiscountCoupon = lifeDiscountCouponMapper.selectById(counponId);
        LifeDiscountCouponVo lifeDiscountCouponVo = new LifeDiscountCouponVo();
        lifeDiscountCouponVo.setCouponId(Integer.parseInt(counponId));
        BeanUtils.copyProperties(lifeDiscountCoupon, lifeDiscountCouponVo);
        //查规则信息
        LambdaQueryWrapper<LifeDiscountCouponUnavailableRules> rulesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rulesLambdaQueryWrapper.eq(LifeDiscountCouponUnavailableRules::getDiscountCouponId, counponId);
        List<LifeDiscountCouponUnavailableRules> lifeDiscountCouponUnavailableRules = lifeDiscountCouponUnavailableRulesMapper.selectList(rulesLambdaQueryWrapper);

        List<String> weeklyDisabledList = new ArrayList<>();
        List<String> holidayDisabledList = new ArrayList<>();
        String claimRule = "";
        for (LifeDiscountCouponUnavailableRules rule : lifeDiscountCouponUnavailableRules) {
            if (rule.getUnavailableRuleType().equals(DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue())) {
                weeklyDisabledList.add(rule.getUnavailableRuleValue());
            }
            if (rule.getUnavailableRuleType().equals(DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue())) {
                holidayDisabledList.add(rule.getUnavailableRuleValue());
            }
            if (rule.getUnavailableRuleType().equals(DiscountCouponEnum.CLAIM_RULE.getValue())) {
                claimRule = rule.getUnavailableRuleValue();
            }
        }
        lifeDiscountCouponVo.setWeeklyDisabledList(weeklyDisabledList);
        lifeDiscountCouponVo.setHolidayDisabledList(holidayDisabledList);
        lifeDiscountCouponVo.setClaimRule(claimRule);

        // 查询时间段规则信息
        LambdaQueryWrapper<LifeDiscountCouponQuantumRules> quantumRulesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        quantumRulesLambdaQueryWrapper.eq(LifeDiscountCouponQuantumRules::getDiscountCouponId, counponId);
        List<LifeDiscountCouponQuantumRules> lifeDiscountCouponQuantumRulesList = lifeDiscountCouponQuantumRulesMapper.selectList(quantumRulesLambdaQueryWrapper);
        List<LifeDiscountCouponQuantumRules> availableTimeQuantumList = new ArrayList<>();
        List<LifeDiscountCouponQuantumRules> customizeUnavailableTimeQuantumList = new ArrayList<>();
        for (LifeDiscountCouponQuantumRules lifeDiscountCouponQuantumRules : lifeDiscountCouponQuantumRulesList) {
            if (lifeDiscountCouponQuantumRules.getTimeQuantumType().equals(DiscountCouponEnum.AVAILABLE_TIME_QUANTUM.getValue())) {
                availableTimeQuantumList.add(lifeDiscountCouponQuantumRules);
            }
            if (lifeDiscountCouponQuantumRules.getTimeQuantumType().equals(DiscountCouponEnum.UNAVAILABLE_TIME_QUANTUM.getValue())) {
                customizeUnavailableTimeQuantumList.add(lifeDiscountCouponQuantumRules);
            }
        }
        lifeDiscountCouponVo.setAvailableTimeQuantum(availableTimeQuantumList);
        lifeDiscountCouponVo.setCustomizeUnavailableTimeQuantum(customizeUnavailableTimeQuantumList);
        return lifeDiscountCouponVo;
    }

    public static Date addDaysToDateJava8(Date date, int days) {
        // 将Date转换为Instant
        Instant instant = date.toInstant();

        // 转换为LocalDate并加天数
        LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate newLocalDate = localDate.plusDays(days);

        // 转换回Date
        return Date.from(newLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    @Override
    public List<LifeDiscountCouponVo> getStoreUserCouponList(String storeId, UserLoginInfo userLoginInfo) {
        List<LifeDiscountCouponVo> lifeDiscountCouponVos = new ArrayList<>();
        try {
            LocalDateTime now = LocalDateTime.now();
            //根据店铺id查询该店铺的优惠券,状态是开启领取的券
            List<LifeDiscountCoupon> lifeDiscountCoupons = lifeDiscountCouponMapper.selectList(new LambdaQueryWrapper<LifeDiscountCoupon>()
                    .eq(LifeDiscountCoupon::getStoreId, storeId).eq(LifeDiscountCoupon::getGetStatus, "1") //还有库存
                    .ge(LifeDiscountCoupon::getEndGetDate, LocalDate.now())
                    .orderByDesc(LifeDiscountCoupon::getAttentionCanReceived));
            //根据优惠券列表查询该优惠券是否领取过
            for (LifeDiscountCoupon lifeDiscountCoupon : lifeDiscountCoupons) {
                LifeDiscountCouponVo lifeDiscountCouponVo = new LifeDiscountCouponVo();
                lifeDiscountCouponVo.setCouponId(lifeDiscountCoupon.getId());
                BeanUtils.copyProperties(lifeDiscountCoupon, lifeDiscountCouponVo);
                //查询该人员该卡券是否达到限领数量
                LambdaQueryWrapper<LifeDiscountCouponUser> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(LifeDiscountCouponUser::getCouponId, lifeDiscountCoupon.getId());
                queryWrapper.eq(LifeDiscountCouponUser::getUserId, userLoginInfo.getUserId());
                queryWrapper.orderByDesc(LifeDiscountCouponUser::getReceiveTime);
                List<LifeDiscountCouponUser> lifeDiscountCouponUsers = lifeDiscountCouponUserMapper.selectList(queryWrapper);
                String specifiedDay = lifeDiscountCoupon.getSpecifiedDay();
                Date receiveDay = null;

                //判断是否已经全部领取
                if (lifeDiscountCoupon.getRestrictedQuantity() != 0 && lifeDiscountCouponUsers.size() >= lifeDiscountCoupon.getRestrictedQuantity()) {
                    receiveDay = lifeDiscountCouponUsers.get(0).getReceiveTime();
                    lifeDiscountCouponVo.setExpirationTime(lifeDiscountCouponUsers.get(0).getExpirationTime());
                    lifeDiscountCouponVo.setCanReceived(false);
                } else {
                    //判断一下领取规则
                    LambdaQueryWrapper<LifeDiscountCouponUnavailableRules> rulesLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    rulesLambdaQueryWrapper.eq(LifeDiscountCouponUnavailableRules::getDiscountCouponId, lifeDiscountCoupon.getId());
                    rulesLambdaQueryWrapper.eq(LifeDiscountCouponUnavailableRules::getUnavailableRuleType, DiscountCouponEnum.CLAIM_RULE.getValue());
                    LifeDiscountCouponUnavailableRules lifeDiscountCouponUnavailableRules = lifeDiscountCouponUnavailableRulesMapper.selectOne(rulesLambdaQueryWrapper);
                    //获取一下当前人员领取的最近一次领取
                    if (lifeDiscountCouponUsers.size() == 0) {
                        lifeDiscountCouponVo.setCanReceived(true);
                    } else {
                        LifeDiscountCouponUser lifeDiscountCouponUser = lifeDiscountCouponUsers.get(0);
                        Date receiveTime = lifeDiscountCouponUser.getReceiveTime();
                        receiveDay = lifeDiscountCouponUser.getReceiveTime();

                        // 将 Date 转换为 Instant
                        Instant instant = receiveTime.toInstant();
                        // 获取系统默认时区
                        ZoneId zoneId = ZoneId.systemDefault();
                        // 将 Instant 转换为 LocalDateTime
                        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
                        String unavailableRuleValue = lifeDiscountCouponUnavailableRules.getUnavailableRuleValue();
                        long confineTo = 0L;
                        if (DiscountCouponEnum.DAY.getValue().equals(unavailableRuleValue)) {//如果规则限制一天一张
                            confineTo = ChronoUnit.DAYS.between(localDateTime, now);
                        } else if (DiscountCouponEnum.WEEK.getValue().equals(unavailableRuleValue)) {//如果规则限制一周一张
                            confineTo = ChronoUnit.WEEKS.between(localDateTime, now);
                        } else if (DiscountCouponEnum.MONTH.getValue().equals(unavailableRuleValue)) {//如果规则限制一月一张
                            confineTo = ChronoUnit.MONTHS.between(localDateTime, now);
                        } else if (DiscountCouponEnum.YEAR.getValue().equals(unavailableRuleValue)) {//如果规则限制一年一张
                            confineTo = ChronoUnit.YEARS.between(localDateTime, now);
                        }
                        //如果在规则范围内，则不允许领取,范围外，则可以领取
                        lifeDiscountCouponVo.setCanReceived(confineTo >= 1);
                        lifeDiscountCouponVo.setExpirationTime(lifeDiscountCouponUser.getExpirationTime());
                    }
                }
                if (!StringUtils.isEmpty(specifiedDay)) {
                    int sDay = Integer.parseInt(specifiedDay);
                    if (sDay > 0 && receiveDay != null) {
                        Date validDate = addDaysToDateJava8(receiveDay, sDay);
                        LocalDate validDateLocalDate = validDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                        lifeDiscountCouponVo.setValidDate(validDateLocalDate);
                    } else {
                        lifeDiscountCouponVo.setValidDate(null);
                    }
                }
                lifeDiscountCouponVo.setQuantityClaimed(lifeDiscountCouponUsers.size());
                lifeDiscountCouponVos.add(lifeDiscountCouponVo);
            }
        } catch (Exception e) {
            log.error("LifeDiscountCouponServiceImpl.getStoreUserUsableCouponList ERROR Msg={}", e);
        }
        return lifeDiscountCouponVos;
    }

    @Override
    public Map getStoreUserUsableCouponList(String storeId, UserLoginInfo userLoginInfo, BigDecimal amount) {
        HashMap<String, List> result = new HashMap<>();
        List<LifeDiscountCouponVo> canUseLifeDiscountCouponVos = new ArrayList<>();
        List<LifeDiscountCouponVo> forbidUseLifeDiscountCouponVos = new ArrayList<>();
        LambdaQueryWrapper<LifeDiscountCouponUser> queryWrapper = new LambdaQueryWrapper<>();

        // 获取 Calendar 实例，默认表示当前日期和时间
        Calendar calendar = Calendar.getInstance();
        // 将小时设置为 0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟设置为 0
        calendar.set(Calendar.MINUTE, 0);
        // 将秒设置为 0
        calendar.set(Calendar.SECOND, 0);
        // 将毫秒设置为 0
        calendar.set(Calendar.MILLISECOND, 0);
        // 获取设置后的 Date 对象
        Date midnight = calendar.getTime();

        //storeId（前台传进来的是团购id）
        //根据团购id获取门店id
//        LifeCoupon lifeCoupon = lifeCouponMapper.selectById(storeId);
//        String makeSureStoreId = lifeCoupon.getStoreId();

        //查询可用的优惠券 1、当前优惠券没过使用期，2、当前优惠券为待使用，3、是该门店的券
        queryWrapper.eq(LifeDiscountCouponUser::getUserId, userLoginInfo.getUserId());
        queryWrapper.eq(LifeDiscountCouponUser::getStatus, DiscountCouponEnum.WAITING_USED.getValue());
        queryWrapper.ge(LifeDiscountCouponUser::getExpirationTime, midnight);
        List<LifeDiscountCouponUser> lifeDiscountCouponUserList = lifeDiscountCouponUserMapper.selectList(queryWrapper);

        List<LifeDiscountCouponUser> lifeDiscountCouponUsers = new ArrayList<>();
        for (LifeDiscountCouponUser lifeDiscountCouponUser : lifeDiscountCouponUserList) {
            //查询该优惠券属于哪个店铺
            LifeDiscountCoupon lifeDiscountCoupon = lifeDiscountCouponMapper.selectById(lifeDiscountCouponUser.getCouponId());
            String couponStoreId = lifeDiscountCoupon.getStoreId();
            if (null != couponStoreId && couponStoreId.equals(storeId)) {
                lifeDiscountCouponUsers.add(lifeDiscountCouponUser);
            }
            //平台优惠券
            if (3 == lifeDiscountCoupon.getType()) {
                lifeDiscountCouponUsers.add(lifeDiscountCouponUser);
            }
        }

        //判断今日是否为节该券禁用节假日
        //判断当日是否为节假日
        // 获取当前日期
        Date currentDate = new Date();
        // 创建 SimpleDateFormat 对象，指定日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 格式化日期
        String formattedDate = dateFormat.format(currentDate);
        //获取当前年份
        // 获取 Calendar 实例
        // 获取当前年份
        int year = calendar.get(Calendar.YEAR);
        //获取该年份基础节假日数据
        LambdaQueryWrapper<EssentialHolidayComparison> essentialHolidayComparisonLambdaQueryWrapper = new LambdaQueryWrapper<>();
        essentialHolidayComparisonLambdaQueryWrapper.eq(EssentialHolidayComparison::getParticularYear, year);
        List<EssentialHolidayComparison> essentialHolidayComparisons = essentialHolidayComparisonMapper.selectList(essentialHolidayComparisonLambdaQueryWrapper);
        String festivalName = "";
        for (EssentialHolidayComparison essentialHolidayComparison : essentialHolidayComparisons) {
            if (essentialHolidayComparison.getFestivalDate().equals(formattedDate)) {
                festivalName = essentialHolidayComparison.getFestivalName();
            }
        }
        //获取今日周几
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        String dayName = "";
        switch (dayOfWeek) {
            case Calendar.SUNDAY:
                dayName = "日";
                break;
            case Calendar.MONDAY:
                dayName = "一";
                break;
            case Calendar.TUESDAY:
                dayName = "二";
                break;
            case Calendar.WEDNESDAY:
                dayName = "三";
                break;
            case Calendar.THURSDAY:
                dayName = "四";
                break;
            case Calendar.FRIDAY:
                dayName = "五";
                break;
            case Calendar.SATURDAY:
                dayName = "六";
                break;
        }

        //去重优惠券id
        for (LifeDiscountCouponUser lifeDiscountCouponUser : lifeDiscountCouponUsers) {
            LifeDiscountCoupon lifeDiscountCoupon = lifeDiscountCouponMapper.selectById(lifeDiscountCouponUser.getCouponId());
            LifeDiscountCouponVo lifeDiscountCouponVo = new LifeDiscountCouponVo();
            lifeDiscountCouponVo.setCouponId(lifeDiscountCoupon.getId());
            lifeDiscountCouponVo.setUserCouponId(lifeDiscountCouponUser.getId());
            BeanUtils.copyProperties(lifeDiscountCoupon, lifeDiscountCouponVo);
            lifeDiscountCouponVo.setQuantityClaimed(lifeDiscountCouponUsers.size());
            //默认该券可用
            lifeDiscountCouponVo.setDisabledStatus(true);
            //该券的节假日禁用规则
            LambdaQueryWrapper<LifeDiscountCouponUnavailableRules> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(LifeDiscountCouponUnavailableRules::getDiscountCouponId, lifeDiscountCoupon.getId());
            lambdaQueryWrapper.eq(LifeDiscountCouponUnavailableRules::getUnavailableRuleType, DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue());
            List<LifeDiscountCouponUnavailableRules> lifeDiscountCouponUnavailableRules = lifeDiscountCouponUnavailableRulesMapper.selectList(lambdaQueryWrapper);
            String forbiddenRule = "";
            if (lifeDiscountCouponUnavailableRules.size() > 0) {
                forbiddenRule += HOLIDAY_RULE_TITLE;
            }
            List<String> holidCoupnDis = lifeDiscountCouponUnavailableRules.stream().map(rule -> rule.getUnavailableRuleValue()).collect(Collectors.toList());
            forbiddenRule += String.join(", ", holidCoupnDis);
            for (LifeDiscountCouponUnavailableRules lifeDiscountCouponUnavailableRule : lifeDiscountCouponUnavailableRules) {
                //如果当日为节假日，并且该券该节假日也禁用，则该券禁用
                if (StringUtils.isEmpty(festivalName) && festivalName.equals(lifeDiscountCouponUnavailableRule.getUnavailableRuleValue())) {
                    lifeDiscountCouponVo.setDisabledStatus(false);
                }
            }
            if (lifeDiscountCouponUnavailableRules.size() > 0) {
                forbiddenRule += NOT_USE;
            }
            //该券的周中禁用规则
            LambdaQueryWrapper<LifeDiscountCouponUnavailableRules> lambdaQueryWeekWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWeekWrapper.eq(LifeDiscountCouponUnavailableRules::getDiscountCouponId, lifeDiscountCoupon.getId());
            lambdaQueryWeekWrapper.eq(LifeDiscountCouponUnavailableRules::getUnavailableRuleType, DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue());
            List<LifeDiscountCouponUnavailableRules> weekLifeDiscountCouponUnavailableRules = lifeDiscountCouponUnavailableRulesMapper.selectList(lambdaQueryWeekWrapper);
            if (weekLifeDiscountCouponUnavailableRules.size() > 0) {
                forbiddenRule += WEEK_RULE_TITLE;
            }
            List<String> weekCoupnDis = weekLifeDiscountCouponUnavailableRules.stream().map(rule -> rule.getUnavailableRuleValue()).collect(Collectors.toList());
            forbiddenRule += String.join(", ", weekCoupnDis);
            for (LifeDiscountCouponUnavailableRules lifeDiscountCouponUnavailableRule : lifeDiscountCouponUnavailableRules) {
                //该券该周中日也禁用，则该券禁用
                if (dayName.equals(lifeDiscountCouponUnavailableRule.getUnavailableRuleValue())) {
                    lifeDiscountCouponVo.setDisabledStatus(false);
                }
            }
            if (weekLifeDiscountCouponUnavailableRules.size() > 0) {
                forbiddenRule += NOT_USE;
            }
            //添加限制金额规则
            if (amount.compareTo(lifeDiscountCoupon.getMinimumSpendingAmount()) < 0) {
                lifeDiscountCouponVo.setDisabledStatus(false);
            }
            forbiddenRule += MINIMUM_SPENDING_AMOUNT + lifeDiscountCoupon.getMinimumSpendingAmount();
            lifeDiscountCouponVo.setForbiddenRule(forbiddenRule);
            if (lifeDiscountCouponVo.isDisabledStatus()) {
                canUseLifeDiscountCouponVos.add(lifeDiscountCouponVo);
            } else {
                forbidUseLifeDiscountCouponVos.add(lifeDiscountCouponVo);
            }
        }
        result.put("canUseLifeDiscountCouponVos", canUseLifeDiscountCouponVos);
        result.put("forbidUseLifeDiscountCouponVos", forbidUseLifeDiscountCouponVos);
        return result;
    }

    @Override
    public List<LifeDiscountCouponVo> getUserCouponList(UserLoginInfo userLoginInfo, int page, int size, String tabType) {
        List<LifeDiscountCouponVo> lifeDiscountCouponVos = new ArrayList<>();
        IPage<LifeDiscountCouponUser> iPage = new Page<>(page, size);
        LambdaQueryWrapper<LifeDiscountCouponUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LifeDiscountCouponUser::getUserId, userLoginInfo.getUserId());

        //比较工具对象
        //获取七日前时间
        Date now = new Date();
        // 获取 7 天后的时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.DAY_OF_YEAR, 7);
        // 获取 Calendar 实例，默认表示当前日期和时间
        // 将小时设置为 0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟设置为 0
        calendar.set(Calendar.MINUTE, 0);
        // 将秒设置为 0
        calendar.set(Calendar.SECOND, 0);
        // 将毫秒设置为 0
        calendar.set(Calendar.MILLISECOND, 0);
        Date sevenDaysLater = calendar.getTime();

        // 获取当前时间
        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.setTime(now);
        // 获取 Calendar 实例，默认表示当前日期和时间
        // 将小时设置为 0
        nowCalendar.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟设置为 0
        nowCalendar.set(Calendar.MINUTE, 0);
        // 将秒设置为 0
        nowCalendar.set(Calendar.SECOND, 0);
        // 将毫秒设置为 0
        nowCalendar.set(Calendar.MILLISECOND, 0);
        // 获取设置后的 Date 对象
        Date midnight = nowCalendar.getTime();
        //所有状态未使用且未过期的
        // 获取当前时间
        LocalDateTime localNow = LocalDateTime.now();
        // 计算七日前的日期
        LocalDateTime sevenDaysAgo = localNow.minusDays(7);
        // 将七日前的时间设置为凌晨 0 点 0 分 0 秒 0 纳秒
        LocalDateTime sevenDaysAgoMidnight = sevenDaysAgo.withHour(0).withMinute(0).withSecond(0).withNano(0);
        //判断分页类型
        if (tabType.equals(DiscountCouponEnum.ALL.getValue())) {
            //如果已过期 过期日期大于今天
            queryWrapper.gt(LifeDiscountCouponUser::getExpirationTime, midnight);
            //状态为待使用
            queryWrapper.eq(LifeDiscountCouponUser::getStatus, DiscountCouponEnum.WAITING_USED.getValue());
        } else if (tabType.equals(DiscountCouponEnum.BE_ABOUT_TO_EXPORE.getValue())) {
            //如果即将过期 七天之内
            //小于等于七日后
            queryWrapper.le(LifeDiscountCouponUser::getExpirationTime, sevenDaysLater);
            //大于等于今天
            queryWrapper.ge(LifeDiscountCouponUser::getExpirationTime, midnight);
            //并且状态为待使用
            queryWrapper.eq(LifeDiscountCouponUser::getStatus, DiscountCouponEnum.WAITING_USED.getValue());
        } else if (tabType.equals(DiscountCouponEnum.HAVE_ALREADY_APPLIED.getValue())) {
            //如果已使用 状态为已使用
            queryWrapper.eq(LifeDiscountCouponUser::getStatus, DiscountCouponEnum.HAVE_BEEN_USED.getValue());
        } else if (tabType.equals(DiscountCouponEnum.HAVE_EXPIRED.getValue())) {
            //如果已过期 过期日期小于今天,并且过期时间小于七天
            queryWrapper.lt(LifeDiscountCouponUser::getExpirationTime, midnight);
            queryWrapper.gt(LifeDiscountCouponUser::getExpirationTime, sevenDaysAgoMidnight);
            //并且状态为待使用
            queryWrapper.eq(LifeDiscountCouponUser::getStatus, DiscountCouponEnum.WAITING_USED.getValue());
        }
        queryWrapper.orderByDesc(LifeDiscountCouponUser::getCreatedTime);
        IPage<LifeDiscountCouponUser> lifeDiscountCouponUserIPage = lifeDiscountCouponUserMapper.selectPage(iPage, queryWrapper);
        //根据券id去查询该券的基本信息
        //根据优惠券列表查询该优惠券是否领取过
        //
        // 判断是否在领取时间内
        Instant instant = now.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localNow1 = instant.atZone(zoneId).toLocalDate();

        //所有优惠券
        List<Integer> couponIdList = lifeDiscountCouponUserIPage.getRecords().stream().map(LifeDiscountCouponUser::getCouponId).collect(Collectors.toList());
        List<LifeDiscountCoupon> lifeDiscountCoupons = lifeDiscountCouponMapper.selectList(new QueryWrapper<LifeDiscountCoupon>().lambda().in(!couponIdList.isEmpty(), LifeDiscountCoupon::getId, couponIdList));
        if (lifeDiscountCoupons.isEmpty()) {
            return lifeDiscountCouponVos;
        }
        //优惠券的所有门店
        List<String> storeIdList = lifeDiscountCoupons.stream().map(LifeDiscountCoupon::getStoreId).collect(Collectors.toList());
        List<StoreInfo> storeInfoList = storeInfoMapper.getList(new LambdaQueryWrapper<StoreInfo>().in(!storeIdList.isEmpty(), StoreInfo::getId, storeIdList));
        if (storeInfoList.isEmpty() && lifeDiscountCouponUserIPage.getRecords().isEmpty()) {
            return lifeDiscountCouponVos;
        }
        for (LifeDiscountCouponUser lifeDiscountCouponUser : lifeDiscountCouponUserIPage.getRecords()) {
            LifeDiscountCoupon lifeDiscountCouponOne = lifeDiscountCoupons.stream().filter(lifeDiscountCoupon -> lifeDiscountCoupon.getId().equals(lifeDiscountCouponUser.getCouponId())).collect(Collectors.toList()).get(0);
            StoreInfo storeInfoOne = null;
            if (!storeInfoList.isEmpty() && !StringUtils.isEmpty(lifeDiscountCouponOne.getStoreId())) {
                storeInfoOne = storeInfoList.stream().filter(storeInfo -> storeInfo.getId().toString().equals(lifeDiscountCouponOne.getStoreId())).collect(Collectors.toList()).get(0);
            }
            LifeDiscountCouponVo lifeDiscountCouponVo = new LifeDiscountCouponVo();
            if (null != storeInfoOne) {
                lifeDiscountCouponVo.setBusinessSection(storeInfoOne.getBusinessSection());
                lifeDiscountCouponVo.setBusinessSectionName(storeInfoOne.getBusinessSectionName());
            }
            lifeDiscountCouponVo.setCouponId(lifeDiscountCouponOne.getId());
            BeanUtils.copyProperties(lifeDiscountCouponOne, lifeDiscountCouponVo);
            lifeDiscountCouponVo.setQuantityClaimed(lifeDiscountCouponUserIPage.getRecords().size());
            lifeDiscountCouponVo.setExpirationTime(lifeDiscountCouponUser.getExpirationTime());
            // 设置有效期
            String specifiedDay = lifeDiscountCouponOne.getSpecifiedDay();
            if (!StringUtils.isEmpty(specifiedDay)) {
                int sDay = Integer.parseInt(specifiedDay);
                if (sDay > 0) {
                    Date receiveDay = lifeDiscountCouponUser.getReceiveTime();
                    Date validDate = addDaysToDateJava8(receiveDay, sDay);
                    LocalDate validDateLocalDate = validDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                    lifeDiscountCouponVo.setValidDate(validDateLocalDate);
                }
            }

            // 判断是否到了优惠券的开始时间
            lifeDiscountCouponVo.setReachUseTimeFlag(1);
            if (null != lifeDiscountCouponOne.getBeginGetDate() && localNow1.isBefore(lifeDiscountCouponOne.getBeginGetDate())) {
                lifeDiscountCouponVo.setReachUseTimeFlag(0);
            }
            lifeDiscountCouponVos.add(lifeDiscountCouponVo);
        }
        return lifeDiscountCouponVos;
    }

    //获取纯日期
    public static Date getPureDate(Date date) {
        Instant instant = date.toInstant();
        LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDateTime localDateTime = localDate.atStartOfDay(); // 时间为 00:00
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    @Override
    public IPage<LifeDiscountCouponVo> getStoreAllCouponList(String storeId, UserLoginInfo userLoginInfo, int page, int size, String couponName, String tab, int couponsFromType, int couponStatus) {

        if (couponsFromType == 1) {
            IPage<LifeDiscountCoupon> iPage = new Page<>(page, size);
            List<LifeDiscountCouponVo> lifeDiscountCouponVos = new ArrayList<>();
            LambdaQueryWrapper<LifeDiscountCoupon> lifeDiscountCouponLambdaQueryWrapper = new LambdaQueryWrapper<LifeDiscountCoupon>();
            lifeDiscountCouponLambdaQueryWrapper.eq(LifeDiscountCoupon::getCouponStatus, couponStatus);
            //如果couponName不为空，则模糊查询
            if (!StringUtils.isEmpty(couponName)) {
                lifeDiscountCouponLambdaQueryWrapper.like(LifeDiscountCoupon::getName, couponName);
            }
            //获取当日日期
            Date now = new Date();
            //如果根据类型查询
            if (!StringUtils.isEmpty(tab) && tab.equals("1")) {//进行中
                //开始时间小于当前时间
                lifeDiscountCouponLambdaQueryWrapper.le(LifeDiscountCoupon::getBeginGetDate, getPureDate(now));
                //结束时间大于当前时间
                lifeDiscountCouponLambdaQueryWrapper.ge(LifeDiscountCoupon::getEndGetDate, getPureDate(now));
                lifeDiscountCouponLambdaQueryWrapper.gt(LifeDiscountCoupon::getSingleQty, 0);

                //不要已暂停关闭领取的
                lifeDiscountCouponLambdaQueryWrapper.eq(LifeDiscountCoupon::getGetStatus, 1);
            } else if (!StringUtils.isEmpty(tab) && tab.equals("2")) {//已结束
                //结束时间小于当前时间
                lifeDiscountCouponLambdaQueryWrapper.lt(LifeDiscountCoupon::getEndGetDate, getPureDate(now));

                //不要已暂停关闭领取的
                lifeDiscountCouponLambdaQueryWrapper.eq(LifeDiscountCoupon::getGetStatus, 1);
            }
            lifeDiscountCouponLambdaQueryWrapper.eq(LifeDiscountCoupon::getStoreId, storeId);
            lifeDiscountCouponLambdaQueryWrapper.orderByDesc(LifeDiscountCoupon::getCreatedTime);
            //根据店铺id查询该店铺的优惠券,状态是开启领取的券
            IPage<LifeDiscountCoupon> lifeDiscountCouponIPage = lifeDiscountCouponMapper.selectPage(iPage, lifeDiscountCouponLambdaQueryWrapper);
            for (LifeDiscountCoupon lifeDiscountCoupon : lifeDiscountCouponIPage.getRecords()) {
                LifeDiscountCouponVo lifeDiscountCouponVo = new LifeDiscountCouponVo();
                lifeDiscountCouponVo.setCouponId(lifeDiscountCoupon.getId());
                //处理一下该优惠券的状态 状态(0:进行中，1:已结束，2:未开始，3:已暂停)
                // 将 Date 转换为 LocalDate
                Instant instant = now.toInstant();
                ZoneId zoneId = ZoneId.systemDefault();
                LocalDate localNow = instant.atZone(zoneId).toLocalDate();
                if (!StringUtils.isEmpty(lifeDiscountCoupon.getBeginGetDate()) && !StringUtils.isEmpty(lifeDiscountCoupon.getEndGetDate())) {
                    int startResult = localNow.compareTo(lifeDiscountCoupon.getBeginGetDate());
                    int endResult = localNow.compareTo(lifeDiscountCoupon.getEndGetDate());
                    //如果当前时间小于开始时间
                    if (lifeDiscountCoupon.getSingleQty() == null || lifeDiscountCoupon.getSingleQty() == 0) {//无库存则已售罄
                        lifeDiscountCouponVo.setStatus(Integer.parseInt(DiscountCouponEnum.HAVE_SOLD_OUT.getValue()));
                    } else if (startResult < 0) {
                        lifeDiscountCouponVo.setStatus(Integer.parseInt(DiscountCouponEnum.HAVE_NOT_STARTED.getValue()));
                    } else if (lifeDiscountCoupon.getGetStatus() == null || lifeDiscountCoupon.getGetStatus().toString().equals(DiscountCouponEnum.NO_GET.getValue())) {
                        lifeDiscountCouponVo.setStatus(Integer.parseInt(DiscountCouponEnum.SUSPEND_GET.getValue()));
                    } else if (endResult > 0) {
                        lifeDiscountCouponVo.setStatus(Integer.parseInt(DiscountCouponEnum.FINISHED.getValue()));
                    } else if (startResult >= 0 && endResult <= 0) {
                        lifeDiscountCouponVo.setStatus(Integer.parseInt(DiscountCouponEnum.UNDER_WAY.getValue()));
                    }
                } else {
                    if (lifeDiscountCoupon.getCouponStatus() == 0) {
                        //如果为草稿则状态为未开始
                        lifeDiscountCouponVo.setStatus(Integer.parseInt(DiscountCouponEnum.HAVE_NOT_STARTED.getValue()));
                    } else {
                        // 开始结束时间为空，数据异常跳过
                        continue;
                    }
                }
                // 查询三个规则
                List<LifeDiscountCouponUnavailableRules> discountCouponId = lifeDiscountCouponUnavailableRulesMapper.selectList(new QueryWrapper<LifeDiscountCouponUnavailableRules>().eq("discount_coupon_id", lifeDiscountCoupon.getId()));
                Map<String, List<LifeDiscountCouponUnavailableRules>> collect = discountCouponId.stream().collect(Collectors.groupingBy(x -> x.getUnavailableRuleType()));
                if (collect.containsKey(DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue())) {
                    lifeDiscountCouponVo.setWeeklyDisabledList(collect.get(DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue()).stream().map(x -> x.getUnavailableRuleValue()).collect(Collectors.toList()));
                }
                if (collect.containsKey(DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue())) {
                    lifeDiscountCouponVo.setHolidayDisabledList(collect.get(DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue()).stream().map(x -> x.getUnavailableRuleValue()).collect(Collectors.toList()));
                }
                if (collect.containsKey(DiscountCouponEnum.CLAIM_RULE.getValue())) {
                    lifeDiscountCouponVo.setClaimRule(collect.get(DiscountCouponEnum.CLAIM_RULE.getValue()).get(0).getUnavailableRuleValue());
                }
                BeanUtils.copyProperties(lifeDiscountCoupon, lifeDiscountCouponVo);
                lifeDiscountCouponVos.add(lifeDiscountCouponVo);
            }
            IPage<LifeDiscountCouponVo> lifeDiscountCouponVoIPage = new Page<>();
            BeanUtils.copyProperties(lifeDiscountCouponIPage, lifeDiscountCouponVoIPage);
            lifeDiscountCouponVoIPage.setRecords(lifeDiscountCouponVos);
            return lifeDiscountCouponVoIPage;
        } else {
            // 查询好友优惠券
            IPage<LifeDiscountCouponStoreFriendVo> iPage = new Page<>(page, size);
            List<LifeDiscountCouponVo> friendLifeDiscountCouponVos = new ArrayList<>();
            QueryWrapper<LifeDiscountCouponStoreFriendVo> friendLifeDiscountCouponQueryWrapper = new QueryWrapper<>();
            //如果couponName不为空，则模糊查询
            if (!StringUtils.isEmpty(couponName)) {
                friendLifeDiscountCouponQueryWrapper.like("ldc.name", couponName);
            }
            //获取当日日期
            Date now = new Date();
            //如果根据类型查询
            if (!StringUtils.isEmpty(tab) && tab.equals("1")) {//进行中
                //开始时间小于当前时间
                friendLifeDiscountCouponQueryWrapper.le("ldc.start_date", getPureDate(now));
                //结束时间大于当前时间
                friendLifeDiscountCouponQueryWrapper.ge("ldc.end_date", getPureDate(now));
                //不要已暂停关闭领取的
                friendLifeDiscountCouponQueryWrapper.eq("ldc.get_status", 1);
            } else if (!StringUtils.isEmpty(tab) && tab.equals("2")) {//已结束
                //结束时间小于当前时间
                friendLifeDiscountCouponQueryWrapper.lt("ldc.end_date", getPureDate(now));

                //不要已暂停关闭领取的
                friendLifeDiscountCouponQueryWrapper.eq("ldc.get_status", 1);
            }
            friendLifeDiscountCouponQueryWrapper.orderByDesc("ldc.created_time");
            friendLifeDiscountCouponQueryWrapper.eq("ldcsf.store_user_id", storeId);
            friendLifeDiscountCouponQueryWrapper.eq("ldcsf.delete_flag", 0);
            IPage<LifeDiscountCouponVo> lifeDiscountCouponIPage = lifeDiscountCouponStoreFriendMapper.selectPage(iPage, friendLifeDiscountCouponQueryWrapper);
            // 查询三个券规则，设置
            for (LifeDiscountCouponVo record : lifeDiscountCouponIPage.getRecords()) {
                LifeDiscountCouponVo lifeDiscountcouponVo = new LifeDiscountCouponVo();
                // 将 Date 转换为 LocalDate
                Instant instant = now.toInstant();
                ZoneId zoneId = ZoneId.systemDefault();
                LocalDate localNow = instant.atZone(zoneId).toLocalDate();
                if (!StringUtils.isEmpty(record.getBeginGetDate()) && !StringUtils.isEmpty(record.getEndGetDate())) {
                    int startResult = localNow.compareTo(record.getBeginGetDate());
                    int endResult = localNow.compareTo(record.getEndGetDate());
                    //如果当前时间小于开始时间
                    if (record.getSingleQty() == null || record.getSingleQty() == 0) {//无库存则已售罄
                        record.setStatus(Integer.parseInt(DiscountCouponEnum.HAVE_SOLD_OUT.getValue()));
                    } else if (record.getGetStatus() == null || record.getGetStatus().toString().equals(DiscountCouponEnum.NO_GET.getValue())) {
                        record.setStatus(Integer.parseInt(DiscountCouponEnum.SUSPEND_GET.getValue()));
                    } else if (startResult < 0) {
                        record.setStatus(Integer.parseInt(DiscountCouponEnum.HAVE_NOT_STARTED.getValue()));
                    } else if (endResult > 0) {
                        record.setStatus(Integer.parseInt(DiscountCouponEnum.FINISHED.getValue()));
                    } else {
                        record.setStatus(Integer.parseInt(DiscountCouponEnum.UNDER_WAY.getValue()));
                    }
                } else {
                    if (record.getCouponStatus() == 0) {
                        //如果为草稿则状态为未开始
                        record.setStatus(Integer.parseInt(DiscountCouponEnum.HAVE_NOT_STARTED.getValue()));
                    } else {
                        // 开始结束时间为空，数据异常跳过
                        continue;
                    }
                }
                List<LifeDiscountCouponUnavailableRules> discountCouponId = lifeDiscountCouponUnavailableRulesMapper.selectList(new QueryWrapper<LifeDiscountCouponUnavailableRules>().eq("discount_coupon_id", record.getCouponId()));
                Map<String, List<LifeDiscountCouponUnavailableRules>> collect = discountCouponId.stream().collect(Collectors.groupingBy(x -> x.getUnavailableRuleType()));
                if (collect.containsKey(DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue())) {
                    record.setWeeklyDisabledList(collect.get(DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue()).stream().map(x -> x.getUnavailableRuleValue()).collect(Collectors.toList()));
                }
                if (collect.containsKey(DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue())) {
                    record.setHolidayDisabledList(collect.get(DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue()).stream().map(x -> x.getUnavailableRuleValue()).collect(Collectors.toList()));
                }
                if (collect.containsKey(DiscountCouponEnum.CLAIM_RULE.getValue())) {
                    record.setClaimRule(collect.get(DiscountCouponEnum.CLAIM_RULE.getValue()).get(0).getUnavailableRuleValue());
                }
                BeanUtils.copyProperties(record, lifeDiscountcouponVo);
                friendLifeDiscountCouponVos.add(lifeDiscountcouponVo);
            }
            IPage<LifeDiscountCouponVo> friendLifeDiscountCouponVoIPage = new Page<>();
            BeanUtils.copyProperties(lifeDiscountCouponIPage, friendLifeDiscountCouponVoIPage);
            friendLifeDiscountCouponVoIPage.setRecords(friendLifeDiscountCouponVos);
            return friendLifeDiscountCouponVoIPage;
        }
    }

    @Override
    public List<LifeDiscountCouponVo> getStoreAllCouponListPaginateNot(String status, String storeId, UserLoginInfo userLoginInfo, int couponStatus) {
        List<LifeDiscountCouponVo> lifeDiscountCouponVos = new ArrayList<>();
        //根据店铺id查询该店铺的优惠券,状态是开启领取的券
        List<LifeDiscountCoupon> lifeDiscountCoupons = lifeDiscountCouponMapper.selectList(new LambdaQueryWrapper<LifeDiscountCoupon>().eq(LifeDiscountCoupon::getStoreId, storeId).eq(LifeDiscountCoupon::getCouponStatus, couponStatus).ne(LifeDiscountCoupon::getSingleQty, 0).orderByDesc(LifeDiscountCoupon::getCreatedTime));
        for (LifeDiscountCoupon lifeDiscountCoupon : lifeDiscountCoupons) {
            LifeDiscountCouponVo lifeDiscountCouponVo = new LifeDiscountCouponVo();
            lifeDiscountCouponVo.setCouponId(lifeDiscountCoupon.getId());
            //处理一下该优惠券的状态 状态(0:进行中，1:已结束，2:未开始，3:已暂停)
            // 获取当前时间
            Date now = new Date();
            // 将 Date 转换为 LocalDate
            Instant instant = now.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDate localNow = instant.atZone(zoneId).toLocalDate();
            if (!StringUtils.isEmpty(lifeDiscountCoupon.getBeginGetDate()) && !StringUtils.isEmpty(lifeDiscountCoupon.getEndGetDate())) {
                int startResult = localNow.compareTo(lifeDiscountCoupon.getBeginGetDate());
                int endResult = localNow.compareTo(lifeDiscountCoupon.getEndGetDate());
                //如果当前时间小于开始时间
                if (lifeDiscountCoupon.getGetStatus().toString().equals(DiscountCouponEnum.NO_GET.getValue())) {
                    lifeDiscountCouponVo.setStatus(Integer.parseInt(DiscountCouponEnum.SUSPEND_GET.getValue()));
                } else if (startResult < 0) {
                    lifeDiscountCouponVo.setStatus(Integer.parseInt(DiscountCouponEnum.HAVE_NOT_STARTED.getValue()));
                } else if (endResult > 0) {
                    lifeDiscountCouponVo.setStatus(Integer.parseInt(DiscountCouponEnum.FINISHED.getValue()));
                } else if (startResult >= 0 && endResult <= 0) {
                    lifeDiscountCouponVo.setStatus(Integer.parseInt(DiscountCouponEnum.UNDER_WAY.getValue()));
                }
            }
            BeanUtils.copyProperties(lifeDiscountCoupon, lifeDiscountCouponVo);
            //如果按照状态查询了
            if (!StringUtils.isEmpty(status) && !status.equals(lifeDiscountCouponVo.getStatus().toString())) {
                continue;
            }
            lifeDiscountCouponVos.add(lifeDiscountCouponVo);
        }
        return lifeDiscountCouponVos;
    }

    @Override
    public String getCouponRule(String couponId, UserLoginInfo userLoginInfo) {
        LifeDiscountCoupon lifeDiscountCoupon = lifeDiscountCouponMapper.selectById(couponId);

        // 获取 Calendar 实例，默认表示当前日期和时间
        Calendar calendar = Calendar.getInstance();
        // 将小时设置为 0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        // 将分钟设置为 0
        calendar.set(Calendar.MINUTE, 0);
        // 将秒设置为 0
        calendar.set(Calendar.SECOND, 0);
        // 将毫秒设置为 0
        calendar.set(Calendar.MILLISECOND, 0);
        // 获取设置后的 Date 对象
        Date midnight = calendar.getTime();

        //判断今日是否为节该券禁用节假日
        //判断当日是否为节假日
        // 获取当前日期
        Date currentDate = new Date();
        // 创建 SimpleDateFormat 对象，指定日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 格式化日期
        String formattedDate = dateFormat.format(currentDate);
        //获取当前年份
        // 获取 Calendar 实例
        // 获取当前年份
        int year = calendar.get(Calendar.YEAR);
        //获取该年份基础节假日数据
        LambdaQueryWrapper<EssentialHolidayComparison> essentialHolidayComparisonLambdaQueryWrapper = new LambdaQueryWrapper<>();
        essentialHolidayComparisonLambdaQueryWrapper.eq(EssentialHolidayComparison::getParticularYear, year);
        List<EssentialHolidayComparison> essentialHolidayComparisons = essentialHolidayComparisonMapper.selectList(essentialHolidayComparisonLambdaQueryWrapper);
        String festivalName = "";
        for (EssentialHolidayComparison essentialHolidayComparison : essentialHolidayComparisons) {
            if (essentialHolidayComparison.getFestivalDate().equals(formattedDate)) {
                festivalName = essentialHolidayComparison.getFestivalName();
            }
        }
        //获取今日周几
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        String dayName = "";
        switch (dayOfWeek) {
            case Calendar.SUNDAY:
                dayName = "日";
                break;
            case Calendar.MONDAY:
                dayName = "一";
                break;
            case Calendar.TUESDAY:
                dayName = "二";
                break;
            case Calendar.WEDNESDAY:
                dayName = "三";
                break;
            case Calendar.THURSDAY:
                dayName = "四";
                break;
            case Calendar.FRIDAY:
                dayName = "五";
                break;
            case Calendar.SATURDAY:
                dayName = "六";
                break;
        }

        LifeDiscountCouponVo lifeDiscountCouponVo = new LifeDiscountCouponVo();
        //该券的节假日禁用规则
        LambdaQueryWrapper<LifeDiscountCouponUnavailableRules> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LifeDiscountCouponUnavailableRules::getDiscountCouponId, lifeDiscountCoupon.getId());
        lambdaQueryWrapper.eq(LifeDiscountCouponUnavailableRules::getUnavailableRuleType, DiscountCouponEnum.HOLIDAY_UNAVAILABLE.getValue());
        List<LifeDiscountCouponUnavailableRules> lifeDiscountCouponUnavailableRules = lifeDiscountCouponUnavailableRulesMapper.selectList(lambdaQueryWrapper);
        String forbiddenRule = "";
        if (lifeDiscountCouponUnavailableRules.size() > 0) {
            forbiddenRule += HOLIDAY_RULE_TITLE;
        }
        List<String> holidCoupnDis = lifeDiscountCouponUnavailableRules.stream().map(rule -> rule.getUnavailableRuleValue()).collect(Collectors.toList());
        forbiddenRule += String.join(", ", holidCoupnDis);
        for (LifeDiscountCouponUnavailableRules lifeDiscountCouponUnavailableRule : lifeDiscountCouponUnavailableRules) {
            //如果当日为节假日，并且该券该节假日也禁用，则该券禁用
            if (StringUtils.isEmpty(festivalName) && festivalName.equals(lifeDiscountCouponUnavailableRule.getUnavailableRuleValue())) {
                lifeDiscountCouponVo.setDisabledStatus(false);
            }
        }
        if (lifeDiscountCouponUnavailableRules.size() > 0) {
            forbiddenRule += NOT_USE;
        }
        //该券的周中禁用规则
        LambdaQueryWrapper<LifeDiscountCouponUnavailableRules> lambdaQueryWeekWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWeekWrapper.eq(LifeDiscountCouponUnavailableRules::getDiscountCouponId, lifeDiscountCoupon.getId());
        lambdaQueryWeekWrapper.eq(LifeDiscountCouponUnavailableRules::getUnavailableRuleType, DiscountCouponEnum.WEEKDAY_UNAVAILABLE.getValue());
        List<LifeDiscountCouponUnavailableRules> weekLifeDiscountCouponUnavailableRules = lifeDiscountCouponUnavailableRulesMapper.selectList(lambdaQueryWeekWrapper);
        if (weekLifeDiscountCouponUnavailableRules.size() > 0) {
            forbiddenRule += WEEK_RULE_TITLE;
        }
        List<String> weekCoupnDis = weekLifeDiscountCouponUnavailableRules.stream().map(rule -> rule.getUnavailableRuleValue()).collect(Collectors.toList());
        forbiddenRule += String.join(", ", weekCoupnDis);
        for (LifeDiscountCouponUnavailableRules lifeDiscountCouponUnavailableRule : lifeDiscountCouponUnavailableRules) {
            //该券该周中日也禁用，则该券禁用
            if (dayName.equals(lifeDiscountCouponUnavailableRule.getUnavailableRuleValue())) {
                lifeDiscountCouponVo.setDisabledStatus(false);
            }
        }
        if (weekLifeDiscountCouponUnavailableRules.size() > 0) {
            forbiddenRule += NOT_USE;
        }
        forbiddenRule += MINIMUM_SPENDING_AMOUNT + lifeDiscountCoupon.getMinimumSpendingAmount();
        lifeDiscountCouponVo.setForbiddenRule(forbiddenRule);

        return forbiddenRule;
    }

    /**
     * <p>
     * 获取优惠券状态
     * </p>
     *
     * @author zhangchen
     * @since 2025-07-14
     */
    @Override
    public LifeCouponStatusVo getCouponStatus(String couponId) {
        if (couponId == null || couponId.isEmpty()) {
            log.error("获取优惠券状态失败：couponId为空");
            throw new IllegalArgumentException("couponId不能为空");
        }
        try {
            LifeDiscountCoupon lifeDiscountCoupon = lifeDiscountCouponMapper.selectById(couponId);
            LifeCouponStatusVo lifeCouponStatusVo = new LifeCouponStatusVo();
            if (lifeDiscountCoupon != null) {
                lifeCouponStatusVo.setCouponId(couponId);
                lifeCouponStatusVo.setSingleQty(lifeDiscountCoupon.getSingleQty());
            } else {
                // 可选：设置默认值或标记状态为无效
                lifeCouponStatusVo.setSingleQty(0); // 或其他默认逻辑
            }
            return lifeCouponStatusVo;
        } catch (Exception e) {
            log.error("获取优惠券状态失败: {}", e);
            throw new RuntimeException("获取优惠券状态失败", e); // 或者根据项目规范处理
        }
    }

    /**
     * 获取平台优惠券列表
     *
     * @param pageNum    分页页码
     * @param pageSize   分页大小
     * @param name       优惠券名称
     * @param createDate 创建时间
     * @param couponType 类型:(1:优惠券, 2:红包, 3:平台优惠券)
     * @return IPage<LifeDiscountCoupon>
     */
    @Override
    public IPage<LifeDiscountCoupon> getPlatformCouponList(Integer pageNum, Integer pageSize, String name, String createDate, Integer couponType) {
        IPage<LifeDiscountCoupon> iPage = new Page<>(pageNum, pageSize);
        return lifeDiscountCouponMapper.selectPage(iPage, new LambdaQueryWrapper<LifeDiscountCoupon>()
                .eq(LifeDiscountCoupon::getType, 3)
                .like(!StringUtils.isEmpty(name), LifeDiscountCoupon::getName, name)
                .orderByDesc(LifeDiscountCoupon::getCreatedTime)
                .between(!StringUtils.isEmpty(createDate), LifeDiscountCoupon::getCreatedTime, createDate + " 00:00:00", createDate + " 23:59:59"));
    }

    /**
     * 新增平台优惠券
     *
     * @param lifeDiscountCoupon 优惠券实体
     * @return boolean
     */
    @Override
    public boolean addPlatformCoupon(LifeDiscountCoupon lifeDiscountCoupon) {
        lifeDiscountCoupon.setStartDate(LocalDate.now());
        if (null == lifeDiscountCoupon.getSpecifiedDay()) {
            return false;
        }
        lifeDiscountCoupon.setEndDate(LocalDate.now().plusDays(Long.parseLong(lifeDiscountCoupon.getSpecifiedDay())));
        lifeDiscountCoupon.setType(3);
        lifeDiscountCoupon.setGetStatus(1);
//        lifeDiscountCoupon.setMinimumSpendingAmount(BigDecimal.ZERO);
        return this.save(lifeDiscountCoupon);
    }

    /**
     * 查询平台优惠券详情
     *
     * @param id 柱间, 妈的就叫这个吧! 哈西辣妈!
     * @return LifeDiscountCoupon
     */
    @Override
    public LifeDiscountCoupon getOnePlatformCoupon(Integer id) {
        return this.getById(id);
    }

    /**
     * 发放平台优惠券
     *
     * @param userIds  用户主键List
     * @param couponId 优惠券id
     * @return boolean
     */
    @Override
    public boolean issuePlatformCoupon(List<Integer> userIds, Integer couponId) {
        LifeDiscountCoupon lifeDiscountCoupon = this.getById(couponId);
        if (lifeDiscountCoupon == null) {
            //如果优惠券为空，则返回失败
            return false;
        }
        List<LifeDiscountCouponUser> lifeDiscountCouponUsers = new ArrayList<>();
        for (Integer userId : userIds) {
            LifeDiscountCouponUser lifeDiscountCouponUser = new LifeDiscountCouponUser();
            lifeDiscountCouponUser.setUserId(userId);
            lifeDiscountCouponUser.setCouponId(couponId);
            lifeDiscountCouponUser.setReceiveTime(new Date());
            lifeDiscountCouponUser.setExpirationTime(LocalDate.now().plusDays(Long.parseLong(lifeDiscountCoupon.getSpecifiedDay())));
            lifeDiscountCouponUser.setStatus(0);
            lifeDiscountCouponUser.setDeleteFlag(0);
            lifeDiscountCouponUser.setCreatedTime(new Date());
            lifeDiscountCouponUsers.add(lifeDiscountCouponUser);
        }
        return lifeDiscountCouponUserService.saveBatch(lifeDiscountCouponUsers);
    }

    /**
     * 收回平台优惠券
     *
     * @param couponId 优惠券id
     * @return boolean
     */
    @Override
    public boolean recoverPlatformCoupon(Integer couponId) {
        return lifeDiscountCouponUserService.remove(new LambdaQueryWrapper<LifeDiscountCouponUser>().eq(LifeDiscountCouponUser::getCouponId, couponId));
    }

    /**
     * 删除平台优惠券
     *
     * @param couponId 优惠券id
     * @return boolean
     */
    @Override
    public boolean deletePlatformCoupon(Integer couponId) {
        int count = lifeDiscountCouponUserService.count(new LambdaQueryWrapper<LifeDiscountCouponUser>().eq(LifeDiscountCouponUser::getCouponId, couponId));
        if (count > 0) {
            return false;
        } else {
            return this.removeById(couponId);
        }
    }

    /**
     * 分页查询该券发放记录
     *
     * @param pageNum  分页页码
     * @param pageSize 分页大小
     * @param userName 用户名
     * @param phone    手机号
     * @param couponId 优惠券id
     * @return IPage<LifeDiscountCouponUserWebVo>
     */
    @Override
    public IPage<LifeDiscountCouponUserWebVo> getPlatformCouponLogList(Integer pageNum, Integer pageSize, String userName, String phone, Integer couponId) {
        IPage<LifeDiscountCouponUserWebVo> iPage = new Page<>(pageNum, pageSize);
        return lifeDiscountCouponUserMapper.selectPageList(iPage, new QueryWrapper<LifeDiscountCouponUserWebVo>()
                .eq(!StringUtils.isEmpty(couponId), "a.coupon_id", couponId)
                .like(!StringUtils.isEmpty(userName), "b.real_name", userName)
                .like(!StringUtils.isEmpty(phone), "b.user_phone", phone));
    }

    @Override
    public List<LifeDiscountCoupon> getPlatformCoupon(Integer couponId, Integer couponType) {
        LambdaQueryWrapper<LifeDiscountCoupon> lifeDiscountCouponLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (couponId != null) {
            lifeDiscountCouponLambdaQueryWrapper.eq(LifeDiscountCoupon::getId, couponId);
        }
        if (couponType != null) {
            lifeDiscountCouponLambdaQueryWrapper.eq(LifeDiscountCoupon::getType, couponType);
        }
        lifeDiscountCouponLambdaQueryWrapper.eq(LifeDiscountCoupon::getDeleteFlag, 0);
        lifeDiscountCouponLambdaQueryWrapper.eq(LifeDiscountCoupon::getGetStatus, 1);
        return lifeDiscountCouponMapper.selectList(lifeDiscountCouponLambdaQueryWrapper);
    }
}
