From 80c45ea7934443bea5355bc6bace96ec23a58600 Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期二, 27 一月 2026 15:34:51 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_New' into dev_New

---
 src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java |  142 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 138 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java b/src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java
index a18d5c4..f006c5b 100644
--- a/src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java
+++ b/src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java
@@ -1,5 +1,6 @@
 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;
@@ -8,20 +9,38 @@
 import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
 import com.ruoyi.framework.web.controller.BaseController;
 import com.ruoyi.framework.web.domain.AjaxResult;
+import com.ruoyi.framework.web.domain.R;
 import com.ruoyi.framework.web.page.TableDataInfo;
 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 io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
 import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.CollectionUtils;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.math.BigDecimal;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.Objects;
 import java.util.stream.Collectors;
@@ -35,6 +54,8 @@
 @RestController
 @RequestMapping("/sales/ledger")
 @AllArgsConstructor
+@Api(tags = "閿�鍞彴璐�")
+@Slf4j
 public class SalesLedgerController extends BaseController {
 
     private ISalesLedgerService salesLedgerService;
@@ -43,6 +64,62 @@
 
     @Autowired
     private InvoiceLedgerMapper invoiceLedgerMapper;
+
+    @Autowired
+    private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper;
+
+    @Autowired
+    private ReceiptPaymentMapper receiptPaymentMapper;
+
+    /**
+     * 瀵煎叆閿�鍞彴璐�
+     */
+    @Log(title = "瀵煎叆閿�鍞彴璐�", businessType = BusinessType.INSERT)
+    @PostMapping("/import")
+    @ApiOperation("瀵煎叆閿�鍞彴璐�")
+    public AjaxResult importData(@RequestParam("file")
+                                 @ApiParam(value = "Excel鏂囦欢", required = true)
+                                 MultipartFile file) {
+        return salesLedgerService.importData(file);
+    }
+
+    @ApiOperation("瀵煎嚭閿�鍞彴璐︽ā鏉�")
+    @PostMapping("/exportTemplate")
+    public void exportTemplate(HttpServletResponse response) {
+        // 1. 妯℃澘鏂囦欢鍦╮esources/static涓嬬殑璺緞
+        String templatePath = "static/閿�鍞彴璐﹀鍏ユā鏉�.xlsx";
+
+        // 2. 鑾峰彇妯℃澘鏂囦欢鐨勮緭鍏ユ祦
+        try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(templatePath)) {
+            if (inputStream == null) {
+                throw new FileNotFoundException("妯℃澘鏂囦欢涓嶅瓨鍦細" + templatePath);
+            }
+
+            // 3. 璁剧疆鍝嶅簲澶达紝瑙﹀彂娴忚鍣ㄤ笅杞�
+            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+            response.setCharacterEncoding("utf-8");
+            String fileName = URLEncoder.encode("閿�鍞彴璐﹀鍏ユā鏉�.xlsx", "utf-8").replaceAll("\\+", "%20");
+            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);
+
+            // 4. 灏嗘ā鏉挎枃浠跺啓鍏ュ搷搴旇緭鍑烘祦
+            try (OutputStream outputStream = response.getOutputStream()) {
+                byte[] buffer = new byte[1024];
+                int len;
+                while ((len = inputStream.read(buffer)) > 0) {
+                    outputStream.write(buffer, 0, len);
+                }
+                outputStream.flush();
+            }
+        } catch (IOException e) {
+            log.error("瀵煎嚭閿�鍞彴璐︽ā鏉垮け璐�", e);
+            // 鑻ユā鏉挎枃浠惰鍙栧け璐ワ紝杩斿洖閿欒鎻愮ず
+            try {
+                response.getWriter().write("妯℃澘瀵煎嚭澶辫触锛�" + e.getMessage());
+            } catch (IOException ex) {
+                log.error("鍝嶅簲杈撳嚭閿欒", ex);
+            }
+        }
+    }
 
     /**
      * 鏌ヨ閿�鍞彴璐﹀垪琛�
@@ -65,10 +142,10 @@
                 if (salesLedger.getId().intValue() == invoiceLedgerDto.getSalesLedgerId()) {
                     BigDecimal noInvoiceAmountTotal = salesLedger.getContractAmount().subtract(invoiceLedgerDto.getInvoiceTotal());
                     salesLedger.setNoInvoiceAmountTotal(noInvoiceAmountTotal);
-
                 }
             }
         }
+
         return getDataTable(list);
     }
 
@@ -86,9 +163,29 @@
     @Log(title = "閿�鍞彴璐�", businessType = BusinessType.EXPORT)
     @PostMapping("/export")
     public void export(HttpServletResponse response, SalesLedgerDto salesLedgerDto) {
-        List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedgerDto);
+        Page page = new Page(-1,-1);
+        IPage<SalesLedger> salesLedgerIPage = listPage(page, salesLedgerDto);
         ExcelUtil<SalesLedger> util = new ExcelUtil<SalesLedger>(SalesLedger.class);
+        if(salesLedgerIPage == null){
+            util.exportExcel(response, new ArrayList<>(), "閿�鍞彴璐︽暟鎹�");
+            return;
+        }
+        List<SalesLedger> list = salesLedgerIPage.getRecords();
         util.exportExcel(response, list, "閿�鍞彴璐︽暟鎹�");
+    }
+
+    /**
+     * 瀵煎嚭寮�绁ㄧ櫥璁板垪琛�
+     */
+    @Log(title = "瀵煎嚭寮�绁ㄧ櫥璁板垪琛�", businessType = BusinessType.EXPORT)
+    @PostMapping("/exportOne")
+    public void exportOne(HttpServletResponse response, SalesLedgerDto salesLedgerDto) {
+        Page page = new Page();
+        page.setCurrent(-1);
+        page.setSize(-1);
+        IPage<SalesLedger> salesLedgerIPage = listPage(page, salesLedgerDto);
+        ExcelUtil<SalesLedger> util = new ExcelUtil<SalesLedger>(SalesLedger.class);
+        util.exportExcel(response, salesLedgerIPage == null ? new ArrayList<>() : salesLedgerIPage.getRecords(), "瀵煎嚭寮�绁ㄧ櫥璁板垪琛�");
     }
 
     /**
@@ -178,8 +275,18 @@
         List<Long> salesLedgerIds = iPage.getRecords().stream().map(SalesLedger::getId).collect(Collectors.toList());
         List<InvoiceLedgerDto> invoiceLedgerDtoList = invoiceLedgerMapper.invoicedTotal(salesLedgerIds);
         if(CollectionUtils.isEmpty(invoiceLedgerDtoList)){
-            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;
@@ -190,6 +297,26 @@
                     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;
                 }
             }
@@ -203,9 +330,16 @@
         if (ObjectUtils.isNotEmpty(salesLedgerDto.getStatus())) {
             if (salesLedgerDto.getStatus()) {
                 iPage.getRecords().removeIf(salesLedger -> Objects.equals(salesLedger.getNoInvoiceAmountTotal(), new BigDecimal("0.00")));
+                iPage.setTotal(iPage.getRecords().size());
             }
         }
-        iPage.setTotal(iPage.getRecords().size());
+
         return iPage;
     }
+
+    @ApiOperation("鏌ヨ閿�鍞彴璐︽秷鑰楃墿鏂欎俊鎭�")
+    @GetMapping("/getSalesLedgerWithProductsLoss")
+    public R getSalesLedgerWithProductsLoss(Long salesLedgerId) {
+        return R.ok(salesLedgerService.getSalesLedgerWithProductsLoss(salesLedgerId));
+    }
 }

--
Gitblit v1.9.3