From bdf43f554f9bc7f0de94e7530e79af9193341334 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期四, 19 三月 2026 15:38:49 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java |  173 ++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 146 insertions(+), 27 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 4530d45..d2cfe98 100644
--- a/src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java
+++ b/src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java
@@ -27,6 +27,7 @@
 import java.util.Arrays;
 
 public class ZyStationFakeSegConnect implements ZyStationConnectApi {
+    private static final long DEFAULT_FAKE_RUN_BLOCK_TIMEOUT_MS = 10000L;
 
     // 绔欑偣绾ч攣锛氭瘡涓珯鐐圭嫭绔嬩竴鎶婇攣锛屾彁鍗囧苟鍙戞�ц兘
     private final Map<Integer, ReentrantLock> stationLocks = new ConcurrentHashMap<>();
@@ -128,6 +129,9 @@
             boolean initialized = false;
             // 涓婁竴姝ユ墽琛屾椂闂达紙鐢ㄤ簬鍫靛妫�娴嬶級
             long stepExecuteTime = System.currentTimeMillis();
+            long runBlockTimeoutMs = getFakeRunBlockTimeoutMs();
+            // 浠呭湪姣忔鍒拌揪鐩爣鏃舵墽琛屼竴娆″埌浣嶅鐞嗭紝閬垮厤閲嶅鐢熸垚鏉$爜
+            boolean arrivalHandled = false;
 
             while (true) {
                 if (Thread.currentThread().isInterrupted()) {
@@ -143,30 +147,35 @@
                 StationCommand command = commandQueue.poll(100, TimeUnit.MILLISECONDS);
                 if (command != null) {
                     taskLastUpdateTime.put(taskNo, System.currentTimeMillis());
+                    List<Integer> newPath = command.getNavigatePath();
+                    Integer lastInQueue = getLastInQueue(pendingPathQueue);
+                    int startIndex = getPathAppendStartIndex(newPath, currentStationId, lastInQueue);
 
-                    // 棣栨鎺ユ敹鍛戒护鏃跺垵濮嬪寲
-                    if (finalTargetStationId == null) {
-                        finalTargetStationId = command.getTargetStaNo();
-                        if (checkTaskNoInArea(taskNo)) {
-                            generateBarcode = true;
+                    if (newPath != null && !newPath.isEmpty() && startIndex < 0) {
+                        News.info("[WCS Debug] 浠诲姟{}蹇界暐鏃犳硶琛旀帴鐨勬棫璺緞娈�: {}, 褰撳墠浣嶇疆: {}, 闃熷垪灏�: {}", taskNo,
+                                newPath, currentStationId, lastInQueue);
+                        continue;
+                    }
+
+                    // 姣忔鎺ユ敹鍛戒护閮藉埛鏂扮洰鏍囷紝閬垮厤娌跨敤鏃х洰鏍囧鑷寸姸鎬佹姈鍔�
+                    Integer commandTargetStationId = command.getTargetStaNo();
+                    if (commandTargetStationId != null) {
+                        if (!commandTargetStationId.equals(finalTargetStationId)) {
+                            arrivalHandled = false;
+                            News.info("[WCS Debug] 浠诲姟{}鍒囨崲鐩爣: {} -> {}", taskNo, finalTargetStationId,
+                                    commandTargetStationId);
                         }
+                        finalTargetStationId = commandTargetStationId;
+                        // 褰撳墠绔欑偣鍏堝悓姝ユ渶鏂扮洰鏍囷紝閬垮厤涓婂眰鍦ㄧ獥鍙f湡閲嶅涓嬪彂鍚屼竴璺緞
+                        syncCurrentStationTarget(taskNo, currentStationId, finalTargetStationId);
+                    }
+
+                    if (!generateBarcode && checkTaskNoInArea(taskNo)) {
+                        generateBarcode = true;
                     }
 
                     // 灏嗘柊璺緞杩藉姞鍒板緟鎵ц闃熷垪
-                    List<Integer> newPath = command.getNavigatePath();
                     if (newPath != null && !newPath.isEmpty()) {
-                        // 鑾峰彇闃熷垪涓渶鍚庝竴涓珯鐐癸紙鐢ㄤ簬琛旀帴鐐瑰幓閲嶏級
-                        Integer lastInQueue = getLastInQueue(pendingPathQueue);
-                        if (lastInQueue == null) {
-                            lastInQueue = currentStationId;
-                        }
-
-                        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));
                         }
@@ -232,6 +241,7 @@
                             currentStationId = nextStationId;
                             pendingPathQueue.poll();
                             stepExecuteTime = System.currentTimeMillis();
+                            arrivalHandled = false;
                             News.info("[WCS Debug] 浠诲姟{}绉诲姩鍒扮珯鐐�: {}, 鍓╀綑闃熷垪: {}", taskNo, currentStationId,
                                     pendingPathQueue.size());
                             sleep(1000); // 妯℃嫙绉诲姩鑰楁椂
@@ -240,7 +250,8 @@
                             if (!checkTaskNoInArea(taskNo)) {
                                 boolean fakeAllowCheckBlock = getFakeAllowCheckBlock();
 
-                                if (fakeAllowCheckBlock && System.currentTimeMillis() - stepExecuteTime > 10000) {
+                                if (fakeAllowCheckBlock
+                                        && System.currentTimeMillis() - stepExecuteTime > runBlockTimeoutMs) {
                                     // 璁ゅ畾鍫靛
                                     boolean result = runBlockStation(taskNo, currentStationId, currentDeviceNo, taskNo,
                                             currentStationId);
@@ -261,18 +272,20 @@
                     // 璺緞闃熷垪涓虹┖锛岀瓑寰呮柊鐨勫垎娈靛懡浠�
                     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);
+                        // 宸插埌杈惧綋鍓嶇洰鏍囧悗缁х画绛夊緟涓嬩竴鏉″垎娈靛懡浠わ紝閬垮厤鎺掑簭/缁曞湀鍦烘櫙鍚炴帀鍚庣画鍛戒护
+                        if (!arrivalHandled) {
+                            if (generateBarcode) {
+                                Integer targetDeviceNo = getDeviceNoByStationId(finalTargetStationId);
+                                if (targetDeviceNo != null) {
+                                    generateStationBarcode(taskNo, finalTargetStationId, targetDeviceNo);
+                                    News.info("[WCS Debug] 浠诲姟{}鍒拌揪鐩爣{}骞剁敓鎴愭潯鐮�", taskNo, finalTargetStationId);
+                                }
                             }
+                            arrivalHandled = true;
                         }
-                        break;
                     }
 
-                    // 鏈埌杈炬渶缁堢洰鏍囷紝绛夊緟鏂扮殑鍒嗘鍛戒护
+                    // 缁х画绛夊緟鏂扮殑鍒嗘鍛戒护
                     Long lastTime = taskLastUpdateTime.get(taskNo);
                     if (lastTime != null && System.currentTimeMillis() - lastTime > 30000) {
                         // 瓒呮椂锛�30绉掑唴娌℃湁鏀跺埌鏂板垎娈靛懡浠�
@@ -305,6 +318,72 @@
     }
 
     /**
+     * 璁$畻鏂拌矾寰勫湪闃熷垪涓殑杩藉姞璧风偣锛岄伩鍏嶉噸澶嶄笅鍙戝鑷磋矾寰勬潵鍥炶烦
+     */
+    private int getPathAppendStartIndex(List<Integer> newPath, Integer currentStationId, Integer lastInQueue) {
+        if (newPath == null || newPath.isEmpty()) {
+            return 0;
+        }
+
+        if (lastInQueue != null) {
+            int idx = newPath.lastIndexOf(lastInQueue);
+            if (idx >= 0) {
+                return idx + 1;
+            }
+        }
+
+        if (currentStationId != null) {
+            int idx = newPath.lastIndexOf(currentStationId);
+            if (idx >= 0) {
+                return idx + 1;
+            }
+            return -1;
+        }
+
+        if (lastInQueue != null) {
+            return -1;
+        }
+
+        return 0;
+    }
+
+    /**
+     * 鍛戒护鍒氬埌杈炬椂鍚屾褰撳墠绔欑偣鐩爣锛岄檷浣庝笂灞傞噸澶嶅彂鍚屼竴璺緞鐨勬鐜�
+     */
+    private void syncCurrentStationTarget(Integer taskNo, Integer currentStationId, Integer targetStationId) {
+        if (currentStationId == null || targetStationId == null) {
+            return;
+        }
+        Integer currentDeviceNo = getDeviceNoByStationId(currentStationId);
+        if (currentDeviceNo == null) {
+            return;
+        }
+
+        lockStations(currentStationId);
+        try {
+            List<ZyStationStatusEntity> statusList = deviceStatusMap.get(currentDeviceNo);
+            if (statusList == null) {
+                return;
+            }
+
+            ZyStationStatusEntity currentStatus = statusList.stream()
+                    .filter(item -> item.getStationId().equals(currentStationId)).findFirst().orElse(null);
+            if (currentStatus == null) {
+                return;
+            }
+
+            if (currentStatus.getTaskNo() != null && currentStatus.getTaskNo() > 0
+                    && !currentStatus.getTaskNo().equals(taskNo) && currentStatus.isLoading()) {
+                return;
+            }
+
+            updateStationDataInternal(currentStationId, currentDeviceNo, taskNo, targetStationId, null, null, null);
+        } finally {
+            unlockStations(currentStationId);
+        }
+    }
+
+    /**
      * 鑾峰彇鏄惁鍏佽妫�鏌ュ牭濉炵殑閰嶇疆
      */
     private boolean getFakeAllowCheckBlock() {
@@ -318,6 +397,25 @@
             }
         }
         return fakeAllowCheckBlock;
+    }
+
+    private long getFakeRunBlockTimeoutMs() {
+        long timeoutMs = DEFAULT_FAKE_RUN_BLOCK_TIMEOUT_MS;
+        Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
+        if (systemConfigMapObj instanceof Map) {
+            Map<?, ?> systemConfigMap = (Map<?, ?>) systemConfigMapObj;
+            Object value = systemConfigMap.get("fakeRunBlockTimeoutMs");
+            if (value != null) {
+                try {
+                    long parsed = Long.parseLong(String.valueOf(value).trim());
+                    if (parsed > 0) {
+                        timeoutMs = parsed;
+                    }
+                } catch (Exception ignore) {
+                }
+            }
+        }
+        return timeoutMs;
     }
 
     @Override
@@ -347,6 +445,10 @@
         }
 
         if (command.getCommandType() == StationCommandType.WRITE_INFO) {
+            if (command.getBarcode() != null) {
+                updateStationBarcode(deviceNo, stationId, command.getBarcode());
+                return;
+            }
             if (taskNo == 9998 && targetStationId == 0) {
                 // 鐢熸垚鍑哄簱绔欑偣浠跨湡鏁版嵁
                 generateFakeOutStationData(deviceNo, stationId);
@@ -402,6 +504,23 @@
         }
     }
 
+    private void updateStationBarcode(Integer deviceNo, Integer stationId, String barcode) {
+        List<ZyStationStatusEntity> statusList = deviceStatusMap.get(deviceNo);
+        if (statusList == null) {
+            return;
+        }
+
+        ZyStationStatusEntity status = statusList.stream()
+                .filter(item -> item.getStationId().equals(stationId)).findFirst().orElse(null);
+        if (status == null) {
+            return;
+        }
+
+        synchronized (status) {
+            status.setBarcode(barcode);
+        }
+    }
+
     // segmentedPathCommand 鏂规硶宸插垹闄わ紝鍔熻兘宸叉暣鍚堝埌 runTaskLoop
 
     private Integer getDeviceNoByStationId(Integer stationId) {

--
Gitblit v1.9.1