From 9c5b9144ed8ed3190a21c4cb66b80e1ed9b7c954 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期五, 03 四月 2026 15:19:06 +0800
Subject: [PATCH] #预调度优化

---
 src/main/java/com/zy/core/utils/CrnOperateProcessUtils.java |  552 ++++++++++++++++++++++++++++++------------------------
 1 files changed, 308 insertions(+), 244 deletions(-)

diff --git a/src/main/java/com/zy/core/utils/CrnOperateProcessUtils.java b/src/main/java/com/zy/core/utils/CrnOperateProcessUtils.java
index 614f005..4be2698 100644
--- a/src/main/java/com/zy/core/utils/CrnOperateProcessUtils.java
+++ b/src/main/java/com/zy/core/utils/CrnOperateProcessUtils.java
@@ -31,6 +31,8 @@
 import com.zy.core.model.command.CrnCommand;
 import com.zy.core.model.protocol.CrnProtocol;
 import com.zy.core.model.protocol.StationProtocol;
+import com.zy.core.task.MainProcessLane;
+import com.zy.core.task.MainProcessTaskSubmitter;
 import com.zy.core.thread.CrnThread;
 import com.zy.core.thread.StationThread;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -69,100 +71,105 @@
     private StationOperateProcessUtils stationOperateProcessUtils;
     @Autowired
     private WrkAnalysisService wrkAnalysisService;
+    @Autowired
+    private MainProcessTaskSubmitter mainProcessTaskSubmitter;
 
     private static final String CRN_OUT_REQUIRE_STATION_OUT_ENABLE_CONFIG = "crnOutRequireStationOutEnable";
 
     public synchronized void crnIoExecute() {
+        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
+        for (BasCrnp basCrnp : basCrnps) {
+            crnIoExecute(basCrnp);
+        }
+    }
+
+    public void crnIoExecute(BasCrnp basCrnp) {
+        if (basCrnp == null || basCrnp.getCrnNo() == null) {
+            return;
+        }
         Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
         if (systemConfigMapObj != null) {
             HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
             if (systemConfigMap.get("crnRunMethod").equals("solver")) {
-                plannerExecute();
+                plannerExecute(basCrnp);
             }else {
-                crnIoExecuteNormal();
+                crnIoExecuteNormal(basCrnp);
             }
         }
     }
 
     //鍏ュ嚭搴�  ===>>  鍫嗗灈鏈哄叆鍑哄簱浣滀笟涓嬪彂
