From fd5f427018a1d308da9fcb4104c9f5340419ee78 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期六, 21 三月 2026 10:50:01 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/utils/StationOperateProcessUtils.java |  166 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 136 insertions(+), 30 deletions(-)

diff --git a/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java b/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
index 5bfa108..eb1a6a7 100644
--- a/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
+++ b/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
@@ -40,6 +40,8 @@
     private static final int STATION_IDLE_RECOVER_SECONDS = 10;
     private static final int STATION_IDLE_RECOVER_LIMIT_SECONDS = 10;
     private static final int STATION_IDLE_TRACK_EXPIRE_SECONDS = 60 * 60;
+    private static final long STATION_MOVE_RESET_WAIT_MS = 1000L;
+    private static final String IDLE_RECOVER_CLEARED_MEMO = "idleRecoverRerouteCleared";
 
     @Autowired
     private BasDevpService basDevpService;
@@ -784,6 +786,9 @@
                     && currentBatchSeq.equals(wrkMast.getBatchSeq());
         }
         if (toTarget) {
+            if (!hasReachableOutReleaseSlot(currentStationId, wrkMast.getStaNo())) {
+                return null;
+            }
             return new OutOrderDispatchDecision(wrkMast.getStaNo(), false);
         }
 
@@ -845,8 +850,8 @@
 
         try {
             List<NavigateNode> nodes = navigateUtils.calcByStationId(currentStationId, finalTargetStationId);
-            for (int i = nodes.size() - 1; i >= 0; i--) {
-                Integer stationId = getStationIdFromNode(nodes.get(i));
+            for (NavigateNode node : nodes) {
+                Integer stationId = getStationIdFromNode(node);
                 if (stationId == null) {
                     continue;
                 }
@@ -859,6 +864,59 @@
             }
         } catch (Exception ignore) {}
         return finalTargetStationId;
+    }
+
+    private boolean hasReachableOutReleaseSlot(Integer currentStationId,
+                                               Integer finalTargetStationId) {
+        if (currentStationId == null || finalTargetStationId == null) {
+            return true;
+        }
+
+        try {
+            List<NavigateNode> nodes = navigateUtils.calcByStationId(currentStationId, finalTargetStationId);
+            if (nodes == null || nodes.isEmpty()) {
+                return true;
+            }
+
+            for (NavigateNode node : nodes) {
+                Integer stationId = getStationIdFromNode(node);
+                if (stationId == null || Objects.equals(stationId, currentStationId)) {
+                    continue;
+                }
+
+                if (!isPathStationBlocked(stationId)) {
+                    return true;
+                }
+            }
+            return false;
+        } catch (Exception ignore) {
+            return true;
+        }
+    }
+
+    private boolean isPathStationBlocked(Integer stationId) {
+        if (stationId == null) {
+            return true;
+        }
+
+        BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", stationId));
+        if (basStation == null) {
+            return true;
+        }
+
+        StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
+        if (stationThread == null) {
+            return true;
+        }
+
+        StationProtocol stationProtocol = stationThread.getStatusMap().get(stationId);
+        if (stationProtocol == null) {
+            return true;
+        }
+
+        return !stationProtocol.isAutoing()
+                || stationProtocol.isLoading()
+                || (stationProtocol.getTaskNo() != null && stationProtocol.getTaskNo() > 0);
     }
 
     private Integer resolveNextCircleOrderTarget(Integer currentStationId, List<Integer> orderedOutStationList) {
@@ -958,10 +1016,6 @@
             return;
         }
 
-        if (hasFollowUpMoveCommandAfterStay(idleTrack, stationProtocol.getTaskNo(), stationProtocol.getStationId())) {
-            return;
-        }
-
         OutOrderDispatchDecision dispatchDecision = null;
         Integer moveStaNo;
         if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)) {
@@ -975,6 +1029,8 @@
         }
 
         redisUtil.set(RedisKeyType.CHECK_STATION_IDLE_RECOVER_LIMIT_.key + stationProtocol.getTaskNo(), "lock", STATION_IDLE_RECOVER_LIMIT_SECONDS);
+        resetSegmentMoveCommandsBeforeReroute(stationProtocol.getTaskNo());
+        int clearedCommandCount = clearIssuedMoveCommandsDuringIdleStay(idleTrack, stationProtocol.getTaskNo(), stationProtocol.getStationId());
 
         StationCommand command = stationThread.getCommand(
                 StationCommandType.MOVE,
@@ -991,8 +1047,8 @@
         MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command));
         syncOutOrderWatchState(wrkMast, stationProtocol.getStationId(), outOrderList, dispatchDecision, command);
         saveStationTaskIdleTrack(new StationTaskIdleTrack(wrkMast.getWrkNo(), stationProtocol.getStationId(), System.currentTimeMillis()));
