From 75ef0dfecf2331c0828a1a182b1d3243d3041c51 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 21 四月 2026 13:19:44 +0800
Subject: [PATCH] refactor: 统一日志组件为Serilog并优化相关功能

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs |  288 +++++++++++++++++++++++----------------------------------
 1 files changed, 115 insertions(+), 173 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 fde583b..de85b5f 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
@@ -1,14 +1,13 @@
-锘縰sing Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Mvc;
+锘縰sing Microsoft.AspNetCore.Mvc;
+using WIDESEA_Common.Constants;
+using WIDESEA_Common.StockEnum;
 using WIDESEA_Core;
 using WIDESEA_Core.BaseController;
 using WIDESEA_DTO.MES;
-using WIDESEA_IStockService;
 using WIDESEA_IBasicService;
+using WIDESEA_IStockService;
 using WIDESEA_ISystemService;
 using WIDESEA_Model.Models;
-using WIDESEA_Common.StockEnum;
-using System.Diagnostics;
 
 namespace WIDESEA_WMSServer.Controllers.Stock
 {
@@ -19,22 +18,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) : base(service)
+            IStockInfoService stockInfoService,
+            IMESDeviceConfigService mesDeviceConfigService,
+            IMesUploadHelper mesUploadHelper) : base(service)
         {
-            _mesLogService = mesLogService;
             _mesService = mesService;
             _sysDictionaryService = sysDictionaryService;
             _stockInfoService = stockInfoService;
+            _mesDeviceConfigService = mesDeviceConfigService;
+            _mesUploadHelper = mesUploadHelper;
         }
 
         /// <summary>
@@ -49,82 +51,63 @@
 
             try
             {
-                // 1. 鍙傛暟楠岃瘉
-                if (dto.SfcList == null || !dto.SfcList.Any())
-                {
-                    return response.Error("鐢佃姱鐮佸垪琛ㄤ笉鑳戒负绌�");
-                }
+                //// 1. 鍙傛暟楠岃瘉
+                //if (dto.SfcList == null || !dto.SfcList.Any())
+                //{
+                //    return response.Error("鐢佃姱鐮佸垪琛ㄤ笉鑳戒负绌�");
+                //}
 
                 // 2. 楠岃瘉鐢佃姱鐘舵�侊紙闈�'宸查攣瀹�'鐘舵�佸厑璁哥粦瀹氾級
-                var stockDetail = await Service.Repository.QueryFirstAsync(x => dto.SfcList.Contains(x.SerialNumber));
-                if (stockDetail != null && stockDetail.Status == 99)
-                {
-                    return response.Error("褰撳墠搴撳瓨鏄庣粏鍖呭惈宸查攣瀹氱姸鎬侊紝涓嶅厑璁告墽琛岀粦瀹氭搷浣�");
-                }
-                var stockInfo = await _stockInfoService.Repository.QueryFirstAsync(x => stockDetail.StockId == x.Id);
+                //var stockDetail = await Service.Repository.QueryFirstAsync(x => dto.SfcList.Contains(x.SerialNumber));
+                //if (stockDetail != null && stockDetail.Status == 99)
+                //{
+                //    return response.Error("褰撳墠搴撳瓨鏄庣粏鍖呭惈宸查攣瀹氱姸鎬侊紝涓嶅厑璁告墽琛岀粦瀹氭搷浣�");
+                //}
+                var stockInfo = await _stockInfoService.Repository.QueryDataNavFirstAsync(x => x.PalletCode == dto.PalletCode);
 
-                // 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
+                    ContainerSfcList = stockInfo.Details.Select(sfc => new ContainerSfcItem
                     {
-                        Sfc = sfc,
-                        Location = dto.Location ?? ""
+                        Sfc = sfc.SerialNumber,
+                        Location = sfc.InboundOrderRowNo.ToString() ?? ""
                     }).ToList(),
                     OperationType = dto.OperationType
                 };
 
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
-                // 4. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
-                _ = Task.Run(async () =>
-                {
-                    var localStopwatch = Stopwatch.StartNew();
-                    try
+                // 5. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
+                _mesUploadHelper.FireAndForget(
+                    stockInfo.PalletCode,
+                    MesUploadStatusEnum.缁勭洏涓婁紶鎴愬姛,
+                    "BindContainer",
+                    requestJson,
+                    () =>
                     {
-                        var result = _mesService.BindContainer(mesRequest);
-                        localStopwatch.Stop();
+                        var result = string.IsNullOrWhiteSpace(token)
+                            ? _mesService.BindContainer(mesRequest)
+                            : _mesService.BindContainer(mesRequest, token);
+                        return (
+                            result?.Data?.IsSuccess ?? false,
+                            System.Text.Json.JsonSerializer.Serialize(result),
+                            result?.Data?.Msg ?? result?.ErrorMessage ?? "鏈煡閿欒"
+                        );
+                    },
+                    App.User.UserName);
 
-                        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
-                        });
-                    }
-                });
-
-                // 5. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
+                // 6. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
                 return response.OK("鎵樼洏鐢佃姱缁戝畾鎴愬姛");
             }
             catch (Exception ex)
