package com.qmth.sop.task.start;

import com.qmth.sop.business.cache.CommonCacheService;
import com.qmth.sop.business.entity.SysConfig;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.ExceptionResultEnum;
import com.qmth.sop.common.enums.JobEnum;
import com.qmth.sop.task.job.*;
import com.qmth.sop.task.service.QuartzService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @Description: 服务启动时初始化运行，哪个微服务模块需要则拿此模版去用
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/7/3
 */
@Component
public class StartRunning implements CommandLineRunner {
    private final static Logger log = LoggerFactory.getLogger(StartRunning.class);

    @Resource
    QuartzService quartzService;

    @Resource
    CommonCacheService commonCacheService;

    @Override
    public void run(String... args) throws Exception {
        log.info("服务器启动时执行 start");

        SysConfig sysConfig = commonCacheService.addSysConfigCache(SystemConstant.REMIND_JOB_POLL_TIME);
        Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("任务提醒轮询时间未设置"));

        log.info("增加序列重置定时任务 start");
        Map sequenceMap = new HashMap();
        sequenceMap.put(SystemConstant.NAME, SequenceJob.class.getName());
        quartzService.deleteJob(JobEnum.SEQUENCE_JOB.name(), JobEnum.SEQUENCE_JOB_GROUP.name());
//        quartzService.addJob(SequenceJob.class, JobEnum.SEQUENCE_JOB.name(), JobEnum.SEQUENCE_JOB_GROUP.name(), "0 0/1 * * * ?", sequenceMap);
        quartzService.addJob(SequenceJob.class, JobEnum.SEQUENCE_JOB.name(), JobEnum.SEQUENCE_JOB_GROUP.name(), "0 0 0 * * ?", sequenceMap);
        log.info("增加序列重置定时任务 end");

        log.info("增加流程任务提醒定时任务 start");
        Map flowTaskRemindJobMap = new HashMap();
        flowTaskRemindJobMap.computeIfAbsent(SystemConstant.NAME, v -> FlowTaskRemindJob.class.getName());
        quartzService.deleteJob(JobEnum.FLOW_TASK_REMIND_JOB.name(), JobEnum.FLOW_TASK_REMIND_JOB_GROUP.name());
        //每天0点定时任务
//        quartzService.addJob(FlowTaskRemindJob.class, JobEnum.FLOW_TASK_REMIND_JOB.name(), JobEnum.FLOW_TASK_REMIND_JOB_GROUP.name(), "0 0/1 * * * ?", flowTaskRemindJobMap);
        quartzService.addJob(FlowTaskRemindJob.class, JobEnum.FLOW_TASK_REMIND_JOB.name(), JobEnum.FLOW_TASK_REMIND_JOB_GROUP.name(), sysConfig.getConfigValue(), flowTaskRemindJobMap);
        log.info("增加流程任务提醒定时任务 end");

        log.info("fxxk派单同步定时任务 start");
        Map<Object,Object> fxxkCrmSyncMap = new HashMap<>();
        fxxkCrmSyncMap.put(SystemConstant.NAME, FxxkCrmSyncJob.class.getName());
        quartzService.deleteJob(JobEnum.SYNC_FXXK_CRM.name(), JobEnum.SYNC_FXXK_CRM_GROUP.name());
        quartzService.addJob(FxxkCrmSyncJob.class, JobEnum.SYNC_FXXK_CRM.name(), JobEnum.SYNC_FXXK_CRM_GROUP.name(), "0 0/5 6-23 * * ?", fxxkCrmSyncMap);
        log.info("fxxk派单同步定时任务 end");

        log.info("rtzf用户推送定时任务 start");
        Map<Object,Object> rtzfUserPushMap = new HashMap<>();
        rtzfUserPushMap.put(SystemConstant.NAME, RtzfUserPushJob.class.getName());
        quartzService.deleteJob(JobEnum.SYNC_RTZF_USER_PUSH.name(), JobEnum.SYNC_RTZF_USER_PUSH_GROUP.name());
        quartzService.addJob(RtzfUserPushJob.class, JobEnum.SYNC_RTZF_USER_PUSH.name(), JobEnum.SYNC_RTZF_USER_PUSH_GROUP.name(), "0 0/5 6-23 * * ?", rtzfUserPushMap);
        log.info("rtzf用户推送定时任务 end");

        log.info("rtzf用户更新定时任务 start");
        Map<Object,Object> rtzfUserUpdateMap = new HashMap<>();
        rtzfUserUpdateMap.put(SystemConstant.NAME, RtzfUserUpdateJob.class.getName());
        quartzService.deleteJob(JobEnum.SYNC_RTZF_USER_UPDATE.name(), JobEnum.SYNC_RTZF_USER_UPDATE_GROUP.name());
        quartzService.addJob(RtzfUserUpdateJob.class, JobEnum.SYNC_RTZF_USER_UPDATE.name(), JobEnum.SYNC_RTZF_USER_UPDATE_GROUP.name(), "0 2/5 6-23 * * ?", rtzfUserUpdateMap);
        log.info("rtzf用户更新定时任务 end");

        log.info("自动打卡定时任务 start");
        Map<Object,Object> autoAddDingRecordMap = new HashMap<>();
        autoAddDingRecordMap.put(SystemConstant.NAME, AutoAddDingRecordJob.class.getName());
        quartzService.deleteJob(JobEnum.AUTO_ADD_DING_RECORD.name(), JobEnum.AUTO_ADD_DING_RECORD_GROUP.name());
        quartzService.addJob(AutoAddDingRecordJob.class, JobEnum.AUTO_ADD_DING_RECORD.name(), JobEnum.AUTO_ADD_DING_RECORD_GROUP.name(), "0 30 23 * * ?", autoAddDingRecordMap);
        log.info("自动打卡定时任务 end");

        log.info("服务器启动时执行 end");
    }
}
