package com.qmth.teachcloud.task.start;

import com.qmth.distributed.print.business.service.PrintCommonService;
import com.qmth.teachcloud.common.contant.SystemConstant;
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;

    @Resource
    PrintCommonService printCommonService;

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

        log.info("增加学校信息同步定时任务 start");
        Map schoolJobMap = new HashMap();
        schoolJobMap.computeIfAbsent("name", v -> TimedSyncSchoolJob.class.getName());
        quartzService.deleteJob(JobEnum.TIMED_SYNC_SCHOOL_JOB.name(), JobEnum.TIMED_TASK_JOB_GROUP.name());
        // 每天0点定时任务
        quartzService.addJob(TimedSyncSchoolJob.class, JobEnum.TIMED_SYNC_SCHOOL_JOB.name(), JobEnum.TIMED_TASK_JOB_GROUP.name(), "0 0 0 * * ?", schoolJobMap);
        log.info("增加学校信息同步定时任务 end");

        log.info("增加用户推送失败再次推送定时任务 start");
        Map userPushErrorMap = new HashMap();
        userPushErrorMap.computeIfAbsent("name", v -> TimedSyncUserPushErrorJob.class.getName());
        quartzService.deleteJob(JobEnum.TIMED_SYNC_USER_PUSH_ERROR_JOB.name(), JobEnum.TIMED_SYNC_USER_PUSH_ERROR_JOB_GROUP.name());
        // 用户推送失败再次推送 每分钟推送一次
        quartzService.addJob(TimedSyncUserPushErrorJob.class, JobEnum.TIMED_SYNC_USER_PUSH_ERROR_JOB.name(), JobEnum.TIMED_SYNC_USER_PUSH_ERROR_JOB_GROUP.name(), "0 0/1 * * * ?", userPushErrorMap);
        log.info("增加用户推送失败再次推送定时任务 end");

        log.info("增加用户需重新推送再次推送定时任务 start");
        Map userNeedPushAgainMap = new HashMap();
        userNeedPushAgainMap.computeIfAbsent("name", v -> TimedSyncUserNeedPushAgainJob.class.getName());
        quartzService.deleteJob(JobEnum.TIMED_SYNC_USER_NEED_PUSH_AGAIN_JOB.name(), JobEnum.TIMED_SYNC_USER_NEED_PUSH_AGAIN_JOB_GROUP.name());
        // 用户推送失败再次推送 每分钟推送一次
        quartzService.addJob(TimedSyncUserNeedPushAgainJob.class, JobEnum.TIMED_SYNC_USER_NEED_PUSH_AGAIN_JOB.name(), JobEnum.TIMED_SYNC_USER_NEED_PUSH_AGAIN_JOB_GROUP.name(), "0 0 0 * * ?", userNeedPushAgainMap);
        log.info("增加用户推送失败再次推送定时任务 end");


//        log.info("增加重新生成pdf定时任务 start");
//        Map taskJobMap = new HashMap();
//        schoolJobMap.computeIfAbsent("name", v -> ResetCreatePdfJob.class.getName());
//        quartzService.deleteJob(JobEnum.RESET_CREATE_PDF_JOB.name(), JobEnum.RESET_CREATE_PDF_JOB_GROUP.name());
//         //每天0点定时任务
//        quartzService.addJob(ResetCreatePdfJob.class, JobEnum.RESET_CREATE_PDF_JOB.name(), JobEnum.RESET_CREATE_PDF_JOB_GROUP.name(), "0 0/1 * * * ?", taskJobMap);
//        log.info("增加重新生成pdf定时任务 end");

        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_GROUP.name());
        // 每天15点定时任务
        quartzService.addJob(SendSmsExpireJob.class, JobEnum.SMS_NOTICE_TASK_EXPIRE_JOB.name(), JobEnum.SMS_NOTICE_TASK_EXPIRE_JOB_GROUP.name(), "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_GROUP.name());
        // 每天9点定时任务
        quartzService.addJob(SendSmsOverdueJob.class, JobEnum.SMS_NOTICE_TASK_OVERDUE_JOB.name(), JobEnum.SMS_NOTICE_TASK_OVERDUE_JOB_GROUP.name(), "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_GROUP.name());
        // 每隔1小时定时任务
        quartzService.addJob(ResendSmsJob.class, JobEnum.SMS_NOTICE_TASK_RESEND_JOB.name(), JobEnum.SMS_NOTICE_TASK_RESEND_JOB_GROUP.name(), "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_GROUP.name());
        // 每分钟定时任务
        quartzService.addJob(RedisMqSyncJob.class, JobEnum.REDIS_MQ_JOB.name(), JobEnum.REDIS_MQ_JOB_GROUP.name(), "0 0/1 * * * ?", redisMqJobMap);
        log.info("增加mq信息同步定时任务 end");

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