From bf2aa9dd56432a74940ca1bb08fb4d7eaee37045 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期五, 27 三月 2026 11:03:37 +0800
Subject: [PATCH] feat(WCS): 完善 WIDESEAWCS_Tasks 模块日志系统

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/CommonConveyorLineNewJob.cs |  219 +++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 166 insertions(+), 53 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/CommonConveyorLineNewJob.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/CommonConveyorLineNewJob.cs
index f8219c9..cad24d5 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/CommonConveyorLineNewJob.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineNewJob/CommonConveyorLineNewJob.cs
@@ -1,24 +1,6 @@
-锘�#region << 鐗� 鏈� 娉� 閲� >>
-
-/*----------------------------------------------------------------
- * 鍛藉悕绌洪棿锛歐IDESEAWCS_Tasks.ConveyorLineJob
- * 鍒涘缓鑰咃細鑳$搴�
- * 鍒涘缓鏃堕棿锛�2024/8/2 16:13:36
- * 鐗堟湰锛歏1.0.0
- * 鎻忚堪锛�
- *
- * ----------------------------------------------------------------
- * 淇敼浜猴細
- * 淇敼鏃堕棿锛�
- * 鐗堟湰锛歏1.0.1
- * 淇敼璇存槑锛�
- *
- *----------------------------------------------------------------*/
-
-#endregion << 鐗� 鏈� 娉� 閲� >>
-
-using AutoMapper;
+using MapsterMapper;
 using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Logging;
 using Quartz;
 using SqlSugar;
 using System.Text.Json;
@@ -26,6 +8,7 @@
 using WIDESEAWCS_Common.TaskEnum;
 using WIDESEAWCS_Core;
 using WIDESEAWCS_Core.Helper;
+using WIDESEAWCS_Core.LogHelper;
 using WIDESEAWCS_DTO.TaskInfo;
 using WIDESEAWCS_ITaskInfoService;
 using WIDESEAWCS_Model.Models;
