1
huangxiaoqiang
2026-02-25 02a4fdd78cef9c7d2a26bfb9aa187179cf0baac3
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/Task/Dt_TaskService.cs
@@ -1,4 +1,5 @@
using Mapster;
using AngleSharp.Io;
using Mapster;
using Masuit.Tools;
using SixLabors.Fonts.Tables.AdvancedTypographic;
using SqlSugar;
@@ -10,6 +11,7 @@
using WIDESEA_DTO.WMS;
using WIDESEA_IServices;
using WIDESEA_IStoragIntegrationServices;
using WIDESEA_Model.Models;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_QuartzJob.Models;
@@ -86,30 +88,30 @@
        {
            if (task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
            {
                if (task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
                //if (task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
                //{
                if (!task.Roadway.Contains("FR") && stock.ProcessCode != "OCVB")
                {
                    if (!task.Roadway.Contains("FR") && stock.ProcessCode != "OCVB")
                    var agingOutputDto = MapToAgingOutputDto(stock);
                    content = await _agingInOrOutInputService.GetOCVOutputAsync(agingOutputDto);
                    if (content.Status)
                    {
                        var agingOutputDto = MapToAgingOutputDto(stock);
                        content = await _agingInOrOutInputService.GetOCVOutputAsync(agingOutputDto);
                        var result = JsonConvert.DeserializeObject<BasicResult>(content.Data.ToString());
                        if (content.Status)
                        if (!result.Success && result.MessageCode == "E10001")
                        {
                            var result = JsonConvert.DeserializeObject<BasicResult>(content.Data.ToString());
                            if (!result.Success && result.MessageCode == "E10001")
                            {
                                await HandleOutBoundError(stock, task);
                            }
                        }
                        else
                        {
                            task.Remark = "NG";
                            ConsoleHelper.WriteErrorLine("MOM通报点芯NG");
                            return content.Error("MOM通报点芯NG");
                            await HandleOutBoundError(stock, task);
                        }
                    }
                    else
                    {
                        task.Remark = "NG";
                        ConsoleHelper.WriteErrorLine("MOM通报点芯NG");
                        return content.Error("MOM通报点芯NG");
                    }
                }
                //}
            }
            if (task.Remark == "NG")
@@ -429,8 +431,8 @@
            Console.WriteLine(err.Message.ToString());
            LogFactory.GetLog("任务完成").Error(true, err);
            content.Error(err.Message);
            task.ErrorMessage= err.Message;
            task.TaskState=(int)TaskInStatusEnum.SC_InExecuting;
            task.ErrorMessage = err.Message;
            task.TaskState = (int)TaskInStatusEnum.SC_InExecuting;
            await BaseDal.Update(task);
        }
        return content;
@@ -617,13 +619,13 @@
        }
        LogFactory.GetLog("任务完成").InfoFormat(true, "验证任务是否存在", JsonConvert.SerializeObject(task));
        if (task.TaskType == (int)TaskOutboundTypeEnum.InToOut)
        {
            return await CompleteInToOutTaskAsync(task);
        }
        // 验证库存是否存在
        var stock = await _stockInfoRepository.QueryFirstNavAsync(x => x.PalletCode == task.PalletCode);
        if (task.TaskType == (int)TaskOutboundTypeEnum.InToOut)
        {
            return await CompleteInToOutTaskAsync(task, stock);
        }
        // 根据任务类型调用相应的完成任务方法
        switch (task.TaskType)
        {
@@ -637,7 +639,7 @@
            case (int)TaskOutboundTypeEnum.OutTray:
            case (int)TaskOutboundTypeEnum.Outbound:
            case (int)TaskOutboundTypeEnum.OutNG:
                LogFactory.GetLog("任务完成").InfoFormat(true, "出库任务", "");
                return await CompleteStackTaskAsync(task, stock);
@@ -648,7 +650,6 @@
            case (int)TaskRelocationTypeEnum.Relocation:
                return await CompleteTransferTaskAsync(task, stock);
            default:
                return content.Error("任务类型不存在");
        }
@@ -666,15 +667,24 @@
        {
            // 查询任务
            var task = await QueryTaskByPalletCode(input.PalletCode);
            if (task != null)
            if (task != null && (task.TaskState == (int)TaskInStatusEnum.InNew || task.TaskState == (int)TaskOutStatusEnum.OutNew))
            {
                return CreateAndReturnWMSTaskDTO(task);
            }
            if(task != null && (task.TaskState != (int)TaskInStatusEnum.InNew || task.TaskState != (int)TaskOutStatusEnum.OutNew))
            {
                return content.Error($"托盘{input.PalletCode}存在任务");
            }
            // 查询库存信息(这里后续要完善质检回库逻辑)
            var stock = await QueryStockInfo(input.PalletCode);
            if (stock != null)
            {
                List<string> strings = new List<string>() { "GW", "CW", "FR" };
                if (stock.AreaCode.Contains(strings))
                {
                    return content.Error($"托盘【{stock.PalletCode}】存在库存不允许入库");
                }
                Dt_Task taskNew = new Dt_Task
                {
                    Grade = 1,
@@ -765,6 +775,7 @@
                //    return await HandleErrorCells(input, area, serialNosError);
                //}
                #endregion
                var boxing = CreateBoxingInfo(result, input.PalletCode);
                if (boxing == null) return content.Error("组盘失败");
@@ -861,8 +872,8 @@
                var configOne = _configService.GetByConfigKey(CateGoryConst.CONFIG_SYS_InStacker, SysConfigConst.CHInboundEmptyProductLineOne);
                var configTwo = _configService.GetByConfigKey(CateGoryConst.CONFIG_SYS_InStacker, SysConfigConst.CHInboundEmptyProductLineTwo);
                List<string>  productLineconfigOne = configOne.ConfigValue.Split(',').ToList();
                List<string>  productLineconfigTwo = configTwo.ConfigValue.Split(',').ToList();
                List<string> productLineconfigOne = configOne.ConfigValue.Split(',').ToList();
                List<string> productLineconfigTwo = configTwo.ConfigValue.Split(',').ToList();
                if (productLineconfigOne.Count == 0 && productLineconfigTwo.Count() == 0)
                {
@@ -881,13 +892,13 @@
                {
                    ConsoleHelper.WriteErrorLine($"未找到{result.ProductionLine}对应配置巷道");
                    return content.Error($"未找到{result.ProductionLine}对应配置巷道");
                }
                }
            }
            else
            {
                strings = stationManagers.Roadway.Split(',').ToList();
            }
            return await CreateNewTask(input, result.ProductionLine, result.ProcessCode, strings, 1);
        }
@@ -906,8 +917,8 @@
    // 获取组盘信息
    private DtBoxingInfo CreateBoxingInfo(ResultTrayCellsStatus result, string palletCode)
    {
         var boxing=_boxingInfoRepository.QueryFirst(x=>x.PalletCode == palletCode);
    {
        var boxing = _boxingInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
        if (boxing == null)
        {
            return new DtBoxingInfo
@@ -926,8 +937,11 @@
                }).ToList()
            };
        }
        else {
            _boxingInfoRepository.DeleteData(boxing);
        else
        {
            _boxingInfoRepository.Db.DeleteNav<DtBoxingInfo>(x => x.Id == boxing.Id)
                                            .Include(x => x.BoxingInfoDetails)
                                            .ExecuteCommandAsync();
            return new DtBoxingInfo
            {
                PalletCode = palletCode,
@@ -944,7 +958,7 @@
                }).ToList()
            };
        }
    }
    // 获取工艺申请
