package com.qmth.themis.task.start;

import com.qmth.themis.business.constant.SpringContextHolder;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.enums.MqGroupEnum;
import com.qmth.themis.business.enums.MqTagEnum;
import com.qmth.themis.business.service.ThemisCacheService;
import com.qmth.themis.business.util.MqUtil;
import com.qmth.themis.mq.listener.RocketMessageConsumer;
import com.qmth.themis.mq.templete.impl.*;
import com.qmth.themis.task.enums.QuartzTaskEnum;
import com.qmth.themis.task.listener.QuartzOrderlyImpl;
import com.qmth.themis.task.quartz.*;
import com.qmth.themis.task.service.QuartzService;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
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
    RocketMessageConsumer rocketMessageConsumer;

    @Value("${rocketmq.name-server}")
    String nameServer;

    @Resource
    MqUtil mqUtil;

    @Value("${spring.application.name}")
    String applicationName;

    @Resource
    ThemisCacheService themisCacheService;

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public void run(String... args) throws Exception {
        log.info("服务器启动时执行 start");
        log.info("增加mqjob start");
        themisCacheService.setCurrentExamListCache();
        themisCacheService.setOrgExamListCache();

        Map mqMap = new HashMap();
        mqMap.put(SystemConstant.NAME, MqJob.class.getName());
        quartzService.deleteJob(QuartzTaskEnum.MQ_JOB_NAME.name(), QuartzTaskEnum.MQ_JOB_GROUP_NAME.name());
        quartzService.addJob(MqJob.class, QuartzTaskEnum.MQ_JOB_NAME.name(), QuartzTaskEnum.MQ_JOB_GROUP_NAME.name(), "0 0/1 * * * ?", mqMap);
        log.info("增加mqjob end");

        log.info("增加mqActivityjob start");
        Map mqActivityMap = new HashMap();
        mqActivityMap.put(SystemConstant.NAME, MqActivityJob.class.getName());
        quartzService.deleteJob(QuartzTaskEnum.MQ_ACTIVITY_JOB_NAME.name(), QuartzTaskEnum.MQ_ACTIVITY_JOB_GROUP_NAME.name());
        quartzService.addJob(MqActivityJob.class, QuartzTaskEnum.MQ_ACTIVITY_JOB_NAME.name(), QuartzTaskEnum.MQ_ACTIVITY_JOB_GROUP_NAME.name(), "0 0 0 * * ?", mqActivityMap);
        log.info("增加mqActivityjob end");

        log.info("增加客观题答案缓存加载job start");
        Map objectiveAnswerCacheMap = new HashMap();
        objectiveAnswerCacheMap.put(SystemConstant.NAME, ObjectiveAnswerCacheLoadJob.class.getName());
        quartzService.deleteJob(QuartzTaskEnum.OBJECTIVE_ANSWER_CACHE_LOAD_JOB_NAME.name(), QuartzTaskEnum.OBJECTIVE_ANSWER_CACHE_LOAD_JOB_GROUP_NAME.name());
        quartzService.addJob(ObjectiveAnswerCacheLoadJob.class, QuartzTaskEnum.OBJECTIVE_ANSWER_CACHE_LOAD_JOB_NAME.name(), QuartzTaskEnum.OBJECTIVE_ANSWER_CACHE_LOAD_JOB_GROUP_NAME.name(), "0 0 1 * * ?", objectiveAnswerCacheMap);
        log.info("增加客观题答案缓存加载job end");

        log.info("增加考试信息统计job start");
        Map examSummaryMap = new HashMap();
        examSummaryMap.put(SystemConstant.NAME, ExamSummaryJob.class.getName());
        quartzService.deleteJob(QuartzTaskEnum.EXAM_SUMMARY_JOB_NAME.name(), QuartzTaskEnum.EXAM_SUMMARY_JOB_GROUP_NAME.name());
        quartzService.addJob(ExamSummaryJob.class, QuartzTaskEnum.EXAM_SUMMARY_JOB_NAME.name(), QuartzTaskEnum.EXAM_SUMMARY_JOB_GROUP_NAME.name(), "0 0/1 * * * ?", examSummaryMap);
        log.info("增加考试信息统计job end");

        log.info("增加考试列表job start");
        Map examListMap = new HashMap();
        examListMap.put(SystemConstant.NAME, ExamListJob.class.getName());
        quartzService.deleteJob(QuartzTaskEnum.EXAM_LIST_JOB_NAME.name(), QuartzTaskEnum.EXAM_LIST_JOB_GROUP_NAME.name());
        quartzService.addJob(ExamListJob.class, QuartzTaskEnum.EXAM_LIST_JOB_NAME.name(), QuartzTaskEnum.EXAM_LIST_JOB_GROUP_NAME.name(), "0 0 0/1 * * ?", examListMap);
        log.info("增加考试列表job end");

//        log.info("增加修复job start");
//        Map repairMap = new HashMap();
//        repairMap.put(SystemConstant.NAME, RepairJob.class.getName());
//        quartzService.deleteJob(QuartzTaskEnum.REPAIR_JOB_NAME.name(), QuartzTaskEnum.REPAIR_JOB_GROUP_NAME.name());
//        quartzService.addJob(RepairJob.class, QuartzTaskEnum.REPAIR_JOB_NAME.name(), QuartzTaskEnum.REPAIR_JOB_GROUP_NAME.name(), "0 0/10 * * * ?", repairMap);
//        log.info("增加修复job end");

        /**
         * session
         */
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.SESSION_GROUP.name()), mqUtil.getTopic(), MqTagEnum.WEB.name() + "||" + MqTagEnum.WIN.name() + "||" + MqTagEnum.MAC.name() + "||" + MqTagEnum.WXAPP.name() + "||" + MqTagEnum.IOS.name() + "||" + MqTagEnum.ANDROID.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(SessionConcurrentlyImpl.class));
        /**
         * userLog
         */
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.USER_LOG_GROUP.name()), mqUtil.getTopic(), MqTagEnum.USER.name() + "||" + MqTagEnum.STUDENT.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(UserLogConcurrentlyImpl.class));
        /**
         * log
         */
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.LOG_GROUP.name()), mqUtil.getTopic(), MqTagEnum.EXCEPTION_LOG.name() + "||" + MqTagEnum.MONITOR_LOG.name() + "||" + MqTagEnum.ONLINE_LOG.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(LogConcurrentlyImpl.class));
        /**
         * task
         */
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.TASK_GROUP.name()), mqUtil.getTopic(), MqTagEnum.EXAM_STUDENT_IMPORT.name() + "||" + MqTagEnum.ROOM_CODE_IMPORT.name() + "||" + MqTagEnum.ROOM_CODE_EXPORT.name() + "||" + MqTagEnum.EXAM_PAPER_IMPORT.name() + "||" + MqTagEnum.EXAM_STUDENT_EXPORT.name() + "||" + MqTagEnum.MARK_RESULT_SIMPLE_EXPORT.name() + "||" + MqTagEnum.MARK_RESULT_STANDARD_EXPORT.name() + "||" + MqTagEnum.CLOUD_MARK_SCORE_SYNC.name() + "||" + MqTagEnum.CLOUD_MARK_DATA_PUSH.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(TaskConcurrentlyImpl.class));
        /**
         * websocket mq start
         */
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.WEBSOCKET_DELAY_GROUP.name()), mqUtil.getTopic(), MqTagEnum.OE_UN_NORMAL.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(WebsocketUnNormalConcurrentlyImpl.class));
        /**
         * quartz mq start
         */
