XtDayAlarmJob.java 9.8 KB
package com.skua.modules.quartz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.skua.modules.alarmtmp.entity.AlarmRecordHistory;
import com.skua.modules.alarmtmp.entity.AlarmRuleLevelConfigTemplate;
import com.skua.modules.alarmtmp.service.AlarmMessageHistoryService;
import com.skua.modules.alarmtmp.service.AlarmRecordHistoryService;
import com.skua.modules.alarmtmp.service.IAlarmRuleLevelConfigTemplateService;
import com.skua.modules.alarmtmp.vo.SystemNoticeVO;
import com.skua.core.api.vo.DictModel;
import com.skua.core.util.DateUtils;

import com.skua.modules.system.datestandard.service.ISysMonitorMetricInfoService;
import com.skua.modules.system.service.ISysDictService;
import com.skua.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.skua.modules.util.SystemNoticeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Slf4j
@Component
public class XtDayAlarmJob implements Job {

    @Autowired
    private ISysMonitorMetricInfoService sysMonitorMetricInfoService;
    @Autowired
    private SystemNoticeUtils systemNoticeUtils;
    @Autowired
    private ISysDictService sysDictService;

    @Autowired
    private AlarmRecordHistoryService alarmRecordHistoryService;

    @Autowired
    private AlarmMessageHistoryService alarmMessageHistoryService;

    @Autowired
    private IAlarmRuleLevelConfigTemplateService alarmRuleLevelConfigTemplateService;

    @Autowired
    private ISysUserService sysUserService;

    //在${date}时${departName}的日处理量为:${num}负荷率${conent},请注意!
    private static String prefix = "【金控数矿】";
    private static String departNameTemplete = "${departName}";
    private static String contentTemplete = "${alarmParamName}";
    private static String suffix = ",请注意!";

