肖洋
2024-12-09 eae9077f3978f695f4e3af5301e8a08bdd8bf23c
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/Task/Dt_TaskService.cs
@@ -1,12 +1,16 @@

using AngleSharp.Io;
using log4net.Core;
using Mapster;
using Masuit.Tools;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using SixLabors.Fonts.Tables.AdvancedTypographic;
using WIDESEA_Core.Const;
using WIDESEA_DTO.MOM;
using WIDESEA_DTO.WMS;
using WIDESEA_IServices;
using WIDESEA_IStoragIntegrationServices;
using WIDESEA_Model.Models;
using WIDESEA_Services;
using WIDESEA_StorageBasicRepository;
using WIDESEAWCS_BasicInfoRepository;
@@ -102,9 +106,12 @@
            // 事务处理
            await _unitOfWorkManage.UseTranAsync(async () =>
            {
                await UpdateLocationAsync(loc);
                if (task.TaskType != (int)TaskOutboundTypeEnum.OutQuality)
                {
                await DeleteStockInfoAsync(stock.Id);
                await DeleteStockInfoDetailsAsync(stock.StockInfoDetails);
                }
                await UpdateLocationAsync(loc);
                //if (task.Roadway.Contains("FR") || task.Roadway.Contains("GW") || task.TaskType == (int)TaskOutboundTypeEnum.OutTray)   //如果是分容或高温出库 将任务删除
                await DeleteTaskAsync(task.TaskId);
            });
@@ -165,7 +172,8 @@
                        TargetValue = parameterInfo.TargetValue,
                        LowerLomit = parameterInfo.LowerSpecificationsLimit,
                        UpperLimit = parameterInfo.UpperSpecificationsLimit,
                        DefectCode = defectCode
                        DefectCode = defectCode,
                        UOMCode = parameterInfo.UOMCode,
                    }
                }
            }).ToList()
@@ -762,11 +770,32 @@
            var stock = await QueryStockInfo(input.PalletCode);
            if (stock != null)
            {
                Dt_Task taskNew = new Dt_Task
                {
                    Grade = 1,
                    Roadway = stock.LocationInfo.RoadwayNo,
                    TargetAddress = stock.LocationInfo.RoadwayNo,
                    Dispatchertime = DateTime.Now,
                    MaterialNo = "",
                    NextAddress = stock.LocationInfo.RoadwayNo,
                    OrderNo = null,
                    PalletCode = stock.PalletCode,
                    SourceAddress = input.Position,
                    CurrentAddress = input.Position,
                    TaskState = (int)TaskInStatusEnum.InNew,
                    TaskType = (int)TaskInboundTypeEnum.InQuality,
                    TaskNum = BaseDal.GetTaskNo().Result,
                    Creater = "System", // 修正拼写错误
                    CreateDate = DateTime.Now,
                    TaskId = 0,
                };
                var taskDTO = CreateTaskDTO(task);
                // TODO质检回库
            }
            // 根据位置查询区域信息
            var area = await QueryAreaInfoByPosition(input.Position);
            var area = await QueryAreaInfoByPosition(input.Position.Replace("-1", ""));
            if (area == null)
            {
                return content.Error("该点位不在区域列表中存在");
@@ -780,6 +809,12 @@
            var result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString());
            if (!result.Success)
            {
                if (result.SerialNos.Count <= 0)
                {
                    // 空托盘入库逻辑
                    return await RequestTrayInTaskAsync(input);
                }
                else
                return content.Error(result.MOMMessage);
            }
@@ -790,11 +825,6 @@
                return await HandleErrorCells(input, area, serialNosError);
            }
            if (result.SerialNos.Count <= 0)
            {
                // 空托盘入库逻辑
                return await RequestTrayInTaskAsync(input);
            }
            var boxing = CreateBoxingInfo(result, input.PalletCode);
            if (boxing == null) return content.Error("组盘失败");
