From 4506a1ed31e5d3c009ea5049f385ada6527bb595 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 21 四月 2026 00:18:08 +0800
Subject: [PATCH] refactor(StockService): MES调用改为Task.Run异步执行

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs |   97 +++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 83 insertions(+), 14 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
index 9ea94a7..81914e7 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -1,8 +1,10 @@
 锘縰sing Newtonsoft.Json;
 using SqlSugar;
+using System.Diagnostics;
 using WIDESEA_Common.Constants;
 using WIDESEA_Common.StockEnum;
 using WIDESEA_Core;
+using WIDESEA_Core.Helper;
 using WIDESEA_DTO.MES;
 using WIDESEA_DTO.Stock;
 using WIDESEA_IBasicService;
@@ -51,6 +53,8 @@
         /// </summary>
         public IMesService _mesService { get; }
 
+        private readonly IMesLogService _mesLogService;
+
         /// <summary>
         /// 鏋勯�犲嚱鏁�
         /// </summary>
@@ -65,7 +69,8 @@
             IStockInfo_HtyService stockInfo_HtyService,
             IMesService mesService,
             IWarehouseService warehouseService,
-            ISqlSugarClient sqlSugarClient)
+            ISqlSugarClient sqlSugarClient,
+            IMesLogService mesLogService)
         {
             StockInfoDetailService = stockInfoDetailService;
             StockInfoService = stockInfoService;
@@ -74,6 +79,7 @@
             _mesService = mesService;
             _warehouseService = warehouseService;
             SqlSugarClient = sqlSugarClient;
+            _mesLogService = mesLogService;
         }
 
         /// <summary>
@@ -429,7 +435,7 @@
                     }
                 }
 
-                // 3. 璋冪敤MES瑙g粦
+                // 3. Fire-and-forget寮傛璋冪敤MES瑙g粦
                 var unbindRequest = new UnBindContainerRequest
                 {
                     EquipmentCode = equipmentCode,
@@ -438,13 +444,43 @@
                     ContainCode = palletCode,
                     SfcList = sfcList
                 };
-                var unbindResult = string.IsNullOrWhiteSpace(token)
-                    ? _mesService.UnBindContainer(unbindRequest)
-                    : _mesService.UnBindContainer(unbindRequest, token);
-                if (unbindResult == null || unbindResult.Data == null || !unbindResult.Data.IsSuccess)
+                _ = Task.Run(() =>
                 {
-                    return content.Error($"MES瑙g粦澶辫触: {unbindResult?.Data?.Msg ?? unbindResult?.ErrorMessage ?? "鏈煡閿欒"}");
-                }
+                    var stopwatch = Stopwatch.StartNew();
+                    try
+                    {
+                        var unbindResult = string.IsNullOrWhiteSpace(token)
+                            ? _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);
+                    }
+                });
 
                 // 4. 鍒犻櫎涓存椂琛ㄨ褰�
                 await SqlSugarClient.Deleteable<Dt_SplitTemp>().Where(t => t.PalletCode == palletCode).ExecuteCommandAsync();
@@ -466,6 +502,7 @@
         public async Task<WebResponseContent> GroupPalletConfirmAsync(string palletCode, string deviceName)
         {
             WebResponseContent content = new WebResponseContent();
+            var stopwatch = Stopwatch.StartNew();
             try
             {
                 if (string.IsNullOrWhiteSpace(palletCode))
@@ -510,13 +547,45 @@
                         Location = d.InboundOrderRowNo.ToString()
                     }).ToList()
                 };
-                var bindResult = string.IsNullOrWhiteSpace(token)
-                    ? _mesService.BindContainer(bindRequest)
-                    : _mesService.BindContainer(bindRequest, token);
-                if (bindResult == null || bindResult.Data == null || !bindResult.Data.IsSuccess)
+                string requestJson = bindRequest.ToJson();
+                // 3. Fire-and-forget寮傛璋冪敤MES缁戝畾
+                _ = Task.Run(() =>
                 {
-                    return content.Error($"MES缁戝畾澶辫触: {bindResult?.Data?.Msg ?? bindResult?.ErrorMessage ?? "鏈煡閿欒"}");
-                }
+                    var stopwatch = Stopwatch.StartNew();
+                    try
+                    {
+                        var bindResult = string.IsNullOrWhiteSpace(token)
+                            ? _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);
+                    }
+                });
 
                 return content.OK("鎵归噺缁勭洏纭鎴愬姛");
             }

--
Gitblit v1.9.3