From 9571229a2013472dc701ecf5767f2873b36d8f90 Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期四, 11 十二月 2025 17:07:19 +0800
Subject: [PATCH] 修复导入Excel功能工程号自增; 添加选择工程号自动填写玻璃id列表

---
 mes-processes/mes-plcSend/src/main/java/com/mes/task/service/TaskExecutionEngine.java | 1099 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 966 insertions(+), 133 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 2156e46..4c5f2d3 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
@@ -102,6 +102,8 @@
 
         TaskExecutionContext context = new TaskExecutionContext(parameters);
         runningTaskContexts.put(task.getTaskId(), context);
+        // 灏嗘湰娆′换鍔℃秹鍙婄殑璁惧鍒楄〃瀛樺叆涓婁笅鏂囷紝渚夸簬鍙栨秷浠诲姟鏃跺仛璁惧绾ф敹灏撅紙濡傚仠姝㈠ぇ杞﹀唴閮ㄧ洃鎺у畾鏃跺櫒锛�
+        context.getSharedData().put("devices", devices);
         
         task.setTotalSteps(devices.size());
         task.setStatus(MultiDeviceTask.Status.RUNNING.name());
@@ -148,7 +150,7 @@
             
             for (DeviceConfig device : devices) {
                 String deviceType = device.getDeviceType();
-                log.info("澶勭悊璁惧: deviceId={}, deviceType={}, deviceName={}, WORKSTATION_SCANNER甯搁噺={}, equals={}", 
+                log.debug("澶勭悊璁惧: deviceId={}, deviceType={}, deviceName={}, WORKSTATION_SCANNER甯搁噺={}, equals={}", 
                         device.getId(), deviceType, device.getDeviceName(), 
                         DeviceConfig.DeviceType.WORKSTATION_SCANNER,
                         DeviceConfig.DeviceType.WORKSTATION_SCANNER.equals(deviceType));
@@ -157,25 +159,20 @@
                         || (deviceType != null && (deviceType.contains("鎵爜") || deviceType.contains("SCANNER")));
                 boolean isLargeGlass = DeviceConfig.DeviceType.LARGE_GLASS.equals(deviceType);
                 boolean isTransfer = DeviceConfig.DeviceType.WORKSTATION_TRANSFER.equals(deviceType);
-                log.info("璁惧绫诲瀷鍒ゆ柇: deviceId={}, isLoadVehicle={}, isScanner={}, isLargeGlass={}, isTransfer={}", 
+                log.debug("璁惧绫诲瀷鍒ゆ柇: deviceId={}, isLoadVehicle={}, isScanner={}, isLargeGlass={}, isTransfer={}", 
                         device.getId(), isLoadVehicle, isScanner, isLargeGlass, isTransfer);
 
                 // 1. 鍗ц浆绔嬫壂鐮佽澶囷細鍚姩瀹氭椂鍣ㄦ壂鎻忥紙姣�10绉掑鐞嗕竴涓幓鐠僆D锛�
                 if (isScanner) {
-                    log.info("妫�娴嬪埌鎵爜璁惧锛屽噯澶囧惎鍔ㄥ畾鏃跺櫒: deviceId={}, deviceType={}, deviceName={}", 
+                    log.debug("妫�娴嬪埌鎵爜璁惧锛屽噯澶囧惎鍔ㄥ畾鏃跺櫒: deviceId={}, deviceType={}, deviceName={}", 
                             device.getId(), device.getDeviceType(), device.getDeviceName());
                     TaskStepDetail step = createStepRecord(task, device, currentOrder);
-                    // 璁剧疆姝ラ涓鸿繍琛岀姸鎬侊紝骞惰缃紑濮嬫椂闂�
-                    step.setStatus(TaskStepDetail.Status.RUNNING.name());
-                    step.setStartTime(new Date());
-                    taskStepDetailMapper.updateById(step);
-                    notificationService.notifyStepUpdate(task.getTaskId(), step);
                     
                     ScheduledFuture<?> scannerTask = startScannerTimer(task, step, device, context);
                     if (scannerTask != null) {
                         registerScheduledTask(task.getTaskId(), scannerTask);
                         stepSummaries.add(createStepSummary(device.getDeviceName(), true, "瀹氭椂鍣ㄥ凡鍚姩锛屾瘡10绉掓壂鎻忎竴娆�"));
-                        log.info("鎵爜璁惧瀹氭椂鍣ㄥ惎鍔ㄦ垚鍔�: deviceId={}, taskId={}", device.getId(), task.getTaskId());
+                        log.debug("鎵爜璁惧瀹氭椂鍣ㄥ惎鍔ㄦ垚鍔�: deviceId={}, taskId={}", device.getId(), task.getTaskId());
                     } else {
                         log.warn("鎵爜璁惧瀹氭椂鍣ㄥ惎鍔ㄥけ璐ワ紝glassIds鍙兘涓虹┖: deviceId={}, taskId={}, contextParams={}", 
                                 device.getId(), task.getTaskId(), context.getParameters());
@@ -190,20 +187,15 @@
 
                 // 2. 鍗ц浆绔嬭澶囷細鍚姩瀹氭椂鍣ㄥ畾鏈熸鏌ュ苟澶勭悊锛堜腑杞澶囷級
                 if (isTransfer) {
-                    log.info("妫�娴嬪埌鍗ц浆绔嬭澶囷紝鍑嗗鍚姩瀹氭椂鍣�: deviceId={}, deviceType={}, deviceName={}", 
+                    log.debug("妫�娴嬪埌鍗ц浆绔嬭澶囷紝鍑嗗鍚姩瀹氭椂鍣�: deviceId={}, deviceType={}, deviceName={}", 
                             device.getId(), device.getDeviceType(), device.getDeviceName());
                     TaskStepDetail step = createStepRecord(task, device, currentOrder);
-                    // 璁剧疆姝ラ涓鸿繍琛岀姸鎬侊紝骞惰缃紑濮嬫椂闂�
-                    step.setStatus(TaskStepDetail.Status.RUNNING.name());
-                    step.setStartTime(new Date());
-                    taskStepDetailMapper.updateById(step);
-                    notificationService.notifyStepUpdate(task.getTaskId(), step);
                     
                     ScheduledFuture<?> transferTask = startTransferTimer(task, step, device, context);
                     if (transferTask != null) {
                         registerScheduledTask(task.getTaskId(), transferTask);
                         stepSummaries.add(createStepSummary(device.getDeviceName(), true, "瀹氭椂鍣ㄥ凡鍚姩锛屽畾鏈熸鏌ュ苟澶勭悊鐜荤拑鎵规"));
-                        log.info("鍗ц浆绔嬭澶囧畾鏃跺櫒鍚姩鎴愬姛: deviceId={}, taskId={}", device.getId(), task.getTaskId());
+                        log.debug("鍗ц浆绔嬭澶囧畾鏃跺櫒鍚姩鎴愬姛: deviceId={}, taskId={}", device.getId(), task.getTaskId());
                     } else {
                         log.warn("鍗ц浆绔嬭澶囧畾鏃跺櫒鍚姩澶辫触: deviceId={}, taskId={}", device.getId(), task.getTaskId());
                         stepSummaries.add(createStepSummary(device.getDeviceName(), false, "鍚姩瀹氭椂鍣ㄥけ璐�"));
@@ -221,11 +213,6 @@
                     boolean isInboundVehicle = currentLoadVehicleIndex == 1; // 绗竴涓ぇ杞︽槸杩涚墖澶ц溅
                     
                     TaskStepDetail step = createStepRecord(task, device, currentOrder);
-                    // 璁剧疆姝ラ涓鸿繍琛岀姸鎬侊紝骞惰缃紑濮嬫椂闂�
-                    step.setStatus(TaskStepDetail.Status.RUNNING.name());
-                    step.setStartTime(new Date());
-                    taskStepDetailMapper.updateById(step);
-                    notificationService.notifyStepUpdate(task.getTaskId(), step);
                     
                     ScheduledFuture<?> vehicleTask;
                     if (isInboundVehicle) {
@@ -260,11 +247,6 @@
                 // 4. 澶х悊鐗囩璁惧锛氬惎鍔ㄥ畾鏃跺櫒閫昏緫澶勭悊锛堜笉娑夊強PLC浜や簰锛屽彧璐熻矗閫昏緫澶勭悊锛�
                 if (isLargeGlass) {
                     TaskStepDetail step = createStepRecord(task, device, currentOrder);
-                    // 璁剧疆姝ラ涓鸿繍琛岀姸鎬侊紝骞惰缃紑濮嬫椂闂�
-                    step.setStatus(TaskStepDetail.Status.RUNNING.name());
-                    step.setStartTime(new Date());
-                    taskStepDetailMapper.updateById(step);
-                    notificationService.notifyStepUpdate(task.getTaskId(), step);
                     
                     ScheduledFuture<?> largeGlassTask = startLargeGlassTimer(task, step, device, context);
                     if (largeGlassTask != null) {
@@ -296,7 +278,7 @@
             // 瀹氭椂鍣ㄤ細鍦ㄥ悗鍙版寔缁繍琛岋紝鐩村埌鎵嬪姩鍋滄鎴栬秴鏃�
             boolean hasScheduledTasks = !CollectionUtils.isEmpty(taskScheduledTasks.get(task.getTaskId()));
             if (hasScheduledTasks) {
-                log.info("浠诲姟宸插惎鍔ㄦ墍鏈夊畾鏃跺櫒锛屼繚鎸佽繍琛岀姸鎬�: taskId={}, scheduledTasksCount={}", 
+                log.debug("浠诲姟宸插惎鍔ㄦ墍鏈夊畾鏃跺櫒锛屼繚鎸佽繍琛岀姸鎬�: taskId={}, scheduledTasksCount={}", 
                         task.getTaskId(), taskScheduledTasks.get(task.getTaskId()).size());
                 // 浠诲姟淇濇寔 RUNNING 鐘舵�侊紝瀹氭椂鍣ㄥ湪鍚庡彴杩愯
                 // 涓嶆洿鏂颁换鍔$姸鎬佷负 COMPLETED锛岃浠诲姟鎸佺画杩愯
@@ -360,6 +342,35 @@
         if (context != null) {
             context.getSharedData().put("taskCancelled", true);
             log.warn("宸叉爣璁颁换鍔″彇娑�: taskId={}", taskId);
+
+            // 鍚屾椂閫氱煡鐩稿叧璁惧閫昏緫澶勭悊鍣ㄦ墽琛屽彇娑堟敹灏鹃�昏緫锛堜緥濡傚仠姝㈠ぇ杞﹀唴閮ㄧ殑鐩戞帶瀹氭椂鍣級
+            try {
+                Map<String, Object> cancelParams = new HashMap<>();
+                cancelParams.put("_taskContext", context);
+                Object devicesObj = context.getSharedData().get("devices");
+                if (devicesObj instanceof List) {
+                    @SuppressWarnings("unchecked")
+                    List<DeviceConfig> devices = (List<DeviceConfig>) devicesObj;
+                    for (DeviceConfig device : devices) {
+                        if (device == null) {
+                            continue;
+                        }
+                        try {
+                            DeviceLogicHandler handler = handlerFactory.getHandler(device.getDeviceType());
+                            if (handler != null) {
+                                // 鐩墠澶ц溅閫昏緫澶勭悊鍣ㄤ細鍦╮eset/clear绛夋搷浣滀腑鍋滄鍐呴儴鐩戞帶瀹氭椂鍣�
+                                // 杩欓噷缁熶竴璋冪敤涓�娆♀�渞eset鈥濅綔涓轰换鍔″彇娑堟椂鐨勬敹灏惧姩浣�
+                                handler.execute(device, "reset", cancelParams);
+                            }
+                        } catch (Exception e) {
+                            log.warn("浠诲姟鍙栨秷鏃舵墽琛岃澶囨敹灏�(reset)澶辫触: taskId={}, deviceId={}, error={}",
+                                    taskId, device.getId(), e.getMessage());
+                        }
+                    }
+                }
+            } catch (Exception e) {
+                log.warn("浠诲姟鍙栨秷鏃舵墽琛岃澶囨敹灏鹃�昏緫寮傚父: taskId={}, error={}", taskId, e.getMessage());
+            }
         } else {
             log.warn("璇锋眰鍙栨秷浠诲姟浣嗘湭鎵惧埌涓婁笅鏂�: taskId={}", taskId);
         }
@@ -376,7 +387,7 @@
         try {
             TaskParameters params = context.getParameters();
             List<String> glassIds = params.getGlassIds();
-            log.info("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒鍒濆鍖�: taskId={}, deviceId={}, glassIds={}, glassIdsSize={}, isEmpty={}", 
+            log.debug("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒鍒濆鍖�: taskId={}, deviceId={}, glassIds={}, glassIdsSize={}, isEmpty={}", 
                     task.getTaskId(), device.getId(), glassIds, 
                     glassIds != null ? glassIds.size() : 0, 
                     CollectionUtils.isEmpty(glassIds));
@@ -391,19 +402,25 @@
             AtomicInteger successCount = new AtomicInteger(0);
             AtomicInteger failCount = new AtomicInteger(0);
             
-            final long CYCLE_INTERVAL_MS = 10_000; // 10绉掗棿闅�
+            // 浠庤澶囬厤缃腑鑾峰彇鎵爜闂撮殧锛岄粯璁�10绉�
+            Map<String, Object> logicParams = parseLogicParams(device);
+            Integer scanIntervalMs = getLogicParam(logicParams, "scanIntervalMs", 10_000);
             
-            log.info("鍚姩鍗ц浆绔嬫壂鐮佸畾鏃跺櫒: taskId={}, deviceId={}, glassCount={}, interval={}s, glassIds={}",
-                    task.getTaskId(), device.getId(), glassIds.size(), CYCLE_INTERVAL_MS / 1000, glassIds);
+            log.debug("鍚姩鍗ц浆绔嬫壂鐮佸畾鏃跺櫒: taskId={}, deviceId={}, glassCount={}, interval={}ms, glassIds={}",
+                    task.getTaskId(), device.getId(), glassIds.size(), scanIntervalMs, glassIds);
             
             // 鍚姩瀹氭椂浠诲姟
             ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(() -> {
                 try {
                     if (isTaskCancelled(context)) {
-                        log.info("浠诲姟宸插彇娑堬紝鍋滄鍗ц浆绔嬫壂鐮佸畾鏃跺櫒: taskId={}, deviceId={}", 
+                        log.debug("浠诲姟宸插彇娑堬紝鍋滄鍗ц浆绔嬫壂鐮佸畾鏃跺櫒: taskId={}, deviceId={}",
                                 task.getTaskId(), device.getId());
                         return;
                     }
+                    // 瀹氭椂鍣ㄧ涓�娆℃墽琛屾椂锛屽皢璁惧鐘舵�佷粠 WAITING 璁剧疆涓� RUNNING
+                    deviceCoordinationService.syncDeviceStatus(device,
+                            DeviceCoordinationService.DeviceStatus.RUNNING, context);
+                    ensureStepRunning(step, task.getTaskId());
                     // 妫�鏌ユ槸鍚﹂渶瑕佹殏鍋�
                     if (shouldPauseScanner(context)) {
                         log.debug("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒鏆傚仠: taskId={}, deviceId={}", task.getTaskId(), device.getId());
@@ -413,9 +430,25 @@
                     // 妫�鏌ユ槸鍚﹁繕鏈夊緟澶勭悊鐨勭幓鐠僆D
                     String glassId = glassIdQueue.poll();
                     if (glassId == null) {
-                        log.info("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒瀹屾垚: taskId={}, deviceId={}, processed={}/{}, success={}, fail={}",
+                        log.debug("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒瀹屾垚: taskId={}, deviceId={}, processed={}/{}, success={}, fail={}",
                                 task.getTaskId(), device.getId(), processedCount.get(), glassIds.size(),
                                 successCount.get(), failCount.get());
+                        
+                        // 娓呯┖plcRequest鍜宲lcGlassId锛堢‘淇漃LC鐘舵�佹竻鐞嗭級
+                        try {
+                            DeviceLogicHandler handler = handlerFactory.getHandler(device.getDeviceType());
+                            if (handler != null) {
+                                Map<String, Object> clearParams = new HashMap<>();
+                                clearParams.put("_taskContext", context);
+                                handler.execute(device, "clearPlc", clearParams);
+                                log.debug("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒瀹屾垚锛屽凡娓呯┖PLC璇锋眰瀛楁: taskId={}, deviceId={}", 
+                                        task.getTaskId(), device.getId());
+                            }
+                        } catch (Exception e) {
+                            log.warn("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒瀹屾垚鏃舵竻绌篜LC澶辫触: taskId={}, deviceId={}, error={}", 
+                                    task.getTaskId(), device.getId(), e.getMessage());
+                        }
+                        
                         // 鑻ヤ箣鍓嶆湭鍑虹幇澶辫触锛屽啀灏嗙姸鎬佺疆涓哄畬鎴�
                         boolean alreadyFailed = TaskStepDetail.Status.FAILED.name().equals(step.getStatus());
                         if (!alreadyFailed) {
@@ -426,6 +459,8 @@
                             }
                             taskStepDetailMapper.updateById(step);
                             notificationService.notifyStepUpdate(task.getTaskId(), step);
+                            // 鎵爜璁惧瀹屾垚鍚庡皾璇曡嚜鍔ㄦ敹灏炬暣涓换鍔�
+                            checkAndCompleteTaskIfDone(step.getTaskId());
                         }
                         deviceCoordinationService.syncDeviceStatus(device,
                                 DeviceCoordinationService.DeviceStatus.COMPLETED, context);
@@ -433,32 +468,31 @@
                     }
                     
                     int currentIndex = processedCount.incrementAndGet();
-                    log.info("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒澶勭悊绗瑊}/{}涓幓鐠�: taskId={}, deviceId={}, glassId={}",
+                    log.debug("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒澶勭悊绗瑊}/{}涓幓鐠�: taskId={}, deviceId={}, glassId={}",
                             currentIndex, glassIds.size(), task.getTaskId(), device.getId(), glassId);
                     
                     // 鎵ц鍗曟鎵弿
                     Map<String, Object> scanParams = new HashMap<>();
                     scanParams.put("glassId", glassId);
                     scanParams.put("_taskContext", context);
-                    log.info("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒鍑嗗鎵ц: taskId={}, deviceId={}, glassId={}, scanParams={}", 
+                    log.debug("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒鍑嗗鎵ц: taskId={}, deviceId={}, glassId={}, scanParams={}", 
                             task.getTaskId(), device.getId(), glassId, scanParams);
                     
                     DeviceLogicHandler handler = handlerFactory.getHandler(device.getDeviceType());
                     if (handler != null) {
-                        // 灏唋ogicParams鍚堝苟鍒皊canParams涓�
-                        Map<String, Object> logicParams = parseLogicParams(device);
+                        // 灏唋ogicParams鍚堝苟鍒皊canParams涓紙浣跨敤宸插畾涔夌殑logicParams鍙橀噺锛�
                         if (logicParams != null && !logicParams.isEmpty()) {
                             scanParams.put("_logicParams", logicParams);
                         }
-                        log.info("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒璋冪敤handler.execute: taskId={}, deviceId={}, glassId={}, operation=scanOnce, scanParamsKeys={}, scanParams={}", 
+                        log.debug("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒璋冪敤handler.execute: taskId={}, deviceId={}, glassId={}, operation=scanOnce, scanParamsKeys={}, scanParams={}", 
                                 task.getTaskId(), device.getId(), glassId, scanParams.keySet(), scanParams);
                         DevicePlcVO.OperationResult result = handler.execute(device, "scanOnce", scanParams);
-                        log.info("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒handler.execute杩斿洖: taskId={}, deviceId={}, glassId={}, success={}", 
+                        log.debug("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒handler.execute杩斿洖: taskId={}, deviceId={}, glassId={}, success={}", 
                                 task.getTaskId(), device.getId(), glassId, result.getSuccess());
                         
                         if (Boolean.TRUE.equals(result.getSuccess())) {
                             successCount.incrementAndGet();
-                            log.info("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒澶勭悊鎴愬姛: taskId={}, deviceId={}, glassId={}",
+                            log.debug("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒澶勭悊鎴愬姛: taskId={}, deviceId={}, glassId={}",
                                     task.getTaskId(), device.getId(), glassId);
                         } else {
                             failCount.incrementAndGet();
@@ -466,9 +500,10 @@
                                     task.getTaskId(), device.getId(), glassId, result.getMessage());
                         }
                         
-                        // 鏇存柊姝ラ鐘舵��
-                        updateStepStatus(step, result);
-                        // 閫氱煡姝ラ鏇存柊锛堣鍓嶇瀹炴椂鐪嬪埌姝ラ鐘舵�侊級
+                        // 鏇存柊姝ラ鐘舵�侊紙鏄剧ず杩涘害锛屼繚鎸丷UNNING鐘舵�佺洿鍒版墍鏈夌幓鐠冨鐞嗗畬鎴愶級
+                        updateStepStatusForScanner(step, result, currentIndex, glassIds.size(), 
+                                successCount.get(), failCount.get());
+                        // 閫氱煡姝ラ鏇存柊锛堣鍓嶇瀹炴椂鐪嬪埌姝ラ鐘舵�佸拰杩涘害锛�
                         notificationService.notifyStepUpdate(task.getTaskId(), step);
                         boolean opSuccess = Boolean.TRUE.equals(result.getSuccess());
                         updateTaskProgress(task, step.getStepOrder(), opSuccess);
@@ -481,8 +516,10 @@
                     log.error("鍗ц浆绔嬫壂鐮佸畾鏃跺櫒鎵ц寮傚父: taskId={}, deviceId={}", task.getTaskId(), device.getId(), e);
                     failCount.incrementAndGet();
                 }
-            }, 0, CYCLE_INTERVAL_MS, TimeUnit.MILLISECONDS);
+            }, 0, scanIntervalMs, TimeUnit.MILLISECONDS);
             
+            // 鍦ㄤ覆琛屾墽琛屾ā寮忎笅锛屾壂鐮佽澶囨槸绗竴涓紝搴旇绔嬪嵆璁剧疆涓� RUNNING
+            // 鍏朵粬璁惧淇濇寔 WAITING锛岀洿鍒板畠浠湡姝e紑濮嬪伐浣�
             deviceCoordinationService.syncDeviceStatus(device,
                     DeviceCoordinationService.DeviceStatus.RUNNING, context);
             return future;
@@ -504,17 +541,28 @@
             Map<String, Object> logicParams = parseLogicParams(device);
             Integer monitorIntervalMs = getLogicParam(logicParams, "monitorIntervalMs", 5_000);
             
-            log.info("鍚姩鍗ц浆绔嬭澶囧畾鏃跺櫒: taskId={}, deviceId={}, interval={}ms",
+            log.debug("鍚姩鍗ц浆绔嬭澶囧畾鏃跺櫒: taskId={}, deviceId={}, interval={}ms",
                     task.getTaskId(), device.getId(), monitorIntervalMs);
             
             // 鍚姩瀹氭椂浠诲姟
+            // 浣跨敤AtomicBoolean鏍囪鏄惁绗竴娆℃墽琛�
+            final java.util.concurrent.atomic.AtomicBoolean firstExecution = new java.util.concurrent.atomic.AtomicBoolean(true);
+            
             ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(() -> {
                 try {
                     if (isTaskCancelled(context)) {
-                        log.info("浠诲姟宸插彇娑堬紝鍋滄鍗ц浆绔嬭澶囧畾鏃跺櫒: taskId={}, deviceId={}",
+                        log.debug("浠诲姟宸插彇娑堬紝鍋滄鍗ц浆绔嬭澶囧畾鏃跺櫒: taskId={}, deviceId={}",
                                 task.getTaskId(), device.getId());
                         return;
                     }
+                    
+                    // 濡傛灉姝ラ宸茬粡瀹屾垚锛屼笉鍐嶆墽琛屽悗缁�昏緫锛堥伩鍏嶇姸鎬佽閲嶇疆锛�
+                    if (TaskStepDetail.Status.COMPLETED.name().equals(step.getStatus())) {
+                        log.debug("鍗ц浆绔嬭澶囨楠ゅ凡瀹屾垚锛屽仠姝㈠畾鏃跺櫒鎵ц: taskId={}, deviceId={}",
+                                task.getTaskId(), device.getId());
+                        return;
+                    }
+                    
                     // 鏋勫缓鍙傛暟
                     Map<String, Object> params = new HashMap<>();
                     params.put("_taskContext", context);
@@ -527,19 +575,61 @@
                     if (handler != null) {
                         DevicePlcVO.OperationResult result = handler.execute(device, "checkAndProcess", params);
                         
+                        // 妫�鏌ユ槸鍚︽湁鏁版嵁锛氬鏋滄湁鏁版嵁鎴栨鍦ㄥ鐞嗭紝璁剧疆涓篟UNNING锛涘鏋滅紦鍐查槦鍒椾负绌轰笖鏃犲緟澶勭悊鐜荤拑锛屼繚鎸丳ENDING
+                        String message = result.getMessage();
+                        boolean hasData = result.getSuccess() != null && result.getSuccess() 
+                                && message != null && !message.contains("缂撳啿闃熷垪涓虹┖锛屾棤寰呭鐞嗙幓鐠�");
+                        
+                        // 濡傛灉褰撳墠鏄疨ENDING鐘舵�侊紝涓旀娴嬪埌鏈夋暟鎹紝鍒欒缃负RUNNING锛堣澶囧紑濮嬪伐浣滐級
+                        boolean isPending = TaskStepDetail.Status.PENDING.name().equals(step.getStatus());
+                        
+                        if (hasData && isPending) {
+                            // 妫�娴嬪埌鏁版嵁涓斿綋鍓嶆槸绛夊緟鐘舵�侊紝璁惧寮�濮嬪伐浣滐紝璁剧疆涓篟UNNING
+                            deviceCoordinationService.syncDeviceStatus(device,
+                                    DeviceCoordinationService.DeviceStatus.RUNNING, context);
+                            step.setStatus(TaskStepDetail.Status.RUNNING.name());
+                            if (step.getStartTime() == null) {
+                                step.setStartTime(new Date());
+                            }
+                            taskStepDetailMapper.updateById(step);
+                            notificationService.notifyStepUpdate(task.getTaskId(), step);
+                            log.debug("鍗ц浆绔嬭澶囧畾鏃跺櫒妫�娴嬪埌鏁版嵁锛屼粠PENDING杞负RUNNING: taskId={}, deviceId={}, message={}",
+                                    task.getTaskId(), device.getId(), message);
+                        } else if (!hasData) {
+                            // 娌℃湁鏁版嵁锛屼繚鎸丳ENDING鐘舵�侊紝绛夊緟鎵爜璁惧杈撳嚭
+                            if (firstExecution.compareAndSet(true, false)) {
+                                // 绗竴娆℃墽琛岋紝纭繚鐘舵�佹槸PENDING
+                                if (!TaskStepDetail.Status.PENDING.name().equals(step.getStatus())) {
+                                    step.setStatus(TaskStepDetail.Status.PENDING.name());
+                                    step.setSuccessMessage("绛夊緟鎵爜璁惧杈撳嚭鏁版嵁");
+                                    taskStepDetailMapper.updateById(step);
+                                    notificationService.notifyStepUpdate(task.getTaskId(), step);
+                                }
+                                log.debug("鍗ц浆绔嬭澶囧畾鏃跺櫒绗竴娆℃墽琛岋紝鏃犳暟鎹紝淇濇寔PENDING: taskId={}, deviceId={}, message={}",
+                                        task.getTaskId(), device.getId(), message);
+                            }
+                            return; // 涓嶆墽琛屽悗缁�昏緫锛岀瓑寰呬笅涓�娆″畾鏃跺櫒瑙﹀彂
+                        }
+                        
                         // 鏇存柊姝ラ鐘舵�侊紙鍖哄垎绛夊緟涓拰鐪熸瀹屾垚锛�
-                        updateStepStatusForTransfer(step, result);
+                        updateStepStatusForTransfer(step, result, device, context);
                         // 閫氱煡姝ラ鏇存柊锛堣鍓嶇瀹炴椂鐪嬪埌姝ラ鐘舵�侊級
                         notificationService.notifyStepUpdate(task.getTaskId(), step);
                         boolean opSuccess = Boolean.TRUE.equals(result.getSuccess());
                         updateTaskProgress(task, step.getStepOrder(), opSuccess);
+                        
+                        // 鏍规嵁鎵ц缁撴灉鏇存柊璁惧鐘舵��
+                        // 娉ㄦ剰锛氳澶囧凡缁忓紑濮嬪伐浣滐紙瀹氭椂鍣ㄥ凡鎵ц锛夛紝鎵�浠ュ簲璇ヤ繚鎸丷UNNING鐘舵��
+                        // 鍙湁鍦ㄧ湡姝e畬鎴愭椂鎵嶈缃负COMPLETED
                         if (opSuccess) {
-                            String message = result.getMessage();
+                            // 璁惧姝e湪宸ヤ綔锛堢瓑寰呯紦鍐层�佸鐞嗕腑绛夛級锛屼繚鎸丷UNNING鐘舵��
+                            deviceCoordinationService.syncDeviceStatus(device,
+                                    DeviceCoordinationService.DeviceStatus.RUNNING, context);
                             if (message != null && message.contains("鎵规宸插啓鍏LC")) {
-                                log.info("鍗ц浆绔嬭澶囧畾鏃跺櫒鎵ц鎴愬姛锛堝凡鍐欏叆PLC锛�: taskId={}, deviceId={}, message={}",
+                                log.debug("鍗ц浆绔嬭澶囧畾鏃跺櫒鎵ц鎴愬姛锛堝凡鍐欏叆PLC锛�: taskId={}, deviceId={}, message={}",
                                         task.getTaskId(), device.getId(), message);
                             } else {
-                                log.debug("鍗ц浆绔嬭澶囧畾鏃跺櫒绛夊緟涓�: taskId={}, deviceId={}, message={}",
+                                log.debug("鍗ц浆绔嬭澶囧畾鏃跺櫒宸ヤ綔涓紙绛夊緟缂撳啿锛�: taskId={}, deviceId={}, message={}",
                                         task.getTaskId(), device.getId(), message);
                             }
                         } else {
@@ -554,8 +644,9 @@
                 }
             }, 0, monitorIntervalMs, TimeUnit.MILLISECONDS);
             
+            // 鍦ㄤ覆琛屾墽琛屾ā寮忎笅锛岃澶囧惎鍔ㄥ畾鏃跺櫒鏃跺厛璁剧疆涓� WAITING锛屽畾鏃跺櫒绗竴娆℃墽琛屾椂鍐嶈缃负 RUNNING
             deviceCoordinationService.syncDeviceStatus(device,
-                    DeviceCoordinationService.DeviceStatus.RUNNING, context);
+                    DeviceCoordinationService.DeviceStatus.WAITING, context);
             return future;
         } catch (Exception e) {
             log.error("鍚姩鍗ц浆绔嬭澶囧畾鏃跺櫒澶辫触: taskId={}, deviceId={}", task.getTaskId(), device.getId(), e);
@@ -574,33 +665,98 @@
             final long MONITOR_INTERVAL_MS = 2_000; // 2绉掔洃鎺т竴娆�
             final AtomicInteger lastProcessedCount = new AtomicInteger(0);
             
-            log.info("鍚姩杩涚墖澶ц溅璁惧瀹氭椂鍣�: taskId={}, deviceId={}, interval={}s",
+            log.debug("鍚姩杩涚墖澶ц溅璁惧瀹氭椂鍣�: taskId={}, deviceId={}, interval={}s",
                     task.getTaskId(), device.getId(), MONITOR_INTERVAL_MS / 1000);
             
             // 鍚姩瀹氭椂浠诲姟
             ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(() -> {
                 try {
                     if (isTaskCancelled(context)) {
-                        log.info("浠诲姟宸插彇娑堬紝鍋滄杩涚墖澶ц溅瀹氭椂鍣�: taskId={}, deviceId={}",
+                        log.debug("浠诲姟宸插彇娑堬紝鍋滄杩涚墖澶ц溅瀹氭椂鍣�: taskId={}, deviceId={}",
                                 task.getTaskId(), device.getId());
                         return;
                     }
-                    // 妫�鏌ユ槸鍚︽湁鍗ц浆绔嬩富浣撳凡杈撳嚭銆佸噯澶囦笂澶ц溅鐨勭幓鐠冧俊鎭�
-                    List<String> readyGlassIds = getTransferReadyGlassIds(context);
-                    if (CollectionUtils.isEmpty(readyGlassIds)) {
-                        // 娌℃湁鍗ц浆绔嬭緭鍑虹殑鐜荤拑锛岀户缁瓑寰�
+                    // 宸插畬鎴�/澶辫触鐨勬楠や笉鍐嶅洖閫�鐘舵��
+                    if (TaskStepDetail.Status.COMPLETED.name().equals(step.getStatus())
+                            || TaskStepDetail.Status.FAILED.name().equals(step.getStatus())) {
+                        return;
+                    }
+                    // 杩涚墖澶ц溅璁惧锛氬彧鏈夊湪鐪熸寮�濮嬪鐞嗘椂鎵嶈缃负RUNNING
+                    // 鍏堟鏌ュ崸杞珛璁惧鏄惁宸插畬鎴愶紝濡傛灉杩樺湪鎵ц涓紝涓嶅簲璇ュ紑濮嬪ぇ杞︾殑宸ヤ綔
+                    boolean transferCompleted = isTransferDeviceCompleted(task.getTaskId(), context);
+                    if (!transferCompleted) {
+                        // 鍗ц浆绔嬭繕鍦ㄦ墽琛屼腑锛岀瓑寰呭崸杞珛瀹屾垚
+                        if (!TaskStepDetail.Status.PENDING.name().equals(step.getStatus())) {
+                            step.setStatus(TaskStepDetail.Status.PENDING.name());
+                            step.setSuccessMessage("绛夊緟鍗ц浆绔嬪畬鎴�");
+                            if (step.getStartTime() == null) {
+                                step.setStartTime(new Date());
+                            }
+                            taskStepDetailMapper.updateById(step);
+                            notificationService.notifyStepUpdate(task.getTaskId(), step);
+                        }
+                        log.debug("鍗ц浆绔嬭繕鍦ㄦ墽琛屼腑锛岃繘鐗囧ぇ杞︾瓑寰�: taskId={}, deviceId={}", 
+                                task.getTaskId(), device.getId());
                         return;
                     }
                     
-                    // 濡傛灉鐜荤拑ID鏁伴噺娌℃湁鍙樺寲锛岃鏄庢病鏈夋柊鐨勭幓鐠冿紝缁х画绛夊緟
+                    // 妫�鏌ユ槸鍚︽湁鍗ц浆绔嬩富浣撳凡杈撳嚭銆佸噯澶囦笂澶ц溅鐨勭幓鐠冧俊鎭�
+                    List<String> readyGlassIds = getTransferReadyGlassIds(context);
+                    
+                    // 濡傛灉褰撳墠娌℃湁鏂扮殑鐜荤拑锛屼絾鍗ц浆绔嬪凡瀹屾垚锛屽彲浠ヨ疆璇ES浠诲姟/纭鐘舵��
+                    if (CollectionUtils.isEmpty(readyGlassIds)) {
+                        // 妫�鏌ユ槸鍚︽墍鏈夊垵濮嬬幓鐠冮兘宸茶杞�
+                        @SuppressWarnings("unchecked")
+                        List<String> initialGlassIds = (List<String>) context.getSharedData().get("initialGlassIds");
+                        List<String> loadedGlassIds = getLoadedGlassIds(context);
+                        
+                        if (initialGlassIds != null && !initialGlassIds.isEmpty()) {
+                            // 濡傛灉鎵�鏈夊垵濮嬬幓鐠冮兘宸茶杞斤紝璇存槑宸茬粡澶勭悊瀹岋紝涓嶅簲璇ュ啀鍙樺洖绛夊緟
+                            if (loadedGlassIds != null && !loadedGlassIds.isEmpty() 
+                                    && loadedGlassIds.containsAll(initialGlassIds)) {
+                                // 鎵�鏈夌幓鐠冮兘宸茶杞斤紝淇濇寔RUNNING鐘舵�侊紝缁х画杞MES浠诲姟/纭
+                                if (!TaskStepDetail.Status.RUNNING.name().equals(step.getStatus())) {
+                                    step.setStatus(TaskStepDetail.Status.RUNNING.name());
+                                    if (step.getStartTime() == null) {
+                                        step.setStartTime(new Date());
+                                    }
+                                    taskStepDetailMapper.updateById(step);
+                                    notificationService.notifyStepUpdate(task.getTaskId(), step);
+                                }
+                                pollMesForVehicle(task, step, device, context);
+                                return;
+                            }
+                        }
+                        
+                        // 濡傛灉澶ц溅宸茬粡瑁呰浇杩囩幓鐠冿紙RUNNING鐘舵�侊級锛岃疆璇ES浠诲姟/纭鐘舵��
+                        if (TaskStepDetail.Status.RUNNING.name().equals(step.getStatus())) {
+                            pollMesForVehicle(task, step, device, context);
+                        } else {
+                            // 濡傛灉杩樻病鏈夎杞借繃鐜荤拑锛岀瓑寰呭崸杞珛杈撳嚭
+                            if (!TaskStepDetail.Status.PENDING.name().equals(step.getStatus())) {
+                                step.setStatus(TaskStepDetail.Status.PENDING.name());
+                                step.setSuccessMessage("绛夊緟鍗ц浆绔嬭緭鍑虹幓鐠�");
+                                if (step.getStartTime() == null) {
+                                    step.setStartTime(new Date());
+                                }
+                                taskStepDetailMapper.updateById(step);
+                                notificationService.notifyStepUpdate(task.getTaskId(), step);
+                            }
+                        }
+                        return;
+                    }
+                    
+                    // 濡傛灉鐜荤拑ID鏁伴噺娌℃湁鍙樺寲锛岃鏄庢病鏈夋柊鐨勭幓鐠�
                     int currentCount = readyGlassIds.size();
                     if (currentCount == lastProcessedCount.get()) {
+                        // 鐜荤拑鏁伴噺娌℃湁鍙樺寲锛氭病鏈夋柊鐜荤拑锛屼絾浠嶉渶杞MES浠诲姟/纭锛岄伩鍏嶉敊杩嘙ES渚х殑鍙樺寲
+                        pollMesForVehicle(task, step, device, context);
                         log.debug("澶ц溅璁惧瀹氭椂鍣細鐜荤拑ID鏁伴噺鏈彉鍖栵紝缁х画绛夊緟: taskId={}, deviceId={}, count={}",
                                 task.getTaskId(), device.getId(), currentCount);
                         return;
                     }
                     
-                    log.info("杩涚墖澶ц溅璁惧瀹氭椂鍣ㄦ娴嬪埌鍗ц浆绔嬭緭鍑虹殑鐜荤拑淇℃伅: taskId={}, deviceId={}, glassCount={}",
+                        log.debug("杩涚墖澶ц溅璁惧瀹氭椂鍣ㄦ娴嬪埌鍗ц浆绔嬭緭鍑虹殑鐜荤拑淇℃伅: taskId={}, deviceId={}, glassCount={}",
                             task.getTaskId(), device.getId(), currentCount);
                     
                     // 妫�鏌ュ閲�
@@ -615,10 +771,23 @@
                         if (logicParams != null && !logicParams.isEmpty()) {
                             checkParams.put("_logicParams", logicParams);
                         }
-                        DevicePlcVO.OperationResult result = handler.execute(device, "feedGlass", checkParams);
+                        // 绗竴姝ワ細鍐欏叆澶ц溅涓婃枡璇锋眰
+                        DevicePlcVO.OperationResult feedResult = handler.execute(device, "feedGlass", checkParams);
                         
-                        if (Boolean.TRUE.equals(result.getSuccess())) {
-                            log.info("杩涚墖澶ц溅璁惧瀹氭椂鍣ㄦ墽琛屾垚鍔�: taskId={}, deviceId={}, glassCount={}",
+                        if (Boolean.TRUE.equals(feedResult.getSuccess())) {
+                            // 鐪熸寮�濮嬪鐞嗭紝璁剧疆涓篟UNNING
+                            deviceCoordinationService.syncDeviceStatus(device,
+                                    DeviceCoordinationService.DeviceStatus.RUNNING, context);
+                            // 姝ラ鐘舵�佷篃璁剧疆涓篟UNNING
+                            if (!TaskStepDetail.Status.RUNNING.name().equals(step.getStatus())) {
+                                step.setStatus(TaskStepDetail.Status.RUNNING.name());
+                                if (step.getStartTime() == null) {
+                                    step.setStartTime(new Date());
+                                }
+                                taskStepDetailMapper.updateById(step);
+                                notificationService.notifyStepUpdate(task.getTaskId(), step);
+                            }
+                            log.debug("杩涚墖澶ц溅璁惧瀹氭椂鍣ㄦ墽琛屾垚鍔�: taskId={}, deviceId={}, glassCount={}",
                                     task.getTaskId(), device.getId(), readyGlassIds.size());
                             // 灏嗗凡瑁呰浇鐨勭幓鐠僆D淇濆瓨鍒板叡浜暟鎹腑锛堜緵澶х悊鐗囩浣跨敤锛�
                             setLoadedGlassIds(context, new ArrayList<>(readyGlassIds));
@@ -627,20 +796,101 @@
                             lastProcessedCount.set(0);
                             // 纭繚鍗ц浆绔嬫壂鐮佺户缁繍琛�
                             setScannerPause(context, false);
+                            
+                            // feedGlass鎴愬姛鍚庯紝鍏堟鏌ES浠诲姟锛坈heckMesTask锛夋潵寮�濮嬫墽琛屼换鍔�
+                            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());
+                                }
+                            } catch (Exception e) {
+                                log.warn("杩涚墖澶ц溅璁惧妫�鏌ES浠诲姟寮傚父: taskId={}, deviceId={}, error={}",
+                                        task.getTaskId(), device.getId(), e.getMessage());
+                            }
                         } else {
+                            // 瑁呬笉涓嬶紝淇濇寔WAITING鐘舵�佸拰PENDING姝ラ鐘舵��
+                            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);
+                            }
                             // 瑁呬笉涓嬶紝璁板綍瀹归噺涓嶈冻锛堟槸鍚﹂渶瑕佸奖鍝嶆壂鐮佺敱宸ヨ壓鍐嶅喅瀹氾級
                             log.warn("杩涚墖澶ц溅璁惧瀹氭椂鍣ㄥ閲忎笉瓒�: taskId={}, deviceId={}, message={}",
-                                    task.getTaskId(), device.getId(), result.getMessage());
+                                    task.getTaskId(), device.getId(), feedResult.getMessage());
                             lastProcessedCount.set(currentCount); // 璁板綍褰撳墠鏁伴噺锛岄伩鍏嶉噸澶嶆鏌�
                         }
                         
-                        // 鏇存柊姝ラ鐘舵��
-                        updateStepStatus(step, result);
-                        boolean opSuccess = Boolean.TRUE.equals(result.getSuccess());
-                        updateTaskProgress(task, step.getStepOrder(), opSuccess);
-                        if (!opSuccess) {
-                            deviceCoordinationService.syncDeviceStatus(device,
-                                    DeviceCoordinationService.DeviceStatus.FAILED, context);
+                        // 绗簩姝ワ細妫�鏌ES纭鐘舵�侊紙濡傛灉澶ц溅澶勭悊鍣ㄦ敮鎸佺殑璇濓級
+                        // 鍙湁鍦ㄤ换鍔″凡寮�濮嬫墽琛岋紙鏈変换鍔¤褰曪級鏃舵墠妫�鏌ES纭
+                        DevicePlcVO.OperationResult mesResult = null;
+                        try {
+                            mesResult = handler.execute(device, "checkMesConfirm", Collections.emptyMap());
+                        } catch (Exception e) {
+                            log.warn("杩涚墖澶ц溅璁惧妫�鏌ES纭鐘舵�佸紓甯�: taskId={}, deviceId={}, error={}",
+                                    task.getTaskId(), device.getId(), e.getMessage());
+                        }
+                        
+                        // 濡傛灉MES宸茬‘璁ゅ畬鎴愶紙mesConfirm=1锛夛紝妫�鏌ュ崸杞珛璁惧鐘舵�佸拰鐜荤拑淇℃伅
+                        // 濡傛灉鍗ц浆绔嬪凡瀹屾垚涓旀墍鏈夌幓鐠冮兘宸茶杞斤紝鍙互鏍囪涓哄畬鎴�
+                        if (mesResult != null && mesResult.getData() != null) {
+                            Object completedFlag = mesResult.getData().get("completed");
+                            boolean mesConfirmed = false;
+                            if (completedFlag instanceof Boolean) {
+                                mesConfirmed = (Boolean) completedFlag;
+                            } else if (completedFlag != null) {
+                                mesConfirmed = "true".equalsIgnoreCase(String.valueOf(completedFlag));
+                            }
+                            
+                            if (mesConfirmed) {
+                                // MES宸茬‘璁ゅ畬鎴愶紝妫�鏌ュ崸杞珛璁惧鏄惁宸插畬鎴�
+                                boolean transferCompletedForMes = isTransferDeviceCompleted(task.getTaskId(), context);
+                                if (transferCompletedForMes) {
+                                    // 妫�鏌ヤ换鍔′笂涓嬫枃涓殑鍒濆鐜荤拑ID鍜屽凡瑁呰浇鐨勭幓鐠僆D
+                                    @SuppressWarnings("unchecked")
+                                    List<String> initialGlassIds = (List<String>) context.getSharedData().get("initialGlassIds");
+                                    List<String> loadedGlassIds = getLoadedGlassIds(context);
+                                    
+                                    if (initialGlassIds != null && !initialGlassIds.isEmpty() 
+                                            && loadedGlassIds != null && !loadedGlassIds.isEmpty()) {
+                                        // 妫�鏌ユ槸鍚︽墍鏈夊垵濮嬬幓鐠冮兘宸茶杞�
+                                        boolean allGlassesLoaded = loadedGlassIds.containsAll(initialGlassIds);
+                                        if (allGlassesLoaded) {
+                                            // 鍗ц浆绔嬪凡瀹屾垚涓旀墍鏈夌幓鐠冮兘宸茶杞斤紝鏍囪涓哄畬鎴�
+                                            log.info("MES宸茬‘璁や笖鍗ц浆绔嬪凡瀹屾垚涓旀墍鏈夌幓鐠冨凡瑁呰浇锛屼换鍔¤嚜鍔ㄥ畬鎴�: taskId={}, deviceId={}, initialCount={}, loadedCount={}",
+                                                    task.getTaskId(), device.getId(), initialGlassIds.size(), loadedGlassIds.size());
+                                            // mesResult宸茬粡鍖呭惈completed=true锛屼笉闇�瑕佷慨鏀�
+                                        }
+                                    }
+                                } else {
+                                    // 鍗ц浆绔嬭繕鏈畬鎴愶紝涓嶅簲璇ユ爣璁颁负瀹屾垚
+                                    log.debug("MES宸茬‘璁や絾鍗ц浆绔嬫湭瀹屾垚锛岀瓑寰呭崸杞珛瀹屾垚: taskId={}, deviceId={}", 
+                                            task.getTaskId(), device.getId());
+                                }
+                            }
+                        }
+                        
+                        // 鏇存柊姝ラ鐘舵�侊紙澶ц溅璁惧淇濇寔RUNNING锛岀洿鍒癕ES纭瀹屾垚鎴栦换鍔″彇娑堬級
+                        if (mesResult != null) {
+                            updateStepStatusForVehicle(task.getTaskId(), step, mesResult);
+                            boolean opSuccess = Boolean.TRUE.equals(mesResult.getSuccess());
+                            updateTaskProgress(task, step.getStepOrder(), opSuccess);
+                            if (!opSuccess) {
+                                deviceCoordinationService.syncDeviceStatus(device,
+                                        DeviceCoordinationService.DeviceStatus.FAILED, context);
+                            }
+                        } else {
+                            updateStepStatusForVehicle(task.getTaskId(), step, feedResult);
+                            boolean opSuccess = Boolean.TRUE.equals(feedResult.getSuccess());
+                            updateTaskProgress(task, step.getStepOrder(), opSuccess);
+                            if (!opSuccess) {
+                                deviceCoordinationService.syncDeviceStatus(device,
+                                        DeviceCoordinationService.DeviceStatus.FAILED, context);
+                            }
                         }
                     }
                 } catch (Exception e) {
@@ -648,8 +898,9 @@
                 }
             }, 0, MONITOR_INTERVAL_MS, TimeUnit.MILLISECONDS);
             
+            // 鍦ㄤ覆琛屾墽琛屾ā寮忎笅锛岃澶囧惎鍔ㄥ畾鏃跺櫒鏃跺厛璁剧疆涓� WAITING锛屽畾鏃跺櫒绗竴娆℃墽琛屾椂鍐嶈缃负 RUNNING
             deviceCoordinationService.syncDeviceStatus(device,
-                    DeviceCoordinationService.DeviceStatus.RUNNING, context);
+                    DeviceCoordinationService.DeviceStatus.WAITING, context);
             return future;
         } catch (Exception e) {
             log.error("鍚姩杩涚墖澶ц溅璁惧瀹氭椂鍣ㄥけ璐�: taskId={}, deviceId={}", task.getTaskId(), device.getId(), e);
@@ -667,27 +918,99 @@
         try {
             final long MONITOR_INTERVAL_MS = 2_000; // 2绉掔洃鎺т竴娆�
             
-            log.info("鍚姩鍑虹墖澶ц溅璁惧瀹氭椂鍣�: taskId={}, deviceId={}, interval={}s",
+            log.debug("鍚姩鍑虹墖澶ц溅璁惧瀹氭椂鍣�: taskId={}, deviceId={}, interval={}s",
                     task.getTaskId(), device.getId(), MONITOR_INTERVAL_MS / 1000);
             
             // 鍚姩瀹氭椂浠诲姟
             ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(() -> {
                 try {
                     if (isTaskCancelled(context)) {
-                        log.info("浠诲姟宸插彇娑堬紝鍋滄鍑虹墖澶ц溅瀹氭椂鍣�: taskId={}, deviceId={}",
+                        log.debug("浠诲姟宸插彇娑堬紝鍋滄鍑虹墖澶ц溅瀹氭椂鍣�: taskId={}, deviceId={}",
                                 task.getTaskId(), device.getId());
                         return;
                     }
+                    // 鍑虹墖澶ц溅璁惧锛氬彧鏈夊湪鐪熸寮�濮嬪鐞嗘椂鎵嶈缃负RUNNING
                     // 妫�鏌ユ槸鍚︽湁宸插鐞嗙殑鐜荤拑淇℃伅锛堜粠澶х悊鐗囩鏉ョ殑锛�
                     List<String> processedGlassIds = getProcessedGlassIds(context);
+                    boolean isRunning = TaskStepDetail.Status.RUNNING.name().equals(step.getStatus());
+                    
+                    // 濡傛灉娌℃湁宸插鐞嗙幓鐠冿紝鍒欎笉搴斾富鍔ㄦ妸姝ラ鎷夊埌RUNNING锛屽彧淇濇寔宸茶繍琛岀姸鎬�
                     if (CollectionUtils.isEmpty(processedGlassIds)) {
-                        log.debug("鍑虹墖澶ц溅璁惧瀹氭椂鍣細鏆傛棤宸插鐞嗙殑鐜荤拑淇℃伅: taskId={}, deviceId={}",
-                                task.getTaskId(), device.getId());
+                        if (isRunning) {
+                            // 宸茬粡鍦ㄨ繍琛岀殑鎯呭喌涓嬶紝缁х画杞MES浠诲姟/纭锛岄伩鍏嶉敊杩囩‘璁�
+                            DeviceLogicHandler handler = handlerFactory.getHandler(device.getDeviceType());
+                            if (handler != null) {
+                                Map<String, Object> logicParams = parseLogicParams(device);
+                                
+                                // 鍏堟鏌ES浠诲姟锛堝鏋渕esSend=1锛屼細鍒涘缓浠诲姟骞跺紑濮嬫墽琛岋級
+                                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());
+                                    }
+                                } catch (Exception e) {
+                                    log.warn("鍑虹墖澶ц溅璁惧妫�鏌ES浠诲姟寮傚父: taskId={}, deviceId={}, error={}",
+                                            task.getTaskId(), device.getId(), e.getMessage());
+                                }
+                                
+                                // 鐒跺悗妫�鏌ES纭鐘舵�侊紙鍙湁鍦ㄤ换鍔″凡寮�濮嬫墽琛屾椂鎵嶆鏌ワ級
+                                DevicePlcVO.OperationResult mesResult = null;
+                                try {
+                                    mesResult = handler.execute(device, "checkMesConfirm", Collections.emptyMap());
+                                } catch (Exception e) {
+                                    log.warn("鍑虹墖澶ц溅璁惧妫�鏌ES纭鐘舵�佸紓甯�: taskId={}, deviceId={}, error={}",
+                                            task.getTaskId(), device.getId(), e.getMessage());
+                                }
+                                
+                                // 鏇存柊姝ラ鐘舵�侊紙澶ц溅璁惧淇濇寔RUNNING锛岀洿鍒癕ES纭瀹屾垚鎴栦换鍔″彇娑堬級
+                                if (mesResult != null) {
+                                    updateStepStatusForVehicle(task.getTaskId(), step, mesResult);
+                                    boolean opSuccess = Boolean.TRUE.equals(mesResult.getSuccess());
+                                    updateTaskProgress(task, step.getStepOrder(), opSuccess);
+                                    if (!opSuccess) {
+                                        deviceCoordinationService.syncDeviceStatus(device,
+                                                DeviceCoordinationService.DeviceStatus.FAILED, context);
+                                    }
+                                }
+                            }
+                        } else {
+                            // 鏈繍琛屼笖娌℃湁宸插鐞嗙幓鐠冿紝淇濇寔PENDING
+                            if (!TaskStepDetail.Status.PENDING.name().equals(step.getStatus())
+                                    && !TaskStepDetail.Status.COMPLETED.name().equals(step.getStatus())) {
+                                step.setStatus(TaskStepDetail.Status.PENDING.name());
+                                step.setSuccessMessage("绛夊緟澶х悊鐗囩澶勭悊瀹屾垚");
+                                taskStepDetailMapper.updateById(step);
+                                notificationService.notifyStepUpdate(task.getTaskId(), step);
+                            }
+                            log.debug("鍑虹墖澶ц溅璁惧瀹氭椂鍣細鏆傛棤宸插鐞嗙殑鐜荤拑淇℃伅: taskId={}, deviceId={}",
+                                    task.getTaskId(), device.getId());
+                        }
                         return;
                     }
                     
-                    log.info("鍑虹墖澶ц溅璁惧瀹氭椂鍣ㄦ娴嬪埌宸插鐞嗙殑鐜荤拑淇℃伅: taskId={}, deviceId={}, glassCount={}",
+                    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);
+                        }
+                        log.debug("鍑虹墖澶ц溅绛夊緟澶х悊鐗囩瀹屾垚鍏ㄩ儴鐜荤拑: taskId={}, deviceId={}, processed={}, initial={}",
+                                task.getTaskId(), device.getId(), processedGlassIds.size(), initialGlassIds.size());
+                        return;
+                    }
                     
                     // 鎵ц鍑虹墖鎿嶄綔
                     Map<String, Object> checkParams = new HashMap<>();
@@ -701,25 +1024,80 @@
                         if (logicParams != null && !logicParams.isEmpty()) {
                             checkParams.put("_logicParams", logicParams);
                         }
-                        DevicePlcVO.OperationResult result = handler.execute(device, "feedGlass", checkParams);
+                        // 绗竴姝ワ細鍐欏叆澶ц溅鍑虹墖璇锋眰
+                        DevicePlcVO.OperationResult feedResult = handler.execute(device, "feedGlass", checkParams);
                         
-                        if (Boolean.TRUE.equals(result.getSuccess())) {
-                            log.info("鍑虹墖澶ц溅璁惧瀹氭椂鍣ㄦ墽琛屾垚鍔�: taskId={}, deviceId={}, glassCount={}",
+                        if (Boolean.TRUE.equals(feedResult.getSuccess())) {
+                            // 鐪熸寮�濮嬪鐞嗭紝璁剧疆涓篟UNNING
+                            deviceCoordinationService.syncDeviceStatus(device,
+                                    DeviceCoordinationService.DeviceStatus.RUNNING, context);
+                            // 姝ラ鐘舵�佷篃璁剧疆涓篟UNNING
+                            if (!TaskStepDetail.Status.RUNNING.name().equals(step.getStatus())) {
+                                step.setStatus(TaskStepDetail.Status.RUNNING.name());
+                                if (step.getStartTime() == null) {
+                                    step.setStartTime(new Date());
+                                }
+                                taskStepDetailMapper.updateById(step);
+                                notificationService.notifyStepUpdate(task.getTaskId(), step);
+                            }
+                            log.debug("鍑虹墖澶ц溅璁惧瀹氭椂鍣ㄦ墽琛屾垚鍔�: taskId={}, deviceId={}, glassCount={}",
                                     task.getTaskId(), device.getId(), processedGlassIds.size());
                             // 娓呯┖宸插鐞嗙殑鐜荤拑ID鍒楄〃锛堝凡澶勭悊锛�
                             clearProcessedGlassIds(context);
+                            
+                            // feedGlass鎴愬姛鍚庯紝鍏堟鏌ES浠诲姟锛坈heckMesTask锛夋潵寮�濮嬫墽琛屼换鍔�
+                            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());
+                                }
+                            } catch (Exception e) {
+                                log.warn("鍑虹墖澶ц溅璁惧妫�鏌ES浠诲姟寮傚父: taskId={}, deviceId={}, error={}",
+                                        task.getTaskId(), device.getId(), e.getMessage());
+                            }
                         } else {
+                            // 娌℃湁鏁版嵁锛屼繚鎸乄AITING鐘舵�佸拰PENDING姝ラ鐘舵��
+                            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);
+                            }
                             log.debug("鍑虹墖澶ц溅璁惧瀹氭椂鍣ㄦ墽琛屽け璐�: taskId={}, deviceId={}, message={}",
-                                    task.getTaskId(), device.getId(), result.getMessage());
+                                    task.getTaskId(), device.getId(), feedResult.getMessage());
                         }
                         
-                        // 鏇存柊姝ラ鐘舵��
-                        updateStepStatus(step, result);
-                        boolean opSuccess = Boolean.TRUE.equals(result.getSuccess());
-                        updateTaskProgress(task, step.getStepOrder(), opSuccess);
-                        if (!opSuccess) {
-                            deviceCoordinationService.syncDeviceStatus(device,
-                                    DeviceCoordinationService.DeviceStatus.FAILED, context);
+                        // 绗簩姝ワ細妫�鏌ES纭鐘舵�侊紙濡傛灉澶ц溅澶勭悊鍣ㄦ敮鎸佺殑璇濓級
+                        // 鍙湁鍦ㄤ换鍔″凡寮�濮嬫墽琛岋紙鏈変换鍔¤褰曪級鏃舵墠妫�鏌ES纭
+                        DevicePlcVO.OperationResult mesResult = null;
+                        try {
+                            mesResult = handler.execute(device, "checkMesConfirm", Collections.emptyMap());
+                        } catch (Exception e) {
+                            log.warn("鍑虹墖澶ц溅璁惧妫�鏌ES纭鐘舵�佸紓甯�: taskId={}, deviceId={}, error={}",
+                                    task.getTaskId(), device.getId(), e.getMessage());
+                        }
+                        
+                        // 鏇存柊姝ラ鐘舵�侊紙澶ц溅璁惧淇濇寔RUNNING锛岀洿鍒癕ES纭瀹屾垚鎴栦换鍔″彇娑堬級
+                        if (mesResult != null) {
+                            updateStepStatusForVehicle(task.getTaskId(), step, mesResult);
+                            boolean opSuccess = Boolean.TRUE.equals(mesResult.getSuccess());
+                            updateTaskProgress(task, step.getStepOrder(), opSuccess);
+                            if (!opSuccess) {
+                                deviceCoordinationService.syncDeviceStatus(device,
+                                        DeviceCoordinationService.DeviceStatus.FAILED, context);
+                            }
+                        } else {
+                            updateStepStatusForVehicle(task.getTaskId(), step, feedResult);
+                            boolean opSuccess = Boolean.TRUE.equals(feedResult.getSuccess());
+                            updateTaskProgress(task, step.getStepOrder(), opSuccess);
+                            if (!opSuccess) {
+                                deviceCoordinationService.syncDeviceStatus(device,
+                                        DeviceCoordinationService.DeviceStatus.FAILED, context);
+                            }
                         }
                     }
                 } catch (Exception e) {
@@ -727,8 +1105,9 @@
                 }
             }, 0, MONITOR_INTERVAL_MS, TimeUnit.MILLISECONDS);
             
+            // 鍦ㄤ覆琛屾墽琛屾ā寮忎笅锛岃澶囧惎鍔ㄥ畾鏃跺櫒鏃跺厛璁剧疆涓� WAITING锛屽畾鏃跺櫒绗竴娆℃墽琛屾椂鍐嶈缃负 RUNNING
             deviceCoordinationService.syncDeviceStatus(device,
-                    DeviceCoordinationService.DeviceStatus.RUNNING, context);
+                    DeviceCoordinationService.DeviceStatus.WAITING, context);
             return future;
         } catch (Exception e) {
             log.error("鍚姩鍑虹墖澶ц溅璁惧瀹氭椂鍣ㄥけ璐�: taskId={}, deviceId={}", task.getTaskId(), device.getId(), e);
@@ -749,31 +1128,88 @@
             Integer processTimeSeconds = getLogicParam(logicParams, "processTimeSeconds", 30);
             final long PROCESS_TIME_MS = processTimeSeconds * 1000;
             
-            log.info("鍚姩澶х悊鐗囩璁惧瀹氭椂鍣�: taskId={}, deviceId={}, processTime={}s",
+            log.debug("鍚姩澶х悊鐗囩璁惧瀹氭椂鍣�: taskId={}, deviceId={}, processTime={}s",
                     task.getTaskId(), device.getId(), processTimeSeconds);
             
             // 鍚姩瀹氭椂浠诲姟
             ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(() -> {
                 try {
                     if (isTaskCancelled(context)) {
-                        log.info("浠诲姟宸插彇娑堬紝鍋滄澶х悊鐗囩瀹氭椂鍣�: taskId={}, deviceId={}",
+                        log.debug("浠诲姟宸插彇娑堬紝鍋滄澶х悊鐗囩瀹氭椂鍣�: taskId={}, deviceId={}",
                                 task.getTaskId(), device.getId());
                         return;
                     }
+                    // 濡傛灉姝ラ宸茬粡瀹屾垚锛屼笉鍐嶅鐞�
+                    if (TaskStepDetail.Status.COMPLETED.name().equals(step.getStatus())) {
+                        // 妫�鏌ユ槸鍚︽墍鏈夊垵濮嬬幓鐠冮兘宸插鐞嗗畬
+                        @SuppressWarnings("unchecked")
+                        List<String> initialGlassIds = (List<String>) context.getSharedData().get("initialGlassIds");
+                        List<String> processedGlassIds = getProcessedGlassIds(context);
+                        
+                        if (initialGlassIds != null && !initialGlassIds.isEmpty() 
+                                && processedGlassIds != null && !processedGlassIds.isEmpty()
+                                && processedGlassIds.containsAll(initialGlassIds)) {
+                            // 鎵�鏈夌幓鐠冮兘宸插鐞嗗畬锛屼繚鎸佸畬鎴愮姸鎬�
+                            log.debug("澶х悊鐗囩璁惧宸插畬鎴愪笖鎵�鏈夌幓鐠冨凡澶勭悊: taskId={}, deviceId={}, initialCount={}, processedCount={}",
+                                    task.getTaskId(), device.getId(), initialGlassIds.size(), processedGlassIds.size());
+                            return;
+                        }
+                    }
+                    
+                    // 澶х悊鐗囩璁惧锛氬彧鏈夊湪鐪熸寮�濮嬪鐞嗘椂鎵嶈缃负RUNNING
                     // 妫�鏌ユ槸鍚︽湁宸茶杞界殑鐜荤拑淇℃伅锛堜粠杩涚墖澶ц溅鏉ョ殑锛�
                     List<String> loadedGlassIds = getLoadedGlassIds(context);
                     if (CollectionUtils.isEmpty(loadedGlassIds)) {
+                        // 娌℃湁鏁版嵁锛屾鏌ユ槸鍚︽墍鏈夌幓鐠冮兘宸插鐞嗗畬
+                        @SuppressWarnings("unchecked")
+                        List<String> initialGlassIds = (List<String>) context.getSharedData().get("initialGlassIds");
+                        List<String> processedGlassIds = getProcessedGlassIds(context);
+                        
+                        if (initialGlassIds != null && !initialGlassIds.isEmpty() 
+                                && processedGlassIds != null && !processedGlassIds.isEmpty()
+                                && processedGlassIds.containsAll(initialGlassIds)) {
+                            // 鎵�鏈夌幓鐠冮兘宸插鐞嗗畬锛屾爣璁颁负瀹屾垚
+                            if (!TaskStepDetail.Status.COMPLETED.name().equals(step.getStatus())) {
+                                step.setStatus(TaskStepDetail.Status.COMPLETED.name());
+                                step.setSuccessMessage("鎵�鏈夌幓鐠冨凡澶勭悊瀹屾垚");
+                                if (step.getEndTime() == null) {
+                                    step.setEndTime(new Date());
+                                }
+                                if (step.getStartTime() != null && step.getEndTime() != null) {
+                                    step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
+                                }
+                                taskStepDetailMapper.updateById(step);
+                                notificationService.notifyStepUpdate(task.getTaskId(), step);
+                                checkAndCompleteTaskIfDone(step.getTaskId());
+                            }
+                            return;
+                        }
+                        
+                        // 娌℃湁鏁版嵁涓旀湭瀹屾垚锛屼繚鎸乄AITING鐘舵�佸拰PENDING姝ラ鐘舵��
+                        deviceCoordinationService.syncDeviceStatus(device,
+                                DeviceCoordinationService.DeviceStatus.WAITING, context);
+                        if (!TaskStepDetail.Status.PENDING.name().equals(step.getStatus())
+                                && !TaskStepDetail.Status.COMPLETED.name().equals(step.getStatus())) {
+                            step.setStatus(TaskStepDetail.Status.PENDING.name());
+                            step.setSuccessMessage("绛夊緟杩涚墖澶ц溅瑁呰浇鐜荤拑");
+                            taskStepDetailMapper.updateById(step);
+                            notificationService.notifyStepUpdate(task.getTaskId(), step);
+                        }
                         log.debug("澶х悊鐗囩璁惧瀹氭椂鍣細鏆傛棤宸茶杞界殑鐜荤拑淇℃伅: taskId={}, deviceId={}",
                                 task.getTaskId(), device.getId());
                         return;
                     }
+                    
+                    // 鏈夋暟鎹紝璁剧疆涓篟UNNING
+                    deviceCoordinationService.syncDeviceStatus(device,
+                            DeviceCoordinationService.DeviceStatus.RUNNING, context);
                     
                     // 妫�鏌ョ幓鐠冩槸鍚﹀凡缁忓鐞嗗畬鎴愶紙閫氳繃澶勭悊鏃堕棿鍒ゆ柇锛�
                     Long processStartTime = getProcessStartTime(context);
                     if (processStartTime == null) {
                         // 绗竴娆℃娴嬪埌鐜荤拑锛岃褰曞紑濮嬪鐞嗘椂闂�
                         setProcessStartTime(context, System.currentTimeMillis());
-                        log.info("澶х悊鐗囩璁惧寮�濮嬪鐞�: taskId={}, deviceId={}, glassCount={}, processTime={}s",
+                        log.debug("澶х悊鐗囩璁惧寮�濮嬪鐞�: taskId={}, deviceId={}, glassCount={}, processTime={}s",
                                 task.getTaskId(), device.getId(), loadedGlassIds.size(), processTimeSeconds);
                         return;
                     }
@@ -787,7 +1223,7 @@
                     }
                     
                     // 澶勭悊鏃堕棿宸插埌锛屽畬鎴愪换鍔℃眹鎶�
-                    log.info("澶х悊鐗囩璁惧澶勭悊瀹屾垚: taskId={}, deviceId={}, glassCount={}, processTime={}s",
+                    log.debug("澶х悊鐗囩璁惧澶勭悊瀹屾垚: taskId={}, deviceId={}, glassCount={}, processTime={}s",
                             task.getTaskId(), device.getId(), loadedGlassIds.size(), processTimeSeconds);
                     
                     // 灏嗗凡澶勭悊鐨勭幓鐠僆D杞Щ鍒板凡澶勭悊鍒楄〃锛堜緵鍑虹墖澶ц溅浣跨敤锛�
@@ -800,12 +1236,17 @@
                     step.setErrorMessage(null);
                     step.setOutputData(toJson(Collections.singletonMap("glassIds", loadedGlassIds)));
                     taskStepDetailMapper.updateById(step);
+                    // 澶х悊鐗囩瀹屾垚鍚庡皾璇曡嚜鍔ㄦ敹灏炬暣涓换鍔�
+                    checkAndCompleteTaskIfDone(step.getTaskId());
                     
                 } catch (Exception e) {
                     log.error("澶х悊鐗囩璁惧瀹氭椂鍣ㄦ墽琛屽紓甯�: taskId={}, deviceId={}", task.getTaskId(), device.getId(), e);
                 }
             }, 0, 1_000, TimeUnit.MILLISECONDS); // 姣忕妫�鏌ヤ竴娆�
             
+            // 鍦ㄤ覆琛屾墽琛屾ā寮忎笅锛岃澶囧惎鍔ㄥ畾鏃跺櫒鏃跺厛璁剧疆涓� WAITING锛屽畾鏃跺櫒绗竴娆℃墽琛屾椂鍐嶈缃负 RUNNING
+            deviceCoordinationService.syncDeviceStatus(device,
+                    DeviceCoordinationService.DeviceStatus.WAITING, context);
             return future;
         } catch (Exception e) {
             log.error("鍚姩澶х悊鐗囩璁惧瀹氭椂鍣ㄥけ璐�: taskId={}, deviceId={}", task.getTaskId(), device.getId(), e);
@@ -852,7 +1293,14 @@
      */
     private void setLoadedGlassIds(TaskExecutionContext context, List<String> glassIds) {
         if (context != null) {
-            context.getSharedData().put("loadedGlassIds", new ArrayList<>(glassIds));
+            // 绱姞璁板綍锛岄伩鍏嶅悗缁� containsAll 鍒ゆ柇鍥犺鐩栦涪澶卞巻鍙茬幓鐠冭�屽洖閫�涓虹瓑寰�
+            List<String> merged = new ArrayList<>(getLoadedGlassIds(context)); // 纭繚鍙彉
+            if (glassIds != null) {
+                merged.addAll(glassIds);
+            }
+            // 鍘婚噸
+            List<String> distinct = merged.stream().distinct().collect(java.util.stream.Collectors.toList());
+            context.getSharedData().put("loadedGlassIds", distinct);
         }
     }
     
@@ -1024,7 +1472,7 @@
                     future.cancel(false);
                 }
             }
-            log.info("宸插仠姝换鍔$殑鎵�鏈夊畾鏃跺櫒: taskId={}, count={}", taskId, futures.size());
+            log.debug("宸插仠姝换鍔$殑鎵�鏈夊畾鏃跺櫒: taskId={}, count={}", taskId, futures.size());
         }
         runningTaskContexts.remove(taskId);
     }
@@ -1040,7 +1488,7 @@
         long timeoutMs = timeoutMinutes * 60 * 1000;
         long deadline = System.currentTimeMillis() + timeoutMs;
         
-        log.info("绛夊緟瀹氭椂鍣ㄤ换鍔″畬鎴�: taskId={}, timeout={}鍒嗛挓", taskId, timeoutMinutes);
+        log.debug("绛夊緟瀹氭椂鍣ㄤ换鍔″畬鎴�: taskId={}, timeout={}鍒嗛挓", taskId, timeoutMinutes);
         
         while (System.currentTimeMillis() < deadline) {
             List<ScheduledFuture<?>> futures = taskScheduledTasks.get(taskId);
@@ -1069,7 +1517,54 @@
             }
         }
         
-        log.info("瀹氭椂鍣ㄤ换鍔$瓑寰呭畬鎴�: taskId={}", taskId);
+        log.debug("瀹氭椂鍣ㄤ换鍔$瓑寰呭畬鎴�: taskId={}", taskId);
+    }
+    
+    /**
+     * 褰撴煇涓楠ゅ彲鑳藉畬鎴愭椂锛屾鏌ヤ换鍔℃槸鍚︽墍鏈夋楠ら兘宸插畬鎴愶紝濡傛灉鏄垯鑷姩灏嗕换鍔℃爣璁颁负宸插畬鎴�
+     */
+    private void checkAndCompleteTaskIfDone(String taskId) {
+        if (taskId == null) {
+            return;
+        }
+        try {
+            MultiDeviceTask task = multiDeviceTaskMapper.selectOne(
+                    Wrappers.<MultiDeviceTask>lambdaQuery()
+                            .eq(MultiDeviceTask::getTaskId, taskId)
+            );
+            if (task == null) {
+                return;
+            }
+            // 浠呭湪浠诲姟浠嶄负RUNNING鏃舵墠灏濊瘯鑷姩鏀跺熬
+            if (!MultiDeviceTask.Status.RUNNING.name().equals(task.getStatus())) {
+                return;
+            }
+            
+            int totalSteps = task.getTotalSteps() != null ? task.getTotalSteps() : 0;
+            if (totalSteps <= 0) {
+                return;
+            }
+            
+            int completedSteps = countCompletedSteps(taskId);
+            if (completedSteps < totalSteps) {
+                return;
+            }
+            
+            // 鎵�鏈夋楠ら兘宸插畬鎴愶紝鏀跺熬浠诲姟
+            task.setStatus(MultiDeviceTask.Status.COMPLETED.name());
+            task.setEndTime(new Date());
+            multiDeviceTaskMapper.updateById(task);
+            
+            // 鍋滄鎵�鏈夊畾鏃跺櫒
+            stopScheduledTasks(taskId);
+            
+            // 閫氱煡浠诲姟瀹屾垚
+            notificationService.notifyTaskStatus(task);
+            
+            log.info("鎵�鏈夋楠ゅ凡瀹屾垚锛岃嚜鍔ㄥ皢浠诲姟鏍囪涓哄凡瀹屾垚: taskId={}, totalSteps={}", taskId, totalSteps);
+        } catch (Exception e) {
+            log.warn("妫�鏌ュ苟鑷姩瀹屾垚浠诲姟澶辫触: taskId={}", taskId, e);
+        }
     }
     
     /**
@@ -1102,35 +1597,327 @@
         }
         step.setOutputData(toJson(result));
         taskStepDetailMapper.updateById(step);
+        if (StringUtils.hasText(step.getTaskId())) {
+            notificationService.notifyStepUpdate(step.getTaskId(), step);
+        }
+    }
+    
+    /**
+     * 纭繚姝ラ杩涘叆RUNNING鐘舵�侊紙浠呭湪绗竴娆$湡姝f墽琛屽墠璋冪敤锛�
+     */
+    private void ensureStepRunning(TaskStepDetail step, String taskId) {
+        if (step == null) {
+            return;
+        }
+        if (!TaskStepDetail.Status.RUNNING.name().equals(step.getStatus())) {
+            step.setStatus(TaskStepDetail.Status.RUNNING.name());
+            if (step.getStartTime() == null) {
+                step.setStartTime(new Date());
+            }
+            taskStepDetailMapper.updateById(step);
+            notificationService.notifyStepUpdate(taskId, step);
+        }
+    }
+    
+    /**
+     * 鏇存柊鎵爜璁惧姝ラ鐘舵�侊紙鏄剧ず杩涘害锛屼繚鎸丷UNNING鐘舵�佺洿鍒版墍鏈夌幓鐠冨鐞嗗畬鎴愶級
+     */
+    private void updateStepStatusForScanner(TaskStepDetail step, DevicePlcVO.OperationResult result,
+                                            int currentIndex, int totalCount,
+                                            int successCount, int failCount) {
+        if (step == null || result == null) {
+            return;
+        }
+        
+        boolean success = Boolean.TRUE.equals(result.getSuccess());
+        
+        // 淇濇寔RUNNING鐘舵�侊紝鐩村埌鎵�鏈夌幓鐠冨鐞嗗畬鎴愶紙鍦ㄥ畾鏃跺櫒瀹屾垚鏃跺啀璁剧疆涓篊OMPLETED锛�
+        step.setStatus(TaskStepDetail.Status.RUNNING.name());
+        
+        // 鏇存柊鏃堕棿鍜岃�楁椂锛屽墠绔彲浠ュ疄鏃剁湅鍒版墽琛岃�楁椂
+        Date now = new Date();
+        if (step.getStartTime() == null) {
+            step.setStartTime(now);
+        }
+        if (step.getStartTime() != null) {
+            step.setDurationMs(now.getTime() - step.getStartTime().getTime());
+        }
+        
+        // 鏇存柊杩涘害淇℃伅
+        String progressMessage = String.format("姝e湪澶勭悊 %d/%d (鎴愬姛:%d, 澶辫触:%d)", 
+                currentIndex, totalCount, successCount, failCount);
+        
+        if (success) {
+            // 鎴愬姛鏃舵樉绀鸿繘搴﹀拰鎴愬姛娑堟伅
+            String resultMessage = result.getMessage();
+            if (StringUtils.hasText(resultMessage)) {
+                step.setSuccessMessage(progressMessage + " - " + resultMessage);
+            } else {
+                step.setSuccessMessage(progressMessage);
+            }
+            step.setErrorMessage(null);
+        } else {
+            // 澶辫触鏃舵樉绀鸿繘搴﹀拰閿欒娑堟伅
+            String errorMessage = result.getMessage();
+            step.setErrorMessage(progressMessage + " - " + (StringUtils.hasText(errorMessage) ? errorMessage : "澶勭悊澶辫触"));
+            step.setSuccessMessage(null);
+        }
+        
+        step.setOutputData(toJson(result));
+        taskStepDetailMapper.updateById(step);
+    }
+    
+    /**
+     * 鏇存柊澶ц溅璁惧姝ラ鐘舵�侊紙淇濇寔RUNNING锛岀洿鍒版墜鍔ㄥ仠姝㈡垨浠诲姟鍙栨秷锛涘け璐ユ椂鏍囪涓篎AILED锛�
+     */
+    private void updateStepStatusForVehicle(String taskId, TaskStepDetail step, DevicePlcVO.OperationResult result) {
+        if (step == null || result == null) {
+            return;
+        }
+        // 濡傛灉姝ラ宸茬粡澶勪簬瀹屾垚鎴栧け璐ョ姸鎬侊紝鍒欎笉鍐嶈閲嶅鏇存柊锛堥槻姝㈢姸鎬佸弽澶嶅垏鎹級
+        if (TaskStepDetail.Status.COMPLETED.name().equals(step.getStatus())
+                || TaskStepDetail.Status.FAILED.name().equals(step.getStatus())) {
+            log.debug("姝ラ宸插畬鎴愭垨澶辫触锛屼笉鍐嶆洿鏂扮姸鎬�: stepId={}, status={}", step.getId(), step.getStatus());
+            return;
+        }
+        boolean success = Boolean.TRUE.equals(result.getSuccess());
+        boolean completed = false;
+        if (result.getData() != null && result.getData().get("completed") != null) {
+            Object flag = result.getData().get("completed");
+            if (flag instanceof Boolean) {
+                completed = (Boolean) flag;
+            } else {
+                completed = "true".equalsIgnoreCase(String.valueOf(flag));
+            }
+        }
+        Date now = new Date();
+
+        // 鍒濆鍖栧紑濮嬫椂闂�
+        if (step.getStartTime() == null) {
+            step.setStartTime(now);
+        }
+
+        boolean waiting = false;
+        String waitingReason = null;
+        if (result.getData() != null) {
+            Object waitingFlag = result.getData().get("waiting");
+            if (waitingFlag instanceof Boolean) {
+                waiting = (Boolean) waitingFlag;
+            } else if (waitingFlag != null) {
+                waiting = "true".equalsIgnoreCase(String.valueOf(waitingFlag));
+            }
+            Object reason = result.getData().get("waitingReason");
+            if (reason != null) {
+                waitingReason = String.valueOf(reason);
+            }
+        }
+        
+        if (success && !completed) {
+            // 鎴愬姛浣嗘湭瀹屾垚锛氭牴鎹畐aiting鐘舵�佸喅瀹氭樉绀轰负绛夊緟杩樻槸鎵ц涓�
+            // 娉ㄦ剰锛氬鏋滄楠ゅ凡缁忔槸RUNNING鐘舵�侊紙璇存槑宸茬粡瑁呰浇杩囩幓鐠冿級锛屼笉搴旇鏀瑰洖PENDING
+            boolean isAlreadyRunning = TaskStepDetail.Status.RUNNING.name().equals(step.getStatus());
+            if (waiting && !isAlreadyRunning) {
+                // 鍙湁鍦ㄨ繕娌℃湁寮�濮嬭繍琛屾椂锛屾墠璁剧疆涓篜ENDING
+                step.setStatus(TaskStepDetail.Status.PENDING.name());
+            } else {
+                // 濡傛灉宸茬粡杩愯杩囷紝鎴栬�呬笉鏄瓑寰呯姸鎬侊紝淇濇寔鎴栬缃负RUNNING
+                step.setStatus(TaskStepDetail.Status.RUNNING.name());
+            }
+            String message = result.getMessage();
+            if (!StringUtils.hasText(message) && waiting) {
+                message = "澶ц溅璁惧绛夊緟涓�" + (StringUtils.hasText(waitingReason) ? "锛�" + waitingReason + "锛�" : "");
+            }
+            step.setSuccessMessage(StringUtils.hasText(message) ? message : (waiting && !isAlreadyRunning ? "澶ц溅璁惧绛夊緟涓�" : "澶ц溅璁惧杩愯涓�"));
+            step.setErrorMessage(null);
+            if (step.getStartTime() != null) {
+                step.setDurationMs(now.getTime() - step.getStartTime().getTime());
+            }
+        } else if (success && completed) {
+            // 鎴愬姛涓擬ES宸茬‘璁ゅ畬鎴愶細鏍囪涓篊OMPLETED骞惰褰曠粨鏉熸椂闂�
+            step.setStatus(TaskStepDetail.Status.COMPLETED.name());
+            String message = result.getMessage();
+            step.setSuccessMessage(StringUtils.hasText(message) ? message : "澶ц溅璁惧浠诲姟宸插畬鎴�");
+            step.setErrorMessage(null);
+            if (step.getEndTime() == null) {
+                step.setEndTime(now);
+            }
+            if (step.getStartTime() != null && step.getEndTime() != null) {
+                step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
+            }
+            // 灏濊瘯鑷姩鏀跺熬鏁翠釜浠诲姟
+            checkAndCompleteTaskIfDone(step.getTaskId());
+        } else {
+            // 澶辫触锛氭爣璁颁负FAILED骞惰褰曠粨鏉熸椂闂�
+            step.setStatus(TaskStepDetail.Status.FAILED.name());
+            String message = result.getMessage();
+            step.setErrorMessage(message);
+            if (step.getEndTime() == null) {
+                step.setEndTime(now);
+            }
+            if (step.getStartTime() != null && step.getEndTime() != null) {
+                step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
+            }
+        }
+
+        step.setOutputData(toJson(result));
+        taskStepDetailMapper.updateById(step);
+        // 閫氱煡鍓嶇姝ラ鐘舵�佸凡鏇存柊
+        notificationService.notifyStepUpdate(taskId, step);
+    }
+
+    /**
+     * 杞澶ц溅璁惧鐨凪ES浠诲姟鍜岀‘璁ょ姸鎬�
+     * 鏃犺姝ラ褰撳墠鏄惁涓篟UNNING/PENDING锛岄兘鍙互璋冪敤锛岀敤浜庨伩鍏嶉敊杩嘙ES绔悗鍐欏叆鐨勪换鍔�
+     */
+    private void pollMesForVehicle(MultiDeviceTask task,
+                                   TaskStepDetail step,
+                                   DeviceConfig device,
+                                   TaskExecutionContext context) {
+        try {
+            DeviceLogicHandler handler = handlerFactory.getHandler(device.getDeviceType());
+            if (handler == null) {
+                return;
+            }
+
+            Map<String, Object> logicParams = parseLogicParams(device);
+
+            // 鍏堟鏌ES浠诲姟锛堝鏋渕esSend=1锛屼細鍒涘缓浠诲姟骞跺紑濮嬫墽琛岋級
+            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());
+                }
+            } catch (Exception e) {
+                log.warn("澶ц溅璁惧妫�鏌ES浠诲姟寮傚父: taskId={}, deviceId={}, error={}",
+                        task.getTaskId(), device.getId(), e.getMessage());
+            }
+
+            // 鐒跺悗妫�鏌ES纭鐘舵��
+            DevicePlcVO.OperationResult mesResult = null;
+            try {
+                mesResult = handler.execute(device, "checkMesConfirm", Collections.emptyMap());
+            } catch (Exception e) {
+                log.warn("澶ц溅璁惧妫�鏌ES纭鐘舵�佸紓甯�: taskId={}, deviceId={}, error={}",
+                        task.getTaskId(), device.getId(), e.getMessage());
+            }
+
+            // 濡傛灉MES宸茬‘璁ゅ畬鎴愶紙mesConfirm=1锛夛紝妫�鏌ュ崸杞珛璁惧鐘舵�佸拰鐜荤拑淇℃伅
+            // 濡傛灉鍗ц浆绔嬪凡瀹屾垚涓旀墍鏈夌幓鐠冮兘宸茶杞斤紝鍙互鏍囪涓哄畬鎴�
+            if (mesResult != null && mesResult.getData() != null) {
+                Object completedFlag = mesResult.getData().get("completed");
+                boolean mesConfirmed = false;
+                if (completedFlag instanceof Boolean) {
+                    mesConfirmed = (Boolean) completedFlag;
+                } else if (completedFlag != null) {
+                    mesConfirmed = "true".equalsIgnoreCase(String.valueOf(completedFlag));
+                }
+                
+                if (mesConfirmed) {
+                    // MES宸茬‘璁ゅ畬鎴愶紝妫�鏌ュ崸杞珛璁惧鏄惁宸插畬鎴�
+                    boolean transferCompleted = isTransferDeviceCompleted(task.getTaskId(), context);
+                    if (transferCompleted) {
+                        // 妫�鏌ヤ换鍔′笂涓嬫枃涓殑鍒濆鐜荤拑ID鍜屽凡瑁呰浇鐨勭幓鐠僆D
+                        @SuppressWarnings("unchecked")
+                        List<String> initialGlassIds = (List<String>) context.getSharedData().get("initialGlassIds");
+                        List<String> loadedGlassIds = getLoadedGlassIds(context);
+                        
+                        if (initialGlassIds != null && !initialGlassIds.isEmpty() 
+                                && loadedGlassIds != null && !loadedGlassIds.isEmpty()) {
+                            // 妫�鏌ユ槸鍚︽墍鏈夊垵濮嬬幓鐠冮兘宸茶杞�
+                            boolean allGlassesLoaded = loadedGlassIds.containsAll(initialGlassIds);
+                            if (allGlassesLoaded) {
+                                // 鍗ц浆绔嬪凡瀹屾垚涓旀墍鏈夌幓鐠冮兘宸茶杞斤紝鏍囪涓哄畬鎴�
+                                log.info("MES宸茬‘璁や笖鍗ц浆绔嬪凡瀹屾垚涓旀墍鏈夌幓鐠冨凡瑁呰浇锛屼换鍔¤嚜鍔ㄥ畬鎴�: taskId={}, deviceId={}, initialCount={}, loadedCount={}",
+                                        task.getTaskId(), device.getId(), initialGlassIds.size(), loadedGlassIds.size());
+                                // mesResult宸茬粡鍖呭惈completed=true锛屼笉闇�瑕佷慨鏀�
+                            }
+                        }
+                    } else {
+                        // 鍗ц浆绔嬭繕鏈畬鎴愶紝涓嶅簲璇ユ爣璁颁负瀹屾垚
+                        log.debug("MES宸茬‘璁や絾鍗ц浆绔嬫湭瀹屾垚锛岀瓑寰呭崸杞珛瀹屾垚: taskId={}, deviceId={}", 
+                                task.getTaskId(), device.getId());
+                    }
+                }
+            }
+
+            // 鏇存柊姝ラ鐘舵��
+            if (mesResult != null) {
+                updateStepStatusForVehicle(task.getTaskId(), step, mesResult);
+                boolean opSuccess = Boolean.TRUE.equals(mesResult.getSuccess());
+                updateTaskProgress(task, step.getStepOrder(), opSuccess);
+                if (!opSuccess) {
+                    deviceCoordinationService.syncDeviceStatus(device,
+                            DeviceCoordinationService.DeviceStatus.FAILED, context);
+                }
+            }
+        } catch (Exception e) {
+            log.warn("杞澶ц溅璁惧MES浠诲姟/纭鐘舵�佸紓甯�: taskId={}, deviceId={}, error={}",
+                    task.getTaskId(), device.getId(), e.getMessage());
+        }
     }
     
     /**
      * 鏇存柊鍗ц浆绔嬭澶囨楠ょ姸鎬侊紙鍖哄垎绛夊緟涓拰鐪熸瀹屾垚锛�
      */
-    private void updateStepStatusForTransfer(TaskStepDetail step, DevicePlcVO.OperationResult result) {
+    private void updateStepStatusForTransfer(TaskStepDetail step, DevicePlcVO.OperationResult result, 
+                                            DeviceConfig device, TaskExecutionContext context) {
         if (step == null || result == null) {
             return;
         }
         boolean success = Boolean.TRUE.equals(result.getSuccess());
         String message = result.getMessage();
         
-        // 鍒ゆ柇鏄惁鐪熸瀹屾垚锛堝彧鏈夊啓鍏LC鎵嶇畻瀹屾垚锛�
-        boolean isRealCompleted = success && message != null && message.contains("鎵规宸插啓鍏LC");
+        // 鍒ゆ柇鏄惁鐪熸瀹屾垚锛�
+        // 1. 鍐欏叆PLC鎴愬姛涓旂紦鍐插凡娓呯┖锛堣〃绀烘墍鏈夌幓鐠冨凡澶勭悊瀹岋紝鏃犳柊鐜荤拑锛�
+        // 娉ㄦ剰锛氱紦鍐查槦鍒椾负绌轰笖鏃犲緟澶勭悊鐜荤拑锛屽湪浠诲姟鍒氬紑濮嬫椂涔熷彲鑳藉嚭鐜帮紝涓嶅簲璇ョ珛鍗虫爣璁颁负瀹屾垚
+        // 鍙湁褰撲换鍔″凡缁忚繍琛屼竴娈垫椂闂达紝涓旂‘瀹炴病鏈夌幓鐠冮渶瑕佸鐞嗘椂锛屾墠鏍囪涓哄畬鎴�
+        boolean isRealCompleted = success && message != null && (
+                (message.contains("鎵规宸插啓鍏LC") && message.contains("缂撳啿宸叉竻绌猴紝浠诲姟瀹屾垚"))
+        );
         
         if (isRealCompleted) {
             // 鐪熸瀹屾垚锛氳缃负瀹屾垚鐘舵�侊紝骞惰缃粨鏉熸椂闂�
-            step.setStatus(TaskStepDetail.Status.COMPLETED.name());
-            step.setSuccessMessage(message);
-            if (step.getEndTime() == null) {
-                step.setEndTime(new Date());
+            // 娉ㄦ剰锛氫竴鏃︽爣璁颁负瀹屾垚锛岀姸鎬佷笉搴旇鍐嶈鏀瑰彉
+            if (!TaskStepDetail.Status.COMPLETED.name().equals(step.getStatus())) {
+                step.setStatus(TaskStepDetail.Status.COMPLETED.name());
+                step.setSuccessMessage(message);
+                if (step.getEndTime() == null) {
+                    step.setEndTime(new Date());
+                }
+                // 璁$畻鑰楁椂
+                if (step.getStartTime() != null && step.getEndTime() != null) {
+                    step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
+                }
+                log.info("鍗ц浆绔嬭澶囨楠ゅ凡瀹屾垚: stepId={}, durationMs={}, message={}", 
+                        step.getId(), step.getDurationMs(), message);
+                // 鍗ц浆绔嬩富浣撳畬鎴愬悗灏濊瘯鑷姩鏀跺熬鏁翠釜浠诲姟
+                checkAndCompleteTaskIfDone(step.getTaskId());
+                // 鏇存柊璁惧鐘舵�佷负宸插畬鎴�
+                if (device != null) {
+                    deviceCoordinationService.syncDeviceStatus(device, 
+                            DeviceCoordinationService.DeviceStatus.COMPLETED, context);
+                }
             }
-        } else if (success) {
-            // 绛夊緟涓細淇濇寔杩愯鐘舵�侊紝鍙洿鏂版秷鎭�
+        } else if (success && message != null && message.contains("鎵规宸插啓鍏LC")) {
+            // 鍐欏叆PLC鎴愬姛浣嗙紦鍐茶繕鏈夌幓鐠冿紙杞︽弧鎯呭喌锛夛紝缁х画杩愯
             if (!TaskStepDetail.Status.RUNNING.name().equals(step.getStatus())) {
                 step.setStatus(TaskStepDetail.Status.RUNNING.name());
             }
             step.setSuccessMessage(message);
             // 纭繚寮�濮嬫椂闂村凡璁剧疆
+            if (step.getStartTime() == null) {
+                step.setStartTime(new Date());
+            }
+        } else if (success) {
+            // 璁惧姝e湪宸ヤ綔锛堢瓑寰呯紦鍐层�佺瓑寰呮洿澶氱幓鐠冪瓑锛夛紝淇濇寔RUNNING鐘舵��
+            // 鍥犱负瀹氭椂鍣ㄥ凡缁忔墽琛岋紝璇存槑璁惧宸茬粡寮�濮嬪伐浣滀簡
+            if (!TaskStepDetail.Status.RUNNING.name().equals(step.getStatus())) {
+                step.setStatus(TaskStepDetail.Status.RUNNING.name());
+            }
+            step.setSuccessMessage(message);
+            // 纭繚寮�濮嬫椂闂村凡璁剧疆锛堣澶囧凡缁忓紑濮嬪伐浣滐級
             if (step.getStartTime() == null) {
                 step.setStartTime(new Date());
             }
@@ -1140,6 +1927,10 @@
             step.setErrorMessage(message);
             if (step.getEndTime() == null) {
                 step.setEndTime(new Date());
+            }
+            // 璁$畻鑰楁椂
+            if (step.getStartTime() != null && step.getEndTime() != null) {
+                step.setDurationMs(step.getEndTime().getTime() - step.getStartTime().getTime());
             }
         }
         
@@ -1368,7 +2159,7 @@
     }
 
     /**
-     * 鍒嗘壒鎵ц澶ц溅璁惧鐜荤拑涓婃枡锛堝綋鐜荤拑ID鏁伴噺瓒呰繃6涓笖璁剧疆浜嗗崟鐗囬棿闅旀椂锛�
+     * 鍒嗘壒鎵ц澶ц溅璁惧鐜荤拑涓婃枡锛堝綋鐜荤拑ID鏁伴噺瓒呰繃6涓椂锛�
      */
     private StepResult executeLoadVehicleWithBatches(MultiDeviceTask task,
                                                       DeviceConfig device,
@@ -1376,13 +2167,12 @@
                                                       TaskExecutionContext context,
                                                       List<Map<String, Object>> stepSummaries) {
         List<String> allGlassIds = context.getParameters().getGlassIds();
-        Integer glassIntervalMs = context.getParameters().getGlassIntervalMs();
         int batchSize = 6; // 姣忔壒鏈�澶�6涓幓鐠僆D
         
         // 鍒嗘壒澶勭悊
         int totalBatches = (allGlassIds.size() + batchSize - 1) / batchSize;
-        log.info("澶ц溅璁惧鍒嗘壒涓婃枡: deviceId={}, totalGlassIds={}, batchSize={}, totalBatches={}, glassIntervalMs={}",
-                device.getId(), allGlassIds.size(), batchSize, totalBatches, glassIntervalMs);
+        log.debug("澶ц溅璁惧鍒嗘壒涓婃枡: deviceId={}, totalGlassIds={}, batchSize={}, totalBatches={}",
+                device.getId(), allGlassIds.size(), batchSize, totalBatches);
         
         for (int batchIndex = 0; batchIndex < totalBatches; batchIndex++) {
             int startIndex = batchIndex * batchSize;
@@ -1392,7 +2182,6 @@
             // 鍒涘缓涓存椂鍙傛暟锛屽彧鍖呭惈褰撳墠鎵规鐨勭幓鐠僆D
             TaskParameters batchParams = new TaskParameters();
             batchParams.setGlassIds(new ArrayList<>(batchGlassIds));
-            batchParams.setGlassIntervalMs(glassIntervalMs);
             batchParams.setPositionCode(context.getParameters().getPositionCode());
             batchParams.setPositionValue(context.getParameters().getPositionValue());
             
@@ -1413,20 +2202,8 @@
                 return stepResult;
             }
             
-            log.info("澶ц溅璁惧鍒嗘壒涓婃枡鎴愬姛: deviceId={}, batchIndex={}/{}, glassIds={}",
+            log.debug("澶ц溅璁惧鍒嗘壒涓婃枡鎴愬姛: deviceId={}, batchIndex={}/{}, glassIds={}",
                     device.getId(), batchIndex + 1, totalBatches, batchGlassIds);
-            
-            // 濡傛灉涓嶆槸鏈�鍚庝竴鎵癸紝绛夊緟闂撮殧锛堟ā鎷熺幓鐠冩瘡鐗囪繍鍔ㄧ殑鏃堕棿锛�
-            // 杩欎釜绛夊緟璁╁ぇ杞︽湁鏃堕棿澶勭悊褰撳墠鎵规鐨勭幓鐠冿紝鐒跺悗鍐嶄紶閫掍笅涓�鎵�
-            if (batchIndex < totalBatches - 1 && glassIntervalMs != null && glassIntervalMs > 0) {
-                try {
-                    log.info("绛夊緟鍗曠墖闂撮殧锛堟ā鎷熺幓鐠冭繍鍔ㄦ椂闂达級: glassIntervalMs={}ms, 澶ц溅鍙湪姝ゆ湡闂寸户缁鐜荤拑", glassIntervalMs);
-                    Thread.sleep(glassIntervalMs);
-                } catch (InterruptedException e) {
-                    Thread.currentThread().interrupt();
-                    return StepResult.failure(device.getDeviceName(), "绛夊緟鍗曠墖闂撮殧鏃惰涓柇");
-                }
-            }
         }
         
         // 鏇存柊涓婁笅鏂囦腑鐨勫凡鍔犺浇鐜荤拑ID
@@ -1487,7 +2264,7 @@
         Map<String, Object> params = buildOperationParams(device, context);
         // 灏哻ontext寮曠敤鏀惧叆params锛屼緵璁惧澶勭悊鍣ㄤ娇鐢紙鐢ㄤ簬璁惧鍗忚皟锛�
         params.put("_taskContext", context);
-        log.info("executeStepWithRetry鏋勫缓鍙傛暟: deviceId={}, deviceType={}, operation={}, paramsKeys={}, params={}", 
+        log.debug("executeStepWithRetry鏋勫缓鍙傛暟: deviceId={}, deviceType={}, operation={}, paramsKeys={}, params={}", 
                 device.getId(), device.getDeviceType(), determineOperation(device, params), params.keySet(), params);
         step.setInputData(toJson(params));
         taskStepDetailMapper.updateById(step);
@@ -1503,7 +2280,7 @@
                 if (retryAttempt > 0) {
                     // 閲嶈瘯鍓嶇瓑寰�
                     long waitTime = retryPolicy.calculateRetryInterval(retryAttempt);
-                    log.info("姝ラ鎵ц閲嶈瘯: deviceId={}, operation={}, retryAttempt={}/{}, waitTime={}ms", 
+                    log.debug("姝ラ鎵ц閲嶈瘯: deviceId={}, operation={}, retryAttempt={}/{}, waitTime={}ms", 
                         device.getId(), operation, retryAttempt, retryPolicy.getMaxRetryCount(), waitTime);
                     Thread.sleep(waitTime);
                     
@@ -1697,7 +2474,7 @@
             try {
                 if (retryAttempt > 0) {
                     long waitTime = retryPolicy.calculateRetryInterval(retryAttempt);
-                    log.info("浜や簰姝ラ鎵ц閲嶈瘯: deviceId={}, retryAttempt={}/{}, waitTime={}ms", 
+                    log.debug("浜や簰姝ラ鎵ц閲嶈瘯: deviceId={}, retryAttempt={}/{}, waitTime={}ms", 
                         device.getId(), retryAttempt, retryPolicy.getMaxRetryCount(), waitTime);
                     Thread.sleep(waitTime);
                     
@@ -1891,6 +2668,66 @@
         }
     }
 
+    /**
+     * 妫�鏌ュ崸杞珛璁惧鏄惁宸插畬鎴�
+     * 杩斿洖true琛ㄧず鍗ц浆绔嬪凡瀹屾垚锛圕OMPLETED锛夛紝鍙互鍒ゆ柇澶ц溅鏄惁瀹屾垚
+     * 杩斿洖false琛ㄧず鍗ц浆绔嬭繕鍦ㄨ繍琛屼腑锛圧UNNING锛夋垨绛夊緟涓紙PENDING锛夛紝涓嶅簲璇ユ爣璁板ぇ杞︿负瀹屾垚
+     */
+    private boolean isTransferDeviceCompleted(String taskId, TaskExecutionContext context) {
+        if (taskId == null || context == null) {
+            return false;
+        }
+        try {
+            // 浠庝笂涓嬫枃涓幏鍙栬澶囧垪琛�
+            @SuppressWarnings("unchecked")
+            List<DeviceConfig> devices = (List<DeviceConfig>) context.getSharedData().get("devices");
+            if (devices == null || devices.isEmpty()) {
+                return false;
+            }
+            
+            // 鏌ユ壘鍗ц浆绔嬭澶�
+            DeviceConfig transferDevice = null;
+            for (DeviceConfig device : devices) {
+                if (DeviceConfig.DeviceType.WORKSTATION_TRANSFER.equals(device.getDeviceType())) {
+                    transferDevice = device;
+                    break;
+                }
+            }
+            
+            if (transferDevice == null) {
+                // 娌℃湁鍗ц浆绔嬭澶囷紝杩斿洖true锛堜笉褰卞搷鍒ゆ柇锛�
+                return true;
+            }
+            
+            // 鏌ユ壘鍗ц浆绔嬭澶囩殑姝ラ锛堝簲璇ュ彧鏈変竴涓楠わ級
+            List<TaskStepDetail> transferSteps = taskStepDetailMapper.selectList(
+                Wrappers.<TaskStepDetail>lambdaQuery()
+                    .eq(TaskStepDetail::getTaskId, taskId)
+                    .eq(TaskStepDetail::getDeviceId, transferDevice.getId())
+                    .orderByDesc(TaskStepDetail::getStepOrder)
+                    .last("LIMIT 1")
+            );
+            
+            if (transferSteps == null || transferSteps.isEmpty()) {
+                // 娌℃湁鎵惧埌姝ラ锛岃繑鍥瀎alse锛堝崸杞珛鍙兘杩樻病寮�濮嬶級
+                return false;
+            }
+            
+            // 妫�鏌ユ楠ょ姸鎬侊細鍙湁COMPLETED鎵嶇畻瀹屾垚锛孯UNNING鎴朠ENDING閮戒笉绠楀畬鎴�
+            TaskStepDetail transferStep = transferSteps.get(0);
+            String status = transferStep.getStatus();
+            boolean isCompleted = TaskStepDetail.Status.COMPLETED.name().equals(status);
+            
+            log.debug("妫�鏌ュ崸杞珛璁惧鐘舵��: taskId={}, deviceId={}, status={}, isCompleted={}", 
+                    taskId, transferDevice.getId(), status, isCompleted);
+            
+            return isCompleted;
+        } catch (Exception e) {
+            log.warn("妫�鏌ュ崸杞珛璁惧鐘舵�佸け璐�: taskId={}", taskId, e);
+            return false;
+        }
+    }
+
     private String determineOperation(DeviceConfig device, Map<String, Object> params) {
         if (params != null && params.containsKey("operation")) {
             Object op = params.get("operation");
@@ -1914,10 +2751,6 @@
                 if (taskParams.getPositionValue() != null) {
                     params.put("positionValue", taskParams.getPositionValue());
                 }
-                // 浼犻�掑崟鐗囬棿闅旈厤缃紝濡傛灉浠诲姟鍙傛暟涓湁璁剧疆锛屼紭鍏堜娇鐢ㄤ换鍔″弬鏁扮殑锛屽惁鍒欎娇鐢ㄨ澶囬厤缃殑
-                if (taskParams.getGlassIntervalMs() != null) {
-                    params.put("glassIntervalMs", taskParams.getGlassIntervalMs());
-                }
                 params.put("triggerRequest", true);
                 break;
             case DeviceConfig.DeviceType.LARGE_GLASS:
@@ -1935,13 +2768,13 @@
             case DeviceConfig.DeviceType.WORKSTATION_SCANNER:
                 // 鍗ц浆绔嬫壂鐮佽澶囷細浠庝换鍔″弬鏁颁腑鑾峰彇鐜荤拑ID鍒楄〃锛屽彇绗竴涓綔涓哄綋鍓嶈娴嬭瘯鐨勭幓鐠僆D
                 // 娉ㄦ剰锛氭壂鐮佽澶囬�氬父閫氳繃瀹氭椂鍣ㄦ墽琛岋紝浣嗗鏋滈�氳繃executeStep鎵ц锛屼篃闇�瑕佷紶閫抔lassId
-                log.info("buildOperationParams澶勭悊鎵爜璁惧: deviceId={}, taskParams.glassIds={}, isEmpty={}", 
+                log.debug("buildOperationParams澶勭悊鎵爜璁惧: deviceId={}, taskParams.glassIds={}, isEmpty={}", 
                         device.getId(), taskParams.getGlassIds(), 
                         CollectionUtils.isEmpty(taskParams.getGlassIds()));
                 if (!CollectionUtils.isEmpty(taskParams.getGlassIds())) {
                     params.put("glassId", taskParams.getGlassIds().get(0));
                     params.put("glassIds", new ArrayList<>(taskParams.getGlassIds()));
-                    log.info("buildOperationParams涓烘壂鐮佽澶囨坊鍔爂lassId: deviceId={}, glassId={}, glassIdsSize={}", 
+                    log.debug("buildOperationParams涓烘壂鐮佽澶囨坊鍔爂lassId: deviceId={}, glassId={}, glassIdsSize={}", 
                             device.getId(), taskParams.getGlassIds().get(0), taskParams.getGlassIds().size());
                 } else {
                     log.warn("buildOperationParams鎵爜璁惧glassIds涓虹┖: deviceId={}, taskParams.glassIds={}, taskParams={}", 
@@ -2017,7 +2850,7 @@
         if (!CollectionUtils.isEmpty(scannerGlassIds)) {
             context.getParameters().setGlassIds(new ArrayList<>(scannerGlassIds));
             context.setLoadedGlassIds(new ArrayList<>(scannerGlassIds));
-            log.info("鍗ц浆绔嬫壂鐮佽幏鍙栧埌鐜荤拑ID: {}", scannerGlassIds);
+            log.debug("鍗ц浆绔嬫壂鐮佽幏鍙栧埌鐜荤拑ID: {}", scannerGlassIds);
         } else {
             log.warn("鍗ц浆绔嬫壂鐮佹湭鑾峰彇鍒扮幓鐠僆D锛屽悗缁澶囧彲鑳芥棤娉曟墽琛�");
         }

--
Gitblit v1.8.0