From 96adc295cb04fd135d63d3a907f2732274f90965 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 21 四月 2026 01:11:21 +0800
Subject: [PATCH] feat: 添加MES异步上传辅助服务并重构相关代码

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs |   83 ++---------------------------------------
 1 files changed, 4 insertions(+), 79 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
index d232686..241396b 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
@@ -17,10 +17,7 @@
 using WIDESEA_Core.Helper;
 using WIDESEA_DTO.GradingMachine;
 using WIDESEA_DTO.MES;
-using WIDESEA_DTO.Stock;
 using WIDESEA_DTO.Task;
-using Newtonsoft.Json;
-using System.Diagnostics;
 using WIDESEA_IBasicService;
 using WIDESEA_IRecordService;
 using WIDESEA_IStockService;
@@ -44,6 +41,7 @@
         private readonly IRecordService _recordService;
         private readonly IMESDeviceConfigService _mesDeviceConfigService;
         private readonly IMesLogService _mesLogService;
+        private readonly IMesUploadHelper _mesUploadHelper;
 
         public IRepository<Dt_Task> Repository => BaseDal;
 
@@ -70,7 +68,8 @@
             IUnitOfWorkManage unitOfWorkManage,
             IRecordService recordService,
             IMESDeviceConfigService mesDeviceConfigService,
-            IMesLogService mesLogService) : base(BaseDal)
+            IMesLogService mesLogService,
+            IMesUploadHelper mesUploadHelper) : base(BaseDal)
         {
             _mapper = mapper;
             _stockInfoService = stockInfoService;
@@ -85,6 +84,7 @@
             _recordService = recordService;
             _mesDeviceConfigService = mesDeviceConfigService;
             _mesLogService = mesLogService;
+            _mesUploadHelper = mesUploadHelper;
         }
 
         /// <summary>
@@ -200,80 +200,5 @@
             return DetermineTargetAddress(roadway, addressMap);
         }
 
-        /// <summary>
-        /// 寮傛鎵цMES涓婁紶 - 涓嶉樆濉炰富涓氬姟閫昏緫
-        /// </summary>
-        /// <param name="palletCode">鎵樼洏鍙�</param>
-        /// <param name="successStatus">鎴愬姛鏃剁殑鐘舵�佹灇涓惧�硷紙濂囨暟锛�</param>
-        /// <param name="uploadFunc">鍏蜂綋鐨凪ES璋冪敤鍑芥暟</param>
-        private async Task MesUploadAsync(string palletCode, MesUploadStatusEnum successStatus, Func<Task<HttpResponseResult<MesResponse>>> uploadFunc)
-        {
-            var stopwatch = Stopwatch.StartNew();
-            string requestJson = "";
-            string responseJson = "";
-            bool isSuccess = false;
-            string errorMessage = "";
-
-            try
-            {
-                // 璋冪敤MES
-                var result = await uploadFunc();
-
-                stopwatch.Stop();
-                isSuccess = result?.Data?.IsSuccess ?? false;
-                errorMessage = result?.Data?.Msg ?? result?.ErrorMessage ?? "鏈煡閿欒";
-                responseJson = JsonConvert.SerializeObject(result);
-
-                // 鏍规嵁鎴愬姛/澶辫触鍐冲畾鐘舵�佸�硷細濂囨暟=鎴愬姛锛屽伓鏁�=澶辫触
-                var uploadStatus = isSuccess ? (int)successStatus : (int)successStatus + 1;
-
-                // 鏇存柊搴撳瓨琛ㄧ姸鎬侊紙涓嶇瓑寰咃級
-                _ = _stockInfoService.UpdateMesUploadStatusAsync(palletCode, uploadStatus);
-
-                // 璁板綍MES鏃ュ織
-                await LogMesCallAsync(palletCode, successStatus.ToString(), requestJson, responseJson,
-                    stopwatch.ElapsedMilliseconds, isSuccess ? "鎴愬姛" : "澶辫触", errorMessage);
-            }
-            catch (Exception ex)
-            {
-                stopwatch.Stop();
-                errorMessage = ex.Message;
-
-                // 鏇存柊鐘舵�佷负澶辫触锛坰uccessStatus+1 鍗充负澶辫触鐘舵�侊級
-                var uploadStatus = (int)successStatus + 1;
-                _ = _stockInfoService.UpdateMesUploadStatusAsync(palletCode, uploadStatus);
-
-                // 璁板綍寮傚父鏃ュ織
-                await LogMesCallAsync(palletCode, successStatus.ToString(), requestJson, responseJson,
-                    stopwatch.ElapsedMilliseconds, "澶辫触", errorMessage);
-            }
-        }
-
-        /// <summary>
-        /// 璁板綍MES鎺ュ彛璋冪敤鏃ュ織
-        /// </summary>
-        private async Task LogMesCallAsync(string palletCode, string apiType, string requestJson,
-            string responseJson, long durationMs, string status, string errorMessage)
-        {
-            try
-            {
-                var mesLog = new MesApiLogDto
-                {
-                    PalletCode = palletCode,
-                    ApiType = apiType,
-                    RequestJson = requestJson,
-                    ResponseJson = responseJson,
-                    IsSuccess = status == "鎴愬姛",
-                    ErrorMessage = errorMessage,
-                    ElapsedMs = (int)durationMs,
-                    Creator = "System"
-                };
-                await _mesLogService.LogAsync(mesLog);
-            }
-            catch
-            {
-                // 鏃ュ織璁板綍澶辫触涓嶅奖鍝嶄富娴佺▼
-            }
-        }
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3