| | |
| | | |
| | | @Override |
| | | public IPage<ProcurementPageDtoCopy> listPageCopyByProduction(Page page, ProcurementPageDto procurementDto) { |
| | | IPage<ProcurementPageDtoCopy> procurementPageDtoCopyIPage = procurementRecordMapper.listPageCopyByProduction(page, procurementDto); |
| | | IPage<ProcurementPageDtoCopy> procurementPageDtoCopyIPage = procurementRecordMapper.listPagePRS(page, procurementDto); |
| | | List<ProcurementPageDtoCopy> procurementPageDtoCopyList = procurementPageDtoCopyIPage.getRecords(); |
| | | // 计算待入库数量 |
| | | // 查询采购记录已入库数量 |
| | |
| | | if(CollectionUtils.isEmpty( collect)){ |
| | | return procurementPageDtoCopyIPage; |
| | | } |
| | | LambdaQueryWrapper<ProcurementRecordOut> procurementRecordLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | procurementRecordLambdaQueryWrapper.in(ProcurementRecordOut::getProcurementRecordStorageId, collect); |
| | | procurementRecordLambdaQueryWrapper.eq(ProcurementRecordOut::getType, 2); |
| | | List<ProcurementRecordOut> procurementRecords = procurementRecordOutMapper.selectList(procurementRecordLambdaQueryWrapper); |
| | | if(CollectionUtils.isEmpty( procurementRecords)){ |
| | | return procurementPageDtoCopyIPage; |
| | | } |
| | | // LambdaQueryWrapper<ProcurementRecordOut> procurementRecordLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | // procurementRecordLambdaQueryWrapper.in(ProcurementRecordOut::getProcurementRecordStorageId, collect); |
| | | // procurementRecordLambdaQueryWrapper.eq(ProcurementRecordOut::getType, 2); |
| | | // List<ProcurementRecordOut> procurementRecords = procurementRecordOutMapper.selectList(procurementRecordLambdaQueryWrapper); |
| | | // if(CollectionUtils.isEmpty(procurementRecords)){ |
| | | // return procurementPageDtoCopyIPage; |
| | | // } |
| | | // for (ProcurementPageDtoCopy dto : procurementPageDtoCopyList) { |
| | | // // 根据采购台账ID筛选对应的出库记录 |
| | | // List<ProcurementRecordOut> collect1 = procurementRecords.stream() |
| | | // .filter(ProcurementRecordOut -> ProcurementRecordOut.getProcurementRecordStorageId().equals(dto.getId())) |
| | | // .collect(Collectors.toList()); |
| | | // |
| | | // // 如果没有相关的出库记录,跳过该条数据 |
| | | // if(CollectionUtils.isEmpty(collect1)){ |
| | | // dto.setInboundNum0(dto.getInboundNum()); |
| | | // dto.setTotalInboundNum(BigDecimal.ZERO); |
| | | // continue; |
| | | // } |
| | | // |
| | | // // 计算已出库数量总和,并设置待出库数量 |
| | | // BigDecimal totalInboundNum = collect1.stream() |
| | | // .map(ProcurementRecordOut::getInboundNum) |
| | | // .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | // // 出库数量 = 总数量 - 待出库数量 |
| | | // dto.setTotalInboundNum(totalInboundNum); |
| | | // // 待出库数量 = 总数量 - 已出库数量 |
| | | // dto.setInboundNum0(dto.getInboundNum().subtract(totalInboundNum)); |
| | | // // 库存价值 |
| | | // if(dto.getUnitPrice() != null){ |
| | | // dto.setTotalPrice(dto.getTotalInboundNum().multiply(dto.getUnitPrice())); |
| | | // } |
| | | // } |
| | | // 1. 查询采购记录已入库的出库记录(按storageId分组) |
| | | LambdaQueryWrapper<ProcurementRecordOut> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.in(ProcurementRecordOut::getProcurementRecordStorageId, collect); |
| | | List<ProcurementRecordOut> recordOutList = procurementRecordOutMapper.selectList(queryWrapper); |
| | | |
| | | // 2. 按SalesLedgerProductId分组,统计每个id对应的已出库数量总和-已出库数量 |
| | | Map<Integer, BigDecimal> storageIdToTotalOutNumMap = recordOutList.stream() |
| | | .collect(Collectors.groupingBy( |
| | | ProcurementRecordOut::getSalesLedgerProductId, |
| | | Collectors.reducing( |
| | | BigDecimal.ZERO, |
| | | ProcurementRecordOut::getInboundNum, |
| | | (a, b) -> a.add(b == null ? BigDecimal.ZERO : b) |
| | | ) |
| | | )); |
| | | // 2. procurementPageDtoCopyList按SalesLedgerProductId分组,统计每个id对应的已出库数量总和-入库库数量 |
| | | Map<Long, BigDecimal> storageIdToTotalintNumMap = procurementPageDtoCopyList.stream() |
| | | .collect(Collectors.groupingBy( |
| | | ProcurementPageDtoCopy::getSalesLedgerProductId, |
| | | Collectors.reducing( |
| | | BigDecimal.ZERO, |
| | | ProcurementPageDtoCopy::getInboundNum, |
| | | (a, b) -> a.add(b == null ? BigDecimal.ZERO : b) |
| | | ) |
| | | )); |
| | | // 3. 循环给dto赋值 |
| | | for (ProcurementPageDtoCopy dto : procurementPageDtoCopyList) { |
| | | // 根据采购台账ID筛选对应的出库记录 |
| | | List<ProcurementRecordOut> collect1 = procurementRecords.stream() |
| | | .filter(ProcurementRecordOut -> ProcurementRecordOut.getProcurementRecordStorageId().equals(dto.getId())) |
| | | .collect(Collectors.toList()); |
| | | Integer storageId = dto.getId(); |
| | | Integer salesLedgerProductId = Integer.valueOf(Math.toIntExact(dto.getSalesLedgerProductId())); |
| | | // 获取当前salesLedgerProductId对应的已出库总数(默认0) |
| | | BigDecimal totalInboundNum = storageIdToTotalOutNumMap.getOrDefault(salesLedgerProductId, BigDecimal.ZERO); |
| | | |
| | | // 如果没有相关的出库记录,跳过该条数据 |
| | | if(CollectionUtils.isEmpty(collect1)){ |
| | | dto.setInboundNum0(dto.getInboundNum()); |
| | | dto.setTotalInboundNum(BigDecimal.ZERO); |
| | | continue; |
| | | } |
| | | |
| | | // 计算已出库数量总和,并设置待出库数量 |
| | | BigDecimal totalInboundNum = collect1.stream() |
| | | .map(ProcurementRecordOut::getInboundNum) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | // 出库数量 = 总数量 - 待出库数量 |
| | | // 待出库数量 = 总数量 - 已出库数量(总数量空值则默认0) |
| | | // BigDecimal totalNum = dto.getInboundNum() == null ? BigDecimal.ZERO : dto.getInboundNum(); |
| | | BigDecimal totalNum = storageIdToTotalintNumMap.getOrDefault(salesLedgerProductId, BigDecimal.ZERO); |
| | | dto.setInboundNum0(totalNum.subtract(totalInboundNum)); |
| | | // 已出库数量 |
| | | dto.setTotalInboundNum(totalInboundNum); |
| | | // 待出库数量 = 总数量 - 已出库数量 |
| | | dto.setInboundNum0(dto.getInboundNum().subtract(totalInboundNum)); |
| | | // 库存价值 |
| | | if(dto.getUnitPrice() != null){ |
| | | dto.setTotalPrice(dto.getTotalInboundNum().multiply(dto.getUnitPrice())); |
| | | } |
| | | // 库存价值 = 已出库数量 * 单价(单价空值则默认0) |
| | | BigDecimal unitPrice = dto.getUnitPrice() == null ? BigDecimal.ZERO : dto.getUnitPrice(); |
| | | dto.setTotalPrice(totalInboundNum.multiply(unitPrice)); |
| | | } |
| | | return procurementPageDtoCopyIPage; |
| | | } |