@@ -1049,7 +1079,7 @@
            SourceAddress = stockInfo.LocationCode,
            CurrentAddress = stockInfo.LocationCode,
            TaskState = (int)TaskOutStatusEnum.OutNew,
            TaskType = tag == (int)TaskOutboundTypeEnum.Outbound ? (int)TaskOutboundTypeEnum.Outbound : (int)TaskOutboundTypeEnum.OutTray,
            TaskType = tag,
            TaskNum = BaseDal.GetTaskNo().Result,
            Creater = "System", // 修正拼写错误
            CreateDate = DateTime.Now,
@@ -1151,6 +1181,84 @@
            content.Error(ex.Message);
        }
        return content;
    }
    #endregion
    #region 指定任务出库
    public async Task<WebResponseContent> CreateAndSendOutboundTask(string locationCode, string palletCode)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            // 查询库存信息
            var stockInfo = await _stockInfoRepository.QueryFirstNavAsync(x => x.LocationCode == locationCode && x.PalletCode == palletCode);
            if (stockInfo == null)
            {
                return content.Error("未查询到对应的库存信息");
            }
            var task = await BaseDal.QueryFirstAsync(x => x.PalletCode == palletCode);
            if (task != null)
            {
                return content.Error($"【{palletCode}】已存在任务");
            }
            int taskType = 0;
            if (stockInfo.IsFull)
                taskType = (int)TaskOutboundTypeEnum.OutQuality;
            else
                taskType = (int)TaskOutboundTypeEnum.OutTray;
            // 创建并添加任务到数据库
            task = CreateTask(stockInfo, "1020-1", taskType);
            //var taskId = await BaseDal.AddDataAsync(task);
            //if (taskId <= 0)
            //{
            //    return content.Error("任务创建失败");
            //}
            // 创建任务传输用的DTO对象
            var taskDTO = CreateTaskDTO(task);
            // 获取WMS IP地址用于发送任务请求
            var wmsIpAddress = GetWCSIpReceiveTask();
            if (wmsIpAddress == null)
            {
                throw new InvalidOperationException("WMS IP 未配置");
            }
            var tasks = new List<WMSTaskDTO>() { taskDTO };
            // 发送任务请求到WMS
            var result = await HttpHelper.PostAsync(wmsIpAddress, tasks.ToJsonString());
            content = JsonConvert.DeserializeObject<WebResponseContent>(result);
            if (content.Status)
            {
                // 添加任务到数据库
                await BaseDal.AddDataAsync(task);
                // 更新库存位置状态为不可用
                stockInfo.LocationInfo.LocationStatus = (int)LocationEnum.InStockDisable;
                await _locationRepository.UpdateDataAsync(stockInfo.LocationInfo);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生异常: {ex.Message}");
            content.Error(ex.Message);
        }
        return content;
    }
    private string GetWCSIpReceiveTask()
    {
        var configs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
        var wmsBase = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.WCSIPAddress)?.ConfigValue;
        var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.ReceiveTask)?.ConfigValue;
        if (wmsBase == null || ipAddress == null)
        {
            return null;
        }
        return wmsBase + ipAddress;
    }
    #endregion
@@ -1448,6 +1556,16 @@
                bool isResult = await BaseDal.UpdateDataAsync(task);
                return content.OK(data: task);
            }
            else if (task.TaskType == (int)TaskInboundTypeEnum.InQuality)
            {
                var stockInfo = _stockInfoRepository.QueryFirst(x => x.PalletCode == input.PalletCode);
                task.CurrentAddress = input.Position;
                task.TargetAddress = stockInfo.LocationInfo.LocationCode;
                task.NextAddress = stockInfo.LocationInfo.LocationCode;
                task.TaskState = (int)TaskInStatusEnum.Line_InFinish;
                bool isResult = await BaseDal.UpdateDataAsync(task);
                return content.OK(data: task);
            }
            else
            {
                var location = await GetLocationDistributeAsync(task.Roadway);