From 18fa07479d3c7f5a9b683ab0f698528d9bd2a9ec Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期二, 21 四月 2026 11:23:53 +0800
Subject: [PATCH] feat: 入库管理、出库台账增加合同号字段;支持合同模糊查询

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java |  108 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 107 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
index 280568a..9b21618 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -418,7 +418,112 @@
 
     @Override
     public IPage<SalesLedger> selectSalesLedgerListPage(Page page, SalesLedgerDto salesLedgerDto) {
-        return salesLedgerMapper.selectSalesLedgerListPage(page, salesLedgerDto);
+        IPage<SalesLedger> iPage = salesLedgerMapper.selectSalesLedgerListPage(page, salesLedgerDto);
+
+        if (CollectionUtils.isEmpty(iPage.getRecords())) {
+            return iPage;
+        }
+
+        List<Long> salesLedgerIds = iPage.getRecords().stream().map(SalesLedger::getId).collect(Collectors.toList());
+
+        boolean hasWidthHeightFilter = salesLedgerDto.getWidth() != null || salesLedgerDto.getHeight() != null;
+        Map<Long, List<SalesLedgerProduct>> matchedProductsMap = Collections.emptyMap();
+        if (hasWidthHeightFilter) {
+            LambdaQueryWrapper<SalesLedgerProduct> productQueryWrapper = new LambdaQueryWrapper<SalesLedgerProduct>()
+                    .in(SalesLedgerProduct::getSalesLedgerId, salesLedgerIds)
+                    .eq(SalesLedgerProduct::getType, 1);
+            if (salesLedgerDto.getWidth() != null) {
+                productQueryWrapper.eq(SalesLedgerProduct::getWidth, salesLedgerDto.getWidth());
+            }
+            if (salesLedgerDto.getHeight() != null) {
+                productQueryWrapper.eq(SalesLedgerProduct::getHeight, salesLedgerDto.getHeight());
+            }
+            List<SalesLedgerProduct> matchedProducts = salesLedgerProductMapper.selectList(productQueryWrapper);
+            matchedProductsMap = CollectionUtils.isEmpty(matchedProducts) ? Collections.emptyMap()
+                    : matchedProducts.stream().collect(Collectors.groupingBy(SalesLedgerProduct::getSalesLedgerId));
+        }
+
+        List<SalesLedgerProductTotalsDto> productTotals = salesLedgerProductMapper.selectSalesLedgerProductTotals(salesLedgerIds, 1);
+        Map<Long, SalesLedgerProductTotalsDto> productTotalsMap = CollectionUtils.isEmpty(productTotals)
+                ? Collections.emptyMap()
+                : productTotals.stream()
+                .filter(t -> t.getSalesLedgerId() != null)
+                .collect(Collectors.toMap(SalesLedgerProductTotalsDto::getSalesLedgerId, Function.identity(), (a, b) -> a));
+
+        List<InvoiceLedgerDto> invoiceLedgerDtoList = invoiceLedgerMapper.invoicedTotal(salesLedgerIds);
+        if (CollectionUtils.isEmpty(invoiceLedgerDtoList)) {
+            invoiceLedgerDtoList = Collections.emptyList();
+        }
+
+        Map<Long, BigDecimal> invoiceTotals = invoiceLedgerDtoList.stream()
+                .filter(dto -> dto.getSalesLedgerId() != null && dto.getInvoiceTotal() != null)
+                .collect(Collectors.toMap(
+                        dto -> dto.getSalesLedgerId().longValue(),
+                        InvoiceLedgerDto::getInvoiceTotal,
+                        BigDecimal::add
+                ));
+
+        List<ReceiptPayment> receiptPayments = Collections.emptyList();
+        if (!CollectionUtils.isEmpty(salesLedgerIds)) {
+            receiptPayments = receiptPaymentMapper.selectList(new LambdaQueryWrapper<ReceiptPayment>()
+                    .in(ReceiptPayment::getSalesLedgerId, salesLedgerIds));
+        }
+
+        Map<Long, BigDecimal> receiptTotals = new HashMap<>();
+        if (!CollectionUtils.isEmpty(receiptPayments)) {
+            for (ReceiptPayment receiptPayment : receiptPayments) {
+                if (receiptPayment.getSalesLedgerId() != null && receiptPayment.getReceiptPaymentAmount() != null) {
+                    receiptTotals.merge(receiptPayment.getSalesLedgerId(), receiptPayment.getReceiptPaymentAmount(), BigDecimal::add);
+                }
+            }
+        }
+
+        for (SalesLedger salesLedger : iPage.getRecords()) {
+            Long ledgerId = salesLedger.getId();
+
+            SalesLedgerProductTotalsDto totals = productTotalsMap.get(ledgerId);
+            if (totals != null) {
+                salesLedger.setProductTotalQuantity(totals.getTotalQuantity() != null ? totals.getTotalQuantity() : BigDecimal.ZERO);
+                salesLedger.setProductTotalArea(totals.getTotalArea() != null ? totals.getTotalArea() : BigDecimal.ZERO);
+            } else {
+                salesLedger.setProductTotalQuantity(BigDecimal.ZERO);
+                salesLedger.setProductTotalArea(BigDecimal.ZERO);
+            }
+            if (hasWidthHeightFilter) {
+                salesLedger.setMatchedProducts(matchedProductsMap.getOrDefault(ledgerId, Collections.emptyList()));
+            }
+
+            BigDecimal contractAmount = salesLedger.getContractAmount() == null ? BigDecimal.ZERO : salesLedger.getContractAmount();
+            BigDecimal invoiceTotal = invoiceTotals.getOrDefault(ledgerId, BigDecimal.ZERO);
+            BigDecimal receiptPaymentAmountTotal = receiptTotals.getOrDefault(ledgerId, BigDecimal.ZERO);
+
+            BigDecimal noInvoiceAmountTotal = contractAmount.subtract(invoiceTotal);
+            if (noInvoiceAmountTotal.compareTo(BigDecimal.ZERO) < 0) {
+                noInvoiceAmountTotal = BigDecimal.ZERO;
+            }
+
+            BigDecimal noReceiptPaymentAmountTotal = invoiceTotal.subtract(receiptPaymentAmountTotal);
+            if (noReceiptPaymentAmountTotal.compareTo(BigDecimal.ZERO) < 0) {
+                noReceiptPaymentAmountTotal = BigDecimal.ZERO;
+            }
+
+            salesLedger.setNoInvoiceAmountTotal(noInvoiceAmountTotal);
+            salesLedger.setInvoiceTotal(invoiceTotal);
+            salesLedger.setReceiptPaymentAmountTotal(receiptPaymentAmountTotal);
+            salesLedger.setNoReceiptAmount(noReceiptPaymentAmountTotal);
+
+            boolean hasInvoiceOperation = invoiceTotal.compareTo(BigDecimal.ZERO) > 0;
+            boolean hasReceiptOperation = receiptPaymentAmountTotal.compareTo(BigDecimal.ZERO) > 0;
+            salesLedger.setIsEdit(!(hasInvoiceOperation || hasReceiptOperation));
+        }
+
+        if (salesLedgerDto.getStatus() != null && salesLedgerDto.getStatus()) {
+            iPage.getRecords().removeIf(salesLedger ->
+                    Objects.equals(salesLedger.getNoInvoiceAmountTotal(), new BigDecimal("0.00")));
+            iPage.setTotal(iPage.getRecords().size());
+        }
+
+        return iPage;
     }
 
     @Override
@@ -1536,6 +1641,7 @@
                 Set<String> orderNos = groupOrderNos.getOrDefault(key, Collections.emptySet());
                 group.setSalesContractNo(String.join(",", orderNos));
                 group.setProductName(productName);
+                group.setSpecificationModel(specificationModel);
 
                 Map<String, SalesInvoicesDto.InvoiceItemDto> mergedItems = new LinkedHashMap<>();
                 BigDecimal groupQty = BigDecimal.ZERO;

--
Gitblit v1.9.3