From f20ae2997c5949062ea2f230cbc491af4d1d2075 Mon Sep 17 00:00:00 2001 From: xiaohuo <14141624+dsgfhrh@user.noreply.gitee.com> Date: Tue, 21 Oct 2025 23:08:05 +0800 Subject: [PATCH] =?UTF-8?q?10.21=E9=A2=84=E8=AD=A6=E6=A8=A1=E7=89=88?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ScheduleRulesController.java | 1 + .../AlertMessageController.java | 7 +- .../com/alops/system/domain/AlertRule.java | 1 + .../alops/system/domain/vo/AlertRuleVo.java | 1 + .../system/mapper/DictAlarmParamMapper.java | 4 + .../service/impl/AlertLaunchServiceImpl.java | 38 +++--- .../service/impl/AlertRuleServiceImpl.java | 124 +++++++++++++++++- .../mapper/system/DictAlarmParamMapper.xml | 8 ++ 8 files changed, 161 insertions(+), 23 deletions(-) diff --git a/ALOps_sys_backend/alops-admin/src/main/java/com/alops/web/controller/equManagement/ScheduleRulesController.java b/ALOps_sys_backend/alops-admin/src/main/java/com/alops/web/controller/equManagement/ScheduleRulesController.java index 26f2b35d..72e933c7 100644 --- a/ALOps_sys_backend/alops-admin/src/main/java/com/alops/web/controller/equManagement/ScheduleRulesController.java +++ b/ALOps_sys_backend/alops-admin/src/main/java/com/alops/web/controller/equManagement/ScheduleRulesController.java @@ -106,6 +106,7 @@ public class ScheduleRulesController extends BaseController // util.exportExcel(response, list, "定时采集规则数据"); // } // + /** * 获取定时采集规则详细信息 */ diff --git a/ALOps_sys_backend/alops-admin/src/main/java/com/alops/web/controller/exceptionController/AlertMessageController.java b/ALOps_sys_backend/alops-admin/src/main/java/com/alops/web/controller/exceptionController/AlertMessageController.java index 526b2816..04ae0b6d 100644 --- a/ALOps_sys_backend/alops-admin/src/main/java/com/alops/web/controller/exceptionController/AlertMessageController.java +++ b/ALOps_sys_backend/alops-admin/src/main/java/com/alops/web/controller/exceptionController/AlertMessageController.java @@ -1,6 +1,7 @@ package com.alops.web.controller.exceptionController; import java.util.List; +import java.util.Map; import com.alops.common.annotation.Log; import com.alops.common.core.controller.BaseController; @@ -49,12 +50,12 @@ public class AlertMessageController extends BaseController // @PostMapping("/executeImmediate") // public AjaxResult executeImmediate(@RequestBody Map> paramMap) { // try { -// List ruleIds = paramMap.get("ruleIds"); +// List ruleIds = paramMap.get("ruleIds"); // if (ruleIds == null || ruleIds.isEmpty()) { -// return AjaxResult.error("请选择至少一条规则"); +// return AjaxResult.error("请选择至少一条规则"); // } // -// iAlertLaunchService.executeImmediateAlerts(); // 调用 Service 执行 +// iAlertLaunchService.executeImmediateAlerts(); // 调用 Service 执行 // return AjaxResult.success("立即预警任务已触发,存入成功,请查看"); // // } catch (Exception e) { diff --git a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/domain/AlertRule.java b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/domain/AlertRule.java index 1a593e09..6ebb387d 100644 --- a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/domain/AlertRule.java +++ b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/domain/AlertRule.java @@ -54,6 +54,7 @@ public class AlertRule extends BaseEntity /** 预警模版(参数用数据库命名法) */ @Excel(name = "预警模版", readConverterExp = "参=数用数据库命名法") + @JsonIgnore private String alertTemplate; /** 预警类型 */ diff --git a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/domain/vo/AlertRuleVo.java b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/domain/vo/AlertRuleVo.java index b790d1f8..0671e8b0 100644 --- a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/domain/vo/AlertRuleVo.java +++ b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/domain/vo/AlertRuleVo.java @@ -49,6 +49,7 @@ public class AlertRuleVo extends BaseEntity { @JsonIgnore private String alertSql; + @JsonIgnore /** 预警模版(参数用数据库命名法) */ @Excel(name = "预警模版", readConverterExp = "参=数用数据库命名法") private String alertTemplate; diff --git a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/mapper/DictAlarmParamMapper.java b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/mapper/DictAlarmParamMapper.java index 7b0caae4..05db8fd1 100644 --- a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/mapper/DictAlarmParamMapper.java +++ b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/mapper/DictAlarmParamMapper.java @@ -2,6 +2,7 @@ package com.alops.system.mapper; import com.alops.system.domain.DictAlarmParam; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Select; import org.springframework.data.repository.query.Param; import java.util.List; @@ -16,6 +17,9 @@ import java.util.List; public interface DictAlarmParamMapper { + + + String selectCnNameByParamName(@Param("paramName") String paramName); List selectByTableName(); /** * 查询【请填写功能名称】 diff --git a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/AlertLaunchServiceImpl.java b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/AlertLaunchServiceImpl.java index ea5c0aec..d297b6ce 100644 --- a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/AlertLaunchServiceImpl.java +++ b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/AlertLaunchServiceImpl.java @@ -208,32 +208,32 @@ public class AlertLaunchServiceImpl implements IAlertLaunchService { return new ArrayList<>(); } - // 2. 需要比较的字段,如 health、temperature 等 + // 2. 需要比较的字段,如 health、temperature 等 String col = conditions.get(0).getParamName(); - // 3. SQL:取每台设备最新记录和次新记录,比较该字段是否变化,并关联设备类型 + // 3. SQL:取每台设备最新记录和次新记录,比较该字段是否变化,并关联设备类型 String sql = - "SELECT g1.*, t.name AS equ_type,b.name AS equ_name,b.equ_number" + + "SELECT g1.*, t.name AS equ_type, b.name AS equ_name, b.equ_number " + "FROM equ_gather g1 " + "JOIN equ_gather g2 ON g1.equ_id = g2.equ_id " + - "JOIN equ_base b ON g1.equ_id = b.id " + - "JOIN equ_type t ON b.equ_type = t.id " + + "JOIN equ_base b ON g1.equ_id = b.id " + + "JOIN equ_type t ON b.equ_type = t.id " + "WHERE g1.gather_time = ( " + - " SELECT MAX(tg.gather_time) " + - " FROM equ_gather tg " + - " WHERE tg.equ_id = g1.equ_id " + + " SELECT MAX(tg.gather_time) " + + " FROM equ_gather tg " + + " WHERE tg.equ_id = g1.equ_id " + + ") " + + "AND g2.gather_time = ( " + + " SELECT MAX(t2.gather_time) " + + " FROM equ_gather t2 " + + " WHERE t2.equ_id = g1.equ_id " + + " AND t2.gather_time < ( " + + " SELECT MAX(t3.gather_time) " + + " FROM equ_gather t3 " + + " WHERE t3.equ_id = g1.equ_id " + " ) " + - " AND g2.gather_time = ( " + - " SELECT MAX(t2.gather_time) " + - " FROM equ_gather t2 " + - " WHERE t2.equ_id = g1.equ_id " + - " AND t2.gather_time < ( " + - " SELECT MAX(t3.gather_time) " + - " FROM equ_gather t3 " + - " WHERE t3.equ_id = g1.equ_id " + - " ) " + - " ) " + - " AND g1.`" + col + "` <> g2.`" + col + "`"; + ") " + + "AND g1.`" + col + "` <> g2.`" + col + "`"; // 4. 返回查询结果 return jdbcTemplate.queryForList(sql); diff --git a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/AlertRuleServiceImpl.java b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/AlertRuleServiceImpl.java index bad959f0..5ad4545b 100644 --- a/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/AlertRuleServiceImpl.java +++ b/ALOps_sys_backend/alops-system/src/main/java/com/alops/system/service/impl/AlertRuleServiceImpl.java @@ -11,6 +11,7 @@ import com.alops.system.domain.dto.AlertRuleAddDto; import com.alops.system.domain.vo.AlertRuleVo; import com.alops.system.mapper.AlertRuleConditionMapper; import com.alops.system.mapper.AlertRuleMapper; +import com.alops.system.mapper.DictAlarmParamMapper; import com.alops.system.service.IAlertRuleService; import org.apache.commons.lang3.StringEscapeUtils; @@ -32,6 +33,9 @@ public class AlertRuleServiceImpl implements IAlertRuleService @Autowired private AlertRuleConditionMapper alertRuleConditionMapper; + @Autowired + private DictAlarmParamMapper dictAlarmParamMapper; + //生成预警规则 /** * 保存预警规则及条件,同时生成 SQL @@ -58,6 +62,9 @@ public class AlertRuleServiceImpl implements IAlertRuleService } rule.setAlertSql(sql); rule.setEnable(1); + //生成模版 + String alertTemplate=generateAlertTemplate(dto); + rule.setAlertTemplate(alertTemplate); // 插入规则 alertRuleMapper.insertAlertRule(rule); @@ -91,9 +98,11 @@ public class AlertRuleServiceImpl implements IAlertRuleService sql = buildSql(rule, conditions); } rule.setAlertSql(sql); + //生成模版 + String alertTemplate=generateAlertTemplate(dto); + rule.setAlertTemplate(alertTemplate); // 3. 更新规则表 - alertRuleMapper.updateAlertRule(rule); // 4. 先删旧条件,再插入新条件 @@ -306,6 +315,119 @@ public class AlertRuleServiceImpl implements IAlertRuleService } + /** + * 生成基础告警模板 + */ +// public String generateAlertTemplate(AlertRuleAddDto dto) { +// +// AlertRuleCondition condition = dto.getAlertRuleConditionList().get(0); +// String paramName = condition.getParamName(); +// +// // 查询字典表获取参数中文名 +// String paramCnName = dictAlarmParamMapper.selectCnNameByParamName(paramName); +// +// if (paramCnName == null || paramCnName.isEmpty()) { +// paramCnName = paramName; // 查不到就用原名 +// } +// +// String alertTemplate; +// if ("veneer_status".equalsIgnoreCase(paramName)) { +// // 单板运行状态模板 +// alertTemplate = "设备名称{equ_name},设备号{equ_number},单板号是{veneer_id},单板运行状态是{veneer_status} 超出正常范围,请尽快检查,"; +// } else { +// // 其他参数模板 +// alertTemplate = String.format("设备名称{equ_name},设备号{equ_number},%s是{%s},超出正常范围,请尽快检查,", paramCnName, paramName); +// } +// +// return alertTemplate; +// } + public String generateAlertTemplate(AlertRuleAddDto dto) { + AlertRuleCondition condition = dto.getAlertRuleConditionList().get(0); + String paramName = condition.getParamName(); + String operator = condition.getOperator(); + String compareValue = condition.getCompareValue(); + + // 查询字典表获取参数中文名 + String paramCnName = dictAlarmParamMapper.selectCnNameByParamName(paramName); + if (paramCnName == null || paramCnName.isEmpty()) { + paramCnName = paramName; // 查不到就用原名 + } + + String alertTemplate; + + if ("veneer_status".equalsIgnoreCase(paramName)) { + // 单板运行状态模板 + alertTemplate = "设备名称{equ_name},设备号{equ_number},单板号是{veneer_id},单板运行状态是{veneer_status} 超出正常范围,请尽快检查,"; + } else { + // 判断是否有比较符号和比较值(即为新逻辑) + if (operator != null && !operator.isEmpty()) { + // 处理比较符号 -> 中文描述 + String operatorDesc; + switch (operator) { + case "=": + operatorDesc = "等于"; + break; + case "!=": + operatorDesc = "不等于"; + break; + case "<": + operatorDesc = "小于"; + break; + case "<=": + operatorDesc = "小于等于"; + break; + case ">": + operatorDesc = "大于"; + break; + case ">=": + operatorDesc = "大于等于"; + break; + case "between": + // between 用逗号分隔两个值 + String[] range = compareValue != null ? compareValue.split(",") : new String[0]; + if (range.length == 2) { + operatorDesc = String.format("不在%s和%s之间", range[0], range[1]); + } else { + operatorDesc = "不在设定范围内"; + } + break; + case "CHANGED": + operatorDesc = "发生了改变"; + break; + default: + operatorDesc = operator; + } + + // 根据不同符号生成模板 + if ("CHANGED".equalsIgnoreCase(operator)) { + alertTemplate = String.format( + "设备名称{equ_name},设备号{equ_number},%s是{%s}, %s,请尽快检查,", + paramCnName, paramName, operatorDesc + ); + } else if ("between".equalsIgnoreCase(operator)) { + alertTemplate = String.format( + "设备名称{equ_name},设备号{equ_number},%s{%s} %s,请尽快检查,", + paramCnName, paramName, operatorDesc + ); + } else { + alertTemplate = String.format( + "设备名称{equ_name},设备号{equ_number},%s是{%s} %s %s,超出正常范围,请尽快检查,", + paramCnName, paramName, operatorDesc, compareValue + ); + } + + } else { + // 没有比较符号时,保持原逻辑 + alertTemplate = String.format( + "设备名称{equ_name},设备号{equ_number},%s是{%s},超出正常范围,请尽快检查,", + paramCnName, paramName + ); + } + } + + return alertTemplate; + } + /** diff --git a/ALOps_sys_backend/alops-system/src/main/resources/mapper/system/DictAlarmParamMapper.xml b/ALOps_sys_backend/alops-system/src/main/resources/mapper/system/DictAlarmParamMapper.xml index 181eda7d..4d0c1766 100644 --- a/ALOps_sys_backend/alops-system/src/main/resources/mapper/system/DictAlarmParamMapper.xml +++ b/ALOps_sys_backend/alops-system/src/main/resources/mapper/system/DictAlarmParamMapper.xml @@ -21,6 +21,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" select id, param_name, table_name, column_name, param_type from dict_alarm_param + + +