gongchunyi
7 小时以前 c34f874753481fc5105c86f9bcf1eb21efcab92d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.ruoyi.account.service.impl;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.dto.AccountDto3;
import com.ruoyi.account.dto.ReportDateDto;
import com.ruoyi.account.mapper.AccountIncomeMapper;
import com.ruoyi.account.pojo.AccountIncome;
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.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
 
@AllArgsConstructor
@Service
public class AccountIncomeServiceImpl extends ServiceImpl<AccountIncomeMapper, AccountIncome> implements AccountIncomeService {
 
    private AccountIncomeMapper accountIncomeMapper;
 
    private SysDictDataMapper sysDictDataMapper;
 
 
    //分页查询
    @Override
    public IPage<AccountIncome> accountIncomeListPage(Page page, AccountIncome accountIncome) {
        resolveIncomeMethodLabelFilter(accountIncome);
        return accountIncomeMapper.accountIncomeListPage(page,accountIncome);
    }
 
    private void resolveIncomeMethodLabelFilter(AccountIncome accountIncome) {
        if (accountIncome == null) {
            return;
        }
        if (accountIncome.getIncomeMethodLabel() == null || accountIncome.getIncomeMethodLabel().trim().isEmpty()) {
            return;
        }
        String targetLabel = accountIncome.getIncomeMethodLabel().trim();
        List<String> paymentMethodList = selectDictValuesByLabel("payment_methods", targetLabel);
        List<String> receiptPaymentMethodList = selectDictValuesByLabel("receipt_payment_type", targetLabel);
        if (paymentMethodList.isEmpty()) {
            paymentMethodList = Collections.singletonList("__NO_MATCH__");
        }
        if (receiptPaymentMethodList.isEmpty()) {
            receiptPaymentMethodList = Collections.singletonList("__NO_MATCH__");
        }
        accountIncome.setPaymentMethodList(paymentMethodList);
        accountIncome.setReceiptPaymentMethodList(receiptPaymentMethodList);
        accountIncome.setIncomeMethod(null);
    }
 
    private List<String> selectDictValuesByLabel(String dictType, String label) {
        return sysDictDataMapper.selectDictDataByType(dictType).stream()
                .filter(item -> label.equals(item.getDictLabel()))
                .map(SysDictData::getDictValue)
                .filter(v -> v != null && !v.trim().isEmpty())
                .distinct()
                .collect(Collectors.toList());
    }
 
    //导出
    @Override
    public void accountIncomeExport(HttpServletResponse response, AccountIncome accountIncome) {
        List<AccountIncome> accountIncomes =accountIncomeMapper.accountIncomeExport(accountIncome);
        ExcelUtil<AccountIncome> util = new ExcelUtil<AccountIncome>(AccountIncome.class);
        util.exportExcel(response, accountIncomes, "收入管理导出");
    }
 
    //财务报表年查询
    @Override
    public List<AccountDto3> reportIncome(ReportDateDto reportDateDto) {
        List<AccountDto3> accountDto3s = new ArrayList<>();
        //先查询收入类型有哪些
        List<SysDictData> incomeTypes = sysDictDataMapper.selectDictDataByType("income_types");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        int currentYear = LocalDate.now().getYear(); // 获取当前年份(如2025)
        for (SysDictData incomeType : incomeTypes) {
            AccountDto3 accountDto3 = new AccountDto3();
            accountDto3.setTypeName(incomeType.getDictLabel());//类型
            List<BigDecimal> account=new ArrayList<>();
            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));
 
                // 当月最后一天
                LocalDate lastDay = firstDay.plusMonths(1).minusDays(1);
                dateQueryDto.setEntryDateEnd(lastDay.format(formatter));
 
                // 累加数据
                account.add(accountIncomeMapper.report1(dateQueryDto, incomeType.getDictValue()));
 
                // 月份加一(自动处理跨年,比如12月加1个月会变成下一年1月)
                currentDate = currentDate.plusMonths(1);
            }
            accountDto3.setAccount(account);//类型
            accountDto3s.add(accountDto3);
        }
        return accountDto3s;
    }
 
    @Override
    public AccountIncome getByInvoiceNumber(String purchaseContractNumber) {
        AccountIncome accountIncome = accountIncomeMapper.selectOne(new LambdaQueryWrapper<AccountIncome>()
                .eq(AccountIncome::getInvoiceNumber, purchaseContractNumber));
        return accountIncome;
    }
 
    @Override
    public List<AccountIncome> getByInvoiceNumberList(String purchaseContractNumber) {
        return accountIncomeMapper.selectList(new LambdaQueryWrapper<AccountIncome>()
                .eq(AccountIncome::getInvoiceNumber, purchaseContractNumber));
    }
}