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

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs |  241 ++++++++++++++++++++++++++---------------------
 1 files changed, 133 insertions(+), 108 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
index 4abd388..fde583b 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
@@ -7,6 +7,7 @@
 using WIDESEA_IBasicService;
 using WIDESEA_ISystemService;
 using WIDESEA_Model.Models;
+using WIDESEA_Common.StockEnum;
 using System.Diagnostics;
 
 namespace WIDESEA_WMSServer.Controllers.Stock
@@ -45,7 +46,6 @@
         public async Task<WebResponseContent> BindContainer([FromBody] BindContainerRequestDto dto)
         {
             var response = new WebResponseContent();
-            var stopwatch = Stopwatch.StartNew();
 
             try
             {
@@ -80,46 +80,55 @@
 
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
-                // 4. 璋冪敤MES鎺ュ彛锛堝悓姝ユ柟娉曪級
-                var mesResult = _mesService.BindContainer(mesRequest);
-                stopwatch.Stop();
-
-                // 5. 璁板綍鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
+                // 4. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
+                _ = Task.Run(async () =>
                 {
-                    ApiType = "BindContainer",
-                    RequestJson = requestJson,
-                    ResponseJson = System.Text.Json.JsonSerializer.Serialize(mesResult),
-                    IsSuccess = mesResult.IsSuccess,
-                    ErrorMessage = mesResult.ErrorMessage,
-                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
-                    Creator = App.User.UserName
+                    var localStopwatch = Stopwatch.StartNew();
+                    try
+                    {
+                        var result = _mesService.BindContainer(mesRequest);
+                        localStopwatch.Stop();
+
+                        bool isSuccess = result?.IsSuccess ?? false;
+                        int status = isSuccess
+                            ? (int)MesUploadStatusEnum.缁勭洏涓婁紶鎴愬姛
+                            : (int)MesUploadStatusEnum.缁勭洏涓婁紶澶辫触;
+
+                        await _stockInfoService.UpdateMesUploadStatusAsync(stockInfo.PalletCode, status);
+
+                        await _mesLogService.LogAsync(new MesApiLogDto
+                        {
+                            PalletCode = stockInfo.PalletCode,
+                            ApiType = "BindContainer",
+                            RequestJson = requestJson,
+                            ResponseJson = System.Text.Json.JsonSerializer.Serialize(result),
+                            IsSuccess = isSuccess,
+                            ErrorMessage = result?.ErrorMessage ?? "鏈煡閿欒",
+                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
+                            Creator = App.User.UserName
+                        });
+                    }
+                    catch (Exception ex)
+                    {
+                        localStopwatch.Stop();
+                        await _stockInfoService.UpdateMesUploadStatusAsync(stockInfo.PalletCode, (int)MesUploadStatusEnum.缁勭洏涓婁紶澶辫触);
+                        await _mesLogService.LogAsync(new MesApiLogDto
+                        {
+                            PalletCode = stockInfo.PalletCode,
+                            ApiType = "BindContainer",
+                            IsSuccess = false,
+                            ErrorMessage = ex.Message,
+                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
+                            Creator = App.User.UserName
+                        });
+                    }
                 });
 
-                // 6. 杩斿洖缁撴灉
-                if (mesResult.IsSuccess)
-                {
-                    return response.OK("鎵樼洏鐢佃姱缁戝畾鎴愬姛");
-                }
-                else
-                {
-                    return response.Error($"MES鎺ュ彛璋冪敤澶辫触: {mesResult.ErrorMessage}");
-                }
+                // 5. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
+                return response.OK("鎵樼洏鐢佃姱缁戝畾鎴愬姛");
             }
-            catch (System.Exception ex)
+            catch (Exception ex)
             {
-                stopwatch.Stop();
-
-                // 璁板綍閿欒鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
-                {
-                    ApiType = "BindContainer",
-                    IsSuccess = false,
-                    ErrorMessage = ex.Message,
-                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
-                    Creator = App.User.UserName
-                });
-
                 return response.Error($"鎵樼洏鐢佃姱缁戝畾澶辫触: {ex.Message}");
             }
         }