    //添加到报警记录表,用于移动端审批的使用
    private static String tableName = "alarm_record_history";

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        try {
            log.info("日均值报警任务开始");
            //查询pg日均值警报
            List<Map<String, Object>> excessAlarmPgList = checkPgExcessAlarm();
            sendMessage(excessAlarmPgList);
            log.info("日均值报警任务结束");
        } catch (Exception e) {
            log.info("日均值报警任务异常" + e);
        }


    }


    public List<Map<String, Object>> checkPgExcessAlarm() {

        List<Map<String, Object>> result = Lists.newArrayList();

        List<Map<String, Object>> mets = sysMonitorMetricInfoService.queryWaterListAll1("8", DateUtils.getLastDay());

        //获取负荷比例
        List<DictModel> fhData = sysDictService.queryDictItemsByCode("fh_data");
        if (fhData == null) {
            return result;
        }
        Double low = 0.0;
        Double up = 0.0;

        for (DictModel fhDatum : fhData) {
            if (fhDatum.getText().contains("低负荷")) {
                low = Double.parseDouble(fhDatum.getValue());
            }
            if (fhDatum.getText().contains("高负荷")) {
                up = Double.parseDouble(fhDatum.getValue());
            }
        }
        for (Map<String, Object> v : mets) {
            Double fh = (Double) v.get("fh");
            if (fh != null) {
                if (fh <= low) {
                    v.put("tread", "low");
                    v.put("limit", low);
                    result.add(v);
                } else if (fh >= up) {
                    v.put("tread", "up");
                    v.put("limit", up);
                    result.add(v);
                }
            }
        }
        return result;

    }


    public void sendMessage(List<Map<String, Object>> alarmList) {

        DecimalFormat df = new DecimalFormat("#.##");
        String date = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        alarmList.forEach(v -> {

            String tread = (String) v.get("tread");
            Double limit = (Double) v.get("limit");
            Double fh = (Double) v.get("fh");
            String departName = (String) v.get("departName");
            String metricdCode = (String) v.get("metricdCode");
            Double metricdValue = (Double) v.get("metricdValue");
            String departId = (String) v.get("departId");
            String time = (String) v.get("time");
            String content = "";
            QueryWrapper<AlarmRuleLevelConfigTemplate> alarmRuleLevelConfigTemplateQueryWrapper = new QueryWrapper<>();
            alarmRuleLevelConfigTemplateQueryWrapper.eq("depart_id", departId);
            alarmRuleLevelConfigTemplateQueryWrapper.eq("alarm_template_name", "日均值流量报警配置");
            alarmRuleLevelConfigTemplateQueryWrapper.eq("alarm_template_type", 3);
            AlarmRuleLevelConfigTemplate alarmRuleLevelConfigTemplate = alarmRuleLevelConfigTemplateService.getOne(alarmRuleLevelConfigTemplateQueryWrapper);

            if (alarmRuleLevelConfigTemplate != null) {

                //在${date}时${departName}的日处理量为:${num}负荷率${conent},请注意!
                content = contentTemplete.replace("${alarmParamName}", df.format(fh));

                //是否需要报警
                departName = departNameTemplete.replace("${departName}", departName);


                Map<String, Object> alarmRecordHistoryMap = Maps.newHashMap();
                //如果没有上条记录或者处理状态(0:未处理,1:已处理)为已处理
                if (!alarmRecordHistoryMap.containsKey(departId + "_" + metricdCode)) {

                    alarmRecordHistoryMap.put(departId + "_" + metricdCode, "1");

                    AlarmRecordHistory alarmRecordHistory = new AlarmRecordHistory();

                    //手动创建UUID,用于报警自动送审
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");

                    alarmRecordHistory.setId(uuid);
                    alarmRecordHistory.setAlarmParamCode(metricdCode);
                    alarmRecordHistory.setAlarmParamName("日处理量");
                    alarmRecordHistory.setAlarmParamUnit("m³");
                    alarmRecordHistory.setActualAvgValue(df.format(fh));
                    alarmRecordHistory.setActualMinValue(df.format(metricdValue));
                    alarmRecordHistory.setAlarmRuleLevelName(alarmRuleLevelConfigTemplate.getAlarmTemplateLevelName());

                    alarmRecordHistory.setAlarmRuleType("7");
                    alarmRecordHistory.setAlarmParamType(alarmRuleLevelConfigTemplate.getAlarmTemplateName());

                    try {
                        alarmRecordHistory.setRecordTime(simpleDateFormat1.parse(date));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    alarmRecordHistory.setDepartId(departId);
                    alarmRecordHistory.setHandleStatus(0);
                    alarmRecordHistory.setDelFlag(1);
                    alarmRecordHistory.setAlarmParamUpperLimit(limit + "%");
                    //在${date}时${departName}的日处理量为:${num}负荷率${conent},请注意!
                    String contentTmp = "";
                    if ("up".equals(tread)) {
                        alarmRecordHistory.setAlarmParamLowerLimit("高负荷运行");
                        alarmRecordHistory.setAlarmContent(prefix + "在" + time + "时" + departName + "的日处理量为:" + metricdValue + "(m³),负荷率达到" + content + "%,处于高负荷运行状态" + suffix);
                        contentTmp = "在" + time + "时" + departName + "的日处理量为:" + metricdValue + "(m³),负荷率达到" + content + "%,处于高负荷运行状态" + suffix;
                    } else if ("low".equals(tread)) {
                        alarmRecordHistory.setAlarmParamLowerLimit("低负荷运行");
                        alarmRecordHistory.setAlarmContent(prefix + "在" + time + "时" + departName + "的日处理量为:" + metricdValue + "(m³),负荷率达到" + content + "%,处于低负荷运行状态" + suffix);
                        contentTmp = "在" + time + "时" + departName + "的日处理量为:" + metricdValue + "(m³),负荷率达到" + content + "%,处于低负荷运行状态" + suffix;
                    }
//                    alarmRecordHistory.setTableName(tableName);
                    alarmRecordHistory.setCreateBy("admin");
                    alarmRecordHistoryService.save(alarmRecordHistory);

                    //判断是否需要发送系统通知 (内容跟报警记录信息相同)
                    if ("1".equals(alarmRuleLevelConfigTemplate.getIsSendNotice())) {
                        if (StringUtils.isNotBlank(alarmRuleLevelConfigTemplate.getAlarmNoticeRecipient())) {
                            SystemNoticeVO systemNoticeVO = new SystemNoticeVO(alarmRuleLevelConfigTemplate.getAlarmNoticeRecipient(),
                                    alarmRuleLevelConfigTemplate.getAlarmTemplateLevelName(), contentTmp, departId, "7");
                            systemNoticeUtils.dealNoticeMessage(systemNoticeVO);
                        }
                    }
                }
            }
        });
    }


}