From fe2a1e74780259605cd230e6f9c629c3dd7fdf15 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期五, 10 四月 2026 20:19:06 +0800
Subject: [PATCH] feat: 添加消防任务支持并修复堆垛机状态处理

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_WCS.cs |  229 ++++++++++++++++++++++++---------------------------------
 1 files changed, 97 insertions(+), 132 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_WCS.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_WCS.cs
index 76a54ef..8d2a3ac 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_WCS.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_WCS.cs
@@ -182,22 +182,53 @@
                 if (stockInfo == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴斿簱瀛樹俊鎭�");
 
                 // 鍒ゆ柇鏄笉鏄瀬鍗峰簱浠诲姟
-                //if (taskDto.WarehouseId == (int)WarehouseEnum.FJ1 || taskDto.WarehouseId == (int)WarehouseEnum.ZJ1)
-                //{
-                //    return WebResponseContent.Instance.OK();
-                //}
+                if (taskDto.WarehouseId == (int)WarehouseEnum.FJ1 || taskDto.WarehouseId == (int)WarehouseEnum.ZJ1)
+                {
+
+                    return await CompleteAgvInboundTaskAsync(taskDto);
+                }
 
                 return await ExecuteWithinTransactionAsync(async () =>
                 {
                     WebResponseContent content = new WebResponseContent();
                     stockInfo.LocationCode = location.LocationCode;
                     stockInfo.LocationId = location.Id;
-                    stockInfo.OutboundDate = task.Roadway switch
+
+                    var now = DateTime.Now;
+                    if (task.Roadway.Contains("GW"))
                     {
-                        var r when r.Contains("GW") => DateTime.Now.AddHours(2),
-                        var r when r.Contains("CW") => DateTime.Now.AddHours(1),
-                        _ => DateTime.Now
-                    };
+                        if (stockInfo.Remark.IsNullOrEmpty())
+                        {
+                            stockInfo.OutboundDate = now.AddHours(16);
+                            stockInfo.Remark = "GW_1";
+                        }
+                        else if (stockInfo.Remark == "GW_1")
+                        {
+                            stockInfo.OutboundDate = now.AddHours(24);
+                            stockInfo.Remark = "GW_2";
+                        }
+                        else
+                        {
+                            stockInfo.OutboundDate = now.AddHours(16);
+                        }
+                    }
+                    else if (task.Roadway.Contains("CW"))
+                    {
+                        if (stockInfo.Remark == "GW_2")
+                        {
+                            stockInfo.OutboundDate = now.AddHours(12);
+                            stockInfo.Remark = "CW_1";
+                        }
+                        else
+                        {
+                            stockInfo.OutboundDate = now.AddHours(12);
+                        }
+                    }
+                    else
+                    {
+                        stockInfo.OutboundDate = now;
+                    }
+
                     stockInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.GetHashCode();
 
                     location.LocationStatus = LocationStatusEnum.InStock.GetHashCode();
@@ -207,18 +238,18 @@
                     if (!updateLocationResult || !updateStockResult)
                         return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
                     // 璋冪敤MES鎵樼洏杩涚珯
-                    var inboundRequest = new InboundInContainerRequest
-                    {
-                        EquipmentCode = "STK-GROUP-001",
-                        ResourceCode = "STK-GROUP-001",
-                        LocalTime = DateTime.Now,
-                        ContainerCode = taskDto.PalletCode
-                    };
-                    var inboundResult = _mesService.InboundInContainer(inboundRequest);
-                    if (inboundResult == null || inboundResult.Data == null || !inboundResult.Data.IsSuccess)
-                    {
-                        return content.Error($"浠诲姟瀹屾垚澶辫触锛歁ES杩涚珯澶辫触: {inboundResult?.Data?.Msg ?? inboundResult?.ErrorMessage ?? "鏈煡閿欒"}");
-                    }
+                    //var inboundRequest = new InboundInContainerRequest
+                    //{
+                    //    EquipmentCode = "STK-GROUP-001",
+                    //    ResourceCode = "STK-GROUP-001",
+                    //    LocalTime = DateTime.Now,
+                    //    ContainerCode = taskDto.PalletCode
+                    //};
+                    //var inboundResult = _mesService.InboundInContainer(inboundRequest);
+                    //if (inboundResult == null || inboundResult.Data == null || !inboundResult.Data.IsSuccess)
+                    //{
+                    //    return content.Error($"浠诲姟瀹屾垚澶辫触锛歁ES杩涚珯澶辫触: {inboundResult?.Data?.Msg ?? inboundResult?.ErrorMessage ?? "鏈煡閿欒"}");
+                    //}
                     return await CompleteTaskAsync(task, "鍏ュ簱瀹屾垚");
                 });
             }