-    public synchronized void crnIoExecuteNormal() {
-        Set<Integer> crnMoveBlockedCrnNos = executeCrnMoveTask();
-
-        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
-        Map<Integer, BasCrnp> dispatchCrnMap = new HashMap<>();
-        Map<Integer, CrnThread> dispatchThreadMap = new HashMap<>();
-        Map<Integer, CrnProtocol> dispatchProtocolMap = new HashMap<>();
-        for (BasCrnp basCrnp : basCrnps) {
-            if (basCrnp == null || basCrnp.getCrnNo() == null || crnMoveBlockedCrnNos.contains(basCrnp.getCrnNo())) {
-                continue;
-            }
-            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo());
-            if (crnThread == null) {
-                continue;
-            }
-
-            CrnProtocol crnProtocol = crnThread.getStatus();
-            if (crnProtocol == null) {
-                continue;
-            }
-
-            long runningCount = wrkMastService.count(new QueryWrapper<WrkMast>()
-                    .eq("crn_no", basCrnp.getCrnNo())
-                    .in("wrk_sts", WrkStsType.INBOUND_RUN.sts, WrkStsType.OUTBOUND_RUN.sts, WrkStsType.LOC_MOVE_RUN.sts, WrkStsType.CRN_MOVE_RUN.sts));
-            if (runningCount > 0) {
-                continue;
-            }
-
-            // 鍙湁褰撳爢鍨涙満绌洪棽 骞朵笖 鏃犱换鍔℃椂鎵嶇户缁墽琛�
-            if (crnProtocol.getMode() == CrnModeType.AUTO.id
-                    && crnProtocol.getTaskNo() == 0
-                    && crnProtocol.getStatus() == CrnStatusType.IDLE.id
-                    && crnProtocol.getLoaded() == 0
-                    && crnProtocol.getForkPos() == 0
-                    && crnProtocol.getAlarm() == 0
-            ) {
-                Object clearLock = redisUtil.get(RedisKeyType.CLEAR_CRN_TASK_LIMIT.key + basCrnp.getCrnNo());
-                if (clearLock != null) {
-                    continue;
-                }
-                dispatchCrnMap.put(basCrnp.getCrnNo(), basCrnp);
-                dispatchThreadMap.put(basCrnp.getCrnNo(), crnThread);
-                dispatchProtocolMap.put(basCrnp.getCrnNo(), crnProtocol);
-            }
-        }
-
-        if (dispatchCrnMap.isEmpty()) {
+    private void crnIoExecuteNormal(BasCrnp currentCrn) {
+        if (currentCrn == null || currentCrn.getCrnNo() == null) {
             return;
         }
 
+        Integer crnNo = currentCrn.getCrnNo();
+        CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crnNo);
+        if (crnThread == null) {
+            return;
+        }
+
+        CrnProtocol crnProtocol = crnThread.getStatus();
+        if (crnProtocol == null) {
+            return;
+        }
+
+        long runningCount = wrkMastService.count(new QueryWrapper<WrkMast>()
+                .eq("crn_no", crnNo)
+                .in("wrk_sts",
+                        WrkStsType.INBOUND_RUN.sts
+                        , WrkStsType.OUTBOUND_RUN.sts
+                        , WrkStsType.LOC_MOVE_RUN.sts
+                        , WrkStsType.CRN_MOVE_RUN.sts
+                ));
+        if (runningCount > 0) {
+            return;
+        }
+
+        // 鍙湁褰撳爢鍨涙満绌洪棽 骞朵笖 鏃犱换鍔℃椂鎵嶇户缁墽琛�
+        if (crnProtocol.getMode() != CrnModeType.AUTO.id
+                || crnProtocol.getTaskNo() != 0
+                || crnProtocol.getStatus() != CrnStatusType.IDLE.id
+                || crnProtocol.getLoaded() != 0
+                || crnProtocol.getForkPos() != 0
+                || crnProtocol.getAlarm() != 0) {
+            return;
+        }
+
+        Object clearLock = redisUtil.get(RedisKeyType.CLEAR_CRN_TASK_LIMIT.key + crnNo);
+        if (clearLock != null) {
+            return;
+        }
+
+        //鎵ц绉诲姩浠诲姟
+        boolean executed = executeCrnMoveTask(crnThread, crnProtocol);
+        if (executed) {
+            return;
+        }
+
+        String lastIo = resolveCrnLastIo(crnProtocol);
+
         List<WrkMast> taskQueue = wrkMastService.list(new QueryWrapper<WrkMast>()
-                .in("crn_no", new ArrayList<>(dispatchCrnMap.keySet()))
+                .eq("crn_no", crnNo)
                 .in("wrk_sts",
                         WrkStsType.INBOUND_STATION_RUN_COMPLETE.sts,
                         WrkStsType.NEW_OUTBOUND.sts,
                         WrkStsType.NEW_LOC_MOVE.sts));
-        taskQueue.sort(Comparator
-                .comparingInt(this::resolveBatchOutboundRank)
+        taskQueue.sort(Comparator.comparingInt((WrkMast wrkMast) -> resolveTaskTypeRank(wrkMast, lastIo))
+                .thenComparingInt(this::resolveBatchOutboundRank)
                 .thenComparingInt(this::resolveBatchSeqOrder)
                 .thenComparingDouble(this::resolveTaskIoPri)
                 .thenComparingLong(this::resolveTaskQueueTime)
                 .thenComparingInt(this::resolveTaskQueueNo));
 
         for (WrkMast wrkMast : taskQueue) {
-            if (wrkMast == null || wrkMast.getCrnNo() == null) {
-                continue;
-            }
-
-            Integer crnNo = wrkMast.getCrnNo();
-            BasCrnp basCrnp = dispatchCrnMap.get(crnNo);
-            CrnThread crnThread = dispatchThreadMap.get(crnNo);
-            CrnProtocol crnProtocol = dispatchProtocolMap.get(crnNo);
-            if (basCrnp == null || crnThread == null || crnProtocol == null) {
+            if (wrkMast == null || wrkMast.getCrnNo() == null || !Objects.equals(wrkMast.getCrnNo(), crnNo)) {
                 continue;
             }
 
             if (wrkMast.getWrkSts() != null && wrkMast.getWrkSts() == WrkStsType.INBOUND_STATION_RUN_COMPLETE.sts) {
-                boolean result = this.crnExecuteInPlanner(basCrnp, crnThread, wrkMast);
+                boolean result = this.crnExecuteInPlanner(currentCrn, crnThread, wrkMast);
                 if (result) {
                     crnProtocol.setLastIo("O");
                     return;
@@ -171,7 +178,7 @@
             }
 
             if (wrkMast.getWrkSts() != null && wrkMast.getWrkSts() == WrkStsType.NEW_OUTBOUND.sts) {
-                boolean result = this.crnExecuteOutPlanner(basCrnp, crnThread, wrkMast);
+                boolean result = this.crnExecuteOutPlanner(currentCrn, crnThread, wrkMast);
                 if (result) {
                     crnProtocol.setLastIo("I");
                     return;
@@ -180,7 +187,7 @@
             }
 
             if (wrkMast.getWrkSts() != null && wrkMast.getWrkSts() == WrkStsType.NEW_LOC_MOVE.sts) {
-                boolean transfer = this.crnExecuteMovePlanner(basCrnp, crnThread, wrkMast);
+                boolean transfer = this.crnExecuteMovePlanner(currentCrn, crnThread, wrkMast);
                 if (transfer) {
                     return;
                 }
@@ -190,6 +197,44 @@
 
     private int resolveBatchOutboundRank(WrkMast wrkMast) {
         return isBatchOutboundTask(wrkMast) ? 0 : 1;
+    }
+
+    private String resolveCrnLastIo(CrnProtocol crnProtocol) {
+        if (crnProtocol == null) {
+            return "I";
+        }
+        if (Cools.isEmpty(crnProtocol.getLastIo())) {
+            return "I";
+        }
+        return crnProtocol.getLastIo();
+    }
+
+    private int resolveTaskTypeRank(WrkMast wrkMast, String lastIo) {
+        if (wrkMast == null || wrkMast.getWrkSts() == null) {
+            return Integer.MAX_VALUE;
+        }
+
+        if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_LOC_MOVE.sts)) {
+            return 0;
+        }
+
+        if (Objects.equals("O", lastIo)) {
+            if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.INBOUND_STATION_RUN_COMPLETE.sts)) {
+                return 1;
+            }
+            if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_OUTBOUND.sts)) {
+                return 2;
+            }
+            return 3;
+        }
+
+        if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_OUTBOUND.sts)) {
+            return 1;
+        }
+        if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.INBOUND_STATION_RUN_COMPLETE.sts)) {
+            return 2;
+        }
+        return 3;
     }
 
     private int resolveBatchSeqOrder(WrkMast wrkMast) {
@@ -350,7 +395,7 @@
             return false;
         }
 
