| | |
| | | } |
| | | |
| | | @Override |
| | | public InventoryInformationDto getReportList() { |
| | | InventoryInformationDto inventoryInformationDto = new InventoryInformationDto(); |
| | | IPage<ProcurementPageDto> procurementPageDtoIPage = this.listPage(new Page<>(1, -1), new ProcurementPageDto()); |
| | | if(CollectionUtils.isEmpty(procurementPageDtoIPage.getRecords())){ |
| | | return inventoryInformationDto; |
| | | } |
| | | // 计算总库存数量 |
| | | inventoryInformationDto.setTotalInventoryCount(procurementPageDtoIPage.getRecords().stream() |
| | | .map(ProcurementPageDto::getInboundNum0) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add) |
| | | .intValue()); |
| | | // 计算总库存金额-ProcurementPageDto里每个对象的inboundNum0值和taxInclusiveUnitPrice的乘积,之后相加得到总库存金额 |
| | | BigDecimal totalInventoryValue = procurementPageDtoIPage.getRecords().stream() |
| | | // 过滤空对象,避免NPE |
| | | .filter(Objects::nonNull) |
| | | // 处理每个对象的空值:null转为0 |
| | | .map(dto -> { |
| | | // 入库数量:null → 0 |
| | | BigDecimal inboundNum0 = Optional.ofNullable(dto.getInboundNum0()).orElse(BigDecimal.ZERO); |
| | | // 含税单价:null → 0 |
| | | BigDecimal taxInclusiveUnitPrice = Optional.ofNullable(dto.getTaxInclusiveUnitPrice()).orElse(BigDecimal.ZERO); |
| | | // 计算单个对象的库存金额:数量 × 含税单价 |
| | | return inboundNum0.multiply(taxInclusiveUnitPrice); |
| | | }) |
| | | // 所有单个金额求和,初始值为0 |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | // 设置总库存金额 |
| | | inventoryInformationDto.setTotalInventoryValue(totalInventoryValue); |
| | | // 计算库存变动数量-ProcurementPageDto里每个对象的inboundNum值和inboundNum0值的差值,之后相加得到库存变动数量 |
| | | inventoryInformationDto.setInventoryChangeCount(procurementPageDtoIPage.getRecords().stream() |
| | | // 过滤空对象,避免NPE |
| | | .filter(Objects::nonNull) |
| | | // 处理每个对象的空值:null转为0 |
| | | .map(dto -> { |
| | | // 入库数量:null → 0 |
| | | BigDecimal inboundNum = Optional.ofNullable(dto.getInboundNum()).orElse(BigDecimal.ZERO); |
| | | // 待出库数量:null → 0 |
| | | BigDecimal inboundNum0 = Optional.ofNullable(dto.getInboundNum0()).orElse(BigDecimal.ZERO); |
| | | // 计算单个对象的库存变动数量:数量 - 待出库数量 |
| | | return inboundNum.subtract(inboundNum0); |
| | | }) |
| | | // 所有单个变动数量求和,初始值为0 |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add) |
| | | .intValue()); |
| | | // 计算库存变动金额ProcurementPageDto里每个对象的taxInclusiveTotalPrice值的和 |
| | | BigDecimal inventoryChangeValue = procurementPageDtoIPage.getRecords().stream() |
| | | // 过滤空对象,避免NPE |
| | | .filter(Objects::nonNull) |
| | | // 处理每个对象的空值:null转为0 |
| | | .map(dto -> { |
| | | // 含税总价:null → 0 |
| | | BigDecimal taxInclusiveTotalPrice = Optional.ofNullable(dto.getTaxInclusiveTotalPrice()).orElse(BigDecimal.ZERO); |
| | | // 计算单个对象的入库库存金额:含税总价 |
| | | return taxInclusiveTotalPrice; |
| | | }) |
| | | // 所有单个变动金额求和,初始值为0 |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | // 设置库存变动金额 |
| | | inventoryInformationDto.setInventoryChangeValue(inventoryChangeValue.subtract(totalInventoryValue)); |
| | | return inventoryInformationDto; |
| | | } |
| | | @Override |
| | | public IPage<ProcurementPageDto> listPageByProduction(Page page, ProcurementPageDto procurementDto) { |
| | | IPage<ProcurementPageDto> procurementPageDtoIPage = procurementRecordMapper.listPageByProduction(page, procurementDto); |
| | | List<ProcurementPageDto> procurementPageDtos = procurementPageDtoIPage.getRecords(); |
| | |
| | | |
| | | @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; |
| | | } |
| | | // 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()); |
| | | |
| | | // 如果没有相关的出库记录,跳过该条数据 |
| | | 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); |
| | | // 出库数量 = 总数量 - 待出库数量 |
| | | Integer storageId = dto.getId(); |
| | | Integer salesLedgerProductId = Integer.valueOf(Math.toIntExact(dto.getSalesLedgerProductId())); |
| | | // 获取当前salesLedgerProductId对应的已出库总数(默认0) |
| | | BigDecimal totalInboundNum = storageIdToTotalOutNumMap.getOrDefault(salesLedgerProductId, BigDecimal.ZERO); |
| | | // 已出库数量 |
| | | dto.setTotalInboundNum(totalInboundNum); |
| | | // 待出库数量 = 总数量 - 已出库数量 |
| | | dto.setInboundNum0(dto.getInboundNum().subtract(totalInboundNum)); |
| | | // 库存价值 |
| | | if(dto.getUnitPrice() != null){ |
| | | dto.setTotalPrice(dto.getTotalInboundNum().multiply(dto.getUnitPrice())); |
| | | } |
| | | // 待出库数量 = 总数量 - 已出库数量(总数量空值则默认0) |
| | | // BigDecimal totalNum = dto.getInboundNum() == null ? BigDecimal.ZERO : dto.getInboundNum(); |
| | | BigDecimal totalNum = storageIdToTotalintNumMap.getOrDefault(salesLedgerProductId, BigDecimal.ZERO); |
| | | dto.setInboundNum(totalNum); |
| | | dto.setInboundNum0(totalNum.subtract(totalInboundNum)); |
| | | |
| | | // 库存价值 = 已出库数量 * 单价(单价空值则默认0) |
| | | BigDecimal unitPrice = dto.getUnitPrice() == null ? BigDecimal.ZERO : dto.getUnitPrice(); |
| | | dto.setTotalPrice(totalInboundNum.multiply(unitPrice)); |
| | | } |
| | | return procurementPageDtoCopyIPage; |
| | | } |