From c36c5c66732415fb5a6d8f68082637bf27cb5538 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 21 四月 2026 00:36:12 +0800
Subject: [PATCH] fix(StockInfoDetailController): MES凭证改为动态获取

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs |  204 +++++++++++++++++++++++++++++----------------------
 1 files changed, 116 insertions(+), 88 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs
index 1d4292c..02447f2 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs
@@ -7,9 +7,9 @@
 using WIDESEA_DTO.MES;
 using WIDESEA_IStockService;
 using WIDESEA_IBasicService;
-using WIDESEA_IMesService;
 using WIDESEA_ISystemService;
 using WIDESEA_Model.Models;
+using WIDESEA_Common.Constants;
 using WIDESEA_Common.StockEnum;
 using System.Diagnostics;
 
@@ -24,17 +24,23 @@
     {
         private readonly IMesLogService _mesLogService;
         private readonly IMesService _mesService;
+        private readonly IMESDeviceConfigService _mesDeviceConfigService;
         private readonly ISys_DictionaryService _sysDictionaryService;
+        private readonly IStockInfoService _stockInfoService;
 
         public StockInfoController(
             IStockInfoService service,
             IMesLogService mesLogService,
             IMesService mesService,
-            ISys_DictionaryService sysDictionaryService) : base(service)
+            IMESDeviceConfigService mesDeviceConfigService,
+            ISys_DictionaryService sysDictionaryService,
+            IStockInfoService stockInfoService) : base(service)
         {
             _mesLogService = mesLogService;
             _mesService = mesService;
+            _mesDeviceConfigService = mesDeviceConfigService;
             _sysDictionaryService = sysDictionaryService;
+            _stockInfoService = stockInfoService;
         }
 
         /// <summary>
@@ -58,7 +64,6 @@
         public async Task<WebResponseContent> InboundInContainer([FromBody] InboundInContainerRequestDto dto)
         {
             var response = new WebResponseContent();
-            var stopwatch = Stopwatch.StartNew();
 
             try
             {
@@ -82,63 +87,75 @@
                     return response.Error($"褰撳墠搴撳瓨鐘舵�佷笉鍏佽杩涚珯鎿嶄綔锛屽綋鍓嶇姸鎬侊細{stockInfo.StockStatus}");
                 }
 
-                // 4. 鑾峰彇绯荤粺閰嶇疆 - 鐩存帴浠庢暟鎹簱鏌ヨ
-                var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
+                // 4. 鍔ㄦ�佽幏鍙朚ES鍑瘉
+                string deviceName = stockInfo.Remark == "GW_1" ? "楂樻俯闈欑疆1"
+                    : stockInfo.Remark == "GW_2" ? "楂樻俯闈欑疆2"
+                    : "甯告俯闈欑疆1";
+                var mesConfig = _mesDeviceConfigService.GetByDeviceName(deviceName);
 
-                string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
-                string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
-
-                // 5. 鏋勯�燤ES璇锋眰
                 var mesRequest = new InboundInContainerRequest
                 {
-                    EquipmentCode = equipmentCode,
-                    ResourceCode = resourceCode,
+                    EquipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE,
+                    ResourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE,
                     LocalTime = DateTime.Now,
                     ContainerCode = dto.PalletCode
                 };
 
+                string token = mesConfig?.Token;
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
+                string palletCode = stockInfo.PalletCode;
 
-                // 6. 璋冪敤MES鎺ュ彛锛堝悓姝ユ柟娉曪級
-                var mesResult = _mesService.InboundInContainer(mesRequest);
-                stopwatch.Stop();
-
-                // 7. 璁板綍鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
+                // 5. 寮傛鎵цMES璋冪敤锛坒ire-and-forget锛�
+                _ = Task.Run(async () =>
                 {
-                    ApiType = "InboundInContainer",
-                    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 = string.IsNullOrWhiteSpace(token)
+                            ? _mesService.InboundInContainer(mesRequest)
+                            : _mesService.InboundInContainer(mesRequest, token);
+                        localStopwatch.Stop();
+
+                        bool isSuccess = result?.IsSuccess ?? false;
+                        int status = isSuccess
+                            ? (int)MesUploadStatusEnum.杩涚珯涓婁紶鎴愬姛
+                            : (int)MesUploadStatusEnum.杩涚珯涓婁紶澶辫触;
+
+                        await _stockInfoService.UpdateMesUploadStatusAsync(palletCode, status);
+
+                        await _mesLogService.LogAsync(new MesApiLogDto
+                        {
+                            PalletCode = palletCode,
+                            ApiType = "InboundInContainer",
+                            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(palletCode, (int)MesUploadStatusEnum.杩涚珯涓婁紶澶辫触);
+                        await _mesLogService.LogAsync(new MesApiLogDto
+                        {
+                            PalletCode = palletCode,
+                            ApiType = "InboundInContainer",
+                            IsSuccess = false,
+                            ErrorMessage = ex.Message,
+                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
+                            Creator = App.User.UserName
+                        });
+                    }
                 });
 
-                // 8. 杩斿洖缁撴灉
-                if (mesResult.IsSuccess)
-                {
-                    return response.OK("鎵樼洏杩涚珯鎴愬姛");
-                }
-                else
-                {
-                    return response.Error($"MES鎺ュ彛璋冪敤澶辫触: {mesResult.ErrorMessage}");
-                }
+                // 6. 绔嬪嵆杩斿洖鎴愬姛
+                return response.OK("鎵樼洏杩涚珯鎴愬姛");
             }
             catch (System.Exception ex)
             {
-                stopwatch.Stop();
-
-                // 璁板綍閿欒鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
-                {
-                    ApiType = "InboundInContainer",
-                    IsSuccess = false,
-                    ErrorMessage = ex.Message,
-                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
-                    Creator = App.User.UserName
-                });
-
                 return response.Error($"鎵樼洏杩涚珯澶辫触: {ex.Message}");
             }
         }
