package com.qmth.teachcloud.task.start;

import com.qmth.distributed.print.business.service.PrintCommonService;
import com.qmth.teachcloud.task.enums.JobEnum;
import com.qmth.teachcloud.task.job.*;
import com.qmth.teachcloud.task.job.service.JobService;
import com.qmth.teachcloud.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;

/**
 * @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
    JobService jobService;

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

        log.info("增加任务到期提醒定时任务 start");
        Map expireJobMap = new HashMap();
        expireJobMap.computeIfAbsent("name", v -> SendSmsExpireJob.class.getName());
        quartzService.deleteJob(JobEnum.SMS_NOTICE_TASK_EXPIRE_JOB.name(), JobEnum.SMS_NOTICE_TASK_EXPIRE_JOB.getGroupName());
        // 每天15点定时任务
        quartzService.addJob(SendSmsExpireJob.class, JobEnum.SMS_NOTICE_TASK_EXPIRE_JOB.name(), JobEnum.SMS_NOTICE_TASK_EXPIRE_JOB.getGroupName(), "0 0 15 * * ?", expireJobMap);
        log.info("增加任务到期提醒定时任务 end");

        log.info("增加任务逾期提醒定时任务 start");
        Map orverdueJobMap = new HashMap();
        orverdueJobMap.computeIfAbsent("name", v -> SendSmsOverdueJob.class.getName());
        quartzService.deleteJob(JobEnum.SMS_NOTICE_TASK_OVERDUE_JOB.name(), JobEnum.SMS_NOTICE_TASK_OVERDUE_JOB.getGroupName());
        // 每天9点定时任务
        quartzService.addJob(SendSmsOverdueJob.class, JobEnum.SMS_NOTICE_TASK_OVERDUE_JOB.name(), JobEnum.SMS_NOTICE_TASK_OVERDUE_JOB.getGroupName(), "0 0 9 * * ?", orverdueJobMap);
        log.info("增加任务逾期提醒定时任务 end");

        log.info("增加短信发送失败重发定时任务 start");
        Map rensendJobMap = new HashMap();
        rensendJobMap.computeIfAbsent("name", v -> ResendSmsJob.class.getName());
        quartzService.deleteJob(JobEnum.SMS_NOTICE_TASK_RESEND_JOB.name(), JobEnum.SMS_NOTICE_TASK_RESEND_JOB.getGroupName());
        // 每隔1小时定时任务
        quartzService.addJob(ResendSmsJob.class, JobEnum.SMS_NOTICE_TASK_RESEND_JOB.name(), JobEnum.SMS_NOTICE_TASK_RESEND_JOB.getGroupName(), "0 0/30 * * * ?", rensendJobMap);
        log.info("增加短信发送失败重发定时任务 end");

        log.info("增加mq信息同步定时任务 start");
        Map redisMqJobMap = new HashMap();
        redisMqJobMap.computeIfAbsent("name", v -> RedisMqSyncJob.class.getName());
        quartzService.deleteJob(JobEnum.REDIS_MQ_JOB.name(), JobEnum.REDIS_MQ_JOB.getGroupName());
        // 每分钟定时任务
        quartzService.addJob(RedisMqSyncJob.class, JobEnum.REDIS_MQ_JOB.name(), JobEnum.REDIS_MQ_JOB.getGroupName(), "0 0/1 * * * ?", redisMqJobMap);
        log.info("增加mq信息同步定时任务 end");

        // 每天0点开始，每2小时一次
        log.info("增加自动统分定时任务 start");
        Map reunifyJobMap = new HashMap();
        reunifyJobMap.computeIfAbsent("name", v -> SubjectCalculateJob.class.getName());
        quartzService.deleteJob(JobEnum.SYNC_REUNIFY_JOB.name(), JobEnum.SYNC_REUNIFY_JOB.getGroupName());
        quartzService.addJob(SubjectCalculateJob.class, JobEnum.SYNC_REUNIFY_JOB.name(), JobEnum.SYNC_REUNIFY_JOB.getGroupName(), "0 0 0/2 * * ?", reunifyJobMap);
        log.info("增加自动统分定时任务 end");

        // 每1分钟一次
        log.info("增加更新评卷员质量监控指标定时任务 start");
        Map qualityJobMap = new HashMap();
        qualityJobMap.computeIfAbsent("name", v -> UpdateMarkerQualityJob.class.getName());
        quartzService.deleteJob(JobEnum.UPDATE_MARKER_QUALITY.name(), JobEnum.UPDATE_MARKER_QUALITY.getGroupName());
        quartzService.addJob(UpdateMarkerQualityJob.class, JobEnum.UPDATE_MARKER_QUALITY.name(), JobEnum.UPDATE_MARKER_QUALITY.getGroupName(), "0 */1 * * * ?", qualityJobMap);
        log.info("增加更新评卷员质量监控指标定时任务 end");

        // 每1分钟一次
        log.info("增加评卷任务生成定时任务 start");
        Map buildMarkTaskJobMap = new HashMap();
        buildMarkTaskJobMap.computeIfAbsent("name", v -> BuildMarkTaskJob.class.getName());
        quartzService.deleteJob(JobEnum.BUILD_MARK_TASK.name(), JobEnum.BUILD_MARK_TASK.getGroupName());
        quartzService.addJob(BuildMarkTaskJob.class, JobEnum.BUILD_MARK_TASK.name(), JobEnum.BUILD_MARK_TASK.getGroupName(), "0 */1 * * * ?", buildMarkTaskJobMap);
        log.info("增加评卷任务生成定时任务 end");

        // 每1分钟一次
        log.info("增加初始化阅卷数据定时任务 start");
        Map initMarkDataJobMap = new HashMap();
        initMarkDataJobMap.computeIfAbsent("name", v -> InitMarkDataJob.class.getName());
        quartzService.deleteJob(JobEnum.INIT_MARK_DATA.name(), JobEnum.INIT_MARK_DATA.getGroupName());
        quartzService.addJob(InitMarkDataJob.class, JobEnum.INIT_MARK_DATA.name(), JobEnum.INIT_MARK_DATA.getGroupName(), "0 */1 * * * ?", initMarkDataJobMap);
        log.info("增加初始化阅卷数据定时任务 end");

        // 每10分钟一次
        log.info("增加清空过期任务定时任务 start");
        Map clearTimeoutTaskJobMap = new HashMap();
        clearTimeoutTaskJobMap.computeIfAbsent("name", v -> ClearTimeoutTaskJob.class.getName());
        quartzService.deleteJob(JobEnum.CLEAR_TIMEOUT_TASK.name(), JobEnum.CLEAR_TIMEOUT_TASK.getGroupName());
        quartzService.addJob(ClearTimeoutTaskJob.class, JobEnum.CLEAR_TIMEOUT_TASK.name(), JobEnum.CLEAR_TIMEOUT_TASK.getGroupName(), "0 0/10 6-23 * * ?", clearTimeoutTaskJobMap);
        log.info("增加清空过期任务定时任务 end");

        // 每2分钟一次
        log.info("增加PDF生成定时任务 start");
        Map createPdfJobMap = new HashMap();
        createPdfJobMap.computeIfAbsent("name", v -> CreatePdfTaskJob.class.getName());
        quartzService.deleteJob(JobEnum.CREATE_PDF_JOB.name(), JobEnum.CREATE_PDF_JOB.getGroupName());
        quartzService.addJob(CreatePdfTaskJob.class, JobEnum.CREATE_PDF_JOB.name(), JobEnum.CREATE_PDF_JOB.getGroupName(), "0 0/2 * * * ?", createPdfJobMap);
        log.info("增加清空过期任务定时任务 end");

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