mitao
2025-03-20 16dc6f5f8bf29162b1c491d858c56fce34d97515
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
package com.ruoyi.system.service.impl;
 
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.model.TBill;
import com.ruoyi.system.model.TContract;
import com.ruoyi.system.model.THouse;
import com.ruoyi.system.service.ITStreetService;
import com.ruoyi.system.service.TBillService;
import com.ruoyi.system.service.TContractService;
import com.ruoyi.system.service.THouseService;
import com.ruoyi.system.vo.ScreenRentIncomeTrendVO;
import com.ruoyi.system.vo.ScreenRentRankVO;
import com.ruoyi.system.vo.ScreenTopStaticsDataVO;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Calendar;
import java.util.ArrayList;
 
/**
 * @author mitao
 * @date 2025/3/19
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ScreenService {
    private final THouseService tHouseService;
    private final TContractService tContractService;
    private final TBillService tBillService;
    private final ITStreetService tStreetService;
    /**
     * 获取顶部统计数据
     * @return
     */
    public ScreenTopStaticsDataVO getTopStaticsData() {
        ScreenTopStaticsDataVO vo = new ScreenTopStaticsDataVO();
        //房屋总面积
        List<THouse> houseList = tHouseService.list();
        Double totalArea = houseList.stream().map(item -> Double.parseDouble(item.getHouseArea())).reduce(0D, Double::sum);
        vo.setHouseTotalArea(totalArea);
        //已出租面积
        Double totalRentedArea = houseList.stream().filter(item -> !item.getLeaseStatus().equals("1"))
                .map(item -> Double.parseDouble(item.getHouseArea())).reduce(0D, Double::sum);
        vo.setHouseRentedArea(totalRentedArea);
        //总计应收租金
        List<TBill> billList = tBillService.list();
        BigDecimal totalReceivableRent = billList.stream().filter(item -> !item.getPayFeesStatus().equals("5"))
                .map(TBill::getPayableFeesMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        vo.setTotalReceivableRent(totalReceivableRent);
        //总计已收租金
        BigDecimal totalReceivedRent = billList.stream().map(TBill::getPayFeesMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        vo.setTotalReceivedRent(totalReceivedRent);
        //本月新增租户数
        Integer newTenantCount = tContractService.getCurrentMonthRentCount();
        vo.setNewTenantCount(newTenantCount);
        //总计租户数 系统租户列表里有生效合同绑定的租户总数。
        Long count = tContractService.lambdaQuery().in(TContract::getStatus, "4", "5", "6", "7", "8", "9").groupBy(TContract::getTenantId).count();
        vo.setTotalTenantCount(count.intValue());
        Map<String, Date> quarterDate = DateUtils.getQuarterDate(new Date());
        Date first = quarterDate.get("first");
        Date last = quarterDate.get("last");
        List<TBill> currentQuarterBillList = tBillService.lambdaQuery().between(TBill::getPayableFeesTime, first, last).list();
        //本季度已交租金
        BigDecimal totalRentPaid = currentQuarterBillList.stream().map(TBill::getPayFeesMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        vo.setTotalRentPaid(totalRentPaid);
        //本季度应交租金
        BigDecimal totalRentShould = currentQuarterBillList.stream().filter(item -> !item.getPayFeesStatus().equals("5"))
                .map(TBill::getPayableFeesMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        vo.setTotalRentShould(totalRentShould);
        return vo;
    }
 
    /**
     * 区域租金排名
     * @return
     */
    public List<ScreenRentRankVO> streetRentRank() {
        return  tBillService.getStreetRentRank();
    }
 
    /**
     * 租金收入趋势
     * @return
     */
    public ScreenRentIncomeTrendVO rentIncomeTrend() {
        ScreenRentIncomeTrendVO vo = new ScreenRentIncomeTrendVO();
        
        // 获取当前日期
        Date currentDate = new Date();
        List<String> quarterLabels = new ArrayList<>();
        List<BigDecimal> incomeData = new ArrayList<>();
        
        // 获取最近7个季度的数据
        for (int i = 6; i >= 0; i--) {
            // 计算对应季度的起止时间
            Date targetDate = DateUtils.addMonths(currentDate, -3 * i);
            Map<String, Date> quarterDate = DateUtils.getQuarterDate(targetDate);
            Date quarterStart = quarterDate.get("first");
            Date quarterEnd = quarterDate.get("last");
            
            // 获取该季度的账单数据并计算总和
            BigDecimal quarterIncome = tBillService.lambdaQuery()
                .between(TBill::getPayableFeesTime, quarterStart, quarterEnd)
                .list()
                .stream()
                .map(TBill::getPayFeesMoney)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
            
            // 生成季度标签 (格式: YY-MM月)
            Calendar cal = Calendar.getInstance();
            cal.setTime(quarterEnd);
            String label = String.format("%02d-%d月",
                cal.get(Calendar.YEAR) % 100,
                cal.get(Calendar.MONTH) + 1);
            
            quarterLabels.add(label);
            incomeData.add(quarterIncome);
        }
        
        vo.setQuarters(quarterLabels);
        vo.setIncomeData(incomeData);
        return vo;
    }
}