wanshenmean
11 小时以前 63ca4ac71443473a0ff72758e1ae3739b7640a68
refactor(TaskService_Inbound): InboundInContainer改为Task.Run异步执行

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
已修改1个文件
98 ■■■■■ 文件已修改
Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/WCS/TaskService_Inbound.cs 98 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/WCS/TaskService_Inbound.cs
@@ -179,41 +179,71 @@
                            return WebResponseContent.Instance.Error("任务完成失败");
                        // 根据库存Remark选择静置设备,查MES动态凭证
                        //string deviceName = stockInfo.Remark == "GW_1" ? "高温静置1"
                        //    : stockInfo.Remark == "GW_2" ? "高温静置2"
                        //    : "常温静置1";
                        //var mesConfig = _mesDeviceConfigService.GetByDeviceName(deviceName);
                        //string equipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE;
                        //string resourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE;
                        //string token = mesConfig?.Token;
                        string deviceName = stockInfo.Remark == "GW_1" ? "高温静置1"
                            : stockInfo.Remark == "GW_2" ? "高温静置2"
                            : "常温静置1";
                        var mesConfig = _mesDeviceConfigService.GetByDeviceName(deviceName);
                        string equipmentCode = mesConfig?.EquipmentCode ?? StockConstants.MES_EQUIPMENT_CODE;
                        string resourceCode = mesConfig?.ResourceCode ?? StockConstants.MES_RESOURCE_CODE;
                        string token = mesConfig?.Token;
                        // 调用MES托盘进站
                        //var inboundRequest = new InboundInContainerRequest
                        //{
                        //    EquipmentCode = equipmentCode,
                        //    ResourceCode = resourceCode,
                        //    LocalTime = DateTime.Now,
                        //    ContainerCode = taskDto.PalletCode
                        //};
                        //string requestJson = inboundRequest.ToJson();
                        //var inboundResult = string.IsNullOrWhiteSpace(token)
                        //    ? _mesService.InboundInContainer(inboundRequest)
                        //    : _mesService.InboundInContainer(inboundRequest, token);
                        //stopwatch.Stop();
                        //await _mesLogService.LogAsync(new MesApiLogDto
                        //{
                        //    ApiType = "InboundInContainer",
                        //    RequestJson = requestJson,
                        //    ResponseJson = JsonConvert.SerializeObject(inboundResult),
                        //    IsSuccess = inboundResult.IsSuccess,
                        //    ErrorMessage = inboundResult.ErrorMessage,
                        //    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                        //    Creator = "systeam"
                        //});
                        //if (inboundResult == null || inboundResult.Data == null || !inboundResult.Data.IsSuccess)
                        //{
                        //    return content.Error($"任务完成失败:MES进站失败: {inboundResult?.Data?.Msg ?? inboundResult?.ErrorMessage ?? "未知错误"}");
                        //}
                        // 异步调用MES托盘进站,不阻塞主逻辑
                        var palletCode = taskDto.PalletCode;
                        var localEquipmentCode = equipmentCode;
                        var localResourceCode = resourceCode;
                        var localToken = token;
                        _ = Task.Run(async () =>
                        {
                            var localStopwatch = Stopwatch.StartNew();
                            try
                            {
                                var inboundRequest = new InboundInContainerRequest
                                {
                                    EquipmentCode = localEquipmentCode,
                                    ResourceCode = localResourceCode,
                                    LocalTime = DateTime.Now,
                                    ContainerCode = palletCode
                                };
                                string localRequestJson = inboundRequest.ToJson();
                                var inboundResult = string.IsNullOrWhiteSpace(localToken)
                                    ? _mesService.InboundInContainer(inboundRequest)
                                    : _mesService.InboundInContainer(inboundRequest, localToken);
                                localStopwatch.Stop();
                                bool isSuccess = inboundResult?.Data?.IsSuccess ?? false;
                                int status = isSuccess
                                    ? (int)MesUploadStatusEnum.进站上传成功
                                    : (int)MesUploadStatusEnum.进站上传失败;
                                await _stockInfoService.UpdateMesUploadStatusAsync(palletCode, status);
                                await _mesLogService.LogAsync(new MesApiLogDto
                                {
                                    PalletCode = palletCode,
                                    ApiType = "InboundInContainer",
                                    RequestJson = localRequestJson,
                                    ResponseJson = JsonConvert.SerializeObject(inboundResult),
                                    IsSuccess = isSuccess,
                                    ErrorMessage = inboundResult?.Data?.Msg ?? inboundResult?.ErrorMessage ?? "未知错误",
                                    ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
                                    Creator = "systeam"
                                });
                            }
                            catch (Exception ex)
                            {
                                localStopwatch.Stop();
                                await _stockInfoService.UpdateMesUploadStatusAsync(palletCode, (int)MesUploadStatusEnum.进站上传失败);
                                await _mesLogService.LogAsync(new MesApiLogDto
                                {
                                    PalletCode = palletCode,
                                    ApiType = "InboundInContainer",
                                    IsSuccess = false,
                                    ErrorMessage = ex.Message,
                                    ElapsedMs = (int)localStopwatch.ElapsedMilliseconds,
                                    Creator = "systeam"
                                });
                            }
                        });
                        return await CompleteTaskAsync(task, "入库完成");
                    });
                }