@@ -1016,6 +1030,8 @@
                stockInfo.LocationInfo.LocationStatus = (int)LocationEnum.InStockDisable;
                await _locationRepository.UpdateDataAsync(stockInfo.LocationInfo);
            }
            if (position == "1670")
                LogFactory.GetLog($"OCV{position}请求{productionLine}任务").Info(true, $"返回参数{JsonConvert.SerializeObject(taskDTO)}");
            // 返回成功响应
            return content.OK(data: taskDTO);
@@ -1054,16 +1070,28 @@
            var result = new DtStockInfo();
            result = await _stockInfoRepository.Db.Queryable<DtStockInfo>()
            var stockInfoList = await _stockInfoRepository.Db.Queryable<DtStockInfo>()
                            .Includes(x => x.LocationInfo)
                            .Includes(x => x.StockInfoDetails)
                            //.Includes(x => x.StockInfoDetails)
                            .Where(x => x.AreaCode == areaCode && x.OutboundTime < DateTime.Now && x.IsFull)
                            .WhereIF(!productionLine.IsNullOrEmpty(), x => x.ProductionLine == productionLine)
                            .WhereIF(!devices.IsNullOrEmpty(), x => devices.Contains(x.LocationInfo.RoadwayNo))
                            .Where(x => x.LocationInfo.LocationStatus == (int)LocationEnum.InStock && x.LocationInfo.AreaId == area.AreaID && x.LocationInfo.EnalbeStatus == (int)EnableEnum.Enable)
                            .WhereIF(!materielCodes.IsNullOrEmpty(), x => x.StockInfoDetails.Any(y => materielCodes.Contains(y.MaterielCode)))
                            //.WhereIF(!materielCodes.IsNullOrEmpty(), x => x.StockInfoDetails.Any(y => materielCodes.Contains(y.MaterielCode)))
                            .OrderBy(x => x.OutboundTime)
                            .FirstAsync();
                            .ToListAsync();
            foreach (var stock in stockInfoList)
            {
                var hasMatchingDetail = await _stockInfoRepository.Db.Queryable<DtStockInfoDetail>()
                    .Where(d => d.StockId == stock.Id && materielCodes.Contains(d.MaterielCode))
                    .AnyAsync();
                if (hasMatchingDetail)
                {
                    result = stock;
                    break;
                }
            }
            if (result.IsNullOrEmpty())
                ConsoleHelper.WriteErrorLine($"{area.AreaName}-{productionLine}查询实盘库存信息失败:未找到符合条件的数据");
