wanshenmean
11 小时以前 75ef0dfecf2331c0828a1a182b1d3243d3041c51
Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
@@ -1,13 +1,13 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using 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 System.Diagnostics;
namespace WIDESEA_WMSServer.Controllers.Stock
{
@@ -18,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>
@@ -45,81 +48,70 @@
        public async Task<WebResponseContent> BindContainer([FromBody] BindContainerRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            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. 构造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
                    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接口(同步方法)
                var mesResult = _mesService.BindContainer(mesRequest);
                stopwatch.Stop();
                // 5. 异步调用MES接口(fire-and-forget)
                _mesUploadHelper.FireAndForget(
                    stockInfo.PalletCode,
                    MesUploadStatusEnum.组盘上传成功,
                    "BindContainer",
                    requestJson,
                    () =>
                    {
                        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);
                // 5. 记录日志
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                    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
                });
                // 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}");
            }
        }
@@ -133,76 +125,65 @@
        public async Task<WebResponseContent> UnbindContainer([FromBody] UnbindContainerRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            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. 构造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
                    SfcList = stockInfo.Details.Select(x => x.SerialNumber).ToList(),
                };
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
                // 4. 调用MES接口(同步方法)
                var mesResult = _mesService.UnBindContainer(mesRequest);
                stopwatch.Stop();
                // 5. 异步调用MES接口(fire-and-forget)
                _mesUploadHelper.FireAndForget(
                    stockInfo.PalletCode,
                    MesUploadStatusEnum.拆盘上传成功,
                    "UnbindContainer",
                    requestJson,
                    () =>
                    {
                        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);
                // 5. 记录日志
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                    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
                });
                // 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}");
            }
        }
@@ -216,7 +197,6 @@
        public async Task<WebResponseContent> ContainerNgReport([FromBody] ContainerNgReportRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            try
            {
@@ -240,11 +220,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
@@ -258,46 +243,28 @@
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
                // 4. 调用MES接口(同步方法)
                var mesResult = _mesService.ContainerNgReport(mesRequest);
                stopwatch.Stop();
                // 5. 异步调用MES接口(fire-and-forget)
                _mesUploadHelper.FireAndForget(
                    stockInfo.PalletCode,
                    MesUploadStatusEnum.NG上报成功,
                    "ContainerNgReport",
                    requestJson,
                    () =>
                    {
                        var result = _mesService.ContainerNgReport(mesRequest);
                        return (
                            result?.IsSuccess ?? false,
                            System.Text.Json.JsonSerializer.Serialize(result),
                            result?.ErrorMessage ?? "未知错误"
                        );
                    },
                    App.User.UserName);
                // 5. 记录日志
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                    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
                });
                // 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}");
            }
        }
@@ -347,4 +314,4 @@
            return defaultValue;
        }
    }
}
}