From a0f1f5629bc1c6338d7d95aa01b31cfd671bd398 Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期五, 16 一月 2026 18:02:31 +0800
Subject: [PATCH] yys 财务报表优化
---
src/main/java/com/ruoyi/account/service/impl/AccountExpenseServiceImpl.java | 140 +++++++++++++++++++++++++++++++++++-----------
1 files changed, 106 insertions(+), 34 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 a7ab8cf..b7791a9 100644
--- a/src/main/java/com/ruoyi/account/service/impl/AccountExpenseServiceImpl.java
+++ b/src/main/java/com/ruoyi/account/service/impl/AccountExpenseServiceImpl.java
@@ -8,6 +8,7 @@
import com.ruoyi.account.dto.AccountDto;
import com.ruoyi.account.dto.AccountDto2;
import com.ruoyi.account.dto.AccountDto3;
+import com.ruoyi.account.dto.ReportDateDto;
import com.ruoyi.account.mapper.AccountExpenseMapper;
import com.ruoyi.account.mapper.AccountIncomeMapper;
import com.ruoyi.account.pojo.AccountExpense;
@@ -24,8 +25,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
@@ -88,24 +91,38 @@
//璐㈠姟鎶ヨ〃骞存煡璇�
@Override
- public List<AccountDto3> reportExpense() {
+ public List<AccountDto3> reportExpense(ReportDateDto reportDateDto) {
List<AccountDto3> accountDto3s = new ArrayList<>();
//鍏堟煡璇㈡敹鍏ョ被鍨嬫湁鍝簺
List<SysDictData> incomeTypes = sysDictDataMapper.selectDictDataByType("expense_types");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
- int currentYear = LocalDate.now().getYear(); // 鑾峰彇褰撳墠骞翠唤锛堝2025锛�
+// int currentYear = LocalDate.now().getYear(); // 鑾峰彇褰撳墠骞翠唤锛堝2025锛�
for (SysDictData incomeType : incomeTypes) {
AccountDto3 accountDto3 = new AccountDto3();
accountDto3.setTypeName(incomeType.getDictLabel());//绫诲瀷
List<BigDecimal> account=new ArrayList<>();
- for (int i = 1; i <= 12; i++) {
- // 褰撴湀绗竴澶╋細骞翠唤涓哄綋鍓嶅勾锛屾湀浠戒负i锛屾棩鏈熶负1
- LocalDate firstDay = LocalDate.of(currentYear, i, 1);
+ LocalDate startDate = reportDateDto.getEntryDateStart();
+ LocalDate endDate = reportDateDto.getEntryDateEnd();
+
+ // 鍒濆鍖栧惊鐜彉閲忎负璧峰鏃ユ湡
+ LocalDate currentDate = startDate;
+
+ // 寰幆锛氬綋鍓嶆棩鏈熶笉瓒呰繃缁撴潫鏃ユ湡鏃剁户缁�
+ while (!currentDate.isAfter(endDate)) {
+ // 褰撴湀绗竴澶�
+ LocalDate firstDay = currentDate.withDayOfMonth(1);
DateQueryDto dateQueryDto = new DateQueryDto();
dateQueryDto.setEntryDateStart(firstDay.format(formatter));
- // 褰撴湀鏈�鍚庝竴澶╋細绗竴澶╃殑鏈堜唤鐨勬渶鍚庝竴澶�
- dateQueryDto.setEntryDateEnd(firstDay.plusMonths(1).minusDays(1).format(formatter));
- account.add(accountExpenseMapper.report1(dateQueryDto,incomeType.getDictValue()));
+
+ // 褰撴湀鏈�鍚庝竴澶�
+ LocalDate lastDay = firstDay.plusMonths(1).minusDays(1);
+ dateQueryDto.setEntryDateEnd(lastDay.format(formatter));
+
+ // 绱姞鏁版嵁
+ account.add(accountExpenseMapper.report1(dateQueryDto, incomeType.getDictValue()));
+
+ // 鏈堜唤鍔犱竴锛堣嚜鍔ㄥ鐞嗚法骞达紝姣斿12鏈堝姞1涓湀浼氬彉鎴愪笅涓�骞�1鏈堬級
+ currentDate = currentDate.plusMonths(1);
}
accountDto3.setAccount(account);//绫诲瀷
accountDto3s.add(accountDto3);
@@ -115,38 +132,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;
}
@@ -157,5 +223,11 @@
.eq(AccountExpense::getInvoiceNumber, purchaseContractNumber));
}
+ @Override
+ public List<AccountExpense> getByInvoiceNumberList(String purchaseContractNumber) {
+ return accountExpenseMapper.selectList(Wrappers.<AccountExpense>lambdaQuery()
+ .eq(AccountExpense::getInvoiceNumber, purchaseContractNumber));
+ }
+
}
--
Gitblit v1.9.3