From e14a4372b6bd4a38e40a3a68bde32350d96071ab Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期四, 15 一月 2026 13:11:56 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java |  602 ++++++++++++++++++++++++++---------------------------
 1 files changed, 295 insertions(+), 307 deletions(-)

diff --git a/src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java b/src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java
index 9680550..4530d45 100644
--- a/src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java
+++ b/src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java
@@ -3,7 +3,6 @@
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.zy.asrs.entity.DeviceConfig;
-import com.zy.common.model.NavigateNode;
 import com.zy.common.utils.RedisUtil;
 import com.zy.core.News;
 import com.zy.core.enums.RedisKeyType;
@@ -23,12 +22,14 @@
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
 import java.util.Map;
-import java.util.function.Supplier;
+import java.util.Arrays;
 
 public class ZyStationFakeSegConnect implements ZyStationConnectApi {
 
-    private static int LOCK_STATION = 0;
+    // 绔欑偣绾ч攣锛氭瘡涓珯鐐圭嫭绔嬩竴鎶婇攣锛屾彁鍗囧苟鍙戞�ц兘
+    private final Map<Integer, ReentrantLock> stationLocks = new ConcurrentHashMap<>();
     private HashMap<Integer, List<ZyStationStatusEntity>> deviceStatusMap = new HashMap<>();
     private HashMap<Integer, DeviceConfig> deviceConfigMap = new HashMap<>();
     private RedisUtil redisUtil;
@@ -66,7 +67,8 @@
         }
         DeviceConfig deviceConfig = deviceConfigMap.get(deviceNo);
         if (statusList.isEmpty()) {
-            List<ZyStationStatusEntity> init = JSON.parseArray(deviceConfig.getFakeInitStatus(), ZyStationStatusEntity.class);
+            List<ZyStationStatusEntity> init = JSON.parseArray(deviceConfig.getFakeInitStatus(),
+                    ZyStationStatusEntity.class);
             if (init != null) {
                 statusList.addAll(init);
                 for (ZyStationStatusEntity status : statusList) {
@@ -94,11 +96,19 @@
             return new CommandResponse(false, "浠诲姟鍙蜂负绌�");
         }
 
-        taskQueues.computeIfAbsent(taskNo, k -> new LinkedBlockingQueue<>()).offer(command);
-        taskLastUpdateTime.put(taskNo, System.currentTimeMillis());
+        // 澶勭悊闈炵Щ鍔ㄥ懡浠�
+        if (command.getCommandType() != StationCommandType.MOVE) {
+            handleCommand(deviceNo, command);
+        } else {
+            // 灏嗙Щ鍔ㄥ懡浠よ拷鍔犲埌浠诲姟闃熷垪锛堟敮鎸佸垎娈典笅鍙戯級
+            taskQueues.computeIfAbsent(taskNo, k -> new LinkedBlockingQueue<>()).offer(command);
+            taskLastUpdateTime.put(taskNo, System.currentTimeMillis());
 
-        if (taskRunning.putIfAbsent(taskNo, true) == null) {
-            executor.submit(() -> runTaskLoop(deviceNo, taskNo));
+            // 鍙湁浠诲姟鏈惎鍔ㄦ椂鎵嶅惎鍔ㄦ墽琛屽櫒锛屽悗缁垎娈靛懡浠や粎杩藉姞鍒伴槦鍒�
+            if (taskRunning.putIfAbsent(taskNo, true) == null) {
+                executor.submit(() -> runTaskLoop(deviceNo, taskNo));
+            }
+            // 鍚庣画鍒嗘鍛戒护涓嶅啀杩斿洖閿欒锛屾甯歌拷鍔犲埌闃熷垪
         }
 
         return new CommandResponse(true, "鍛戒护宸插彈鐞嗭紙寮傛鎵ц锛�");
@@ -106,122 +116,171 @@
 
     private void runTaskLoop(Integer deviceNo, Integer taskNo) {
         try {
-            // 鐢ㄤ簬瀛樺偍褰撳墠浠诲姟寰呮墽琛岀殑瀹屾暣璺緞闃熷垪
-            BlockingQueue<Integer> pathQueue = new LinkedBlockingQueue<>();
-            // 褰撳墠鏄惁姝e湪鎵ц绉诲姩
-            boolean isMoving = false;
-            // 褰撳墠绉诲姩鍒扮殑璺緞绱㈠紩
-            int currentPathIndex = 0;
-            // 瀹屾暣璺緞璁板綍
-            List<Integer> fullPath = new ArrayList<>();
-            
-            StationCommand initialCommand = null;
+            // 寰呮墽琛岀殑璺緞闃熷垪锛堝瓨鍌ㄧ珯鐐笽D搴忓垪锛�
+            LinkedBlockingQueue<Integer> pendingPathQueue = new LinkedBlockingQueue<>();
+            // 褰撳墠鎵�鍦ㄧ珯鐐笽D
+            Integer currentStationId = null;
+            // 鏈�缁堢洰鏍囩珯鐐笽D
             Integer finalTargetStationId = null;
+            // 鏄惁闇�瑕佺敓鎴愭潯鐮�
             boolean generateBarcode = false;
+            // 鏄惁宸插垵濮嬪寲璧风偣
+            boolean initialized = false;
+            // 涓婁竴姝ユ墽琛屾椂闂达紙鐢ㄤ簬鍫靛妫�娴嬶級
+            long stepExecuteTime = System.currentTimeMillis();
 
             while (true) {
+                if (Thread.currentThread().isInterrupted()) {
+                    break;
+                }
+
                 BlockingQueue<StationCommand> commandQueue = taskQueues.get(taskNo);
                 if (commandQueue == null) {
                     break;
                 }
 
-                // 灏濊瘯鑾峰彇鏂板懡浠わ紝濡傛灉娌℃湁鏂板懡浠ゅ垯缁х画鎵ц鐜版湁璺緞
+                // 灏濊瘯鑾峰彇鏂扮殑鍒嗘鍛戒护
                 StationCommand command = commandQueue.poll(100, TimeUnit.MILLISECONDS);
                 if (command != null) {
                     taskLastUpdateTime.put(taskNo, System.currentTimeMillis());
-                    
-                    if (initialCommand == null) {
-                        initialCommand = command;
+
+                    // 棣栨鎺ユ敹鍛戒护鏃跺垵濮嬪寲
+                    if (finalTargetStationId == null) {
                         finalTargetStationId = command.getTargetStaNo();
                         if (checkTaskNoInArea(taskNo)) {
                             generateBarcode = true;
                         }
                     }
 
+                    // 灏嗘柊璺緞杩藉姞鍒板緟鎵ц闃熷垪
                     List<Integer> newPath = command.getNavigatePath();
                     if (newPath != null && !newPath.isEmpty()) {
-                        // 濡傛灉鏄涓�娈佃矾寰�
-                        if (fullPath.isEmpty()) {
-                            fullPath.addAll(newPath);
-                            for (Integer stationId : newPath) {
-                                pathQueue.offer(stationId);
-                            }
-                        } else {
-                            // 杩藉姞璺緞锛岄渶瑕佸幓閲嶈鎺ョ偣
-                            Integer lastStationId = fullPath.get(fullPath.size() - 1);
-                            int startIndex = 0;
-                            if (!newPath.isEmpty() && newPath.get(0).equals(lastStationId)) {
-                                startIndex = 1;
-                            }
-                            for (int i = startIndex; i < newPath.size(); i++) {
-                                Integer stationId = newPath.get(i);
-                                fullPath.add(stationId);
-                                pathQueue.offer(stationId);
-                            }
+                        // 鑾峰彇闃熷垪涓渶鍚庝竴涓珯鐐癸紙鐢ㄤ簬琛旀帴鐐瑰幓閲嶏級
+                        Integer lastInQueue = getLastInQueue(pendingPathQueue);
+                        if (lastInQueue == null) {
+                            lastInQueue = currentStationId;
                         }
-                    }
-                    
-                    // 澶勭悊闈炵Щ鍔ㄥ懡浠�
-                    if (command.getCommandType() != StationCommandType.MOVE) {
-                        handleCommand(deviceNo, command);
+
+                        int startIndex = 0;
+                        // 濡傛灉鏂拌矾寰勭殑璧风偣涓庡綋鍓嶄綅缃垨闃熷垪鏈熬閲嶅锛屽垯璺宠繃
+                        if (lastInQueue != null && !newPath.isEmpty() && newPath.get(0).equals(lastInQueue)) {
+                            startIndex = 1;
+                        }
+
+                        for (int i = startIndex; i < newPath.size(); i++) {
+                            pendingPathQueue.offer(newPath.get(i));
+                        }
+
+                        News.info("[WCS Debug] 浠诲姟{}杩藉姞璺緞娈�: {} -> 闃熷垪澶у皬: {}", taskNo, newPath, pendingPathQueue.size());
                     }
                 }
 
                 // 鎵ц绉诲姩閫昏緫
-                if (!pathQueue.isEmpty()) {
-                    // 濡傛灉鍒氬紑濮嬶紝鍏堝垵濮嬪寲褰撳墠浣嶇疆
-                    if (currentPathIndex == 0 && !fullPath.isEmpty()) {
-                         Integer startStationId = fullPath.get(0);
-                         Integer deviceId = getDeviceNoByStationId(startStationId);
-                         if (deviceId != null) {
-                             initStationMove(taskNo, startStationId, deviceId, taskNo, finalTargetStationId, true, null);
-                         }
-                    }
+                if (!pendingPathQueue.isEmpty()) {
+                    Integer nextStationId = pendingPathQueue.peek();
 
-                    // 鍙栧嚭涓嬩竴涓洰鏍囩偣
-                    Integer nextStationId = pathQueue.peek(); // 杩欓噷鐨勯�昏緫闇�瑕佹敼涓洪�愪釜琛岃蛋
-                    // 瀹為檯琛岃蛋閫昏緫搴旇鏄湪杩欓噷娑堣垂 pathQueue
-                    // 涓轰簡绠�鍖栵紝鎴戜滑灏� pathQueue 杞负 stationMoveByPathIds 鐨勯�昏緫锛屼絾杩欓噷闇�瑕佹敼閫犳垚姝ヨ繘寮�
-                    
-                    // 閲嶆柊璁捐锛歳unTaskLoop 璐熻矗涓嶆柇娑堣垂 pathQueue 骞舵墽琛屽崟姝ョЩ鍔�
-                    if (nextStationId != null) {
-                        Integer currentStationId = fullPath.get(currentPathIndex);
-                        if (currentStationId.equals(nextStationId)) {
-                            pathQueue.poll(); // 绉婚櫎宸插埌杈剧殑鐐�
+                    // 濡傛灉灏氭湭鍒濆鍖栬捣鐐�
+                    if (!initialized && currentStationId == null) {
+                        // 浼樺厛鏌ユ壘鎵樼洏褰撳墠瀹為檯浣嶇疆锛堟敮鎸佸牭濉炲悗閲嶈矾鐢卞満鏅級
+                        Integer actualCurrentStationId = findCurrentStationIdByTask(taskNo);
+                        if (actualCurrentStationId != null) {
+                            // 鎵惧埌浜嗗綋鍓嶆墭鐩樹綅缃紝浣跨敤瀹為檯浣嶇疆浣滀负璧风偣
+                            currentStationId = actualCurrentStationId;
+                            initialized = true;
+
+                            // 娓呴櫎璇ョ珯鐐圭殑 runBlock 鏍囪锛堝牭濉炴仮澶嶏級
+                            Integer deviceId = getDeviceNoByStationId(currentStationId);
+                            if (deviceId != null) {
+                                clearRunBlock(currentStationId, deviceId);
+                            }
+
+                            // 濡傛灉璺緞璧风偣涓庡綋鍓嶄綅缃浉鍚岋紝绉婚櫎璧风偣閬垮厤閲嶅
+                            if (nextStationId.equals(currentStationId)) {
+                                pendingPathQueue.poll();
+                            }
+
+                            stepExecuteTime = System.currentTimeMillis();
+                            News.info("[WCS Debug] 浠诲姟{}鎭㈠鎵ц锛屽綋鍓嶄綅缃�: {}", taskNo, currentStationId);
                             continue;
                         }
-                        
-                        // 鎵ц浠� currentStationId 鍒� nextStationId 鐨勭Щ鍔�
-                        Integer currentDeviceNo = getDeviceNoByStationId(currentStationId);
-                        Integer nextDeviceNo = getDeviceNoByStationId(nextStationId);
-                        
-                        if (currentDeviceNo != null && nextDeviceNo != null) {
-                            boolean moveSuccess = stationMoveToNext(taskNo, currentStationId, currentDeviceNo, nextStationId, nextDeviceNo, taskNo, finalTargetStationId);
-                            if (moveSuccess) {
-                                currentPathIndex++;
-                                pathQueue.poll();
-                                sleep(1000); // 妯℃嫙鑰楁椂
-                            } else {
-                                sleep(1000); // 澶辫触閲嶈瘯绛夊緟
+
+                        // 鏈壘鍒板綋鍓嶄綅缃紙棣栨鎵ц锛夛紝棣栦釜绔欑偣灏辨槸璧风偣
+                        currentStationId = nextStationId;
+                        Integer deviceId = getDeviceNoByStationId(currentStationId);
+                        if (deviceId != null) {
+                            boolean result = initStationMove(taskNo, currentStationId, deviceId, taskNo,
+                                    finalTargetStationId, true, null);
+                            if (result) {
+                                initialized = true;
+                                pendingPathQueue.poll(); // 绉婚櫎璧风偣
+                                stepExecuteTime = System.currentTimeMillis();
+                                News.info("[WCS Debug] 浠诲姟{}鍒濆鍖栬捣鐐�: {}", taskNo, currentStationId);
                             }
-                        } else {
-                            pathQueue.poll(); // 鏃犳硶鎵ц锛岃烦杩�
                         }
+                        sleep(500);
+                        continue;
+                    }
+
+                    // 鎵ц浠庡綋鍓嶇珯鐐瑰埌涓嬩竴绔欑偣鐨勭Щ鍔�
+                    Integer currentDeviceNo = getDeviceNoByStationId(currentStationId);
+                    Integer nextDeviceNo = getDeviceNoByStationId(nextStationId);
+
+                    if (currentDeviceNo != null && nextDeviceNo != null) {
+                        boolean moveSuccess = stationMoveToNext(taskNo, currentStationId, currentDeviceNo,
+                                nextStationId, nextDeviceNo, taskNo, finalTargetStationId);
+                        if (moveSuccess) {
+                            currentStationId = nextStationId;
+                            pendingPathQueue.poll();
+                            stepExecuteTime = System.currentTimeMillis();
+                            News.info("[WCS Debug] 浠诲姟{}绉诲姩鍒扮珯鐐�: {}, 鍓╀綑闃熷垪: {}", taskNo, currentStationId,
+                                    pendingPathQueue.size());
+                            sleep(1000); // 妯℃嫙绉诲姩鑰楁椂
+                        } else {
+                            // 绉诲姩澶辫触锛屾鏌ユ槸鍚﹀牭濉�
+                            if (!checkTaskNoInArea(taskNo)) {
+                                boolean fakeAllowCheckBlock = getFakeAllowCheckBlock();
+
+                                if (fakeAllowCheckBlock && System.currentTimeMillis() - stepExecuteTime > 10000) {
+                                    // 璁ゅ畾鍫靛
+                                    boolean result = runBlockStation(taskNo, currentStationId, currentDeviceNo, taskNo,
+                                            currentStationId);
+                                    if (result) {
+                                        News.info("[WCS Debug] 浠诲姟{}鍦ㄧ珯鐐箋}琚爣璁颁负鍫靛", taskNo, currentStationId);
+                                        pendingPathQueue.clear();
+                                        break;
+                                    }
+                                }
+                            }
+                            sleep(500); // 澶辫触閲嶈瘯绛夊緟
+                        }
+                    } else {
+                        // 鏃犳硶鑾峰彇璁惧鍙凤紝璺宠繃璇ョ珯鐐�
+                        pendingPathQueue.poll();
                     }
                 } else {
-                    // 闃熷垪涓虹┖锛屾鏌ユ槸鍚﹁秴鏃�
-                    Long lastTime = taskLastUpdateTime.get(taskNo);
-                    if (lastTime != null && System.currentTimeMillis() - lastTime > 30000) {
-                        // 瀹屾垚浠诲姟鍚庢竻鐞�
-                        if (fullPath.size() > 0 && generateBarcode) {
-                             Integer lastStation = fullPath.get(fullPath.size()-1);
-                             Integer lastDevice = getDeviceNoByStationId(lastStation);
-                             if (lastDevice != null) {
-                                 generateStationBarcode(taskNo, finalTargetStationId, lastDevice);
-                             }
+                    // 璺緞闃熷垪涓虹┖锛岀瓑寰呮柊鐨勫垎娈靛懡浠�
+                    if (currentStationId != null && finalTargetStationId != null
+                            && currentStationId.equals(finalTargetStationId)) {
+                        // 宸插埌杈炬渶缁堢洰鏍囷紝姝e父缁撴潫
+                        if (generateBarcode) {
+                            Integer targetDeviceNo = getDeviceNoByStationId(finalTargetStationId);
+                            if (targetDeviceNo != null) {
+                                generateStationBarcode(taskNo, finalTargetStationId, targetDeviceNo);
+                                News.info("[WCS Debug] 浠诲姟{}鍒拌揪鐩爣{}骞剁敓鎴愭潯鐮�", taskNo, finalTargetStationId);
+                            }
                         }
                         break;
                     }
+
+                    // 鏈埌杈炬渶缁堢洰鏍囷紝绛夊緟鏂扮殑鍒嗘鍛戒护
+                    Long lastTime = taskLastUpdateTime.get(taskNo);
+                    if (lastTime != null && System.currentTimeMillis() - lastTime > 30000) {
+                        // 瓒呮椂锛�30绉掑唴娌℃湁鏀跺埌鏂板垎娈靛懡浠�
+                        News.info("[WCS Debug] 浠诲姟{}绛夊緟鍒嗘瓒呮椂锛屽綋鍓嶄綅缃�: {}, 鐩爣: {}", taskNo, currentStationId,
+                                finalTargetStationId);
+                        break;
+                    }
+                    // 缁х画绛夊緟鏂板垎娈靛懡浠わ紙涓嶅仛浠讳綍浜嬫儏锛屼笅涓�杞惊鐜細灏濊瘯鑾峰彇鏂板懡浠わ級
                 }
             }
         } catch (InterruptedException e) {
@@ -230,7 +289,35 @@
             taskQueues.remove(taskNo);
             taskLastUpdateTime.remove(taskNo);
             taskRunning.remove(taskNo);
+            News.info("[WCS Debug] 浠诲姟{}鎵ц缁撴潫骞舵竻鐞嗚祫婧�", taskNo);
         }
+    }
+
+    /**
+     * 鑾峰彇闃熷垪涓渶鍚庝竴涓厓绱狅紙涓嶇Щ闄わ級
+     */
+    private Integer getLastInQueue(LinkedBlockingQueue<Integer> queue) {
+        Integer last = null;
+        for (Integer item : queue) {
+            last = item;
+        }
+        return last;
+    }
+
+    /**
+     * 鑾峰彇鏄惁鍏佽妫�鏌ュ牭濉炵殑閰嶇疆
+     */
+    private boolean getFakeAllowCheckBlock() {
+        boolean fakeAllowCheckBlock = true;
+        Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
+        if (systemConfigMapObj != null) {
+            HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
+            String value = systemConfigMap.get("fakeAllowCheckBlock");
+            if (value != null && !value.equals("Y")) {
+                fakeAllowCheckBlock = false;
+            }
+        }
+        return fakeAllowCheckBlock;
     }
 
     @Override
@@ -261,7 +348,7 @@
 
         if (command.getCommandType() == StationCommandType.WRITE_INFO) {
             if (taskNo == 9998 && targetStationId == 0) {
-                //鐢熸垚鍑哄簱绔欑偣浠跨湡鏁版嵁
+                // 鐢熸垚鍑哄簱绔欑偣浠跨湡鏁版嵁
                 generateFakeOutStationData(deviceNo, stationId);
                 return;
             }
@@ -270,12 +357,7 @@
         if (taskNo > 0 && taskNo != 9999 && taskNo != 9998 && stationId == targetStationId) {
             generateStationData(deviceNo, taskNo, stationId, targetStationId);
         }
-
-        List<Integer> navigatePath = command.getNavigatePath();
-        if (navigatePath != null && !navigatePath.isEmpty()) {
-            segmentedPathCommand(command, generateBarcode);
-            return;
-        }
+        // 娉ㄦ剰锛歁OVE 绫诲瀷鐨勫懡浠ょ幇宸插湪 sendCommand 涓鐞嗭紝handleCommand 浠呭鐞嗛潪 MOVE 鍛戒护
     }
 
     private void generateFakeOutStationData(Integer deviceNo, Integer stationId) {
@@ -320,32 +402,7 @@
         }
     }
 
-    private void segmentedPathCommand(StationCommand command, boolean generateBarcode) {
-        Integer taskNo = command.getTaskNo();
-        Integer finalTargetStationId = command.getTargetStaNo();
-        List<Integer> path = command.getNavigatePath();
-        if (path == null || path.isEmpty()) {
-            return;
-        }
-        
-        Integer currentStationId = findCurrentStationIdByTask(taskNo);
-        int startIdx = 0;
-        if (currentStationId != null) {
-            int idx = path.indexOf(currentStationId);
-            if (idx >= 0) {
-                startIdx = idx;
-            }
-        }
-        
-        Integer segmentTargetStationId = path.get(path.size() - 1);
-        int endIdx = path.size() - 1;
-        
-        boolean isFinalSegment = segmentTargetStationId.equals(finalTargetStationId);
-        boolean appendMode = currentStationId != null && path.indexOf(currentStationId) >= 0;
-        boolean generateBarcodeFinal = generateBarcode && isFinalSegment;
-        
-        stationMoveByPathIds(path, taskNo, finalTargetStationId, false, generateBarcodeFinal, startIdx, endIdx, appendMode);
-    }
+    // segmentedPathCommand 鏂规硶宸插垹闄わ紝鍔熻兘宸叉暣鍚堝埌 runTaskLoop
 
     private Integer getDeviceNoByStationId(Integer stationId) {
         for (Integer devNo : deviceStatusMap.keySet()) {
@@ -377,116 +434,7 @@
         return null;
     }
 
-    private boolean stationMoveByPathIds(List<Integer> stationPath, Integer taskNo, Integer targetStationId, boolean clearData, boolean generateBarcode, int startIdx, int endIdx, boolean appendMode) {
-        Integer lastStationId = null;
-        Integer targetStationDeviceNo = getDeviceNoByStationId(targetStationId);
-        long executeTime = System.currentTimeMillis();
-        int i = Math.max(0, startIdx);
-        while (i < stationPath.size() && i <= endIdx) {
-            if (Thread.currentThread().isInterrupted()) {
-                return false;
-            }
-            Integer currentStationId = stationPath.get(i);
-            Integer currentStationDeviceNo = getDeviceNoByStationId(currentStationId);
-            if (currentStationDeviceNo == null) {
-                return false;
-            }
-
-            Integer nextStationId = null;
-            Integer nextStationDeviceNo = null;
-            try {
-                nextStationId = stationPath.get(i + 1);
-                nextStationDeviceNo = getDeviceNoByStationId(nextStationId);
-            } catch (Exception ignore) {}
-
-            if (!checkTaskNoInArea(taskNo)) {
-                boolean fakeAllowCheckBlock = true;
-                Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
-                if (systemConfigMapObj != null) {
-                    HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
-                    if (!systemConfigMap.get("fakeAllowCheckBlock").equals("Y")) {
-                        fakeAllowCheckBlock = false;
-                    }
-                }
-
-                if (fakeAllowCheckBlock && System.currentTimeMillis() - executeTime > 1000 * 10) {
-                    boolean result = runBlockStation(taskNo, currentStationId, currentStationDeviceNo, taskNo, currentStationId);
-                    if(!result) {
-                        continue;
-                    }
-                    return false;
-                }
-            }
-
-            if (i == startIdx && !appendMode) {
-                boolean result = initStationMove(taskNo, currentStationId, currentStationDeviceNo, taskNo, targetStationId, true, null);
-                if (!result) {
-                    continue;
-                }
-                sleep(1000);
-                if (Thread.currentThread().isInterrupted()) {
-                    return false;
-                }
-            }
-
-            if (nextStationId != null && nextStationDeviceNo != null) {
-                boolean result = stationMoveToNext(taskNo, currentStationId, currentStationDeviceNo, nextStationId, nextStationDeviceNo, taskNo, targetStationId);
-                if (!result) {
-                    continue;
-                }
-                lastStationId = currentStationId;
-            }
-
-            if (currentStationId.equals(targetStationId)) {
-                break;
-            }
-
-            i++;
-            executeTime = System.currentTimeMillis();
-            sleep(1000);
-            if (Thread.currentThread().isInterrupted()) {
-                return false;
-            }
-        }
-
-        if (generateBarcode) {
-            if (lastStationId != null && targetStationDeviceNo != null) {
-                while (true) {
-                    if (Thread.currentThread().isInterrupted()) {
-                        break;
-                    }
-                    boolean result = generateStationBarcode(taskNo, targetStationId, targetStationDeviceNo);
-                    sleep(1000);
-                    if (!result) {
-                        continue;
-                    }
-                    break;
-                }
-            }
-        }
-
-        if (clearData) {
-            sleep(10000);
-            if (Thread.currentThread().isInterrupted()) {
-                return true;
-            }
-            if (lastStationId != null && targetStationDeviceNo != null) {
-                while (true) {
-                    if (Thread.currentThread().isInterrupted()) {
-                        break;
-                    }
-                    boolean result = clearStation(targetStationDeviceNo, taskNo, targetStationId);
-                    sleep(1000);
-                    if (!result) {
-                        continue;
-                    }
-                    break;
-                }
-            }
-        }
-
-        return true;
-    }
+    // stationMoveByPathIds 鏂规硶宸插垹闄わ紝鍔熻兘宸叉暣鍚堝埌 runTaskLoop
 
     private void sleep(long ms) {
         try {
@@ -496,32 +444,40 @@
         }
     }
 
-    public synchronized boolean setLockStation(Integer uuid) {
-        if (LOCK_STATION == 0) {
-            LOCK_STATION = uuid;
-            return true;
-        }else {
-            if(LOCK_STATION == uuid) {
-                return true;
-            }
-        }
-        return false;
+    /**
+     * 鑾峰彇绔欑偣閿侊紝濡傛灉涓嶅瓨鍦ㄥ垯鍒涘缓
+     */
+    private ReentrantLock getStationLock(Integer stationId) {
+        return stationLocks.computeIfAbsent(stationId, k -> new ReentrantLock());
     }
 
-    public synchronized boolean releaseLockStation(Integer uuid) {
-        if (LOCK_STATION != uuid) {
-            return false;
+    /**
+     * 鎸夐『搴忛攣瀹氬涓珯鐐癸紙閬垮厤姝婚攣锛�
+     */
+    private void lockStations(Integer... stationIds) {
+        Integer[] sorted = Arrays.copyOf(stationIds, stationIds.length);
+        Arrays.sort(sorted);
+        for (Integer stationId : sorted) {
+            getStationLock(stationId).lock();
         }
-
-        LOCK_STATION = 0;
-        return true;
     }
 
-    public synchronized boolean updateStationData(Integer lockTaskNo, Integer stationId, Integer deviceNo, Integer taskNo, Integer targetStaNo, Boolean isLoading, String barcode, Boolean runBlock) {
-        if (LOCK_STATION != lockTaskNo) {
-            return false;
+    /**
+     * 鎸夐�嗗簭瑙i攣澶氫釜绔欑偣
+     */
+    private void unlockStations(Integer... stationIds) {
+        Integer[] sorted = Arrays.copyOf(stationIds, stationIds.length);
+        Arrays.sort(sorted);
+        for (int i = sorted.length - 1; i >= 0; i--) {
+            getStationLock(sorted[i]).unlock();
         }
+    }
 
+    /**
+     * 鏇存柊绔欑偣鏁版嵁锛堣皟鐢ㄥ墠蹇呴』宸叉寔鏈夎绔欑偣鐨勯攣锛�
+     */
+    private boolean updateStationDataInternal(Integer stationId, Integer deviceNo, Integer taskNo, Integer targetStaNo,
+            Boolean isLoading, String barcode, Boolean runBlock) {
         List<ZyStationStatusEntity> statusList = deviceStatusMap.get(deviceNo);
         if (statusList == null) {
             return false;
@@ -556,8 +512,13 @@
         return true;
     }
 
-    public synchronized boolean initStationMove(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo, Integer taskNo, Integer targetStationId, Boolean isLoading, String barcode) {
-        boolean executeResult = lockExecute(lockTaskNo, () -> {
+    /**
+     * 鍒濆鍖栫珯鐐圭Щ鍔紙浣跨敤绔欑偣绾ч攣锛�
+     */
+    public boolean initStationMove(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo,
+            Integer taskNo, Integer targetStationId, Boolean isLoading, String barcode) {
+        lockStations(currentStationId);
+        try {
             List<ZyStationStatusEntity> statusList = deviceStatusMap.get(currentStationDeviceNo);
             if (statusList == null) {
                 return false;
@@ -576,25 +537,28 @@
                 }
             }
 
-            boolean result = updateStationData(lockTaskNo, currentStationId, currentStationDeviceNo, taskNo, targetStationId, isLoading, barcode, false);
-            if (!result) {
-                return false;
-            }
-            return true;
-        });
-
-        return executeResult;
+            return updateStationDataInternal(currentStationId, currentStationDeviceNo, taskNo, targetStationId,
+                    isLoading, barcode, false);
+        } finally {
+            unlockStations(currentStationId);
+        }
     }
 
-    public synchronized boolean stationMoveToNext(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo, Integer nextStationId, Integer nextStationDeviceNo, Integer taskNo, Integer targetStaNo) {
-        boolean executeResult = lockExecute(lockTaskNo, () -> {
+    /**
+     * 绔欑偣绉诲姩鍒颁笅涓�涓綅缃紙浣跨敤绔欑偣绾ч攣锛屾寜ID椤哄簭鑾峰彇閿侀伩鍏嶆閿侊級
+     */
+    public boolean stationMoveToNext(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo,
+            Integer nextStationId, Integer nextStationDeviceNo, Integer taskNo, Integer targetStaNo) {
+        // 鍚屾椂閿佸畾褰撳墠绔欑偣鍜屼笅涓�涓珯鐐癸紙鎸塈D椤哄簭锛岄伩鍏嶆閿侊級
+        lockStations(currentStationId, nextStationId);
+        try {
             List<ZyStationStatusEntity> statusList = deviceStatusMap.get(currentStationDeviceNo);
             if (statusList == null) {
                 return false;
             }
 
             List<ZyStationStatusEntity> nextStatusList = deviceStatusMap.get(nextStationDeviceNo);
-            if (statusList == null) {
+            if (nextStatusList == null) {
                 return false;
             }
 
@@ -612,23 +576,31 @@
                 return false;
             }
 
-            boolean result = updateStationData(lockTaskNo, nextStationId, nextStationDeviceNo, taskNo, targetStaNo, true, null, false);
+            boolean result = updateStationDataInternal(nextStationId, nextStationDeviceNo, taskNo, targetStaNo, true,
+                    null, false);
             if (!result) {
                 return false;
             }
 
-            boolean result2 = updateStationData(lockTaskNo, currentStationId, currentStationDeviceNo, 0, 0, false, "", false);
+            boolean result2 = updateStationDataInternal(currentStationId, currentStationDeviceNo, 0, 0, false, "",
+                    false);
             if (!result2) {
                 return false;
             }
 
             return true;
-        });
-        return executeResult;
+        } finally {
+            unlockStations(currentStationId, nextStationId);
+        }
     }
 
-    public synchronized boolean generateStationBarcode(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo) {
-        boolean executeResult = lockExecute(lockTaskNo, () -> {
+    /**
+     * 鐢熸垚绔欑偣鏉$爜锛堜娇鐢ㄧ珯鐐圭骇閿侊級
+     */
+    public boolean generateStationBarcode(Integer lockTaskNo, Integer currentStationId,
+            Integer currentStationDeviceNo) {
+        lockStations(currentStationId);
+        try {
             List<ZyStationStatusEntity> statusList = deviceStatusMap.get(currentStationDeviceNo);
             if (statusList == null) {
                 return false;
@@ -644,20 +616,21 @@
             Random random = new Random();
 
             String barcodeTime = String.valueOf(System.currentTimeMillis());
-            String barcode = String.valueOf(random.nextInt(10)) + String.valueOf(random.nextInt(10)) + barcodeTime.substring(7);
+            String barcode = String.valueOf(random.nextInt(10)) + String.valueOf(random.nextInt(10))
+                    + barcodeTime.substring(7);
 
-            boolean result = updateStationData(lockTaskNo, currentStationId, currentStationDeviceNo, null, null, null, barcode, null);
-            if (!result) {
-                return false;
-            }
-            return true;
-        });
-
-        return executeResult;
+            return updateStationDataInternal(currentStationId, currentStationDeviceNo, null, null, null, barcode, null);
+        } finally {
+            unlockStations(currentStationId);
+        }
     }
 
-    public synchronized boolean clearStation(Integer deviceNo, Integer lockTaskNo, Integer currentStationId) {
-        boolean executeResult = lockExecute(lockTaskNo, () -> {
+    /**
+     * 娓呴櫎绔欑偣鏁版嵁锛堜娇鐢ㄧ珯鐐圭骇閿侊級
+     */
+    public boolean clearStation(Integer deviceNo, Integer lockTaskNo, Integer currentStationId) {
+        lockStations(currentStationId);
+        try {
             List<ZyStationStatusEntity> statusList = deviceStatusMap.get(deviceNo);
             if (statusList == null) {
                 return false;
@@ -670,18 +643,19 @@
                 return false;
             }
 
-            boolean result = updateStationData(deviceNo, lockTaskNo, currentStationId, 0, 0, false, "", false);
-            if (!result) {
-                return false;
-            }
-            return true;
-        });
-
-        return executeResult;
+            return updateStationDataInternal(currentStationId, deviceNo, 0, 0, false, "", false);
+        } finally {
+            unlockStations(currentStationId);
+        }
     }
 
-    public synchronized boolean runBlockStation(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo, Integer taskNo, Integer blockStationId) {
-        boolean executeResult = lockExecute(lockTaskNo, () -> {
+    /**
+     * 鏍囪绔欑偣鍫靛锛堜娇鐢ㄧ珯鐐圭骇閿侊級
+     */
+    public boolean runBlockStation(Integer lockTaskNo, Integer currentStationId, Integer currentStationDeviceNo,
+            Integer taskNo, Integer blockStationId) {
+        lockStations(currentStationId);
+        try {
             List<ZyStationStatusEntity> statusList = deviceStatusMap.get(currentStationDeviceNo);
             if (statusList == null) {
                 return false;
@@ -694,24 +668,38 @@
                 return false;
             }
 
-            boolean result = updateStationData(lockTaskNo, currentStationId, currentStationDeviceNo, taskNo, blockStationId, true, "", true);
-            if (!result) {
-                return false;
-            }
-            return true;
-        });
-
-        return executeResult;
+            return updateStationDataInternal(currentStationId, currentStationDeviceNo, taskNo, blockStationId, true, "",
+                    true);
+        } finally {
+            unlockStations(currentStationId);
+        }
     }
 
-    public boolean lockExecute(Integer taskNo, Supplier<Boolean> function) {
-        if (!setLockStation(taskNo)) {
-            return false;
-        }
+    /**
+     * 娓呴櫎绔欑偣鍫靛鏍囪锛堝牭濉炴仮澶嶆椂浣跨敤锛�
+     */
+    public void clearRunBlock(Integer stationId, Integer deviceNo) {
+        lockStations(stationId);
+        try {
+            List<ZyStationStatusEntity> statusList = deviceStatusMap.get(deviceNo);
+            if (statusList == null) {
+                return;
+            }
 
-        boolean result = function.get();
-        releaseLockStation(taskNo);
-        return result;
+            ZyStationStatusEntity currentStatus = statusList.stream()
+                    .filter(item -> item.getStationId().equals(stationId)).findFirst().orElse(null);
+
+            if (currentStatus == null) {
+                return;
+            }
+
+            if (currentStatus.isRunBlock()) {
+                currentStatus.setRunBlock(false);
+                News.info("[WCS Debug] 绔欑偣{}鍫靛鏍囪宸叉竻闄�", stationId);
+            }
+        } finally {
+            unlockStations(stationId);
+        }
     }
 
     private boolean checkTaskNoInArea(Integer taskNo) {
@@ -724,7 +712,7 @@
         Integer start = data.getInteger("start");
         Integer end = data.getInteger("end");
 
-        if(taskNo >= start && taskNo <= end) {
+        if (taskNo >= start && taskNo <= end) {
             return true;
         }
 

--
Gitblit v1.9.1