From 64a2aa2301946f777659239247233e47ad1e3076 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期日, 12 四月 2026 11:54:15 +0800
Subject: [PATCH] feat(事务管理): 添加异步事务处理方法

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskService.cs                       |    3 
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/OutboundTaskFlowService.cs     |   40 +++++----
 Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_WCS.cs                      |   72 ++++++++---------
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs  |   17 +--
 Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/IUnitOfWorkManage.cs    |    2 
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/RobotTaskFlowService.cs        |    3 
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseRepository/UnitOfWorks/IUnitOfWorkManage.cs |   20 +++-
 Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs     |   26 ++++++
 8 files changed, 106 insertions(+), 77 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseRepository/UnitOfWorks/IUnitOfWorkManage.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseRepository/UnitOfWorks/IUnitOfWorkManage.cs
index 5d0ade2..b38290a 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseRepository/UnitOfWorks/IUnitOfWorkManage.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseRepository/UnitOfWorks/IUnitOfWorkManage.cs
@@ -1,10 +1,5 @@
 锘縰sing SqlSugar;
-using System;
-using System.Collections.Generic;
-using System.Linq;
 using System.Reflection;
-using System.Text;
-using System.Threading.Tasks;
 
 namespace WIDESEAWCS_Core.BaseRepository
 {
@@ -15,6 +10,7 @@
         /// </summary>
         /// <returns>SqlSugarClient鏁版嵁搴撳鎴风瀵硅薄</returns>
         SqlSugarClient GetDbClient();
+
         int TranCount { get; }
 
         /// <summary>
@@ -27,28 +23,40 @@
         /// 寮�濮嬩竴涓簨鍔�
         /// </summary>
         void BeginTran();
+
         /// <summary>
         /// 寮�濮嬩竴涓簨鍔�
         /// </summary>
         /// <param name="method">瑙﹀彂浜嬪姟鐨勬柟娉曚俊鎭�</param>
         void BeginTran(MethodInfo method);
+
+        /// <summary>
+        /// 寮�濮嬩竴涓簨鍔★紝骞舵墽琛屾彁渚涚殑鍑芥暟
+        /// </summary>
+        /// <param name="func">濮旀墭鏂规硶</param>
+        /// <returns></returns>
+        WebResponseContent BeginTran(Func<WebResponseContent> func);
+
         /// <summary>
         /// 鎻愪氦褰撳墠浜嬪姟
         /// </summary>
         void CommitTran();
+
         /// <summary>
         /// 鎻愪氦浜嬪姟
         /// </summary>
         /// <param name="method">瑙﹀彂鎻愪氦浜嬪姟鐨勬柟娉曚俊鎭�</param>
         void CommitTran(MethodInfo method);
+
         /// <summary>
         /// 鍥炴粴褰撳墠浜嬪姟
         /// </summary>
         void RollbackTran();
+
         /// <summary>
         /// 鍥炴粴褰撳墠浜嬪姟
         /// </summary>
         /// <param name="method">瑙﹀彂鍥炴粴鐨勬柟娉曚俊鎭�</param>
         void RollbackTran(MethodInfo method);
     }
-}
+}
\ No newline at end of file
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs
index 2310264..e6f58d7 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs
@@ -1,13 +1,7 @@
 锘縰sing Microsoft.Extensions.Logging;
 using SqlSugar;
-using System;
 using System.Collections.Concurrent;
-using System.Collections.Generic;
-using System.Linq;
 using System.Reflection;
-using System.Text;
-using System.Threading;
-using System.Threading.Tasks;
 using WIDESEAWCS_Core.Helper;
 
 namespace WIDESEAWCS_Core.BaseRepository
@@ -16,13 +10,16 @@
     {
         // 瀹氫箟鏃ュ織璁板綍鍣�
         private readonly ILogger<UnitOfWorkManage> _logger;
+
         // 瀹氫箟SqlSugarClient
         private readonly ISqlSugarClient _sqlSugarClient;
 
         // 瀹氫箟浜嬪姟璁℃暟鍣�
         private int _tranCount { get; set; }
+
         // 瀹氫箟浜嬪姟璁℃暟鍣ㄧ殑鍙灞炴��
         public int TranCount => _tranCount;
+
         // 瀹氫箟浜嬪姟鏍�
         public readonly ConcurrentStack<string> TranStack = new();
 
@@ -44,7 +41,6 @@
             return _sqlSugarClient as SqlSugarClient;
         }
 
