From b8640dc78123f4be2483feed2f48b9183983f51f Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期一, 13 四月 2026 14:11:54 +0800
Subject: [PATCH] #站点运行优化

---
 src/main/java/com/zy/core/utils/station/StationRerouteProcessor.java |  323 +++++++++++------------------------------------------
 1 files changed, 66 insertions(+), 257 deletions(-)

diff --git a/src/main/java/com/zy/core/utils/station/StationRerouteProcessor.java b/src/main/java/com/zy/core/utils/station/StationRerouteProcessor.java
index 9deba63..3cf4495 100644
--- a/src/main/java/com/zy/core/utils/station/StationRerouteProcessor.java
+++ b/src/main/java/com/zy/core/utils/station/StationRerouteProcessor.java
@@ -50,9 +50,8 @@
 @Component
 public class StationRerouteProcessor {
     private static final int OUT_ORDER_DISPATCH_LIMIT_SECONDS = 2;
-    private static final int STATION_IDLE_RECOVER_SECONDS = 10;
-    private static final int STATION_IDLE_RECOVER_LIMIT_SECONDS = 30;
     private static final long STATION_MOVE_RESET_WAIT_MS = 1000L;
+    private static final int RUN_BLOCK_DIRECT_REASSIGN_LIMIT_SECONDS = 15 * 60;
 
     @Autowired
     private BasDevpService basDevpService;
@@ -75,29 +74,12 @@
     @Autowired
     private StationDispatchRuntimeStateSupport stationDispatchRuntimeStateSupport;
 
-    public void checkStationRunBlock() {
-        try {
-            List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
-            for (BasDevp basDevp : basDevps) {
-                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
-                if (stationThread == null) {
-                    continue;
-                }
-                for (StationProtocol stationProtocol : stationThread.getStatus()) {
-                    if (stationProtocol == null || stationProtocol.getStationId() == null) {
-                        continue;
-                    }
-                    checkStationRunBlock(basDevp, stationProtocol.getStationId());
-                }
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
     public void checkStationRunBlock(BasDevp basDevp, Integer stationId) {
         try {
             if (basDevp == null || basDevp.getDevpNo() == null || stationId == null) {
+                return;
+            }
+            if (shouldSkipRunBlockStation(basDevp, stationId)) {
                 return;
             }
             StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
@@ -131,10 +113,18 @@
             if (lock != null) {
                 return;
             }
-            redisUtil.set(RedisKeyType.CHECK_STATION_RUN_BLOCK_LIMIT_.key + stationProtocol.getTaskNo(), "lock", 15);
+            redisUtil.set(RedisKeyType.CHECK_STATION_RUN_BLOCK_LIMIT_.key + stationProtocol.getTaskNo(), "lock", 30);
 
             if (shouldUseRunBlockDirectReassign(wrkMast, stationProtocol.getStationId(), runBlockReassignLocStationList)) {
-                executeRunBlockDirectReassign(basDevp, stationThread, stationProtocol, wrkMast);
+                if (stationMoveCoordinator != null) {
+                    stationMoveCoordinator.withTaskDispatchLock(stationProtocol.getTaskNo(),
+                            () -> {
+                                executeRunBlockDirectReassign(basDevp, stationThread, stationProtocol, wrkMast);
+                                return null;
+                            });
+                } else {
+                    executeRunBlockDirectReassign(basDevp, stationThread, stationProtocol, wrkMast);
+                }
                 return;
             }
 
@@ -155,60 +145,6 @@
             executeSharedReroute(context);
         } catch (Exception e) {
             e.printStackTrace();
-        }
-    }
-
-    public void checkStationIdleRecover() {
-        try {
-            List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
-            for (BasDevp basDevp : basDevps) {
-                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
-                if (stationThread == null) {
-                    continue;
-                }
-
-                for (StationProtocol stationProtocol : stationThread.getStatus()) {
-                    if (stationProtocol != null && stationProtocol.getStationId() != null) {
-                        checkStationIdleRecover(basDevp, stationProtocol.getStationId());
-                    }
-                }
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    public void checkStationIdleRecover(BasDevp basDevp, Integer stationId) {
-        try {
-            if (basDevp == null || basDevp.getDevpNo() == null || stationId == null) {
-                return;
-            }
-            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
-            if (stationThread == null) {
-                return;
-            }
-            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
-            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(stationId);
-            if (stationProtocol == null
-                    || !stationProtocol.isAutoing()
-                    || !stationProtocol.isLoading()
-                    || stationProtocol.getTaskNo() <= 0
-                    || stationProtocol.isRunBlock()) {
-                return;
-            }
-            checkStationIdleRecover(basDevp, stationThread, stationProtocol, basDevp.getOutOrderIntList());
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    public void checkStationOutOrder() {
-        List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<BasDevp>());
-        for (BasDevp basDevp : basDevps) {
-            List<StationObjModel> orderList = basDevp.getOutOrderList$();
-            for (StationObjModel stationObjModel : orderList) {
-                checkStationOutOrder(basDevp, stationObjModel);
-            }
         }
     }
 
@@ -254,67 +190,6 @@
                     "checkStationOutOrder"
             ).withDispatchDeviceNo(stationObjModel.getDeviceNo())
                     .withSuppressDispatchGuard()
-                    .withOutOrderDispatchLock()
-                    .withResetSegmentCommandsBeforeDispatch();
-            executeSharedReroute(context);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    public void watchCircleStation() {
-        List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<BasDevp>());
-        for (BasDevp basDevp : basDevps) {
-            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
-            if (stationThread == null) {
-                continue;
-            }
-            for (StationProtocol stationProtocol : stationThread.getStatus()) {
-                if (stationProtocol == null || stationProtocol.getStationId() == null) {
-                    continue;
-                }
-                watchCircleStation(basDevp, stationProtocol.getStationId());
-            }
-        }
-    }
-
-    public void watchCircleStation(BasDevp basDevp, Integer stationId) {
-        try {
-            if (basDevp == null || basDevp.getDevpNo() == null || stationId == null) {
-                return;
-            }
-            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
-            if (stationThread == null) {
-                return;
-            }
-            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
-            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(stationId);
-            if (stationProtocol == null
-                    || !stationProtocol.isAutoing()
-                    || !stationProtocol.isLoading()
-                    || stationProtocol.getTaskNo() <= 0
-                    || !stationOutboundDecisionSupport.isWatchingCircleArrival(stationProtocol.getTaskNo(), stationProtocol.getStationId())) {
-                return;
-            }
-
-            WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
-            if (wrkMast == null
-                    || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)
-                    || Objects.equals(stationProtocol.getStationId(), wrkMast.getStaNo())) {
-                return;
-            }
-
-            Double pathLenFactor = stationOutboundDecisionSupport.resolveOutboundPathLenFactor(wrkMast);
-            RerouteContext context = RerouteContext.create(
-                    RerouteSceneType.WATCH_CIRCLE,
-                    basDevp,
-                    stationThread,
-                    stationProtocol,
-                    wrkMast,
-                    basDevp.getOutOrderIntList(),
-                    pathLenFactor,
-                    "watchCircleStation"
-            ).withSuppressDispatchGuard()
                     .withOutOrderDispatchLock()
                     .withResetSegmentCommandsBeforeDispatch();
             executeSharedReroute(context);
@@ -448,32 +323,24 @@
                 && runBlockReassignLocStationList.contains(stationId);
     }
 
-    public boolean shouldSkipIdleRecoverForRecentDispatch(Integer taskNo, Integer stationId) {
-        if (taskNo == null || taskNo <= 0 || stationId == null) {
+    private boolean shouldSkipRunBlockStation(BasDevp basDevp, Integer stationId) {
+        if (basDevp == null || stationId == null) {
             return false;
         }
-        long thresholdMs = STATION_IDLE_RECOVER_SECONDS * 1000L;
-        StationMoveSession session = stationMoveCoordinator == null ? null : stationMoveCoordinator.loadSession(taskNo);
-        if (session != null && session.isActive() && session.getLastIssuedAt() != null) {
-            if (Objects.equals(stationId, session.getCurrentStationId())
-                    || Objects.equals(stationId, session.getDispatchStationId())
-                    || session.containsStation(stationId)) {
-                long elapsedMs = System.currentTimeMillis() - session.getLastIssuedAt();
-                if (elapsedMs < thresholdMs) {
-                    stationDispatchRuntimeStateSupport.saveIdleTrack(new StationTaskIdleTrack(taskNo, stationId, System.currentTimeMillis()));
-                    News.info("杈撻�佺珯鐐逛换鍔″垰瀹屾垚鍛戒护涓嬪彂锛屽凡璺宠繃鍋滅暀閲嶇畻銆傜珯鐐瑰彿={}锛屽伐浣滃彿={}锛岃窛涓婃涓嬪彂={}ms锛宺outeVersion={}",
-                            stationId, taskNo, elapsedMs, session.getRouteVersion());
-                    return true;
-                }
+        return containsStation(basDevp.getBarcodeStationList$(), stationId)
+                || containsStation(basDevp.getInStationList$(), stationId);
+    }
+
+    private boolean containsStation(List<StationObjModel> stationList, Integer stationId) {
+        if (stationList == null || stationList.isEmpty() || stationId == null) {
+            return false;
+        }
+        for (StationObjModel stationObjModel : stationList) {
+            if (stationObjModel != null && Objects.equals(stationObjModel.getStationId(), stationId)) {
+                return true;
             }
         }
-        if (!stationDispatchRuntimeStateSupport.hasRecentIssuedMoveCommand(taskNo, stationId, thresholdMs)) {
-            return false;
-        }
-        stationDispatchRuntimeStateSupport.saveIdleTrack(new StationTaskIdleTrack(taskNo, stationId, System.currentTimeMillis()));
-        News.info("杈撻�佺珯鐐逛换鍔″垰瀹屾垚鍛戒护涓嬪彂锛屽凡璺宠繃鍋滅暀閲嶇畻銆傜珯鐐瑰彿={}锛屽伐浣滃彿={}锛岃窛鏈�杩戝懡浠や笅鍙�<{}ms锛宺outeVersion={}",
-                stationId, taskNo, thresholdMs, session == null ? null : session.getRouteVersion());
-        return true;
+        return false;
     }
 
     private RerouteExecutionResult executeReroutePlanWithTaskLock(RerouteContext context,
@@ -482,18 +349,8 @@
                                                                   Integer taskNo,
                                                                   Integer stationId) {
         boolean runBlockReroute = context.sceneType() == RerouteSceneType.RUN_BLOCK_REROUTE;
-        if (context.checkRecentDispatch()
-                && shouldSkipIdleRecoverForRecentDispatch(taskNo, stationId)) {
-            return RerouteExecutionResult.skip("recent-dispatch");
-        }
         int currentTaskBufferCommandCount = countCurrentTaskBufferCommands(stationProtocol.getTaskBufferItems(), taskNo);
         if (currentTaskBufferCommandCount > 0 && !runBlockReroute) {
-            if (context.sceneType() == RerouteSceneType.IDLE_RECOVER) {
-                News.info("杈撻�佺珯鐐逛换鍔″仠鐣欒秴鏃讹紝浣嗙紦瀛樺尯浠嶅瓨鍦ㄥ綋鍓嶄换鍔″懡浠わ紝宸茶烦杩囬噸绠椼�傜珯鐐瑰彿={}锛屽伐浣滃彿={}锛屽綋鍓嶄换鍔″懡浠ゆ暟={}",
-                        stationId,
-                        taskNo,
-                        currentTaskBufferCommandCount);
-            }
             return RerouteExecutionResult.skip("buffer-has-current-task");
         }
         if (currentTaskBufferCommandCount > 0 && runBlockReroute) {
@@ -513,39 +370,23 @@
             return RerouteExecutionResult.skip("out-order-lock");
         }
 
-        if (context.cancelSessionBeforeDispatch() && stationMoveCoordinator != null) {
-            stationMoveCoordinator.markCancelPending(taskNo, "reroute_pending");
-        }
-
-        if (runBlockReroute) {
-            if (context.cancelSessionBeforeDispatch() && stationMoveCoordinator != null) {
-                stationMoveCoordinator.cancelSession(taskNo);
-            }
-            if (context.resetSegmentCommandsBeforeDispatch()) {
-                stationDispatchRuntimeStateSupport.signalSegmentReset(taskNo, STATION_MOVE_RESET_WAIT_MS);
-            }
-        }
-
-        if (!runBlockReroute
-                && context.cancelSessionBeforeDispatch() && stationMoveCoordinator != null) {
-            stationMoveCoordinator.cancelSession(taskNo);
-        }
         if (!isBlank(context.executionLockKey())
                 && !stationDispatchRuntimeStateSupport.tryAcquireLock(context.executionLockKey(), context.executionLockSeconds())) {
                 return RerouteExecutionResult.skip("scene-lock");
         }
-        if (!runBlockReroute && context.resetSegmentCommandsBeforeDispatch()) {
+        if (context.resetSegmentCommandsBeforeDispatch()) {
             stationDispatchRuntimeStateSupport.signalSegmentReset(taskNo, STATION_MOVE_RESET_WAIT_MS);
         }
 
         int clearedCommandCount = 0;
-        if (context.clearIdleIssuedCommands()) {
-            clearedCommandCount = stationDispatchRuntimeStateSupport.clearIssuedMoveCommandsDuringIdleStay(context.idleTrack(), taskNo, stationId);
-        }
 
         boolean offered = offerDevpCommandWithDedup(context.dispatchDeviceNo(), plan.command(), plan.dispatchScene());
         if (!offered) {
             return RerouteExecutionResult.skip("dispatch-dedup");
+        }
+        if (context.cancelSessionBeforeDispatch() && stationMoveCoordinator != null) {
+            stationMoveCoordinator.markCancelPending(taskNo, "reroute_pending");
+            stationMoveCoordinator.cancelSession(taskNo);
         }
 
         applyRerouteDispatchEffects(context, plan, clearedCommandCount);
@@ -588,17 +429,6 @@
                     dispatchDecision != null && dispatchDecision.isCircle()
             );
         }
-        if (context.sceneType() == RerouteSceneType.IDLE_RECOVER) {
-            stationDispatchRuntimeStateSupport.saveIdleTrack(new StationTaskIdleTrack(wrkMast.getWrkNo(), stationProtocol.getStationId(), System.currentTimeMillis()));
-            News.info("杈撻�佺珯鐐逛换鍔″仠鐣檣}绉掓湭杩愯锛屽凡閲嶆柊璁$畻璺緞骞堕噸鍚繍琛岋紝绔欑偣鍙�={}锛岀洰鏍囩珯={}锛屽伐浣滃彿={}锛屾竻鐞嗘棫鍒嗘鍛戒护鏁�={}锛屽懡浠ゆ暟鎹�={}",
-                    STATION_IDLE_RECOVER_SECONDS,
-                    stationProtocol.getStationId(),
-                    plan.command().getTargetStaNo(),
-                    wrkMast.getWrkNo(),
-                    clearedCommandCount,
-                    JSON.toJSONString(plan.command()));
-            return;
-        }
         if (context.sceneType() == RerouteSceneType.RUN_BLOCK_REROUTE) {
             News.info("杈撻�佺珯鐐瑰牭濉炲悗閲嶆柊璁$畻璺緞鍛戒护涓嬪彂鎴愬姛锛岀珯鐐瑰彿={}锛屽伐浣滃彿={}锛屽懡浠ゆ暟鎹�={}",
                     stationProtocol.getStationId(),
@@ -611,51 +441,6 @@
         }
     }
 
-    private void checkStationIdleRecover(BasDevp basDevp,
-                                         StationThread stationThread,
-                                         StationProtocol stationProtocol,
-                                         List<Integer> outOrderList) {
-        if (stationProtocol == null || stationProtocol.getTaskNo() == null || stationProtocol.getTaskNo() <= 0) {
-            return;
-        }
-        if (!Objects.equals(stationProtocol.getStationId(), stationProtocol.getTargetStaNo())) {
-            return;
-        }
-
-        StationTaskIdleTrack idleTrack = stationDispatchRuntimeStateSupport.touchIdleTrack(stationProtocol.getTaskNo(), stationProtocol.getStationId());
-        if (shouldSkipIdleRecoverForRecentDispatch(stationProtocol.getTaskNo(), stationProtocol.getStationId())) {
-            return;
-        }
-        if (idleTrack == null || !idleTrack.isTimeout(STATION_IDLE_RECOVER_SECONDS)) {
-            return;
-        }
-
-        WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
-        if (!canRecoverIdleStationTask(wrkMast, stationProtocol.getStationId())) {
-            return;
-        }
-
-        Object lock = redisUtil.get(RedisKeyType.CHECK_STATION_IDLE_RECOVER_LIMIT_.key + stationProtocol.getTaskNo());
-        if (lock != null) {
-            return;
-        }
-        Double pathLenFactor = stationOutboundDecisionSupport.resolveOutboundPathLenFactor(wrkMast);
-        RerouteContext context = RerouteContext.create(
-                RerouteSceneType.IDLE_RECOVER,
-                basDevp,
-                stationThread,
-                stationProtocol,
-                wrkMast,
-                outOrderList,
-                pathLenFactor,
-                "checkStationIdleRecover"
-        ).withCancelSessionBeforeDispatch()
-                .withExecutionLock(RedisKeyType.CHECK_STATION_IDLE_RECOVER_LIMIT_.key + stationProtocol.getTaskNo(), STATION_IDLE_RECOVER_LIMIT_SECONDS)
-                .withResetSegmentCommandsBeforeDispatch()
-                .clearIdleIssuedCommands(idleTrack);
-        executeSharedReroute(context);
-    }
-
     private void executeRunBlockDirectReassign(BasDevp basDevp,
                                                StationThread stationThread,
                                                StationProtocol stationProtocol,
@@ -663,19 +448,27 @@
         if (basDevp == null || stationThread == null || stationProtocol == null || wrkMast == null) {
             return;
         }
+        if (isDirectReassignContextStale(stationProtocol, wrkMast)) {
+            return;
+        }
         int currentTaskBufferCommandCount = countCurrentTaskBufferCommands(
                 stationProtocol.getTaskBufferItems(),
                 stationProtocol.getTaskNo()
         );
         if (currentTaskBufferCommandCount > 0) {
-            News.info("杈撻�佺珯鐐硅繍琛屽牭濉為噸鍒嗛厤宸茶烦杩囷紝缂撳瓨鍖轰粛瀛樺湪褰撳墠浠诲姟鍛戒护銆傜珯鐐瑰彿={}锛屽伐浣滃彿={}锛屽綋鍓嶄换鍔″懡浠ゆ暟={}",
+            News.info("杈撻�佺珯鐐硅繍琛屽牭濉為噸鍒嗛厤妫�娴嬪埌鏃у垎娈靛懡浠ゆ畫鐣欙紝灏嗗厛閲嶇疆鏈湴鍒嗘鐘舵�佸悗缁х画閲嶅彂銆傜珯鐐瑰彿={}锛屽伐浣滃彿={}锛屽綋鍓嶄换鍔″懡浠ゆ暟={}",
                     stationProtocol.getStationId(),
                     stationProtocol.getTaskNo(),
                     currentTaskBufferCommandCount);
-            return;
         }
-        if (stationMoveCoordinator != null) {
-            stationMoveCoordinator.cancelSession(wrkMast.getWrkNo());
+        if (!stationDispatchRuntimeStateSupport.tryAcquireRunBlockDirectReassignLock(
+                wrkMast.getWrkNo(),
+                stationProtocol.getStationId(),
+                RUN_BLOCK_DIRECT_REASSIGN_LIMIT_SECONDS)) {
+            News.info("杈撻�佺珯鐐硅繍琛屽牭濉為噸鍒嗛厤宸茶烦杩囷紝15鍒嗛挓鍐呬笉鍏佽閲嶅鐢宠銆傜珯鐐瑰彿={}锛屽伐浣滃彿={}",
+                    stationProtocol.getStationId(),
+                    wrkMast.getWrkNo());
+            return;
         }
         String response = wmsOperateUtils.applyReassignTaskLocNo(wrkMast.getWrkNo(), stationProtocol.getStationId());
         if (Cools.isEmpty(response)) {
@@ -753,11 +546,14 @@
         if (!wrkMastService.updateById(wrkMast)) {
             return;
         }
+        stationDispatchRuntimeStateSupport.signalSegmentReset(wrkMast.getWrkNo(), STATION_MOVE_RESET_WAIT_MS);
         boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "checkStationRunBlock_direct");
         if (!offered) {
             return;
         }
         if (stationMoveCoordinator != null) {
+            stationMoveCoordinator.markCancelPending(wrkMast.getWrkNo(), "reroute_pending");
+            stationMoveCoordinator.cancelSession(wrkMast.getWrkNo());
             stationMoveCoordinator.recordDispatch(
                     wrkMast.getWrkNo(),
                     stationProtocol.getStationId(),
@@ -768,15 +564,28 @@
         }
     }
 
-    private boolean canRecoverIdleStationTask(WrkMast wrkMast, Integer currentStationId) {
-        if (wrkMast == null || currentStationId == null || wrkMast.getStaNo() == null) {
+    private boolean isDirectReassignContextStale(StationProtocol stationProtocol, WrkMast wrkMast) {
+        if (stationProtocol == null || wrkMast == null || stationMoveCoordinator == null) {
             return false;
         }
-        if (Objects.equals(currentStationId, wrkMast.getStaNo())) {
+        Integer taskNo = wrkMast.getWrkNo();
+        Integer triggerStationId = stationProtocol.getStationId();
+        if (taskNo == null || taskNo <= 0 || triggerStationId == null) {
             return false;
         }
-        return Objects.equals(wrkMast.getWrkSts(), WrkStsType.INBOUND_STATION_RUN.sts)
-                || Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts);
+        StationMoveSession session = stationMoveCoordinator.loadSession(taskNo);
+        if (session == null || !session.isActive()) {
+            return false;
+        }
+        Integer currentStationId = session.getCurrentStationId();
+        if (currentStationId == null || Objects.equals(currentStationId, triggerStationId)) {
+            return false;
+        }
+        News.info("杈撻�佺珯鐐硅繍琛屽牭濉為噸鍒嗛厤宸茶烦杩囷紝浠诲姟宸茬寮�瑙﹀彂绔欑偣銆傝Е鍙戠珯鐐�={}锛屽綋鍓嶇珯鐐�={}锛屽伐浣滃彿={}",
+                triggerStationId,
+                currentStationId,
+                taskNo);
+        return true;
     }
 
     private int countCurrentTaskBufferCommands(List<StationTaskBufferItem> taskBufferItems, Integer currentTaskNo) {

--
Gitblit v1.9.1