From a8f45091019012eeafec563913dee71cda3d9790 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 11 三月 2026 16:36:49 +0800
Subject: [PATCH] refactor: modularize WIDESEAWCS_Tasks workflows

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs |  350 +++++++--------------------------------------------------
 1 files changed, 45 insertions(+), 305 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
index 5831db5..c9f777a 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/StackerCraneJob/CommonStackerCraneJob.cs
@@ -1,24 +1,14 @@
-锘縰sing System;
-using System.Collections.Generic;
+锘縰sing Quartz;
+using System;
 using System.Diagnostics.CodeAnalysis;
-using System.Linq;
-using System.Text;
+using System.IO;
 using System.Threading.Tasks;
-using Microsoft.AspNetCore.Components.Routing;
-using Quartz;
 using WIDESEAWCS_Common.TaskEnum;
-using WIDESEAWCS_Communicator;
-using WIDESEAWCS_Core.Enums;
-using WIDESEAWCS_Core.Helper;
 using WIDESEAWCS_ITaskInfoRepository;
 using WIDESEAWCS_ITaskInfoService;
 using WIDESEAWCS_Model.Models;
 using WIDESEAWCS_QuartzJob;
-using WIDESEAWCS_QuartzJob.DeviceBase;
-using WIDESEAWCS_QuartzJob.Models;
-using WIDESEAWCS_QuartzJob.Service;
-using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
-using WIDESEAWCS_TaskInfoService;
+using WIDESEAWCS_QuartzJob.StackerCrane;
 using WIDESEAWCS_Tasks.StackerCraneJob;
 
 namespace WIDESEAWCS_Tasks
@@ -29,16 +19,24 @@
         private readonly ITaskService _taskService;
         private readonly ITaskExecuteDetailService _taskExecuteDetailService;
         private readonly ITaskRepository _taskRepository;
-        private readonly IRouterService _routerService;
-        private readonly StackerCraneCommandConfig _config;
 
-        public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService)
+        private readonly StackerCraneCommandConfig _config;
+        private readonly StackerCraneTaskSelector _taskSelector;
+        private readonly StackerCraneCommandBuilder _commandBuilder;
+
+        public CommonStackerCraneJob(
+            ITaskService taskService,
+            ITaskExecuteDetailService taskExecuteDetailService,
+            ITaskRepository taskRepository,
+            WIDESEAWCS_QuartzJob.Service.IRouterService routerService)
         {
             _taskService = taskService;
             _taskExecuteDetailService = taskExecuteDetailService;
             _taskRepository = taskRepository;
-            _routerService = routerService;
+
             _config = LoadConfig();
+            _taskSelector = new StackerCraneTaskSelector(taskService, routerService);
+            _commandBuilder = new StackerCraneCommandBuilder(taskService, routerService, _config);
         }
 
         /// <summary>
@@ -59,6 +57,7 @@
             {
                 Console.WriteLine($"閰嶇疆鍔犺浇澶辫触: {ex.Message}锛屼娇鐢ㄩ粯璁ら厤缃�");
             }
+
             return new StackerCraneCommandConfig();
         }
 
@@ -74,144 +73,60 @@
                     return Task.CompletedTask;
                 }
 
+                // 璁㈤槄涓�娆′换鍔″畬鎴愪簨浠躲��
                 if (!commonStackerCrane.IsEventSubscribed)
                 {
                     commonStackerCrane.StackerCraneTaskCompletedEventHandler += CommonStackerCrane_StackerCraneTaskCompletedEventHandler;
                 }
 
-                if (commonStackerCrane.IsCanSendTask(commonStackerCrane.Communicator, commonStackerCrane.DeviceProDTOs, commonStackerCrane.DeviceProtocolDetailDTOs))
+                if (!commonStackerCrane.IsCanSendTask(commonStackerCrane.Communicator, commonStackerCrane.DeviceProDTOs, commonStackerCrane.DeviceProtocolDetailDTOs))
                 {
-                    commonStackerCrane.CheckStackerCraneTaskCompleted();
+                    return Task.CompletedTask;
+                }
 