-        if (!allowBatchOutboundExecute(wrkMast)) {
+        if (!allowBatchOutboundExecute(wrkMast, true)) {
             return false;
         }
 
@@ -423,13 +468,19 @@
         return false;
     }
 
-    private boolean allowBatchOutboundExecute(WrkMast wrkMast) {
+    public boolean canOutboundTaskExecuteInCurrentBatchWindow(WrkMast wrkMast) {
+        return allowBatchOutboundExecute(wrkMast, false);
+    }
+
+    private boolean allowBatchOutboundExecute(WrkMast wrkMast, boolean logBlockedReason) {
         if (wrkMast == null || Cools.isEmpty(wrkMast.getBatch())) {
             return true;
         }
 
         if (Cools.isEmpty(wrkMast.getBatchSeq())) {
-            News.taskInfo(wrkMast.getWrkNo(), "鎵规:{} 缂哄皯鎵规搴忓彿锛屾殏涓嶅厑璁稿爢鍨涙満鍑哄簱", wrkMast.getBatch());
+            if (logBlockedReason) {
+                News.taskInfo(wrkMast.getWrkNo(), "鎵规:{} 缂哄皯鎵规搴忓彿锛屾殏涓嶅厑璁稿爢鍨涙満鍑哄簱", wrkMast.getBatch());
+            }
             return false;
         }
 
@@ -444,7 +495,9 @@
         }
 
         if (Cools.isEmpty(firstBatchWrkMast.getBatchSeq())) {
-            News.taskInfo(wrkMast.getWrkNo(), "鎵规:{} 瀛樺湪鏈厤缃壒娆″簭鍙风殑浠诲姟锛屾殏涓嶅厑璁稿爢鍨涙満鍑哄簱", wrkMast.getBatch());
+            if (logBlockedReason) {
+                News.taskInfo(wrkMast.getWrkNo(), "鎵规:{} 瀛樺湪鏈厤缃壒娆″簭鍙风殑浠诲姟锛屾殏涓嶅厑璁稿爢鍨涙満鍑哄簱", wrkMast.getBatch());
+            }
             return false;
         }
 
@@ -452,7 +505,9 @@
         boolean firstBatchTaskExecuted = firstBatchWrkMast.getWrkSts() != null
                 && !firstBatchWrkMast.getWrkSts().equals(WrkStsType.NEW_OUTBOUND.sts);
         if (!currentIsFirstBatchTask && !firstBatchTaskExecuted) {
-            News.taskInfo(wrkMast.getWrkNo(), "鎵规:{} 棣栦釜搴忓彿浠诲姟:{} 灏氭湭鎵ц锛屽綋鍓嶄换鍔℃殏涓嶅厑璁稿嚭搴�", wrkMast.getBatch(), firstBatchWrkMast.getWrkNo());
+            if (logBlockedReason) {
+                News.taskInfo(wrkMast.getWrkNo(), "鎵规:{} 棣栦釜搴忓彿浠诲姟:{} 灏氭湭鎵ц锛屽綋鍓嶄换鍔℃殏涓嶅厑璁稿嚭搴�", wrkMast.getBatch(), firstBatchWrkMast.getWrkNo());
+            }
             return false;
         }
 
@@ -463,19 +518,24 @@
 
         List<WrkMast> unfinishedBatchWrkMasts = listUnfinishedBatchWrkMasts(wrkMast.getBatch());
         if (hasMissingBatchSeq(unfinishedBatchWrkMasts)) {
-            News.taskInfo(wrkMast.getWrkNo(), "鎵规:{} 瀛樺湪鏈厤缃壒娆″簭鍙风殑鏈畬鎴愪换鍔★紝鏆備笉鍏佽鍫嗗灈鏈哄嚭搴�", wrkMast.getBatch());
+            if (logBlockedReason) {
+                News.taskInfo(wrkMast.getWrkNo(), "鎵规:{} 瀛樺湪鏈厤缃壒娆″簭鍙风殑鏈畬鎴愪换鍔★紝鏆備笉鍏佽鍫嗗灈鏈哄嚭搴�", wrkMast.getBatch());
+            }
             return false;
         }
 
         if (!isWithinBatchExecuteWindow(wrkMast, unfinishedBatchWrkMasts, batchRunningLimit)) {
-            Integer windowStartSeq = unfinishedBatchWrkMasts.get(0).getBatchSeq();
-            Integer windowEndSeq = unfinishedBatchWrkMasts.get(Math.min(batchRunningLimit, unfinishedBatchWrkMasts.size()) - 1).getBatchSeq();
-            News.taskInfo(wrkMast.getWrkNo(),
-                    "鎵规:{} 褰撳墠涓ユ牸鎵ц绐楀彛搴忓彿涓篬{}-{}]锛屽綋鍓嶅簭鍙�={}锛屾殏涓嶅厑璁稿爢鍨涙満鍑哄簱",
-                    wrkMast.getBatch(),
-                    windowStartSeq,
-                    windowEndSeq,
-                    wrkMast.getBatchSeq());
+            if (logBlockedReason) {
+                List<Integer> windowBatchSeqList = unfinishedBatchWrkMasts.stream()
+                        .limit(Math.min(batchRunningLimit, unfinishedBatchWrkMasts.size()))
+                        .map(WrkMast::getBatchSeq)
+                        .collect(java.util.stream.Collectors.toList());
+                News.taskInfo(wrkMast.getWrkNo(),
+                        "鎵规:{} 褰撳墠涓ユ牸鎵ц绐楀彛搴忓彿鍒楄〃涓簕}锛屽綋鍓嶅簭鍙�={}锛屾殏涓嶅厑璁稿爢鍨涙満鍑哄簱",
+                        wrkMast.getBatch(),
+                        windowBatchSeqList,
+                        wrkMast.getBatchSeq());
+            }
             return false;
         }
 
@@ -487,7 +547,9 @@
                         WrkStsType.COMPLETE_OUTBOUND.sts,
                         WrkStsType.SETTLE_OUTBOUND.sts));
         if (batchRunningCount >= batchRunningLimit) {
-            News.taskInfo(wrkMast.getWrkNo(), "鎵规:{} 鎵ц涓换鍔℃暟杈惧埌涓婇檺锛屽綋鍓�={}锛屼笂闄�={}", wrkMast.getBatch(), batchRunningCount, batchRunningLimit);
+            if (logBlockedReason) {
+                News.taskInfo(wrkMast.getWrkNo(), "鎵规:{} 鎵ц涓换鍔℃暟杈惧埌涓婇檺锛屽綋鍓�={}锛屼笂闄�={}", wrkMast.getBatch(), batchRunningCount, batchRunningLimit);
+            }
             return false;
         }
 