-
         // 鍒涘缓UnitOfWork
         public UnitOfWork CreateUnitOfWork()
         {
@@ -56,7 +52,7 @@
 
             uow.Db.Open();
             uow.Tenant.BeginTran();
-            
+
             _logger.LogDebug("UnitOfWork Begin");
             return uow;
         }
@@ -101,7 +97,7 @@
                     }
                     return content;
                 }
-                catch(Exception ex)
+                catch (Exception ex)
                 {
                     RollbackTran();
                     return WebResponseContent.Instance.Error(ex.Message);
@@ -141,7 +137,6 @@
                 {
                     spinner.SpinOnce();
                 }
-
 
                 if (result == method.GetFullName())
                 {
@@ -209,4 +204,4 @@
             }
         }
     }
-}
+}
\ No newline at end of file
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/OutboundTaskFlowService.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/OutboundTaskFlowService.cs
index 74deb07..e44f6cf 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/OutboundTaskFlowService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/OutboundTaskFlowService.cs
@@ -1,19 +1,18 @@
 锘縰sing Autofac;
 using Newtonsoft.Json;
 using System.Diagnostics.CodeAnalysis;
+using WIDESEA_Core;
 using WIDESEAWCS_Common.HttpEnum;
 using WIDESEAWCS_Common.TaskEnum;
-using WIDESEA_Core;
 using WIDESEAWCS_Core;
-using WIDESEAWCS_Core.Helper;
+using WIDESEAWCS_Core.BaseRepository;
 using WIDESEAWCS_Core.Enums;
+using WIDESEAWCS_Core.Helper;
 using WIDESEAWCS_DTO;
 using WIDESEAWCS_DTO.Stock;
 using WIDESEAWCS_DTO.TaskInfo;
-using WIDESEAWCS_ITaskInfoRepository;
 using WIDESEAWCS_ITaskInfoService;
 using WIDESEAWCS_Model.Models;
-using WIDESEAWCS_QuartzJob.DTO;
 using WIDESEAWCS_QuartzJob.Models;
 using WIDESEAWCS_QuartzJob.Service;
 
@@ -28,8 +27,8 @@
         private readonly IRouterService _routerService;
         private readonly HttpClientHelper _httpClientHelper;
         private readonly IRobotTaskService _robotTaskService;
-        private readonly ITaskRepository _taskRepository;
         private readonly IComponentContext _componentContext;
+        private readonly IUnitOfWorkManage _unitOfWorkManage;
 
         /// <summary>
         /// 鍒濆鍖栧嚭搴撲换鍔℃祦绋嬫湇鍔°��
@@ -37,15 +36,14 @@
         /// <param name="routerService">璺敱鏈嶅姟銆�</param>
         /// <param name="httpClientHelper">WMS鎺ュ彛璋冪敤甯姪绫汇��</param>
         /// <param name="robotTaskService">鏈烘鎵嬩换鍔℃湇鍔°��</param>
-        /// <param name="taskRepository">浠诲姟浠撳偍锛堢敤浜庡垹闄ゅ嚭搴撲换鍔★級銆�</param>
         /// <param name="componentContext">Autofac缁勪欢涓婁笅鏂囷紙鐢ㄤ簬寤惰繜瑙f瀽ITaskService浠ラ伩鍏嶅惊鐜緷璧栵級銆�</param>
