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_StockService/StockSerivce.cs |  111 +++++++++++++++++++------------------------------------
 1 files changed, 38 insertions(+), 73 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
index 81914e7..1e37368 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -1,6 +1,5 @@
 锘縰sing Newtonsoft.Json;
 using SqlSugar;
-using System.Diagnostics;
 using WIDESEA_Common.Constants;
 using WIDESEA_Common.StockEnum;
 using WIDESEA_Core;
@@ -54,6 +53,7 @@
         public IMesService _mesService { get; }
 
         private readonly IMesLogService _mesLogService;
+        private readonly IMesUploadHelper _mesUploadHelper;
 
         /// <summary>
         /// 鏋勯�犲嚱鏁�
@@ -62,6 +62,7 @@
         /// <param name="stockInfoService">搴撳瓨淇℃伅鏈嶅姟</param>
         /// <param name="stockInfoDetail_HtyService">搴撳瓨鏄庣粏鍘嗗彶鏈嶅姟</param>
         /// <param name="stockInfo_HtyService">搴撳瓨鍘嗗彶鏈嶅姟</param>
+        /// <param name="mesUploadHelper">MES寮傛涓婁紶杈呭姪鏈嶅姟</param>
         public StockService(
             IStockInfoDetailService stockInfoDetailService,
             IStockInfoService stockInfoService,
@@ -70,7 +71,8 @@
             IMesService mesService,
             IWarehouseService warehouseService,
             ISqlSugarClient sqlSugarClient,
-            IMesLogService mesLogService)
+            IMesLogService mesLogService,
+            IMesUploadHelper mesUploadHelper)
         {
             StockInfoDetailService = stockInfoDetailService;
             StockInfoService = stockInfoService;
@@ -80,6 +82,7 @@
             _warehouseService = warehouseService;
             SqlSugarClient = sqlSugarClient;
             _mesLogService = mesLogService;
+            _mesUploadHelper = mesUploadHelper;
         }
 
         /// <summary>
@@ -444,43 +447,25 @@
                     ContainCode = palletCode,
                     SfcList = sfcList
                 };
-                _ = Task.Run(() =>
-                {
-                    var stopwatch = Stopwatch.StartNew();
-                    try
+                string requestJson = unbindRequest.ToJson();
+                var localToken = token;
+
+                _mesUploadHelper.FireAndForget(
+                    palletCode,
+                    MesUploadStatusEnum.鎷嗙洏涓婁紶鎴愬姛,
+                    "UnBindContainer",
+                    requestJson,
+                    () =>
                     {
-                        var unbindResult = string.IsNullOrWhiteSpace(token)
+                        var result = string.IsNullOrWhiteSpace(localToken)
                             ? _mesService.UnBindContainer(unbindRequest)
-                            : _mesService.UnBindContainer(unbindRequest, token);
-                        stopwatch.Stop();
-
-                        bool isSuccess = unbindResult?.Data?.IsSuccess ?? false;
-                        int status = isSuccess
-                            ? (int)MesUploadStatusEnum.鎷嗙洏涓婁紶鎴愬姛
-                            : (int)MesUploadStatusEnum.鎷嗙洏涓婁紶澶辫触;
-
-                        // 鏇存柊MES涓婁紶鐘舵��
-                        StockInfoService.UpdateMesUploadStatusAsync(palletCode, status).ConfigureAwait(false);
-
-                        // 璁板綍MES鏃ュ織
-                        _mesLogService.LogAsync(new MesApiLogDto
-                        {
-                            PalletCode = palletCode,
-                            ApiType = "UnBindContainer",
-                            RequestJson = unbindRequest.ToJson(),
-                            ResponseJson = System.Text.Json.JsonSerializer.Serialize(unbindResult),
-                            IsSuccess = isSuccess,
-                            ErrorMessage = unbindResult?.Data?.Msg ?? unbindResult?.ErrorMessage ?? "鏈煡閿欒",
-                            ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
-                            Creator = "System"
-                        }).ConfigureAwait(false);
-                    }
-                    catch (Exception ex)
-                    {
-                        // 璋冪敤澶辫触
-                        StockInfoService.UpdateMesUploadStatusAsync(palletCode, (int)MesUploadStatusEnum.鎷嗙洏涓婁紶澶辫触).ConfigureAwait(false);
-                    }
-                });
+                            : _mesService.UnBindContainer(unbindRequest, localToken);
+                        return (
+                            result?.Data?.IsSuccess ?? false,
+                            System.Text.Json.JsonSerializer.Serialize(result),
+                            result?.Data?.Msg ?? result?.ErrorMessage ?? "鏈煡閿欒"
+                        );
+                    });
 
                 // 4. 鍒犻櫎涓存椂琛ㄨ褰�
                 await SqlSugarClient.Deleteable<Dt_SplitTemp>().Where(t => t.PalletCode == palletCode).ExecuteCommandAsync();
