From 715cf4c8b9e097aae6c4bcaf00bdd67a763529c4 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期一, 30 三月 2026 13:58:09 +0800
Subject: [PATCH] feat(stockChat): 集成 SignalR 实现实时库存更新

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs |  181 ++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 152 insertions(+), 29 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
index 20dc592..ebf7fb8 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
@@ -12,6 +12,7 @@
 using WIDESEA_Core.Core;
 using WIDESEA_Core.Helper;
 using WIDESEA_DTO.GradingMachine;
+using WIDESEA_DTO.MES;
 using WIDESEA_DTO.Stock;
 using WIDESEA_DTO.Task;
 using WIDESEA_IBasicService;
@@ -29,6 +30,9 @@
         private readonly HttpClientHelper _httpClientHelper;
         private readonly IConfiguration _configuration;
         private readonly RoundRobinService _roundRobinService;
+        private readonly IMesService _mesService;
+        private readonly ITask_HtyService _task_HtyService;
+        private readonly IStockInfo_HtyService _stockInfo_HtyService;
 
         public IRepository<Dt_Task> Repository => BaseDal;
 
@@ -48,7 +52,10 @@
             ILocationInfoService locationInfoService,
             HttpClientHelper httpClientHelper,
             IConfiguration configuration,
-            RoundRobinService roundRobinService) : base(BaseDal)
+            RoundRobinService roundRobinService,
+            IMesService mesService,
+            ITask_HtyService task_HtyService,
+            IStockInfo_HtyService stockInfo_HtyService) : base(BaseDal)
         {
             _mapper = mapper;
             _stockInfoService = stockInfoService;
@@ -56,6 +63,9 @@
             _httpClientHelper = httpClientHelper;
             _configuration = configuration;
             _roundRobinService = roundRobinService;
+            _mesService = mesService;
+            _task_HtyService = task_HtyService;
+            _stockInfo_HtyService = stockInfo_HtyService;
         }
 
         #region WCS閫昏緫澶勭悊
@@ -67,7 +77,7 @@
         {
             try
             {
-                WebResponseContent content = await GetTasksByPalletCodeAsync(taskDto.PalletCode);
+                WebResponseContent content = await GetTaskByPalletCodeAsync(taskDto.PalletCode);
                 if (content.Status)
                 {
                     return content;
@@ -214,6 +224,7 @@
 
                 return await ExecuteWithinTransactionAsync(async () =>
                 {
+                    WebResponseContent content = new WebResponseContent();
                     stockInfo.LocationCode = location.LocationCode;
                     stockInfo.LocationId = location.Id;
                     stockInfo.OutboundDate = task.Roadway switch
@@ -230,6 +241,19 @@
                     var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
                     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 ?? "鏈煡閿欒"}");
+                    }
                     return await CompleteTaskAsync(task);
                 });
             }
@@ -255,6 +279,7 @@
                 var stockInfo = await _stockInfoService.GetStockInfoAsync(taskDto.PalletCode);
                 if (stockInfo == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴斿簱瀛樹俊鎭�");
 
+                WebResponseContent content = new WebResponseContent();
                 return await ExecuteWithinTransactionAsync(async () =>
                 {
                     stockInfo.LocationId = 0;
@@ -267,6 +292,22 @@
                     var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
                     if (!updateLocationResult || !updateStockResult)
                         return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
+
+                    // 璋冪敤MES鎵樼洏鍑虹珯
+                    var outboundRequest = new OutboundInContainerRequest
+                    {
+                        EquipmentCode = "STK-GROUP-001",
+                        ResourceCode = "STK-GROUP-001",
+                        LocalTime = DateTime.Now,
+                        ContainerCode = taskDto.PalletCode,
+                        ParamList = new List<ParamItem>()
+                    };
+                    var outboundResult = _mesService.OutboundInContainer(outboundRequest);
+                    if (outboundResult == null || outboundResult.Data == null || !outboundResult.Data.IsSuccess)
+                    {
+                        return content.Error($"浠诲姟瀹屾垚澶辫触锛歁ES鍑虹珯澶辫触: {outboundResult?.Data?.Msg ?? outboundResult?.ErrorMessage ?? "鏈煡閿欒"}");
+                    }
+
                     return await CompleteTaskAsync(task);
                 });
             }