@@ -611,110 +673,119 @@
     public synchronized void crnIoExecuteFinish() {
         List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
         for (BasCrnp basCrnp : basCrnps) {
-            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo());
-            if(crnThread == null){
-                continue;
+            crnIoExecuteFinish(basCrnp);
+        }
+    }
+
+    public void crnIoExecuteFinish(BasCrnp basCrnp) {
+        if (basCrnp == null || basCrnp.getCrnNo() == null) {
+            return;
+        }
+        CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo());
+        if(crnThread == null){
+            return;
+        }
+
+        CrnProtocol crnProtocol = crnThread.getStatus();
+        if(crnProtocol == null){
+            return;
+        }
+
+        if (crnProtocol.getMode() == CrnModeType.AUTO.id
+                && crnProtocol.getTaskNo() > 0
+                && crnProtocol.getStatus() == CrnStatusType.WAITING.id
+        ) {
+            Object lock = redisUtil.get(RedisKeyType.CRN_IO_EXECUTE_FINISH_LIMIT.key + basCrnp.getCrnNo());
+            if(lock != null){
+                return;
             }
 
-            CrnProtocol crnProtocol = crnThread.getStatus();
-            if(crnProtocol == null){
-                continue;
+            WrkMast wrkMast = wrkMastService.selectByWorkNo(crnProtocol.getTaskNo());
+            if (wrkMast == null) {
+                News.error("鍫嗗灈鏈哄浜庣瓑寰呯‘璁や笖浠诲姟瀹屾垚鐘舵�侊紝浣嗘湭鎵惧埌宸ヤ綔妗c�傚爢鍨涙満鍙�={}锛屽伐浣滃彿={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
+                return;
             }
 
-            if (crnProtocol.getMode() == CrnModeType.AUTO.id
-                    && crnProtocol.getTaskNo() > 0
-                    && crnProtocol.getStatus() == CrnStatusType.WAITING.id
-            ) {
-                Object lock = redisUtil.get(RedisKeyType.CRN_IO_EXECUTE_FINISH_LIMIT.key + basCrnp.getCrnNo());
-                if(lock != null){
-                    continue;
+            Long updateWrkSts = null;
+            Date now = new Date();
+            if(wrkMast.getWrkSts() == WrkStsType.INBOUND_RUN.sts){
+                updateWrkSts = WrkStsType.COMPLETE_INBOUND.sts;
+                notifyUtils.notify(String.valueOf(SlaveType.Crn), crnProtocol.getCrnNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_IN_TASK_COMPLETE, null);
+            }else if(wrkMast.getWrkSts() == WrkStsType.OUTBOUND_RUN.sts){
+                updateWrkSts = WrkStsType.OUTBOUND_RUN_COMPLETE.sts;
+                notifyUtils.notify(String.valueOf(SlaveType.Crn), crnProtocol.getCrnNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_OUT_TASK_COMPLETE, null);
+
+                List<StationObjModel> outStationList = basCrnp.getOutStationList$();
+                if(outStationList.isEmpty()){
+                    News.info("鍫嗗灈鏈�:{} 鍑哄簱绔欑偣鏈缃�", basCrnp.getCrnNo());
+                    return;
                 }
 
-                // 鑾峰彇寰呯‘璁ゅ伐浣滄。
-                WrkMast wrkMast = wrkMastService.selectByWorkNo(crnProtocol.getTaskNo());
-                if (wrkMast == null) {
-                    News.error("鍫嗗灈鏈哄浜庣瓑寰呯‘璁や笖浠诲姟瀹屾垚鐘舵�侊紝浣嗘湭鎵惧埌宸ヤ綔妗c�傚爢鍨涙満鍙�={}锛屽伐浣滃彿={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
-                    continue;
-                }
-
-                Long updateWrkSts = null;
-                Date now = new Date();
-                if(wrkMast.getWrkSts() == WrkStsType.INBOUND_RUN.sts){
-                    updateWrkSts = WrkStsType.COMPLETE_INBOUND.sts;
-                    notifyUtils.notify(String.valueOf(SlaveType.Crn), crnProtocol.getCrnNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_IN_TASK_COMPLETE, null);
-                }else if(wrkMast.getWrkSts() == WrkStsType.OUTBOUND_RUN.sts){
-                    updateWrkSts = WrkStsType.OUTBOUND_RUN_COMPLETE.sts;
-                    notifyUtils.notify(String.valueOf(SlaveType.Crn), crnProtocol.getCrnNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_OUT_TASK_COMPLETE, null);
-
-                    List<StationObjModel> outStationList = basCrnp.getOutStationList$();
-                    if(outStationList.isEmpty()){
-                        News.info("鍫嗗灈鏈�:{} 鍑哄簱绔欑偣鏈缃�", basCrnp.getCrnNo());
-                        return;
+                StationObjModel outStationObjModel = null;
+                for (StationObjModel stationObjModel : outStationList) {
+                    if (stationObjModel.getStationId().equals(wrkMast.getSourceStaNo())) {
+                        outStationObjModel = stationObjModel;
+                        break;
                     }
-
-                    StationObjModel outStationObjModel = null;
-                    for (StationObjModel stationObjModel : outStationList) {
-                        if (stationObjModel.getStationId().equals(wrkMast.getSourceStaNo())) {
-                            outStationObjModel = stationObjModel;
-                            break;
-                        }
-                    }
-                    redisUtil.set(RedisKeyType.CRN_OUT_TASK_COMPLETE_STATION_INFO.key + wrkMast.getWrkNo(), JSON.toJSONString(outStationObjModel, SerializerFeature.DisableCircularReferenceDetect), 60 * 60 * 24);
-                }else if(wrkMast.getWrkSts() == WrkStsType.LOC_MOVE_RUN.sts){
-                    updateWrkSts = WrkStsType.COMPLETE_LOC_MOVE.sts;
-                    notifyUtils.notify(String.valueOf(SlaveType.Crn), crnProtocol.getCrnNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_TRANSFER_TASK_COMPLETE, null);
-                }else if(wrkMast.getWrkSts() == WrkStsType.CRN_MOVE_RUN.sts){
-                    updateWrkSts = WrkStsType.COMPLETE_CRN_MOVE.sts;
-                }else{
-                    News.error("鍫嗗灈鏈哄浜庣瓑寰呯‘璁や笖浠诲姟瀹屾垚鐘舵�侊紝浣嗗伐浣滅姸鎬佸紓甯搞�傚爢鍨涙満鍙�={}锛屽伐浣滃彿={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
-                    continue;
                 }
-
-                wrkMast.setWrkSts(updateWrkSts);
-                wrkMast.setSystemMsg("");
-                wrkMast.setIoTime(now);
-                wrkMast.setModiTime(now);
-                if (wrkMastService.updateById(wrkMast)) {
-                    wrkAnalysisService.markCraneComplete(wrkMast, now, updateWrkSts);
-                    CrnCommand resetCommand = crnThread.getResetCommand(crnProtocol.getTaskNo(), crnProtocol.getCrnNo());
-                    MessageQueue.offer(SlaveType.Crn, crnProtocol.getCrnNo(), new Task(2, resetCommand));
-                    News.info("鍫嗗灈鏈轰换鍔$姸鎬佹洿鏂版垚鍔燂紝鍫嗗灈鏈哄彿={}锛屽伐浣滃彿={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
-                }
-
-                redisUtil.set(RedisKeyType.CRN_IO_EXECUTE_FINISH_LIMIT.key + basCrnp.getCrnNo(), "lock",10);
+                redisUtil.set(RedisKeyType.CRN_OUT_TASK_COMPLETE_STATION_INFO.key + wrkMast.getWrkNo(), JSON.toJSONString(outStationObjModel, SerializerFeature.DisableCircularReferenceDetect), 60 * 60 * 24);
+            }else if(wrkMast.getWrkSts() == WrkStsType.LOC_MOVE_RUN.sts){
+                updateWrkSts = WrkStsType.COMPLETE_LOC_MOVE.sts;
+                notifyUtils.notify(String.valueOf(SlaveType.Crn), crnProtocol.getCrnNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.CRN_TRANSFER_TASK_COMPLETE, null);
+            }else if(wrkMast.getWrkSts() == WrkStsType.CRN_MOVE_RUN.sts){
+                updateWrkSts = WrkStsType.COMPLETE_CRN_MOVE.sts;
+            }else{
+                News.error("鍫嗗灈鏈哄浜庣瓑寰呯‘璁や笖浠诲姟瀹屾垚鐘舵�侊紝浣嗗伐浣滅姸鎬佸紓甯搞�傚爢鍨涙満鍙�={}锛屽伐浣滃彿={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
+                return;
             }
+
+            wrkMast.setWrkSts(updateWrkSts);
+            wrkMast.setSystemMsg("");
+            wrkMast.setIoTime(now);
+            wrkMast.setModiTime(now);
+            if (wrkMastService.updateById(wrkMast)) {
+                wrkAnalysisService.markCraneComplete(wrkMast, now, updateWrkSts);
+                CrnCommand resetCommand = crnThread.getResetCommand(crnProtocol.getTaskNo(), crnProtocol.getCrnNo());
+                MessageQueue.offer(SlaveType.Crn, crnProtocol.getCrnNo(), new Task(2, resetCommand));
+                News.info("鍫嗗灈鏈轰换鍔$姸鎬佹洿鏂版垚鍔燂紝鍫嗗灈鏈哄彿={}锛屽伐浣滃彿={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
+            }
+
+            redisUtil.set(RedisKeyType.CRN_IO_EXECUTE_FINISH_LIMIT.key + basCrnp.getCrnNo(), "lock",10);
         }
     }
 
     public synchronized void plannerExecute() {
-        Set<Integer> crnMoveBlockedCrnNos = executeCrnMoveTask();
-
-        int nowSec = (int) (System.currentTimeMillis() / 1000);
         List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
         for (BasCrnp basCrnp : basCrnps) {
-            if (basCrnp == null || basCrnp.getCrnNo() == null || crnMoveBlockedCrnNos.contains(basCrnp.getCrnNo())) {
-                continue;
-            }
+            plannerExecute(basCrnp);
+        }
+    }
+
+    private void plannerExecute(BasCrnp basCrnp) {
+        int nowSec = (int) (System.currentTimeMillis() / 1000);
+        if (basCrnp == null || basCrnp.getCrnNo() == null) {
+            return;
+        }
             String key = RedisKeyType.PLANNER_SCHEDULE.key + "CRN-" + basCrnp.getCrnNo();
             List<Object> items = redisUtil.lGet(key, 0, -1);
             if (items == null || items.isEmpty()) {
-                continue;
+                return;
             }
 
             CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, basCrnp.getCrnNo());
             if (crnThread == null) {
-                continue;
+                return;
             }
             CrnProtocol crnProtocol = crnThread.getStatus();
             if (crnProtocol == null) {
-                continue;
+                return;
             }
             List<WrkMast> running = wrkMastService.list(new QueryWrapper<WrkMast>()
                     .eq("crn_no", basCrnp.getCrnNo())
                     .in("wrk_sts", WrkStsType.INBOUND_RUN.sts, WrkStsType.OUTBOUND_RUN.sts, WrkStsType.LOC_MOVE_RUN.sts, WrkStsType.CRN_MOVE_RUN.sts)
             );
             if (!running.isEmpty()) {
-                continue;
+                return;
             }
             if (!(crnProtocol.getMode() == CrnModeType.AUTO.id
                     && crnProtocol.getTaskNo() == 0
@@ -722,7 +793,7 @@
                     && crnProtocol.getLoaded() == 0
                     && crnProtocol.getForkPos() == 0
                     && crnProtocol.getAlarm() == 0)) {
-                continue;
+                return;
             }
 
             for (Object v : items) {
@@ -785,7 +856,6 @@
                     }
                 }
             }
-        }
     }
 
     private synchronized boolean crnExecuteMovePlanner(BasCrnp basCrnp, CrnThread crnThread, WrkMast wrkMast) {
@@ -842,76 +912,20 @@
         return false;
     }
 
-    private Set<Integer> executeCrnMoveTask() {
-        List<WrkMast> pendingTaskQueue = wrkMastService.list(new QueryWrapper<WrkMast>()
-                .eq("io_type", WrkIoType.CRN_MOVE.id)
-                .eq("wrk_sts", WrkStsType.NEW_CRN_MOVE.sts)
-                .orderByAsc("appe_time")
-                .orderByAsc("wrk_no"));
-        Set<Integer> blockedCrnNoSet = new HashSet<>();
-        for (WrkMast wrkMast : pendingTaskQueue) {
-            if (wrkMast != null && wrkMast.getCrnNo() != null) {
-                blockedCrnNoSet.add(wrkMast.getCrnNo());
-            }
-        }
-        if (blockedCrnNoSet.isEmpty()) {
-            return blockedCrnNoSet;
-        }
-
-        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
-        Map<Integer, CrnThread> dispatchThreadMap = new HashMap<>();
-        Map<Integer, CrnProtocol> dispatchProtocolMap = new HashMap<>();
-        for (BasCrnp basCrnp : basCrnps) {
-            if (basCrnp == null || basCrnp.getCrnNo() == null) {
-                continue;
-            }
-
-            Integer crnNo = basCrnp.getCrnNo();
-            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crnNo);
-            if (crnThread == null) {
-                continue;
-            }
-
-            CrnProtocol crnProtocol = crnThread.getStatus();
-            if (crnProtocol == null) {
-                continue;
-            }
-
-            long runningCount = wrkMastService.count(new QueryWrapper<WrkMast>()
-                    .eq("crn_no", crnNo)
-                    .in("wrk_sts",
-                            WrkStsType.INBOUND_RUN.sts,
-                            WrkStsType.OUTBOUND_RUN.sts,
-                            WrkStsType.LOC_MOVE_RUN.sts,
-                            WrkStsType.CRN_MOVE_RUN.sts));
-            if (runningCount > 0) {
-                continue;
-            }
-
-            if (!Objects.equals(crnProtocol.getMode(), CrnModeType.AUTO.id)
-                    || !Objects.equals(crnProtocol.getTaskNo(), 0)
-                    || !Objects.equals(crnProtocol.getStatus(), CrnStatusType.IDLE.id)
-                    || !Objects.equals(crnProtocol.getLoaded(), 0)
-                    || !Objects.equals(crnProtocol.getForkPos(), 0)
-                    || !Objects.equals(crnProtocol.getAlarm(), 0)) {
-                continue;
-            }
-
-            Object clearLock = redisUtil.get(RedisKeyType.CLEAR_CRN_TASK_LIMIT.key + crnNo);
-            if (clearLock != null) {
-                continue;
-            }
-
-            dispatchThreadMap.put(crnNo, crnThread);
-            dispatchProtocolMap.put(crnNo, crnProtocol);
-        }
-
-        if (dispatchThreadMap.isEmpty()) {
-            return blockedCrnNoSet;
+    private boolean executeCrnMoveTask(CrnThread crnThread, CrnProtocol crnProtocol) {
+        long runningCount = wrkMastService.count(new QueryWrapper<WrkMast>()
+                .eq("crn_no", crnProtocol.getCrnNo())
+                .in("wrk_sts",
+                        WrkStsType.INBOUND_RUN.sts,
+                        WrkStsType.OUTBOUND_RUN.sts,
+                        WrkStsType.LOC_MOVE_RUN.sts,
+                        WrkStsType.CRN_MOVE_RUN.sts));
+        if (runningCount > 0) {
+            return false;
         }
 
         List<WrkMast> taskQueue = wrkMastService.list(new QueryWrapper<WrkMast>()
-                .in("crn_no", new ArrayList<>(dispatchThreadMap.keySet()))
+                .eq("crn_no", crnProtocol.getCrnNo())
                 .eq("io_type", WrkIoType.CRN_MOVE.id)
                 .eq("wrk_sts", WrkStsType.NEW_CRN_MOVE.sts)
                 .orderByAsc("appe_time")
@@ -922,32 +936,80 @@
                 continue;
             }
 
-            Integer crnNo = wrkMast.getCrnNo();
-            CrnThread crnThread = dispatchThreadMap.get(crnNo);
-            if (crnThread == null || dispatchProtocolMap.get(crnNo) == null) {
+            if (crnProtocol.getBay() == Utils.getBay(wrkMast.getLocNo())) {
+                Date now = new Date();
+                wrkMast.setWrkSts(WrkStsType.COMPLETE_CRN_MOVE.sts);
+                wrkMast.setSystemMsg("鍫嗗灈鏈哄凡鍦ㄧ洰鏍囩偣锛屼换鍔$洿鎺ュ畬鎴�");
+                wrkMast.setIoTime(now);
+                wrkMast.setModiTime(now);
+                if (wrkMastService.updateById(wrkMast)) {
+                    wrkAnalysisService.markCraneStart(wrkMast, now);
+                }
                 continue;
             }
 
-            CrnCommand moveCommand = crnThread.getMoveCommand(wrkMast.getLocNo(), wrkMast.getWrkNo(), crnNo);
+            CrnCommand moveCommand = crnThread.getMoveCommand(wrkMast.getLocNo(), wrkMast.getWrkNo(), crnProtocol.getCrnNo());
             if (moveCommand == null) {
                 continue;
             }
 
             Date now = new Date();
             wrkMast.setWrkSts(WrkStsType.CRN_MOVE_RUN.sts);
-            wrkMast.setCrnNo(crnNo);
             wrkMast.setSystemMsg("");
             wrkMast.setIoTime(now);
             wrkMast.setModiTime(now);
             if (wrkMastService.updateById(wrkMast)) {
                 wrkAnalysisService.markCraneStart(wrkMast, now);
-                MessageQueue.offer(SlaveType.Crn, crnNo, new Task(2, moveCommand));
+                MessageQueue.offer(SlaveType.Crn, crnProtocol.getCrnNo(), new Task(2, moveCommand));
                 News.info("鍫嗗灈鏈虹Щ鍔ㄥ懡浠や笅鍙戞垚鍔燂紝鍫嗗灈鏈哄彿={}锛屽伐浣滃彿={}锛岀洰鏍囦綅={}锛屼换鍔℃暟鎹�={}",
-                        crnNo, wrkMast.getWrkNo(), wrkMast.getLocNo(), JSON.toJSON(moveCommand));
-                return blockedCrnNoSet;
+                        crnProtocol.getCrnNo(), wrkMast.getWrkNo(), wrkMast.getLocNo(), JSON.toJSON(moveCommand));
+                return true;
             }
         }
-        return blockedCrnNoSet;
+
+        return false;
+    }
+
+    public void submitCrnIoTasks(long minIntervalMs) {
+        submitCrnIoTasks(MainProcessLane.CRN_IO, minIntervalMs);
+    }
+
+    public void submitCrnIoTasks(MainProcessLane lane, long minIntervalMs) {
+        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
+        for (BasCrnp basCrnp : basCrnps) {
+            Integer crnNo = basCrnp == null ? null : basCrnp.getCrnNo();
+            if (crnNo == null) {
+                continue;
+            }
+            mainProcessTaskSubmitter.submitKeyedSerialTask(
+                    lane,
+                    crnNo,
+                    "crnIoExecute",
+                    minIntervalMs,
+                    () -> crnIoExecute(basCrnp)
+            );
+        }
+    }
+
+    public void submitCrnIoExecuteFinishTasks(long minIntervalMs) {
+        submitCrnIoExecuteFinishTasks(MainProcessLane.CRN_IO_FINISH, minIntervalMs);
+    }
+
+    public void submitCrnIoExecuteFinishTasks(MainProcessLane lane, long minIntervalMs) {
+        List<BasCrnp> basCrnps = basCrnpService.list(new QueryWrapper<>());
+        for (BasCrnp basCrnp : basCrnps) {
+            Integer crnNo = basCrnp == null ? null : basCrnp.getCrnNo();
+            if (crnNo == null) {
+                continue;
+            }
+            mainProcessTaskSubmitter.submitKeyedSerialTask(
+                    lane,
+                    crnNo,
+                    "crnIoExecuteFinish",
+                    minIntervalMs,
+                    () -> crnIoExecuteFinish(basCrnp)
+            );
+        }
     }
 
     //妫�娴嬫祬搴撲綅鐘舵��
@@ -1014,6 +1076,10 @@
 
     //璋冨害鍫嗗灈鏈虹Щ鍔�
     public synchronized boolean dispatchCrnMove(Integer crnNo, String targetLocNo) {
+        return dispatchCrnMove(crnNo, targetLocNo, false);
+    }
+
+    public synchronized boolean dispatchCrnMove(Integer crnNo, String targetLocNo, boolean allowQueueWhenBusy) {
         if (crnNo == null || Cools.isEmpty(targetLocNo)) {
             return false;
         }
@@ -1044,20 +1110,18 @@
             return false;
         }
 
-        if (crnProtocol.getLevel() == targetLev) {
-            return false;
-        }
-
-        long runningCount = wrkMastService.count(new QueryWrapper<WrkMast>()
-                .eq("crn_no", crnNo)
-                .in("wrk_sts",
-                        WrkStsType.INBOUND_RUN.sts,
-                        WrkStsType.OUTBOUND_RUN.sts,
-                        WrkStsType.LOC_MOVE_RUN.sts,
-                        WrkStsType.CRN_MOVE_RUN.sts));
-        if (runningCount > 0) {
-            News.info("鍫嗗灈鏈�:{} 瀛樺湪鎵ц涓殑浠诲姟锛屾殏涓嶇敓鎴愮Щ鍔ㄤ换鍔�", crnNo);
-            return false;
+        if (!allowQueueWhenBusy) {
+            long runningCount = wrkMastService.count(new QueryWrapper<WrkMast>()
+                    .eq("crn_no", crnNo)
+                    .in("wrk_sts",
+                            WrkStsType.INBOUND_RUN.sts,
+                            WrkStsType.OUTBOUND_RUN.sts,
+                            WrkStsType.LOC_MOVE_RUN.sts,
+                            WrkStsType.CRN_MOVE_RUN.sts));
+            if (runningCount > 0) {
+                News.info("鍫嗗灈鏈�:{} 瀛樺湪鎵ц涓殑浠诲姟锛屾殏涓嶇敓鎴愮Щ鍔ㄤ换鍔�", crnNo);
+                return false;
+            }
         }
 
         WrkMast activeTask = wrkMastService.getOne(new QueryWrapper<WrkMast>()

--
Gitblit v1.9.1