//        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.QUARTZ_GROUP.name()), mqUtil.getTopic(), MqTagEnum.EXAM_ACTIVITY.name() + "||" + MqTagEnum.EXAM_STUDENT.name() + "||" + MqTagEnum.EXAM_AUDIO.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(QuartzOrderlyImpl.class));
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.QUARTZ_GROUP.name()), mqUtil.getTopic(), MqTagEnum.EXAM_ACTIVITY.name() + "||" + MqTagEnum.EXAM_STUDENT.name() + "||" + MqTagEnum.EXAM_ACTIVITY_FINAL_PREPARE_TIME.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(QuartzOrderlyImpl.class));
        //计算客观分
//        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.CALCULATE_OBJECTIVE_SCORE_GROUP.name()), mqUtil.getTopic(), MqTagEnum.CALCULATE_OBJECTIVE_SCORE.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(CalculateObjectiveScoreConcurrentlyImpl.class));
        //重新算分
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.SCORE_CALCULATE_GROUP.name()), mqUtil.getTopic(), MqTagEnum.EXAM_SCORE_CALCULATE.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(CalculateScoreConcurrentlyImpl.class));
        //人脸验证保存
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.FACE_VERIFY_SAVE_GROUP.name()), mqUtil.getTopic(), MqTagEnum.FACE_VERIFY_SAVE.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(FaceVerifyConcurrentlyImpl.class));

        //活体验证保存
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.LIVENESS_VERIFY_SAVE_GROUP.name()), mqUtil.getTopic(), MqTagEnum.LIVENESS_VERIFY_SAVE.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(LivenessVerifyConcurrentlyImpl.class));

        //考试记录数据更新
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.EXAM_RECORD_UPDATE_GROUP.name()), mqUtil.getTopic(), MqTagEnum.EXAM_RECORD_UPDATE.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(ExamRecordUpdateConcurrentlyImpl.class));
        //考试记录数据持久化
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.EXAM_RECORD_PERSISTED_GROUP.name()), mqUtil.getTopic(), MqTagEnum.EXAM_RECORD_PERSISTED.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(ExamRecordPersistedConcurrentlyImpl.class));
        //考试断点记录数据持久化
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.EXAM_BREAK_RECORD_PERSISTED_GROUP.name()), mqUtil.getTopic(), MqTagEnum.EXAM_BREAK_HISTORY_PERSISTED.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(ExamRecordBreakHistoryPersistedConcurrentlyImpl.class));

//        考生数据更新
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.EXAM_STUDENT_UPDATE_GROUP.name()), mqUtil.getTopic(), MqTagEnum.EXAM_STUDNET_UPDATE.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(ExamStudentUpdateConcurrentlyImpl.class));

        //考试断点
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.EXAM_BREAK_GROUP.name()), mqUtil.getTopic(), MqTagEnum.EXAM_BREAK.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(ExamBreakConcurrentlyImpl.class));
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.EXAM_BREAK_DELAY_GROUP.name()), mqUtil.getTopic(), MqTagEnum.EXAM_BREAK_DELAY.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(ExamBreakDelayConcurrentlyImpl.class));
        /**
         * tencent video mq start
         */
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.TENCENT_VIDEO_GROUP.name()), mqUtil.getTopic(), MqTagEnum.TENCENT_VIDEO.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(TencentVideoConcurrentlyImpl.class));
        SystemConstant.initTempFiles();
        SystemConstant.getSearcher(SystemConstant.TEMP_FILES_DIR + File.separator + applicationName);
        log.info("服务器启动时执行 end");
    }
}