@@ -34,72 +17,178 @@
 
 namespace WIDESEAWCS_Tasks
 {
+    /// <summary>
+    /// 杈撻�佺嚎浠诲姟浣滀笟锛圦uartz Job锛�- 鏍稿績鎵ц閫昏緫
+    /// </summary>
+    /// <remarks>
+    /// Quartz 瀹氭椂浠诲姟锛岃礋璐e鐞嗚緭閫佺嚎鐨勪换鍔¤皟搴︺��
+    /// 浣跨敤 [DisallowConcurrentExecution] 绂佹骞跺彂鎵ц锛岀‘淇濆悓涓�璁惧鐨勪换鍔′覆琛屽鐞嗐��
+    ///
+    /// 鏍稿績鑱岃矗锛�
+    /// 1. 鑾峰彇杈撻�佺嚎鐨勬墍鏈夊瓙璁惧浣嶇疆
+    /// 2. 骞惰澶勭悊姣忎釜瀛愯澶囩殑娑堟伅
+    /// 3. 鏍规嵁浠诲姟鐘舵�佽皟鐢ㄧ浉搴旂殑璋冨害鏂规硶
+    /// 4. 澶勭悊鍏ュ簱鍜屽嚭搴撲袱澶х被浠诲姟
+    ///
+    /// 璇� Job 閫氳繃 Parallel.For 骞惰澶勭悊澶氫釜瀛愯澶囷紝鎻愰珮澶勭悊鏁堢巼銆�
+    /// </remarks>
     [DisallowConcurrentExecution]
     public class CommonConveyorLineNewJob : IJob
     {
+        /// <summary>
+        /// 浠诲姟鏈嶅姟
+        /// </summary>
         private readonly ITaskService _taskService;
+
+        /// <summary>
+        /// 浠诲姟鎵ц鏄庣粏鏈嶅姟
+        /// </summary>
         private readonly ITaskExecuteDetailService _taskExecuteDetailService;
+
+        /// <summary>
+        /// 璺敱鏈嶅姟
+        /// </summary>
         private readonly IRouterService _routerService;
+
+        /// <summary>
+        /// 瀵硅薄鏄犲皠鍣�
+        /// </summary>
         private readonly IMapper _mapper;
+
+        /// <summary>
+        /// 杈撻�佺嚎璋冨害澶勭悊鍣�
+        /// </summary>
+        /// <remarks>
+        /// 灏佽浜嗚緭閫佺嚎涓氬姟閫昏緫鐨勫鐞嗘柟娉曘��
+        /// </remarks>
         private ConveyorLineDispatchHandler _conveyorLineDispatch;
+
+        /// <summary>
+        /// HTTP 瀹㈡埛绔府鍔╃被
+        /// </summary>
+        /// <remarks>
+        /// 鐢ㄤ簬璋冪敤 WMS 绯荤粺鐨� HTTP 鎺ュ彛銆�
+        /// </remarks>
         private readonly HttpClientHelper _httpClientHelper;
 
-        public CommonConveyorLineNewJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper, HttpClientHelper httpClientHelper)
+        /// <summary>
+        /// 鏃ュ織璁板綍鍣�
+        /// </summary>
+        private readonly ILogger<CommonConveyorLineNewJob> _logger;
+
+        /// <summary>
+        /// 鏋勯�犲嚱鏁�
+        /// </summary>
+        /// <param name="taskService">浠诲姟鏈嶅姟</param>
+        /// <param name="taskExecuteDetailService">浠诲姟鎵ц鏄庣粏鏈嶅姟</param>
+        /// <param name="routerService">璺敱鏈嶅姟</param>
+        /// <param name="mapper">瀵硅薄鏄犲皠鍣�</param>
+        /// <param name="httpClientHelper">HTTP 瀹㈡埛绔府鍔╃被</param>
+        /// <param name="logger">鏃ュ織璁板綍鍣�</param>
+        public CommonConveyorLineNewJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, IRouterService routerService, IMapper mapper, HttpClientHelper httpClientHelper, ILogger<CommonConveyorLineNewJob> logger)
         {
             _taskService = taskService;
             _taskExecuteDetailService = taskExecuteDetailService;
             _routerService = routerService;
             _mapper = mapper;
-            _conveyorLineDispatch = new ConveyorLineDispatchHandler(_taskService, _taskExecuteDetailService, _routerService, _mapper);
             _httpClientHelper = httpClientHelper;
+            _logger = logger;
+
+            // 鍒濆鍖栬皟搴﹀鐞嗗櫒
+            _conveyorLineDispatch = new ConveyorLineDispatchHandler(_taskService, _taskExecuteDetailService, _routerService, _mapper, _logger);
         }
 
