wanshenmean
23 小时以前 63ca4ac71443473a0ff72758e1ae3739b7640a68
Code/WMS/WIDESEA_WMSServer/WIDESEA_StockService/StockSerivce.cs
@@ -1,8 +1,10 @@
using Newtonsoft.Json;
using SqlSugar;
using System.Diagnostics;
using WIDESEA_Common.Constants;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.MES;
using WIDESEA_DTO.Stock;
using WIDESEA_IBasicService;
@@ -51,6 +53,8 @@
        /// </summary>
        public IMesService _mesService { get; }
        private readonly IMesLogService _mesLogService;
        /// <summary>
        /// 构造函数
        /// </summary>
@@ -65,7 +69,8 @@
            IStockInfo_HtyService stockInfo_HtyService,
            IMesService mesService,
            IWarehouseService warehouseService,
            ISqlSugarClient sqlSugarClient)
            ISqlSugarClient sqlSugarClient,
            IMesLogService mesLogService)
        {
            StockInfoDetailService = stockInfoDetailService;
            StockInfoService = stockInfoService;
@@ -74,6 +79,7 @@
            _mesService = mesService;
            _warehouseService = warehouseService;
            SqlSugarClient = sqlSugarClient;
            _mesLogService = mesLogService;
        }
        /// <summary>
@@ -429,7 +435,7 @@
                    }
                }
                // 3. 调用MES解绑
                // 3. Fire-and-forget异步调用MES解绑
                var unbindRequest = new UnBindContainerRequest
                {
                    EquipmentCode = equipmentCode,
@@ -438,13 +444,43 @@
                    ContainCode = palletCode,
                    SfcList = sfcList
                };
                var unbindResult = string.IsNullOrWhiteSpace(token)
                    ? _mesService.UnBindContainer(unbindRequest)
                    : _mesService.UnBindContainer(unbindRequest, token);
                if (unbindResult == null || unbindResult.Data == null || !unbindResult.Data.IsSuccess)
                _ = Task.Run(() =>
                {
                    return content.Error($"MES解绑失败: {unbindResult?.Data?.Msg ?? unbindResult?.ErrorMessage ?? "未知错误"}");
                }
                    var stopwatch = Stopwatch.StartNew();
                    try
                    {
                        var unbindResult = string.IsNullOrWhiteSpace(token)
                            ? _mesService.UnBindContainer(unbindRequest)
                            : _mesService.UnBindContainer(unbindRequest, token);
                        stopwatch.Stop();
                        bool isSuccess = unbindResult?.Data?.IsSuccess ?? false;
                        int status = isSuccess
                            ? (int)MesUploadStatusEnum.拆盘上传成功
                            : (int)MesUploadStatusEnum.拆盘上传失败;
                        // 更新MES上传状态
                        StockInfoService.UpdateMesUploadStatusAsync(palletCode, status).ConfigureAwait(false);
                        // 记录MES日志
                        _mesLogService.LogAsync(new MesApiLogDto
                        {
                            PalletCode = palletCode,
                            ApiType = "UnBindContainer",
                            RequestJson = unbindRequest.ToJson(),
                            ResponseJson = System.Text.Json.JsonSerializer.Serialize(unbindResult),
                            IsSuccess = isSuccess,
                            ErrorMessage = unbindResult?.Data?.Msg ?? unbindResult?.ErrorMessage ?? "未知错误",
                            ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                            Creator = "System"
                        }).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        // 调用失败
                        StockInfoService.UpdateMesUploadStatusAsync(palletCode, (int)MesUploadStatusEnum.拆盘上传失败).ConfigureAwait(false);
                    }
                });
                // 4. 删除临时表记录
                await SqlSugarClient.Deleteable<Dt_SplitTemp>().Where(t => t.PalletCode == palletCode).ExecuteCommandAsync();
@@ -466,6 +502,7 @@
        public async Task<WebResponseContent> GroupPalletConfirmAsync(string palletCode, string deviceName)
        {
            WebResponseContent content = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
            try
            {
                if (string.IsNullOrWhiteSpace(palletCode))
@@ -510,13 +547,45 @@
                        Location = d.InboundOrderRowNo.ToString()
                    }).ToList()
                };
                var bindResult = string.IsNullOrWhiteSpace(token)
                    ? _mesService.BindContainer(bindRequest)
                    : _mesService.BindContainer(bindRequest, token);
                if (bindResult == null || bindResult.Data == null || !bindResult.Data.IsSuccess)
                string requestJson = bindRequest.ToJson();
                // 3. Fire-and-forget异步调用MES绑定
                _ = Task.Run(() =>
                {
                    return content.Error($"MES绑定失败: {bindResult?.Data?.Msg ?? bindResult?.ErrorMessage ?? "未知错误"}");
                }
                    var stopwatch = Stopwatch.StartNew();
                    try
                    {
                        var bindResult = string.IsNullOrWhiteSpace(token)
                            ? _mesService.BindContainer(bindRequest)
                            : _mesService.BindContainer(bindRequest, token);
                        stopwatch.Stop();
                        bool isSuccess = bindResult?.Data?.IsSuccess ?? false;
                        int status = isSuccess
                            ? (int)MesUploadStatusEnum.组盘上传成功
                            : (int)MesUploadStatusEnum.组盘上传失败;
                        // 更新MES上传状态
                        StockInfoService.UpdateMesUploadStatusAsync(palletCode, status).ConfigureAwait(false);
                        // 记录MES日志
                        _mesLogService.LogAsync(new MesApiLogDto
                        {
                            PalletCode = palletCode,
                            ApiType = "BindContainer",
                            RequestJson = requestJson,
                            ResponseJson = System.Text.Json.JsonSerializer.Serialize(bindResult),
                            IsSuccess = isSuccess,
                            ErrorMessage = bindResult?.ErrorMessage ?? "未知错误",
                            ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                            Creator = "System"
                        }).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        // 调用失败
                        StockInfoService.UpdateMesUploadStatusAsync(palletCode, (int)MesUploadStatusEnum.组盘上传失败).ConfigureAwait(false);
                    }
                });
                return content.OK("批量组盘确认成功");
            }