package com.ruoyi.hezhi.scheduledTask;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

/**
 *  订单定时任务
 */
@Component
public class ScheduledTaskUtils {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());



    private static final String ORDER_CANCEL_LOCK = "ORDER_CANCEL_LOCK";



    @Resource
    private ScheduledTaskService scheduledTaskService;

    @Resource
    private RedisCache redisCache;






    /**
     * 订单超时未支付取消
     * 每分钟执行一次
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void orderCancelTask(){
        Date nowDate = DateUtils.getNowDate();
        if(!redisCache.hasKey(ORDER_CANCEL_LOCK)){
            // 执行任务
            logger.info("定时任务: 考试订单超时未支付取消 任务开始执行 {}", nowDate);
            scheduledTaskService.orderCancelTask(ORDER_CANCEL_LOCK);
        }else{
            logger.info("定时任务: 考试订单超时未支付取消 退回{" + ORDER_CANCEL_LOCK + ":{}}", nowDate);
        }

    }



}
