From dcbd4934d063f471c01cbcf93574c2e2ac5f16b5 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期五, 27 三月 2026 09:37:36 +0800
Subject: [PATCH] feat: 提交WCS与WMS代码调整

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs |  239 +++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 132 insertions(+), 107 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
index 550c291..81bbbf0 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
@@ -67,7 +67,7 @@
         {
             try
             {
-                WebResponseContent content = await GetTasksByPalletCodeAsync(taskDto.PalletCode);
+                WebResponseContent content = await GetTaskByPalletCodeAsync(taskDto.PalletCode);
                 if (content.Status)
                 {
                     return content;
@@ -172,18 +172,23 @@
                 var locationInfo = await _locationInfoService.GetLocationInfo(task.Roadway);
                 if (locationInfo == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴旂殑璐т綅");
 
-                locationInfo.LocationStatus = LocationStatusEnum.FreeLock.GetHashCode();
-                task.CurrentAddress = task.SourceAddress;
-                task.NextAddress = locationInfo.LocationCode;
-                task.TargetAddress = locationInfo.LocationCode;
-                task.TaskStatus = TaskInStatusEnum.Line_InFinish.GetHashCode();
+                return await ExecuteWithinTransactionAsync(async () =>
+                {
+                    locationInfo.LocationStatus = LocationStatusEnum.FreeLock.GetHashCode();
+                    task.CurrentAddress = task.SourceAddress;
+                    task.NextAddress = locationInfo.LocationCode;
+                    task.TargetAddress = locationInfo.LocationCode;
+                    task.TaskStatus = TaskInStatusEnum.Line_InFinish.GetHashCode();
 
-                var updateResult = await BaseDal.UpdateDataAsync(task);
-                var locationResult = await _locationInfoService.UpdateLocationInfoAsync(locationInfo);
+                    var updateTaskResult = await BaseDal.UpdateDataAsync(task);
+                    var updateLocationResult = await _locationInfoService.UpdateLocationInfoAsync(locationInfo);
+                    if (!updateTaskResult || !updateLocationResult)
+                    {
+                        return WebResponseContent.Instance.Error("浠诲姟鏇存柊澶辫触");
+                    }
 
-                return WebResponseContent.Instance.OK(
-                    updateResult && locationResult ? "浠诲姟鏇存柊鎴愬姛" : "浠诲姟鏇存柊澶辫触",
-                    locationInfo.LocationCode);
+                    return WebResponseContent.Instance.OK("浠诲姟鏇存柊鎴愬姛", locationInfo.LocationCode);
+                });
             }
             catch (Exception ex)
             {
@@ -205,23 +210,28 @@
                 if (location == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴旂殑璐т綅");
 
                 var stockInfo = await _stockInfoService.GetStockInfoAsync(taskDto.PalletCode);
-                stockInfo.LocationCode = location.LocationCode;
-                stockInfo.LocationId = location.Id;
-                stockInfo.OutboundDate = task.Roadway switch
+                if (stockInfo == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴斿簱瀛樹俊鎭�");
+
+                return await ExecuteWithinTransactionAsync(async () =>
                 {
-                    var r when r.Contains("GW") => DateTime.Now.AddHours(2),
-                    var r when r.Contains("CW") => DateTime.Now.AddHours(1),
-                    _ => DateTime.Now
-                };
-                stockInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.GetHashCode();
+                    stockInfo.LocationCode = location.LocationCode;
+                    stockInfo.LocationId = location.Id;
+                    stockInfo.OutboundDate = task.Roadway switch
+                    {
+                        var r when r.Contains("GW") => DateTime.Now.AddHours(2),
+                        var r when r.Contains("CW") => DateTime.Now.AddHours(1),
+                        _ => DateTime.Now
+                    };
+                    stockInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.GetHashCode();
 
-                location.LocationStatus = LocationStatusEnum.InStock.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("浠诲姟瀹屾垚澶辫触");
-                return await CompleteTaskAsync(task);
+                    var updateLocationResult = await _locationInfoService.UpdateLocationInfoAsync(location);
+                    var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
+                    if (!updateLocationResult || !updateStockResult)
+                        return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
+                    return await CompleteTaskAsync(task);
+                });
             }
             catch (Exception ex)
             {
@@ -242,18 +252,23 @@
                 var location = await _locationInfoService.GetLocationInfo(task.Roadway, task.SourceAddress);
                 if (location == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴旂殑璐т綅");
 
-                var stockInfo = await _stockInfoService.GetStockInfoAsync(taskDto.PalletCode); 
-                stockInfo.LocationId = 0;
-                stockInfo.LocationCode = null;
-                stockInfo.OutboundDate = DateTime.Now;
+                var stockInfo = await _stockInfoService.GetStockInfoAsync(taskDto.PalletCode);
+                if (stockInfo == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴斿簱瀛樹俊鎭�");
 
-                location.LocationStatus = LocationStatusEnum.Free.GetHashCode();
+                return await ExecuteWithinTransactionAsync(async () =>
+                {
+                    stockInfo.LocationId = 0;
+                    stockInfo.LocationCode = null;
+                    stockInfo.OutboundDate = DateTime.Now;
 
-                var updateLocationResult = await _locationInfoService.UpdateLocationInfoAsync(location);
-                var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
-                if (!updateLocationResult || !updateStockResult)
-                    return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
-                return await CompleteTaskAsync(task);
+                    location.LocationStatus = LocationStatusEnum.Free.GetHashCode();
+
+                    var updateLocationResult = await _locationInfoService.UpdateLocationInfoAsync(location);
+                    var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
+                    if (!updateLocationResult || !updateStockResult)
+                        return WebResponseContent.Instance.Error("浠诲姟瀹屾垚澶辫触");
+                    return await CompleteTaskAsync(task);
+                });
             }
             catch (Exception ex)
             {
@@ -282,21 +297,24 @@
                 var stockInfo = await _stockInfoService.GetStockInfoAsync(taskDto.PalletCode);
                 if (stockInfo == null) return WebResponseContent.Instance.Error("鏈壘鍒板搴斿簱瀛樹俊鎭�");
 
-                stockInfo.LocationCode = targetLocation.LocationCode;
-                stockInfo.LocationId = targetLocation.Id;
-                stockInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.GetHashCode();
+                return await ExecuteWithinTransactionAsync(async () =>
+                {
+                    stockInfo.LocationCode = targetLocation.LocationCode;
+                    stockInfo.LocationId = targetLocation.Id;
+                    stockInfo.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.GetHashCode();
 
-                sourceLocation.LocationStatus = LocationStatusEnum.Free.GetHashCode();
-                targetLocation.LocationStatus = LocationStatusEnum.InStock.GetHashCode();
+                    sourceLocation.LocationStatus = LocationStatusEnum.Free.GetHashCode();
+                    targetLocation.LocationStatus = LocationStatusEnum.InStock.GetHashCode();
 
-                var updateSourceResult = await _locationInfoService.UpdateLocationInfoAsync(sourceLocation);
-                var updateTargetResult = await _locationInfoService.UpdateLocationInfoAsync(targetLocation);
-                var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
+                    var updateSourceResult = await _locationInfoService.UpdateLocationInfoAsync(sourceLocation);
+                    var updateTargetResult = await _locationInfoService.UpdateLocationInfoAsync(targetLocation);
+                    var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
 
-                if (!updateSourceResult || !updateTargetResult || !updateStockResult)
-                    return WebResponseContent.Instance.Error("绉诲簱浠诲姟瀹屾垚澶辫触");
+                    if (!updateSourceResult || !updateTargetResult || !updateStockResult)
+                        return WebResponseContent.Instance.Error("绉诲簱浠诲姟瀹屾垚澶辫触");
 
-                return await CompleteTaskAsync(task);
+                    return await CompleteTaskAsync(task);
+                });
             }
             catch (Exception ex)
             {
@@ -313,7 +331,7 @@
         {
             try
             {
-                WebResponseContent content = await GetTasksByPalletCodeAsync(taskDto.PalletCode);
+                WebResponseContent content = await GetTaskByPalletCodeAsync(taskDto.PalletCode);
                 if (content.Status)
                 {
                     return content;
@@ -363,8 +381,10 @@
                 };
                 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)
             {
@@ -403,15 +423,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)
             {
@@ -549,45 +569,54 @@
                     taskList.Add(task);
                 }
 
-                var addResult = await BaseDal.AddDataAsync(taskList) > 0;
-                if (!addResult)
+                var transactionResult = await ExecuteWithinTransactionAsync(async () =>
                 {
-                    return WebResponseContent.Instance.Error($"鎵归噺鍒涘缓浠诲姟澶辫触锛屽叡 {taskList.Count} 涓换鍔�");
-                }
-
-                // 浠诲姟鍒涘缓鎴愬姛鍚庯紝鍚屾閿佸畾搴撳瓨鍜岃揣浣嶇姸鎬侊紝閬垮厤閲嶅鍒嗛厤
-                var stocksToUpdate = stocksToProcess
-                    .Select(s =>
+                    var addResult = await BaseDal.AddDataAsync(taskList) > 0;
+                    if (!addResult)
                     {
-                        s.StockStatus = StockStatusEmun.鍑哄簱閿佸畾.GetHashCode();
-                        return s;
-                    })
-                    .ToList();
-
-                var updateStockResult = await _stockInfoService.Repository.UpdateDataAsync(stocksToUpdate);
-                if (!updateStockResult)
-                {
-                    return WebResponseContent.Instance.Error($"浠诲姟鍒涘缓鎴愬姛锛屼絾搴撳瓨鐘舵�佹洿鏂板け璐ワ紝鍏� {stocksToUpdate.Count} 鏉�");
-                }
-
-                var locationsToUpdate = stocksToProcess
-                    .Where(s => s.LocationDetails != null)
-                    .GroupBy(s => s.LocationDetails.Id)
-                    .Select(g =>
-                    {
-                        var location = g.First().LocationDetails;
-                        location.LocationStatus = LocationStatusEnum.InStockLock.GetHashCode();
-                        return location;
-                    })
-                    .ToList();
-
-                if (locationsToUpdate.Any())
-                {
-                    var updateLocationResult = await _locationInfoService.Repository.UpdateDataAsync(locationsToUpdate);
-                    if (!updateLocationResult)
-                    {
-                        return WebResponseContent.Instance.Error($"浠诲姟鍒涘缓鎴愬姛锛屼絾璐т綅鐘舵�佹洿鏂板け璐ワ紝鍏� {locationsToUpdate.Count} 鏉�");
+                        return WebResponseContent.Instance.Error($"鎵归噺鍒涘缓浠诲姟澶辫触锛屽叡 {taskList.Count} 涓换鍔�");
                     }
+
+                    // 浠诲姟鍒涘缓鎴愬姛鍚庯紝鍚屾閿佸畾搴撳瓨鍜岃揣浣嶇姸鎬侊紝閬垮厤閲嶅鍒嗛厤
+                    var stocksToUpdate = stocksToProcess
+                        .Select(s =>
+                        {
+                            s.StockStatus = StockStatusEmun.鍑哄簱閿佸畾.GetHashCode();
+                            return s;
+                        })
+                        .ToList();
+
+                    var updateStockResult = await _stockInfoService.Repository.UpdateDataAsync(stocksToUpdate);
+                    if (!updateStockResult)
+                    {
+                        return WebResponseContent.Instance.Error($"浠诲姟鍒涘缓鎴愬姛锛屼絾搴撳瓨鐘舵�佹洿鏂板け璐ワ紝鍏� {stocksToUpdate.Count} 鏉�");
+                    }
+
+                    var locationsToUpdate = stocksToProcess
+                        .Where(s => s.LocationDetails != null)
+                        .GroupBy(s => s.LocationDetails.Id)
+                        .Select(g =>
+                        {
+                            var location = g.First().LocationDetails;
+                            location.LocationStatus = LocationStatusEnum.InStockLock.GetHashCode();
+                            return location;
+                        })
+                        .ToList();
+
+                    if (locationsToUpdate.Any())
+                    {
+                        var updateLocationResult = await _locationInfoService.Repository.UpdateDataAsync(locationsToUpdate);
+                        if (!updateLocationResult)
+                        {
+                            return WebResponseContent.Instance.Error($"浠诲姟鍒涘缓鎴愬姛锛屼絾璐т綅鐘舵�佹洿鏂板け璐ワ紝鍏� {locationsToUpdate.Count} 鏉�");
+                        }
+                    }
+
+                    return WebResponseContent.Instance.OK();
+                });
+                if (!transactionResult.Status)
+                {
+                    return transactionResult;
                 }
 
                 // 6. 閫氱煡 WCS锛堝紓姝ワ紝涓嶅奖鍝嶄富娴佺▼锛�
@@ -752,30 +781,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