-        public OutboundTaskFlowService(IRouterService routerService, HttpClientHelper httpClientHelper, IRobotTaskService robotTaskService, ITaskRepository taskRepository, IComponentContext componentContext)
+        public OutboundTaskFlowService(IRouterService routerService, HttpClientHelper httpClientHelper, IRobotTaskService robotTaskService, IComponentContext componentContext, IUnitOfWorkManage unitOfWorkManage)
         {
             _routerService = routerService;
             _httpClientHelper = httpClientHelper;
             _robotTaskService = robotTaskService;
-            _taskRepository = taskRepository;
             _componentContext = componentContext;
+            _unitOfWorkManage = unitOfWorkManage;
         }
 
         /// <summary>
@@ -154,15 +152,23 @@
 
                 if (inboundTaskDto != null)
                 {
-                    // 鍏堝垹闄ゆ湰鍦板嚭搴撲换鍔★紝閬垮厤鎵樼洏鍙峰敮涓�閿啿绐�
-                    _taskRepository.DeleteAndMoveIntoHty(task, OperateTypeEnum.鑷姩瀹屾垚);
-
-                    // 璋冪敤ReceiveWMSTask鍒涘缓鏈湴鍏ュ簱浠诲姟
-                    var receiveResult = TaskService.ReceiveWMSTask(new List<WMSTaskDTO> { inboundTaskDto });
-                    if (!receiveResult.Status)
+                    _unitOfWorkManage.BeginTran(() =>
                     {
-                        return content.Error($"鍒涘缓鏈湴鍏ュ簱浠诲姟澶辫触: {receiveResult.Message}");
-                    }
+                        // 鍏堝垹闄ゆ湰鍦板嚭搴撲换鍔★紝閬垮厤鎵樼洏鍙峰敮涓�閿啿绐�
+                        bool isDeleted = TaskService.Repository.DeleteAndMoveIntoHty(task, OperateTypeEnum.鑷姩瀹屾垚);
+                        if (!isDeleted)
+                        {
+                            return content.Error($"鍒犻櫎鏈湴鍑哄簱浠诲姟澶辫触,浠诲姟鍙�:銆恵task.TaskNum}銆�,鎵樼洏鍙�:銆恵task.PalletCode}銆�");
+                        }
+
+                        // 璋冪敤ReceiveWMSTask鍒涘缓鏈湴鍏ュ簱浠诲姟
+                        var receiveResult = TaskService.ReceiveWMSTask(new List<WMSTaskDTO> { inboundTaskDto });
+                        if (!receiveResult.Status)
+                        {
+                            return content.Error($"鍒涘缓鏈湴鍏ュ簱浠诲姟澶辫触: {receiveResult.Message}");
+                        }
+                        return content.OK("鍒涘缓鏈湴鍏ュ簱浠诲姟鎴愬姛");
+                    });
                 }
             }
 
@@ -244,4 +250,4 @@
             return WebResponseContent.Instance.OK();
         }
     }
-}
+}
\ No newline at end of file
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/RobotTaskFlowService.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/RobotTaskFlowService.cs
index a87a9e5..271cd9b 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/RobotTaskFlowService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/RobotTaskFlowService.cs
@@ -1,7 +1,6 @@
 锘縰sing System.Diagnostics.CodeAnalysis;
 using WIDESEAWCS_Common.TaskEnum;
 using WIDESEAWCS_Core;
-using WIDESEAWCS_Core.Helper;
 using WIDESEAWCS_DTO.TaskInfo;
 using WIDESEAWCS_ITaskInfoService;
 using WIDESEAWCS_Model.Models;
@@ -53,4 +52,4 @@
             return WebResponseContent.Instance.OK();
         }
     }
-}
+}
\ No newline at end of file
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskService.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskService.cs
index ac3cb72..c6c5522 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/TaskService.cs
@@ -676,8 +676,7 @@
             return BaseDal.QueryFirst(x =>
                 x.TaskType == (int)TaskInboundTypeEnum.Inbound &&
                 x.TaskStatus == (int)TaskInStatusEnum.InNew &&
-                x.SourceAddress == sourceAddress &&
-                x.Creater == "WMS");
+                x.SourceAddress == sourceAddress);
         }
     }
 
diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/IUnitOfWorkManage.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/IUnitOfWorkManage.cs
index 7d715d8..57ea228 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/IUnitOfWorkManage.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/IUnitOfWorkManage.cs
@@ -17,6 +17,8 @@
 
         void BeginTran();
         void BeginTran(MethodInfo method);
