|  |  |  | 
|---|
|  |  |  | package com.ruoyi.system.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.ruoyi.common.core.domain.R; | 
|---|
|  |  |  | import com.ruoyi.common.utils.DateUtils; | 
|---|
|  |  |  | import com.ruoyi.common.utils.SecurityUtils; | 
|---|
|  |  |  | import com.ruoyi.system.model.TBill; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.ruoyi.system.vo.ScreenRentIncomeTrendVO; | 
|---|
|  |  |  | import com.ruoyi.system.vo.ScreenRentRankVO; | 
|---|
|  |  |  | import com.ruoyi.system.vo.ScreenTopStaticsDataVO; | 
|---|
|  |  |  | import com.ruoyi.system.vo.TenantCountTrendVO; | 
|---|
|  |  |  | import lombok.RequiredArgsConstructor; | 
|---|
|  |  |  | import org.springframework.context.annotation.Lazy; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | 
|---|
|  |  |  | private final THouseService tHouseService; | 
|---|
|  |  |  | private final TContractService tContractService; | 
|---|
|  |  |  | private final TBillService tBillService; | 
|---|
|  |  |  | private final TContractService contractService; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取顶部统计数据 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | 
|---|
|  |  |  | .filter(item -> !item.getPayFeesStatus().equals("5")) | 
|---|
|  |  |  | .map(TBill::getPayableFeesMoney) | 
|---|
|  |  |  | .reduce(BigDecimal.ZERO, BigDecimal::add) | 
|---|
|  |  |  | .divide(new BigDecimal("10000"),2, RoundingMode.HALF_UP); | 
|---|
|  |  |  | .divide(new BigDecimal("10000"),2, RoundingMode.DOWN); | 
|---|
|  |  |  | vo.setTotalReceivableRent(totalReceivableRent); | 
|---|
|  |  |  | //总计已收租金 | 
|---|
|  |  |  | BigDecimal totalReceivedRent = billList.stream() | 
|---|
|  |  |  | .map(TBill::getPayFeesMoney) | 
|---|
|  |  |  | .reduce(BigDecimal.ZERO, BigDecimal::add) | 
|---|
|  |  |  | .divide(new BigDecimal("10000"),2, RoundingMode.HALF_UP); | 
|---|
|  |  |  | .divide(new BigDecimal("10000"),2, RoundingMode.DOWN); | 
|---|
|  |  |  | vo.setTotalReceivedRent(totalReceivedRent); | 
|---|
|  |  |  | //本月新增租户数 | 
|---|
|  |  |  | Integer newTenantCount = tContractService.getCurrentMonthRentCount(businessDeptId); | 
|---|
|  |  |  | 
|---|
|  |  |  | BigDecimal totalRentPaid = currentQuarterBillList.stream() | 
|---|
|  |  |  | .map(TBill::getPayFeesMoney) | 
|---|
|  |  |  | .reduce(BigDecimal.ZERO, BigDecimal::add) | 
|---|
|  |  |  | .divide(new BigDecimal("10000"),2, RoundingMode.HALF_UP); | 
|---|
|  |  |  | .divide(new BigDecimal("10000"),2, RoundingMode.DOWN); | 
|---|
|  |  |  | vo.setTotalRentPaid(totalRentPaid); | 
|---|
|  |  |  | //本季度应交租金 | 
|---|
|  |  |  | BigDecimal totalRentShould = currentQuarterBillList.stream() | 
|---|
|  |  |  | .filter(item -> !item.getPayFeesStatus().equals("5")) | 
|---|
|  |  |  | .map(TBill::getPayableFeesMoney) | 
|---|
|  |  |  | .reduce(BigDecimal.ZERO, BigDecimal::add) | 
|---|
|  |  |  | .divide(new BigDecimal("10000"),2, RoundingMode.HALF_UP); | 
|---|
|  |  |  | .divide(new BigDecimal("10000"),2, RoundingMode.DOWN); | 
|---|
|  |  |  | vo.setTotalRentShould(totalRentShould); | 
|---|
|  |  |  | //本季度欠费 | 
|---|
|  |  |  | BigDecimal totalRentOwe = currentQuarterBillList.stream() | 
|---|
|  |  |  | .map(TBill::getOutstandingMoney) | 
|---|
|  |  |  | .reduce(BigDecimal.ZERO, BigDecimal::add) | 
|---|
|  |  |  | .divide(BigDecimal.valueOf(10000L), 2, RoundingMode.HALF_UP); | 
|---|
|  |  |  | .divide(BigDecimal.valueOf(10000L), 2, RoundingMode.DOWN); | 
|---|
|  |  |  | vo.setTotalRentOwe(totalRentOwe); | 
|---|
|  |  |  | //总计欠费 | 
|---|
|  |  |  | BigDecimal totalRentOweAll = billList.stream() | 
|---|
|  |  |  | .map(TBill::getOutstandingMoney) | 
|---|
|  |  |  | .reduce(BigDecimal.ZERO, BigDecimal::add) | 
|---|
|  |  |  | .divide(BigDecimal.valueOf(10000L), 2, RoundingMode.HALF_UP); | 
|---|
|  |  |  | .divide(BigDecimal.valueOf(10000L), 2, RoundingMode.DOWN); | 
|---|
|  |  |  | vo.setTotalRentOweAll(totalRentOweAll); | 
|---|
|  |  |  | return vo; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | .map(TBill::getPayFeesMoney) | 
|---|
|  |  |  | .filter(Objects::nonNull) | 
|---|
|  |  |  | .reduce(BigDecimal.ZERO, BigDecimal::add) | 
|---|
|  |  |  | .divide(BigDecimal.valueOf(10000L), 2, RoundingMode.HALF_UP); | 
|---|
|  |  |  | .divide(BigDecimal.valueOf(10000L), 2, RoundingMode.DOWN); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 计算季度欠费总和 | 
|---|
|  |  |  | BigDecimal quarterOutstanding = quarterBills.stream() | 
|---|
|  |  |  | .map(TBill::getOutstandingMoney) | 
|---|
|  |  |  | .filter(Objects::nonNull) | 
|---|
|  |  |  | .reduce(BigDecimal.ZERO, BigDecimal::add) | 
|---|
|  |  |  | .divide(BigDecimal.valueOf(10000L), 2, RoundingMode.HALF_UP); | 
|---|
|  |  |  | .divide(BigDecimal.valueOf(10000L), 2, RoundingMode.DOWN); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 生成季度标签 (格式: YY-MM月) | 
|---|
|  |  |  | Calendar cal = Calendar.getInstance(); | 
|---|
|  |  |  | 
|---|
|  |  |  | return vo; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public R<List<TenantCountTrendVO>> getTenantCountTrend() { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 获取当前日期 | 
|---|
|  |  |  | Date currentDate = new Date(); | 
|---|
|  |  |  | String businessDeptId = SecurityUtils.getBusinessDeptId(); | 
|---|
|  |  |  | List<TenantCountTrendVO> list = new ArrayList<>(); | 
|---|
|  |  |  | 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"); | 
|---|
|  |  |  | System.out.println("第" + (i + 1) + "季度的起止时间:" + quarterStart + " - " + quarterEnd); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<TContract> contracts = contractService.list(new LambdaQueryWrapper<TContract>() | 
|---|
|  |  |  | .eq(!"0".equals(businessDeptId), TContract::getBusinessDeptId, businessDeptId) | 
|---|
|  |  |  | .eq(TContract::getPayType, 2) | 
|---|
|  |  |  | .isNotNull(TContract::getSignTime) | 
|---|
|  |  |  | .between(TContract::getSignTime, quarterStart, quarterEnd) | 
|---|
|  |  |  | .orderByAsc(TContract::getSignTime)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TenantCountTrendVO vo = new TenantCountTrendVO(); | 
|---|
|  |  |  | // 生成季度标签 (格式: 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); | 
|---|
|  |  |  | vo.setDate(label); | 
|---|
|  |  |  | long count = contracts.stream().map(TContract::getTenantId).distinct().count(); | 
|---|
|  |  |  | vo.setCount(count); | 
|---|
|  |  |  | list.add(vo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(list); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        DateTimeFormatter quarterFormatter = DateTimeFormatter.ofPattern("yyyy-MM"); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        List<TenantCountTrendVO> trendData = contracts.stream() | 
|---|
|  |  |  | //                .collect(Collectors.groupingBy(contract -> { | 
|---|
|  |  |  | //                    LocalDate date = contract.getSignTime().toLocalDate(); | 
|---|
|  |  |  | //                    int quarter = (date.getMonthValue() - 1) / 3 + 1; | 
|---|
|  |  |  | //                    return YearQuarter.from(date.withMonth(quarter * 3 - 2)); | 
|---|
|  |  |  | //                }, TreeMap::new, Collectors.counting())) | 
|---|
|  |  |  | //                .entrySet().stream() | 
|---|
|  |  |  | //                .map(entry -> new TenantCountTrendVO( | 
|---|
|  |  |  | //                        entry.getKey().format(quarterFormatter), | 
|---|
|  |  |  | //                        entry.getValue())) | 
|---|
|  |  |  | //                .collect(Collectors.toList()); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        return R.ok(trendData); | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|