+        /// <summary>
+        /// Quartz Job 鐨勬墽琛屽叆鍙�
+        /// </summary>
+        /// <remarks>
+        /// 鎵ц娴佺▼锛�
+        /// 1. 浠� JobDataMap 鑾峰彇杈撻�佺嚎璁惧淇℃伅
+        /// 2. 鑾峰彇璇ヨ緭閫佺嚎鐨勬墍鏈夊瓙璁惧浣嶇疆鍒楄〃
+        /// 3. 骞惰澶勭悊姣忎釜瀛愯澶囩殑娑堟伅
+        /// 4. 妫�鏌ユ墭鐩樹綅缃紙鐗瑰畾閰嶇疆鐨勪綅缃級
+        /// 5. 鏍规嵁浠诲姟鐘舵�佸垎鍙戝埌鐩稿簲鐨勫鐞嗘柟娉�
+        ///
+        /// 骞惰澶勭悊鎻愰珮浜嗗澶氱珯鍙拌緭閫佺嚎鐨勫鐞嗘晥鐜囥��
+        /// </remarks>
+        /// <param name="context">Quartz 浣滀笟鎵ц涓婁笅鏂�</param>
         public Task Execute(IJobExecutionContext context)
         {
             try
             {
+                // 浠� JobDataMap 鑾峰彇杈撻�佺嚎璁惧鍙傛暟
                 CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
                 if (conveyorLine != null)
                 {
+                    // 鑾峰彇璇ヨ緭閫佺嚎涓嬬殑鎵�鏈夊瓙璁惧浣嶇疆缂栫爜
                     List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode);
                     if (childDeviceCodes == null || childDeviceCodes.Count == 0)
                     {
-                        Console.WriteLine($"杈撻�佺嚎 {conveyorLine.DeviceCode} 娌℃湁瀛愯澶�");
+                        // 娌℃湁瀛愯澶囷紝鐩存帴杩斿洖
+                        _logger.LogInformation("杈撻�佺嚎 {DeviceCode} 娌℃湁瀛愯澶�", conveyorLine.DeviceCode);
+                        QuartzLogger.Info($"杈撻�佺嚎 {conveyorLine.DeviceCode} 娌℃湁瀛愯澶�", conveyorLine.DeviceCode);
                         return Task.CompletedTask;
                     }
 
-                    // 鍒涘缓骞惰閫夐」
+                    // 鍒涘缓骞惰閫夐」锛岄檺鍒舵渶澶у苟鍙戞暟
                     var parallelOptions = new ParallelOptions
                     {
-                        MaxDegreeOfParallelism = Math.Min(childDeviceCodes.Count, Environment.ProcessorCount * 2), // 鍚堢悊闄愬埗骞跺彂鏁�
+                        // 闄愬埗骞跺彂鏁帮細瀛愯澶囨暟閲忓拰 CPU 鏍稿績鏁�*2 鐨勮緝灏忓��
+                        MaxDegreeOfParallelism = Math.Min(childDeviceCodes.Count, Environment.ProcessorCount * 2),
                     };
+
+                    _logger.LogDebug("Execute锛氬紑濮嬪苟琛屽鐞嗚緭閫佺嚎 {DeviceCode}锛屽瓙璁惧鏁伴噺: {Count}", conveyorLine.DeviceCode, childDeviceCodes.Count);
+                    QuartzLogger.Debug($"寮�濮嬪苟琛屽鐞嗚緭閫佺嚎锛屽瓙璁惧鏁伴噺: {childDeviceCodes.Count}", conveyorLine.DeviceCode);
+
+                    // 骞惰澶勭悊姣忎釜瀛愯澶�
                     Parallel.For(0, childDeviceCodes.Count, parallelOptions, i =>
                     {
                         string childDeviceCode = childDeviceCodes[i];
                         var correlationId = Guid.NewGuid().ToString("N");
                         try
                         {
+                            // 璇诲彇璇ヤ綅缃殑 PLC 鍛戒护鏁版嵁
                             ConveyorLineTaskCommandNew command = conveyorLine.ReadCustomer<ConveyorLineTaskCommandNew>(childDeviceCode);
 
-                            #region 妫�鏌ョ壒瀹氫綅缃槸鍚︽湁鎵樼洏
+                            // 濡傛灉鍛戒护涓虹┖锛岃烦杩�
+                            if (command == null)
+                            {
+                                _logger.LogDebug("Execute锛氬瓙璁惧 {ChildDeviceCode} 鍛戒护涓虹┖锛岃烦杩�", childDeviceCode);
+                                QuartzLogger.Debug($"瀛愯澶� {childDeviceCode} 鍛戒护涓虹┖锛岃烦杩�", conveyorLine.DeviceCode);
+                                return;
+                            }
 
+                            // 濡傛灉 WCS_ACK 涓� 1锛屽厛娓呴櫎锛堣〃绀哄鐞嗚繃涓婁竴娆¤姹傦級
+                            if (command.WCS_ACK == 1)
+                                conveyorLine.SetValue(ConveyorLineDBNameNew.WCS_ACK, 0, childDeviceCode);
+
+                            // ========== 妫�鏌ョ壒瀹氫綅缃槸鍚︽湁鎵樼洏 ==========
+                            // 浠庨厤缃腑璇诲彇闇�瑕佹鏌ユ墭鐩樼殑浣嶇疆鍒楄〃
                             var checkPalletPositions = App.Configuration.GetSection("CheckPalletPositions")
                                 .Get<List<CheckPalletPosition>>() ?? new List<CheckPalletPosition>();
 
+                            // 濡傛灉褰撳墠璁惧鍦ㄦ鏌ュ垪琛ㄤ腑
                             if (checkPalletPositions.Any(x => x.Code == childDeviceCode))
                             {
+                                // 妫�鏌ヨ緭閫佺嚎鐘舵�侊紙鏄惁鏈夋墭鐩橈級
                                 if (command.CV_State.ObjToBool())
                                 {
+                                    // 妫�鏌ヨ浣嶇疆鏄惁宸叉湁浠诲姟
                                     var existingTask = _taskService.Repository.QueryFirst(x => x.TargetAddress == childDeviceCode);
                                     if (existingTask.IsNullOrEmpty())
                                     {
+                                        // 娌℃湁浠诲姟锛屽悜 WMS 璇锋眰鍑哄簱鎵樼洏浠诲姟
                                         var position = checkPalletPositions.FirstOrDefault(x => x.Code == childDeviceCode);
+                                        _logger.LogInformation("Execute锛氭鏌ユ墭鐩樹綅缃� {ChildDeviceCode}锛岃姹俉MS鍑哄簱鎵樼洏浠诲姟", childDeviceCode);
+                                        QuartzLogger.Info($"妫�鏌ユ墭鐩樹綅缃� {childDeviceCode}锛岃姹俉MS鍑哄簱鎵樼洏浠诲姟", conveyorLine.DeviceCode);
+
                                         var responseResult = _httpClientHelper.Post<WebResponseContent>("GetOutBoundTrayTaskAsync", new CreateTaskDto()
                                         {
                                             WarehouseId = position.WarehouseId,
                                             TargetAddress = childDeviceCode
                                         }.Serialize());
 
+                                        // 濡傛灉璇锋眰鎴愬姛锛屾帴鏀� WMS 杩斿洖鐨勪换鍔�
                                         if (responseResult.IsSuccess && responseResult.Data.Status)
                                         {
                                             var wmsTask = JsonSerializer.Deserialize<List<WMSTaskDTO>>(responseResult.Data.Data.Serialize());
@@ -110,43 +199,54 @@
                                 }
                             }
 
-                            #endregion
-
-                            if (command == null || command.PLC_STB != 1)
+                            // ========== 妫�鏌� PLC_STB 鏍囧織 ==========
+                            // 鍙湁褰� PLC_STB 涓� 1 鏃舵墠澶勭悊浠诲姟
+                            if (command.PLC_STB != 1)
                             {
+                                _logger.LogDebug("Execute锛氬瓙璁惧 {ChildDeviceCode} PLC_STB 涓嶄负1锛岃烦杩�", childDeviceCode);
+                                QuartzLogger.Debug($"瀛愯澶� {childDeviceCode} PLC_STB 涓嶄负1锛岃烦杩�", conveyorLine.DeviceCode);
                                 return;
                             }
 
-                            if (command.Barcode.IsNullOrEmpty())
+                            // ========== 澶勭悊鏃犳墭鐩樻潯鐮佺殑鎯呭喌 ==========
+                            // 鏃犳墭鐩樻潯鐮佹椂锛岃姹傚嚭搴撲换鍔�
+                            if (command.Barcode.IsNullOrEmpty() || command.Barcode.Replace("\0", "") == "")
                             {
-                                //鏃犳墭鐩樺彿鏃�
+                                _logger.LogDebug("Execute锛氬瓙璁惧 {ChildDeviceCode} 鏃犳墭鐩樻潯鐮侊紝璇锋眰鍑哄簱浠诲姟", childDeviceCode);
+                                QuartzLogger.Debug($"瀛愯澶� {childDeviceCode} 鏃犳墭鐩樻潯鐮侊紝璇锋眰鍑哄簱浠诲姟", conveyorLine.DeviceCode);
                                 _conveyorLineDispatch.RequestOutbound(conveyorLine, command, childDeviceCode);
                                 return;
                             }
 
+                            // ========== 澶勭悊宸叉湁浠诲姟鍙风殑鎯呭喌 ==========
                             if (command.TaskNo > 0)
                             {
+                                // 鏌ヨ姝e湪鎵ц鐨勪换鍔�
                                 Dt_Task task = _taskService.QueryExecutingConveyorLineTask(command.TaskNo, childDeviceCode);
-                                if (task.IsNullOrEmpty())
+                                if (!task.IsNullOrEmpty())
                                 {
-                                    _conveyorLineDispatch.RequestInbound(conveyorLine, command, childDeviceCode);
+                                    _logger.LogInformation("Execute锛氬瓙璁惧 {ChildDeviceCode} 澶勭悊浠诲姟 {TaskNum}锛岀姸鎬�: {Status}", childDeviceCode, task.TaskNum, task.TaskStatus);
+                                    QuartzLogger.Info($"澶勭悊浠诲姟 {task.TaskNum}锛岀姸鎬�: {task.TaskStatus}", conveyorLine.DeviceCode);
+                                    // 澶勭悊浠诲姟鐘舵�侊紙鏍规嵁鐘舵�佸垎鍙戝埌涓嶅悓鏂规硶锛�
+                                    ProcessTaskState(conveyorLine, command, task, childDeviceCode);
                                     return;
                                 }
-
-                                // 澶勭悊浠诲姟鐘舵��
-                                ProcessTaskState(conveyorLine, command, task, childDeviceCode);
                             }
                         }
                         catch (Exception innerEx)
                         {
-                            Console.Error.WriteLine($"{DateTime.UtcNow:O} [{childDeviceCode}] CorrelationId={correlationId} {innerEx}");
+                            // 璁板綍寮傚父锛屼絾涓嶅奖鍝嶅叾浠栧瓙璁惧鐨勫鐞�
+                            _logger.LogError(innerEx, "Execute锛氬瓙璁惧 {ChildDeviceCode} 澶勭悊寮傚父锛孋orrelationId: {CorrelationId}", childDeviceCode, correlationId);
+                            QuartzLogger.Error($"瀛愯澶囧鐞嗗紓甯�: {innerEx.Message}", conveyorLine.DeviceCode, innerEx);
                         }
                     });
                 }
             }
             catch (Exception ex)
             {
-                Console.Error.WriteLine(ex);
+                // 璁板綍鏁翠綋寮傚父
+                _logger.LogError(ex, "Execute锛氳緭閫佺嚎 {DeviceCode} 鎵ц寮傚父", ex.Message);
+                QuartzLogger.Error($"杈撻�佺嚎鎵ц寮傚父: {ex.Message}", "CommonConveyorLineNewJob", ex);
             }
             return Task.CompletedTask;
         }
@@ -154,46 +254,59 @@
         /// <summary>
         /// 澶勭悊浠诲姟鐘舵��
         /// </summary>
-        /// <param name="conveyorLine">杈撻�佺嚎瀹炰緥瀵硅薄</param>
-        /// <param name="command">璇诲彇鐨勮姹備俊鎭�</param>
-        /// <param name="task">瀛愯澶囩紪鍙�</param>
-        /// <param name="childDeviceCode"></param>
+        /// <remarks>
+        /// 鏍规嵁浠诲姟鐨勫綋鍓嶇姸鎬侊紝璋冪敤鐩稿簲鐨勮皟搴︽柟娉曪細
+        /// - InExecuting: 鍏ュ簱鎵ц涓� -> 璋冪敤 RequestInNextAddress
+        /// - OutExecuting: 鍑哄簱鎵ц涓� -> 鏍规嵁鏄惁鍒拌揪鐩爣鍦板潃璋冪敤瀵瑰簲鏂规硶
+        /// - InFinish: 鍏ュ簱瀹屾垚 -> 璋冪敤 ConveyorLineInFinish
+        /// - OutFinish: 鍑哄簱瀹屾垚 -> 璋冪敤 ConveyorLineOutFinish
+        /// </remarks>
+        /// <param name="conveyorLine">杈撻�佺嚎璁惧瀵硅薄</param>
+        /// <param name="command">PLC 鍛戒护鏁版嵁</param>
+        /// <param name="task">浠诲姟瀵硅薄</param>
+        /// <param name="childDeviceCode">瀛愯澶囩紪鐮�</param>
         private void ProcessTaskState(CommonConveyorLine conveyorLine, ConveyorLineTaskCommandNew command, Dt_Task task, string childDeviceCode)
         {
-            // 瀹氫箟鐘舵�佸父閲忥紙濡傛灉绫讳腑宸插畾涔夊垯鍙Щ闄わ級
-            const int InExecuting = (int)TaskInStatusEnum.Line_InExecuting;
-            const int OutExecuting = (int)TaskOutStatusEnum.Line_OutExecuting;
-            const int InFinish = (int)TaskInStatusEnum.InFinish;
-            const int OutFinish = (int)TaskOutStatusEnum.OutFinish;
+            // 瀹氫箟浠诲姟鐘舵�佸父閲�
+            const int InExecuting = (int)TaskInStatusEnum.Line_InExecuting;     // 鍏ュ簱鎵ц涓�
+            const int OutExecuting = (int)TaskOutStatusEnum.Line_OutExecuting; // 鍑哄簱鎵ц涓�
+            const int InFinish = (int)TaskInStatusEnum.InFinish;                 // 鍏ュ簱瀹屾垚
+            const int OutFinish = (int)TaskOutStatusEnum.OutFinish;             // 鍑哄簱瀹屾垚
 
-            int state = task.TaskState;
+            // 鑾峰彇褰撳墠浠诲姟鐘舵��
+            int state = task.TaskStatus;
+
+            // 鍒ゆ柇褰撳墠瀛愯澶囨槸鍚︿负鐩爣鍦板潃
             bool isTargetAddress = task.TargetAddress == childDeviceCode;
 
-            // 澶勭悊鐘舵�侀�昏緫
+            // 鏍规嵁鐘舵�佸垎鍙戝鐞�
             switch (state)
             {
                 case InExecuting:
-                    //if (isTargetAddress)
-                    //    _conveyorLineDispatch.ConveyorLineInFinish(conveyorLine, command, childDeviceCode);
-                    //else
+                    // 鍏ュ簱鎵ц涓紝璋冪敤涓嬩竴鍦板潃澶勭悊
                     _conveyorLineDispatch.RequestInNextAddress(conveyorLine, command, childDeviceCode);
                     break;
 
                 case OutExecuting:
+                    // 鍑哄簱鎵ц涓�
                     if (isTargetAddress)
+                        // 鍒拌揪鐩爣鍦板潃锛岃皟鐢ㄥ嚭搴撳畬鎴�
                         _conveyorLineDispatch.ConveyorLineOutFinish(conveyorLine, command, childDeviceCode);
                     else
+                        // 鏈埌杈剧洰鏍囧湴鍧�锛岃皟鐢ㄥ嚭搴撲笅涓�鍦板潃澶勭悊
                         _conveyorLineDispatch.RequestOutNextAddress(conveyorLine, command, childDeviceCode);
                     break;
 
                 case InFinish:
+                    // 鍏ュ簱瀹屾垚
                     _conveyorLineDispatch.ConveyorLineInFinish(conveyorLine, command, childDeviceCode);
                     break;
 
                 case OutFinish:
+                    // 鍑哄簱瀹屾垚
                     _conveyorLineDispatch.ConveyorLineOutFinish(conveyorLine, command, childDeviceCode);
                     break;
             }
         }
     }
-}
\ No newline at end of file
+}

--
Gitblit v1.9.3