package com.qmth.teachcloud.eds.start;

import com.qmth.teachcloud.eds.enums.JobEnum;
import com.qmth.teachcloud.eds.job.GdpuDataSyncJob;
import com.qmth.teachcloud.eds.job.StudentScoreSyncJob;
import com.qmth.teachcloud.eds.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;


    @Override
    public void run(String... args) throws Exception {
        log.info("服务器启动时执行 start");
        // 每天0点开始，一天一次
        log.info("增加自动同步考生定时任务 start");
        Map examDataMap = new HashMap();
        examDataMap.computeIfAbsent("name", v -> GdpuDataSyncJob.class.getName());
        quartzService.deleteJob(JobEnum.GDPU_DATA_JOB.name(), JobEnum.GDPU_DATA_JOB_GROUP.name());
//        quartzService.addJob(GdpuDataSyncJob.class, JobEnum.GDPU_DATA_JOB.name(), JobEnum.GDPU_DATA_JOB_GROUP.name(), "0 0 0 * * ?", examDataMap);
        log.info("增加自动同步考生定时任务 end");

        // 每天1点开始，一天一次
        log.info("增加自动同步考生成绩定时任务 start");
        Map scoreMap = new HashMap();
        scoreMap.computeIfAbsent("name", v -> StudentScoreSyncJob.class.getName());
        quartzService.deleteJob(JobEnum.STUDENT_SCORE_JOB.name(), JobEnum.STUDENT_SCORE_JOB_GROUP.name());
        quartzService.addJob(StudentScoreSyncJob.class, JobEnum.STUDENT_SCORE_JOB.name(), JobEnum.STUDENT_SCORE_JOB_GROUP.name(), "0 0 1 * * ?", scoreMap);
        log.info("增加自动同步考生成绩定时任务 end");

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