package com.sifang.modules.task;

import com.google.gson.Gson;
import com.jfinal.plugin.activerecord.Db;
import com.sifang.modules.sunshine.settleSet.service.CreateRewardsPunishSettleService;
import com.sifang.modules.sunshine.settleSet.vo.JobSaveLogVo;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.common.utils.IdGen;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class RewardsPunishSettleTask {

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

    @Autowired
    private CreateRewardsPunishSettleService rewardsPunishSettleService;

    //每月1号凌晨2点20分执行 计算奖励政策
    @Scheduled(cron = "0 20 2 1 * ?")
    void rewardsPunishSettle(){
        logger.info(">>>>>>>>rewardsPunishSettle  定时任务计算奖励金额开始 time:{}", DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
        long begin = System.currentTimeMillis();
        Gson gson=new Gson();
        try{
            List<JobSaveLogVo> logVos= rewardsPunishSettleService.rewardsPunishSettle();
            long end = System.currentTimeMillis();
            long hs= (end-begin) / 1000;
            logger.info("rewardsPunishSettle 定时任务结束，共耗时：[" + hs + "]秒");
            Db.update("INSERT INTO `crm_time_job_log` VALUES(?,NOW(),?,?,?,?) ", IdGen.uuid(),gson.toJson(logVos),"执行成功，耗时:"+hs,"","0");
        }catch (Exception e){
            rewardsPunishSettleService.saveNoCaseException(gson.toJson(e));
            logger.error("rewardsPunishSettle e:",e);
        }
    }

    //每天上午1:00触发  计算一下奖惩政策 是否失效（目前无用，手动失效了）
  /*  @Scheduled(cron = "0 0 1 ? * * ")*/
    void ifRewardsPunishSettleInvalid(){
        logger.info(">>>>>>>>ifRewardsPunishSettleInvalid  定时任务开始");
        long begin = System.currentTimeMillis();
        rewardsPunishSettleService.ifRewardsPunishSettleInvalid();
        long end = System.currentTimeMillis();
        logger.info("ifRewardsPunishSettleInvalid 定时任务结束，共耗时：[" + (end-begin) / 1000 + "]秒");
    }

}
