From 5c766d7e5c969b7530a014ded771973e242f25e0 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 18 三月 2026 14:51:07 +0800
Subject: [PATCH] refactor: 拆分 TaskService 任务流程并补充注释

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/RobotTaskService.cs |  210 ++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 194 insertions(+), 16 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/RobotTaskService.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/RobotTaskService.cs
index cc96eac..465fcb5 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/RobotTaskService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/RobotTaskService.cs
@@ -17,25 +17,32 @@
 
 #endregion << 鐗� 鏈� 娉� 閲� >>
 
-using AutoMapper;
+using MapsterMapper;
+using Microsoft.Extensions.Configuration;
+using Newtonsoft.Json;
 using SqlSugar;
 using System.Diagnostics.CodeAnalysis;
+using WIDESEA_Core;
+using WIDESEAWCS_Common.HttpEnum;
 using WIDESEAWCS_Common.TaskEnum;
 using WIDESEAWCS_Core;
 using WIDESEAWCS_Core.BaseServices;
-using WIDESEAWCS_Core.Enums;
+using WIDESEAWCS_Core.Helper;
+using WIDESEAWCS_DTO.Stock;
 using WIDESEAWCS_DTO.TaskInfo;
 using WIDESEAWCS_ITaskInfoRepository;
 using WIDESEAWCS_ITaskInfoService;
 using WIDESEAWCS_Model.Models;
