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/Flows/OutboundTaskFlowService.cs |   54 +++++++++++++++++++++++++++++++++---------------------
 1 files changed, 33 insertions(+), 21 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/OutboundTaskFlowService.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/OutboundTaskFlowService.cs
index b6d645d..e44f6cf 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/OutboundTaskFlowService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_TaskInfoService/Flows/OutboundTaskFlowService.cs
@@ -1,18 +1,18 @@
-锘縰sing Newtonsoft.Json;
+锘縰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;
 
@@ -27,8 +27,8 @@
         private readonly IRouterService _routerService;
         private readonly HttpClientHelper _httpClientHelper;
         private readonly IRobotTaskService _robotTaskService;
-        private readonly ITaskService _taskService;
-        private readonly ITaskRepository _taskRepository;
+        private readonly IComponentContext _componentContext;
+        private readonly IUnitOfWorkManage _unitOfWorkManage;
 
         /// <summary>
         /// 鍒濆鍖栧嚭搴撲换鍔℃祦绋嬫湇鍔°��
@@ -36,16 +36,20 @@
         /// <param name="routerService">璺敱鏈嶅姟銆�</param>
         /// <param name="httpClientHelper">WMS鎺ュ彛璋冪敤甯姪绫汇��</param>
         /// <param name="robotTaskService">鏈烘鎵嬩换鍔℃湇鍔°��</param>
-        /// <param name="taskService">浠诲姟鏈嶅姟锛堢敤浜庢帴鏀跺叆搴撲换鍔★級銆�</param>
-        /// <param name="taskRepository">浠诲姟浠撳偍锛堢敤浜庡垹闄ゅ嚭搴撲换鍔★級銆�</param>
-        public OutboundTaskFlowService(IRouterService routerService, HttpClientHelper httpClientHelper, IRobotTaskService robotTaskService, ITaskService taskService, ITaskRepository taskRepository)
+        /// <param name="componentContext">Autofac缁勪欢涓婁笅鏂囷紙鐢ㄤ簬寤惰繜瑙f瀽ITaskService浠ラ伩鍏嶅惊鐜緷璧栵級銆�</param>
+        public OutboundTaskFlowService(IRouterService routerService, HttpClientHelper httpClientHelper, IRobotTaskService robotTaskService, IComponentContext componentContext, IUnitOfWorkManage unitOfWorkManage)
         {
             _routerService = routerService;
             _httpClientHelper = httpClientHelper;
             _robotTaskService = robotTaskService;
-            _taskService = taskService;
-            _taskRepository = taskRepository;
+            _componentContext = componentContext;
+            _unitOfWorkManage = unitOfWorkManage;
         }
+
+        /// <summary>
+        /// 寤惰繜瑙f瀽ITaskService浠ラ伩鍏嶅惊鐜緷璧�
+        /// </summary>
+        private ITaskService TaskService => _componentContext.Resolve<ITaskService>();
 
         /// <summary>
         /// 鎺ユ敹WMS浠诲姟鏃跺垵濮嬪寲鍑哄簱浠诲姟銆�
@@ -148,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("鍒涘缓鏈湴鍏ュ簱浠诲姟鎴愬姛");
+                    });
                 }
             }
 
@@ -238,4 +250,4 @@
             return WebResponseContent.Instance.OK();
         }
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3