package com.ruoyi.scheduledTask;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.cs.service.ICsPricingInventoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 定时任务服务
 */
@Component
public class ScheduledTaskService {

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



    @Resource
    private RedisCache redisCache;

    @Resource
    private ICsPricingInventoryService csPricingInventoryService;

    /**
     *  testTask
     * @param redisKey redis锁
     */
    @Transactional(rollbackFor =Exception.class )
    public void  testTask(String redisKey){
        try{
                redisCache.setCacheObject(redisKey, 0);
                System.out.println("测试定时任务++++执行");


        }catch(Exception e){
            e.printStackTrace();
            logger.error(DateUtils.getNowDate()+"->定时任务执行异常:" + "测试");
        }finally {
            logger.info(DateUtils.getNowDate()+"->定时任务执行结束："+"测试");
            // 释放锁
            redisCache.deleteObject(redisKey);
        }

    }



    /**
     * 账户到期提醒
     * @param redisKey redis锁
     */
    @Transactional(rollbackFor =Exception.class )
    public void accountExpirationTask(String redisKey){
        try {

            // 查询 待 账户到期列表
            int i = csPricingInventoryService.countAccountExpirationList();
            if(i > 0){
                // redis锁
                redisCache.setCacheObject(redisKey, 0);
                csPricingInventoryService.editAccountExpirationList();
            }
        }catch(Exception e){
            e.printStackTrace();
            logger.error(DateUtils.getNowDate()+"->定时任务执行异常："+"账户到期提醒");
        }finally {
            logger.info(DateUtils.getNowDate()+"->定时任务执行结束："+"账户到期提醒");
            // 释放锁
            redisCache.deleteObject(redisKey);
        }
    }



}
