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/StockInfoDetailController.cs |  288 ++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 170 insertions(+), 118 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..3680ce8 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,8 @@
 using WIDESEA_IBasicService;
 using WIDESEA_ISystemService;
 using WIDESEA_Model.Models;
+using WIDESEA_Common.Constants;
+using WIDESEA_Common.StockEnum;
 using System.Diagnostics;
 
 namespace WIDESEA_WMSServer.Controllers.Stock
@@ -22,18 +24,21 @@
         private readonly IMesService _mesService;
         private readonly ISys_DictionaryService _sysDictionaryService;
         private readonly IStockInfoService _stockInfoService;
+        private readonly IMESDeviceConfigService _mesDeviceConfigService;
 
         public StockInfoDetailController(
             IStockInfoDetailService service,
             IMesLogService mesLogService,
             IMesService mesService,
             ISys_DictionaryService sysDictionaryService,
-            IStockInfoService stockInfoService) : base(service)
+            IStockInfoService stockInfoService,
+            IMESDeviceConfigService mesDeviceConfigService) : base(service)
         {
             _mesLogService = mesLogService;
             _mesService = mesService;
             _sysDictionaryService = sysDictionaryService;
             _stockInfoService = stockInfoService;
+            _mesDeviceConfigService = mesDeviceConfigService;
         }
 
         /// <summary>
@@ -45,7 +50,6 @@
         public async Task<WebResponseContent> BindContainer([FromBody] BindContainerRequestDto dto)
         {
             var response = new WebResponseContent();
-            var stopwatch = Stopwatch.StartNew();
 
             try
             {
@@ -63,11 +67,17 @@
                 }
                 var stockInfo = await _stockInfoService.Repository.QueryFirstAsync(x => stockDetail.StockId == x.Id);
 
-                // 3. 鏋勯�燤ES璇锋眰 - 灏嗙數鑺垪琛ㄨ浆鎹负ContainerSfcItem鏍煎紡
+                // 3. 鍔ㄦ�佽幏鍙朚ES鍑瘉
+                var mesConfig = _mesDeviceConfigService.GetByDeviceName("缁勭洏鏈烘鎵�");
+                string equipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE;
+                string resourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE;
+                string token = mesConfig?.Token;
+
+                // 4. 鏋勯�燤ES璇锋眰 - 灏嗙數鑺垪琛ㄨ浆鎹负ContainerSfcItem鏍煎紡
                 var mesRequest = new BindContainerRequest
                 {
-                    EquipmentCode = "STK-GROUP-001",
-                    ResourceCode = "STK-GROUP-001",
+                    EquipmentCode = equipmentCode,
+                    ResourceCode = resourceCode,
                     LocalTime = DateTime.Now,
                     ContainerCode = stockInfo.PalletCode,
                     ContainerSfcList = dto.SfcList.Select(sfc => new ContainerSfcItem
@@ -80,46 +90,58 @@
 
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
-                // 4. 璋冪敤MES鎺ュ彛锛堝悓姝ユ柟娉曪級
-                var mesResult = _mesService.BindContainer(mesRequest);
-                stopwatch.Stop();
-
-                // 5. 璁板綍鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
+                // 5. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
+                var localToken = token;
+                _ = 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 = string.IsNullOrWhiteSpace(localToken)
+                            ? _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
+                        });
+                    }
                 });
 