-                    Dt_Task? task = GetTask(commonStackerCrane);
-                    if (task != null)
-                    {
-                        object? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
-                        if (stackerCraneTaskCommand != null)
-                        {
-                            bool sendFlag = SendStackerCraneCommand(commonStackerCrane, stackerCraneTaskCommand);
-                            if (sendFlag)
-                            {
-                                commonStackerCrane.LastTaskType = task.TaskType;
-                                _taskService.UpdateTaskStatusToNext(task.TaskNum);
-                            }
-                        }
-                    }
+                commonStackerCrane.CheckStackerCraneTaskCompleted();
+
+                // 浠诲姟閫夋嫨涓嬫矇鍒颁笓鐢ㄩ�夋嫨鍣ㄣ��
+                Dt_Task? task = _taskSelector.SelectTask(commonStackerCrane);
+                if (task == null)
+                {
+                    return Task.CompletedTask;
+                }
+
+                // 鍛戒护鏋勫缓涓嬫矇鍒颁笓鐢ㄦ瀯寤哄櫒銆�
+                object? stackerCraneTaskCommand = _commandBuilder.ConvertToStackerCraneTaskCommand(task);
+                if (stackerCraneTaskCommand == null)
+                {
+                    return Task.CompletedTask;
+                }
+
+                bool sendFlag = SendStackerCraneCommand(commonStackerCrane, stackerCraneTaskCommand);
+                if (sendFlag)
+                {
+                    commonStackerCrane.LastTaskType = task.TaskType;
+                    _taskService.UpdateTaskStatusToNext(task.TaskNum);
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine($"CommonStackerCraneJob Error: {ex.Message}");
             }
+
             return Task.CompletedTask;
         }
 
         /// <summary>
         /// 浠诲姟瀹屾垚浜嬩欢璁㈤槄鐨勬柟娉�
         /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e)
+        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, StackerCraneTaskCompletedEventArgs e)
         {
             CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane;
             if (commonStackerCrane != null)
             {
-                //if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5)
-                //{
                 Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
                 _taskService.StackCraneTaskCompleted(e.TaskNum);
                 commonStackerCrane.SetValue(StackerCraneDBName.WorkAction, 5);
-                //}
             }
-        }
-
-        /// <summary>
-        /// 鑾峰彇浠诲姟
-        /// </summary>
-        /// <param name="commonStackerCrane">鍫嗗灈鏈哄璞�</param>
-        /// <returns></returns>
-        private Dt_Task? GetTask(IStackerCrane commonStackerCrane)
-        {
-            Dt_Task? task = null;
-            if (commonStackerCrane.LastTaskType == null)
-            {
-                task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
-            }
-            else
-            {
-                if (commonStackerCrane.LastTaskType.GetValueOrDefault().GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
-                {
-                    task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
-                    task ??= _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
-                }
-                else
-                {
-                    task = _taskService.QueryStackerCraneOutTask(commonStackerCrane.DeviceCode);
-                }
-            }
-
-            if (task != null && task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup)
-            {
-                if (IsOutTaskStationAvailable(task))
-                {
-                    return task;
-                }
-
-                List<string> otherOutStationCodes = _routerService.QueryNextRoutes(commonStackerCrane.DeviceCode, task.NextAddress, task.TaskType)
-                    .Select(x => x.ChildPosi).ToList();
-                List<Dt_Task> tasks = _taskService.QueryStackerCraneOutTasks(commonStackerCrane.DeviceCode, otherOutStationCodes);
-                foreach (var alternativeTask in tasks)
-                {
-                    if (IsOutTaskStationAvailable(alternativeTask))
-                    {
-                        return alternativeTask;
-                    }
-                }
-                task = _taskService.QueryStackerCraneInTask(commonStackerCrane.DeviceCode);
-            }
-
-            return task;
-        }
-
-        /// <summary>
-        /// 鍑哄簱浠诲姟鍒ゆ柇鍑哄簱绔欏彴鏄惁鍙敤
-        /// </summary>
-        /// <param name="task">浠诲姟瀹炰綋</param>
-        /// <returns>濡傛灉绔欏彴鍙敤杩斿洖true锛屽惁鍒欒繑鍥瀎alse</returns>
-        private bool IsOutTaskStationAvailable([NotNull] Dt_Task task)
-        {
-            Dt_Router? router = _routerService.QueryNextRoute(task.Roadway, task.NextAddress, task.TaskType);
-            if (router == null)
-            {
-                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鏈壘鍒扮珯鍙般�恵task.NextAddress}銆戜俊鎭紝鏃犳硶鏍¢獙绔欏彴");
-                return false;
-            }
-
-            IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == router.ChildPosiDeviceCode);
-            if (device == null)
-            {
-                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鏈壘鍒板嚭搴撶珯鍙般�恵router.ChildPosiDeviceCode}銆戝搴旂殑閫氳瀵硅薄锛屾棤娉曞垽鏂嚭搴撶珯鍙版槸鍚﹁鍗犵敤");
-                return false;
-            }
-
-            CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
-            return conveyorLine.IsOccupied(router.ChildPosi);
-        }
-
-        /// <summary>
-        /// 浠诲姟瀹炰綋杞崲鎴愬懡浠odel
-        /// </summary>
-        public object? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
-        {
-            // 鏍规嵁閰嶇疆鍒ゆ柇鍛戒护绫诲瀷
-            string commandType = GetCommandType(task.Roadway);
-
-            // 鍒涘缓骞舵瀯寤哄懡浠�
-            return commandType switch
-            {
-                "Formation" => BuildCommand(task, CreateFormationCommand(task)),
-                _ => BuildCommand(task, CreateStandardCommand(task))
-            };
         }
 
         private static bool SendStackerCraneCommand(IStackerCrane commonStackerCrane, object command)
