package com.jay.monitor.data.server.report.alert;

import com.jay.monitor.data.server.componts.GroupFactory;
import com.jay.monitor.data.server.enums.AlertMsgEnums;
import com.jay.monitor.data.server.enums.AlertRuleEnums;
import com.jay.monitor.data.server.factory.AbstractThreadProcess;
import com.jay.monitor.data.server.models.dto.alert.AlertDto;
import com.jay.monitor.data.server.models.dto.alert.RealTimeAlert;
import com.jay.monitor.data.server.models.store.AlertPlatformConfig;
import com.jay.monitor.data.server.models.store.AlertPlatformNode;
import com.jay.monitor.data.server.models.store.AlertRuleConfig;
import com.jay.monitor.data.server.report.alert.notify.AlertRealTimeMsg;
import com.jay.monitor.data.server.utils.DataUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 告警发送执行器
 *
 * @author liukaixiong
 * @Email liukx@elab-plus.com
 * @date 2021/9/7 - 17:45
 */
@Component
public class AlertSendProcess extends AbstractThreadProcess<AlertDto> implements InitializingBean {

    private String prefix_alert = getClass().getSimpleName() + "-";

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private AlertDataService alertDataService;

    @Autowired(required = false)
    private List<AlertRealTimeMsg> realTimeMsgProcesses;

    @Override
    public void consumer(AlertDto req) {
        String ruleId = req.getRuleId();
        RealTimeAlert realTimeAlert = req.getRealTimeAlert();
        // 查出规则
        AlertRuleConfig alertRuleConfig = alertDataService.getRuleId(ruleId);

        List<AlertPlatformNode> alertPlatformList = alertRuleConfig.getAlertPlatformList();

        for (int i = 0; i < alertPlatformList.size(); i++) {
            AlertPlatformNode alertPlatformNode = alertPlatformList.get(i);

            String alertPlatformId = alertPlatformNode.getAlertPlatformId();
            AlertMsgEnums platformType = alertPlatformNode.getPlatformType();

            AlertPlatformConfig cacheAlertPlatform = alertDataService.getAlertPlatform(alertPlatformId);
            AlertRealTimeMsg alertRealTimeMsg =
                GroupFactory.getINSTANCE().getObject(prefix_alert, platformType.name(), AlertRealTimeMsg.class);

            // 关键告警原因
            String title = realTimeAlert.getTitle();

            realTimeAlert.setTitle(title);

            Map<String, Object> extMap =
                DataUtils.getDefaultValue(realTimeAlert.getExtMap(), new LinkedHashMap<String, Object>());

            extMap.put("规则名称", alertRuleConfig.getRuleName());
            //            extMap.put(fieldName, fieldValue);

            realTimeAlert.setExtMap(extMap);
            realTimeAlert.setUniqueMark(alertRuleConfig.getId());

            if (!alertRealTimeMsg.isSkipAlert(realTimeAlert)) {
                logger.debug("[[[[[[[[触发告警]]]]]]]]");
                alertRealTimeMsg.send(AlertRuleEnums.CONFIG_RULE, cacheAlertPlatform, realTimeAlert);
            } else {
                logger.debug("该项目处于告警休眠期，不告警。");
            }
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        GroupFactory.getINSTANCE().registerObject(prefix_alert, realTimeMsgProcesses, (bean) -> {
            AlertRealTimeMsg alertRealTimeMsg = (AlertRealTimeMsg)bean;
            return alertRealTimeMsg.alertType().name();
        });
    }
}
