| | |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.UUID; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int delRegistration(Long[] ids) { |
| | | // 删除采购台账产品开票记录对象 |
| | | LambdaQueryWrapper<ProductRecord> productRecordLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | productRecordLambdaQueryWrapper.in(ProductRecord::getId, Arrays.asList(ids)); |
| | | List<ProductRecord> productRecords = productRecordMapper.selectList(productRecordLambdaQueryWrapper); |
| | | |
| | | // 查询要删除的产品来票记录 |
| | | LambdaQueryWrapper<ProductRecord> productWrapper = new LambdaQueryWrapper<>(); |
| | | productWrapper.in(ProductRecord::getId, Arrays.asList(ids)); |
| | | List<ProductRecord> productRecords = productRecordMapper.selectList(productWrapper); |
| | | |
| | | if(CollectionUtils.isEmpty(productRecords)){ |
| | | return 0; |
| | | } |
| | | LambdaQueryWrapper<TicketRegistration> ticketRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | ticketRegistrationLambdaQueryWrapper.in(TicketRegistration::getId, productRecords.stream().map(ProductRecord::getTicketRegistrationId).collect(Collectors.toList())); |
| | | List<TicketRegistration> ticketRegistrations = ticketRegistrationMapper.selectList(ticketRegistrationLambdaQueryWrapper); |
| | | |
| | | // 修改产品信息 |
| | | for (ProductRecord productRecord : productRecords) { |
| | | BigDecimal subtract = ticketRegistrations.get(0).getInvoiceAmount().subtract(productRecords.get(0).getTicketsAmount()); |
| | | // 小于等于0删除 ,大于0修改 |
| | | if(subtract.compareTo(BigDecimal.ZERO) <= 0){ |
| | | ticketRegistrationMapper.deleteById(ticketRegistrations.get(0)); |
| | | // 删除付款流水记录 |
| | | paymentRegistrationMapper.delete(new LambdaQueryWrapper<PaymentRegistration>().eq(PaymentRegistration::getTicketRegistrationId, ticketRegistrations.get(0).getId())); |
| | | }else if(subtract.compareTo(BigDecimal.ZERO) > 0){ |
| | | ticketRegistrations.get(0).setInvoiceAmount(subtract); |
| | | ticketRegistrationMapper.updateById(ticketRegistrations.get(0)); |
| | | // 收集关联ID |
| | | Set<Long> ticketRegistrationIds = productRecords.stream() |
| | | .map(ProductRecord::getTicketRegistrationId) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | Set<Long> salesLedgerProductIds = productRecords.stream() |
| | | .map(ProductRecord::getSaleLedgerProjectId) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | // 查询来票登记 |
| | | List<TicketRegistration> ticketRegistrations = |
| | | ticketRegistrationMapper.selectBatchIds(ticketRegistrationIds); |
| | | |
| | | // 处理来票登记金额、付款流水 |
| | | for (TicketRegistration ticket : ticketRegistrations) { |
| | | |
| | | // 该来票登记下所有产品记录 |
| | | List<ProductRecord> recordsOfTicket = productRecords.stream() |
| | | .filter(r -> r.getTicketRegistrationId().equals(ticket.getId())) |
| | | .collect(Collectors.toList()); |
| | | |
| | | // 计算要回退的金额 |
| | | BigDecimal rollbackAmount = recordsOfTicket.stream() |
| | | .map(ProductRecord::getTicketsAmount) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | |
| | | BigDecimal remain = ticket.getInvoiceAmount().subtract(rollbackAmount); |
| | | |
| | | if (remain.compareTo(BigDecimal.ZERO) <= 0) { |
| | | // 删除来票登记 |
| | | ticketRegistrationMapper.deleteById(ticket.getId()); |
| | | // 删除付款流水 |
| | | paymentRegistrationMapper.delete( |
| | | new LambdaQueryWrapper<PaymentRegistration>() |
| | | .eq(PaymentRegistration::getTicketRegistrationId, ticket.getId()) |
| | | ); |
| | | } else { |
| | | ticket.setInvoiceAmount(remain); |
| | | ticketRegistrationMapper.updateById(ticket); |
| | | } |
| | | LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | salesLedgerProductLambdaQueryWrapper.eq(SalesLedgerProduct::getId, productRecord.getSaleLedgerProjectId()) |
| | | } |
| | | |
| | | // 回退销售台账产品的未来票 |
| | | for (ProductRecord record : productRecords) { |
| | | |
| | | LambdaQueryWrapper<SalesLedgerProduct> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(SalesLedgerProduct::getId, record.getSaleLedgerProjectId()) |
| | | .eq(SalesLedgerProduct::getType, 2); |
| | | List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(salesLedgerProductLambdaQueryWrapper); |
| | | if(!CollectionUtils.isEmpty(salesLedgerProducts)){ |
| | | for (SalesLedgerProduct salesLedgerProduct : salesLedgerProducts) { |
| | | salesLedgerProduct.setFutureTickets(salesLedgerProduct.getFutureTickets().add(productRecord.getTicketsNum())); |
| | | salesLedgerProduct.setFutureTicketsAmount(salesLedgerProduct.getFutureTicketsAmount().add(productRecord.getTicketsAmount())); |
| | | salesLedgerProductMapper.updateById(salesLedgerProduct); |
| | | |
| | | SalesLedgerProduct product = salesLedgerProductMapper.selectOne(wrapper); |
| | | if (product != null) { |
| | | product.setFutureTickets( |
| | | product.getFutureTickets().add(record.getTicketsNum()) |
| | | ); |
| | | product.setFutureTicketsAmount( |
| | | product.getFutureTicketsAmount().add(record.getTicketsAmount()) |
| | | ); |
| | | salesLedgerProductMapper.updateById(product); |
| | | } |
| | | } |
| | | |
| | | // 删除产品来票记录 |
| | | productRecordMapper.delete(productWrapper); |
| | | |
| | | // 重新计算 currentInvoiceAmount |
| | | for (Long productId : salesLedgerProductIds) { |
| | | refreshCurrentInvoiceAmount(productId); |
| | | } |
| | | // 删除采购台账产品开票记录 |
| | | productRecordMapper.delete(productRecordLambdaQueryWrapper); |
| | | |
| | | return 1; |
| | | } |
| | | |
| | | private void refreshCurrentInvoiceAmount(Long salesLedgerProductId) { |
| | | |
| | | // 查询该产品最新一条来票记录 |
| | | LambdaQueryWrapper<ProductRecord> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(ProductRecord::getSaleLedgerProjectId, salesLedgerProductId) |
| | | .orderByDesc(ProductRecord::getCreatedAt) |
| | | .last("limit 1"); |
| | | |
| | | ProductRecord latestRecord = productRecordMapper.selectOne(wrapper); |
| | | |
| | | SalesLedgerProduct product = |
| | | salesLedgerProductMapper.selectById(salesLedgerProductId); |
| | | |
| | | if (product == null) { |
| | | return; |
| | | } |
| | | |
| | | if (latestRecord == null) { |
| | | // 没有任何来票记录 |
| | | product.setTicketsAmount(BigDecimal.ZERO); |
| | | } else { |
| | | // 永远取最新一条 |
| | | product.setTicketsAmount(latestRecord.getTicketsAmount()); |
| | | } |
| | | |
| | | salesLedgerProductMapper.updateById(product); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public TicketRegistrationDto getRegistrationById(TicketRegistrationDto ticketRegistrationDto) { |
| | | TicketRegistration ticketRegistration = ticketRegistrationMapper.selectById(ticketRegistrationDto.getId()); |