| | |
| | | .Select((stock, item, depot, org, unit) => new |
| | | { |
| | | stock.IqcStatus, |
| | | stock.Visible, |
| | | DepotCode = stock.DepotsCode, |
| | | depot.DepotName, |
| | | stock.DepotSectionsCode, |
| | |
| | | stockStatus = rackingTask?.Code == "200" ? "已在立库中" : "进入立库的路上"; |
| | | } |
| | | |
| | | // 冻结标识为1时,IqcStatus为0;否则根据IqcStatus判断 |
| | | var iqcStatusValue = x.Visible == 1 ? "0" : |
| | | (x.IqcStatus == "特采直接使用" || x.IqcStatus == "已检" || x.IqcStatus == "免检" ? "1" : "0"); |
| | | |
| | | return new |
| | | { |
| | | IqcStatus = x.IqcStatus == "已检" ? "1" : "0", |
| | | IqcStatus = iqcStatusValue, |
| | | ItemType = x.DepotName == "原材料仓" ? "0" : "1", |
| | | StackCode = rackingTask?.PalletCode, |
| | | x.DepotCode, |
| | |
| | | TotalPages = totalPages |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 查询可以退货的物料(旧版本,保留兼容) |
| | | /// </summary> |
| | | /// <returns>可退货物料库存列表</returns> |
| | | public List<ReturnableStockDto> GetReturnableStocks() |
| | | { |
| | | // 优化点1: 使用原生SQL在数据库端完成去重和排序,避免全表加载到内存 |
| | | // Oracle 11g使用ROW_NUMBER()窗口函数获取每个条码的最新记录 |
| | | var rackingTaskSql = @" |
| | | SELECT ITEM_BARCODE AS ItemBarcode, PALLETCODE AS PalletCode, CODE AS Code |
| | | FROM ( |
| | | SELECT ITEM_BARCODE, PALLETCODE, CODE, |
| | | ROW_NUMBER() OVER (PARTITION BY ITEM_BARCODE ORDER BY ID DESC) AS RN |
| | | FROM XB_RACKING_TASK_SYXT_LOG |
| | | WHERE ITEM_BARCODE IS NOT NULL AND (CODE IS NULL OR CODE != '500') |
| | | ) WHERE RN = 1"; |
| | | |
| | | var rackingTaskData = Db.Ado.SqlQuery<RackingTaskDto>(rackingTaskSql); |
| | | |
| | | if (rackingTaskData == null || !rackingTaskData.Any()) |
| | | { |
| | | return new List<ReturnableStockDto>(); |
| | | } |
| | | |
| | | // 优化点2: 过滤null值并使用字典提高条码匹配效率,从O(n)降低到O(1) |
| | | var validRackingData = rackingTaskData.Where(x => !string.IsNullOrEmpty(x.ItemBarcode)).ToList(); |
| | | var rackingTaskDict = validRackingData.ToDictionary(x => x.ItemBarcode); |
| | | var distinctBarcodes = validRackingData.Select(x => x.ItemBarcode).ToList(); |
| | | |
| | | // 优化点3: 在数据库层面完成关联查询,一次性获取所有需要的数据 |
| | | var queryResult = Db.Queryable<MesInvItemStocks>() |
| | | .LeftJoin<MesItems>((stock, item) => stock.ItemId == item.Id) |
| | | .LeftJoin<MesDepots>((stock, item, depot) => |
| | | stock.DepotsCode == depot.DepotCode) |
| | | .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) && |
| | | stock.Quantity > 0) |
| | | .Select((stock, item, depot, org, unit) => new |
| | | { |
| | | stock.IqcStatus, |
| | | stock.StackCode, |
| | | DepotCode = stock.DepotsCode, |
| | | depot.DepotName, |
| | | stock.DepotSectionsCode, |
| | | item.ItemNo, |
| | | item.ItemName, |
| | | item.ItemModel, |
| | | stock.Quantity, |
| | | item.ItemUnit, |
| | | ItemUnitName = unit.Fname, |
| | | stock.IndepDate, |
| | | OrgCode = org.Fnumber, |
| | | OrgName = org.Fname, |
| | | stock.ItemBarcode |
| | | }) |
| | | .ToList(); |
| | | |
| | | // 优化点4: 使用字典查找替代Where().FirstOrDefault(),提高匹配性能 |
| | | var result = queryResult.Select(x => |
| | | { |
| | | rackingTaskDict.TryGetValue(x.ItemBarcode, out var rackingTask); |
| | | |
| | | string stockStatus = "0"; |
| | | if (rackingTask?.Code != null) |
| | | { |
| | | stockStatus = rackingTask.Code == "200" ? "1" : "2"; |
| | | } |
| | | |
| | | return new ReturnableStockDto |
| | | { |
| | | 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, |
| | | StockStatus = stockStatus |
| | | }; |
| | | }).ToList(); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | private class RackingTaskDto |