package com.hai.schedule;

import com.hai.entity.HighDiscountAgentCode;
import com.hai.entity.HighDiscountUserRel;
import com.hai.entity.HighOrder;
import com.hai.service.HighDiscountAgentCodeService;
import com.hai.service.HighDiscountUserRelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: 胡锐
 * @Description: 优惠券定时任务
 * @Date: 2021/4/4 22:43
 */
@Configuration
public class HighDiscountSchedule {

    private static Logger log = LoggerFactory.getLogger(HighDiscountSchedule.class);

    @Resource
    private HighDiscountUserRelService highDiscountUserRelService;

    @Resource
    private HighDiscountAgentCodeService highDiscountAgentCodeService;

    /**
     * @Author 胡锐
     * @Description 处理过期的优惠券
     * @Date 2021/4/4 22:44
     **/
    //@Scheduled(cron = "5 0 0 * * ?")   //每日凌晨12点5秒执行一次
    @Scheduled(cron="0 0/1 * * * ?")   //每1分钟执行一次
    public void expiredDiscount() {
        List<HighDiscountUserRel> expiredDiscount = highDiscountUserRelService.getExpiredDiscount();
        for (HighDiscountUserRel rel : expiredDiscount) {
            try {
                rel.setStatus(0);
                highDiscountUserRelService.updateDiscountUserRel(rel);

                // 二维码
                HighDiscountAgentCode code = highDiscountAgentCodeService.getCodeById(rel.getDiscountAgentCodeId());
                if (code != null) {
                    code.setStatus(4);  // 状态 0：删除  1：待领取  2：待使用  3：已使用 4：已过期
                    highDiscountAgentCodeService.updateCode(code);
                }
            } catch (Exception e) {
                log.error("HighCouponSchedule --> expiredCoupon() error!", e);
            }
        }
    }

}