+        WebResponseContent BeginTran(Func<WebResponseContent> func);
+        Task<WebResponseContent> BeginTranAsync(Func<Task<WebResponseContent>> funcAsync);
         void CommitTran();
         void CommitTran(MethodInfo method);
         void RollbackTran();
diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs
index e3b9076..cea1c65 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseRepository/UnitOfWorks/UnitOfWorkManage.cs
@@ -100,6 +100,32 @@
             }
         }
 
+        private readonly SemaphoreSlim _asyncLock = new SemaphoreSlim(1, 1);
+
+        public async Task<WebResponseContent> BeginTranAsync(Func<Task<WebResponseContent>> funcAsync)
+        {
+            await _asyncLock.WaitAsync();
+            try
+            {
+                BeginTran();   // 鍋囪杩欐槸鍚屾鏂规硶锛屽惎鍔ㄤ簨鍔�
+                WebResponseContent content = await funcAsync();
+                if (content.Status)
+                    CommitTran();   // 鍚屾鎻愪氦
+                else
+                    RollbackTran(); // 鍚屾鍥炴粴
+                return content;
+            }
+            catch (Exception ex)
+            {
+                RollbackTran();
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+            finally
+            {
+                _asyncLock.Release();
+            }
+        }
+
         public void CommitTran()
         {
             lock (this)
diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_WCS.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_WCS.cs
index 9c14375..ed7108a 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_WCS.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_WCS.cs
@@ -1,27 +1,15 @@
 using Mapster;
-using MapsterMapper;
 using Microsoft.Extensions.Configuration;
 using SqlSugar;
-using System.DirectoryServices.Protocols;
-using System.Text.Json;
 using WIDESEA_Common.Constants;
 using WIDESEA_Common.LocationEnum;
 using WIDESEA_Common.StockEnum;
 using WIDESEA_Common.TaskEnum;
 using WIDESEA_Common.WareHouseEnum;
 using WIDESEA_Core;
-using WIDESEA_Core.BaseRepository;
-using WIDESEA_Core.BaseServices;
-using WIDESEA_Core.Core;
-using WIDESEA_Core.Enums;
 using WIDESEA_Core.Helper;
-using WIDESEA_DTO.GradingMachine;
-using WIDESEA_DTO.MES;
 using WIDESEA_DTO.Stock;
 using WIDESEA_DTO.Task;
-using WIDESEA_IBasicService;
-using WIDESEA_IStockService;
-using WIDESEA_ITaskInfoService;
 using WIDESEA_Model.Models;
 
 namespace WIDESEA_TaskInfoService
@@ -185,7 +173,6 @@
                 // 鍒ゆ柇鏄笉鏄瀬鍗峰簱浠诲姟
                 if (taskDto.WarehouseId == (int)WarehouseEnum.FJ1 || taskDto.WarehouseId == (int)WarehouseEnum.ZJ1)
                 {
-
                     return await CompleteAgvInboundTaskAsync(taskDto);
                 }
 
@@ -885,31 +872,31 @@
         {
             try
             {
-                // 1. 鏍规嵁浠诲姟绫诲瀷瀛楃涓茬‘瀹� TaskType 鍜� TaskStatus
                 int taskType;
                 int taskStatus;
                 switch (dto.TaskType)
                 {
                     case "鍏ュ簱":
-                        taskType = TaskTypeEnum.Inbound.GetHashCode();
+                        taskType = TaskInboundTypeEnum.Inbound.GetHashCode();
                         taskStatus = TaskInStatusEnum.InNew.GetHashCode();
                         break;
+
                     case "鍑哄簱":
-                        taskType = TaskTypeEnum.Outbound.GetHashCode();
+                        taskType = TaskOutboundTypeEnum.Outbound.GetHashCode();
                         taskStatus = TaskOutStatusEnum.OutNew.GetHashCode();
                         break;
+
                     case "绉诲簱":
-                        taskType = TaskTypeEnum.Relocation.GetHashCode();
+                        taskType = TaskRelocationTypeEnum.Relocation.GetHashCode();
                         taskStatus = TaskRelocationStatusEnum.RelocationNew.GetHashCode();
                         break;
+
                     default:
                         return WebResponseContent.Instance.Error($"涓嶆敮鎸佺殑浠诲姟绫诲瀷: {dto.TaskType}");
                 }
 
-                // 2. 鐢熸垚浠诲姟鍙�
                 int taskNum = await BaseDal.GetTaskNo();
 
-                // 3. 鏋勫缓浠诲姟瀹炰綋
                 var task = new Dt_Task
                 {
                     TaskNum = taskNum,
@@ -919,6 +906,7 @@
                     TaskType = taskType,
                     TaskStatus = taskStatus,
                     Grade = dto.Grade,
+                    Roadway = dto.TargetAddress,
                     WarehouseId = dto.WarehouseId,
                     CurrentAddress = dto.SourceAddress,
                     NextAddress = dto.TargetAddress,
@@ -927,31 +915,37 @@
                     ModifyDate = DateTime.Now
                 };
 
-                // 4. 淇濆瓨鍒版暟鎹簱
-                var result = await BaseDal.AddDataAsync(task) > 0;
-                if (!result)
-                    return WebResponseContent.Instance.Error("鍒涘缓浠诲姟澶辫触");
-
-                // 5. 鍙戦�佸埌 WCS
-                var wmsTaskDto = new WMSTaskDTO
+                var wmsTaskDtos = new List<WMSTaskDTO>()
                 {
-                    TaskNum = task.TaskNum,
-                    PalletCode = task.PalletCode,
-                    SourceAddress = task.SourceAddress,
-                    TargetAddress = task.TargetAddress,
-                    TaskType = task.TaskType,
-                    TaskStatus = task.TaskStatus,
-                    WarehouseId = task.WarehouseId
+                    new()
+                    {
+                        TaskNum = task.TaskNum,
+                        PalletCode = task.PalletCode,
+                        SourceAddress = task.SourceAddress,
+                        TargetAddress = task.TargetAddress,
+                        TaskType = task.TaskType,
+                        Roadway = task.Roadway,
+                        TaskStatus = task.TaskStatus,
+                        WarehouseId = task.WarehouseId
+                    }
                 };
 
-                var wcsResult = _httpClientHelper.Post<WebResponseContent>(
-                    "http://localhost:9292/api/Task/ReceiveManualTask",
-                    wmsTaskDto.ToJson());
+                await _unitOfWorkManage.BeginTranAsync(async () =>
+                {
+                    // 4. 淇濆瓨鍒版暟鎹簱
+                    var result = await BaseDal.AddDataAsync(task) > 0;
+                    if (!result)
+                        return WebResponseContent.Instance.Error("鍒涘缓浠诲姟澶辫触");
 
-                if (!wcsResult.IsSuccess || !wcsResult.Data.Status)
-                    return WebResponseContent.Instance.Error($"浠诲姟宸插垱寤轰絾鍙戦�佺粰WCS澶辫触: {wcsResult.Data?.Message}");
+                    var wcsResult = _httpClientHelper.Post<WebResponseContent>(
+                        "http://localhost:9292/api/Task/ReceiveManualTask",
+                        wmsTaskDtos.ToJson());
 
-                return WebResponseContent.Instance.OK($"鎵嬪姩鍒涘缓浠诲姟鎴愬姛锛屼换鍔″彿: {taskNum}");
+                    if (!wcsResult.IsSuccess || !wcsResult.Data.Status)
+                        return WebResponseContent.Instance.Error($"浠诲姟宸插垱寤轰絾鍙戦�佺粰WCS澶辫触: {wcsResult.Data?.Message}");
+
+                    return WebResponseContent.Instance.OK($"鎵嬪姩鍒涘缓浠诲姟鎴愬姛锛屼换鍔″彿: {taskNum}");
+                });
             }
             catch (Exception ex)
             {

--
Gitblit v1.9.3