@@ -223,180 +138,5 @@
                 _ => false
             };
         }
-
-        /// <summary>
-        /// 鏍规嵁 Roadway 鑾峰彇鍛戒护绫诲瀷
-        /// </summary>
-        private string GetCommandType(string roadway)
-        {
-            foreach (var mapping in _config.RoadwayCommandMapping)
-            {
-                if (roadway.Contains(mapping.Key))
-                {
-                    return mapping.Value;
-                }
-            }
-            return _config.DefaultCommandType;
-        }
-
-        /// <summary>
-        /// 鍒涘缓鏍囧噯鍫嗗灈鏈哄懡浠�
-        /// </summary>
-        private static StackerCraneTaskCommand CreateStandardCommand(Dt_Task task)
-        {
-            return new StackerCraneTaskCommand
-            {
-                //Barcode = task.PalletCode,
-                TaskNum = task.TaskNum,
-                WorkType = 1,
-                WorkAction = 1
-            };
-        }
-
-        /// <summary>
-        /// 鍒涘缓鍒嗗鍫嗗灈鏈哄懡浠�
-        /// </summary>
-        private static FormationStackerCraneTaskCommand CreateFormationCommand(Dt_Task task)
-        {
-            return new FormationStackerCraneTaskCommand
-            {
-                Barcode = task.PalletCode,
-                TaskNum = task.TaskNum,
-                WorkType = 1,
-                WorkAction = 1,
-                FireAlarm = 0,
-                HeartBeat = 0,
-                FieldName = string.Empty
-            };
-        }
-
-        /// <summary>
-        /// 閫氱敤鍛戒护鏋勫缓鏂规硶
-        /// </summary>
-        private T? BuildCommand<T>(Dt_Task task, T command) where T : class
-        {
-            TaskTypeGroup taskTypeGroup = task.TaskType.GetTaskTypeGroup();
-
-            return taskTypeGroup switch
-            {
-                TaskTypeGroup.InboundGroup => BuildInboundCommand(task, command),
-                TaskTypeGroup.OutbondGroup => BuildOutboundCommand(task, command),
-                TaskTypeGroup.RelocationGroup => BuildRelocationCommand(task, command),
-                _ => command
-            };
-        }
-
-        /// <summary>
-        /// 閫氱敤鍏ュ簱鍛戒护鏋勫缓
-        /// </summary>
-        private T? BuildInboundCommand<T>(Dt_Task task, T command) where T : class
-        {
-            Dt_Router? router = _routerService.QueryNextRoute(task.CurrentAddress, task.Roadway, task.TaskType);
-            if (router == null)
-            {
-                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鏈壘鍒扮珯鍙般�恵task.CurrentAddress}銆戜俊鎭紝鏃犳硶鑾峰彇瀵瑰簲鐨勫爢鍨涙満鍙栬揣绔欏彴淇℃伅");
-                return null;
-            }
-
-            SetCommandProperty(command, "StartRow", Convert.ToInt16(router.SrmRow));
-            SetCommandProperty(command, "StartColumn", Convert.ToInt16(router.SrmColumn));
-            SetCommandProperty(command, "StartLayer", Convert.ToInt16(router.SrmLayer));
-
-            if (!TryParseAddress(task.NextAddress, out short endRow, out short endColumn, out short endLayer))
-            {
-                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鍏ュ簱浠诲姟缁堢偣閿欒锛岀粓鐐癸細銆恵task.NextAddress}銆�");
-                return null;
-            }
-
-            SetCommandProperty(command, "EndRow", endRow);
-            SetCommandProperty(command, "EndColumn", endColumn);
-            SetCommandProperty(command, "EndLayer", endLayer);
-
-            return command;
-        }
-
-        /// <summary>
-        /// 閫氱敤鍑哄簱鍛戒护鏋勫缓
-        /// </summary>
-        private T? BuildOutboundCommand<T>(Dt_Task task, T command) where T : class
-        {
-            Dt_Router? router = _routerService.QueryNextRoute(task.Roadway, task.TargetAddress, task.TaskType);
-            if (router == null)
-            {
-                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鏈壘鍒扮珯鍙般�恵task.TargetAddress}銆戜俊鎭紝鏃犳硶鑾峰彇瀵瑰簲鐨勫爢鍨涙満鏀捐揣绔欏彴淇℃伅");
-                return null;
-            }
-
-            SetCommandProperty(command, "EndRow", Convert.ToInt16(router.SrmRow));
-            SetCommandProperty(command, "EndColumn", Convert.ToInt16(router.SrmColumn));
-            SetCommandProperty(command, "EndLayer", Convert.ToInt16(router.SrmLayer));
-
-            if (!TryParseAddress(task.CurrentAddress, out short startRow, out short startColumn, out short startLayer))
-            {
-                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"鍑哄簱浠诲姟璧风偣閿欒锛岃捣鐐癸細銆恵task.CurrentAddress}銆�");
-                return null;
-            }
-
-            SetCommandProperty(command, "StartRow", startRow);
-            SetCommandProperty(command, "StartColumn", startColumn);
-            SetCommandProperty(command, "StartLayer", startLayer);
-
-            return command;
-        }
-
-        /// <summary>
-        /// 閫氱敤绉诲簱鍛戒护鏋勫缓
-        /// </summary>
-        private T? BuildRelocationCommand<T>(Dt_Task task, T command) where T : class
-        {
-            if (!TryParseAddress(task.NextAddress, out short endRow, out short endColumn, out short endLayer))
-            {
-                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"绉诲簱浠诲姟缁堢偣閿欒锛岀粓鐐癸細銆恵task.NextAddress}銆�");
-                return null;
-            }
-
-            SetCommandProperty(command, "EndRow", endRow);
-            SetCommandProperty(command, "EndColumn", endColumn);
-            SetCommandProperty(command, "EndLayer", endLayer);
-
-            if (!TryParseAddress(task.CurrentAddress, out short startRow, out short startColumn, out short startLayer))
-            {
-                _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"绉诲簱浠诲姟璧风偣閿欒锛岃捣鐐癸細銆恵task.CurrentAddress}銆�");
-                return null;
-            }
-
-            SetCommandProperty(command, "StartRow", startRow);
-            SetCommandProperty(command, "StartColumn", startColumn);
-            SetCommandProperty(command, "StartLayer", startLayer);
-
-            return command;
-        }
-
-        /// <summary>
-        /// 浣跨敤鍙嶅皠璁剧疆鍛戒护灞炴��
-        /// </summary>
-        private static void SetCommandProperty<T>(T command, string propertyName, object value) where T : class
-        {
-            var property = typeof(T).GetProperty(propertyName);
-            property?.SetValue(command, value);
-        }
-
-        /// <summary>
-        /// 瑙f瀽鍦板潃瀛楃涓诧紙鏍煎紡锛氳-鍒�-灞傦級
-        /// </summary>
-        private bool TryParseAddress(string address, out short row, out short column, out short layer)
-        {
-            row = column = layer = 0;
-
-            string[] parts = address.Split("-");
-            if (parts.Length != 3)
-            {
-                return false;
-            }
-
-            return short.TryParse(parts[0], out row)
-                && short.TryParse(parts[1], out column)
-                && short.TryParse(parts[2], out layer);
-        }
     }
-}
\ No newline at end of file
+}

--
Gitblit v1.9.3