@@ -502,7 +487,6 @@
         public async Task<WebResponseContent> GroupPalletConfirmAsync(string palletCode, string deviceName)
         {
             WebResponseContent content = new WebResponseContent();
-            var stopwatch = Stopwatch.StartNew();
             try
             {
                 if (string.IsNullOrWhiteSpace(palletCode))
@@ -548,44 +532,25 @@
                     }).ToList()
                 };
                 string requestJson = bindRequest.ToJson();
+                var localToken = token;
+
                 // 3. Fire-and-forget寮傛璋冪敤MES缁戝畾
-                _ = Task.Run(() =>
-                {
-                    var stopwatch = Stopwatch.StartNew();
-                    try
+                _mesUploadHelper.FireAndForget(
+                    palletCode,
+                    MesUploadStatusEnum.缁勭洏涓婁紶鎴愬姛,
+                    "BindContainer",
+                    requestJson,
+                    () =>
                     {
-                        var bindResult = string.IsNullOrWhiteSpace(token)
+                        var result = string.IsNullOrWhiteSpace(localToken)
                             ? _mesService.BindContainer(bindRequest)
-                            : _mesService.BindContainer(bindRequest, token);
-                        stopwatch.Stop();
-
-                        bool isSuccess = bindResult?.Data?.IsSuccess ?? false;
-                        int status = isSuccess
-                            ? (int)MesUploadStatusEnum.缁勭洏涓婁紶鎴愬姛
-                            : (int)MesUploadStatusEnum.缁勭洏涓婁紶澶辫触;
-
-                        // 鏇存柊MES涓婁紶鐘舵��
-                        StockInfoService.UpdateMesUploadStatusAsync(palletCode, status).ConfigureAwait(false);
-
-                        // 璁板綍MES鏃ュ織
-                        _mesLogService.LogAsync(new MesApiLogDto
-                        {
-                            PalletCode = palletCode,
-                            ApiType = "BindContainer",
-                            RequestJson = requestJson,
-                            ResponseJson = System.Text.Json.JsonSerializer.Serialize(bindResult),
-                            IsSuccess = isSuccess,
-                            ErrorMessage = bindResult?.ErrorMessage ?? "鏈煡閿欒",
-                            ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
-                            Creator = "System"
-                        }).ConfigureAwait(false);
-                    }
-                    catch (Exception ex)
-                    {
-                        // 璋冪敤澶辫触
-                        StockInfoService.UpdateMesUploadStatusAsync(palletCode, (int)MesUploadStatusEnum.缁勭洏涓婁紶澶辫触).ConfigureAwait(false);
-                    }
-                });
+                            : _mesService.BindContainer(bindRequest, localToken);
+                        return (
+                            result?.Data?.IsSuccess ?? false,
+                            System.Text.Json.JsonSerializer.Serialize(result),
+                            result?.Data?.Msg ?? result?.ErrorMessage ?? "鏈煡閿欒"
+                        );
+                    });
 
                 return content.OK("鎵归噺缁勭洏纭鎴愬姛");
             }

--
Gitblit v1.9.3