buhuazhen
6 天以前 161c5e12e3d7f5e2f4345ed3609fe05037ea2b23
src/main/java/com/ruoyi/account/service/impl/AccountExpenseServiceImpl.java
@@ -155,35 +155,44 @@
    @Override
    public Map<String, List<String>> analysis() {
        // 获取本周的时间范围
        LocalDate startOfWeek = LocalDate.now().with(DayOfWeek.MONDAY);
        LocalDate endOfWeek = LocalDate.now().with(DayOfWeek.SUNDAY);
        // 统计最近12个月的数据
        LocalDate today = LocalDate.now();
        Map<String, List<String>> result = new HashMap<>();
        List<String> days = new ArrayList<>();
        List<String> months = 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)) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        // 从当前月份往前推12个月
        for (int i = 0; i < 12; i++) {
            LocalDate monthDate = today.minusMonths(i);
            LocalDate startOfMonth = monthDate.withDayOfMonth(1);
            LocalDate endOfMonth = monthDate.withDayOfMonth(monthDate.lengthOfMonth());
            // 查询该月的总收入
            BigDecimal totalIncome = accountIncomeMapper.selectList(Wrappers.<AccountIncome>lambdaQuery()
                    .eq(AccountIncome::getInputTime, date.toString()))
                    .between(AccountIncome::getInputTime, startOfMonth.toString(), endOfMonth.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()))
                    .between(AccountExpense::getInputTime, startOfMonth.toString(), endOfMonth.toString()))
                    .stream()
                    .map(AccountExpense::getExpenseMoney)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal netIncome = totalIncome.subtract(totalExpense);
            days.add(date.toString());
            months.add(monthDate.format(formatter));
            totalIncomeList.add(totalIncome.toString());
            totalExpenseList.add(totalExpense.toString());
            netIncomeList.add(netIncome.toString());
        }
        result.put("days", days);  //  天
        result.put("months", months);  // 月份
        result.put("totalIncome", totalIncomeList); // 收入
        result.put("totalExpense", totalExpenseList); // 支出
        result.put("netIncome", netIncomeList); // 净收入