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

import com.jay.monitor.data.server.config.props.AlertProperties;
import com.jay.monitor.data.server.costs.MdConstants;
import com.jay.monitor.data.server.ext.ClazzEqualsProcess;
import com.jay.monitor.data.server.report.alert.notify.AlertReportMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Module TODO
 * @Description TODO
 * @Author liukaixiong
 * @Date 2020/12/14 14:32
 */
@Component
public class DayAlertProcess implements ClazzEqualsProcess {

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

    private String defaultDbType = ".mysql.";

    @Autowired(required = false)
    private List<AlertReportMsg> alertMsgList;

    @Autowired(required = false)
    private List<NameReport> listReportInvokes;

    @Autowired
    private AlertProperties alertProperties;

    public void alert(String date) {

        if (alertMsgList == null || listReportInvokes == null) {
            logger.debug("找不到对应的告警模版实现..");
            return;
        }

        StringBuilder allMdAlertContent = new StringBuilder();

        if (!alertProperties.isEnable()) {
            logger.debug("未开启告警发送");
            return;
        }

        // 构建消息
        for (NameReport invoke : listReportInvokes) {
            String reportContent = invoke.formatReport(date);
            if (reportContent != null) {
                allMdAlertContent.append(reportContent).append(MdConstants.newLine);
            }
        }

        // 发送消息
        for (AlertReportMsg alertMsgProcess : alertMsgList) {
            boolean msgSend = alertMsgProcess.send(date, allMdAlertContent.toString());
            logger.info("消息发送[" + alertMsgProcess.alertType() + "]结果:" + msgSend);
        }
    }

}
