liding
5 天以前 db628292cb9b69b901b44b146ac88c5f68e38389
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
package com.ruoyi.account.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.mapper.FinanceAccountMapper;
import com.ruoyi.account.mapper.FinanceCashflowPlanMapper;
import com.ruoyi.account.pojo.FinanceCashflowPlan;
import com.ruoyi.account.service.FinanceCashflowPlanService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class FinanceCashflowPlanServiceImpl extends ServiceImpl<FinanceCashflowPlanMapper, FinanceCashflowPlan>
        implements FinanceCashflowPlanService {
 
    @Resource
    private FinanceAccountMapper financeAccountMapper;
 
    @Override
    public Map<String, Object> forecast(Integer days, BigDecimal safetyLine) {
        int window = days == null ? 30 : Math.max(1, Math.min(days, 30));
        BigDecimal threshold = safetyLine == null ? financeAccountMapper.selectTotalSafetyLine() : safetyLine;
        if (threshold == null) threshold = BigDecimal.ZERO;
        LocalDate today = LocalDate.now();
        LocalDate lastDay = today.plusDays(window - 1L);
        Date begin = Date.from(today.atStartOfDay(ZoneId.systemDefault()).toInstant());
        Date end = Date.from(lastDay.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
 
        List<FinanceCashflowPlan> plans = list(new QueryWrapper<FinanceCashflowPlan>()
                .eq("status", "0").ge("plan_date", begin).lt("plan_date", end).orderByAsc("plan_date"));
        BigDecimal currentBalance = financeAccountMapper.selectTotalBalance();
        if (currentBalance == null) currentBalance = BigDecimal.ZERO;
 
        List<Map<String, Object>> points = new ArrayList<>();
        BigDecimal balance = currentBalance;
        for (int offset = 0; offset < window; offset++) {
            LocalDate date = today.plusDays(offset);
            BigDecimal income = BigDecimal.ZERO;
            BigDecimal expense = BigDecimal.ZERO;
            for (FinanceCashflowPlan plan : plans) {
                LocalDate planDate = plan.getPlanDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                if (!date.equals(planDate)) continue;
                BigDecimal amount = plan.getExpectedAmount() == null ? BigDecimal.ZERO : plan.getExpectedAmount();
                if ("0".equals(plan.getPlanType())) income = income.add(amount); else expense = expense.add(amount);
            }
            balance = balance.add(income).subtract(expense);
            Map<String, Object> point = new LinkedHashMap<>();
            point.put("date", date.toString());
            point.put("income", income);
            point.put("expense", expense);
            point.put("balance", balance);
            point.put("signal", balance.signum() < 0 ? "gap" : (balance.compareTo(threshold) > 0 ? "surplus" : "safe"));
            points.add(point);
        }
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("currentBalance", currentBalance);
        result.put("safetyLine", threshold);
        result.put("days", window);
        result.put("points", points);
        result.put("finalBalance", balance);
        result.put("signal", balance.signum() < 0 ? "gap" : (balance.compareTo(threshold) > 0 ? "surplus" : "safe"));
        return result;
    }
}