package com.ruoyi.web.controller.task;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.RedisUtils;
import com.ruoyi.zhdd.domain.IncidentProcess;
import com.ruoyi.zhdd.domain.bo.MessagePushBo;
import com.ruoyi.zhdd.domain.bo.MessagePushUser;
import com.ruoyi.zhdd.domain.vo.IncidentTaskVo;
import com.ruoyi.zhdd.domain.vo.IncidentVo;
import com.ruoyi.zhdd.feign.FeignNoticeInfoService;
import com.ruoyi.zhdd.service.IIncidentProcessService;
import com.ruoyi.zhdd.service.IIncidentService;
import com.ruoyi.zhdd.service.IIncidentTaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description: TODO
 * @Author: huangcheng
 * @Date: 2022/1/12
 * @Version V1.0
 */
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class NoticeInfoListiner {

    private final FeignNoticeInfoService feignNoticeInfoService;
    private final IIncidentService incidentService;
    private final IIncidentTaskService incidentTaskService;
    private final IIncidentProcessService incidentProcessService;

    /**
     * 消息发送
     * A:事件接报。B:处置方案
     */
    @Bean
    public void sendNotice() {
        RedisUtils.subscribe(Constants.NOTICE_PUSH, String.class, msg -> {
            // 查询事件信息并发送消息
            MessagePushBo messagePushBo = new MessagePushBo();
            String type = msg.substring(0, 1);
            String title = "未知";
            String synopsis = "未知";
            String text = "未知";
            String messageUser = "";
            IncidentProcess incidentProcess = new IncidentProcess();
            if ("A".equals(type)) {
                IncidentVo incidentVo = incidentService.queryById(msg.substring(1));
                title = StrUtil.concat(true, "应急事件:", incidentVo.getName());
                synopsis = "请查看应急事件";
                text = StrUtil.concat(true, incidentVo.getAddr(), "发生应急事件，请关注消息，做好应急准备");
                incidentProcess.setDes("事件接报");
                incidentProcess.setIncidentId(msg.substring(1));
                messageUser = incidentVo.getExprJson();
            } else if ("B".equals(type)) {
                // 查询处置方案
                IncidentTaskVo incidentTaskVo = incidentTaskService.queryById(msg.substring(1));
                IncidentVo incidentVo = incidentService.queryById(incidentTaskVo.getIncidentId());
                title = "应急处置";
                synopsis = "请查看应急处置方案";
                text = StrUtil.concat(true, "请查看", incidentVo.getName(), "事件的", incidentTaskVo.getTaskName(), "方案");
                incidentProcess.setDes("发送处置方案");
                incidentProcess.setIncidentId(incidentTaskVo.getIncidentId());
                messageUser = incidentVo.getExprJson();
            }
            if (StrUtil.isBlank(messageUser)) {
                return;
            }
            List<MessagePushUser> messagePushUsers = JSONUtil.toList(messageUser, MessagePushUser.class);
            incidentProcess.setStatus(1);
            incidentProcessService.save(incidentProcess);
            messagePushBo.setMsTitle(title);
            messagePushBo.setMsSynopsis(synopsis);
            messagePushBo.setMsText(text);
            messagePushBo.setMsNo(incidentProcess.getId());
            messagePushBo.setMessageReadInfoList(messagePushUsers);
            feignNoticeInfoService.messagePush(messagePushBo);
            log.info("发送消息体 => {}", messagePushBo);
            log.info("订阅事件消息 => {}", msg);
        });
    }

}
