wanshenmean
7 小时以前 96adc295cb04fd135d63d3a907f2732274f90965
Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoController.cs
@@ -7,11 +7,10 @@
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;
namespace WIDESEA_WMSServer.Controllers.Stock
{
@@ -22,19 +21,22 @@
    [ApiController]
    public class StockInfoController : ApiBaseController<IStockInfoService, Dt_StockInfo>
    {
        private readonly IMesLogService _mesLogService;
        private readonly IMesService _mesService;
        private readonly IMESDeviceConfigService _mesDeviceConfigService;
        private readonly ISys_DictionaryService _sysDictionaryService;
        private readonly IMesUploadHelper _mesUploadHelper;
        public StockInfoController(
            IStockInfoService service,
            IMesLogService mesLogService,
            IMesService mesService,
            ISys_DictionaryService sysDictionaryService) : base(service)
            IMESDeviceConfigService mesDeviceConfigService,
            ISys_DictionaryService sysDictionaryService,
            IMesUploadHelper mesUploadHelper) : base(service)
        {
            _mesLogService = mesLogService;
            _mesService = mesService;
            _mesDeviceConfigService = mesDeviceConfigService;
            _sysDictionaryService = sysDictionaryService;
            _mesUploadHelper = mesUploadHelper;
        }
        /// <summary>
@@ -58,7 +60,6 @@
        public async Task<WebResponseContent> InboundInContainer([FromBody] InboundInContainerRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            try
            {
@@ -82,57 +83,48 @@
                    return response.Error($"当前库存状态不允许进站操作,当前状态:{stockInfo.StockStatus}");
                }
                // 4. 构造MES请求
                // 4. 动态获取MES凭证
                string deviceName = stockInfo.Remark == "GW_1" ? "高温静置1"
                    : stockInfo.Remark == "GW_2" ? "高温静置2"
                    : "常温静置1";
                var mesConfig = _mesDeviceConfigService.GetByDeviceName(deviceName);
                var mesRequest = new InboundInContainerRequest
                {
                    EquipmentCode = "STK-GROUP-001",
                    ResourceCode = "STK-GROUP-001",
                    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;
                // 5. 调用MES接口(同步方法)
                var mesResult = _mesService.InboundInContainer(mesRequest);
                stopwatch.Stop();
                // 5. 异步执行MES调用(fire-and-forget)
                _mesUploadHelper.FireAndForget(
                    palletCode,
                    MesUploadStatusEnum.进站上传成功,
                    "InboundInContainer",
                    requestJson,
                    () =>
                    {
                        var result = string.IsNullOrWhiteSpace(token)
                            ? _mesService.InboundInContainer(mesRequest)
                            : _mesService.InboundInContainer(mesRequest, token);
                        return (
                            result?.IsSuccess ?? false,
                            System.Text.Json.JsonSerializer.Serialize(result),
                            result?.ErrorMessage ?? "未知错误"
                        );
                    },
                    App.User.UserName);
                // 6. 记录日志
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                    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
                });
                // 7. 返回结果
                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}");
            }
        }
@@ -146,7 +138,6 @@
        public async Task<WebResponseContent> OutboundInContainer([FromBody] OutboundInContainerRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            try
            {
@@ -176,11 +167,16 @@
                    return response.Error($"当前库存状态不允许出站操作,当前状态:{stockInfo.StockStatus}");
                }
                // 4. 构造MES请求
                // 4. 动态获取MES凭证
                string deviceName = stockInfo.Remark == "GW_1" ? "高温静置1"
                    : stockInfo.Remark == "GW_2" ? "高温静置2"
                    : "常温静置1";
                var mesConfig = _mesDeviceConfigService.GetByDeviceName(deviceName);
                var mesRequest = new OutboundInContainerRequest
                {
                    EquipmentCode = "STK-GROUP-001",
                    ResourceCode = "STK-GROUP-001",
                    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
@@ -191,48 +187,34 @@
                    }).ToList()
                };
                string token = mesConfig?.Token;
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
                string palletCode = stockInfo.PalletCode;
                // 5. 调用MES接口(同步方法)
                var mesResult = _mesService.OutboundInContainer(mesRequest);
                stopwatch.Stop();
                // 5. 异步执行MES调用(fire-and-forget)
                _mesUploadHelper.FireAndForget(
                    palletCode,
                    MesUploadStatusEnum.出站上传成功,
                    "OutboundInContainer",
                    requestJson,
                    () =>
                    {
                        var result = string.IsNullOrWhiteSpace(token)
                            ? _mesService.OutboundInContainer(mesRequest)
                            : _mesService.OutboundInContainer(mesRequest, token);
                        return (
                            result?.IsSuccess ?? false,
                            System.Text.Json.JsonSerializer.Serialize(result),
                            result?.ErrorMessage ?? "未知错误"
                        );
                    },
                    App.User.UserName);
                // 6. 记录日志
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                    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
                });
                // 7. 返回结果
                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}");
            }
        }