@@ -133,7 +142,6 @@
         public async Task<WebResponseContent> UnbindContainer([FromBody] UnbindContainerRequestDto dto)
         {
             var response = new WebResponseContent();
-            var stopwatch = Stopwatch.StartNew();
 
             try
             {
@@ -163,46 +171,55 @@
 
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
-                // 4. 璋冪敤MES鎺ュ彛锛堝悓姝ユ柟娉曪級
-                var mesResult = _mesService.UnBindContainer(mesRequest);
-                stopwatch.Stop();
-
-                // 5. 璁板綍鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
+                // 4. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
+                _ = Task.Run(async () =>
                 {
-                    ApiType = "UnbindContainer",
-                    RequestJson = requestJson,
-                    ResponseJson = System.Text.Json.JsonSerializer.Serialize(mesResult),
-                    IsSuccess = mesResult.IsSuccess,
-                    ErrorMessage = mesResult.ErrorMessage,
-                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
-                    Creator = App.User.UserName
+                    var localStopwatch = Stopwatch.StartNew();
+                    try
+                    {
+                        var result = _mesService.UnBindContainer(mesRequest);
+                        localStopwatch.Stop();
+
+                        bool isSuccess = result?.IsSuccess ?? false;
+                        int status = isSuccess
+                            ? (int)MesUploadStatusEnum.鎷嗙洏涓婁紶鎴愬姛
+                            : (int)MesUploadStatusEnum.鎷嗙洏涓婁紶澶辫触;
+
+                        await _stockInfoService.UpdateMesUploadStatusAsync(stockInfo.PalletCode, status);
+
+                        await _mesLogService.LogAsync(new MesApiLogDto
+                        {
+                            PalletCode = stockInfo.PalletCode,
+                            ApiType = "UnbindContainer",
+                            RequestJson = requestJson,
+                            ResponseJson = System.Text.Json.JsonSerializer.Serialize(result),
+                            IsSuccess = isSuccess,
+                            ErrorMessage = result?.ErrorMessage ?? "鏈煡閿欒",
+                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
+                            Creator = App.User.UserName
+                        });
+                    }
+                    catch (Exception ex)
+                    {
+                        localStopwatch.Stop();
+                        await _stockInfoService.UpdateMesUploadStatusAsync(stockInfo.PalletCode, (int)MesUploadStatusEnum.鎷嗙洏涓婁紶澶辫触);
+                        await _mesLogService.LogAsync(new MesApiLogDto
+                        {
+                            PalletCode = stockInfo.PalletCode,
+                            ApiType = "UnbindContainer",
+                            IsSuccess = false,
+                            ErrorMessage = ex.Message,
+                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
+                            Creator = App.User.UserName
+                        });
+                    }
                 });
 
-                // 6. 杩斿洖缁撴灉
-                if (mesResult.IsSuccess)
-                {
-                    return response.OK("鎵樼洏鐢佃姱瑙g粦鎴愬姛");
-                }
-                else
-                {
-                    return response.Error($"MES鎺ュ彛璋冪敤澶辫触: {mesResult.ErrorMessage}");
-                }
+                // 5. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
+                return response.OK("鎵樼洏鐢佃姱瑙g粦鎴愬姛");
             }
-            catch (System.Exception ex)
+            catch (Exception ex)
             {
-                stopwatch.Stop();
-
-                // 璁板綍閿欒鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
-                {
-                    ApiType = "UnbindContainer",
-                    IsSuccess = false,
-                    ErrorMessage = ex.Message,
-                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
-                    Creator = App.User.UserName
-                });
-
                 return response.Error($"鎵樼洏鐢佃姱瑙g粦澶辫触: {ex.Message}");
             }
         }
@@ -216,7 +233,6 @@
         public async Task<WebResponseContent> ContainerNgReport([FromBody] ContainerNgReportRequestDto dto)
         {
             var response = new WebResponseContent();
-            var stopwatch = Stopwatch.StartNew();
 
             try
             {
@@ -258,46 +274,55 @@
 
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
-                // 4. 璋冪敤MES鎺ュ彛锛堝悓姝ユ柟娉曪級
-                var mesResult = _mesService.ContainerNgReport(mesRequest);
-                stopwatch.Stop();
-
-                // 5. 璁板綍鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
+                // 4. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
+                _ = Task.Run(async () =>
                 {
-                    ApiType = "ContainerNgReport",
-                    RequestJson = requestJson,
-                    ResponseJson = System.Text.Json.JsonSerializer.Serialize(mesResult),
-                    IsSuccess = mesResult.IsSuccess,
-                    ErrorMessage = mesResult.ErrorMessage,
-                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
-                    Creator = App.User.UserName
+                    var localStopwatch = Stopwatch.StartNew();
+                    try
+                    {
+                        var result = _mesService.ContainerNgReport(mesRequest);
+                        localStopwatch.Stop();
+
+                        bool isSuccess = result?.IsSuccess ?? false;
+                        int status = isSuccess
+                            ? (int)MesUploadStatusEnum.NG涓婃姤鎴愬姛
+                            : (int)MesUploadStatusEnum.NG涓婃姤澶辫触;
+
+                        await _stockInfoService.UpdateMesUploadStatusAsync(stockInfo.PalletCode, status);
+
+                        await _mesLogService.LogAsync(new MesApiLogDto
+                        {
+                            PalletCode = stockInfo.PalletCode,
+                            ApiType = "ContainerNgReport",
+                            RequestJson = requestJson,
+                            ResponseJson = System.Text.Json.JsonSerializer.Serialize(result),
+                            IsSuccess = isSuccess,
+                            ErrorMessage = result?.ErrorMessage ?? "鏈煡閿欒",
+                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
+                            Creator = App.User.UserName
+                        });
+                    }
+                    catch (Exception ex)
+                    {
+                        localStopwatch.Stop();
+                        await _stockInfoService.UpdateMesUploadStatusAsync(stockInfo.PalletCode, (int)MesUploadStatusEnum.NG涓婃姤澶辫触);
+                        await _mesLogService.LogAsync(new MesApiLogDto
+                        {
+                            PalletCode = stockInfo.PalletCode,
+                            ApiType = "ContainerNgReport",
+                            IsSuccess = false,
+                            ErrorMessage = ex.Message,
+                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
+                            Creator = App.User.UserName
+                        });
+                    }
                 });
 
-                // 6. 杩斿洖缁撴灉
-                if (mesResult.IsSuccess)
-                {
-                    return response.OK("NG鐢佃姱涓婃姤鎴愬姛");
-                }
-                else
-                {
-                    return response.Error($"MES鎺ュ彛璋冪敤澶辫触: {mesResult.ErrorMessage}");
-                }
+                // 5. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
+                return response.OK("NG鐢佃姱涓婃姤鎴愬姛");
             }
-            catch (System.Exception ex)
+            catch (Exception ex)
             {
-                stopwatch.Stop();
-
-                // 璁板綍閿欒鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
-                {
-                    ApiType = "ContainerNgReport",
-                    IsSuccess = false,
-                    ErrorMessage = ex.Message,
-                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
-                    Creator = App.User.UserName
-                });
-
                 return response.Error($"NG鐢佃姱涓婃姤澶辫触: {ex.Message}");
             }
         }

--
Gitblit v1.9.3