@@ -1111,21 +1139,43 @@
                .Where(x => x.DeviceCode.Contains("CWSC"))
                .ToList().Select(x => x.DeviceCode).ToList();
            //var result = await _stockInfoRepository.Db.Queryable<DtStockInfo>()
            //    .Where(x => areaCodes.Contains(x.AreaCode) && x.OutboundTime < DateTime.Now && x.IsFull == true) // 过滤条件
            //    .WhereIF(!productionLine.IsNullOrEmpty(), x => x.ProductionLine == productionLine)
            //    .Includes(x => x.LocationInfo)
            //    .WhereIF(!deviceCode.IsNullOrEmpty(), x => deviceCode.Contains(x.LocationInfo.RoadwayNo))
            //    .Where(x => x.LocationInfo.LocationStatus == (int)LocationEnum.InStock && areaId.Contains(x.LocationInfo.AreaId) && x.LocationInfo.EnalbeStatus == (int)EnableEnum.Enable) // 过滤条件
            //    .Includes(x => x.StockInfoDetails)
            //    .WhereIF(!materielCodes.IsNullOrEmpty(), x => x.StockInfoDetails.Any(y => materielCodes.Contains(y.MaterielCode)))
            //    .OrderBy(x => x.OutboundTime) // 排序
            //    .FirstAsync(); // 获取第一个元素
            DtStockInfo stockInfo = null;
            var result = await _stockInfoRepository.Db.Queryable<DtStockInfo>()
                .Where(x => areaCodes.Contains(x.AreaCode) && x.OutboundTime < DateTime.Now && x.IsFull == true) // 过滤条件
                .WhereIF(!productionLine.IsNullOrEmpty(), x => x.ProductionLine == productionLine)
                .Includes(x => x.LocationInfo)
                .WhereIF(!deviceCode.IsNullOrEmpty(), x => deviceCode.Contains(x.LocationInfo.RoadwayNo))
                .Where(x => x.LocationInfo.LocationStatus == (int)LocationEnum.InStock && areaId.Contains(x.LocationInfo.AreaId) && x.LocationInfo.EnalbeStatus == (int)EnableEnum.Enable) // 过滤条件
                .Includes(x => x.StockInfoDetails)
                .WhereIF(!materielCodes.IsNullOrEmpty(), x => x.StockInfoDetails.Any(y => materielCodes.Contains(y.MaterielCode)))
                .OrderBy(x => x.OutboundTime) // 排序
                .FirstAsync(); // 获取第一个元素
                .ToListAsync();
            if (result.IsNullOrEmpty())
            foreach (var stock in result)
            {
                var hasMatchingDetail = await _stockInfoRepository.Db.Queryable<DtStockInfoDetail>()
                    .Where(d => d.StockId == stock.Id && materielCodes.Contains(d.MaterielCode))
                    .AnyAsync();
                if (hasMatchingDetail)
                {
                    stockInfo = stock;
                    break;
                }
            }
            if (stockInfo.IsNullOrEmpty())
                ConsoleHelper.WriteErrorLine($"{JsonConvert.SerializeObject(areaCodes)}-{productionLine}查询常温实盘库存信息失败:未找到符合条件的数据");
            return result;
            return stockInfo;
        }
        catch (Exception ex)
        {
@@ -1154,21 +1204,36 @@
                .Where(x => stackers.Contains(x.DeviceCode))
                .ToList().Select(x => x.DeviceCode).ToList();
            DtStockInfo stockInfo = null;
            var result = await _stockInfoRepository.Db.Queryable<DtStockInfo>()
                .Where(x => x.ProductionLine == station.productLine)
                .Where(x => x.AreaCode == areaCode && x.IsFull == false)
                .Includes(x => x.StockInfoDetails)
                .Where(x => x.StockInfoDetails.Any(y => y.MaterielCode == "空托盘"))
                //.Includes(x => x.StockInfoDetails)
                //.Where(x => x.StockInfoDetails.Any(y => y.MaterielCode == "空托盘"))
                .Includes(x => x.LocationInfo)
                .WhereIF(!deviceCode.IsNullOrEmpty(), x => deviceCode.Contains(x.LocationInfo.RoadwayNo))
                .Where(x => x.LocationInfo.LocationStatus == (int)LocationEnum.InStock && x.LocationInfo.AreaId == area.AreaID && x.LocationInfo.EnalbeStatus == (int)EnableEnum.Enable) // 过滤条件
                .OrderBy(x => x.CreateDate) // 排序
                .FirstAsync(); // 转换为列表
                .ToListAsync();
            foreach (var stock in result)
            {
                var hasMatchingDetail = await _stockInfoRepository.Db.Queryable<DtStockInfoDetail>()
                    .Where(d => d.StockId == stock.Id && d.MaterielCode == "空托盘")
                    .AnyAsync();
                if (hasMatchingDetail)
                {
                    stockInfo = stock;
                    break;
                }
            }
            if (result.IsNullOrEmpty())
                ConsoleHelper.WriteErrorLine($"{area}-{station.productLine}查询空盘库存信息失败:未找到符合条件的数据");
            return result;
            return stockInfo;
        }
        catch (Exception ex)
        {
@@ -1310,7 +1375,7 @@
            if (stockInfo.AreaCode.Contains("CH"))
            {
                // 创建并添加任务到数据库
                if(stockInfo.LocationInfo.RoadwayNo=="CHSC1"|| stockInfo.LocationInfo.RoadwayNo == "CHSC2"|| stockInfo.LocationInfo.RoadwayNo == "CHSC3")
                if (stockInfo.LocationInfo.RoadwayNo == "CHSC1" || stockInfo.LocationInfo.RoadwayNo == "CHSC2" || stockInfo.LocationInfo.RoadwayNo == "CHSC3")
                {
                    task = CreateTask(stockInfo, "1020-1", taskType);
                }
@@ -1318,7 +1383,7 @@
                {
                    task = CreateTask(stockInfo, "1049-8", taskType);
                }
            }
            else
            {
@@ -2145,7 +2210,7 @@
            ToAddress = await GetRoadWayAsync(process);
        else
            ToAddress = process[0];
        if(string.IsNullOrEmpty(ToAddress))
        if (string.IsNullOrEmpty(ToAddress))
        {
            return content.Error("无法获取目标地址");
        }