From dc3f9cc91759823ce59486f19b138be4b296a0f1 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期二, 28 四月 2026 09:43:28 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/trace/StationTaskTraceRegistry.java |  229 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 229 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/zy/core/trace/StationTaskTraceRegistry.java b/src/main/java/com/zy/core/trace/StationTaskTraceRegistry.java
index 31a92e6..d56af70 100644
--- a/src/main/java/com/zy/core/trace/StationTaskTraceRegistry.java
+++ b/src/main/java/com/zy/core/trace/StationTaskTraceRegistry.java
@@ -4,10 +4,14 @@
 import com.zy.asrs.domain.vo.StationTaskTraceEventVo;
 import com.zy.asrs.domain.vo.StationTaskTraceSegmentVo;
 import com.zy.asrs.domain.vo.StationTaskTraceVo;
+import com.zy.asrs.entity.WrkMast;
+import com.zy.asrs.service.WrkMastService;
 import com.zy.common.utils.RedisUtil;
+import com.zy.core.enums.WrkStsType;
 import com.zy.core.model.command.StationCommand;
 import com.zy.core.enums.RedisKeyType;
 import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -19,6 +23,7 @@
 import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
 
+@Slf4j
 @Component
 public class StationTaskTraceRegistry {
 
@@ -36,6 +41,9 @@
 
     @Autowired
     private RedisUtil redisUtil;
+
+    @Autowired
+    private WrkMastService wrkMastService;
 
     private final Map<Integer, TraceTaskState> taskStateMap = new ConcurrentHashMap<>();
     private volatile boolean loadedFromRedis = false;
@@ -146,14 +154,72 @@
         persistState(state);
     }
 
