11
tjx
2025-11-25 a6bc86e9bb73b0b6631a2e912904690d833d31a5
StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs
@@ -27,14 +27,13 @@
            throw new Exception("每页条数必须为10、20或50");
        }
        // 1. 查询XB_RACKING_TASK_SYXT_LOG中所有的条码并去重
        var distinctBarcodes = Db.Queryable<XbRackingTaskSyxtLog>()
        // 1. 查询XB_RACKING_TASK_SYXT_LOG中ItemBarcode和PalletCode的映射关系
        var rackingTaskData = Db.Queryable<XbRackingTaskSyxtLog>()
            .Where(x => !string.IsNullOrEmpty(x.ItemBarcode))
            .Select(x => x.ItemBarcode)
            .Distinct()
            .Select(x => new { x.ItemBarcode, x.PalletCode })
            .ToList();
        if (distinctBarcodes == null || !distinctBarcodes.Any())
        if (rackingTaskData == null || !rackingTaskData.Any())
        {
            return new PagedResult<ReturnableStockDto>
            {
@@ -49,6 +48,9 @@
            };
        }
        // 1.1 提取去重后的条码列表用于查询
        var distinctBarcodes = rackingTaskData.Select(x => x.ItemBarcode).Distinct().ToList();
        // 2. 构建查询条件
        var query = Db.Queryable<MesInvItemStocks>()
            .LeftJoin<MesItems>((stock, item) => stock.ItemId == item.Id)
@@ -56,7 +58,7 @@
            .LeftJoin<Organize>((stock, item, depot, org) => item.UseOrg == org.Id.ToString())
            .LeftJoin<MesUnit>((stock, item, depot, org, unit) => item.ItemUnit == unit.Id.ToString())
            .Where((stock, item, depot, org, unit) => 
                distinctBarcodes.Contains(stock.ItemBarcode) &&
                (distinctBarcodes.Contains(stock.ItemBarcode) || distinctBarcodes.Contains(stock.StackCode) ) &&
                stock.Quantity > 0);
        // 3. 应用搜索条件
