package com.qmth.themis.exam.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.enums.MqTopicEnum;
import com.qmth.themis.exam.websocket.WebSocketMobileServer;
import com.qmth.themis.exam.websocket.WebSocketOeServer;
import com.qmth.themis.mq.listener.RocketMessageConsumer;
import com.qmth.themis.mq.templete.impl.*;
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;

/**
 * @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
    RocketMessageConsumer rocketMessageConsumer;

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

    @Override
    public void run(String... args) throws Exception {
        log.info("服务器启动时执行 start");
        /**
         * session
         */
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.SESSION_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), 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, MqGroupEnum.USER_LOG_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.USER.name() + "||" + MqTagEnum.STUDENT.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(UserLogConcurrentlyImpl.class));
        /**
         * task
         */
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.TASK_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), 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(), MessageModel.CLUSTERING, SpringContextHolder.getBean(TaskConcurrentlyImpl.class));
        /**
         * log
         */
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.LOG_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.EXCEPTION_LOG.name() + "||" + MqTagEnum.WARNING_LOG.name() + "||" + MqTagEnum.MONITOR_LOG.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(LogConcurrentlyImpl.class));
        /**
         * websocket mq start
         */
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.WEBSOCKET_OE_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.OE_HARD_FINISH.name() + "||" + MqTagEnum.OE_IM_BROADCASTING.name() + "||" + MqTagEnum.OE_IM_CLUSTERING.name() + "||" + MqTagEnum.OE_LIVENESS_VERIFY.name() + "||" + MqTagEnum.OE_MONITOR_FINISH.name() + "||" + MqTagEnum.OE_WARNING_FINISH.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(WebSocketOeServer.class));
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.WEBSOCKET_OE_MOBILE_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.EXAM_STOP.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(WebSocketMobileServer.class));
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.WEBSOCKET_DELAY_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.OE_UN_NORMAL.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(WebsocketUnNormalConcurrentlyImpl.class));
        /**
         * websocket mq end
         */
        //计算客观分
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.CALCULATE_OBJECTIVE_SCORE_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.CALCULATE_OBJECTIVE_SCORE.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(CalculateObjectiveScoreConcurrentlyImpl.class));

        //人脸验证保存
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.FACE_VERIFY_SAVE_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.FACE_VERIFY_SAVE.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(FaceVerifyConcurrentlyImpl.class));

        //活体验证保存
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.LIVENESS_VERIFY_SAVE_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.LIVENESS_VERIFY_SAVE.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(LivenessVerifyConcurrentlyImpl.class));

        //考试记录数据持久化
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.EXAM_RECORD_PERSISTED_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.EXAM_RECORD_PERSISTED.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(ExamRecordPersistedConcurrentlyImpl.class));

        //考试记录数据更新
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.EXAM_RECORD_UPDATE_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.EXAM_RECORD_UPDATE.name() + "||" + MqTagEnum.EXAM_RECORD_UPDATE_COLUMNS.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(ExamRecordUpdateConcurrentlyImpl.class));
        //考试记录数据初始化
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.EXAM_RECORD_INIT_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.EXAM_RECORD_INIT.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(ExamRecordInitConcurrentlyImpl.class));

        //考生数据更新
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.EXAM_STUDENT_UPDATE_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.EXAM_STUDNET_UPDATE.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(ExamStudentUpdateConcurrentlyImpl.class));

        //考试断点
        rocketMessageConsumer.setRocketMQConsumer(nameServer, MqGroupEnum.EXAM_BREAK_GROUP.getCode(), MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.EXAM_BREAK.name(), MessageModel.CLUSTERING, SpringContextHolder.getBean(ExamBreakConcurrentlyImpl.class));
        SystemConstant.initTempFiles();
        log.info("服务器启动时执行 end");
    }
}
