From 366b67926809852c1090f14c5a24fe00109a81b5 Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期一, 28 七月 2025 17:59:52 +0800
Subject: [PATCH] yys  首页接口开发

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java |   92 +++++++++++++++++++++++++++++-----------------
 1 files changed, 58 insertions(+), 34 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 b6c43ed..fa807c9 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -204,53 +204,77 @@
     }
 
     @Override
-    public List<MonthlyAmountDto> getAmountHalfYear() {
+    public List<MonthlyAmountDto> getAmountHalfYear(Integer type) {
         LocalDate now = LocalDate.now();
-        YearMonth currentMonth = YearMonth.from(now);
+        LocalDateTime currentDateTime = LocalDateTime.now();
 
-        List<MonthlyAmountDto> monthlyAmounts = new ArrayList<>();
+        // 鏍规嵁 type 纭畾鏌ヨ鐨勬椂闂撮棿闅旓紙澶╂暟锛夊拰鎬绘煡璇㈠ぉ鏁�
+        int daysPerPeriod;
+        int totalDays;
+        switch (type) {
+            case 1:
+                daysPerPeriod = 5;   // 姣�5澶╂煡涓�娆�
+                totalDays = 30;       // 6娆� 脳 5澶� = 30澶�
+                break;
+            case 2:
+                daysPerPeriod = 15;    // 姣�15澶╂煡涓�娆�
+                totalDays = 90;       // 6娆� 脳 15澶� = 90澶�
+                break;
+            case 3:
+                daysPerPeriod = 30;   // 姣�30澶╂煡涓�娆�
+                totalDays = 180;      // 6娆� 脳 30澶� = 180澶�
+                break;
+            default:
+                throw new IllegalArgumentException("Invalid type value: " + type);
+        }
 
+        List<MonthlyAmountDto> result = new ArrayList<>();
+
+        // 寰幆6娆★紝姣忔鏌ヨ涓�涓椂闂存鐨勬暟鎹�
         for (int i = 0; i < 6; i++) {
-            YearMonth targetMonth = currentMonth.minusMonths(i);
-            LocalDate firstDayOfMonth = targetMonth.atDay(1);
-            LocalDate firstDayOfNextMonth = targetMonth.plusMonths(1).atDay(1);
+            // 璁$畻褰撳墠鏃堕棿娈电殑璧峰鍜岀粨鏉熸椂闂�
+            LocalDateTime endTime = currentDateTime.minusDays(i * daysPerPeriod);
+            LocalDateTime startTime = endTime.minusDays(daysPerPeriod);
 
-            LocalDateTime startOfMonth = firstDayOfMonth.atStartOfDay();
-            LocalDateTime startOfNextMonth = firstDayOfNextMonth.atStartOfDay();
+            // 鏌ヨ鍥炴閲戦
+            LambdaQueryWrapper<ReceiptPayment> receiptPaymentQuery = new LambdaQueryWrapper<>();
+            receiptPaymentQuery
+                    .ge(ReceiptPayment::getCreateTime, startTime)
+                    .lt(ReceiptPayment::getCreateTime, endTime);
+            List<ReceiptPayment> receiptPayments = receiptPaymentMapper.selectList(receiptPaymentQuery);
 
-            LambdaQueryWrapper<ReceiptPayment> receiptPaymentLambdaQueryWrapper = new LambdaQueryWrapper<>();
-            receiptPaymentLambdaQueryWrapper.ge(ReceiptPayment::getCreateTime, startOfMonth)
-                    .lt(ReceiptPayment::getCreateTime, startOfNextMonth);
+            // 鏌ヨ寮�绁ㄩ噾棰�
+            LambdaQueryWrapper<InvoiceLedger> invoiceLedgerQuery = new LambdaQueryWrapper<>();
+            invoiceLedgerQuery
+                    .ge(InvoiceLedger::getCreateTime, startTime)
+                    .lt(InvoiceLedger::getCreateTime, endTime);
+            List<InvoiceLedger> invoiceLedgers = invoiceLedgerMapper.selectList(invoiceLedgerQuery);
 
-            LambdaQueryWrapper<InvoiceLedger> invoiceLedgerLambdaQueryWrapper = new LambdaQueryWrapper<>();
-            invoiceLedgerLambdaQueryWrapper.ge(InvoiceLedger::getCreateTime, startOfMonth)
-                    .lt(InvoiceLedger::getCreateTime, startOfNextMonth);
-
-            // 鑾峰彇鍥炴閲戦
-            List<ReceiptPayment> receiptPaymentList = receiptPaymentMapper.selectList(receiptPaymentLambdaQueryWrapper);
-            //寮�绁ㄩ噾棰�
-            List<InvoiceLedger> invoiceLedgerList = invoiceLedgerMapper.selectList(invoiceLedgerLambdaQueryWrapper);
-
-            // 浣跨敤 Stream 姹傚拰
-            BigDecimal invoiceAmount = invoiceLedgerList.stream()
-                    .map(InvoiceLedger::getInvoiceTotal)
-                    .filter(Objects::nonNull)
-                    .reduce(BigDecimal.ZERO, BigDecimal::add);
-
-            BigDecimal receiptAmount = receiptPaymentList.stream()
+            // 璁$畻鍥炴鎬婚
+            BigDecimal receiptAmount = receiptPayments.stream()
                     .map(ReceiptPayment::getReceiptPaymentAmount)
                     .filter(Objects::nonNull)
                     .reduce(BigDecimal.ZERO, BigDecimal::add);
 
-            MonthlyAmountDto monthlyAmount = new MonthlyAmountDto();
-            monthlyAmount.setMonth(targetMonth.format(DateTimeFormatter.ofPattern("yyyy-MM")));
-            monthlyAmount.setInvoiceAmount(invoiceAmount);
-            monthlyAmount.setReceiptAmount(receiptAmount);
+            // 璁$畻寮�绁ㄦ�婚
+            BigDecimal invoiceAmount = invoiceLedgers.stream()
+                    .map(InvoiceLedger::getInvoiceTotal)
+                    .filter(Objects::nonNull)
+                    .reduce(BigDecimal.ZERO, BigDecimal::add);
 
-            monthlyAmounts.add(monthlyAmount);
+            // 鏋勯�犺繑鍥炵殑 DTO
+            MonthlyAmountDto dto = new MonthlyAmountDto();
+            dto.setMonth(startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " ~ " +
+                    endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+            dto.setReceiptAmount(receiptAmount);
+            dto.setInvoiceAmount(invoiceAmount);
+
+            result.add(dto);
         }
-        Collections.reverse(monthlyAmounts);
-        return monthlyAmounts;
+
+        // 鍙嶈浆鍒楄〃锛屼娇鏃堕棿椤哄簭浠庢棭鍒版櫄
+        Collections.reverse(result);
+        return result;
     }
 
     @Override

--
Gitblit v1.9.3