From 715a556c62dddf22e8a6a2154473977915fb2e80 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期一, 02 三月 2026 17:05:03 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java |   96 ++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 80 insertions(+), 16 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..ccf3230 100644
--- a/src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java
+++ b/src/main/java/com/zy/core/network/fake/ZyStationFakeSegConnect.java
@@ -144,12 +144,20 @@
                 if (command != null) {
                     taskLastUpdateTime.put(taskNo, System.currentTimeMillis());
 
-                    // 棣栨鎺ユ敹鍛戒护鏃跺垵濮嬪寲
-                    if (finalTargetStationId == null) {
-                        finalTargetStationId = command.getTargetStaNo();
-                        if (checkTaskNoInArea(taskNo)) {
-                            generateBarcode = true;
+                    // 姣忔鎺ユ敹鍛戒护閮藉埛鏂扮洰鏍囷紝閬垮厤娌跨敤鏃х洰鏍囧鑷寸姸鎬佹姈鍔�
+                    Integer commandTargetStationId = command.getTargetStaNo();
+                    if (commandTargetStationId != null) {
+                        if (!commandTargetStationId.equals(finalTargetStationId)) {
+                            News.info("[WCS Debug] 浠诲姟{}鍒囨崲鐩爣: {} -> {}", taskNo, finalTargetStationId,
+                                    commandTargetStationId);
                         }
+                        finalTargetStationId = commandTargetStationId;
+                        // 褰撳墠绔欑偣鍏堝悓姝ユ渶鏂扮洰鏍囷紝閬垮厤涓婂眰鍦ㄧ獥鍙f湡閲嶅涓嬪彂鍚屼竴璺緞
+                        syncCurrentStationTarget(taskNo, currentStationId, finalTargetStationId);
+                    }
+
+                    if (!generateBarcode && checkTaskNoInArea(taskNo)) {
+                        generateBarcode = true;
                     }
 
                     // 灏嗘柊璺緞杩藉姞鍒板緟鎵ц闃熷垪
@@ -157,15 +165,7 @@
                     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;
-                        }
+                        int startIndex = getPathAppendStartIndex(newPath, currentStationId, lastInQueue);
 
                         for (int i = startIndex; i < newPath.size(); i++) {
                             pendingPathQueue.offer(newPath.get(i));
@@ -261,7 +261,7 @@
                     // 璺緞闃熷垪涓虹┖锛岀瓑寰呮柊鐨勫垎娈靛懡浠�
                     if (currentStationId != null && finalTargetStationId != null
                             && currentStationId.equals(finalTargetStationId)) {
-                        // 宸插埌杈炬渶缁堢洰鏍囷紝姝e父缁撴潫
+                        // 宸插埌杈剧洰鏍囷細绔嬪嵆娓呯┖褰撳墠闃熷垪骞剁粨鏉熸湰杞墽琛岋紝鍚庣画鏂板懡浠ら噸鏂板垱寤烘墽琛岀嚎绋�
                         if (generateBarcode) {
                             Integer targetDeviceNo = getDeviceNoByStationId(finalTargetStationId);
                             if (targetDeviceNo != null) {
@@ -269,10 +269,13 @@
                                 News.info("[WCS Debug] 浠诲姟{}鍒拌揪鐩爣{}骞剁敓鎴愭潯鐮�", taskNo, finalTargetStationId);
                             }
                         }
+                        commandQueue.clear();
+                        pendingPathQueue.clear();
+                        News.info("[WCS Debug] 浠诲姟{}鍒拌揪鐩爣鍚庢竻绌洪槦鍒楀苟缁撴潫锛岀瓑寰呭悗缁柊鍛戒护閲嶅惎", taskNo);
                         break;
                     }
 
-                    // 鏈埌杈炬渶缁堢洰鏍囷紝绛夊緟鏂扮殑鍒嗘鍛戒护
+                    // 缁х画绛夊緟鏂扮殑鍒嗘鍛戒护
                     Long lastTime = taskLastUpdateTime.get(taskNo);
                     if (lastTime != null && System.currentTimeMillis() - lastTime > 30000) {
                         // 瓒呮椂锛�30绉掑唴娌℃湁鏀跺埌鏂板垎娈靛懡浠�
@@ -305,6 +308,67 @@
     }
 
     /**
+     * 璁$畻鏂拌矾寰勫湪闃熷垪涓殑杩藉姞璧风偣锛岄伩鍏嶉噸澶嶄笅鍙戝鑷磋矾寰勬潵鍥炶烦
+     */
+    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 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() {

--
Gitblit v1.9.1