Commit 3aad554e authored by 王蔡翾's avatar 王蔡翾
Browse files

运行告警定时任务修改

parent 43f0dd0a
......@@ -2,7 +2,6 @@ package com.example.operation_platform.controller;
import com.example.operation_platform.model.dto.WorkerDTO;
import com.example.operation_platform.model.entity.WorkerEntity;
import com.example.operation_platform.model.vo.TermAndImgVO;
import com.example.operation_platform.model.vo.WorkerVO;
import com.example.operation_platform.service.WorkerService;
import com.platform.controller.BaseController;
......
......@@ -6,6 +6,7 @@ import com.example.operation_platform.model.entity.OperationDoEntity;
import com.example.operation_platform.model.vo.AllCountVO;
import com.example.operation_platform.model.vo.MessageNeedDataVo;
import com.example.operation_platform.model.vo.OperationDoVO;
import com.example.operation_platform.model.vo.RunLogWarnMessageVO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
......@@ -34,7 +35,7 @@ public interface OperationDoMapper extends BaseMapper<OperationDoEntity> {
List<Integer> queryRunLogIdList();
AllCountVO querycount();
List<Integer> queryNeedMessageRunLogId();
List<RunLogWarnMessageVO> queryNeedMessageRunLog();
List<OperationDoVO> queryEquipment();//获取设备和id
}
......
......@@ -23,5 +23,5 @@ public class OperationDoVO {
private String operationItems;//维保项目名
private String droneId;//无人机id
private String lastTime;//最近维保时间
private String runLogId;
private String runLogId;//告警日志id
}
package com.example.operation_platform.model.vo;
import lombok.Data;
import java.time.LocalDate;
@Data
public class RunLogWarnMessageVO {
private Integer id;
private Integer runLogId;
private String robotName;
private String stationName;
private LocalDate startTime;
private String WarnMessage;
}
......@@ -5,6 +5,7 @@ import com.example.operation_platform.model.dto.OperationDoDTO;
import com.example.operation_platform.model.entity.OperationDoEntity;
import com.example.operation_platform.model.vo.AllCountVO;
import com.example.operation_platform.model.vo.OperationDoVO;
import com.example.operation_platform.model.vo.RunLogWarnMessageVO;
import java.util.List;
......@@ -28,7 +29,7 @@ public interface OperationDoService extends IService<OperationDoEntity> {
List<Integer> queryRunLogIdList();
//首页统计
AllCountVO querycount();
List<Integer> queryNeedMessageRunLogId();
List<RunLogWarnMessageVO> queryNeedMessageRunLog();
List<OperationDoVO> queryEquipment();//获取设备和id
}
......@@ -7,6 +7,7 @@ import com.example.operation_platform.model.entity.RunLogEntity;
import com.example.operation_platform.model.entity.UavOperateLogEntity;
import com.example.operation_platform.model.vo.OperationRecordVO;
import com.example.operation_platform.model.vo.RunLogVO;
import com.example.operation_platform.model.vo.RunLogWarnMessageVO;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
......@@ -23,5 +24,5 @@ public interface RunLogService extends IService<RunLogEntity> {
String getMergedContentForRunLogId(String id);
List<Map<String, Object>> getAlertLogs();
List<RunLogWarnMessageVO> getAlertLogs();
}
......@@ -9,6 +9,7 @@ import com.example.operation_platform.model.entity.OperationDoEntity;
import com.example.operation_platform.model.entity.OperationItem;
import com.example.operation_platform.model.vo.AllCountVO;
import com.example.operation_platform.model.vo.OperationDoVO;
import com.example.operation_platform.model.vo.RunLogWarnMessageVO;
import com.example.operation_platform.service.OperationDoService;
import com.example.operation_platform.service.OperationItemService;
import com.google.common.collect.Lists;
......@@ -79,8 +80,8 @@ public class OperationDoServiceImpl extends ServiceImpl<OperationDoMapper, Opera
return this.baseMapper.querycount();
}
@Override
public List<Integer> queryNeedMessageRunLogId() {
return this.baseMapper.queryNeedMessageRunLogId();
public List<RunLogWarnMessageVO> queryNeedMessageRunLog() {
return this.baseMapper. queryNeedMessageRunLog();
}
@Override
public List<OperationDoVO> queryEquipment() {
......
......@@ -6,6 +6,7 @@ import com.example.operation_platform.model.dto.RunLogDTO;
import com.example.operation_platform.model.entity.RunLogEntity;
import com.example.operation_platform.model.entity.UavOperateLogEntity;
import com.example.operation_platform.model.vo.RunLogVO;
import com.example.operation_platform.model.vo.RunLogWarnMessageVO;
import com.example.operation_platform.service.OperateLogService;
import com.example.operation_platform.service.OperationDoService;
import com.example.operation_platform.service.RunLogService;
......@@ -114,22 +115,38 @@ public String getMergedContentForRunLogId(String id) {
.map(UavOperateLogEntity::getContent)
.collect(Collectors.joining(", "));
}
//处理需要发送消息的运行告警信息,并返回
@Override
public List<Map<String, Object>> getAlertLogs() {
List<Map<String, Object>> result = new ArrayList<>();
List<Integer> runLogIds = operationDoService.queryNeedMessageRunLogId();
for (Integer runLogId : runLogIds) {
public List<RunLogWarnMessageVO> getAlertLogs() {
// 最终返回的结果列表
List<RunLogWarnMessageVO> result = new ArrayList<>();
// 查询需要处理的消息日志
List<RunLogWarnMessageVO> runLogWarnMessages = operationDoService.queryNeedMessageRunLog();
// 创建 runLogId 到 RunLogWarnMessageVO 的映射,便于快速查找
Map<Integer, RunLogWarnMessageVO> runLogMap = runLogWarnMessages.stream()
.collect(Collectors.toMap(RunLogWarnMessageVO::getRunLogId, vo -> vo));
// 遍历所有需要检查的 runLogId
for (RunLogWarnMessageVO vo : runLogWarnMessages) {
Integer runLogId = vo.getRunLogId();
List<UavOperateLogEntity> logs = getInfoById(runLogId.toString());
// 检查每条日志内容
for (UavOperateLogEntity log : logs) {
if (log.getContent().contains("告警") || log.getContent().contains("失败")) {
Map<String, Object> entry = new HashMap<>();
entry.put("run_log_id", runLogId);
entry.put("content", log.getContent());
entry.put("dateTime", log.getDataTime());
result.add(entry);
// 找到对应的 RunLogWarnMessageVO 并设置 warnMessage
RunLogWarnMessageVO targetVo = runLogMap.get(runLogId);
if (targetVo != null) {
targetVo.setWarnMessage(log.getContent());
}
}
}
}
// 将处理后的列表添加到结果中
result.addAll(runLogWarnMessages);
return result;
}
}
package com.example.operation_platform.task;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.operation_platform.mapper.MessageMapper;
import com.example.operation_platform.mapper.OperationDoMapper;
import com.example.operation_platform.mapper.OperationJobMapper;
import com.example.operation_platform.mapper.WorkerMapper;
import com.example.operation_platform.model.entity.MessageEntity;
import com.example.operation_platform.model.entity.OperationDoEntity;
import com.example.operation_platform.model.entity.OperationJob;
import com.example.operation_platform.model.entity.WorkerEntity;
import com.example.operation_platform.model.vo.OperationJobVO;
import com.example.operation_platform.model.vo.RunLogWarnMessageVO;
import com.example.operation_platform.service.RunLogService;
import com.platform.utils.AliSmsUtil;
import org.json.JSONObject;
......@@ -21,7 +18,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
......@@ -40,21 +36,22 @@ public class RunLogTask {
private MessageMapper messageMapper;
@Autowired
private WorkerMapper workerMapper;
@Autowired
private OperationJobMapper operationJobMapper;
private final Map<Integer, OperationJobVO> alertTrackingMap = new ConcurrentHashMap<>();
private final Set<Integer> sentAlerts = ConcurrentHashMap.newKeySet(); // 用于跟踪已发送的 runLogId
/**
* 实时告警任务(每5分钟执行)
*/
@Scheduled(cron = "0 */5 * * * ?")
public void processRealTimeAlerts() {
// 1. 获取需要发送短信的完整数据列表
List<OperationJobVO> alertJobs = enrichOperationJobListWithWarnMessages(getBaseOperationList());
List<RunLogWarnMessageVO> alertJobs =runLogService.getAlertLogs();
// 2. 直接发送短信(列表已包含所有需要的信息)
alertJobs.forEach(this::sendAlertSms);
alertJobs.forEach(job -> {
if (!sentAlerts.contains(job.getRunLogId())) {
sendAlertSms(job);
sentAlerts.add(job.getRunLogId()); // 添加到已发送集合
}
});
}
/**
......@@ -62,23 +59,27 @@ public class RunLogTask {
*/
@Scheduled(cron = "0 0 0 * * ?")
public void dailyStatusCheck() {
// 1. 获取需要检查的完整数据列表
List<OperationJobVO> alertJobs = enrichOperationJobListWithWarnMessages(getBaseOperationList());
// 1. 获取需要检查的完整数据列表
List<RunLogWarnMessageVO> alertJobs = runLogService.getAlertLogs();
// 2. 判断 alertJobs 是否为空
if (alertJobs.isEmpty()) {
logger.info("每日状态检查未找到需要发送的告警信息,跳过发送。");
return;
}
// 3. 直接发送提醒
alertJobs.forEach(job -> {
sendAlertSms(job);
});
}
// 2. 检查状态并发送提醒
alertJobs.forEach(job -> {
OperationJob current = operationJobMapper.selectById(job.getOperationId());
if (current != null && current.getFinishStatus() != 4) {
sendAlertSms(job); // 使用列表中原有的完整信息
}
});
}
/**
* 发送短信(完全使用OperationJobVO中的数据)
*/
private void sendAlertSms(OperationJobVO job) {
List<String> phones = getPhoneByOperation(job.getOperationId());
private void sendAlertSms(RunLogWarnMessageVO job) {
List<String> phones = getPhoneByOperation(job.getId());
phones.forEach(phone -> {
try {
JSONObject params = new JSONObject();
......@@ -90,90 +91,13 @@ public class RunLogTask {
aliSmsUtil.sendSms(phone, params, "SMS_481615406");
logger.info("已发送短信至 {} | 操作ID: {} | 设备: {} | 告警: {}",
phone, job.getOperationId(), job.getRobotName(), job.getWarnMessage());
phone, job.getId(), job.getRobotName(), job.getWarnMessage());
} catch (Exception e) {
logger.error("发送失败: {} | 错误: {}", phone, e.getMessage());
}
});
}
/**
* 获取基础操作列表(仅获取operationId用于查询)
*/
private List<OperationJobVO> getBaseOperationList() {
// 只需要查询operationId用于关联数据
// List<OperationJob> operationJobs = operationJobMapper.selectList(
// new LambdaQueryWrapper<OperationJob>()
// .select(OperationJob::getId)
// );
List<OperationJob> operationJobs = operationJobMapper.selectList(
new LambdaQueryWrapper<>());
return operationJobs.stream()
.map(job -> {
OperationJobVO vo = new OperationJobVO();
BeanUtil.copyProperties(job, vo);
return vo;
})
.collect(Collectors.toList());
}
/**
* 发送每日提醒短信
*/
private void sendDailyReminder(OperationJobVO job) {
List<String> phones = getPhoneByOperation(job.getOperationId());
phones.forEach(phone -> {
try {
JSONObject params = new JSONObject();
params.put("stationName", job.getStationName());
params.put("device", job.getRobotName());
params.put("alert", job.getWarnMessage());
params.put("time", job.getStartTime());
aliSmsUtil.sendSms(phone, params, "SMS_481615406");
logger.info("已发送每日提醒至:{},任务ID:{}", phone, job.getOperationId());
} catch (Exception e) {
logger.error("发送每日提醒失败,手机号:{}", phone, e);
}
});
}
/**
* 为操作任务列表添加告警信息
*
* @param operationJobVOList 原始操作任务列表
* @return 带有告警信息的操作任务列表
*/
public List<OperationJobVO> enrichOperationJobListWithWarnMessages(List<OperationJobVO> operationJobVOList) {
// 获取告警日志
List<Map<String, Object>> alertLogs = runLogService.getAlertLogs();
// 创建日志ID和内容的映射
Map<Integer, String> logMap = new HashMap<>();
for (Map<String, Object> log : alertLogs) {
Integer logId = (Integer) log.get("run_log_id");
String content = (String) log.get("content");
if (logId != null && content != null && !content.isEmpty()) {
logMap.put(logId, content);
}
}
// 为每个操作任务添加告警信息
for (OperationJobVO operationJobVO : operationJobVOList) {
Integer runLogId = operationJobVO.getRunLogId();
if (runLogId != null && logMap.containsKey(runLogId)) {
operationJobVO.setWarnMessage(logMap.get(runLogId));
}
}
return operationJobVOList;
}
/**
* 获取操作关联的手机号列表
*
......
......@@ -171,15 +171,22 @@
(select count(*) from tb_operation_job tp where tp.runlog_or_operation = 0) as JobCount,
(select count(*) from tb_operation top where top.runlog_or_operation = 1) as warnCount
</select>
<select id="queryNeedMessageRunLogId" resultType="java.lang.Integer">
<!-- 查询运行告警信息,且在进行中的任务-->
<select id="queryNeedMessageRunLog" resultType="com.example.operation_platform.model.vo.RunLogWarnMessageVO">
select
top.tb_run_log_id AS runLogId
top.start_time AS startTime,
top.tb_run_log_id AS runLogId,
COALESCE(br.name, dr.name) AS robotName,
bs.name AS stationName,
top.id AS id
from tb_operation top
left join
tb_operation_job tp on top.id = tp.operation_id
LEFT JOIN bs_robot br ON top.robot_id = br.id
LEFT JOIN bs_station bs ON top.station_id = bs.id
LEFT JOIN tb_operation_job tb ON top.id = tb.operation_id
LEFT JOIN uav_drone dr ON top.drone_id = dr.id
where
top.runlog_or_operation = 1
AND tp.finish_status NOT IN (2, 4)
AND tb.finish_status NOT IN (2, 4)
</select>
<select id="queryEquipment" resultType="com.example.operation_platform.model.vo.OperationDoVO">
select DISTINCT
......
......@@ -379,6 +379,4 @@
</where>
ORDER BY top.start_time DESC
</select>
</mapper>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment