From be5c87afd82e50b6ef58a24e06a7a6cb36fb5007 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期四, 19 三月 2026 17:54:46 +0800
Subject: [PATCH] #仿真优化

---
 src/main/java/com/zy/core/utils/StationOperateProcessUtils.java |   76 +++++++++++++++++++++++++++++++++++--
 1 files changed, 71 insertions(+), 5 deletions(-)

diff --git a/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java b/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
index 92201af..7851a04 100644
--- a/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
+++ b/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
@@ -35,6 +35,7 @@
 @Component
 public class StationOperateProcessUtils {
     private static final int LOOP_LOAD_RESERVE_EXPIRE_SECONDS = 120;
+    private static final int OUT_ORDER_DISPATCH_LIMIT_SECONDS = 10;
 
     @Autowired
     private BasDevpService basDevpService;
@@ -556,6 +557,10 @@
                     continue;
                 }
 
+                if (isWatchingCircleArrival(wrkMast.getWrkNo(), stationProtocol.getStationId())) {
+                    continue;
+                }
+
                 if (Cools.isEmpty(wrkMast.getBatch())) {
                     continue;
                 }
@@ -600,6 +605,10 @@
                         News.taskInfo(wrkMast.getWrkNo(), "鑾峰彇杈撻�佺嚎鍛戒护澶辫触");
                         continue;
                     }
+                    if (!tryAcquireOutOrderDispatchLock(wrkMast.getWrkNo(), stationProtocol.getStationId())) {
+                        continue;
+                    }
+                    clearWatchCircleCommand(wrkMast.getWrkNo());
                     MessageQueue.offer(SlaveType.Devp, stationObjModel.getDeviceNo(), new Task(2, command));
                     News.info("{}浠诲姟鐩存帴鍘荤洰鏍囩偣", wrkMast.getWrkNo());
                 } else if (commandType.equals("toCircle")) {
@@ -615,8 +624,11 @@
                         News.taskInfo(wrkMast.getWrkNo(), "鑾峰彇杈撻�佺嚎鍛戒护澶辫触");
                         continue;
                     }
+                    if (!tryAcquireOutOrderDispatchLock(wrkMast.getWrkNo(), stationProtocol.getStationId())) {
+                        continue;
+                    }
                     MessageQueue.offer(SlaveType.Devp, stationObjModel.getDeviceNo(), new Task(2, command));
-                    redisUtil.set(RedisKeyType.WATCH_CIRCLE_STATION_.key + wrkMast.getWrkNo(), JSON.toJSONString(command, SerializerFeature.DisableCircularReferenceDetect), 60 * 60 * 24);
+                    saveWatchCircleCommand(wrkMast.getWrkNo(), command);
                     News.info("{}浠诲姟杩涜缁曞湀", wrkMast.getWrkNo());
                 }
             }
@@ -647,12 +659,10 @@
                     continue;
                 }
 
-                Object circleObj = redisUtil.get(RedisKeyType.WATCH_CIRCLE_STATION_.key + stationProtocol.getTaskNo());
-                if (circleObj == null) {
+                StationCommand circleCommand = getWatchCircleCommand(stationProtocol.getTaskNo());
+                if (circleCommand == null) {
                     continue;
                 }
-
-                StationCommand circleCommand = JSON.parseObject(circleObj.toString(), StationCommand.class);
                 if (!stationProtocol.getStationId().equals(circleCommand.getTargetStaNo())) {
                     continue;
                 }
@@ -673,6 +683,14 @@
                 if (command == null) {
                     News.taskInfo(wrkMast.getWrkNo(), "鑾峰彇杈撻�佺嚎鍛戒护澶辫触");
                     continue;
+                }
+                if (!tryAcquireOutOrderDispatchLock(wrkMast.getWrkNo(), stationProtocol.getStationId())) {
+                    continue;
+                }
+                if (Objects.equals(moveStaNo, wrkMast.getStaNo())) {
+                    clearWatchCircleCommand(wrkMast.getWrkNo());
+                } else {
+                    saveWatchCircleCommand(wrkMast.getWrkNo(), command);
                 }
                 MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command));
             }
@@ -741,6 +759,54 @@
         return null;
     }
 
+    private boolean tryAcquireOutOrderDispatchLock(Integer wrkNo, Integer stationId) {
+        if (wrkNo == null || wrkNo <= 0 || stationId == null) {
+            return true;
+        }
+        String key = RedisKeyType.STATION_OUT_ORDER_DISPATCH_LIMIT_.key + wrkNo + "_" + stationId;
+        Object lock = redisUtil.get(key);
+        if (lock != null) {
+            return false;
+        }
+        redisUtil.set(key, "lock", OUT_ORDER_DISPATCH_LIMIT_SECONDS);
+        return true;
+    }
+
+    private boolean isWatchingCircleArrival(Integer wrkNo, Integer stationId) {
+        StationCommand command = getWatchCircleCommand(wrkNo);
+        return command != null && stationId != null && stationId.equals(command.getTargetStaNo());
+    }
+
+    private StationCommand getWatchCircleCommand(Integer wrkNo) {
+        if (wrkNo == null || wrkNo <= 0) {
+            return null;
+        }
+        Object circleObj = redisUtil.get(RedisKeyType.WATCH_CIRCLE_STATION_.key + wrkNo);
+        if (circleObj == null) {
+            return null;
+        }
+        try {
+            return JSON.parseObject(circleObj.toString(), StationCommand.class);
+        } catch (Exception ignore) {
+            return null;
+        }
+    }
+
+    private void saveWatchCircleCommand(Integer wrkNo, StationCommand command) {
+        if (wrkNo == null || wrkNo <= 0 || command == null) {
+            return;
+        }
+        redisUtil.set(RedisKeyType.WATCH_CIRCLE_STATION_.key + wrkNo,
+                JSON.toJSONString(command, SerializerFeature.DisableCircularReferenceDetect), 60 * 60 * 24);
+    }
+
+    private void clearWatchCircleCommand(Integer wrkNo) {
+        if (wrkNo == null || wrkNo <= 0) {
+            return;
+        }
+        redisUtil.del(RedisKeyType.WATCH_CIRCLE_STATION_.key + wrkNo);
+    }
+
     public Integer getOutStationBatchSeq(List<NavigateNode> pathList, Integer searchStationId, String searchBatch) {
         List<Integer> checkList = new ArrayList<>();
         for (int i = pathList.size() - 1; i >= 0; i--) {

--
Gitblit v1.9.1