wanshenmean
11 小时以前 c36c5c66732415fb5a6d8f68082637bf27cb5538
Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
@@ -5,9 +5,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,16 +23,22 @@
        private readonly IMesLogService _mesLogService;
        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) : base(service)
            ISys_DictionaryService sysDictionaryService,
            IStockInfoService stockInfoService,
            IMESDeviceConfigService mesDeviceConfigService) : base(service)
        {
            _mesLogService = mesLogService;
            _mesService = mesService;
            _sysDictionaryService = sysDictionaryService;
            _stockInfoService = stockInfoService;
            _mesDeviceConfigService = mesDeviceConfigService;
        }
        /// <summary>
@@ -43,41 +50,36 @@
        public async Task<WebResponseContent> BindContainer([FromBody] BindContainerRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            try
            {
                // 1. 参数验证
                if (string.IsNullOrWhiteSpace(dto.PalletCode))
                {
                    return response.Error("托盘编号不能为空");
                }
                if (dto.SfcList == null || !dto.SfcList.Any())
                {
                    return response.Error("电芯码列表不能为空");
                }
                // 2. 验证电芯状态(非'已锁定'状态允许绑定)
                var stockDetails = await Service.Repository.QueryDataAsync(x => dto.SfcList.Contains(x.SerialNumber));
                if (stockDetails != null && stockDetails.Any(d => d.Status == 99))
                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);
                // 3. 获取系统配置
                var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
                // 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;
                string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
                string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
                // 3. 构造MES请求 - 将电芯列表转换为ContainerSfcItem格式
                // 4. 构造MES请求 - 将电芯列表转换为ContainerSfcItem格式
                var mesRequest = new BindContainerRequest
                {
                    EquipmentCode = equipmentCode,
                    ResourceCode = resourceCode,
                    LocalTime = DateTime.Now,
                    ContainerCode = dto.PalletCode,
                    ContainerCode = stockInfo.PalletCode,
                    ContainerSfcList = dto.SfcList.Select(sfc => new ContainerSfcItem
                    {
                        Sfc = sfc,
@@ -88,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接口(fire-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}");
            }
        }
@@ -141,86 +155,93 @@
        public async Task<WebResponseContent> UnbindContainer([FromBody] UnbindContainerRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            try
            {
                // 1. 参数验证
                if (string.IsNullOrWhiteSpace(dto.PalletCode))
                {
                    return response.Error("托盘编号不能为空");
                }
                if (dto.SfcList == null || !dto.SfcList.Any())
                {
                    return response.Error("电芯码列表不能为空");
                }
                // 2. 验证电芯状态(非'已锁定'状态允许解绑)
                var stockDetails = await Service.Repository.QueryDataAsync(x => dto.SfcList.Contains(x.SerialNumber));
                if (stockDetails != null && stockDetails.Any(d => d.Status == 99))
                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);
                // 3. 获取系统配置
                var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
                // 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;
                string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
                string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
                // 3. 构造MES请求
                // 4. 构造MES请求
                var mesRequest = new UnBindContainerRequest
                {
                    EquipmentCode = equipmentCode,
                    ResourceCode = resourceCode,
                    LocalTime = DateTime.Now,
                    ContainCode = dto.PalletCode,
                    ContainCode = stockInfo.PalletCode,
                    SfcList = dto.SfcList
                };
                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接口(fire-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("托盘电芯解绑成功");
                }
                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 = "UnbindContainer",
                    IsSuccess = false,
                    ErrorMessage = ex.Message,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
                return response.Error($"托盘电芯解绑失败: {ex.Message}");
            }
        }
@@ -234,7 +255,6 @@
        public async Task<WebResponseContent> ContainerNgReport([FromBody] ContainerNgReportRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            try
            {
@@ -251,25 +271,25 @@
                // 2. 验证电芯状态(非'已锁定'状态允许NG上报)
                var sfcList = dto.NgSfcList.Select(x => x.Sfc).ToList();
                var stockDetails = await Service.Repository.QueryDataAsync(x => sfcList.Contains(x.SerialNumber));
                if (stockDetails != null && stockDetails.Any(d => d.Status == 99))
                var stockDetail = await Service.Repository.QueryFirstAsync(x => sfcList.Contains(x.SerialNumber));
                if (stockDetail != null && stockDetail.Status == 99)
                {
                    return response.Error("当前库存明细包含已锁定状态,不允许执行NG上报操作");
                }
                var stockInfo = await _stockInfoService.Repository.QueryFirstAsync(x => stockDetail.StockId == x.Id);
                // 3. 获取系统配置
                var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
                // 3. 动态获取MES凭证
                var mesConfig = _mesDeviceConfigService.GetByDeviceName("组盘机械手");
                string equipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE;
                string resourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE;
                string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
                string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
                // 3. 构造MES请求 - 将DTO格式转换为MES请求格式
                // 4. 构造MES请求 - 将DTO格式转换为MES请求格式
                var mesRequest = new ContainerNgReportRequest
                {
                    EquipmentCode = equipmentCode,
                    ResourceCode = resourceCode,
                    LocalTime = DateTime.Now,
                    ContainerCode = dto.PalletCode,
                    ContainerCode = stockInfo.PalletCode,
                    NgSfcList = dto.NgSfcList.Select(ng => new NgSfcItem
                    {
                        Sfc = ng.Sfc,
@@ -281,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接口(fire-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}");
            }
        }