From d0eaee38a59ec4f2600bfec41b3db5f80500af32 Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期五, 12 十二月 2025 16:16:01 +0800
Subject: [PATCH] yys

---
 src/main/java/com/ruoyi/account/service/impl/AccountExpenseServiceImpl.java |  103 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 77 insertions(+), 26 deletions(-)

diff --git a/src/main/java/com/ruoyi/account/service/impl/AccountExpenseServiceImpl.java b/src/main/java/com/ruoyi/account/service/impl/AccountExpenseServiceImpl.java
index a3c775f..6017793 100644
--- a/src/main/java/com/ruoyi/account/service/impl/AccountExpenseServiceImpl.java
+++ b/src/main/java/com/ruoyi/account/service/impl/AccountExpenseServiceImpl.java
@@ -24,8 +24,10 @@
 import java.math.BigDecimal;
 import java.time.DayOfWeek;
 import java.time.LocalDate;
+import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
+import java.util.stream.Collectors;
 
 @AllArgsConstructor
 @Service
@@ -115,38 +117,87 @@
 
     @Override
     public Map<String, List<String>> analysis() {
-        // 鑾峰彇鏈懆鐨勬椂闂磋寖鍥�
-        LocalDate startOfWeek = LocalDate.now().with(DayOfWeek.MONDAY);
-        LocalDate endOfWeek = LocalDate.now().with(DayOfWeek.SUNDAY);
+        // 鑾峰彇鏈�杩戝洓涓湀锛堝綋鍓嶆湀 + 鍓�3涓湀锛夌殑鏃堕棿鑼冨洿
+        LocalDate today = LocalDate.now();
+        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM"); // 骞存湀鏍煎紡鍖栧櫒
         Map<String, List<String>> result = new HashMap<>();
-        List<String> days = new ArrayList<>();
-        List<String> totalIncomeList = new ArrayList<>();
-        List<String> totalExpenseList = new ArrayList<>();
-        List<String> netIncomeList = new ArrayList<>();
-        // 鏍规嵁鏃堕棿鑼冨洿寰幆鏌ヨ姣忎竴澶╃殑鎬绘敹鍏ワ紝鎬绘敮鍑�,鍑�鏀跺叆锛堟�绘敹鍏�-鎬绘敮鍑猴級
-        for (LocalDate date = startOfWeek; date.isBefore(endOfWeek) || date.isEqual(endOfWeek); date = date.plusDays(1)) {
-            BigDecimal totalIncome = accountIncomeMapper.selectList(Wrappers.<AccountIncome>lambdaQuery()
-                    .eq(AccountIncome::getInputTime, date.toString()))
-                    .stream()
-                    .map(AccountIncome::getIncomeMoney)
-                    .filter(Objects::nonNull)
-                    .reduce(BigDecimal.ZERO, BigDecimal::add);
-            BigDecimal totalExpense = accountExpenseMapper.selectList(Wrappers.<AccountExpense>lambdaQuery()
-                    .eq(AccountExpense::getInputTime, date.toString()))
-                    .stream()
-                    .map(AccountExpense::getExpenseMoney)
-                    .filter(Objects::nonNull)
-                    .reduce(BigDecimal.ZERO, BigDecimal::add);
+        List<String> months = new ArrayList<>(); // 瀛樺偍骞存湀锛堝 2025-12銆�2025-11锛�
+        List<String> totalIncomeList = new ArrayList<>(); // 姣忔湀鎬绘敹鍏�
+        List<String> totalExpenseList = new ArrayList<>(); // 姣忔湀鎬绘敮鍑�
+        List<String> netIncomeList = new ArrayList<>(); // 姣忔湀鍑�鏀跺叆锛堟敹鍏�-鏀嚭锛�
+
+        // 姝ラ1锛氳绠楄繎4涓湀鐨勫勾鏈堝垪琛紙褰撳墠鏈堛�佸墠1鏈堛�佸墠2鏈堛�佸墠3鏈堬級
+        List<String> targetMonths = new ArrayList<>();
+        for (int i = 0; i < 4; i++) {
+            LocalDate currentMonth = today.minusMonths(i);
+            String monthStr = currentMonth.format(monthFormatter);
+            targetMonths.add(monthStr);
+        }
+        // 鍙嶈浆鍒楄〃锛岀‘淇濋『搴忎负銆屽墠3鏈� 鈫� 褰撳墠鏈堛�嶏紙鍙�夛紝鎸夐渶姹傝皟鏁撮『搴忥級
+        Collections.reverse(targetMonths);
+
+        // 姝ラ2锛氫竴娆℃�ф煡璇㈣繎4涓湀鎵�鏈夋敹鍏ユ暟鎹紝鎸夆�滃勾鏈堚�濆垎缁勬眹鎬�
+        LocalDate fourMonthsAgo = today.minusMonths(3).withDayOfMonth(1); // 杩�4涓湀璧峰鏃ワ紙鍓�3鏈�1鍙凤級
+        LocalDate currentMonthEnd = today.withDayOfMonth(today.lengthOfMonth()); // 褰撳墠鏈堢粨鏉熸棩
+        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
+        // 鏌ヨ杩�4涓湀鎵�鏈夋敹鍏�
+        List<AccountIncome> allIncomes = accountIncomeMapper.selectList(
+                Wrappers.<AccountIncome>lambdaQuery()
+                        .ge(AccountIncome::getIncomeDate, fourMonthsAgo.toString()) // 澶т簬绛変簬璧峰鏃�
+                        .le(AccountIncome::getIncomeDate, currentMonthEnd.toString()) // 灏忎簬绛変簬缁撴潫鏃�
+        );
+
+        // 鏀跺叆鎸夆�滃勾鏈堚�濆垎缁勬眹鎬伙紙key锛氬勾鏈堝瓧绗︿覆锛寁alue锛氬綋鏈堟�绘敹鍏ワ級
+        Map<String, BigDecimal> monthlyIncomeMap = allIncomes.stream()
+                .filter(income -> income.getIncomeMoney() != null) // 杩囨护绌洪噾棰�
+                .collect(Collectors.groupingBy(
+                        income -> {
+                            // 灏嗚緭鍏ユ椂闂达紙瀛楃涓诧級杞崲涓篖ocalDate锛屽啀鏍煎紡鍖栦负骞存湀
+                            LocalDate inputDate = income.getIncomeDate().toInstant().atZone(zoneId).toLocalDate();
+                            return inputDate.format(monthFormatter);
+                        },
+                        Collectors.reducing(BigDecimal.ZERO, AccountIncome::getIncomeMoney, BigDecimal::add)
+                ));
+
+        // 姝ラ3锛氫竴娆℃�ф煡璇㈣繎4涓湀鎵�鏈夋敮鍑烘暟鎹紝鎸夆�滃勾鏈堚�濆垎缁勬眹鎬�
+        List<AccountExpense> allExpenses = accountExpenseMapper.selectList(
+                Wrappers.<AccountExpense>lambdaQuery()
+                        .ge(AccountExpense::getExpenseDate, fourMonthsAgo.toString())
+                        .le(AccountExpense::getExpenseDate, currentMonthEnd.toString())
+        );
+
+        // 鏀嚭鎸夆�滃勾鏈堚�濆垎缁勬眹鎬�
+        Map<String, BigDecimal> monthlyExpenseMap = allExpenses.stream()
+                .filter(expense -> expense.getExpenseMoney() != null) // 杩囨护绌洪噾棰�
+                .collect(Collectors.groupingBy(
+                        expense -> {
+                            LocalDate inputDate = expense.getExpenseDate().toInstant().atZone(zoneId).toLocalDate();
+                            return inputDate.format(monthFormatter);
+                        },
+                        Collectors.reducing(BigDecimal.ZERO, AccountExpense::getExpenseMoney, BigDecimal::add)
+                ));
+
+        // 姝ラ4锛氬惊鐜�4涓洰鏍囨湀浠斤紝濉厖缁熻鏁版嵁锛堟棤鏁版嵁鏃堕粯璁や负0锛�
+        for (String month : targetMonths) {
+            // 褰撴湀鎬绘敹鍏ワ紙鏃犳暟鎹垯涓�0锛�
+            BigDecimal totalIncome = monthlyIncomeMap.getOrDefault(month, BigDecimal.ZERO);
+            // 褰撴湀鎬绘敮鍑猴紙鏃犳暟鎹垯涓�0锛�
+            BigDecimal totalExpense = monthlyExpenseMap.getOrDefault(month, BigDecimal.ZERO);
+            // 褰撴湀鍑�鏀跺叆锛堟敹鍏� - 鏀嚭锛�
             BigDecimal netIncome = totalIncome.subtract(totalExpense);
-            days.add(date.toString());
+
+            // 濉厖鍒楄〃
+            months.add(month);
             totalIncomeList.add(totalIncome.toString());
             totalExpenseList.add(totalExpense.toString());
             netIncomeList.add(netIncome.toString());
         }
-        result.put("days", days);  //  澶�
-        result.put("totalIncome", totalIncomeList); // 鏀跺叆
-        result.put("totalExpense", totalExpenseList); // 鏀嚭
-        result.put("netIncome", netIncomeList); // 鍑�鏀跺叆
+
+        // 缁勮缁撴灉
+        result.put("days", months); // 骞存湀锛堝 ["2025-09", "2025-10", "2025-11", "2025-12"]锛�
+        result.put("totalIncome", totalIncomeList); // 瀵瑰簲鏈堜唤鎬绘敹鍏�
+        result.put("totalExpense", totalExpenseList); // 瀵瑰簲鏈堜唤鎬绘敮鍑�
+        result.put("netIncome", netIncomeList); // 瀵瑰簲鏈堜唤鍑�鏀跺叆
 
         return result;
     }

--
Gitblit v1.9.3