wanshenmean
9 小时以前 96adc295cb04fd135d63d3a907f2732274f90965
Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
@@ -7,8 +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
{
@@ -19,22 +19,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>
@@ -63,11 +66,17 @@
                }
                var stockInfo = await _stockInfoService.Repository.QueryFirstAsync(x => stockDetail.StockId == x.Id);
                // 3. 构造MES请求 - 将电芯列表转换为ContainerSfcItem格式
                // 3. 动态获取MES凭证
                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. 构造MES请求 - 将电芯列表转换为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,51 +89,26 @@
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
                // 4. 异步调用MES接口(fire-and-forget)
                _ = Task.Run(async () =>
                {
                    var localStopwatch = Stopwatch.StartNew();
                    try
                // 5. 异步调用MES接口(fire-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?.IsSuccess ?? false,
                            System.Text.Json.JsonSerializer.Serialize(result),
                            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)
@@ -159,11 +143,17 @@
                }
                var stockInfo = await _stockInfoService.Repository.QueryFirstAsync(x => stockDetail.StockId == x.Id);
                // 3. 构造MES请求
                // 3. 动态获取MES凭证
                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. 构造MES请求
                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
@@ -171,51 +161,26 @@
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
                // 4. 异步调用MES接口(fire-and-forget)
                _ = Task.Run(async () =>
                {
                    var localStopwatch = Stopwatch.StartNew();
                    try
                // 5. 异步调用MES接口(fire-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?.IsSuccess ?? false,
                            System.Text.Json.JsonSerializer.Serialize(result),
                            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("托盘电芯解绑成功");
            }
            catch (Exception ex)
@@ -256,11 +221,16 @@
                }
                var stockInfo = await _stockInfoService.Repository.QueryFirstAsync(x => stockDetail.StockId == x.Id);
                // 3. 构造MES请求 - 将DTO格式转换为MES请求格式
                // 3. 动态获取MES凭证
                var mesConfig = _mesDeviceConfigService.GetByDeviceName("组盘机械手");
                string equipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE;
                string resourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE;
                // 4. 构造MES请求 - 将DTO格式转换为MES请求格式
                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 +244,24 @@
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
                // 4. 异步调用MES接口(fire-and-forget)
                _ = Task.Run(async () =>
                {
                    var localStopwatch = Stopwatch.StartNew();
                    try
                // 5. 异步调用MES接口(fire-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)