-        News.info("杈撻�佺珯鐐逛换鍔″仠鐣檣}绉掓湭杩愯锛屽凡閲嶆柊璁$畻璺緞骞堕噸鍚繍琛岋紝绔欑偣鍙�={}锛岀洰鏍囩珯={}锛屽伐浣滃彿={}锛屽懡浠ゆ暟鎹�={}",
-                STATION_IDLE_RECOVER_SECONDS, stationProtocol.getStationId(), moveStaNo, wrkMast.getWrkNo(), JSON.toJSONString(command));
+        News.info("杈撻�佺珯鐐逛换鍔″仠鐣檣}绉掓湭杩愯锛屽凡閲嶆柊璁$畻璺緞骞堕噸鍚繍琛岋紝绔欑偣鍙�={}锛岀洰鏍囩珯={}锛屽伐浣滃彿={}锛屾竻鐞嗘棫鍒嗘鍛戒护鏁�={}锛屽懡浠ゆ暟鎹�={}",
+                STATION_IDLE_RECOVER_SECONDS, stationProtocol.getStationId(), moveStaNo, wrkMast.getWrkNo(), clearedCommandCount, JSON.toJSONString(command));
     }
 
     private boolean canRecoverIdleStationTask(WrkMast wrkMast, Integer currentStationId) {
@@ -1006,41 +1062,91 @@
                 || Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts);
     }
 
-    private boolean hasFollowUpMoveCommandAfterStay(StationTaskIdleTrack idleTrack,
-                                                    Integer taskNo,
-                                                    Integer stationId) {
-        if (idleTrack == null || taskNo == null || stationId == null || idleTrack.firstSeenTime == null) {
-            return false;
+    private void resetSegmentMoveCommandsBeforeReroute(Integer taskNo) {
+        if (redisUtil == null || taskNo == null || taskNo <= 0) {
+            return;
         }
-        if (basStationOptService == null) {
-            return false;
+        String key = RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo;
+        redisUtil.set(key, "cancel", 3);
+        try {
+            Thread.sleep(STATION_MOVE_RESET_WAIT_MS);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+        } catch (Exception ignore) {
         }
+        redisUtil.del(key);
+    }
 
+    private int clearIssuedMoveCommandsDuringIdleStay(StationTaskIdleTrack idleTrack,
+                                                      Integer taskNo,
+                                                      Integer stationId) {
+        if (basStationOptService == null) {
+            return 0;
+        }
         List<BasStationOpt> optList;
         try {
-            optList = basStationOptService.list(new QueryWrapper<BasStationOpt>()
-                    .select("id", "target_station_id")
-                    .eq("task_no", taskNo)
-                    .eq("source_station_id", stationId)
-                    .eq("mode", String.valueOf(StationCommandType.MOVE))
-                    .eq("send", 1)
-                    .ge("send_time", new Date(idleTrack.firstSeenTime))
-                    .orderByDesc("send_time")
-                    .last("limit 3"));
+            optList = listIssuedMoveCommandsDuringIdleStay(idleTrack, taskNo);
         } catch (Exception e) {
-            return false;
+            return 0;
         }
-
         if (optList == null || optList.isEmpty()) {
-            return false;
+            return 0;
         }
 
+        Date now = new Date();
+        String cleanupMemo = buildIdleRecoverClearedMemo(stationId);
+        int clearedCount = 0;
         for (BasStationOpt opt : optList) {
-            if (opt != null && opt.getTargetStationId() != null && !Objects.equals(opt.getTargetStationId(), stationId)) {
-                return true;
+            if (opt == null || opt.getId() == null) {
+                continue;
             }
+            opt.setSend(0);
+            opt.setUpdateTime(now);
+            opt.setMemo(appendCleanupMemo(opt.getMemo(), cleanupMemo));
+            clearedCount++;
         }
-        return false;
+        if (clearedCount > 0) {
+            basStationOptService.updateBatchById(optList);
+        }
+        return clearedCount;
+    }
+
+    private List<BasStationOpt> listIssuedMoveCommandsDuringIdleStay(StationTaskIdleTrack idleTrack,
+                                                                     Integer taskNo) {
+        if (idleTrack == null || taskNo == null || taskNo <= 0 || idleTrack.firstSeenTime == null || basStationOptService == null) {
+            return Collections.emptyList();
+        }
+        List<BasStationOpt> optList = basStationOptService.list(new QueryWrapper<BasStationOpt>()
+                .select("id", "task_no", "send_time", "target_station_id", "memo", "send")
+                .eq("task_no", taskNo)
+                .eq("mode", String.valueOf(StationCommandType.MOVE))
+                .eq("send", 1)
+                .ge("send_time", new Date(idleTrack.firstSeenTime))
+                .orderByAsc("send_time"));
+        if (optList == null || optList.isEmpty()) {
+            return Collections.emptyList();
+        }
+        return optList;
+    }
+
+    private String buildIdleRecoverClearedMemo(Integer stationId) {
+        if (stationId == null) {
+            return IDLE_RECOVER_CLEARED_MEMO;
+        }
+        return IDLE_RECOVER_CLEARED_MEMO + "(stationId=" + stationId + ")";
+    }
+
+    private String appendCleanupMemo(String memo, String cleanupMemo) {
+        if (Cools.isEmpty(cleanupMemo)) {
+            return memo;
+        }
+        if (Cools.isEmpty(memo)) {
+            return cleanupMemo;
+        }
+        if (memo.contains(cleanupMemo)) {
+            return memo;
+        }
+        return memo + " | " + cleanupMemo;
     }
 
     private StationTaskIdleTrack touchStationTaskIdleTrack(Integer taskNo, Integer stationId) {

--
Gitblit v1.9.1