package com.hfkj.schedule;

import com.hfkj.entity.BsDiscount;
import com.hfkj.service.discount.BsDiscountService;
import com.hfkj.service.discount.BsDiscountUserService;
import com.hfkj.sysenum.discount.DiscountStatusEnum;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: DiscountSchedule
 * @author: HuRui
 * @date: 2024/9/4
 **/
@Component
public class DiscountSchedule {

    @Resource
    private BsDiscountUserService discountUserService;
    @Resource
    private BsDiscountService discountService;
    /**
     * 用户优惠券过期
     */
    @Scheduled(cron="0 0/1 * * * ?")   //每分钟执行一次
    public void userDiscountExpiration() {
        try {
            // 更新过期数据
            discountUserService.expiration();;

        } catch (Exception e) {
            System.out.println("更新失败！！！");
        }
    }

    /**
     * 优惠券过期
     */
    @Scheduled(cron="0 0/30 * * * ?")   //每30分钟执行一次
    public void discountExpiration() {
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("status", DiscountStatusEnum.status2.getCode());
            param.put("endTimeE", System.currentTimeMillis());

            List<BsDiscount> list = discountService.getList(param);
            for (BsDiscount discount : list) {
                discountService.done(discount.getDiscountNo());
            }

        } catch (Exception e) {
            System.out.println("更新失败！！！");
        }
    }

}
