From fac6be89f3a02270b7b4a1bbe1f0dc05788c368e Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 21 四月 2026 13:19:53 +0800
Subject: [PATCH] Merge branch 'dev' of http://115.159.85.185:8098/r/SuZhouGuanHong/ShanMeiXinNengYuan into dev

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/MesUploadHelper.cs |   93 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 93 insertions(+), 0 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/MesUploadHelper.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/MesUploadHelper.cs
new file mode 100644
index 0000000..52672b6
--- /dev/null
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/MesUploadHelper.cs
@@ -0,0 +1,93 @@
+using System.Diagnostics;
+using WIDESEA_Common.StockEnum;
+using WIDESEA_Core;
+using WIDESEA_DTO.MES;
+using WIDESEA_IStockService;
+using WIDESEA_IBasicService;
+
+namespace WIDESEA_StockService
+{
+    /// <summary>
+    /// MES寮傛涓婁紶杈呭姪鏈嶅姟瀹炵幇
+    /// </summary>
+    public class MesUploadHelper : IMesUploadHelper
+    {
+        private readonly IStockInfoService _stockInfoService;
+        private readonly IMesLogService _mesLogService;
+
+        /// <summary>
+        /// 鏋勯�犲嚱鏁�
+        /// </summary>
+        /// <param name="stockInfoService">搴撳瓨淇℃伅鏈嶅姟</param>
+        /// <param name="mesLogService">MES鏃ュ織鏈嶅姟</param>
+        public MesUploadHelper(IStockInfoService stockInfoService, IMesLogService mesLogService)
+        {
+            _stockInfoService = stockInfoService;
+            _mesLogService = mesLogService;
+        }
+
+        /// <summary>
+        /// 浠ire-and-forget鏂瑰紡寮傛鎵цMES璋冪敤锛岃嚜鍔ㄦ洿鏂颁笂浼犵姸鎬佸苟璁板綍鏃ュ織
+        /// </summary>
+        public void FireAndForget(
+            string palletCode,
+            MesUploadStatusEnum successStatus,
+            string apiType,
+            string requestJson,
+            Func<(bool isSuccess, string responseJson, string errorMessage)> mesCall,
+            string creator = "System")
+        {
+            _ = Task.Run(async () =>
+            {
+                var stopwatch = Stopwatch.StartNew();
+                try
+                {
+                    var (isSuccess, responseJson, errorMessage) = mesCall();
+                    stopwatch.Stop();
+
+                    // 濂囨暟=鎴愬姛锛屽伓鏁�=澶辫触
+                    int status = isSuccess ? (int)successStatus : (int)successStatus + 1;
+                    await _stockInfoService.UpdateMesUploadStatusAsync(palletCode, status);
+
+                    await LogAsync(palletCode, apiType, requestJson, responseJson,
+                        stopwatch.ElapsedMilliseconds, isSuccess, errorMessage, creator);
+                }
+                catch (Exception ex)
+                {
+                    stopwatch.Stop();
+                    int status = (int)successStatus + 1;
+                    await _stockInfoService.UpdateMesUploadStatusAsync(palletCode, status);
+
+                    await LogAsync(palletCode, apiType, requestJson, "",
+                        stopwatch.ElapsedMilliseconds, false, ex.Message, creator);
+                }
+            });
+        }
+
+        /// <summary>
+        /// 璁板綍MES鎺ュ彛璋冪敤鏃ュ織
+        /// </summary>
+        private async Task LogAsync(string palletCode, string apiType, string requestJson,
+            string responseJson, long elapsedMs, bool isSuccess, string errorMessage, string creator)
+        {
+            try
+            {
+                await _mesLogService.LogAsync(new MesApiLogDto
+                {
+                    PalletCode = palletCode,
+                    ApiType = apiType,
+                    RequestJson = requestJson,
+                    ResponseJson = responseJson,
+                    IsSuccess = isSuccess,
+                    ErrorMessage = errorMessage,
+                    ElapsedMs = (int)elapsedMs,
+                    Creator = creator
+                });
+            }
+            catch
+            {
+                // 鏃ュ織璁板綍澶辫触涓嶅奖鍝嶄富娴佺▼
+            }
+        }
+    }
+}

--
Gitblit v1.9.3