From d16c9af7bb89a7e3abb4071003c63b3a6f6b9e4b Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期二, 07 四月 2026 14:45:27 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java |  210 ++++++++++++++++++++++++++++++----------------------
 1 files changed, 122 insertions(+), 88 deletions(-)

diff --git a/src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java b/src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java
index e33d401..9daff00 100644
--- a/src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java
+++ b/src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java
@@ -2,14 +2,13 @@
 
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.zy.asrs.domain.enums.NotifyMsgType;
+import com.zy.asrs.entity.BasCrnp;
 import com.zy.asrs.entity.BasDevp;
 import com.zy.asrs.entity.BasStation;
 import com.zy.asrs.entity.WrkMast;
-import com.zy.asrs.service.BasDevpService;
-import com.zy.asrs.service.BasStationService;
-import com.zy.asrs.service.WrkAnalysisService;
-import com.zy.asrs.service.WrkMastService;
+import com.zy.asrs.service.*;
 import com.zy.asrs.utils.NotifyUtils;
 import com.zy.common.entity.FindCrnNoResult;
 import com.zy.common.service.CommonService;
@@ -21,6 +20,7 @@
 import com.zy.core.enums.RedisKeyType;
 import com.zy.core.enums.SlaveType;
 import com.zy.core.enums.StationCommandType;
+import com.zy.core.enums.WrkIoType;
 import com.zy.core.enums.WrkStsType;
 import com.zy.core.model.StationObjModel;
 import com.zy.core.model.command.StationCommand;
@@ -62,78 +62,36 @@
     private StationCommandDispatcher stationCommandDispatcher;
     @Autowired
     private StationDispatchLoadSupport stationDispatchLoadSupport;
+    @Autowired
+    private BasCrnpService basCrnpService;
 
