wanshenmean
2 天以前 d0dfd9606e3ccc6f5cd4647270328b52b2e5a384
Code/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/Stock/StockInfoDetailController.cs
@@ -1,15 +1,14 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WIDESEA_Core;
using WIDESEA_Core.BaseController;
using WIDESEA_DTO.MES;
using WIDESEA_IStockService;
using WIDESEA_IBasicService;
using WIDESEA_ISystemService;
using WIDESEA_Model.Models;
using Microsoft.AspNetCore.Mvc;
using WIDESEA_Common.Constants;
using WIDESEA_Common.StockEnum;
using System.Diagnostics;
using WIDESEA_Core;
using WIDESEA_Core.BaseController;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.MES;
using WIDESEA_IBasicService;
using WIDESEA_IStockService;
using WIDESEA_ISystemService;
using WIDESEA_Model.Models;
namespace WIDESEA_WMSServer.Controllers.Stock
{
@@ -20,25 +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,
            IMESDeviceConfigService mesDeviceConfigService) : base(service)
            IMESDeviceConfigService mesDeviceConfigService,
            IMesUploadHelper mesUploadHelper) : base(service)
        {
            _mesLogService = mesLogService;
            _mesService = mesService;
            _sysDictionaryService = sysDictionaryService;
            _stockInfoService = stockInfoService;
            _mesDeviceConfigService = mesDeviceConfigService;
            _mesUploadHelper = mesUploadHelper;
        }
        /// <summary>
@@ -53,22 +52,22 @@
            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凭证
                var mesConfig = _mesDeviceConfigService.GetByDeviceName("组盘机械手");
                var mesConfig = _mesDeviceConfigService.GetByDeviceName(dto.DeviceName);
                string equipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE;
                string resourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE;
                string token = mesConfig?.Token;
@@ -80,10 +79,10 @@
                    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
                };
@@ -91,51 +90,24 @@
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
                // 5. 异步调用MES接口(fire-and-forget)
                var localToken = token;
                _ = Task.Run(async () =>
                {
                    var localStopwatch = Stopwatch.StartNew();
                    try
                _mesUploadHelper.FireAndForget(
                    stockInfo.PalletCode,
                    MesUploadStatusEnum.组盘上传成功,
                    "BindContainer",
                    requestJson,
                    () =>
                    {
                        var result = string.IsNullOrWhiteSpace(localToken)
                        var result = string.IsNullOrWhiteSpace(token)
                            ? _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
                        });
                    }
                });
                            : _mesService.BindContainer(mesRequest, token);
                        return (
                            result?.Data?.IsSuccess ?? false,
                            System.Text.Json.JsonSerializer.Serialize(result),
                            result?.Data?.Msg ?? result?.ErrorMessage ?? "未知错误",
                            _mesService.BuildConfig(token ?? string.Empty).ToJson()
                        );
                    },
                    App.User.UserName);
                // 6. 立即返回成功响应
                return response.OK("托盘电芯绑定成功");
@@ -159,21 +131,21 @@
            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凭证
                var mesConfig = _mesDeviceConfigService.GetByDeviceName("组盘机械手");
                var mesConfig = _mesDeviceConfigService.GetByDeviceName(dto.DeviceName);
                string equipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE;
                string resourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE;
                string token = mesConfig?.Token;
@@ -185,57 +157,30 @@
                    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);
                // 5. 异步调用MES接口(fire-and-forget)
                var localToken = token;
                _ = Task.Run(async () =>
                {
                    var localStopwatch = Stopwatch.StartNew();
                    try
                _mesUploadHelper.FireAndForget(
                    stockInfo.PalletCode,
                    MesUploadStatusEnum.拆盘上传成功,
                    "UnbindContainer",
                    requestJson,
                    () =>
                    {
                        var result = string.IsNullOrWhiteSpace(localToken)
                        var result = string.IsNullOrWhiteSpace(token)
                            ? _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
                        });
                    }
                });
                            : _mesService.UnBindContainer(mesRequest, token);
                        return (
                            result?.Data?.IsSuccess ?? false,
                            System.Text.Json.JsonSerializer.Serialize(result),
                            result?.Data?.Msg ?? result?.ErrorMessage ?? "未知错误",
                            _mesService.BuildConfig(token ?? string.Empty).ToJson()
                        );
                    },
                    App.User.UserName);
                // 6. 立即返回成功响应
                return response.OK("托盘电芯解绑成功");
@@ -302,48 +247,22 @@
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
                // 5. 异步调用MES接口(fire-and-forget)
                _ = Task.Run(async () =>
                {
                    var localStopwatch = Stopwatch.StartNew();
                    try
                _mesUploadHelper.FireAndForget(
                    stockInfo.PalletCode,
                    MesUploadStatusEnum.NG上报成功,
                    "ContainerNgReport",
                    requestJson,
                    () =>
                    {
                        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
                        });
                    }
                });
                        return (
                            result?.Data?.IsSuccess ?? false,
                            System.Text.Json.JsonSerializer.Serialize(result),
                            result?.Data?.Msg ?? result?.ErrorMessage ?? "未知错误",
                            _mesService.BuildConfig(mesConfig?.Token ?? string.Empty).ToJson()
                        );
                    },
                    App.User.UserName);
                // 6. 立即返回成功响应
                return response.OK("NG电芯上报成功");
@@ -399,4 +318,4 @@
            return defaultValue;
        }
    }
}
}