From ea51b55feb73883040ed8a87b5a4aeb0bf94bb5e Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期一, 15 十二月 2025 17:02:27 +0800
Subject: [PATCH] 修改出片任务分批进行

---
 mes-processes/mes-plcSend/src/main/java/com/mes/task/service/TaskExecutionEngine.java |  173 ++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 146 insertions(+), 27 deletions(-)

diff --git a/mes-processes/mes-plcSend/src/main/java/com/mes/task/service/TaskExecutionEngine.java b/mes-processes/mes-plcSend/src/main/java/com/mes/task/service/TaskExecutionEngine.java
index bd81ac1..2362f2e 100644
--- a/mes-processes/mes-plcSend/src/main/java/com/mes/task/service/TaskExecutionEngine.java
+++ b/mes-processes/mes-plcSend/src/main/java/com/mes/task/service/TaskExecutionEngine.java
@@ -829,7 +829,9 @@
                         // 鍙湁鍦ㄤ换鍔″凡寮�濮嬫墽琛岋紙鏈変换鍔¤褰曪級鏃舵墠妫�鏌ES纭
                         DevicePlcVO.OperationResult mesResult = null;
                         try {
-                            mesResult = handler.execute(device, "checkMesConfirm", Collections.emptyMap());
+                            Map<String, Object> confirmParams = new HashMap<>();
+                            confirmParams.put("_taskContext", context);
+                            mesResult = handler.execute(device, "checkMesConfirm", confirmParams);
                         } catch (Exception e) {
                             log.warn("杩涚墖澶ц溅璁惧妫�鏌ES纭鐘舵�佸紓甯�: taskId={}, deviceId={}, error={}",
                                     task.getTaskId(), device.getId(), e.getMessage());
@@ -933,10 +935,42 @@
                     // 妫�鏌ユ槸鍚︽湁宸插鐞嗙殑鐜荤拑淇℃伅锛堜粠澶х悊鐗囩鏉ョ殑锛�
                     List<String> processedGlassIds = getProcessedGlassIds(context);
                     boolean isRunning = TaskStepDetail.Status.RUNNING.name().equals(step.getStatus());
+                    boolean isCompleted = TaskStepDetail.Status.COMPLETED.name().equals(step.getStatus());
+                    
+                    // 鑾峰彇宸插嚭鐗囩殑鐜荤拑ID鍒楄〃锛堝湪鏂规硶寮�濮嬪澹版槑锛岄伩鍏嶉噸澶嶅畾涔夛級
+                    List<String> outboundGlassIds = getOutboundGlassIds(context);
+                    
+                    // 濡傛灉姝ラ宸插畬鎴愶紝妫�鏌ユ槸鍚︽墍鏈夊垵濮嬬幓鐠冮兘宸插嚭鐗�
+                    if (isCompleted) {
+                        @SuppressWarnings("unchecked")
+                        List<String> initialGlassIds = (List<String>) context.getSharedData().get("initialGlassIds");
+                        
+                        // 濡傛灉杩樻湁鏈嚭鐗囩殑鐜荤拑锛岄噸缃楠ょ姸鎬佷负RUNNING锛岀户缁瓑寰�
+                        if (initialGlassIds != null && !initialGlassIds.isEmpty() 
+                                && (outboundGlassIds == null || !outboundGlassIds.containsAll(initialGlassIds))) {
+                            log.info("鍑虹墖澶ц溅姝ラ宸插畬鎴愶紝浣嗚繕鏈夋湭鍑虹墖鐨勭幓鐠冿紝閲嶇疆涓篟UNNING缁х画绛夊緟: taskId={}, deviceId={}, initialCount={}, outboundCount={}",
+                                    task.getTaskId(), device.getId(), 
+                                    initialGlassIds.size(), 
+                                    outboundGlassIds != null ? outboundGlassIds.size() : 0);
+                            step.setStatus(TaskStepDetail.Status.RUNNING.name());
+                            step.setEndTime(null); // 娓呴櫎缁撴潫鏃堕棿
+                            step.setSuccessMessage("绛夊緟鍓╀綑鐜荤拑鍑虹墖");
+                            taskStepDetailMapper.updateById(step);
+                            notificationService.notifyStepUpdate(task.getTaskId(), step);
+                            // 缁х画鎵ц鍚庣画閫昏緫锛屾鏌ユ槸鍚︽湁鏂扮殑宸插鐞嗙幓鐠�
+                        } else {
+                            // 鎵�鏈夌幓鐠冮兘宸插嚭鐗囷紝淇濇寔瀹屾垚鐘舵��
+                            log.debug("鍑虹墖澶ц溅鎵�鏈夌幓鐠冮兘宸插嚭鐗�: taskId={}, deviceId={}, initialCount={}, outboundCount={}",
+                                    task.getTaskId(), device.getId(), 
+                                    initialGlassIds != null ? initialGlassIds.size() : 0, 
+                                    outboundGlassIds != null ? outboundGlassIds.size() : 0);
+                            return;
+                        }
+                    }
                     
                     // 濡傛灉娌℃湁宸插鐞嗙幓鐠冿紝鍒欎笉搴斾富鍔ㄦ妸姝ラ鎷夊埌RUNNING锛屽彧淇濇寔宸茶繍琛岀姸鎬�
                     if (CollectionUtils.isEmpty(processedGlassIds)) {
-                        if (isRunning) {
+                        if (isRunning || isCompleted) {
                             // 宸茬粡鍦ㄨ繍琛岀殑鎯呭喌涓嬶紝缁х画杞MES浠诲姟/纭锛岄伩鍏嶉敊杩囩‘璁�
                             DeviceLogicHandler handler = handlerFactory.getHandler(device.getDeviceType());
                             if (handler != null) {
@@ -946,9 +980,14 @@
                                 DevicePlcVO.OperationResult mesTaskResult = null;
                                 try {
                                     mesTaskResult = handler.execute(device, "checkMesTask", Collections.emptyMap());
-                                    if (mesTaskResult != null && Boolean.TRUE.equals(mesTaskResult.getSuccess())) {
-                                        log.info("鍑虹墖澶ц溅璁惧宸叉鏌ES浠诲姟骞跺紑濮嬫墽琛�: taskId={}, deviceId={}, message={}",
-                                                task.getTaskId(), device.getId(), mesTaskResult.getMessage());
+                                    if (mesTaskResult != null) {
+                                        if (Boolean.TRUE.equals(mesTaskResult.getSuccess())) {
+                                            log.info("鍑虹墖澶ц溅璁惧宸叉鏌ES浠诲姟骞跺紑濮嬫墽琛�: taskId={}, deviceId={}, message={}",
+                                                    task.getTaskId(), device.getId(), mesTaskResult.getMessage());
+                                        } else {
+                                            log.debug("鍑虹墖澶ц溅璁惧妫�鏌ES浠诲姟锛岀瓑寰呬腑: taskId={}, deviceId={}, message={}",
+                                                    task.getTaskId(), device.getId(), mesTaskResult.getMessage());
+                                        }
                                     }
                                 } catch (Exception e) {
                                     log.warn("鍑虹墖澶ц溅璁惧妫�鏌ES浠诲姟寮傚父: taskId={}, deviceId={}, error={}",
@@ -958,7 +997,9 @@
                                 // 鐒跺悗妫�鏌ES纭鐘舵�侊紙鍙湁鍦ㄤ换鍔″凡寮�濮嬫墽琛屾椂鎵嶆鏌ワ級
                                 DevicePlcVO.OperationResult mesResult = null;
                                 try {
-                                    mesResult = handler.execute(device, "checkMesConfirm", Collections.emptyMap());
+                                    Map<String, Object> checkParams = new HashMap<>();
+                                    checkParams.put("_taskContext", context);
+                                    mesResult = handler.execute(device, "checkMesConfirm", checkParams);
                                 } catch (Exception e) {
                                     log.warn("鍑虹墖澶ц溅璁惧妫�鏌ES纭鐘舵�佸紓甯�: taskId={}, deviceId={}, error={}",
                                             task.getTaskId(), device.getId(), e.getMessage());
@@ -993,28 +1034,30 @@
                     log.debug("鍑虹墖澶ц溅璁惧瀹氭椂鍣ㄦ娴嬪埌宸插鐞嗙殑鐜荤拑淇℃伅: taskId={}, deviceId={}, glassCount={}",
                             task.getTaskId(), device.getId(), processedGlassIds.size());
 
-                    // 闇�绛夊緟澶х悊鐗囩瀹屾垚鍏ㄩ儴鐜荤拑鐨勫鐞嗗悗鍐嶅嚭鐗�
-                    @SuppressWarnings("unchecked")
-                    List<String> initialGlassIds = (List<String>) context.getSharedData().get("initialGlassIds");
-                    if (!CollectionUtils.isEmpty(initialGlassIds)
-                            && !processedGlassIds.containsAll(initialGlassIds)) {
-                        // 閮ㄥ垎鐜荤拑灏氭湭鐢卞ぇ鐞嗙墖绗煎鐞嗗畬鎴愶紝淇濇寔绛夊緟
-                        deviceCoordinationService.syncDeviceStatus(device,
-                                DeviceCoordinationService.DeviceStatus.WAITING, context);
-                        if (!TaskStepDetail.Status.PENDING.name().equals(step.getStatus())) {
-                            step.setStatus(TaskStepDetail.Status.PENDING.name());
-                            step.setSuccessMessage("绛夊緟澶х悊鐗囩澶勭悊鍏ㄩ儴鐜荤拑鍚庡啀鍑虹墖");
-                            taskStepDetailMapper.updateById(step);
-                            notificationService.notifyStepUpdate(task.getTaskId(), step);
+                    // 杩囨护鍑鸿繕鏈嚭鐗囩殑鐜荤拑锛堟敮鎸佸垎鎵瑰嚭鐗囷級
+                    // 閲嶆柊鑾峰彇宸插嚭鐗囩殑鐜荤拑ID鍒楄〃锛堝彲鑳藉湪涓婇潰鐨勯�昏緫涓凡鏇存柊锛�
+                    outboundGlassIds = getOutboundGlassIds(context);
+                    List<String> glassIdsToOutbound = new ArrayList<>();
+                    for (String glassId : processedGlassIds) {
+                        if (outboundGlassIds == null || !outboundGlassIds.contains(glassId)) {
+                            glassIdsToOutbound.add(glassId);
                         }
-                        log.debug("鍑虹墖澶ц溅绛夊緟澶х悊鐗囩瀹屾垚鍏ㄩ儴鐜荤拑: taskId={}, deviceId={}, processed={}, initial={}",
-                                task.getTaskId(), device.getId(), processedGlassIds.size(), initialGlassIds.size());
+                    }
+                    
+                    // 濡傛灉娌℃湁闇�瑕佸嚭鐗囩殑鐜荤拑锛堥兘宸茬粡鍑虹墖杩囦簡锛夛紝缁х画绛夊緟鏂扮殑宸插鐞嗙幓鐠�
+                    if (glassIdsToOutbound.isEmpty()) {
+                        log.debug("鍑虹墖澶ц溅宸插鐞嗙殑鐜荤拑閮藉凡鍑虹墖锛岀瓑寰呮柊鐨勫凡澶勭悊鐜荤拑: taskId={}, deviceId={}",
+                                task.getTaskId(), device.getId());
                         return;
                     }
                     
+                    log.debug("鍑虹墖澶ц溅鍑嗗鍑虹墖: taskId={}, deviceId={}, 寰呭嚭鐗囨暟閲�={}, 宸插嚭鐗囨暟閲�={}",
+                            task.getTaskId(), device.getId(), glassIdsToOutbound.size(), 
+                            outboundGlassIds != null ? outboundGlassIds.size() : 0);
+                    
                     // 鎵ц鍑虹墖鎿嶄綔
                     Map<String, Object> checkParams = new HashMap<>();
-                    checkParams.put("glassIds", new ArrayList<>(processedGlassIds));
+                    checkParams.put("glassIds", glassIdsToOutbound);
                     checkParams.put("_taskContext", context);
                     
                     DeviceLogicHandler handler = handlerFactory.getHandler(device.getDeviceType());
@@ -1041,9 +1084,17 @@
                                 notificationService.notifyStepUpdate(task.getTaskId(), step);
                             }
                             log.debug("鍑虹墖澶ц溅璁惧瀹氭椂鍣ㄦ墽琛屾垚鍔�: taskId={}, deviceId={}, glassCount={}",
-                                    task.getTaskId(), device.getId(), processedGlassIds.size());
-                            // 娓呯┖宸插鐞嗙殑鐜荤拑ID鍒楄〃锛堝凡澶勭悊锛�
-                            clearProcessedGlassIds(context);
+                                    task.getTaskId(), device.getId(), glassIdsToOutbound.size());
+                            // 璁板綍宸插嚭鐗囩殑鐜荤拑ID锛堝彧璁板綍鏈鍑虹墖鐨勭幓鐠冿級
+                            addOutboundGlassIds(context, glassIdsToOutbound);
+                            // 浠巔rocessedGlassIds涓Щ闄ゅ凡鍑虹墖鐨勭幓鐠冿紝淇濈暀鏈嚭鐗囩殑鐜荤拑
+                            processedGlassIds.removeAll(glassIdsToOutbound);
+                            // 濡傛灉杩樻湁鏈嚭鐗囩殑鐜荤拑锛屼笉娓呯┖processedGlassIds锛涘鏋滃叏閮ㄥ嚭鐗囦簡锛屾竻绌�
+                            if (processedGlassIds.isEmpty()) {
+                                clearProcessedGlassIds(context);
+                            } else {
+                                setProcessedGlassIds(context, processedGlassIds);
+                            }
                             
                             // feedGlass鎴愬姛鍚庯紝鍏堟鏌ES浠诲姟锛坈heckMesTask锛夋潵寮�濮嬫墽琛屼换鍔�
                             DevicePlcVO.OperationResult mesTaskResult = null;
@@ -1075,10 +1126,49 @@
                         // 鍙湁鍦ㄤ换鍔″凡寮�濮嬫墽琛岋紙鏈変换鍔¤褰曪級鏃舵墠妫�鏌ES纭
                         DevicePlcVO.OperationResult mesResult = null;
                         try {
-                            mesResult = handler.execute(device, "checkMesConfirm", Collections.emptyMap());
+                            Map<String, Object> confirmParams = new HashMap<>();
+                            confirmParams.put("_taskContext", context);
+                            mesResult = handler.execute(device, "checkMesConfirm", confirmParams);
                         } catch (Exception e) {
                             log.warn("鍑虹墖澶ц溅璁惧妫�鏌ES纭鐘舵�佸紓甯�: taskId={}, deviceId={}, error={}",
                                     task.getTaskId(), device.getId(), e.getMessage());
+                        }
+                        
+                        // 瀵逛簬鍑虹墖澶ц溅锛岄渶瑕佹鏌ユ槸鍚︽墍鏈夊垵濮嬬幓鐠冮兘宸插嚭鐗�
+                        // 濡傛灉MES杩斿洖completed=true锛屼絾杩樻湁鏈嚭鐗囩殑鐜荤拑锛屽垯涓嶅簲鏍囪涓哄畬鎴�
+                        if (mesResult != null && mesResult.getData() != null) {
+                            Object completedFlag = mesResult.getData().get("completed");
+                            boolean mesCompleted = false;
+                            if (completedFlag instanceof Boolean) {
+                                mesCompleted = (Boolean) completedFlag;
+                            } else if (completedFlag != null) {
+                                mesCompleted = "true".equalsIgnoreCase(String.valueOf(completedFlag));
+                            }
+                            
+                            // 濡傛灉MES杩斿洖completed=true锛屾鏌ユ槸鍚︽墍鏈夊垵濮嬬幓鐠冮兘宸插嚭鐗�
+                            if (mesCompleted) {
+                                @SuppressWarnings("unchecked")
+                                List<String> initialGlassIds = (List<String>) context.getSharedData().get("initialGlassIds");
+                                // 閲嶆柊鑾峰彇宸插嚭鐗囩殑鐜荤拑ID鍒楄〃锛堝彲鑳藉湪涓婇潰鐨勯�昏緫涓凡鏇存柊锛�
+                                outboundGlassIds = getOutboundGlassIds(context);
+                                
+                                // 濡傛灉杩樻湁鏈嚭鐗囩殑鐜荤拑锛屼慨鏀筸esResult锛屽皢completed璁句负false
+                                if (initialGlassIds != null && !initialGlassIds.isEmpty() 
+                                        && (outboundGlassIds == null || !outboundGlassIds.containsAll(initialGlassIds))) {
+                                    log.debug("鍑虹墖澶ц溅MES杩斿洖completed=true锛屼絾杩樻湁鏈嚭鐗囩殑鐜荤拑: taskId={}, deviceId={}, initialCount={}, outboundCount={}",
+                                            task.getTaskId(), device.getId(), 
+                                            initialGlassIds.size(), 
+                                            outboundGlassIds != null ? outboundGlassIds.size() : 0);
+                                    // 淇敼mesResult锛屽皢completed璁句负false锛屼繚鎸丷UNNING鐘舵��
+                                    Map<String, Object> modifiedData = new HashMap<>(mesResult.getData());
+                                    modifiedData.put("completed", false);
+                                    DevicePlcVO.OperationResult modifiedResult = new DevicePlcVO.OperationResult();
+                                    modifiedResult.setSuccess(mesResult.getSuccess());
+                                    modifiedResult.setMessage(mesResult.getMessage());
+                                    modifiedResult.setData(modifiedData);
+                                    mesResult = modifiedResult;
+                                }
+                            }
                         }
                         
                         // 鏇存柊姝ラ鐘舵�侊紙澶ц溅璁惧淇濇寔RUNNING锛岀洿鍒癕ES纭瀹屾垚鎴栦换鍔″彇娑堬級
@@ -1364,6 +1454,33 @@
     private void clearProcessedGlassIds(TaskExecutionContext context) {
         if (context != null) {
             context.getSharedData().put("processedGlassIds", new ArrayList<>());
+        }
+    }
+    
+    /**
+     * 鑾峰彇宸插嚭鐗囩殑鐜荤拑ID鍒楄〃
+     */
+    @SuppressWarnings("unchecked")
+    private List<String> getOutboundGlassIds(TaskExecutionContext context) {
+        if (context == null) {
+            return Collections.emptyList();
+        }
+        Object glassIds = context.getSharedData().get("outboundGlassIds");
+        if (glassIds instanceof List) {
+            return new ArrayList<>((List<String>) glassIds);
+        }
+        return Collections.emptyList();
+    }
+    
+    /**
+     * 娣诲姞宸插嚭鐗囩殑鐜荤拑ID鍒楄〃
+     */
+    private void addOutboundGlassIds(TaskExecutionContext context, List<String> glassIds) {
+        if (context != null && glassIds != null && !glassIds.isEmpty()) {
+            List<String> existing = getOutboundGlassIds(context);
+            Set<String> allOutbound = new HashSet<>(existing);
+            allOutbound.addAll(glassIds);
+            context.getSharedData().put("outboundGlassIds", new ArrayList<>(allOutbound));
         }
     }
     
@@ -1818,7 +1935,9 @@
             // 鐒跺悗妫�鏌ES纭鐘舵��
             DevicePlcVO.OperationResult mesResult = null;
             try {
-                mesResult = handler.execute(device, "checkMesConfirm", Collections.emptyMap());
+                Map<String, Object> checkParams = new HashMap<>();
+                checkParams.put("_taskContext", context);
+                mesResult = handler.execute(device, "checkMesConfirm", checkParams);
             } catch (Exception e) {
                 log.warn("澶ц溅璁惧妫�鏌ES纭鐘舵�佸紓甯�: taskId={}, deviceId={}, error={}",
                         task.getTaskId(), device.getId(), e.getMessage());

--
Gitblit v1.8.0