@@ -75,11 +77,6 @@
            }
            // 模糊匹配条件
            if (!string.IsNullOrEmpty(conditions.StackCode))
            {
                query = query.Where((stock, item, depot, org, unit) =>
                    stock.StackCode != null && stock.StackCode.Contains(conditions.StackCode));
            }
            if (!string.IsNullOrEmpty(conditions.DepotName))
            {
@@ -153,20 +150,12 @@
            }
        }
        // 4. 查询总记录数
        var totalRecords = query.Count();
        // 5. 计算分页参数
        var totalPages = (int)Math.Ceiling((double)totalRecords / searchDto.PageSize);
        var skip = (searchDto.PageIndex - 1) * searchDto.PageSize;
        // 6. 查询当前页数据(先查出中间数据)
        // 4. 查询所有符合条件的数据(不分页)
        var queryResult = query
            .OrderByDescending((stock, item, depot, org, unit) => stock.IndepDate)
            .Select((stock, item, depot, org, unit) => new
            {
                stock.IqcStatus,
                stock.StackCode,
                DepotCode = stock.DepotsCode,
                depot.DepotName,
                stock.DepotSectionsCode,
@@ -179,47 +168,115 @@
                stock.IndepDate,
                OrgCode = org.Fnumber,
                OrgName = org.Fname,
                stock.ItemBarcode
                stock.ItemBarcode,
                StockStackCode = stock.StackCode
            })
            .Skip(skip)
            .Take(searchDto.PageSize)
            .ToList();
        // 7. 在内存中转换为DTO
        var dataList = queryResult.Select(x => new ReturnableStockDto
        // 5. 在内存中转换为DTO,关联PalletCode并赋值ItemBarcode(优先使用箱条码StockStackCode)
        var tempDataList = queryResult.Select(x =>
        {
            IqcStatus = x.IqcStatus == "已检" ? "1" : "0",
            ItemType = x.DepotName == "原材料仓" ? "0" : "1",
            StackCode = x.StackCode,
            DepotCode = x.DepotCode,
            DepotName = x.DepotName,
            DepotSectionsCode = x.DepotSectionsCode,
            ItemNo = x.ItemNo,
            ItemName = x.ItemName,
            ItemModel = x.ItemModel,
            Quantity = x.Quantity,
            ItemUnit = x.ItemUnit,
            ItemUnitName = x.ItemUnitName,
            IndepDate = x.IndepDate,
            OrgCode = x.OrgCode,
            OrgName = x.OrgName,
            ItemBarcode = x.ItemBarcode
            // 优先使用StockStackCode,否则使用ItemBarcode去查找PalletCode
            var barcodeToMatch = !string.IsNullOrEmpty(x.StockStackCode) ? x.StockStackCode : x.ItemBarcode;
            var palletCode = rackingTaskData
                .Where(r => r.ItemBarcode == barcodeToMatch)
                .Select(r => r.PalletCode)
                .FirstOrDefault();
            return new
            {
                IqcStatus = x.IqcStatus == "已检" ? "1" : "0",
                ItemType = x.DepotName == "原材料仓" ? "0" : "1",
                StackCode = palletCode, // 使用PalletCode作为母托盘编号
                x.DepotCode,
                x.DepotName,
                x.DepotSectionsCode,
                x.ItemNo,
                x.ItemName,
                x.ItemModel,
                x.Quantity,
                x.ItemUnit,
                x.ItemUnitName,
                x.IndepDate,
                x.OrgCode,
                x.OrgName,
                ItemBarcode = barcodeToMatch
            };
        }).ToList();
        // 8. 应用ItemType筛选(在内存中过滤)
        // 5.1 应用IqcStatus搜索条件(在内存中过滤)
        if (conditions != null && !string.IsNullOrEmpty(conditions.IqcStatus))
        {
            tempDataList = tempDataList
                .Where(x => x.IqcStatus == conditions.IqcStatus)
                .ToList();
        }
        // 5.2 应用StackCode搜索条件(在内存中过滤PalletCode)
        if (conditions != null && !string.IsNullOrEmpty(conditions.StackCode))
        {
            tempDataList = tempDataList
                .Where(x => x.StackCode != null && x.StackCode.Contains(conditions.StackCode))
                .ToList();
        }
        // 5.3 分组并合计Quantity
        var dataList = tempDataList
            .GroupBy(x => new
            {
                x.IqcStatus,
                x.ItemType,
                x.StackCode,
                x.DepotCode,
                x.DepotName,
                x.DepotSectionsCode,
                x.ItemNo,
                x.ItemName,
                x.ItemModel,
                x.ItemUnit,
                x.ItemUnitName,
                x.OrgCode,
                x.OrgName,
                x.ItemBarcode
            })
            .Select(g => new ReturnableStockDto
            {
                IqcStatus = g.Key.IqcStatus,
                ItemType = g.Key.ItemType,
                StackCode = g.Key.StackCode,
                DepotCode = g.Key.DepotCode,
                DepotName = g.Key.DepotName,
                DepotSectionsCode = g.Key.DepotSectionsCode,
                ItemNo = g.Key.ItemNo,
                ItemName = g.Key.ItemName,
                ItemModel = g.Key.ItemModel,
                Quantity = g.Sum(x => x.Quantity),
                ItemUnit = g.Key.ItemUnit,
                ItemUnitName = g.Key.ItemUnitName,
                IndepDate = g.Max(x => x.IndepDate),
                OrgCode = g.Key.OrgCode,
                OrgName = g.Key.OrgName,
                ItemBarcode = g.Key.ItemBarcode
            }).ToList();
        // 6. 应用ItemType筛选(在内存中过滤)
        if (conditions?.ItemType != null)
        {
            dataList = dataList.Where(x => x.ItemType == conditions.ItemType).ToList();
            // 重新计算分页信息
            totalRecords = dataList.Count;
            totalPages = (int)Math.Ceiling((double)totalRecords / searchDto.PageSize);
            dataList = dataList.Skip(skip).Take(searchDto.PageSize).ToList();
        }
        // 7. 计算总记录数和分页参数(基于最终结果)
        var totalRecords = dataList.Count;
        var totalPages = (int)Math.Ceiling((double)totalRecords / searchDto.PageSize);
        var skip = (searchDto.PageIndex - 1) * searchDto.PageSize;
        // 8. 对最终结果进行分页
        var pagedDataList = dataList.Skip(skip).Take(searchDto.PageSize).ToList();
        // 9. 返回分页结果
        return new PagedResult<ReturnableStockDto>
        {
            TbBillList = dataList,
            TbBillList = pagedDataList,
            Pagination = new PaginationInfo
            {
                CurrentPage = searchDto.PageIndex,
@@ -339,7 +396,7 @@
                        .LeftJoin<MesItems>((stock, item) =>
                            stock.ItemId == item.Id)
                        .Where((stock, item) =>
                            stock.ItemBarcode == barcode && stock.Quantity > 0)
                            (stock.ItemBarcode == barcode || stock.StackCode == barcode) && stock.Quantity > 0)
                        .GroupBy((stock, item) =>
                            new { stock.ItemId, stock.LotNo })
                        .Select((stock, item) => new