wanshenmean
13 小时以前 343d512ed92c75f406c485fa7a247261a13aefb0
Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs
@@ -7,7 +7,6 @@
using WIDESEA_DTO.MES;
using WIDESEA_IStockService;
using WIDESEA_IBasicService;
using WIDESEA_IMesService;
using WIDESEA_ISystemService;
using WIDESEA_Model.Models;
using WIDESEA_Common.StockEnum;
@@ -25,16 +24,19 @@
        private readonly IMesLogService _mesLogService;
        private readonly IMesService _mesService;
        private readonly ISys_DictionaryService _sysDictionaryService;
        private readonly IStockInfoService _stockInfoService;
        public StockInfoController(
            IStockInfoService service,
            IMesLogService mesLogService,
            IMesService mesService,
            ISys_DictionaryService sysDictionaryService) : base(service)
            ISys_DictionaryService sysDictionaryService,
            IStockInfoService stockInfoService) : base(service)
        {
            _mesLogService = mesLogService;
            _mesService = mesService;
            _sysDictionaryService = sysDictionaryService;
            _stockInfoService = stockInfoService;
        }
        /// <summary>
@@ -58,7 +60,6 @@
        public async Task<WebResponseContent> InboundInContainer([FromBody] InboundInContainerRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            try
            {
@@ -82,63 +83,67 @@
                    return response.Error($"当前库存状态不允许进站操作,当前状态:{stockInfo.StockStatus}");
                }
                // 4. 获取系统配置 - 直接从数据库查询
                var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
                string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
                string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
                // 5. 构造MES请求
                // 4. 构造MES请求
                var mesRequest = new InboundInContainerRequest
                {
                    EquipmentCode = equipmentCode,
                    ResourceCode = resourceCode,
                    EquipmentCode = "STK-GROUP-001",
                    ResourceCode = "STK-GROUP-001",
                    LocalTime = DateTime.Now,
                    ContainerCode = dto.PalletCode
                };
                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调用(fire-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 = _mesService.InboundInContainer(mesRequest);
                        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 +157,6 @@
        public async Task<WebResponseContent> OutboundInContainer([FromBody] OutboundInContainerRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            try
            {
@@ -182,17 +186,11 @@
                    return response.Error($"当前库存状态不允许出站操作,当前状态:{stockInfo.StockStatus}");
                }
                // 4. 获取系统配置
                var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
                string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
                string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
                // 5. 构造MES请求
                // 4. 构造MES请求
                var mesRequest = new OutboundInContainerRequest
                {
                    EquipmentCode = equipmentCode,
                    ResourceCode = resourceCode,
                    EquipmentCode = "STK-GROUP-001",
                    ResourceCode = "STK-GROUP-001",
                    LocalTime = DateTime.Now,
                    ContainerCode = dto.PalletCode,
                    ParamList = dto.ParamList?.Select(p => new ParamItem
@@ -204,47 +202,57 @@
                };
                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调用(fire-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 = _mesService.OutboundInContainer(mesRequest);
                        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}");
            }
        }