From 981d05f97e9eaf273ee82f847a365b63ed6eaed1 Mon Sep 17 00:00:00 2001
From: maven <2163098428@qq.com>
Date: 星期一, 08 十二月 2025 09:50:32 +0800
Subject: [PATCH] yys  修改财务bug

---
 src/main/java/com/ruoyi/account/service/impl/AccountExpenseServiceImpl.java |  167 +++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 130 insertions(+), 37 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 7871fb4..446efca 100644
--- a/src/main/java/com/ruoyi/account/service/impl/AccountExpenseServiceImpl.java
+++ b/src/main/java/com/ruoyi/account/service/impl/AccountExpenseServiceImpl.java
@@ -9,25 +9,24 @@
 import com.ruoyi.account.dto.AccountDto2;
 import com.ruoyi.account.dto.AccountDto3;
 import com.ruoyi.account.mapper.AccountExpenseMapper;
-import com.ruoyi.account.mapper.AccountFileMapper;
 import com.ruoyi.account.mapper.AccountIncomeMapper;
 import com.ruoyi.account.pojo.AccountExpense;
 import com.ruoyi.account.pojo.AccountIncome;
 import com.ruoyi.account.service.AccountExpenseService;
-import com.ruoyi.account.service.AccountIncomeService;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.dto.DateQueryDto;
+import com.ruoyi.project.system.domain.SysDictData;
+import com.ruoyi.project.system.mapper.SysDictDataMapper;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 
 import javax.servlet.http.HttpServletResponse;
 import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.time.DayOfWeek;
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 @AllArgsConstructor
 @Service
@@ -36,6 +35,8 @@
     private AccountExpenseMapper accountExpenseMapper;
 
     private AccountIncomeMapper accountIncomeMapper;
+
+    private SysDictDataMapper sysDictDataMapper;
 
 
     //鍒嗛〉鏌ヨ
