From 2754969dfc6b7b1e410787a9d5e8ce9d6318f043 Mon Sep 17 00:00:00 2001 From: hzj <826100833@qq.com> Date: Thu, 11 Jun 2026 16:59:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9A=82=E6=80=81=E9=AB=98=E7=BA=A7=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E5=BC=82=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/EventDetailServiceImpl.java | 121 +++++++++++------- 1 file changed, 78 insertions(+), 43 deletions(-) diff --git a/pqs-event/event-common/src/main/java/com/njcn/event/common/service/impl/EventDetailServiceImpl.java b/pqs-event/event-common/src/main/java/com/njcn/event/common/service/impl/EventDetailServiceImpl.java index 706e0e228..46b0048e9 100644 --- a/pqs-event/event-common/src/main/java/com/njcn/event/common/service/impl/EventDetailServiceImpl.java +++ b/pqs-event/event-common/src/main/java/com/njcn/event/common/service/impl/EventDetailServiceImpl.java @@ -42,11 +42,14 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; +import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.Duration; import java.time.LocalDateTime; import java.util.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import java.util.stream.Collectors; /** @@ -69,7 +72,8 @@ public class EventDetailServiceImpl extends ServiceImpl getEventDetailData(String id, String startTime, String endTime) { @@ -173,49 +177,7 @@ public class EventDetailServiceImpl extends ServiceImpl { + // 异步任务内执行分析及更新 + analyzeAndUpdateEvent(rmpEventDetailPO.getEventId(), rmpEventDetailPO.getLineId(), rmpEventDetailPO.getWavePath()); + }, executor).exceptionally(ex -> { + // exceptionally 也可以捕获异常,但内部已 try-catch,这里仅做兜底日志 + log.error("异步任务未预期的异常, eventId: {}", rmpEventDetailPO.getEventId(), ex); + return null; + }); + } return true; } + private void analyzeAndUpdateEvent(String eventId, String lineId, String wavePath) { + // 重新查询实体,避免跨线程持久化对象问题 + RmpEventDetailPO po = this.getById(eventId); + if (po == null) { + log.warn("异步分析时事件记录不存在, eventId: {}", eventId); + return; + } + + try { + // Feign 调用链 + LineDetailDataVO lineDetailData = lineFeignClient.getLineDetailData(lineId).getData(); + String ip = lineDetailData.getIp(); + EventAnalysisDTO eventAnalysisDTO = new EventAnalysisDTO(); + eventAnalysisDTO.setIp(ip); + eventAnalysisDTO.setWaveName(wavePath); + + EventAnalysisDTO result = eventCauseFeignClient.analysisCauseAndType(eventAnalysisDTO).getData(); + + // 转换结果 + String reasonCode, typeCode; + if (Objects.isNull(result.getCause())) { + reasonCode = reasonReflection(0); + } else { + reasonCode = reasonReflection(result.getCause()); + } + if (Objects.isNull(result.getType())) { + typeCode = advanceTypeReflection(10); + } else { + typeCode = advanceTypeReflection(result.getType()); + } + + DictData advancereason = dicDataFeignClient.getDicDataByCode(reasonCode).getData(); + DictData advanceType = dicDataFeignClient.getDicDataByCode(typeCode).getData(); + + po.setAdvanceReason(advancereason.getId()); + po.setAdvanceType(advanceType.getId()); + if (Objects.equals(result.getCauseFlag(), 1) && Objects.equals(result.getTypeFlag(), 1)) { + po.setDealFlag(1); + } else { + po.setDealFlag(0); + } + // 更新数据库 + this.updateById(po); + } catch (Exception e) { + // 关键点:所有异常被捕获并记录日志,不会抛出到主线程 + log.error("异步分析失败, eventId={}, wavePath={}", eventId, wavePath, e); + // 可选:设置一个错误标志,避免一直处于未分析状态 + po.setDealFlag(0); + this.updateById(po); + } + } + private String advanceTypeReflection(Integer type) { String result = DicDataEnum.TYPE_REST.getCode(); switch (type) { @@ -356,5 +381,15 @@ public class EventDetailServiceImpl extends ServiceImpl