|  |  | 
 |  |  | package com.ruoyi.sales.controller; | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.metadata.IPage; | 
 |  |  | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; | 
 |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
 |  |  | 
 |  |  | import com.ruoyi.sales.dto.InvoiceLedgerDto; | 
 |  |  | import com.ruoyi.sales.dto.SalesLedgerDto; | 
 |  |  | import com.ruoyi.sales.mapper.InvoiceLedgerMapper; | 
 |  |  | import com.ruoyi.sales.mapper.InvoiceRegistrationProductMapper; | 
 |  |  | import com.ruoyi.sales.mapper.ReceiptPaymentMapper; | 
 |  |  | import com.ruoyi.sales.pojo.InvoiceLedger; | 
 |  |  | import com.ruoyi.sales.pojo.InvoiceRegistrationProduct; | 
 |  |  | import com.ruoyi.sales.pojo.ReceiptPayment; | 
 |  |  | import com.ruoyi.sales.pojo.SalesLedger; | 
 |  |  | import com.ruoyi.sales.service.ICommonFileService; | 
 |  |  | import com.ruoyi.sales.service.ISalesLedgerService; | 
 |  |  | 
 |  |  |  | 
 |  |  | import javax.servlet.http.HttpServletResponse; | 
 |  |  | import java.math.BigDecimal; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Objects; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private InvoiceLedgerMapper invoiceLedgerMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private ReceiptPaymentMapper receiptPaymentMapper; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 查询销售台账列表 | 
 |  |  |      */ | 
 |  |  | 
 |  |  |                 if (salesLedger.getId().intValue() == invoiceLedgerDto.getSalesLedgerId()) { | 
 |  |  |                     BigDecimal noInvoiceAmountTotal = salesLedger.getContractAmount().subtract(invoiceLedgerDto.getInvoiceTotal()); | 
 |  |  |                     salesLedger.setNoInvoiceAmountTotal(noInvoiceAmountTotal); | 
 |  |  |  | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return getDataTable(list); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |             iPage.setTotal(iPage.getRecords().size()); | 
 |  |  |             return iPage; | 
 |  |  |         } | 
 |  |  |         // 计算回款金额,待回款金额 | 
 |  |  |         List<InvoiceRegistrationProduct> invoiceRegistrationProducts = invoiceRegistrationProductMapper.selectList(new LambdaQueryWrapper<InvoiceRegistrationProduct>() | 
 |  |  |                 .in(InvoiceRegistrationProduct::getSalesLedgerId, salesLedgerIds)); | 
 |  |  |  | 
 |  |  |         List<InvoiceLedger> invoiceLedgers = invoiceLedgerMapper.selectList(new LambdaQueryWrapper<InvoiceLedger>() | 
 |  |  |                 .in(InvoiceLedger::getInvoiceRegistrationProductId, invoiceRegistrationProducts.stream().map(InvoiceRegistrationProduct::getId).collect(Collectors.toList()))); | 
 |  |  |         List<ReceiptPayment> receiptPayments = new ArrayList<>(); | 
 |  |  |         if(!CollectionUtils.isEmpty(invoiceLedgers)){ | 
 |  |  |             receiptPayments = receiptPaymentMapper.selectList(new LambdaQueryWrapper<ReceiptPayment>() | 
 |  |  |                     .in(ReceiptPayment::getInvoiceLedgerId, invoiceLedgers.stream().map(InvoiceLedger::getId).collect(Collectors.toList()))); | 
 |  |  |         } | 
 |  |  |         for (SalesLedger salesLedger : iPage.getRecords()) { | 
 |  |  |             boolean existFlag = false; | 
 |  |  |             BigDecimal noInvoiceAmountTotal = BigDecimal.ZERO; | 
 |  |  | 
 |  |  |                     noInvoiceAmountTotal = salesLedger.getContractAmount().subtract(invoiceLedgerDto.getInvoiceTotal()); | 
 |  |  |                     invoiceTotal = invoiceLedgerDto.getInvoiceTotal(); | 
 |  |  |                     existFlag = true; | 
 |  |  |                     if(!CollectionUtils.isEmpty(receiptPayments)){ | 
 |  |  |                         List<InvoiceRegistrationProduct> collect = invoiceRegistrationProducts.stream() | 
 |  |  |                                 .filter(item -> salesLedger.getId().equals(Long.parseLong(item.getSalesLedgerId().toString()))) | 
 |  |  |                                 .collect(Collectors.toList()); | 
 |  |  |                         List<Integer> collect1 = collect.stream() | 
 |  |  |                                 .map(InvoiceRegistrationProduct::getId).collect(Collectors.toList()); | 
 |  |  |                         List<InvoiceLedger> collect2 = invoiceLedgers.stream() | 
 |  |  |                                 .filter(item -> collect1.contains(item.getInvoiceRegistrationProductId())) | 
 |  |  |                                 .collect(Collectors.toList()); | 
 |  |  |                         // 获取已回款金额 | 
 |  |  |                         List<ReceiptPayment> collect3 = receiptPayments.stream() | 
 |  |  |                                 .filter(item -> collect2.stream().anyMatch(item1 -> item1.getId().equals(item.getInvoiceLedgerId()))) | 
 |  |  |                                 .collect(Collectors.toList()); | 
 |  |  |                         BigDecimal receiptPaymentAmountTotal = collect3.stream().map(ReceiptPayment::getReceiptPaymentAmount) | 
 |  |  |                                 .filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add); | 
 |  |  |                         // 获取待回款金额 | 
 |  |  |                         BigDecimal noReceiptPaymentAmountTotal = invoiceLedgerDto.getInvoiceTotal().subtract(receiptPaymentAmountTotal); | 
 |  |  |                         salesLedger.setReceiptPaymentAmountTotal(receiptPaymentAmountTotal); | 
 |  |  |                         salesLedger.setNoReceiptAmount(noReceiptPaymentAmountTotal); | 
 |  |  |                     } | 
 |  |  |                     break; | 
 |  |  |                 } | 
 |  |  |             } |