package com.czg.task;

import cn.hutool.core.collection.CollUtil;
import com.czg.config.RedisCst;
import com.czg.market.dto.ShopCouponDTO;
import com.czg.market.entity.MkPointsGoods;
import com.czg.market.entity.MkShopCouponRecord;
import com.czg.market.entity.ShopCoupon;
import com.czg.market.service.MkPointsGoodsService;
import com.czg.market.service.MkShopCouponRecordService;
import com.czg.market.service.ShopCouponService;
import com.czg.service.RedisService;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单定时任务
 *
 * @author ww
 * @description
 */
@Slf4j
@Component
public class CouponTask {
    @Resource
    private MkShopCouponRecordService mkShopCouponRecordService;
    @Resource
    private ShopCouponService shopCouponService;
    @Resource
    private RedisService redisService;
    @Resource
    private MkPointsGoodsService pointsGoodsService;

    /**
     * 优惠券 过期
     */
//    @Scheduled(cron = "0 30 * * * ? ")
    public void task() {
        try {
            //优惠券 到期  积分商品同步下架
            LocalDateTime now = LocalDateTime.now();
            QueryWrapper queryWrapper = QueryWrapper.create().eq(ShopCoupon::getStatus, 1)
                    .eq(ShopCoupon::getValidType, "custom")
                    .lt(ShopCoupon::getValidEndTime, now);

            List<Long> ids = shopCouponService.listAs(queryWrapper, Long.class);
            shopCouponService.update(new ShopCoupon().setStatus(0), queryWrapper);
            if (CollUtil.isNotEmpty(ids)) {
                MkPointsGoods upMkPointsGoods = new MkPointsGoods();
                upMkPointsGoods.setStatus(0);
                pointsGoodsService.update(upMkPointsGoods, QueryWrapper.create().in(MkPointsGoods::getCouponId, ids));
            }

            mkShopCouponRecordService.update(new MkShopCouponRecord().setStatus(2), new QueryWrapper()
                    .eq(MkShopCouponRecord::getStatus, 0)
                    .le(MkShopCouponRecord::getUseEndTime, now)
            );
            // 计算当前时间加一小时
            LocalDateTime oneHourLater = now.plusHours(1);
            List<MkShopCouponRecord> list = mkShopCouponRecordService.list(new QueryWrapper()
                    .eq(MkShopCouponRecord::getStatus, 0)
                    .le(MkShopCouponRecord::getUseEndTime, oneHourLater)
            );
            for (MkShopCouponRecord mkShopCouponRecord : list) {
                LocalDateTime endTime = mkShopCouponRecord.getUseEndTime();
                if (endTime != null) {
                    Duration duration = Duration.between(now, endTime);
                    long seconds = duration.getSeconds();
                    redisService.set(RedisCst.classKeyExpired.EXPIRED_COUPON + mkShopCouponRecord.getId(), mkShopCouponRecord.getId(), seconds);
                }
            }
        } catch (Exception e) {
            log.error("优惠券 过期 任务 异常", e);
        }
    }

}