+    public void updateLoopHint(Integer taskNo,
+                               Boolean loopAlertActive,
+                               String loopAlertType,
+                               String loopAlertText,
+                               Integer loopAlertCount,
+                               Map<String, Object> details) {
+        ensureCacheLoaded();
+        TraceTaskState state = taskStateMap.get(taskNo);
+        if (state == null) {
+            return;
+        }
+        state.updateLoopHint(loopAlertActive, loopAlertType, loopAlertText, loopAlertCount, details);
+        persistState(state);
+    }
+
     public List<StationTaskTraceVo> listLatestTraces() {
         ensureCacheLoaded();
         cleanupExpired();
+        reconcileInactiveBusinessTasks();
         List<StationTaskTraceVo> result = new ArrayList<>();
         for (TraceTaskState state : taskStateMap.values()) {
             if (state != null) {
                 result.add(state.toVo());
             }
+        }
+        result.sort(new Comparator<StationTaskTraceVo>() {
+            @Override
+            public int compare(StationTaskTraceVo a, StationTaskTraceVo b) {
+                long av = a.getUpdatedAt() == null ? 0L : a.getUpdatedAt();
+                long bv = b.getUpdatedAt() == null ? 0L : b.getUpdatedAt();
+                return Long.compare(bv, av);
+            }
+        });
+        return result;
+    }
+
+    public List<StationTaskTraceVo> listActiveTraces() {
+        List<StationTaskTraceVo> latestTraces = listLatestTraces();
+        List<StationTaskTraceVo> result = new ArrayList<>();
+        for (StationTaskTraceVo traceVo : latestTraces) {
+            if (traceVo == null) {
+                continue;
+            }
+            String status = traceVo.getStatus();
+            if (STATUS_WAITING.equals(status)
+                    || STATUS_RUNNING.equals(status)
+                    || STATUS_REROUTED.equals(status)) {
+                result.add(traceVo);
+            }
+        }
+        return result;
+    }
+
+    public List<StationTaskTraceVo> listPlanningActiveTraceSnapshots() {
+        ensureCacheLoaded();
+        cleanupExpired();
+        List<StationTaskTraceVo> result = new ArrayList<>();
+        for (TraceTaskState state : taskStateMap.values()) {
+            if (state == null) {
+                continue;
+            }
+            StationTaskTraceVo traceVo = state.toPlanningVo();
+            if (traceVo == null) {
+                continue;
+            }
+            result.add(traceVo);
         }
         result.sort(new Comparator<StationTaskTraceVo>() {
             @Override
@@ -177,6 +243,59 @@
         }
     }
 
+    private void reconcileInactiveBusinessTasks() {
+        if (wrkMastService == null) {
+            return;
+        }
+        for (TraceTaskState state : taskStateMap.values()) {
+            if (state == null || state.isTerminal()) {
+                continue;
+            }
+            WrkMast wrkMast;
+            try {
+                wrkMast = wrkMastService.selectByWorkNo(state.taskNo);
+            } catch (Exception ignore) {
+                continue;
+            }
+            if (wrkMast == null) {
+                Integer currentStationId = state.currentStationId != null ? state.currentStationId : state.finalTargetStationId;
+                Map<String, Object> details = new LinkedHashMap<>();
+                details.put("reason", "wrk_missing");
+                state.markTerminal(state.traceVersion, STATUS_FINISHED, currentStationId, null,
+                        "AUTO_FINISHED", "杈撻�佷换鍔℃。宸蹭笉瀛樺湪锛岃建杩硅嚜鍔ㄧ粨鏉�", details);
+                persistState(state);
+                continue;
+            }
+            if (isStationTraceActiveWrkStatus(wrkMast.getWrkSts())) {
+                continue;
+            }
+            Integer currentStationId = state.currentStationId != null ? state.currentStationId : state.finalTargetStationId;
+            Map<String, Object> details = new LinkedHashMap<>();
+            details.put("reason", "wrk_status_transition");
+            details.put("wrkSts", wrkMast.getWrkSts());
+            details.put("wrkStsDesc", wrkMast.getWrkSts$());
+            if (isManualWrkStatus(wrkMast.getWrkSts())) {
+                state.markTerminal(state.traceVersion, STATUS_CANCELLED, currentStationId, null,
+                        "AUTO_CANCELLED", "杈撻�佷换鍔″凡閫�鍑鸿繍琛岋紝杞ㄨ抗鑷姩缁撴潫", details);
+            } else {
+                state.markTerminal(state.traceVersion, STATUS_FINISHED, currentStationId, null,
+                        "AUTO_FINISHED", "杈撻�佷换鍔″凡缁撴潫锛岃建杩硅嚜鍔ㄧ粨鏉�", details);
+            }
+            persistState(state);
+        }
+    }
+
+    private boolean isStationTraceActiveWrkStatus(Long wrkSts) {
+        return Objects.equals(wrkSts, WrkStsType.INBOUND_STATION_RUN.sts)
+                || Objects.equals(wrkSts, WrkStsType.STATION_RUN.sts);
+    }
+
+    private boolean isManualWrkStatus(Long wrkSts) {
+        return Objects.equals(wrkSts, WrkStsType.INBOUND_MANUAL.sts)
+                || Objects.equals(wrkSts, WrkStsType.OUTBOUND_MANUAL.sts)
+                || Objects.equals(wrkSts, WrkStsType.LOC_MOVE_MANUAL.sts);
+    }
+
     private void ensureCacheLoaded() {
         if (loadedFromRedis) {
             return;
@@ -185,7 +304,13 @@
             if (loadedFromRedis) {
                 return;
             }
+            long redisStartNs = System.nanoTime();
             Map<Object, Object> cacheMap = redisUtil == null ? null : redisUtil.hmget(TRACE_CACHE_KEY);
+            long redisCostMs = (System.nanoTime() - redisStartNs) / 1_000_000L;
+            if (redisCostMs > 100) {
+                log.info("StationTaskTraceRegistry Redis鍔犺浇鑰楁椂={}ms, entries={}", redisCostMs,
+                        cacheMap == null ? 0 : cacheMap.size());
+            }
             if (cacheMap != null && !cacheMap.isEmpty()) {
                 long now = System.currentTimeMillis();
                 for (Map.Entry<Object, Object> entry : cacheMap.entrySet()) {
@@ -358,6 +483,10 @@
         private List<StationTaskTraceSegmentVo> segmentList = new ArrayList<>();
         private Integer issuedSegmentCount;
         private Integer totalSegmentCount;
+        private Boolean loopAlertActive;
+        private String loopAlertType;
+        private String loopAlertText;
+        private Integer loopAlertCount;
         private Long updatedAt;
         private Long terminalExpireAt;
         private List<StationTaskTraceEventVo> events = new ArrayList<>();
@@ -385,6 +514,10 @@
         private List<StationTaskTraceSegmentVo> segmentList = new ArrayList<>();
         private Integer issuedSegmentCount = 0;
         private Integer totalSegmentCount = 0;
+        private Boolean loopAlertActive = Boolean.FALSE;
+        private String loopAlertType;
+        private String loopAlertText;
+        private Integer loopAlertCount;
         private final List<StationTaskTraceEventVo> events = new ArrayList<>();
         private Long updatedAt = System.currentTimeMillis();
         private Long terminalExpireAt;
@@ -400,6 +533,7 @@
                                                             List<Integer> localPathStationIds,
                                                             List<StationTaskTraceSegmentVo> localSegmentList) {
             TraceRegistration registration = new TraceRegistration();
+            boolean firstPlan = this.traceVersion == null || this.traceVersion <= 0;
             boolean rerouted = !isTerminalStatus(this.status) && this.traceVersion != null && this.traceVersion > 0;
             int nextTraceVersion = rerouted ? this.traceVersion + 1 : 1;
             int pathOffset = rerouted ? this.passedStationIds.size() : 0;
@@ -421,6 +555,9 @@
             this.latestIssuedSegmentPath = new ArrayList<>();
             this.status = rerouted ? STATUS_REROUTED : STATUS_WAITING;
             this.terminalExpireAt = null;
+            if (firstPlan) {
+                clearLoopHintState();
+            }
             rebuildProgress(planCurrentStationId);
             this.updatedAt = System.currentTimeMillis();
 
@@ -430,6 +567,7 @@
             details.put("segmentCount", this.totalSegmentCount);
             details.put("pathOffset", pathOffset);
             details.put("currentStationId", this.currentStationId);
+            appendLoopHintDetails(details);
             appendEvent(rerouted ? "REROUTED" : "PLAN_READY",
                     rerouted ? "杈撻�佷换鍔¤矾寰勫凡閲嶇畻骞剁画鎺ヨ建杩�" : "杈撻�佷换鍔″垎娈佃鍒掑凡寤虹珛",
                     details);
@@ -525,8 +663,50 @@
             appendEvent(eventType, message, nextDetails);
         }
 
+        private synchronized void updateLoopHint(Boolean loopAlertActive,
+                                                 String loopAlertType,
+                                                 String loopAlertText,
+                                                 Integer loopAlertCount,
+                                                 Map<String, Object> details) {
+            boolean active = Boolean.TRUE.equals(loopAlertActive)
+                    && loopAlertCount != null
+                    && loopAlertCount > 2
+                    && loopAlertText != null
+                    && !loopAlertText.trim().isEmpty();
+            String nextType = active ? loopAlertType : null;
+            String nextText = active ? loopAlertText.trim() : null;
+            Integer nextCount = loopAlertCount != null && loopAlertCount > 0 ? loopAlertCount : null;
+            boolean changed = !Objects.equals(this.loopAlertActive, active)
+                    || !Objects.equals(this.loopAlertType, nextType)
+                    || !Objects.equals(this.loopAlertText, nextText)
+                    || !Objects.equals(this.loopAlertCount, nextCount);
+            if (!changed) {
+                return;
+            }
+
+            this.loopAlertActive = active;
+            this.loopAlertType = nextType;
+            this.loopAlertText = nextText;
+            this.loopAlertCount = nextCount;
+            this.updatedAt = System.currentTimeMillis();
+            if (!active) {
+                return;
+            }
+
+            Map<String, Object> nextDetails = copyDetails(details);
+            nextDetails.put("loopAlertActive", Boolean.TRUE);
+            nextDetails.put("loopAlertType", this.loopAlertType);
+            nextDetails.put("loopAlertText", this.loopAlertText);
+            nextDetails.put("loopAlertCount", this.loopAlertCount);
+            appendEvent("LOOP_REPEAT_ALERT", this.loopAlertText, nextDetails);
+        }
+
         private synchronized boolean shouldRemove(long now) {
             return terminalExpireAt != null && terminalExpireAt <= now;
+        }
+
+        private synchronized boolean isTerminal() {
+            return isTerminalStatus(this.status);
         }
 
         private synchronized StationTaskTraceVo toVo() {
@@ -547,8 +727,30 @@
             vo.setSegmentList(copySegmentListWithIssued(segmentList, issuedSegmentCount));
             vo.setIssuedSegmentCount(issuedSegmentCount);
             vo.setTotalSegmentCount(totalSegmentCount);
+            vo.setLoopAlertActive(loopAlertActive);
+            vo.setLoopAlertType(loopAlertType);
+            vo.setLoopAlertText(loopAlertText);
+            vo.setLoopAlertCount(loopAlertCount);
             vo.setUpdatedAt(updatedAt);
             vo.setEvents(copyEventList(events));
+            return vo;
+        }
+
+        private synchronized StationTaskTraceVo toPlanningVo() {
+            if (isTerminalStatus(this.status)) {
+                return null;
+            }
+            StationTaskTraceVo vo = new StationTaskTraceVo();
+            vo.setTaskNo(taskNo);
+            vo.setThreadImpl(threadImpl);
+            vo.setStatus(status);
+            vo.setTraceVersion(traceVersion);
+            vo.setStartStationId(startStationId);
+            vo.setCurrentStationId(currentStationId);
+            vo.setFinalTargetStationId(finalTargetStationId);
+            vo.setPendingStationIds(copyIntegerList(pendingStationIds));
+            vo.setLatestIssuedSegmentPath(copyIntegerList(latestIssuedSegmentPath));
+            vo.setUpdatedAt(updatedAt);
             return vo;
         }
 
@@ -570,6 +772,10 @@
             snapshot.setSegmentList(copySegmentList(segmentList));
             snapshot.setIssuedSegmentCount(issuedSegmentCount);
             snapshot.setTotalSegmentCount(totalSegmentCount);
+            snapshot.setLoopAlertActive(loopAlertActive);
+            snapshot.setLoopAlertType(loopAlertType);
+            snapshot.setLoopAlertText(loopAlertText);
+            snapshot.setLoopAlertCount(loopAlertCount);
             snapshot.setUpdatedAt(updatedAt);
             snapshot.setTerminalExpireAt(terminalExpireAt);
             snapshot.setEvents(copyEventList(events));
@@ -593,6 +799,10 @@
             state.segmentList = copySegmentList(snapshot.getSegmentList());
             state.issuedSegmentCount = snapshot.getIssuedSegmentCount() == null ? 0 : snapshot.getIssuedSegmentCount();
             state.totalSegmentCount = snapshot.getTotalSegmentCount() == null ? state.segmentList.size() : snapshot.getTotalSegmentCount();
+            state.loopAlertActive = Boolean.TRUE.equals(snapshot.getLoopAlertActive());
+            state.loopAlertType = snapshot.getLoopAlertType();
+            state.loopAlertText = snapshot.getLoopAlertText();
+            state.loopAlertCount = snapshot.getLoopAlertCount();
             state.updatedAt = snapshot.getUpdatedAt() == null ? System.currentTimeMillis() : snapshot.getUpdatedAt();
             state.terminalExpireAt = snapshot.getTerminalExpireAt();
             state.events.clear();
@@ -643,6 +853,25 @@
             this.pendingStationIds = copyIntegerList(fullPath.subList(currentIndex + 1, fullPath.size()));
         }
 
+        private void clearLoopHintState() {
+            this.loopAlertActive = Boolean.FALSE;
+            this.loopAlertType = null;
+            this.loopAlertText = null;
+            this.loopAlertCount = null;
+        }
+
+        private void appendLoopHintDetails(Map<String, Object> details) {
+            if (details == null || this.loopAlertCount == null || this.loopAlertCount <= 0) {
+                return;
+            }
+            details.put("loopAlertCount", this.loopAlertCount);
+            if (Boolean.TRUE.equals(this.loopAlertActive)) {
+                details.put("loopAlertActive", Boolean.TRUE);
+                details.put("loopAlertType", this.loopAlertType);
+                details.put("loopAlertText", this.loopAlertText);
+            }
+        }
+
         private boolean acceptTraceVersion(Integer incomingTraceVersion) {
             return incomingTraceVersion != null
                     && this.traceVersion != null

--
Gitblit v1.9.1