@@ -152,7 +169,6 @@
         public async Task<WebResponseContent> OutboundInContainer([FromBody] OutboundInContainerRequestDto dto)
         {
             var response = new WebResponseContent();
-            var stopwatch = Stopwatch.StartNew();
 
             try
             {
@@ -182,17 +198,16 @@
                     return response.Error($"褰撳墠搴撳瓨鐘舵�佷笉鍏佽鍑虹珯鎿嶄綔锛屽綋鍓嶇姸鎬侊細{stockInfo.StockStatus}");
                 }
 
-                // 4. 鑾峰彇绯荤粺閰嶇疆
-                var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
+                // 4. 鍔ㄦ�佽幏鍙朚ES鍑瘉
+                string deviceName = stockInfo.Remark == "GW_1" ? "楂樻俯闈欑疆1"
+                    : stockInfo.Remark == "GW_2" ? "楂樻俯闈欑疆2"
+                    : "甯告俯闈欑疆1";
+                var mesConfig = _mesDeviceConfigService.GetByDeviceName(deviceName);
 
-                string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
-                string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
-
-                // 5. 鏋勯�燤ES璇锋眰
                 var mesRequest = new OutboundInContainerRequest
                 {
-                    EquipmentCode = equipmentCode,
-                    ResourceCode = resourceCode,
+                    EquipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE,
+                    ResourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE,
                     LocalTime = DateTime.Now,
                     ContainerCode = dto.PalletCode,
                     ParamList = dto.ParamList?.Select(p => new ParamItem
@@ -203,48 +218,61 @@
                     }).ToList()
                 };
 
+                string token = mesConfig?.Token;
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
+                string palletCode = stockInfo.PalletCode;
 
-                // 6. 璋冪敤MES鎺ュ彛锛堝悓姝ユ柟娉曪級
-                var mesResult = _mesService.OutboundInContainer(mesRequest);
-                stopwatch.Stop();
-
-                // 7. 璁板綍鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
+                // 5. 寮傛鎵цMES璋冪敤锛坒ire-and-forget锛�
+                _ = Task.Run(async () =>
                 {
-                    ApiType = "OutboundInContainer",
-                    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 = string.IsNullOrWhiteSpace(token)
+                            ? _mesService.OutboundInContainer(mesRequest)
+                            : _mesService.OutboundInContainer(mesRequest, token);
+                        localStopwatch.Stop();
+
+                        bool isSuccess = result?.IsSuccess ?? false;
+                        int status = isSuccess
+                            ? (int)MesUploadStatusEnum.鍑虹珯涓婁紶鎴愬姛
+                            : (int)MesUploadStatusEnum.鍑虹珯涓婁紶澶辫触;
+
+                        await _stockInfoService.UpdateMesUploadStatusAsync(palletCode, status);
+
+                        await _mesLogService.LogAsync(new MesApiLogDto
+                        {
+                            PalletCode = palletCode,
+                            ApiType = "OutboundInContainer",
+                            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(palletCode, (int)MesUploadStatusEnum.鍑虹珯涓婁紶澶辫触);
+                        await _mesLogService.LogAsync(new MesApiLogDto
+                        {
+                            PalletCode = palletCode,
+                            ApiType = "OutboundInContainer",
+                            IsSuccess = false,
+                            ErrorMessage = ex.Message,
+                            ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
+                            Creator = App.User.UserName
+                        });
+                    }
                 });
 
-                // 8. 杩斿洖缁撴灉
-                if (mesResult.IsSuccess)
-                {
-                    return response.OK("鎵樼洏鍑虹珯鎴愬姛");
-                }
-                else
-                {
-                    return response.Error($"MES鎺ュ彛璋冪敤澶辫触: {mesResult.ErrorMessage}");
-                }
+                // 6. 绔嬪嵆杩斿洖鎴愬姛
+                return response.OK("鎵樼洏鍑虹珯鎴愬姛");
             }
             catch (System.Exception ex)
             {
-                stopwatch.Stop();
-
-                // 璁板綍閿欒鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
-                {
-                    ApiType = "OutboundInContainer",
-                    IsSuccess = false,
-                    ErrorMessage = ex.Message,
-                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
-                    Creator = App.User.UserName
-                });
-
                 return response.Error($"鎵樼洏鍑虹珯澶辫触: {ex.Message}");
             }
         }

--
Gitblit v1.9.3