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.util.MqUtil;
import com.qmth.themis.exam.config.DictionaryConfig;
import com.qmth.themis.exam.websocket.WebSocketMobileServer;
import com.qmth.themis.exam.websocket.WebSocketOeServer;
import com.qmth.themis.mq.listener.RocketMessageConsumer;
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;

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

    @Resource
    MqUtil mqUtil;

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

    @Override
    public void run(String... args) throws Exception {
        log.info("服务器启动时执行 start");
        /**
         * websocket mq start
         */
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.WEBSOCKET_OE_GROUP.name()), mqUtil.getTopic(), 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() + "||" + MqTagEnum.OE_WEBSOCKET_MOBILE_ANSWER_READY.name() + "||" + MqTagEnum.OE_WEBSOCKET_MOBILE_ANSWER_UPLOAD.name() + "||" + MqTagEnum.OE_WEBSOCKET_EXAM_STOP.name() + "||" + MqTagEnum.MONITOR_START.name() + "||" + MqTagEnum.MONITOR_STOP.name() + "||" + MqTagEnum.OE_WEBSOCKET_MOBILE_MONITOR_STATUS.name() + "||" + MqTagEnum.EXAM_EXIT.name(), MessageModel.BROADCASTING, SpringContextHolder.getBean(WebSocketOeServer.class));
//        rocketMessageConsumer.setRocketMQConsumer(nameServer, dictionaryConfig.mqConfigDomain().getMap().get(MqGroupEnum.WEBSOCKET_OE_MOBILE_GROUP.name()), dictionaryConfig.mqConfigDomain().getTopic(), MqTagEnum.EXAM_STOP.name() + "||" + MqTagEnum.EXAM_START.name() + "||" + MqTagEnum.EXAM_AUDIO_MOBILE.name(), MessageModel.BROADCASTING, SpringContextHolder.getBean(WebSocketMobileServer.class));
        rocketMessageConsumer.setRocketMQConsumer(nameServer, mqUtil.getMap().get(MqGroupEnum.WEBSOCKET_OE_MOBILE_GROUP.name()), mqUtil.getTopic(), MqTagEnum.EXAM_STOP.name() + "||" + MqTagEnum.EXAM_START.name(), MessageModel.BROADCASTING, SpringContextHolder.getBean(WebSocketMobileServer.class));
        SystemConstant.initTempFiles();
        SystemConstant.getSearcher(SystemConstant.TEMP_FILES_DIR + File.separator + applicationName);
        log.info("服务器启动时执行 end");
    }
}