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_WMSServer/Controllers/Stock/StockInfoDetailController.cs |  178 +++++++++++++++-------------------------------------------
 1 files changed, 47 insertions(+), 131 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 3680ce8..1b3f335 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
@@ -9,7 +9,6 @@
 using WIDESEA_Model.Models;
 using WIDESEA_Common.Constants;
 using WIDESEA_Common.StockEnum;
-using System.Diagnostics;
 
 namespace WIDESEA_WMSServer.Controllers.Stock
 {
@@ -20,25 +19,25 @@
     [ApiController]
     public class StockInfoDetailController : ApiBaseController<IStockInfoDetailService, Dt_StockInfoDetail>
     {
-        private readonly IMesLogService _mesLogService;
         private readonly IMesService _mesService;
         private readonly ISys_DictionaryService _sysDictionaryService;
         private readonly IStockInfoService _stockInfoService;
         private readonly IMESDeviceConfigService _mesDeviceConfigService;
+        private readonly IMesUploadHelper _mesUploadHelper;
 
         public StockInfoDetailController(
             IStockInfoDetailService service,
-            IMesLogService mesLogService,
             IMesService mesService,
             ISys_DictionaryService sysDictionaryService,
             IStockInfoService stockInfoService,
-            IMESDeviceConfigService mesDeviceConfigService) : base(service)
+            IMESDeviceConfigService mesDeviceConfigService,
+            IMesUploadHelper mesUploadHelper) : base(service)
         {
-            _mesLogService = mesLogService;
             _mesService = mesService;
             _sysDictionaryService = sysDictionaryService;
             _stockInfoService = stockInfoService;
             _mesDeviceConfigService = mesDeviceConfigService;
+            _mesUploadHelper = mesUploadHelper;
         }
 
         /// <summary>
@@ -91,51 +90,23 @@
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
                 // 5. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
-                var localToken = token;
-                _ = Task.Run(async () =>
-                {
-                    var localStopwatch = Stopwatch.StartNew();
-                    try
+                _mesUploadHelper.FireAndForget(
+                    stockInfo.PalletCode,
+                    MesUploadStatusEnum.缁勭洏涓婁紶鎴愬姛,
+                    "BindContainer",
+                    requestJson,
+                    () =>
                     {
-                        var result = string.IsNullOrWhiteSpace(localToken)
+                        var result = string.IsNullOrWhiteSpace(token)
                             ? _mesService.BindContainer(mesRequest)
-                            : _mesService.BindContainer(mesRequest, localToken);
-                        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
-                        });
-                    }
-                });
+                            : _mesService.BindContainer(mesRequest, token);
+                        return (
+                            result?.IsSuccess ?? false,
+                            System.Text.Json.JsonSerializer.Serialize(result),
+                            result?.ErrorMessage ?? "鏈煡閿欒"
+                        );
+                    },
+                    App.User.UserName);
 
                 // 6. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
                 return response.OK("鎵樼洏鐢佃姱缁戝畾鎴愬姛");
@@ -191,51 +162,23 @@
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
                 // 5. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
-                var localToken = token;
-                _ = Task.Run(async () =>
-                {
-                    var localStopwatch = Stopwatch.StartNew();
-                    try
+                _mesUploadHelper.FireAndForget(
+                    stockInfo.PalletCode,
+                    MesUploadStatusEnum.鎷嗙洏涓婁紶鎴愬姛,
+                    "UnbindContainer",
+                    requestJson,
+                    () =>
                     {
-                        var result = string.IsNullOrWhiteSpace(localToken)
+                        var result = string.IsNullOrWhiteSpace(token)
                             ? _mesService.UnBindContainer(mesRequest)
-                            : _mesService.UnBindContainer(mesRequest, localToken);
-                        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
-                        });
-                    }
-                });
+                            : _mesService.UnBindContainer(mesRequest, token);
+                        return (
+                            result?.IsSuccess ?? false,
+                            System.Text.Json.JsonSerializer.Serialize(result),
+                            result?.ErrorMessage ?? "鏈煡閿欒"
+                        );
+                    },
+                    App.User.UserName);
 
                 // 6. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
                 return response.OK("鎵樼洏鐢佃姱瑙g粦鎴愬姛");
@@ -302,48 +245,21 @@
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
                 // 5. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
-                _ = Task.Run(async () =>
-                {
-                    var localStopwatch = Stopwatch.StartNew();
-                    try
+                _mesUploadHelper.FireAndForget(
+                    stockInfo.PalletCode,
+                    MesUploadStatusEnum.NG涓婃姤鎴愬姛,
+                    "ContainerNgReport",
+                    requestJson,
+                    () =>
                     {
                         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
-                        });
-                    }
-                });
+                        return (
+                            result?.IsSuccess ?? false,
+                            System.Text.Json.JsonSerializer.Serialize(result),
+                            result?.ErrorMessage ?? "鏈煡閿欒"
+                        );
+                    },
+                    App.User.UserName);
 
                 // 6. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
                 return response.OK("NG鐢佃姱涓婃姤鎴愬姛");

--
Gitblit v1.9.3