-    public void stationInExecute() {
+    public void stationOutExecuteFinish(StationObjModel stationObjModel) {
         try {
-            List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
-            for (BasDevp basDevp : basDevps) {
-                List<StationObjModel> stationList = basDevp.getBarcodeStationList$();
-                for (StationObjModel entity : stationList) {
-                    stationInExecute(basDevp, entity);
-                }
+            if (stationObjModel == null) {
+                return;
             }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    public void stationOutExecuteFinish() {
-        try {
-            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN.sts));
-            for (WrkMast wrkMast : wrkMasts) {
-                stationOutExecuteFinish(wrkMast);
+            Integer stationId = stationObjModel.getStationId();
+            if (stationId == null) {
+                return;
             }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
 
-    public void stationOutExecuteFinish(WrkMast wrkMast) {
-        try {
+            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
+            if (stationThread == null) {
+                return;
+            }
+            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
+            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(stationId);
+            WrkMast wrkMast = findCurrentStationRunTask(stationProtocol);
+            boolean matchedByRecentArrival = false;
+            if (wrkMast == null) {
+                wrkMast = findRecentArrivalStationRunTask(stationThread, stationId);
+                matchedByRecentArrival = wrkMast != null;
+            }
             if (wrkMast == null) {
                 return;
             }
-            Integer wrkNo = wrkMast.getWrkNo();
-            Integer targetStaNo = wrkMast.getStaNo();
-            if (wrkNo == null || targetStaNo == null) {
-                return;
-            }
 
-            boolean complete = false;
-            Integer targetDeviceNo = null;
-            StationThread stationThread = null;
-            BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", targetStaNo));
-            if (basStation != null) {
-                targetDeviceNo = basStation.getDeviceNo();
-                stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
-                if (stationThread != null) {
-                    Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
-                    StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(basStation.getStationId());
-                    boolean arrived = stationProtocol != null && wrkNo.equals(stationProtocol.getTaskNo());
-                    if (arrived || stationThread.hasRecentArrival(basStation.getStationId(), wrkNo)) {
-                        complete = true;
-                        if (!arrived) {
-                            News.info("杈撻�佺珯鐐瑰嚭搴撳埌杈惧垽瀹氫娇鐢ㄦ渶杩戝埌绔欒ˉ鍋匡紝宸ヤ綔鍙�={}锛岀洰鏍囩珯={}", wrkNo, targetStaNo);
-                        }
-                    }
-                }
-            }
-
-            if (complete) {
-                attemptClearTaskPath(stationThread, wrkNo);
-                completeStationRunTask(wrkMast, targetDeviceNo);
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    public void checkTaskToComplete() {
-        try {
-            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN_COMPLETE.sts));
-            for (WrkMast wrkMast : wrkMasts) {
-                checkTaskToComplete(wrkMast);
-            }
+            completeOutboundStationRun(stationObjModel, stationThread, wrkMast, matchedByRecentArrival);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -175,13 +133,87 @@
                 wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts);
                 wrkMast.setIoTime(new Date());
                 wrkMastService.updateById(wrkMast);
+                clearOutboundDispatchCache(wrkMast);
             }
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 
-    private void attemptClearTaskPath(StationThread stationThread, Integer taskNo) {
+    private void clearOutboundDispatchCache(WrkMast wrkMast) {
+        if (wrkMast == null || wrkMast.getWrkNo() == null) {
+            return;
+        }
+        redisUtil.del(RedisKeyType.DUAL_CRN_OUT_TASK_STATION_INFO.key + wrkMast.getWrkNo());
+    }
+
+    private WrkMast findCurrentStationRunTask(StationProtocol stationProtocol) {
+        if (stationProtocol == null || stationProtocol.getTaskNo() <= 0) {
+            return null;
+        }
+        WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
+        if (wrkMast == null || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)) {
+            return null;
+        }
+        return wrkMast;
+    }
+
+    private WrkMast findRecentArrivalStationRunTask(StationThread stationThread, Integer stationId) {
+        if (stationThread == null || stationId == null || stationId <= 0) {
+            return null;
+        }
+        List<WrkMast> stationRunTasks = wrkMastService.list(new QueryWrapper<WrkMast>()
+                .eq("io_type", WrkIoType.OUT.id)
+                .eq("sta_no", stationId)
+                .eq("wrk_sts", WrkStsType.STATION_RUN.sts)
+                .orderByAsc("io_time", "wrk_no"));
+        for (WrkMast candidate : stationRunTasks) {
+            Integer wrkNo = candidate == null ? null : candidate.getWrkNo();
+            if (wrkNo == null || wrkNo <= 0) {
+                continue;
+            }
+            if (stationThread.hasRecentArrival(stationId, wrkNo)) {
+                return candidate;
+            }
+        }
+        return null;
+    }
+
+    private void completeOutboundStationRun(StationObjModel stationObjModel,
+                                            StationThread stationThread,
+                                            WrkMast wrkMast,
+                                            boolean matchedByRecentArrival) {
+        if (stationObjModel == null || stationThread == null || wrkMast == null || wrkMast.getWrkNo() == null) {
+            return;
+        }
+        Date now = new Date();
+        boolean updated = wrkMastService.update(null, new UpdateWrapper<WrkMast>()
+                .set("wrk_sts", WrkStsType.STATION_RUN_COMPLETE.sts)
+                .set("io_time", now)
+                .set("modi_time", now)
+                .eq("wrk_no", wrkMast.getWrkNo())
+                .eq("wrk_sts", WrkStsType.STATION_RUN.sts));
+        if (!updated) {
+            return;
+        }
+
+        wrkMast.setWrkSts(WrkStsType.STATION_RUN_COMPLETE.sts);
+        wrkMast.setIoTime(now);
+        wrkMast.setModiTime(now);
+        if (stationMoveCoordinator != null) {
+            stationMoveCoordinator.finishSession(wrkMast.getWrkNo());
+        }
+        wrkAnalysisService.markOutboundStationComplete(wrkMast, now);
+        notifyUtils.notify(String.valueOf(SlaveType.Devp), stationObjModel.getDeviceNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.STATION_OUT_TASK_RUN_COMPLETE, null);
+        redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkMast.getWrkNo(), "lock", 60);
+        clearOutboundDispatchCache(wrkMast);
+        attemptClearTaskPath(stationThread, wrkMast.getWrkNo());
+        if (matchedByRecentArrival) {
+            News.info("杈撻�佺珯鍑哄簱瀹屾垚琛ュ伩鍛戒腑鏈�杩戝埌绔欑紦瀛橈紝鐩爣绔�={}锛屽伐浣滃彿={}", stationObjModel.getStationId(), wrkMast.getWrkNo());
+        }
+    }
+
+    public void attemptClearTaskPath(StationThread stationThread, Integer taskNo) {
         if (stationThread == null || taskNo == null || taskNo <= 0) {
             return;
         }
@@ -193,25 +225,6 @@
         } catch (Exception e) {
             News.error("杈撻�佺珯鐐逛换鍔¤繍琛屽畬鎴愬悗娓呯悊娈嬬暀璺緞寮傚父锛屽伐浣滃彿={}", taskNo, e);
         }
-    }
-
-    private void completeStationRunTask(WrkMast wrkMast, Integer deviceNo) {
-        if (wrkMast == null || wrkMast.getWrkNo() == null) {
-            return;
-        }
-        if (stationMoveCoordinator != null) {
-            stationMoveCoordinator.finishSession(wrkMast.getWrkNo());
-        }
-        Date now = new Date();
-        wrkMast.setWrkSts(WrkStsType.STATION_RUN_COMPLETE.sts);
-        wrkMast.setIoTime(now);
-        wrkMast.setModiTime(now);
-        wrkMastService.updateById(wrkMast);
-        wrkAnalysisService.markOutboundStationComplete(wrkMast, now);
-        if (deviceNo != null) {
-            notifyUtils.notify(String.valueOf(SlaveType.Devp), deviceNo, String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.STATION_OUT_TASK_RUN_COMPLETE, null);
-        }
-        redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkMast.getWrkNo(), "lock", 60);
     }
 
     private boolean offerDevpCommandWithDedup(Integer deviceNo, StationCommand command, String scene) {
@@ -247,7 +260,9 @@
 
         if (!stationProtocol.isAutoing()
                 || !stationProtocol.isLoading()
-                || stationProtocol.getTaskNo() <= 0) {
+                || stationProtocol.getTaskNo() <= 0
+                || !stationProtocol.isInEnable()
+        ) {
             return;
         }
 
@@ -256,6 +271,25 @@
             return;
         }
 
+        Integer crnNo = wrkMast.getCrnNo();
+        BasCrnp basCrnp = basCrnpService.getOne(new QueryWrapper<BasCrnp>().eq("crn_no", crnNo));
+        if (basCrnp == null) {
+            News.taskInfo(wrkMast.getWrkNo(), "{}宸ヤ綔,鏈壘鍒板爢鍨涙満鏁版嵁", wrkMast.getWrkNo());
+            return;
+        }
+
+        int maxInTask = 3;
+        if (basCrnp.getMaxInTask() != null) {
+            maxInTask = basCrnp.getMaxInTask();
+        }
+
+        long count = wrkMastService.count(new QueryWrapper<WrkMast>().eq("crn_no", crnNo).eq("wrk_sts", WrkStsType.INBOUND_STATION_RUN.sts));
+        if(count >=  maxInTask) {
+            News.taskInfo(wrkMast.getWrkNo(), "{}宸ヤ綔,鍫嗗灈鏈哄埌杈句换鍔′笂闄愶紝绋嶅悗鎵ц", wrkMast.getWrkNo());
+            stationProtocol.setSystemWarning(wrkMast.getWrkNo() + "宸ヤ綔," + crnNo + "鍙峰爢鍨涙満鍒拌揪浠诲姟涓婇檺锛岀◢鍚庢墽琛�");
+            return;
+        }
+
         String locNo = wrkMast.getLocNo();
         FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
         if (findCrnNoResult == null) {

--
Gitblit v1.9.1