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
    private List<AlertReportMsg> alertMsgList;

    @Autowired
    private List<NameReport> listReportInvokes;

    @Autowired
    private AlertProperties alertProperties;

    public void alert(String date) {
        StringBuilder allMdAlertContent = new StringBuilder();

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

        // 构建消息
        for (int i = 0; i < listReportInvokes.size(); i++) {
            NameReport invoke = listReportInvokes.get(i);
            String reportContent = invoke.formatReport(date);
            if (reportContent != null) {
                allMdAlertContent.append(reportContent + MdConstants.newLine);
            }
        }

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

}