-using WIDESEAWCS_QuartzJob.Models;
-using WIDESEAWCS_QuartzJob.Service;
+using WIDESEAWCS_QuartzJob;
+using WIDESEAWCS_QuartzJob.DTO;
 
 namespace WIDESEAWCS_TaskInfoService
 {
     public class RobotTaskService : ServiceBase<Dt_RobotTask, IRobotTaskRepository>, IRobotTaskService
     {
         private readonly IMapper _mapper;
+        private readonly HttpClientHelper _httpClientHelper;
+        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
 
         private Dictionary<string, OrderByType> _taskOrderBy = new()
             {
@@ -52,22 +59,51 @@
 
         public List<int> TaskRobotTypes => typeof(TaskOtherTypeEnum).GetEnumIndexList();
 
-        public RobotTaskService(IRobotTaskRepository BaseDal, IMapper mapper) : base(BaseDal)
+        public RobotTaskService(IRobotTaskRepository BaseDal, IMapper mapper, HttpClientHelper httpClientHelper, ITaskExecuteDetailService taskExecuteDetailService) : base(BaseDal)
         {
             _mapper = mapper;
+            _httpClientHelper = httpClientHelper;
+            _taskExecuteDetailService = taskExecuteDetailService;
         }
 
-        //public override WebResponseContent DeleteData(object[] keys)
-        //{
-        //    List<int> taskKeys = new List<int>();
-        //    for (int i = 0; i < keys.Length; i++)
-        //    {
-        //        taskKeys.Add(Convert.ToInt32(keys[i]));
-        //    }
-        //    List<Dt_RobotTask> tasks = BaseDal.QueryData(x => taskKeys.Contains(x.RobotTaskId));
-        //    BaseDal.DeleteAndMoveIntoHty(tasks, OperateTypeEnum.浜哄伐鍒犻櫎);
-        //    return WebResponseContent.Instance.OK($"鎴愬姛鍒犻櫎{tasks.Count}鏉℃暟鎹�");
-        //}
+        public WebResponseContent ReceiveWMSTask([NotNull] WMSTaskDTO taskDTO, StockDTO stockDTO)
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                if (BaseDal.QueryFirst(x => x.RobotTaskNum == taskDTO.TaskNum || x.RobotSourceAddressPalletCode == taskDTO.PalletCode) != null)
+                {
+                    return content.Error("浠诲姟宸插瓨鍦�");
+                }
+
+                Dt_RobotTask task = new Dt_RobotTask
+                {
+                    RobotTaskNum = taskDTO.TaskNum,
+                    RobotSourceAddressLineCode = stockDTO.SourceLineNo,
+                    RobotTargetAddressLineCode = stockDTO.TargetLineNo,
+                    RobotRoadway = stockDTO.Roadway,
+                    RobotSourceAddress = stockDTO.SourceLineNo,
+                    RobotTargetAddress = stockDTO.TargetLineNo,
+                    RobotSourceAddressPalletCode = stockDTO.SourcePalletNo,
+                    RobotTargetAddressPalletCode = stockDTO.TargetPalletNo,
+                    RobotTaskType = taskDTO.TaskType,
+                    RobotTaskState = taskDTO.TaskState,
+                    RobotGrade = taskDTO.Grade,
+                    Creater = "WMS"
+                };
+
+                BaseDal.AddData(task);
+
+                _taskExecuteDetailService.AddTaskExecuteDetail(new List<int> { task.RobotTaskNum }, "鎺ユ敹WMS浠诲姟");
+
+                content = WebResponseContent.Instance.OK("鎴愬姛", task);
+            }
+            catch (Exception ex)
+            {
+                content = WebResponseContent.Instance.Error($"浠诲姟鎺ユ敹閿欒,閿欒淇℃伅:{ex.Message}");
+            }
+            return content;
+        }
 
         public Dt_RobotTask? QueryRobotCraneTask(string deviceCode)
         {
@@ -78,5 +114,147 @@
         {
             return await BaseDal.UpdateDataAsync(robotTask);
         }
+
+        /// <summary>
+        /// 鑾峰彇WMS绯荤粺鏈烘鎵嬩换鍔�
+        /// </summary>
+        /// <param name="task"></param>
+        /// <returns></returns>
+        public WebResponseContent GetWMSRobotTask(Dt_Task task)
+        {
+            string configKey = ResolveRobotTaskConfigKey(task.TargetAddress);
+            StockDTO stock = BuildRobotTaskStock(task, configKey);
+
+            var result = _httpClientHelper.Post<WebResponseContent>(configKey, stock.ToJson());
+
+            if (!result.IsSuccess || !result.Data.Status)
+                return WebResponseContent.Instance.Error($"鑾峰彇WMS绯荤粺鏈烘鎵嬩换鍔″け璐�,浠诲姟鍙�:銆恵task.TaskNum}銆�,鎵樼洏鍙�:銆恵task.PalletCode}銆�,鐩爣鍦板潃:銆恵task.TargetAddress}銆�,鎺ュ彛:銆恵configKey}銆�,閿欒淇℃伅:銆恵result.Data?.Message}銆�");
+
+            var wMSTask = JsonConvert.DeserializeObject<WMSTaskDTO>(result.Data.Data?.ToString() ?? string.Empty);
+            if (wMSTask == null)
+                return WebResponseContent.Instance.Error($"鑾峰彇WMS绯荤粺鏈烘鎵嬩换鍔″け璐�,浠诲姟鍙�:銆恵task.TaskNum}銆�,鎵樼洏鍙�:銆恵task.PalletCode}銆�,閿欒淇℃伅:銆怶MS鏈繑鍥炴湁鏁堜换鍔℃暟鎹��");
+
+            return ReceiveWMSTask(wMSTask, stock);
+        }
+
+        /// <summary>
+        /// 鏍规嵁杈撻�佺嚎鐩爣鍦板潃瑙f瀽鏈烘鎵嬩换鍔℃帴鍙c��
+        /// 瑙勫垯锛�
+        /// 1. 浠庨厤缃鍙栫簿纭湴鍧�鏄犲皠锛圓ddressMap锛�
+        /// 2. 鏈懡涓椂榛樿鎹㈢洏
+        /// </summary>
+        public string ResolveRobotTaskConfigKey(string? targetAddress)
+        {
+            string address = (targetAddress ?? string.Empty).Trim();
+            if (string.IsNullOrWhiteSpace(address))
+                return nameof(ConfigKey.CreateRobotChangePalletTask);
+
+            var section = App.Configuration.GetSection("RobotTaskAddressRules");
+            var addressMap = ReadRobotRuleMap(section.GetSection("AddressMap"));
+            if (addressMap.TryGetValue(address, out string? exactTaskType))
+                return MapRobotTaskTypeToConfigKey(exactTaskType);
+
+            return nameof(ConfigKey.CreateRobotChangePalletTask);
+        }
+
+        public int MapWarehouseIdConfigKey(string? targetAddress)
+        {
+            return targetAddress switch
+            {
+                "11068" => 1,
+                "11001" => 3,
+                "11010" => 3,
+                "10010" => 1,
+                "10030" => 1,
+                _ => 1
+            };
+        }
+
+        /// <summary>
+        /// 灏嗛厤缃换鍔$被鍨嬭浆鎹负鎺ュ彛閰嶇疆閿��
+        /// 鏀寔鍊硷細Split/Group/Change锛堝ぇ灏忓啓涓嶆晱鎰燂級
+        /// </summary>
+        public string MapRobotTaskTypeToConfigKey(string? taskType)
+        {
+            string type = (taskType ?? string.Empty).Trim().ToLowerInvariant();
+            return type switch
+            {
+                "split" => nameof(ConfigKey.CreateRobotSplitPalletTask),
+                "group" => nameof(ConfigKey.CreateRobotGroupPalletTask),
+                _ => nameof(ConfigKey.CreateRobotChangePalletTask)
+            };
+        }
+
+        /// <summary>
+        /// 鏍规嵁鎺ュ彛绫诲瀷鏋勫缓鏈烘鎵嬩换鍔″叆鍙傘��
+        /// </summary>
+        public StockDTO BuildRobotTaskStock(Dt_Task task, string configKey)
+        {
+            string targetAddress = task.TargetAddress ?? string.Empty;
+            string roadway = ResolveRobotRuleValue(targetAddress, "AddressRoadwayMap", task.Roadway);
+            string sourceLineNo = ResolveRobotRuleValue(targetAddress, "AddressSourceLineNoMap", task.SourceAddress);
+
+            var stock = new StockDTO
+            {
+                Roadway = roadway,
+                SourceLineNo = sourceLineNo,
+                TargetLineNo = task.TargetAddress,
+                SourcePalletNo = task.PalletCode,
+                TargetPalletNo = task.PalletCode
+            };
+
+            if (configKey == nameof(ConfigKey.CreateRobotSplitPalletTask))
+            {
+                stock.TargetPalletNo = string.Empty;
+            }
+            else if (configKey == nameof(ConfigKey.CreateRobotGroupPalletTask))
+            {
+                stock.SourcePalletNo = string.Empty;
+            }
+            else if (configKey == nameof(ConfigKey.CreateRobotChangePalletTask))
+            {
+                IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceProDTOs.Any(d => d.DeviceChildCode == sourceLineNo));
+                if (device != null)
+                {
+                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
+
+                    DeviceProDTO? devicePro = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(ConveyorLineDBNameNew.Barcode) && x.DeviceChildCode == sourceLineNo);
+                    ConveyorLineTaskCommandNew command = conveyorLine.ReadCustomer<ConveyorLineTaskCommandNew>(sourceLineNo);  // 娴嬭瘯鐢�
+                    //var barcode = conveyorLine.GetValue<ConveyorLineDBNameNew, string>(ConveyorLineDBNameNew.Barcode, sourceLineNo);
+                    stock.SourcePalletNo = string.IsNullOrEmpty(command.Barcode.Replace("\0", "").ToString()) ? string.Empty : command.Barcode.Replace("\0", "").ToString();
+                }
+            }
+
+            return stock;
+        }
+
+        /// <summary>
+        /// 鏍规嵁鐩爣鍦板潃鎸夈�岀簿纭� > 鍥為��鍊笺�嶈В鏋愯鍒欏�笺��
+        /// </summary>
+        public string ResolveRobotRuleValue(string? targetAddress, string addressSectionName, string? fallback)
+        {
+            string address = (targetAddress ?? string.Empty).Trim();
+            string defaultValue = fallback ?? string.Empty;
+            if (string.IsNullOrWhiteSpace(address))
+                return defaultValue;
+
+            var section = App.Configuration.GetSection("RobotTaskAddressRules");
+            var addressMap = ReadRobotRuleMap(section.GetSection(addressSectionName));
+            if (addressMap.TryGetValue(address, out string? value))
+                return value;
+
+            return defaultValue;
+        }
+
+        /// <summary>
+        /// 璇诲彇瑙勫垯鏄犲皠娈点��
+        /// </summary>
+        private Dictionary<string, string> ReadRobotRuleMap(IConfigurationSection section)
+        {
+            return section
+                .GetChildren()
+                .Where(x => !string.IsNullOrWhiteSpace(x.Key) && !string.IsNullOrWhiteSpace(x.Value))
+                .ToDictionary(x => x.Key.Trim(), x => x.Value!.Trim());
+        }
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3