wanshenmean
16 小时以前 63ca4ac71443473a0ff72758e1ae3739b7640a68
Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/WCS/TaskService_Inbound.cs
@@ -1,10 +1,16 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Newtonsoft.Json;
using System.Diagnostics;
using WIDESEA_Common.Constants;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.MES;
using WIDESEA_DTO.Task;
using WIDESEA_IBasicService;
using WIDESEA_Model.Models;
namespace WIDESEA_TaskInfoService
@@ -116,6 +122,7 @@
        /// </summary>
        public async Task<WebResponseContent> InboundFinishTaskAsync(CreateTaskDto taskDto)
        {
            var stopwatch = Stopwatch.StartNew();
            try
            {
                var task = await BaseDal.QueryFirstAsync(s => s.PalletCode == taskDto.PalletCode);
@@ -125,45 +132,121 @@
                if (location == null) return WebResponseContent.Instance.Error("未找到对应的货位");
                var stockInfo = await _stockInfoService.GetStockInfoAsync(taskDto.PalletCode);
                if (stockInfo == null) return WebResponseContent.Instance.Error("未找到对应库存信息");
                // 判断是不是极卷库任务
                if (taskDto.WarehouseId == (int)WarehouseEnum.FJ1 || taskDto.WarehouseId == (int)WarehouseEnum.ZJ1)
                if (stockInfo == null)
                {
                    return await CompleteAgvInboundTaskAsync(taskDto);
                    return await _unitOfWorkManage.BeginTranAsync(async () =>
                    {
                        stockInfo = new Dt_StockInfo
                        {
                            PalletCode = taskDto.PalletCode,
                            WarehouseId = task.WarehouseId,
                            StockStatus = StockStatusEmun.空托盘库存.GetHashCode(),
                            Creater = StockConstants.SYSTEM_USER,
                            Details = null,
                            LocationCode = location.LocationCode,
                            LocationId = location.Id
                        };
                        var updateLocationResult = await _locationInfoService.UpdateLocationInfoAsync(location);
                        var updateStockResult = await _stockInfoService.Repository.AddDataAsync(stockInfo);
                        return await CompleteTaskAsync(task, "入库完成");
                    });
                }
                return await _unitOfWorkManage.BeginTranAsync(async () =>
                else
                {
                    WebResponseContent content = new WebResponseContent();
                    stockInfo.LocationCode = location.LocationCode;
                    stockInfo.LocationId = location.Id;
                    SetOutboundDateByRoadway(task, stockInfo);
                    stockInfo.StockStatus = StockStatusEmun.入库完成.GetHashCode();
                    // 判断是不是极卷库任务
                    if (taskDto.WarehouseId == (int)WarehouseEnum.FJ1 || taskDto.WarehouseId == (int)WarehouseEnum.ZJ1)
                    {
                        return await CompleteAgvInboundTaskAsync(taskDto);
                    }
                    location.LocationStatus = LocationStatusEnum.InStock.GetHashCode();
                    return await _unitOfWorkManage.BeginTranAsync(async () =>
                    {
                        WebResponseContent content = new WebResponseContent();
                        stockInfo.LocationCode = location.LocationCode;
                        stockInfo.LocationId = location.Id;
                    var updateLocationResult = await _locationInfoService.UpdateLocationInfoAsync(location);
                    var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
                    if (!updateLocationResult || !updateStockResult)
                        return WebResponseContent.Instance.Error("任务完成失败");
                    // 调用MES托盘进站
                    //var inboundRequest = new InboundInContainerRequest
                    //{
                    //    EquipmentCode = "STK-GROUP-001",
                    //    ResourceCode = "STK-GROUP-001",
                    //    LocalTime = DateTime.Now,
                    //    ContainerCode = taskDto.PalletCode
                    //};
                    //var inboundResult = _mesService.InboundInContainer(inboundRequest);
                    //if (inboundResult == null || inboundResult.Data == null || !inboundResult.Data.IsSuccess)
                    //{
                    //    return content.Error($"任务完成失败:MES进站失败: {inboundResult?.Data?.Msg ?? inboundResult?.ErrorMessage ?? "未知错误"}");
                    //}
                    return await CompleteTaskAsync(task, "入库完成");
                });
                        SetOutboundDateByRoadway(task, stockInfo);
                        stockInfo.StockStatus = StockStatusEmun.入库完成.GetHashCode();
                        location.LocationStatus = LocationStatusEnum.InStock.GetHashCode();
                        var updateLocationResult = await _locationInfoService.UpdateLocationInfoAsync(location);
                        var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
                        if (!updateLocationResult || !updateStockResult)
                            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;
                        // 异步调用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, "入库完成");
                    });
                }
            }
            catch (Exception ex)
            {
@@ -207,4 +290,4 @@
        #endregion 入库任务
    }
}
}