@@ -56,31 +57,70 @@
     @Override
     public AccountDto report(DateQueryDto dateQueryDto) {
         AccountDto accountDto = new AccountDto();
-        //鑾峰彇璇ユ鏃堕棿鍐呯殑鎵�鏈夋敹鍏�
-        List<AccountDto2> accountIncomes =accountIncomeMapper.report(dateQueryDto);
+
+        // 鑾峰彇璇ユ鏃堕棿鍐呯殑鎵�鏈夋敹鍏�
+        List<AccountDto2> accountIncomes = accountIncomeMapper.report(dateQueryDto);
+        if (accountIncomes == null) {
+            accountIncomes = Collections.emptyList();
+        }
 
         Long incomeNumber = accountIncomeMapper.selectCount(Wrappers.<AccountIncome>lambdaQuery()
                 .between(AccountIncome::getIncomeDate, dateQueryDto.getEntryDateStart(), dateQueryDto.getEntryDateEnd()));
         accountDto.setIncomeNumber(incomeNumber);
-        BigDecimal totalIncome = accountIncomes.stream().map(AccountDto2::getAccount).reduce(BigDecimal.ZERO, BigDecimal::add);
+
+        BigDecimal totalIncome = BigDecimal.ZERO;
+        for (AccountDto2 item : accountIncomes) {
+            if (item.getAccount() != null) {
+                totalIncome = totalIncome.add(item.getAccount());
+            }
+        }
         accountDto.setTotalIncome(totalIncome);
-        accountIncomes.stream().forEach(accountDto2 -> {
-            accountDto2.setProportion(accountDto2.getAccount().divide(totalIncome,2,BigDecimal.ROUND_HALF_UP));
-        });
+        if (totalIncome.compareTo(BigDecimal.ZERO) != 0) {
+            for (AccountDto2 item : accountIncomes) {
+                if (item.getAccount() != null) {
+                    item.setProportion(item.getAccount().divide(totalIncome, 2, RoundingMode.HALF_UP));
+                }
+            }
+        } else {
+            for (AccountDto2 item : accountIncomes) {
+                item.setProportion(BigDecimal.ZERO);
+            }
+        }
         accountDto.setIncomeType(accountIncomes);
-        //鑾峰彇璇ユ鏃堕棿鍐呯殑鎵�鏈夋敮鍑�
-        List<AccountDto2> accountExpenses =accountExpenseMapper.report(dateQueryDto);
-        accountDto.setExpenseType(accountExpenses);
+        // 鑾峰彇璇ユ鏃堕棿鍐呯殑鎵�鏈夋敮鍑�
+        List<AccountDto2> accountExpenses = accountExpenseMapper.report(dateQueryDto);
+        if (accountExpenses == null) {
+            accountExpenses = Collections.emptyList();
+        }
+
+        accountDto.setExpenseType(accountExpenses); // 绉婚櫎浜嗕笅鏂归噸澶嶇殑涓�琛�
+
         Long expenseNumber = accountExpenseMapper.selectCount(Wrappers.<AccountExpense>lambdaQuery()
                 .between(AccountExpense::getExpenseDate, dateQueryDto.getEntryDateStart(), dateQueryDto.getEntryDateEnd()));
         accountDto.setExpenseNumber(expenseNumber);
-        BigDecimal totalExpense = accountExpenses.stream().map(AccountDto2::getAccount).reduce(BigDecimal.ZERO, BigDecimal::add);
+
+        BigDecimal totalExpense = BigDecimal.ZERO;
+        for (AccountDto2 item : accountExpenses) {
+            if (item.getAccount() != null) {
+                totalExpense = totalExpense.add(item.getAccount());
+            }
+        }
         accountDto.setTotalExpense(totalExpense);
-        accountExpenses.stream().forEach(accountDto2 -> {
-            accountDto2.setProportion(accountDto2.getAccount().divide(totalExpense,2,BigDecimal.ROUND_HALF_UP));
-        });
+
+        if (totalExpense.compareTo(BigDecimal.ZERO) != 0) {
+            for (AccountDto2 item : accountExpenses) {
+                if (item.getAccount() != null) {
+                    item.setProportion(item.getAccount().divide(totalExpense, 2, RoundingMode.HALF_UP));
+                }
+            }
+        } else {
+            for (AccountDto2 item : accountExpenses) {
+                item.setProportion(BigDecimal.ZERO);
+            }
+        }
         accountDto.setExpenseType(accountExpenses);
-        //鍑�鏀跺叆
+
+        // 鍑�鏀跺叆
         BigDecimal netRevenue = totalIncome.subtract(totalExpense);
         accountDto.setNetRevenue(netRevenue);
         return accountDto;
@@ -88,26 +128,79 @@
 
     //璐㈠姟鎶ヨ〃骞存煡璇�
     @Override
-    public AccountDto3 reportExpense() {
-        AccountDto3 accountDto3 = new AccountDto3();
+    public  List<AccountDto3> reportExpense() {
+        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锛�
-        List<String> months = new ArrayList<>();
-        Map<String, List<AccountDto2>> map = new HashMap<>();
-        for (int i = 1; i <= 12; i++) {
-            months.add(i + "鏈�");
-            // 褰撴湀绗竴澶╋細骞翠唤涓哄綋鍓嶅勾锛屾湀浠戒负i锛屾棩鏈熶负1
-            LocalDate firstDay = LocalDate.of(currentYear, i, 1);
-            DateQueryDto dateQueryDto = new DateQueryDto();
-            dateQueryDto.setEntryDateStart(firstDay.format(formatter));
-            // 褰撴湀鏈�鍚庝竴澶╋細绗竴澶╃殑鏈堜唤鐨勬渶鍚庝竴澶�
-            dateQueryDto.setEntryDateEnd(firstDay.plusMonths(1).minusDays(1).format(formatter));
-            List<AccountDto2> report = accountExpenseMapper.report(dateQueryDto);
-            map.put(i + "鏈�",report);
+        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);
+                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()));
+            }
+            accountDto3.setAccount(account);//绫诲瀷
+            accountDto3s.add(accountDto3);
         }
-        accountDto3.setMonth(months);
-        accountDto3.setAccountType(map);
-        return accountDto3;
+        return accountDto3s;
+    }
+
+    @Override
+    public Map<String, List<String>> analysis() {
+        // 鑾峰彇鏈懆鐨勬椂闂磋寖鍥�
+        LocalDate startOfWeek = LocalDate.now().with(DayOfWeek.MONDAY);
+        LocalDate endOfWeek = LocalDate.now().with(DayOfWeek.SUNDAY);
+        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);
+            BigDecimal netIncome = totalIncome.subtract(totalExpense);
+            days.add(date.toString());
+            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); // 鍑�鏀跺叆
+
+        return result;
+    }
+
+    @Override
+    public AccountExpense getByInvoiceNumber(String purchaseContractNumber) {
+        return accountExpenseMapper.selectOne(Wrappers.<AccountExpense>lambdaQuery()
+                .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