| | |
| | | public class MesInvItemStocksManager : Repository<MesInvItemStocks> |
| | | { |
| | | /// <summary> |
| | | /// 查询可以退货的物料 |
| | | /// 查询可以退货的物料(带分页和搜索) |
| | | /// </summary> |
| | | /// <param name="searchDto">搜索请求参数</param> |
| | | /// <returns>分页结果</returns> |
| | | public PagedResult<ReturnableStockDto> GetReturnableStocks(ReturnableStockSearchDto searchDto) |
| | | { |
| | | // 参数校验 |
| | | if (searchDto.PageIndex < 1) |
| | | { |
| | | throw new Exception("页码必须大于0"); |
| | | } |
| | | |
| | | if (!new[] { 10, 20, 50 }.Contains(searchDto.PageSize)) |
| | | { |
| | | throw new Exception("每页条数必须为10、20或50"); |
| | | } |
| | | |
| | | // 1. 查询XB_RACKING_TASK_SYXT_LOG中所有的条码并去重 |
| | | var distinctBarcodes = Db.Queryable<XbRackingTaskSyxtLog>() |
| | | .Where(x => !string.IsNullOrEmpty(x.ItemBarcode)) |
| | | .Select(x => x.ItemBarcode) |
| | | .Distinct() |
| | | .ToList(); |
| | | |
| | | if (distinctBarcodes == null || !distinctBarcodes.Any()) |
| | | { |
| | | return new PagedResult<ReturnableStockDto> |
| | | { |
| | | TbBillList = new List<ReturnableStockDto>(), |
| | | Pagination = new PaginationInfo |
| | | { |
| | | CurrentPage = searchDto.PageIndex, |
| | | PageSize = searchDto.PageSize, |
| | | TotalRecords = 0, |
| | | TotalPages = 0 |
| | | } |
| | | }; |
| | | } |
| | | |
| | | // 2. 构建查询条件 |
| | | var query = 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); |
| | | |
| | | // 3. 应用搜索条件 |
| | | var conditions = searchDto.Conditions; |
| | | if (conditions != null) |
| | | { |
| | | // 精确匹配条件 |
| | | if (!string.IsNullOrEmpty(conditions.IqcStatus)) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => stock.IqcStatus == conditions.IqcStatus); |
| | | } |
| | | |
| | | if (conditions.Quantity.HasValue) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => stock.Quantity == conditions.Quantity.Value); |
| | | } |
| | | |
| | | // 模糊匹配条件 |
| | | 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)) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => |
| | | depot.DepotName != null && depot.DepotName.Contains(conditions.DepotName)); |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(conditions.DepotSectionsCode)) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => |
| | | stock.DepotSectionsCode != null && stock.DepotSectionsCode.Contains(conditions.DepotSectionsCode)); |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(conditions.ItemNo)) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => |
| | | item.ItemNo != null && item.ItemNo.Contains(conditions.ItemNo)); |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(conditions.ItemName)) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => |
| | | item.ItemName != null && item.ItemName.Contains(conditions.ItemName)); |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(conditions.ItemModel)) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => |
| | | item.ItemModel != null && item.ItemModel.Contains(conditions.ItemModel)); |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(conditions.ItemUnitName)) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => |
| | | unit.Fname != null && unit.Fname.Contains(conditions.ItemUnitName)); |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(conditions.OrgCode)) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => |
| | | org.Fnumber != null && org.Fnumber.Contains(conditions.OrgCode)); |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(conditions.OrgName)) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => |
| | | org.Fname != null && org.Fname.Contains(conditions.OrgName)); |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(conditions.ItemBarcode)) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => |
| | | stock.ItemBarcode != null && stock.ItemBarcode.Contains(conditions.ItemBarcode)); |
| | | } |
| | | |
| | | // 日期范围条件 |
| | | if (!string.IsNullOrEmpty(conditions.IndepDateStart)) |
| | | { |
| | | if (DateTime.TryParse(conditions.IndepDateStart, out var startDate)) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => stock.IndepDate >= startDate); |
| | | } |
| | | } |
| | | |
| | | if (!string.IsNullOrEmpty(conditions.IndepDateEnd)) |
| | | { |
| | | if (DateTime.TryParse(conditions.IndepDateEnd, out var endDate)) |
| | | { |
| | | query = query.Where((stock, item, depot, org, unit) => stock.IndepDate <= endDate); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 4. 查询总记录数 |
| | | var totalRecords = query.Count(); |
| | | |
| | | // 5. 计算分页参数 |
| | | var totalPages = (int)Math.Ceiling((double)totalRecords / searchDto.PageSize); |
| | | var skip = (searchDto.PageIndex - 1) * searchDto.PageSize; |
| | | |
| | | // 6. 查询当前页数据(先查出中间数据) |
| | | 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, |
| | | item.ItemNo, |
| | | item.ItemName, |
| | | item.ItemModel, |
| | | stock.Quantity, |
| | | item.ItemUnit, |
| | | ItemUnitName = unit.Fname, |
| | | stock.IndepDate, |
| | | OrgCode = org.Fnumber, |
| | | OrgName = org.Fname, |
| | | stock.ItemBarcode |
| | | }) |
| | | .Skip(skip) |
| | | .Take(searchDto.PageSize) |
| | | .ToList(); |
| | | |
| | | // 7. 在内存中转换为DTO |
| | | var dataList = queryResult.Select(x => 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 |
| | | }).ToList(); |
| | | |
| | | // 8. 应用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(); |
| | | } |
| | | |
| | | // 9. 返回分页结果 |
| | | return new PagedResult<ReturnableStockDto> |
| | | { |
| | | TbBillList = dataList, |
| | | Pagination = new PaginationInfo |
| | | { |
| | | CurrentPage = searchDto.PageIndex, |
| | | PageSize = searchDto.PageSize, |
| | | TotalRecords = totalRecords, |
| | | TotalPages = totalPages |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 查询可以退货的物料(旧版本,保留兼容) |
| | | /// </summary> |
| | | /// <returns>可退货物料库存列表</returns> |
| | | public List<ReturnableStockDto> GetReturnableStocks() |
| | |
| | | stock.Quantity > 0) |
| | | .Select((stock, item, depot, org, unit) => new |
| | | { |
| | | IqcStatus = stock.IqcStatus, |
| | | StackCode = stock.StackCode, |
| | | stock.IqcStatus, |
| | | stock.StackCode, |
| | | DepotCode = stock.DepotsCode, |
| | | DepotName = depot.DepotName, |
| | | DepotSectionsCode = stock.DepotSectionsCode, |
| | | ItemNo = item.ItemNo, |
| | | ItemName = item.ItemName, |
| | | ItemModel = item.ItemModel, |
| | | Quantity = stock.Quantity, |
| | | ItemUnit = item.ItemUnit, |
| | | depot.DepotName, |
| | | stock.DepotSectionsCode, |
| | | item.ItemNo, |
| | | item.ItemName, |
| | | item.ItemModel, |
| | | stock.Quantity, |
| | | item.ItemUnit, |
| | | ItemUnitName = unit.Fname, |
| | | IndepDate = stock.IndepDate, |
| | | stock.IndepDate, |
| | | OrgCode = org.Fnumber, |
| | | OrgName = org.Fname, |
| | | ItemBarcode = stock.ItemBarcode |
| | | stock.ItemBarcode |
| | | }) |
| | | .ToList(); |
| | | |