-                // 6. 杩斿洖缁撴灉
-                if (mesResult.IsSuccess)
-                {
-                    return response.OK("鎵樼洏鐢佃姱缁戝畾鎴愬姛");
-                }
-                else
-                {
-                    return response.Error($"MES鎺ュ彛璋冪敤澶辫触: {mesResult.ErrorMessage}");
-                }
+                // 6. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
+                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 +155,6 @@
         public async Task<WebResponseContent> UnbindContainer([FromBody] UnbindContainerRequestDto dto)
         {
             var response = new WebResponseContent();
-            var stopwatch = Stopwatch.StartNew();
 
             try
             {
@@ -151,11 +172,17 @@
                 }
                 var stockInfo = await _stockInfoService.Repository.QueryFirstAsync(x => stockDetail.StockId == x.Id);
 
-                // 3. 鏋勯�燤ES璇锋眰
+                // 3. 鍔ㄦ�佽幏鍙朚ES鍑瘉
+                var mesConfig = _mesDeviceConfigService.GetByDeviceName("缁勭洏鏈烘鎵�");
+                string equipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE;
+                string resourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE;
+                string token = mesConfig?.Token;
+
+                // 4. 鏋勯�燤ES璇锋眰
                 var mesRequest = new UnBindContainerRequest
                 {
-                    EquipmentCode = "STK-GROUP-001",
-                    ResourceCode = "STK-GROUP-001",
+                    EquipmentCode = equipmentCode,
+                    ResourceCode = resourceCode,
                     LocalTime = DateTime.Now,
                     ContainCode = stockInfo.PalletCode,
                     SfcList = dto.SfcList
@@ -163,46 +190,58 @@
 
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
-                // 4. 璋冪敤MES鎺ュ彛锛堝悓姝ユ柟娉曪級
-                var mesResult = _mesService.UnBindContainer(mesRequest);
-                stopwatch.Stop();
-
-                // 5. 璁板綍鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
+                // 5. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
+                var localToken = token;
+                _ = 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 = string.IsNullOrWhiteSpace(localToken)
+                            ? _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
+                        });
+                    }
                 });
 
-                // 6. 杩斿洖缁撴灉
-                if (mesResult.IsSuccess)
-                {
-                    return response.OK("鎵樼洏鐢佃姱瑙g粦鎴愬姛");
-                }
-                else
-                {
-                    return response.Error($"MES鎺ュ彛璋冪敤澶辫触: {mesResult.ErrorMessage}");
-                }
+                // 6. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
+                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 +255,6 @@
         public async Task<WebResponseContent> ContainerNgReport([FromBody] ContainerNgReportRequestDto dto)
         {
             var response = new WebResponseContent();
-            var stopwatch = Stopwatch.StartNew();
 
             try
             {
@@ -240,11 +278,16 @@
                 }
                 var stockInfo = await _stockInfoService.Repository.QueryFirstAsync(x => stockDetail.StockId == x.Id);
 
-                // 3. 鏋勯�燤ES璇锋眰 - 灏咲TO鏍煎紡杞崲涓篗ES璇锋眰鏍煎紡
+                // 3. 鍔ㄦ�佽幏鍙朚ES鍑瘉
+                var mesConfig = _mesDeviceConfigService.GetByDeviceName("缁勭洏鏈烘鎵�");
+                string equipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE;
+                string resourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE;
+
+                // 4. 鏋勯�燤ES璇锋眰 - 灏咲TO鏍煎紡杞崲涓篗ES璇锋眰鏍煎紡
                 var mesRequest = new ContainerNgReportRequest
                 {
-                    EquipmentCode = "STK-GROUP-001",
-                    ResourceCode = "RESOURCE-001",
+                    EquipmentCode = equipmentCode,
+                    ResourceCode = resourceCode,
                     LocalTime = DateTime.Now,
                     ContainerCode = stockInfo.PalletCode,
                     NgSfcList = dto.NgSfcList.Select(ng => new NgSfcItem
@@ -258,46 +301,55 @@
 
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
-                // 4. 璋冪敤MES鎺ュ彛锛堝悓姝ユ柟娉曪級
-                var mesResult = _mesService.ContainerNgReport(mesRequest);
-                stopwatch.Stop();
-
-                // 5. 璁板綍鏃ュ織
-                await _mesLogService.LogAsync(new MesApiLogDto
+                // 5. 寮傛璋冪敤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}");
-                }
+                // 6. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
+                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