package com.ruoyi.web.controller.task; import com.ruoyi.system.model.TBill; import com.ruoyi.system.model.TContract; import com.ruoyi.system.service.TBillService; import com.ruoyi.system.service.TContractService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Random; import java.util.stream.Collectors; /** * @author zhibing.pu * @date 2023/7/11 8:39 */ @Component public class TaskUtil { @Autowired private TContractService contractService; @Autowired private TBillService billService; // 每天凌晨00点执行的定时任务 用于合同生成第一笔账单 @Scheduled(cron = "0 0 0 * * ?") public void dayOfFirstBill() { try { // 查询所有已签订的合同并且未生成第一笔账单的 List list = contractService.lambdaQuery().eq(TContract::getStatus, 4).isNull(TContract::getFirstPayTime).list(); List bills = new ArrayList<>(); for (TContract contract : list) { contract.setFirstPayTime(contract.getStartTime().plusDays(10)); // 第一次应缴费日期 LocalDateTime firstPayTime = contract.getStartTime().plusDays(10); LocalDate localDate = contract.getStartTime().plusDays(10).toLocalDate(); LocalDate now = LocalDate.now(); // 如果应缴费日期和当前时间不相同 跳过 if (!localDate.equals(now)) { continue; } TBill rentBill = new TBill(); rentBill.setContractId(contract.getId()); rentBill.setContractNumber(contract.getContractNumber()); LocalDateTime startPayTime = contract.getStartPayTime(); LocalDateTime endTime1 = contract.getEndTime(); // 计算两个时间相差多少天 long days = ChronoUnit.DAYS.between(startPayTime, endTime1)+1L; // 如果时间小于30天 需要计算每日租金 if (days<30){ rentBill.setPayableFeesMoney(contract.getMonthRent().divide(new BigDecimal("30"),2,BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))); }else{ rentBill.setPayableFeesMoney(contract.getMonthRent()); } rentBill.setPayableFeesTime(firstPayTime); rentBill.setPayFeesStatus("1"); rentBill.setBillType("1"); rentBill.setStartTime(contract.getStartPayTime()); if (contract.getEndTime().getYear() == contract.getStartTime().getYear() && contract.getEndTime().getMonth() == contract.getStartTime().getMonth()) { // 如果同年同月 那么账单周期为合同结束时间 rentBill.setEndTime(contract.getEndTime()); } else { // 否则 取当月最后一天 LocalDateTime endTime = contract.getStartTime().with(TemporalAdjusters.lastDayOfMonth()).withSecond(59).withHour(23).withMinute(59); rentBill.setEndTime(endTime); } // 租金账单 bills.add(rentBill); // 押金账单 TBill depositBill = new TBill(); depositBill.setContractId(contract.getId()); depositBill.setContractNumber(contract.getContractNumber()); depositBill.setPayableFeesMoney(contract.getDeposit()); depositBill.setPayableFeesTime(firstPayTime); depositBill.setPayFeesStatus("1"); depositBill.setBillType("2"); bills.add(depositBill); } contractService.updateBatchById(list); billService.saveBatch(bills); } catch (Exception e) { e.printStackTrace(); } } // 每月15号凌晨执行的定时任务 用于生成租金账单 @Scheduled(cron = "0 0 0 15 * ?") public void monthOfBill() { try { // 查询所有已签订的合同 List list = contractService.lambdaQuery().eq(TContract::getStatus, 4).isNotNull(TContract::getFirstPayTime).list(); List bills = new ArrayList<>(); for (TContract contract : list) { TBill tBill = new TBill(); LocalDateTime endTime = contract.getEndTime(); if (endTime.isBefore(LocalDateTime.now())){ // 最后缴费日期是当天 tBill.setContractId(contract.getId()); tBill.setContractNumber(contract.getContractNumber()); tBill.setPayableFeesMoney(contract.getMonthRent()); // tBill.setPayableFeesTime(); tBill.setPayFeesStatus("1"); tBill.setBillType("1"); tBill.setStartTime(contract.getStartPayTime()); // 如果同年同月 那么账单周期为合同结束时间 tBill.setEndTime(contract.getEndTime()); } contract.setFirstPayTime(contract.getStartTime().plusDays(10)); // 第一次应缴费日期 LocalDateTime firstPayTime = contract.getStartTime().plusDays(10); LocalDate localDate = contract.getStartTime().plusDays(10).toLocalDate(); LocalDate now = LocalDate.now(); switch (contract.getPayType()){ case "1": break; case "2": break; case "3": break; } if (!localDate.equals(now)) { continue; } TBill rentBill = new TBill(); rentBill.setContractId(contract.getId()); rentBill.setContractNumber(contract.getContractNumber()); rentBill.setPayableFeesMoney(contract.getMonthRent()); rentBill.setPayableFeesTime(firstPayTime); rentBill.setPayFeesStatus("1"); rentBill.setBillType("1"); rentBill.setStartTime(contract.getStartPayTime()); if (contract.getEndTime().getYear() == contract.getStartTime().getYear() && contract.getEndTime().getMonth() == contract.getStartTime().getMonth()) { // 如果同年同月 那么账单周期为合同结束时间 rentBill.setEndTime(contract.getEndTime()); } else { // 否则 取当月最后一天 // LocalDateTime endTime = contract.getStartTime().with(TemporalAdjusters.lastDayOfMonth()).withSecond(59).withHour(23).withMinute(59); rentBill.setEndTime(endTime); } // 租金账单 bills.add(rentBill); // 押金账单 TBill depositBill = new TBill(); depositBill.setContractId(contract.getId()); depositBill.setContractNumber(contract.getContractNumber()); depositBill.setPayableFeesMoney(contract.getDeposit()); depositBill.setPayableFeesTime(firstPayTime); depositBill.setPayFeesStatus("1"); depositBill.setBillType("2"); bills.add(depositBill); } contractService.updateBatchById(list); billService.saveBatch(bills); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); LocalDateTime now1 = LocalDateTime.now(); long days = ChronoUnit.DAYS.between(now, now1); long days2 = ChronoUnit.DAYS.between(now.plusDays(1), now1); System.err.println(days); System.err.println(days2); // LocalDateTime endTime = now.with(TemporalAdjusters.lastDayOfMonth()).withSecond(59).withHour(23).withMinute(59); // // System.err.println(endTime); } }