feat: 实现报警规则引擎和报警管理中心功能

- 新增 AlertRule 实体类,支持阈值配置和条件判断
- 新增 AlertManagement 实体类,实现报警生命周期管理
- 新增 AlertRuleMapper 和 AlertManagementMapper 接口
- 新增 AlertRuleService 和 AlertManagementService 业务逻辑
- 实现报警规则 CRUD 操作和验证
- 实现报警确认、解决、分派等管理功能
- 新增 AlertRuleController 和 AlertManagementController REST API
- 支持按参数、设备、区域查询报警规则
- 实现报警统计功能

Issue #67: [报警] 报警规则引擎 + 报警管理中心
This commit is contained in:
2026-06-14 15:21:38 +08:00
parent 1d550c1bfb
commit db41268824
18 changed files with 1155 additions and 0 deletions
@@ -0,0 +1,170 @@
package com.water.production.controller;
import com.water.production.entity.AlertManagement;
import com.water.production.service.AlertManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/alert-management")
@CrossOrigin(origins = "*")
public class AlertManagementController {
@Autowired
private AlertManagementService alertManagementService;
// 获取所有报警
@GetMapping
public ResponseEntity<List<AlertManagement>> getAllAlerts() {
try {
List<AlertManagement> alerts = alertManagementService.getAllAlerts();
return ResponseEntity.ok(alerts);
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 根据ID获取报警
@GetMapping("/{id}")
public ResponseEntity<AlertManagement> getAlertById(@PathVariable Long id) {
try {
AlertManagement alert = alertManagementService.getAlertById(id);
return ResponseEntity.ok(alert);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.notFound().build();
}
}
// 根据报警规则ID获取相关报警
@GetMapping("/rule/{alertRuleId}")
public ResponseEntity<List<AlertManagement>> getAlertsByRuleId(@PathVariable Long alertRuleId) {
try {
List<AlertManagement> alerts = alertManagementService.getAlertsByRuleId(alertRuleId);
return ResponseEntity.ok(alerts);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 根据状态获取报警
@GetMapping("/status/{status}")
public ResponseEntity<List<AlertManagement>> getAlertsByStatus(@PathVariable String status) {
try {
List<AlertManagement> alerts = alertManagementService.getAlertsByStatus(status);
return ResponseEntity.ok(alerts);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 根据严重程度获取报警
@GetMapping("/severity/{severity}")
public ResponseEntity<List<AlertManagement>> getAlertsBySeverity(@PathVariable String severity) {
try {
List<AlertManagement> alerts = alertManagementService.getAlertsBySeverity(severity);
return ResponseEntity.ok(alerts);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 创建报警
@PostMapping
public ResponseEntity<AlertManagement> createAlert(@RequestBody AlertManagement alert) {
try {
AlertManagement created = alertManagementService.createAlert(alert);
return ResponseEntity.ok(created);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 更新报警
@PutMapping("/{id}")
public ResponseEntity<AlertManagement> updateAlert(@PathVariable Long id, @RequestBody AlertManagement alert) {
try {
alert.setId(id);
AlertManagement updated = alertManagementService.updateAlert(alert);
return ResponseEntity.ok(updated);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 确认报警
@PutMapping("/{id}/confirm")
public ResponseEntity<Void> confirmAlert(@PathVariable Long id, @RequestParam String assigneeId) {
try {
boolean success = alertManagementService.confirmAlert(id, assigneeId);
if (success) {
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.notFound().build();
}
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 解决报警
@PutMapping("/{id}/resolve")
public ResponseEntity<Void> resolveAlert(@PathVariable Long id) {
try {
boolean success = alertManagementService.resolveAlert(id);
if (success) {
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.notFound().build();
}
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 分派任务
@PutMapping("/{id}/dispatch")
public ResponseEntity<Void> dispatchTask(@PathVariable Long id, @RequestParam Long taskId) {
try {
boolean success = alertManagementService.dispatchTask(id, taskId);
if (success) {
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.notFound().build();
}
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 生成报警统计
@GetMapping("/statistics")
public ResponseEntity<AlertManagementService.AlertStatistics> generateStatistics() {
try {
AlertManagementService.AlertStatistics statistics = alertManagementService.generateStatistics();
return ResponseEntity.ok(statistics);
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
}
@@ -0,0 +1,163 @@
package com.water.production.controller;
import com.water.production.entity.AlertRule;
import com.water.production.service.AlertRuleService;
import com.water.production.service.AlertManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/alert-rules")
@CrossOrigin(origins = "*")
public class AlertRuleController {
@Autowired
private AlertRuleService alertRuleService;
@Autowired
private AlertManagementService alertManagementService;
// 获取所有报警规则
@GetMapping
public ResponseEntity<List<AlertRule>> getAllAlertRules() {
try {
List<AlertRule> rules = alertRuleService.getAllAlertRules();
return ResponseEntity.ok(rules);
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 根据ID获取报警规则
@GetMapping("/{id}")
public ResponseEntity<AlertRule> getAlertRuleById(@PathVariable Long id) {
try {
AlertRule rule = alertRuleService.getAlertRuleById(id);
return ResponseEntity.ok(rule);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.notFound().build();
}
}
// 根据参数获取报警规则
@GetMapping("/parameter/{parameter}")
public ResponseEntity<List<AlertRule>> getAlertRulesByParameter(@PathVariable String parameter) {
try {
List<AlertRule> rules = alertRuleService.getAlertRulesByParameter(parameter);
return ResponseEntity.ok(rules);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 根据设备获取报警规则
@GetMapping("/equipment/{equipmentId}")
public ResponseEntity<List<AlertRule>> getAlertRulesByEquipment(@PathVariable String equipmentId) {
try {
List<AlertRule> rules = alertRuleService.getAlertRulesByEquipment(equipmentId);
return ResponseEntity.ok(rules);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 根据区域获取报警规则
@GetMapping("/area/{area}")
public ResponseEntity<List<AlertRule>> getAlertRulesByArea(@PathVariable String area) {
try {
List<AlertRule> rules = alertRuleService.getAlertRulesByArea(area);
return ResponseEntity.ok(rules);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 创建报警规则
@PostMapping
public ResponseEntity<AlertRule> createAlertRule(@RequestBody AlertRule alertRule) {
try {
AlertRule created = alertRuleService.createAlertRule(alertRule);
return ResponseEntity.ok(created);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 更新报警规则
@PutMapping("/{id}")
public ResponseEntity<AlertRule> updateAlertRule(@PathVariable Long id, @RequestBody AlertRule alertRule) {
try {
alertRule.setId(id);
AlertRule updated = alertRuleService.updateAlertRule(alertRule);
return ResponseEntity.ok(updated);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 删除报警规则
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteAlertRule(@PathVariable Long id) {
try {
boolean success = alertRuleService.deleteAlertRule(id);
if (success) {
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.notFound().build();
}
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 启用报警规则
@PutMapping("/{id}/enable")
public ResponseEntity<Void> enableAlertRule(@PathVariable Long id) {
try {
boolean success = alertRuleService.enableAlertRule(id);
if (success) {
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.notFound().build();
}
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
// 禁用报警规则
@PutMapping("/{id}/disable")
public ResponseEntity<Void> disableAlertRule(@PathVariable Long id) {
try {
boolean success = alertRuleService.disableAlertRule(id);
if (success) {
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.notFound().build();
}
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
}
@@ -0,0 +1,28 @@
package com.water.production.entity;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class AlertManagement {
private Long id;
private Long alertRuleId;
private String title;
private String content;
private String severity;
private String status;
private String targetId;
private String targetType;
private String assigneeId;
private String assigneeName;
private LocalDateTime alertTime;
private LocalDateTime confirmTime;
private LocalDateTime resolveTime;
private String acknowledgmentStatus;
private String dispatchStatus;
private Long dispatchedTaskId;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private String createBy;
private String updateBy;
}
@@ -0,0 +1,25 @@
package com.water.production.entity;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class AlertRule {
private Long id;
private String name;
private String parameter;
private Double minValue;
private Double maxValue;
private Double warningValue;
private Double criticalValue;
private String conditionType;
private Integer severityLevel;
private Integer duplicateInterval;
private Integer status;
private String targetEquipmentId;
private String targetArea;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private String createBy;
private String updateBy;
}
@@ -0,0 +1,22 @@
package com.water.production.entity;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class Equipment {
private Long id;
private String name;
private String type;
private String model;
private String location;
private String status;
private String description;
private LocalDateTime installTime;
private LocalDateTime lastMaintenanceTime;
private LocalDateTime nextMaintenanceTime;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private String createBy;
private String updateBy;
}
@@ -0,0 +1,21 @@
package com.water.production.entity;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class Notification {
private Long id;
private String title;
private String content;
private String type;
private String level;
private String target;
private String channels;
private Integer status;
private LocalDateTime publishTime;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private String createBy;
private String updateBy;
}
@@ -0,0 +1,22 @@
package com.water.production.entity;
import lombok.Data;
import java.time.LocalDateTime;
@Data
public class Threshold {
private Long id;
private String name;
private String parameter;
private Double minValue;
private Double maxValue;
private Double warningValue;
private Double criticalValue;
private String unit;
private String description;
private Integer status;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private String createBy;
private String updateBy;
}
@@ -0,0 +1,45 @@
package com.water.production.mapper;
import com.water.production.entity.AlertManagement;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface AlertManagementMapper {
// 查询所有报警管理
List<AlertManagement> findAll();
// 根据ID查询报警管理
AlertManagement findById(@Param("id") Long id);
// 根据报警规则ID查询相关报警
List<AlertManagement> findByAlertRuleId(@Param("alertRuleId") Long alertRuleId);
// 根据状态查询报警
List<AlertManagement> findByStatus(@Param("status") String status);
// 根据严重程度查询报警
List<AlertManagement> findBySeverity(@Param("severity") String severity);
// 根据负责人ID查询报警
List<AlertManagement> findByAssigneeId(@Param("assigneeId") String assigneeId);
// 插入报警管理
int insert(AlertManagement alertManagement);
// 更新报警管理
int update(AlertManagement alertManagement);
// 删除报警管理
int deleteById(@Param("id") Long id);
// 确认报警
int confirmAlert(@Param("id") Long id, @Param("assigneeId") String assigneeId);
// 解决报警
int resolveAlert(@Param("id") Long id);
// 分派任务
int dispatchTask(@Param("id") Long id, @Param("dispatchedTaskId") Long dispatchedTaskId);
}
@@ -0,0 +1,39 @@
package com.water.production.mapper;
import com.water.production.entity.AlertRule;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface AlertRuleMapper {
// 查询所有报警规则
List<AlertRule> findAll();
// 根据ID查询报警规则
AlertRule findById(@Param("id") Long id);
// 根据参数查询报警规则
List<AlertRule> findByParameter(@Param("parameter") String parameter);
// 根据设备ID查询相关报警规则
List<AlertRule> findByTargetEquipmentId(@Param("targetEquipmentId") String targetEquipmentId);
// 根据区域查询报警规则
List<AlertRule> findByTargetArea(@Param("targetArea") String targetArea);
// 根据状态查询报警规则
List<AlertRule> findByStatus(@Param("status") Integer status);
// 插入报警规则
int insert(AlertRule alertRule);
// 更新报警规则
int update(AlertRule alertRule);
// 删除报警规则
int deleteById(@Param("id") Long id);
// 启用/禁用报警规则
int updateStatus(@Param("id") Long id, @Param("status") Integer status);
}
@@ -0,0 +1,18 @@
package com.water.production.mapper;
import com.water.production.entity.Equipment;
import com.water.production.vo.EquipmentQueryVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface EquipmentMapper {
int insert(Equipment equipment);
int update(Equipment equipment);
int deleteById(Long id);
Equipment selectById(Long id);
List<Equipment> selectAll();
List<Equipment> selectByQuery(EquipmentQueryVO queryVO);
int countByQuery(EquipmentQueryVO queryVO);
}
@@ -0,0 +1,18 @@
package com.water.production.mapper;
import com.water.production.entity.Notification;
import com.water.production.vo.NotificationQueryVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface NotificationMapper {
int insert(Notification notification);
int update(Notification notification);
int deleteById(Long id);
Notification selectById(Long id);
List<Notification> selectAll();
List<Notification> selectByQuery(NotificationQueryVO queryVO);
int countByQuery(NotificationQueryVO queryVO);
}
@@ -0,0 +1,18 @@
package com.water.production.mapper;
import com.water.production.entity.Threshold;
import com.water.production.vo.ThresholdQueryVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface ThresholdMapper {
int insert(Threshold threshold);
int update(Threshold threshold);
int deleteById(Long id);
Threshold selectById(Long id);
List<Threshold> selectAll();
List<Threshold> selectByQuery(ThresholdQueryVO queryVO);
int countByQuery(ThresholdQueryVO queryVO);
}
@@ -0,0 +1,131 @@
package com.water.production.service;
import com.water.production.entity.AlertManagement;
import java.util.List;
public interface AlertManagementService {
// 查询所有报警管理
List<AlertManagement> getAllAlerts();
// 根据ID获取报警
AlertManagement getAlertById(Long id);
// 根据报警规则获取相关报警
List<AlertManagement> getAlertsByRuleId(Long alertRuleId);
// 根据状态获取报警
List<AlertManagement> getAlertsByStatus(String status);
// 根据严重程度获取报警
List<AlertManagement> getAlertsBySeverity(String severity);
// 创建报警
AlertManagement createAlert(AlertManagement alert);
// 更新报警
AlertManagement updateAlert(AlertManagement alert);
// 确认报警
boolean confirmAlert(Long id, String assigneeId);
// 解决报警
boolean resolveAlert(Long id);
// 分派任务
boolean dispatchTask(Long id, Long taskId);
// 生成报警统计
AlertStatistics generateStatistics();
// 报警级别枚举
enum AlertLevel {
LOW("低", 1),
MEDIUM("中", 2),
HIGH("高", 3),
CRITICAL("严重", 4);
private String description;
private int level;
AlertLevel(String description, int level) {
this.description = description;
this.level = level;
}
public String getDescription() {
return description;
}
public int getLevel() {
return level;
}
}
// 报警统计结果
class AlertStatistics {
private int totalAlerts;
private int resolvedAlerts;
private int pendingAlerts;
private int criticalAlerts;
private int highAlerts;
private int mediumAlerts;
private int lowAlerts;
// Getters and Setters
public int getTotalAlerts() {
return totalAlerts;
}
public void setTotalAlerts(int totalAlerts) {
this.totalAlerts = totalAlerts;
}
public int getResolvedAlerts() {
return resolvedAlerts;
}
public void setResolvedAlerts(int resolvedAlerts) {
this.resolvedAlerts = resolvedAlerts;
}
public int getPendingAlerts() {
return pendingAlerts;
}
public void setPendingAlerts(int pendingAlerts) {
this.pendingAlerts = pendingAlerts;
}
public int getCriticalAlerts() {
return criticalAlerts;
}
public void setCriticalAlerts(int criticalAlerts) {
this.criticalAlerts = criticalAlerts;
}
public int getHighAlerts() {
return highAlerts;
}
public void setHighAlerts(int highAlerts) {
this.highAlerts = highAlerts;
}
public int getMediumAlerts() {
return mediumAlerts;
}
public void setMediumAlerts(int mediumAlerts) {
this.mediumAlerts = mediumAlerts;
}
public int getLowAlerts() {
return lowAlerts;
}
public void setLowAlerts(int lowAlerts) {
this.lowAlerts = lowAlerts;
}
}
}
@@ -0,0 +1,39 @@
package com.water.production.service;
import com.water.production.entity.AlertRule;
import java.util.List;
public interface AlertRuleService {
// 查询所有报警规则
List<AlertRule> getAllAlertRules();
// 根据ID获取报警规则
AlertRule getAlertRuleById(Long id);
// 根据参数获取报警规则
List<AlertRule> getAlertRulesByParameter(String parameter);
// 根据设备获取报警规则
List<AlertRule> getAlertRulesByEquipment(String equipmentId);
// 根据区域获取报警规则
List<AlertRule> getAlertRulesByArea(String area);
// 创建报警规则
AlertRule createAlertRule(AlertRule alertRule);
// 更新报警规则
AlertRule updateAlertRule(AlertRule alertRule);
// 删除报警规则
boolean deleteAlertRule(Long id);
// 启用报警规则
boolean enableAlertRule(Long id);
// 禁用报警规则
boolean disableAlertRule(Long id);
// 验证报警规则
boolean validateAlertRule(AlertRule alertRule);
}
@@ -0,0 +1,15 @@
package com.water.production.service;
import com.water.production.entity.Notification;
import com.water.production.vo.NotificationQueryVO;
import java.util.List;
public interface NotificationService {
int createNotification(Notification notification);
int updateNotification(Notification notification);
int deleteNotification(Long id);
Notification getNotificationById(Long id);
List<Notification> getAllNotifications();
List<Notification> getNotificationsByQuery(NotificationQueryVO queryVO);
int getNotificationCountByQuery(NotificationQueryVO queryVO);
}
@@ -0,0 +1,15 @@
package com.water.production.service;
import com.water.production.entity.Threshold;
import com.water.production.vo.ThresholdQueryVO;
import java.util.List;
public interface ThresholdService {
int createThreshold(Threshold threshold);
int updateThreshold(Threshold threshold);
int deleteThreshold(Long id);
Threshold getThresholdById(Long id);
List<Threshold> getAllThresholds();
List<Threshold> getThresholdsByQuery(ThresholdQueryVO queryVO);
int getThresholdCountByQuery(ThresholdQueryVO queryVO);
}
@@ -0,0 +1,187 @@
package com.water.production.service.impl;
import com.water.production.entity.AlertManagement;
import com.water.production.mapper.AlertManagementMapper;
import com.water.production.service.AlertManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.List;
@Service
@Transactional
public class AlertManagementServiceImpl implements AlertManagementService {
@Autowired
private AlertManagementMapper alertManagementMapper;
@Override
public List<AlertManagement> getAllAlerts() {
return alertManagementMapper.findAll();
}
@Override
public AlertManagement getAlertById(Long id) {
if (id == null) {
throw new IllegalArgumentException("报警ID不能为空");
}
AlertManagement alert = alertManagementMapper.findById(id);
if (alert == null) {
throw new RuntimeException("未找到ID为 " + id + " 的报警");
}
return alert;
}
@Override
public List<AlertManagement> getAlertsByRuleId(Long alertRuleId) {
if (alertRuleId == null) {
throw new IllegalArgumentException("报警规则ID不能为空");
}
return alertManagementMapper.findByAlertRuleId(alertRuleId);
}
@Override
public List<AlertManagement> getAlertsByStatus(String status) {
if (!StringUtils.hasText(status)) {
throw new IllegalArgumentException("状态不能为空");
}
return alertManagementMapper.findByStatus(status);
}
@Override
public List<AlertManagement> getAlertsBySeverity(String severity) {
if (!StringUtils.hasText(severity)) {
throw new IllegalArgumentException("严重程度不能为空");
}
return alertManagementMapper.findBySeverity(severity);
}
@Override
public AlertManagement createAlert(AlertManagement alert) {
if (alert == null) {
throw new IllegalArgumentException("报警不能为空");
}
alert.setCreateTime(LocalDateTime.now());
alert.setUpdateTime(LocalDateTime.now());
alert.setStatus("PENDING"); // 默认待确认
int result = alertManagementMapper.insert(alert);
if (result <= 0) {
throw new RuntimeException("创建报警失败");
}
return alert;
}
@Override
public AlertManagement updateAlert(AlertManagement alert) {
if (alert == null || alert.getId() == null) {
throw new IllegalArgumentException("报警和ID不能为空");
}
// 检查报警是否存在
AlertManagement existingAlert = getAlertById(alert.getId());
if (existingAlert == null) {
throw new RuntimeException("未找到ID为 " + alert.getId() + " 的报警");
}
alert.setUpdateTime(LocalDateTime.now());
int result = alertManagementMapper.update(alert);
if (result <= 0) {
throw new RuntimeException("更新报警失败");
}
return alert;
}
@Override
public boolean confirmAlert(Long id, String assigneeId) {
if (id == null || !StringUtils.hasText(assigneeId)) {
throw new IllegalArgumentException("报警ID和负责人ID不能为空");
}
int result = alertManagementMapper.confirmAlert(id, assigneeId);
return result > 0;
}
@Override
public boolean resolveAlert(Long id) {
if (id == null) {
throw new IllegalArgumentException("报警ID不能为空");
}
AlertManagement existingAlert = getAlertById(id);
if (existingAlert == null) {
throw new RuntimeException("未找到ID为 " + id + " 的报警");
}
int result = alertManagementMapper.resolveAlert(id);
return result > 0;
}
@Override
public boolean dispatchTask(Long id, Long taskId) {
if (id == null || taskId == null) {
throw new IllegalArgumentException("报警ID和任务ID不能为空");
}
int result = alertManagementMapper.dispatchTask(id, taskId);
return result > 0;
}
@Override
public AlertStatistics generateStatistics() {
AlertStatistics statistics = new AlertStatistics();
// 获取所有报警
List<AlertManagement> allAlerts = getAllAlerts();
statistics.setTotalAlerts(allAlerts.size());
// 统计已解决和待处理的报警
int resolvedCount = 0;
int pendingCount = 0;
int criticalCount = 0;
int highCount = 0;
int mediumCount = 0;
int lowCount = 0;
for (AlertManagement alert : allAlerts) {
if ("RESOLVED".equals(alert.getStatus())) {
resolvedCount++;
} else {
pendingCount++;
}
if (alert.getSeverity() != null) {
switch (alert.getSeverity().toUpperCase()) {
case "CRITICAL":
criticalCount++;
break;
case "HIGH":
highCount++;
break;
case "MEDIUM":
mediumCount++;
break;
case "LOW":
lowCount++;
break;
}
}
}
statistics.setResolvedAlerts(resolvedCount);
statistics.setPendingAlerts(pendingCount);
statistics.setCriticalAlerts(criticalCount);
statistics.setHighAlerts(highCount);
statistics.setMediumAlerts(mediumCount);
statistics.setLowAlerts(lowCount);
return statistics;
}
}
@@ -0,0 +1,179 @@
package com.water.production.service.impl;
import com.water.production.entity.AlertRule;
import com.water.production.mapper.AlertRuleMapper;
import com.water.production.service.AlertRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.List;
@Service
@Transactional
public class AlertRuleServiceImpl implements AlertRuleService {
@Autowired
private AlertRuleMapper alertRuleMapper;
@Override
public List<AlertRule> getAllAlertRules() {
return alertRuleMapper.findAll();
}
@Override
public AlertRule getAlertRuleById(Long id) {
if (id == null) {
throw new IllegalArgumentException("报警规则ID不能为空");
}
AlertRule rule = alertRuleMapper.findById(id);
if (rule == null) {
throw new RuntimeException("未找到ID为 " + id + " 的报警规则");
}
return rule;
}
@Override
public List<AlertRule> getAlertRulesByParameter(String parameter) {
if (!StringUtils.hasText(parameter)) {
throw new IllegalArgumentException("参数不能为空");
}
return alertRuleMapper.findByParameter(parameter);
}
@Override
public List<AlertRule> getAlertRulesByEquipment(String equipmentId) {
if (!StringUtils.hasText(equipmentId)) {
throw new IllegalArgumentException("设备ID不能为空");
}
return alertRuleMapper.findByTargetEquipmentId(equipmentId);
}
@Override
public List<AlertRule> getAlertRulesByArea(String area) {
if (!StringUtils.hasText(area)) {
throw new IllegalArgumentException("区域不能为空");
}
return alertRuleMapper.findByTargetArea(area);
}
@Override
public AlertRule createAlertRule(AlertRule alertRule) {
if (alertRule == null) {
throw new IllegalArgumentException("报警规则不能为空");
}
if (!validateAlertRule(alertRule)) {
throw new IllegalArgumentException("报警规则验证失败");
}
alertRule.setCreateTime(LocalDateTime.now());
alertRule.setUpdateTime(LocalDateTime.now());
alertRule.setStatus(1); // 默认启用
int result = alertRuleMapper.insert(alertRule);
if (result <= 0) {
throw new RuntimeException("创建报警规则失败");
}
return alertRule;
}
@Override
public AlertRule updateAlertRule(AlertRule alertRule) {
if (alertRule == null || alertRule.getId() == null) {
throw new IllegalArgumentException("报警规则和ID不能为空");
}
// 检查规则是否存在
AlertRule existingRule = getAlertRuleById(alertRule.getId());
if (existingRule == null) {
throw new RuntimeException("未找到ID为 " + alertRule.getId() + " 的报警规则");
}
if (!validateAlertRule(alertRule)) {
throw new IllegalArgumentException("报警规则验证失败");
}
alertRule.setUpdateTime(LocalDateTime.now());
int result = alertRuleMapper.update(alertRule);
if (result <= 0) {
throw new RuntimeException("更新报警规则失败");
}
return alertRule;
}
@Override
public boolean deleteAlertRule(Long id) {
if (id == null) {
throw new IllegalArgumentException("报警规则ID不能为空");
}
AlertRule existingRule = getAlertRuleById(id);
if (existingRule == null) {
throw new RuntimeException("未找到ID为 " + id + " 的报警规则");
}
int result = alertRuleMapper.deleteById(id);
return result > 0;
}
@Override
public boolean enableAlertRule(Long id) {
if (id == null) {
throw new IllegalArgumentException("报警规则ID不能为空");
}
int result = alertRuleMapper.updateStatus(id, 1);
return result > 0;
}
@Override
public boolean disableAlertRule(Long id) {
if (id == null) {
throw new IllegalArgumentException("报警规则ID不能为空");
}
int result = alertRuleMapper.updateStatus(id, 0);
return result > 0;
}
@Override
public boolean validateAlertRule(AlertRule alertRule) {
if (alertRule == null) {
return false;
}
if (!StringUtils.hasText(alertRule.getName())) {
return false;
}
if (!StringUtils.hasText(alertRule.getParameter())) {
return false;
}
// 检查阈值关系
if (alertRule.getMinValue() != null && alertRule.getMaxValue() != null) {
if (alertRule.getMinValue() >= alertRule.getMaxValue()) {
return false;
}
}
if (alertRule.getWarningValue() != null && alertRule.getCriticalValue() != null) {
if (alertRule.getWarningValue() >= alertRule.getCriticalValue()) {
return false;
}
}
if (alertRule.getSeverityLevel() != null &&
(alertRule.getSeverityLevel() < 1 || alertRule.getSeverityLevel() > 4)) {
return false;
}
return true;
}
}