@@ -249,8 +280,8 @@
                 {
                     OutTaskCompleteDto outTaskCompleteDto = new OutTaskCompleteDto()
                     {
-                        TaskId = task.OrderNo,
-                        DevId = task.TargetAddress,
+                        TaskId = task.OrderNo ?? string.Empty,
+                        DevId = task.TargetAddress ?? string.Empty,
                         ReqTime = DateTime.Now.ToString()
                     };
                     return await OutTaskComplete(outTaskCompleteDto);
@@ -260,7 +291,7 @@
                 return await ExecuteWithinTransactionAsync(async () =>
                 {
                     stockInfo.LocationId = 0;
-                    stockInfo.LocationCode = null;
+                    stockInfo.LocationCode = string.Empty;
                     stockInfo.OutboundDate = DateTime.Now;
 
                     location.LocationStatus = LocationStatusEnum.Free.GetHashCode();
@@ -396,19 +427,13 @@
                     if (!updateLocationResult || !updateStockResult)
                         return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
 
-                    // 淇濆瓨浠诲姟鍘嗗彶
-                    var historyTask = _mapper.Map<Dt_Task_Hty>(task);
-                    historyTask.InsertTime = DateTime.Now;
-                    historyTask.OperateType = "绌烘墭鐩樺叆搴撳畬鎴�";
-                    if (await _task_HtyService.Repository.AddDataAsync(historyTask) <= 0)
-                        return WebResponseContent.Instance.Error("浠诲姟鍘嗗彶淇濆瓨澶辫触");
+                    var saveTaskHistoryResult = await SaveTaskHistoryAsync(task, "绌烘墭鐩樺叆搴撳畬鎴�");
+                    if (!saveTaskHistoryResult.Status)
+                        return saveTaskHistoryResult;
 
-                    // 淇濆瓨搴撳瓨鍘嗗彶
-                    var historyStock = _mapper.Map<Dt_StockInfo_Hty>(stockInfo);
-                    historyStock.InsertTime = DateTime.Now;
-                    historyStock.OperateType = "绌烘墭鐩樺叆搴撳畬鎴�";
-                    if (await _stockInfo_HtyService.Repository.AddDataAsync(historyStock) <= 0)
-                        return WebResponseContent.Instance.Error("搴撳瓨鍘嗗彶淇濆瓨澶辫触");
+                    var saveStockHistoryResult = await SaveStockHistoryAsync(stockInfo, "绌烘墭鐩樺叆搴撳畬鎴�");
+                    if (!saveStockHistoryResult.Status)
+                        return saveStockHistoryResult;
 
                     var deleteResult = await BaseDal.DeleteDataAsync(task);
                     if (!deleteResult) return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
@@ -431,6 +456,13 @@
         {
             try
             {
+                var dt_Task = await BaseDal.QueryFirstAsync(x => x.TargetAddress == taskDto.TargetAddress);
+                if (!dt_Task.IsNullOrEmpty())
+                {
+                    var taskDTO = dt_Task.Adapt<WMSTaskDTO>();
+                    return WebResponseContent.Instance.OK("浠诲姟鍒涘缓鎴愬姛", taskDTO);
+                }
+
                 var stockInfo = await _stockInfoService.Repository.QueryDataNavFirstAsync(x => x.LocationDetails.WarehouseId == taskDto.WarehouseId && x.LocationDetails.LocationStatus == LocationStatusEnum.InStock.GetHashCode() && x.StockStatus == StockStatusEmun.绌烘墭鐩樺簱瀛�.GetHashCode());
                 if (stockInfo == null)
                     return WebResponseContent.Instance.Error("鏈壘鍒板搴旂殑搴撳瓨淇℃伅");
@@ -451,12 +483,22 @@
                     TaskNum = await BaseDal.GetTaskNo(),
                     Creater = "system",
                 };
-                var taskDtos = task.Adapt<WMSTaskDTO>();
 
-                var addResult = await BaseDal.AddDataAsync(task) > 0;
-                if (!addResult)
-                    return WebResponseContent.Instance.Error("浠诲姟鍒涘缓澶辫触");
-                return WebResponseContent.Instance.OK("浠诲姟鍒涘缓鎴愬姛", taskDtos);
+                return await ExecuteWithinTransactionAsync(async () =>
+                {
+                    var locationInfo = await _locationInfoService.GetLocationInfoAsync(stockInfo.LocationId);
+                    locationInfo.LocationStatus = LocationStatusEnum.InStockLock.GetHashCode();
+                    var updateLocationResult = await _locationInfoService.UpdateLocationInfoAsync(locationInfo);
+                    if (!updateLocationResult)
+                        return WebResponseContent.Instance.Error("浠诲姟鍒涘缓澶辫触");
+
+                    var taskDtos = task.Adapt<WMSTaskDTO>();
+
+                    var addResult = await BaseDal.AddDataAsync(task) > 0;
+                    if (!addResult)
+                        return WebResponseContent.Instance.Error("浠诲姟鍒涘缓澶辫触");
+                    return WebResponseContent.Instance.OK("浠诲姟鍒涘缓鎴愬姛", taskDtos);
+                });
             }
             catch (Exception ex)
             {
@@ -483,7 +525,7 @@
                 return await ExecuteWithinTransactionAsync(async () =>
                 {
                     stockInfo.LocationId = 0;
-                    stockInfo.LocationCode = null;
+                    stockInfo.LocationCode = string.Empty;
                     stockInfo.StockStatus = StockStatusEmun.鍑哄簱瀹屾垚.GetHashCode();
 
                     location.LocationStatus = LocationStatusEnum.Free.GetHashCode();
@@ -493,19 +535,13 @@
                     if (!updateLocationResult || !updateStockResult)
                         return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
 
-                    // 淇濆瓨浠诲姟鍘嗗彶
-                    var historyTask = _mapper.Map<Dt_Task_Hty>(task);
-                    historyTask.InsertTime = DateTime.Now;
-                    historyTask.OperateType = "绌烘墭鐩樺嚭搴撳畬鎴�";
-                    if (await _task_HtyService.Repository.AddDataAsync(historyTask) <= 0)
-                        return WebResponseContent.Instance.Error("浠诲姟鍘嗗彶淇濆瓨澶辫触");
+                    var saveTaskHistoryResult = await SaveTaskHistoryAsync(task, "绌烘墭鐩樺嚭搴撳畬鎴�");
+                    if (!saveTaskHistoryResult.Status)
+                        return saveTaskHistoryResult;
 
-                    // 淇濆瓨搴撳瓨鍘嗗彶
-                    var historyStock = _mapper.Map<Dt_StockInfo_Hty>(stockInfo);
-                    historyStock.InsertTime = DateTime.Now;
-                    historyStock.OperateType = "绌烘墭鐩樺嚭搴撳畬鎴�";
-                    if (await _stockInfo_HtyService.Repository.AddDataAsync(historyStock) <= 0)
-                        return WebResponseContent.Instance.Error("搴撳瓨鍘嗗彶淇濆瓨澶辫触");
+                    var saveStockHistoryResult = await SaveStockHistoryAsync(stockInfo, "绌烘墭鐩樺嚭搴撳畬鎴�");
+                    if (!saveStockHistoryResult.Status)
+                        return saveStockHistoryResult;
 
                     var deleteResult = await BaseDal.DeleteDataAsync(task);
                     if (!deleteResult) return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
@@ -542,78 +578,6 @@
             {
                 return WebResponseContent.Instance.Error($"淇敼澶辫触: {ex.Message}");
             }
-        }
-
-        /// <summary>
-        /// 鏌ユ壘鎵樼洏鏄惁鏈変换鍔�
-        /// </summary>
-        /// <param name="palletCode"></param>
-        /// <returns></returns>
-        private async Task<WebResponseContent> GetTaskByPalletCodeAsync(string palletCode)
-        {
-            try
-            {
-                var task = await BaseDal.QueryFirstAsync(s => s.PalletCode == palletCode);
-                if (task == null)
-                    return WebResponseContent.Instance.Error("鏈壘鍒板搴旂殑浠诲姟");
-                var taskDto = _mapper.Map<WMSTaskDTO>(task);
-                return WebResponseContent.Instance.OK("鏌ヨ鎴愬姛", taskDto);
-            }
-            catch (Exception ex)
-            {
-                return WebResponseContent.Instance.Error($"鏌ヨ浠诲姟澶辫触: {ex.Message}");
-            }
-        }
-
-        /// <summary>
-        /// 瀹屾垚浠诲姟鍚庣粺涓�澶勭悊锛堝垹闄や换鍔℃暟鎹級
-        /// </summary>
-        private async Task<WebResponseContent> CompleteTaskAsync(Dt_Task task, string operateType = "")
-        {
-            var deleteTaskResult = await BaseDal.DeleteDataAsync(task);
-            if (!deleteTaskResult) return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
-
-            var historyTask = _mapper.Map<Dt_Task_Hty>(task);
-            historyTask.InsertTime = DateTime.Now;
-            historyTask.OperateType = operateType;
-            var saveResult = await _task_HtyService.Repository.AddDataAsync(historyTask) > 0;
-            if (!saveResult) return WebResponseContent.Instance.Error("浠诲姟鍘嗗彶淇濆瓨澶辫触");
-
-            return WebResponseContent.Instance.OK("浠诲姟瀹屾垚");
-        }
-
-        /// <summary>
-        /// 鏍规嵁宸烽亾纭畾鐩爣鍦板潃锛堟敮鎸佸鍑哄簱鍙h疆璇級
-        /// </summary>
-        private string DetermineTargetAddress(string roadway, Dictionary<string, List<string>> addressMap)
-        {
-            if (string.IsNullOrWhiteSpace(roadway))
-                return "10080";
-
-            // 鏌ユ壘鍖归厤鐨勫贩閬撳墠缂�
-            string matchedPrefix = null;
-            foreach (var kvp in addressMap)
-            {
-                if (roadway.Contains(kvp.Key))
-                {
-                    matchedPrefix = kvp.Key;
-                    break;
-                }
-            }
-
-            if (matchedPrefix == null)
-                return "10080";
-
-            var addresses = addressMap[matchedPrefix];
-            if (addresses == null || addresses.Count == 0)
-                return "10080";
-
-            // 鍗曚釜鍦板潃锛岀洿鎺ヨ繑鍥�
-            if (addresses.Count == 1)
-                return addresses[0];
-
-            // 澶氫釜鍦板潃锛屼娇鐢ㄨ疆璇㈡湇鍔�
-            return _roundRobinService.GetNextAddress(matchedPrefix, addresses);
         }
 
         /// <summary>
@@ -859,18 +823,19 @@
                     if (stockInfo.LocationId > 0 || !string.IsNullOrWhiteSpace(stockInfo.LocationCode))
                         return WebResponseContent.Instance.Error($"鎵樼洏[{stockPalletCode}]搴撳瓨宸茬粦瀹氳揣浣嶏紝涓嶈兘鍒涘缓鏈烘鎵媨taskName}浠诲姟");
                 }
+                var section = App.Configuration.GetSection("RobotTaskAddressRules").GetSection(targetLineNo).GetChildren().Select(c => c.Value).ToArray();
 
                 var task = new Dt_Task
                 {
                     TaskNum = await BaseDal.GetTaskNo(),
                     PalletCode = palletCode,
                     PalletType = stockInfo?.PalletType ?? 0,
-                    Roadway = stock.Roadway,
+                    Roadway = stock.Roadway ?? string.Empty,
                     TaskType = taskType.GetHashCode(),
                     TaskStatus = TaskRobotStatusEnum.RobotNew.GetHashCode(),
-                    SourceAddress = sourceLineNo,
-                    TargetAddress = targetLineNo,
-                    CurrentAddress = sourceLineNo,
+                    SourceAddress = section[0],
+                    TargetAddress = section[1],
+                    CurrentAddress = targetLineNo,
                     NextAddress = targetLineNo,
                     WarehouseId = stockInfo?.WarehouseId ?? 1,
                     Grade = 1,
@@ -882,7 +847,7 @@
                 if (!result)
                     return WebResponseContent.Instance.Error($"鏈烘鎵媨taskName}浠诲姟鍒涘缓澶辫触");
 
-                var wmstaskDto = _mapper.Map<WMSTaskDTO>(task);
+                var wmstaskDto = _mapper.Map<WMSTaskDTO>(task) ?? new WMSTaskDTO();
                 return WebResponseContent.Instance.OK($"鏈烘鎵媨taskName}浠诲姟鍒涘缓鎴愬姛", wmstaskDto);
             }
             catch (Exception ex)
@@ -893,4 +858,4 @@
 
         #endregion WCS閫昏緫澶勭悊
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3