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 |   96 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 73 insertions(+), 23 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
index 0d17367..81914e7 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -435,7 +435,7 @@
                     }
                 }
 
-                // 3. 璋冪敤MES瑙g粦
+                // 3. Fire-and-forget寮傛璋冪敤MES瑙g粦
                 var unbindRequest = new UnBindContainerRequest
                 {
                     EquipmentCode = equipmentCode,
@@ -444,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();
@@ -518,24 +548,44 @@
                     }).ToList()
                 };
                 string requestJson = bindRequest.ToJson();
-                var bindResult = string.IsNullOrWhiteSpace(token)
-                    ? _mesService.BindContainer(bindRequest)
-                    : _mesService.BindContainer(bindRequest, token);
-                stopwatch.Stop();
-                await _mesLogService.LogAsync(new MesApiLogDto
+                // 3. Fire-and-forget寮傛璋冪敤MES缁戝畾
+                _ = Task.Run(() =>
                 {
-                    ApiType = "BindContainer",
-                    RequestJson = requestJson,
-                    ResponseJson = System.Text.Json.JsonSerializer.Serialize(bindResult),
-                    IsSuccess = bindResult.IsSuccess,
-                    ErrorMessage = bindResult.ErrorMessage,
-                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
-                    Creator = "systeam"
+                    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);
+                    }
                 });
-                if (bindResult == null || bindResult.Data == null || !bindResult.Data.IsSuccess)
-                {
-                    return content.Error($"MES缁戝畾澶辫触: {bindResult?.Data?.Msg ?? bindResult?.ErrorMessage ?? "鏈煡閿欒"}");
-                }
 
                 return content.OK("鎵归噺缁勭洏纭鎴愬姛");
             }

--
Gitblit v1.9.3