@@ -331,7 +372,7 @@
         {
             try
             {
-                WebResponseContent content = await GetTasksByPalletCodeAsync(taskDto.PalletCode);
+                WebResponseContent content = await GetTaskByPalletCodeAsync(taskDto.PalletCode);
                 if (content.Status)
                 {
                     return content;
@@ -346,6 +387,47 @@
             catch (Exception ex)
             {
                 return WebResponseContent.Instance.Error($"鏌ヨ浠诲姟澶辫触: {ex.Message}");
+            }
+        }
+
+        /// <summary>
+        /// 绌烘墭鐩樺叆搴撳畬鎴�
+        /// </summary>
+        public async Task<WebResponseContent> InboundFinishTaskTrayAsync(CreateTaskDto taskDto)
+        {
+            try
+            {
+                var task = await BaseDal.QueryFirstAsync(s => s.PalletCode == taskDto.PalletCode);
+                if (task == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴旂殑浠诲姟");
+
+                var location = await _locationInfoService.GetLocationInfo(task.Roadway, task.TargetAddress);
+                if (location == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴旂殑璐т綅");
+
+                var stockInfo = await _stockInfoService.GetStockInfoAsync(taskDto.PalletCode);
+                if (stockInfo == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴斿簱瀛樹俊鎭�");
+
+                return await ExecuteWithinTransactionAsync(async () =>
+                {
+                    stockInfo.LocationCode = location.LocationCode;
+                    stockInfo.LocationId = location.Id;
+                    stockInfo.StockStatus = StockStatusEmun.绌烘墭鐩樺簱瀛�.GetHashCode();
+
+                    location.LocationStatus = LocationStatusEnum.InStock.GetHashCode();
+
+                    var updateLocationResult = await _locationInfoService.UpdateLocationInfoAsync(location);
+                    var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
+                    if (!updateLocationResult || !updateStockResult)
+                        return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
+
+                    var deleteResult = await BaseDal.DeleteDataAsync(task);
+                    if (!deleteResult) return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
+
+                    return WebResponseContent.Instance.OK("浠诲姟瀹屾垚");
+                });
+            }
+            catch (Exception ex)
+            {
+                return WebResponseContent.Instance.Error($"瀹屾垚浠诲姟澶辫触: {ex.Message}");
             }
         }
 
@@ -381,12 +463,55 @@
                 };
                 var taskDtos = task.Adapt<WMSTaskDTO>();
 
-                BaseDal.AddData(task);
-                return WebResponseContent.Instance.OK("鏌ヨ鎴愬姛", taskDtos);
+                var addResult = await BaseDal.AddDataAsync(task) > 0;
+                if (!addResult)
+                    return WebResponseContent.Instance.Error("浠诲姟鍒涘缓澶辫触");
+                return WebResponseContent.Instance.OK("浠诲姟鍒涘缓鎴愬姛", taskDtos);
             }
             catch (Exception ex)
             {
                 return WebResponseContent.Instance.Error($"鏌ヨ浠诲姟澶辫触: {ex.Message}");
+            }
+        }
+
+        /// <summary>
+        /// 绌烘墭鐩樺嚭搴撳畬鎴�
+        /// </summary>
+        public async Task<WebResponseContent> OutboundFinishTaskTrayAsync(CreateTaskDto taskDto)
+        {
+            try
+            {
+                var task = await BaseDal.QueryFirstAsync(s => s.PalletCode == taskDto.PalletCode);
+                if (task == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴旂殑浠诲姟");
+
+                var location = await _locationInfoService.GetLocationInfo(task.Roadway, task.SourceAddress);
+                if (location == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴旂殑璐т綅");
+
+                var stockInfo = await _stockInfoService.GetStockInfoAsync(taskDto.PalletCode);
+                if (stockInfo == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴斿簱瀛樹俊鎭�");
+
+                return await ExecuteWithinTransactionAsync(async () =>
+                {
+                    stockInfo.LocationId = 0;
+                    stockInfo.LocationCode = null;
+                    stockInfo.StockStatus = StockStatusEmun.鍑哄簱瀹屾垚.GetHashCode();
+
+                    location.LocationStatus = LocationStatusEnum.Free.GetHashCode();
+
+                    var updateLocationResult = await _locationInfoService.UpdateLocationInfoAsync(location);
+                    var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
+                    if (!updateLocationResult || !updateStockResult)
+                        return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
+
+                    var deleteResult = await BaseDal.DeleteDataAsync(task);
+                    if (!deleteResult) return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
+
+                    return WebResponseContent.Instance.OK("浠诲姟瀹屾垚");
+                });
+            }
+            catch (Exception ex)
+            {
+                return WebResponseContent.Instance.Error($"瀹屾垚浠诲姟澶辫触: {ex.Message}");
             }
         }
 
@@ -421,15 +546,15 @@
         /// </summary>
         /// <param name="palletCode"></param>
         /// <returns></returns>
-        private async Task<WebResponseContent> GetTasksByPalletCodeAsync(string palletCode)
+        private async Task<WebResponseContent> GetTaskByPalletCodeAsync(string palletCode)
         {
             try
             {
-                var tasks = await BaseDal.QueryFirstAsync(s => s.PalletCode == palletCode);
-                if (tasks == null)
+                var task = await BaseDal.QueryFirstAsync(s => s.PalletCode == palletCode);
+                if (task == null)
                     return WebResponseContent.Instance.Error("鏈壘鍒板搴旂殑浠诲姟");
-                var taskDtos = _mapper.Map<List<WMSTaskDTO>>(tasks);
-                return WebResponseContent.Instance.OK("鏌ヨ鎴愬姛", taskDtos);
+                var taskDto = _mapper.Map<WMSTaskDTO>(task);
+                return WebResponseContent.Instance.OK("鏌ヨ鎴愬姛", taskDto);
             }
             catch (Exception ex)
             {
@@ -440,14 +565,16 @@
         /// <summary>
         /// 瀹屾垚浠诲姟鍚庣粺涓�澶勭悊锛堝垹闄や换鍔℃暟鎹級
         /// </summary>
-        private async Task<WebResponseContent> CompleteTaskAsync(Dt_Task task)
+        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("浠诲姟瀹屾垚");
         }
@@ -779,30 +906,26 @@
             try
             {
                 var stockInfo = await _stockInfoService.GetStockInfoAsync(input.PalletCode, input.LocationCode);
+
+                int locationStatus;
                 if (stockInfo == null)
                 {
                     var location = await _locationInfoService.GetLocationInfoAsync(input.LocationCode);
-
-                    OutputDto outPutDto = new OutputDto()
-                    {
-                        LocationCode = input.LocationCode,
-                        PalletCode = input.PalletCode,
-                        IsNormalProcedure = 1,
-                        LocationStatus = location.LocationStatus
-                    };
-                    content.OK(data: outPutDto);
+                    locationStatus = location?.LocationStatus ?? 0;
                 }
                 else
                 {
-                    OutputDto outPutDto = new OutputDto()
-                    {
-                        LocationCode = input.LocationCode,
-                        PalletCode = input.PalletCode,
-                        IsNormalProcedure = 1,
-                        LocationStatus = stockInfo.LocationDetails.LocationStatus
-                    };
-                    content.OK(data: outPutDto);
+                    locationStatus = stockInfo.LocationDetails?.LocationStatus ?? 0;
                 }
+
+                OutputDto outPutDto = new OutputDto()
+                {
+                    LocationCode = input.LocationCode,
+                    PalletCode = input.PalletCode,
+                    IsNormalProcedure = 1,
+                    LocationStatus = locationStatus
+                };
+                return content.OK(data: outPutDto);
             }
             catch (Exception ex)
             {

--
Gitblit v1.9.3