@@ -146,76 +129,57 @@
             try
             {
                 // 1. 鍙傛暟楠岃瘉
-                if (dto.SfcList == null || !dto.SfcList.Any())
-                {
-                    return response.Error("鐢佃姱鐮佸垪琛ㄤ笉鑳戒负绌�");
-                }
+                //if (dto.SfcList == null || !dto.SfcList.Any())
+                //{
+                //    return response.Error("鐢佃姱鐮佸垪琛ㄤ笉鑳戒负绌�");
+                //}
 
-                // 2. 楠岃瘉鐢佃姱鐘舵�侊紙闈�'宸查攣瀹�'鐘舵�佸厑璁歌В缁戯級
-                var stockDetail = await Service.Repository.QueryFirstAsync(x => dto.SfcList.Contains(x.SerialNumber));
-                if (stockDetail != null && stockDetail.Status == 99)
-                {
-                    return response.Error("褰撳墠搴撳瓨鏄庣粏鍖呭惈宸查攣瀹氱姸鎬侊紝涓嶅厑璁告墽琛岃В缁戞搷浣�");
-                }
-                var stockInfo = await _stockInfoService.Repository.QueryFirstAsync(x => stockDetail.StockId == x.Id);
+                //// 2. 楠岃瘉鐢佃姱鐘舵�侊紙闈�'宸查攣瀹�'鐘舵�佸厑璁歌В缁戯級
+                //var stockDetail = await Service.Repository.QueryFirstAsync(x => dto.SfcList.Contains(x.SerialNumber));
+                //if (stockDetail != null && stockDetail.Status == 99)
+                //{
+                //    return response.Error("褰撳墠搴撳瓨鏄庣粏鍖呭惈宸查攣瀹氱姸鎬侊紝涓嶅厑璁告墽琛岃В缁戞搷浣�");
+                //}
+                var stockInfo = await _stockInfoService.Repository.QueryDataNavFirstAsync(x => dto.PalletCode == x.PalletCode);
 
-                // 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
+                    SfcList = stockInfo.Details.Select(x => x.SerialNumber).ToList(),
                 };
 
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
-                // 4. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
-                _ = Task.Run(async () =>
-                {
-                    var localStopwatch = Stopwatch.StartNew();
-                    try
+                // 5. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
+                _mesUploadHelper.FireAndForget(
+                    stockInfo.PalletCode,
+                    MesUploadStatusEnum.鎷嗙洏涓婁紶鎴愬姛,
+                    "UnbindContainer",
+                    requestJson,
+                    () =>
                     {
-                        var result = _mesService.UnBindContainer(mesRequest);
-                        localStopwatch.Stop();
+                        var result = string.IsNullOrWhiteSpace(token)
+                            ? _mesService.UnBindContainer(mesRequest)
+                            : _mesService.UnBindContainer(mesRequest, token);
+                        return (
+                            result?.Data?.IsSuccess ?? false,
+                            System.Text.Json.JsonSerializer.Serialize(result),
+                            result?.Data?.Msg ?? result?.ErrorMessage ?? "鏈煡閿欒"
+                        );
+                    },
+                    App.User.UserName);
 
-                        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
-                        });
-                    }
-                });
-
-                // 5. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
+                // 6. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
                 return response.OK("鎵樼洏鐢佃姱瑙g粦鎴愬姛");
             }
             catch (Exception ex)
@@ -256,11 +220,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
@@ -274,51 +243,24 @@
 
                 string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
-                // 4. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
-                _ = Task.Run(async () =>
-                {
-                    var localStopwatch = Stopwatch.StartNew();
-                    try
+                // 5. 寮傛璋冪敤MES鎺ュ彛锛坒ire-and-forget锛�
+                _mesUploadHelper.FireAndForget(
+                    stockInfo.PalletCode,
+                    MesUploadStatusEnum.NG涓婃姤鎴愬姛,
+                    "ContainerNgReport",
+                    requestJson,
+                    () =>
                     {
                         var result = _mesService.ContainerNgReport(mesRequest);
-                        localStopwatch.Stop();
+                        return (
+                            result?.IsSuccess ?? false,
+                            System.Text.Json.JsonSerializer.Serialize(result),
+                            result?.ErrorMessage ?? "鏈煡閿欒"
+                        );
+                    },
+                    App.User.UserName);
 
-                        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
-                        });
-                    }
-                });
-
-                // 5. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
+                // 6. 绔嬪嵆杩斿洖鎴愬姛鍝嶅簲
                 return response.OK("NG鐢佃姱涓婃姤鎴愬姛");
             }
             catch (Exception ex)
@@ -372,4 +314,4 @@
             return defaultValue;
         }
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3