huangxiaoqiang
2026-03-31 990afb24199ef1f7a4648728ac28ed74394e459c
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;
@@ -1028,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);
@@ -1056,26 +1060,38 @@
            }
            //var outBoundMateriel = AppSettings.app<OutBoundMateriel>("OutBoundMateriel");
            var outBoundMateriel = _dt_ChangeoversRepository.QueryData(x => x.Status == "1").ToList();
            //var outBoundMateriel = _dt_ChangeoversRepository.QueryData(x => x.Status == "1").ToList();
            List<string>? materielCodes = outBoundMateriel.Count != 0
                ? outBoundMateriel.Where(x => x.ProductionLine == productionLine && x.ProcessCode == area.AreaCode)
                                  .Select(x => x.MaterielCode)
                                  .ToList()
                : null;
            //List<string>? materielCodes = outBoundMateriel.Count != 0
            //    ? outBoundMateriel.Where(x => x.ProductionLine == productionLine && x.ProcessCode == area.AreaCode)
            //                      .Select(x => x.MaterielCode)
            //                      .ToList()
            //    : null;
            var result = new DtStockInfo();
            //DtStockInfo result = null;
            result = await _stockInfoRepository.Db.Queryable<DtStockInfo>()
            var result = 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();
            //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}查询实盘库存信息失败:未找到符合条件的数据");
@@ -1123,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)
        {
@@ -1166,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)
        {
@@ -1544,6 +1597,13 @@
                ConsoleHelper.WriteErrorLine(content.ToJsonString());
                var result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString());
                if (result == null || !result.Success) return content.Error(result?.MOMMessage ?? "Deserialization error");
                List<string> strings = station.productLine.Split(",").ToList();
                if (!result.ProductionLine.Contains(strings))
                {
                    ConsoleHelper.WriteErrorLine($"托盘号【{palletCode}】请求产线【{result.ProductionLine}】不允许入【{station.Roadway}】");
                    return content.Error($"托盘号【{palletCode}】请求产线【{result.ProductionLine}】不允许入【{station.Roadway}】");
                }
                if (result.SerialNos.Count > 0)
                {
@@ -2275,7 +2335,7 @@
             .Where(x => x.DeviceStatus == 1.ToString() && process.Contains(x.DeviceCode))
             .Select(x => x.DeviceCode).ToListAsync();
        var minGroup = _locationRepository.QueryData(x => deviceCode.Contains(x.RoadwayNo) && x.LocationStatus == (int)LocationEnum.Free)
        var minGroup = _locationRepository.QueryData(x => deviceCode.Contains(x.RoadwayNo) && x.LocationStatus == (int)LocationEnum.Free && x.EnalbeStatus == (int)EnableEnum.Enable)
             .GroupBy(x => x.RoadwayNo)
             .OrderByDescending(g => g.Count()) // 根据每个组的元素数量排序
             .ToList(); // 取出数量最多的组
@@ -2284,6 +2344,10 @@
        foreach (var item in minGroup)
        {
            var number = BaseDal.QueryData(x => x.TargetAddress == item.Key).Count();
            if (item.Count() - number <= 0)
            {
                continue;
            }
            result.Add(item.Key, item.Count() - number);
        }