| | |
| | | package com.ruoyi.web.controller.task; |
| | | |
| | | |
| | | import com.ruoyi.system.model.TBill; |
| | | import com.ruoyi.system.model.TContract; |
| | | import com.ruoyi.system.model.TContractRentType; |
| | | import com.ruoyi.system.service.TBillService; |
| | | import com.ruoyi.system.service.TContractRentTypeService; |
| | | 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; |
| | | @Autowired |
| | | private TContractRentTypeService contractRentTypeService; |
| | | |
| | | // 每天凌晨00点执行的定时任务 用于生成违约金 |
| | | @Scheduled(cron = "0 0 0 * * ?") |
| | | public void dayOfProportionBill() { |
| | | try { |
| | | // 查询所有未缴费账单 |
| | | List<TBill> list = billService.lambdaQuery().eq(TBill::getPayFeesStatus, 1).list(); |
| | | for (TBill tBill : list) { |
| | | TContract contract = contractService.getById(tBill.getContractId()); |
| | | LocalDateTime payableFeesTime = tBill.getPayableFeesTime(); |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | // 计算两个时间相差多少个小时 |
| | | long hours = ChronoUnit.HOURS.between(payableFeesTime, now); |
| | | long l = hours / 72; |
| | | if (l>0){ |
| | | // 计算每天租金 |
| | | long days = ChronoUnit.DAYS.between(tBill.getStartTime(),tBill.getEndTime()); |
| | | BigDecimal everyDayMoney = tBill.getPayableFeesMoney().divide(new BigDecimal(days), 2, BigDecimal.ROUND_DOWN); |
| | | // 违约金比例 |
| | | BigDecimal proportion = contract.getProportion(); |
| | | // 预期x天后的违约金 |
| | | BigDecimal money = everyDayMoney.multiply(proportion).multiply(new BigDecimal(l)); |
| | | tBill.setPayableFeesPenalty(money); |
| | | tBill.setOutstandingMoney(money); |
| | | |
| | | } |
| | | } |
| | | billService.updateBatchById(list); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | // 每天凌晨00点执行的定时任务 用于合同生成第一笔账单 |
| | | @Scheduled(cron = "0 0 0 * * ?") |
| | | public void dayOfFirstBill() { |
| | | try { |
| | | // 查询所有已签订的合同并且未生成第一笔账单的 |
| | | List<TContract> list = contractService.lambdaQuery().eq(TContract::getStatus, 4) |
| | | .isNull(TContract::getFirstPayTime).list(); |
| | | List<TBill> bills = new ArrayList<>(); |
| | | List<TContractRentType> contractRentTypes = contractRentTypeService.list(); |
| | | for (TContract contract : list) { |
| | | contract.setFirstPayTime(contract.getStartTime().plusDays(10)); |
| | | // 第一次应缴费日期 |
| | | LocalDateTime firstPayTime = contract.getStartTime().plusDays(10).withHour(0).withMinute(0).withSecond(0); |
| | | 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(); |
| | | // // 计算两个时间相差多少天 |
| | | // // 如果时间小于30天 需要计算每日租金 |
| | | // if (days<30){ |
| | | // rentBill.setPayableFeesMoney(contract.getMonthRent().divide(new BigDecimal("30"),2,BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))); |
| | | // }else{ |
| | | // rentBill.setPayableFeesMoney(contract.getPayType().equals("1")?contract.getMonthRent(): |
| | | // contract.getPayType().equals("2")?contract.getMonthRent().multiply(new BigDecimal("3")):contract.getMonthRent().multiply(new BigDecimal("12")).setScale(2,BigDecimal.ROUND_DOWN)); |
| | | //package com.ruoyi.web.controller.task; |
| | | // |
| | | // |
| | | //import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | //import com.ruoyi.common.utils.SmsUtil; |
| | | //import com.ruoyi.system.mapper.TBillMapper; |
| | | //import com.ruoyi.system.model.TBill; |
| | | //import com.ruoyi.system.model.TContract; |
| | | //import com.ruoyi.system.model.TContractRentType; |
| | | //import com.ruoyi.system.service.TBillService; |
| | | //import com.ruoyi.system.service.TContractRentTypeService; |
| | | //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.LocalTime; |
| | | //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 TBillMapper billMapper; |
| | | // // 用于更新违约金账单 |
| | | // // 每分钟执行一次的定时任务 |
| | | // |
| | | // @Scheduled(cron = "0 * * * * ?") |
| | | // public void dayOfProportionBill() { |
| | | // try { |
| | | // // 查询所有未缴费账单 |
| | | // List<TBill> list = billMapper.selectList(new LambdaQueryWrapper<TBill>().eq(TBill::getPayFeesStatus, 1) |
| | | // .le(TBill::getPayableFeesTime,LocalDate.now())); |
| | | // for (TBill tBill : list) { |
| | | // tBill.setPayFeesStatus("4"); |
| | | // TContract contract = contractService.getById(tBill.getContractId()); |
| | | // LocalDate payableFeesTime = tBill.getPayableFeesTime(); |
| | | // // 将LocalDate转化为LocalDateTime |
| | | // LocalDateTime payableFeesTime1 = LocalDateTime.of(payableFeesTime, LocalTime.of(0, 0, 0)); |
| | | // LocalDateTime now = LocalDateTime.now(); |
| | | // // 计算两个时间相差多少个小时 |
| | | // long hours = ChronoUnit.HOURS.between(payableFeesTime1, now); |
| | | // long l = hours / 24; |
| | | // if (l>=3){ |
| | | // // 违约金比例 |
| | | // BigDecimal proportion = contract.getProportion(); |
| | | // // 按每天 待缴费金额 * XX% 增加违约金费用 |
| | | // if (tBill.getOutstandingMoney().compareTo(new BigDecimal("0"))==0){ |
| | | // tBill.setPayFeesStatus("3"); |
| | | // billMapper.updateById(tBill); |
| | | // continue; |
| | | // } |
| | | // BigDecimal money = tBill.getOutstandingMoney().multiply(new BigDecimal(100).add(proportion)).divide(new BigDecimal(100),2, BigDecimal.ROUND_DOWN); |
| | | // tBill.setOverDays((int) l); |
| | | // tBill.setPayableFeesPenalty((tBill.getPayableFeesPenalty()!=null?tBill.getPayableFeesPenalty():BigDecimal.ZERO).add(money)); |
| | | // tBill.setOutstandingMoney(money); |
| | | // billMapper.updateById(tBill); |
| | | // |
| | | // } |
| | | rentBill.setPayableFeesTime(firstPayTime); |
| | | rentBill.setPayFeesStatus("1"); |
| | | rentBill.setBillType("1"); |
| | | rentBill.setStartTime(contract.getStartPayTime()); |
| | | TContractRentType tContractRentType = contractRentTypes.stream().filter(e -> e.getContractId().equals(contract.getId())).findFirst().orElse(null); |
| | | |
| | | if (tContractRentType!=null && contract.getStartPayTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12).isAfter(tContractRentType.getChangeTime())){ |
| | | // 计算租金变动的天数 |
| | | long moneyDays = ChronoUnit.DAYS.between(tContractRentType.getChangeTime(), contract.getStartPayTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12))+1L; |
| | | contract.setChangeTime(LocalDateTime.now()); |
| | | // 递增递减的租金 |
| | | BigDecimal contractRentTypeMoney = new BigDecimal("0"); |
| | | // 不递增递减的租金 |
| | | BigDecimal originalMoney = new BigDecimal("0"); |
| | | // 原租金 |
| | | switch (tContractRentType.getIncreasingDecreasingType()){ |
| | | case 1: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).add(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays))); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).add(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)))); |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).subtract(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays))); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).subtract(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)))); |
| | | break; |
| | | } |
| | | break; |
| | | case 2: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().add(tContractRentType.getNumericalValue())).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().add(tContractRentType.getNumericalValue()))); |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().subtract(tContractRentType.getNumericalValue())).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().subtract(tContractRentType.getNumericalValue()))); |
| | | |
| | | break; |
| | | } |
| | | break; |
| | | } |
| | | // 不需要涨租金的时间段 |
| | | long originalDays = ChronoUnit.DAYS.between(contract.getFirstPayTime(), tContractRentType.getChangeTime()); |
| | | originalMoney=originalMoney.add(contract.getMonthRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN)) |
| | | .multiply(new BigDecimal(originalDays)); |
| | | rentBill.setPayableFeesMoney(contractRentTypeMoney.add(originalMoney)); |
| | | rentBill.setOutstandingMoney(rentBill.getPayableFeesMoney()); |
| | | if (contract.getFirstPayTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12).isAfter(contract.getEndTime())){ |
| | | rentBill.setEndTime(contract.getFirstPayTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12)); |
| | | }else{ |
| | | rentBill.setEndTime(contract.getEndTime()); |
| | | } |
| | | }else{ |
| | | if (contract.getFirstPayTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12).isAfter(contract.getEndTime())){ |
| | | rentBill.setEndTime(contract.getFirstPayTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12)); |
| | | }else{ |
| | | rentBill.setEndTime(contract.getEndTime()); |
| | | } |
| | | // 不走递增递减 |
| | | long allDays = ChronoUnit.DAYS.between(contract.getFirstPayTime(), rentBill.getEndTime()); |
| | | rentBill.setPayableFeesMoney(contract.getMonthRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(allDays))); |
| | | rentBill.setOutstandingMoney(rentBill.getPayableFeesMoney()); |
| | | |
| | | } |
| | | // 租金账单 |
| | | bills.add(rentBill); |
| | | // 押金账单 |
| | | TBill depositBill = new TBill(); |
| | | depositBill.setContractId(contract.getId()); |
| | | depositBill.setContractNumber(contract.getContractNumber()); |
| | | depositBill.setPayableFeesMoney(contract.getDeposit()); |
| | | depositBill.setOutstandingMoney(depositBill.getPayableFeesMoney()); |
| | | |
| | | depositBill.setPayableFeesTime(firstPayTime); |
| | | depositBill.setPayFeesStatus("1"); |
| | | depositBill.setBillType("2"); |
| | | bills.add(depositBill); |
| | | } |
| | | contractService.updateBatchById(list); |
| | | billService.saveBatch(bills); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | // 每天凌晨00点执行的定时任务 用于生成合同期最后一笔账单 |
| | | @Scheduled(cron = "0 0 0 * * ?") |
| | | public void dayOfEndBill() { |
| | | try { |
| | | // 查询所有已签订的合同并且已经生成第一笔账单的 |
| | | List<TContract> list = contractService.lambdaQuery().eq(TContract::getStatus, 4) |
| | | .ge(TContract::getEndTime,LocalDateTime.now()) |
| | | .isNotNull(TContract::getFirstPayTime).list(); |
| | | List<TContractRentType> contractRentTypes = contractRentTypeService.list(); |
| | | List<TBill> bills = new ArrayList<>(); |
| | | for (TContract contract : list) { |
| | | TBill beforeBill = billService.lambdaQuery().eq(TBill::getContractId, contract.getId()).eq(TBill::getBillType, 1).orderByDesc(TBill::getCreateTime) |
| | | .last("limit 1").one(); |
| | | if (!(beforeBill.getEndTime().toLocalDate().equals(contract.getEndTime().toLocalDate())) |
| | | && |
| | | (contract.getPayType().equals("1")? |
| | | beforeBill.getEndTime().plusMonths(1):contract.getPayType().equals("2")? |
| | | beforeBill.getEndTime().plusMonths(3):beforeBill.getEndTime().plusMonths(12)) |
| | | .with(TemporalAdjusters.lastDayOfMonth()).isAfter(contract.getEndTime()) |
| | | && beforeBill.getEndTime().isBefore(contract.getEndTime()) |
| | | ){ |
| | | TBill tBill = new TBill(); |
| | | tBill.setContractId(contract.getId()); |
| | | long days = ChronoUnit.DAYS.between((contract.getPayType().equals("1")? |
| | | beforeBill.getEndTime().plusMonths(1):contract.getPayType().equals("2")? |
| | | beforeBill.getEndTime().plusMonths(3):beforeBill.getEndTime().plusMonths(12)).with(TemporalAdjusters.firstDayOfMonth()), contract.getEndTime())+1L; |
| | | if (contract.getIsIncreasing()){ |
| | | TContractRentType tContractRentType = contractRentTypes.stream().filter(e -> e.getContractId().equals(contract.getId())).findFirst().orElse(null); |
| | | if (tContractRentType!=null |
| | | && beforeBill.getEndTime().isBefore(tContractRentType.getChangeTime()) |
| | | && beforeBill.getEndTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12).with(TemporalAdjusters.lastDayOfMonth()).isAfter(tContractRentType.getChangeTime())){ |
| | | // 如果没变过 |
| | | if (contract.getChangeTime()==null){ |
| | | contract.setChangeTime(LocalDateTime.now()); |
| | | // 租金递增递减的时长 天 |
| | | long moneyDays = ChronoUnit.DAYS.between(tContractRentType.getChangeTime(), contract.getEndTime()); |
| | | // 递增递减的租金 |
| | | BigDecimal contractRentTypeMoney = new BigDecimal("0"); |
| | | // 不递增递减的租金 |
| | | BigDecimal originalMoney = new BigDecimal("0"); |
| | | // 原租金 |
| | | switch (tContractRentType.getIncreasingDecreasingType()){ |
| | | case 1: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).add(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).add(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)))); |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).subtract(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).subtract(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)))); |
| | | break; |
| | | } |
| | | break; |
| | | case 2: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().add(tContractRentType.getNumericalValue())).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().add(tContractRentType.getNumericalValue()))); |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().subtract(tContractRentType.getNumericalValue())).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().subtract(tContractRentType.getNumericalValue()))); |
| | | |
| | | break; |
| | | } |
| | | break; |
| | | } |
| | | // 不需要涨租金的时间段 |
| | | long originalDays = ChronoUnit.DAYS.between(beforeBill.getEndTime(), tContractRentType.getChangeTime()); |
| | | originalMoney=originalMoney.add(contract.getMonthRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN)) |
| | | .multiply(new BigDecimal(originalDays)); |
| | | tBill.setPayableFeesMoney(contractRentTypeMoney.add(originalMoney)); |
| | | tBill.setOutstandingMoney(tBill.getPayableFeesMoney()); |
| | | |
| | | }else{ |
| | | // 之前已经涨、跌过租金了 需要判断周期是否还需要再涨、跌 |
| | | if ((LocalDateTime.now().getYear() - contract.getChangeTime().getYear())%tContractRentType.getCycleTime()==0){ |
| | | contract.setChangeTime(LocalDateTime.now()); |
| | | // 租金递增递减的时长 天 |
| | | long moneyDays = ChronoUnit.DAYS.between(tContractRentType.getChangeTime(), contract.getEndTime()); |
| | | // 递增递减的租金 |
| | | BigDecimal contractRentTypeMoney = new BigDecimal("0"); |
| | | // 不递增递减的租金 |
| | | BigDecimal originalMoney = new BigDecimal("0"); |
| | | // 原租金 |
| | | switch (tContractRentType.getIncreasingDecreasingType()){ |
| | | case 1: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).add(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).add(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)))); |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).subtract(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).subtract(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)))); |
| | | break; |
| | | } |
| | | break; |
| | | case 2: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().add(tContractRentType.getNumericalValue())).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().add(tContractRentType.getNumericalValue()))); |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().subtract(tContractRentType.getNumericalValue())).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().subtract(tContractRentType.getNumericalValue()))); |
| | | |
| | | break; |
| | | } |
| | | break; |
| | | } |
| | | // 不需要涨租金的时间段 |
| | | long originalDays = ChronoUnit.DAYS.between(beforeBill.getEndTime(), tContractRentType.getChangeTime()); |
| | | originalMoney=originalMoney.add(contract.getMonthRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN)) |
| | | .multiply(new BigDecimal(originalDays)); |
| | | tBill.setPayableFeesMoney(contractRentTypeMoney.add(originalMoney)); |
| | | tBill.setOutstandingMoney(tBill.getPayableFeesMoney()); |
| | | |
| | | }else{ |
| | | // 不涨租金 用上次的 |
| | | // 租金递增递减的时长 天 |
| | | long moneyDays = ChronoUnit.DAYS.between(tContractRentType.getChangeTime(), contract.getEndTime()); |
| | | // 递增递减的租金 |
| | | BigDecimal contractRentTypeMoney = new BigDecimal("0"); |
| | | // 不递增递减的租金 |
| | | BigDecimal originalMoney = new BigDecimal("0"); |
| | | // 原租金 |
| | | switch (tContractRentType.getIncreasingDecreasingType()){ |
| | | case 1: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))) ; |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))); |
| | | break; |
| | | } |
| | | break; |
| | | case 2: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent()).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent()).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | break; |
| | | } |
| | | break; |
| | | } |
| | | // 不需要涨租金的时间段 |
| | | long originalDays = ChronoUnit.DAYS.between(beforeBill.getEndTime(), tContractRentType.getChangeTime()); |
| | | originalMoney=originalMoney.add(contract.getMonthRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN)) |
| | | .multiply(new BigDecimal(originalDays)); |
| | | tBill.setPayableFeesMoney(contractRentTypeMoney.add(originalMoney)); |
| | | tBill.setOutstandingMoney(tBill.getPayableFeesMoney()); |
| | | |
| | | } |
| | | } |
| | | |
| | | } |
| | | }else{ |
| | | long allDays = ChronoUnit.DAYS.between(beforeBill.getEndTime(), contract.getEndTime()); |
| | | tBill.setPayableFeesMoney(contract.getMonthRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(allDays))); |
| | | tBill.setOutstandingMoney(tBill.getPayableFeesMoney()); |
| | | |
| | | } |
| | | tBill.setContractNumber(contract.getContractNumber()); |
| | | if (beforeBill.getEndTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12).getDayOfMonth()<=15){ |
| | | tBill.setPayableFeesTime(contract.getEndTime().withHour(0).withMinute(0).withSecond(0)); |
| | | }else{ |
| | | tBill.setPayableFeesTime((contract.getPayType().equals("1")? |
| | | beforeBill.getEndTime().plusMonths(1).withDayOfMonth(15):contract.getPayType().equals("2")? |
| | | beforeBill.getEndTime().plusMonths(3).withDayOfMonth(15):beforeBill.getEndTime().withDayOfMonth(15).plusMonths(12).withHour(0).withMinute(0).withSecond(0))); |
| | | } |
| | | tBill.setPayFeesStatus("1"); |
| | | tBill.setBillType("1"); |
| | | tBill.setStartTime(beforeBill.getEndTime().plusDays(1)); |
| | | tBill.setEndTime(beforeBill.getEndTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12)); |
| | | bills.add(tBill); |
| | | } |
| | | } |
| | | billService.saveBatch(bills); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | // 每月15号凌晨执行的定时任务 用于生成租金账单 |
| | | @Scheduled(cron = "0 0 0 15 * ?") |
| | | public void monthOfBill() { |
| | | try { |
| | | // 查询所有已签订的合同 |
| | | List<TContract> list = contractService.lambdaQuery().eq(TContract::getStatus, 4) |
| | | .isNotNull(TContract::getFirstPayTime) |
| | | .ge(TContract::getEndTime, LocalDateTime.now()) |
| | | .list(); |
| | | List<TContractRentType> contractRentTypes = contractRentTypeService.list(); |
| | | |
| | | List<TBill> bills = new ArrayList<>(); |
| | | for (TContract contract : list) { |
| | | TBill beforeBill = billService.lambdaQuery().eq(TBill::getContractId, contract.getId()).eq(TBill::getBillType, 1).orderByDesc(TBill::getCreateTime) |
| | | .last("limit 1").one(); |
| | | if (beforeBill.getEndTime().toLocalDate().equals(contract.getEndTime().toLocalDate()))continue; |
| | | if (beforeBill.getEndTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12).with(TemporalAdjusters.lastDayOfMonth()).isBefore(contract.getEndTime())){ |
| | | TBill tBill = new TBill(); |
| | | tBill.setContractId(contract.getId()); |
| | | tBill.setContractNumber(contract.getContractNumber()); |
| | | // 根据支付方式判断需不需要生成订单 |
| | | if (!(beforeBill.getEndTime().toLocalDate().equals(contract.getEndTime().toLocalDate())) |
| | | && |
| | | (contract.getPayType().equals("1")? |
| | | beforeBill.getEndTime().plusMonths(1):contract.getPayType().equals("2")? |
| | | beforeBill.getEndTime().plusMonths(3):beforeBill.getEndTime().plusMonths(12)) |
| | | .with(TemporalAdjusters.lastDayOfMonth()).isBefore(contract.getEndTime()) |
| | | && beforeBill.getEndTime().isBefore(contract.getEndTime()) |
| | | ){ |
| | | tBill.setContractId(contract.getId()); |
| | | long days = ChronoUnit.DAYS.between(beforeBill.getEndTime(), (contract.getPayType().equals("1")? |
| | | beforeBill.getEndTime().plusMonths(1):contract.getPayType().equals("2")? |
| | | beforeBill.getEndTime().plusMonths(3):beforeBill.getEndTime().plusMonths(12)).with(TemporalAdjusters.lastDayOfMonth()))+1L; |
| | | if (contract.getIsIncreasing()){ |
| | | TContractRentType tContractRentType = contractRentTypes.stream().filter(e -> e.getContractId().equals(contract.getId())).findFirst().orElse(null); |
| | | if (tContractRentType!=null |
| | | && beforeBill.getEndTime().isBefore(tContractRentType.getChangeTime()) |
| | | && beforeBill.getEndTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12).with(TemporalAdjusters.lastDayOfMonth()).isAfter(tContractRentType.getChangeTime())){ |
| | | // 如果没变过 |
| | | if (contract.getChangeTime()==null){ |
| | | contract.setChangeTime(LocalDateTime.now()); |
| | | // 租金递增递减的时长 天 |
| | | long moneyDays = ChronoUnit.DAYS.between(tContractRentType.getChangeTime(), contract.getEndTime()); |
| | | // 递增递减的租金 |
| | | BigDecimal contractRentTypeMoney = new BigDecimal("0"); |
| | | // 不递增递减的租金 |
| | | BigDecimal originalMoney = new BigDecimal("0"); |
| | | // 原租金 |
| | | switch (tContractRentType.getIncreasingDecreasingType()){ |
| | | case 1: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).add(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).add(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)))); |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).subtract(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).subtract(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)))); |
| | | break; |
| | | } |
| | | break; |
| | | case 2: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().add(tContractRentType.getNumericalValue())).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().add(tContractRentType.getNumericalValue()))); |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().subtract(tContractRentType.getNumericalValue())).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().subtract(tContractRentType.getNumericalValue()))); |
| | | |
| | | break; |
| | | } |
| | | break; |
| | | } |
| | | // 不需要涨租金的时间段 |
| | | long originalDays = ChronoUnit.DAYS.between(beforeBill.getEndTime(), tContractRentType.getChangeTime()); |
| | | originalMoney=originalMoney.add(contract.getMonthRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN)) |
| | | .multiply(new BigDecimal(originalDays)); |
| | | tBill.setPayableFeesMoney(contractRentTypeMoney.add(originalMoney)); |
| | | tBill.setOutstandingMoney(tBill.getPayableFeesMoney()); |
| | | |
| | | }else{ |
| | | // 之前已经涨、跌过租金了 需要判断周期是否还需要再涨、跌 |
| | | if ((LocalDateTime.now().getYear() - contract.getChangeTime().getYear())%tContractRentType.getCycleTime()==0){ |
| | | contract.setChangeTime(LocalDateTime.now()); |
| | | // 租金递增递减的时长 天 |
| | | long moneyDays = ChronoUnit.DAYS.between(tContractRentType.getChangeTime(), contract.getEndTime()); |
| | | // 递增递减的租金 |
| | | BigDecimal contractRentTypeMoney = new BigDecimal("0"); |
| | | // 不递增递减的租金 |
| | | BigDecimal originalMoney = new BigDecimal("0"); |
| | | // 原租金 |
| | | switch (tContractRentType.getIncreasingDecreasingType()){ |
| | | case 1: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).add(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).add(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)))); |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).subtract(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().multiply(new BigDecimal(100).subtract(tContractRentType.getNumericalValue()).divide(new BigDecimal(100),2,BigDecimal.ROUND_DOWN)))); |
| | | break; |
| | | } |
| | | break; |
| | | case 2: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().add(tContractRentType.getNumericalValue())).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().add(tContractRentType.getNumericalValue()))); |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().subtract(tContractRentType.getNumericalValue())).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | contract.setChangeRent(contractRentTypeMoney.add(contract.getChangeRent().subtract(tContractRentType.getNumericalValue()))); |
| | | |
| | | break; |
| | | } |
| | | break; |
| | | } |
| | | // 不需要涨租金的时间段 |
| | | long originalDays = ChronoUnit.DAYS.between(beforeBill.getEndTime(), tContractRentType.getChangeTime()); |
| | | originalMoney=originalMoney.add(contract.getMonthRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN)) |
| | | .multiply(new BigDecimal(originalDays)); |
| | | tBill.setPayableFeesMoney(contractRentTypeMoney.add(originalMoney)); |
| | | tBill.setOutstandingMoney(tBill.getPayableFeesMoney()); |
| | | |
| | | }else{ |
| | | // 不涨租金 用上次的 |
| | | // 租金递增递减的时长 天 |
| | | long moneyDays = ChronoUnit.DAYS.between(tContractRentType.getChangeTime(), contract.getEndTime()); |
| | | // 递增递减的租金 |
| | | BigDecimal contractRentTypeMoney = new BigDecimal("0"); |
| | | // 不递增递减的租金 |
| | | BigDecimal originalMoney = new BigDecimal("0"); |
| | | // 原租金 |
| | | switch (tContractRentType.getIncreasingDecreasingType()){ |
| | | case 1: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))) ; |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(days))); |
| | | break; |
| | | } |
| | | break; |
| | | case 2: |
| | | switch (tContractRentType.getIncreasingDecreasing()){ |
| | | case 1: |
| | | contractRentTypeMoney =contractRentTypeMoney.add(contract.getChangeRent()).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | break; |
| | | case 2: |
| | | contractRentTypeMoney = contractRentTypeMoney.add(contract.getChangeRent()).divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(moneyDays)); |
| | | break; |
| | | } |
| | | break; |
| | | } |
| | | // 不需要涨租金的时间段 |
| | | long originalDays = ChronoUnit.DAYS.between(beforeBill.getEndTime(), tContractRentType.getChangeTime()); |
| | | originalMoney=originalMoney.add(contract.getMonthRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN)) |
| | | .multiply(new BigDecimal(originalDays)); |
| | | tBill.setPayableFeesMoney(contractRentTypeMoney.add(originalMoney)); |
| | | tBill.setOutstandingMoney(tBill.getPayableFeesMoney()); |
| | | |
| | | } |
| | | } |
| | | |
| | | } |
| | | }else{ |
| | | long allDays = ChronoUnit.DAYS.between(beforeBill.getEndTime(), (contract.getPayType().equals("1")? |
| | | beforeBill.getEndTime().plusMonths(1):contract.getPayType().equals("2")? |
| | | beforeBill.getEndTime().plusMonths(3):beforeBill.getEndTime().plusMonths(12)).with(TemporalAdjusters.lastDayOfMonth())); |
| | | tBill.setPayableFeesMoney(contract.getMonthRent().divide(new BigDecimal(30), 2, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(allDays))); |
| | | tBill.setOutstandingMoney(tBill.getPayableFeesMoney()); |
| | | |
| | | } |
| | | tBill.setContractNumber(contract.getContractNumber()); |
| | | if (beforeBill.getEndTime().plusMonths(contract.getPayType().equals("1")? 1:contract.getPayType().equals("2")? 3:12).getDayOfMonth()<=15){ |
| | | tBill.setPayableFeesTime(contract.getEndTime()); |
| | | }else{ |
| | | tBill.setPayableFeesTime((contract.getPayType().equals("1")? |
| | | beforeBill.getEndTime().plusMonths(1).withDayOfMonth(15):contract.getPayType().equals("2")? |
| | | beforeBill.getEndTime().plusMonths(3).withDayOfMonth(15):beforeBill.getEndTime().withDayOfMonth(15).plusMonths(12))); |
| | | } |
| | | tBill.setPayFeesStatus("1"); |
| | | tBill.setBillType("1"); |
| | | tBill.setStartTime(beforeBill.getEndTime().plusDays(1)); |
| | | tBill.setEndTime(contract.getEndTime()); |
| | | bills.add(tBill); |
| | | } |
| | | |
| | | } |
| | | } |
| | | billService.saveBatch(bills); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | |
| | | // LocalDateTime now = LocalDateTime.now().minusMonths(1).withDayOfMonth(31); |
| | | // System.err.println(now); |
| | | // LocalDateTime now2 = now.plusMonths(1); |
| | | // System.err.println(now2); |
| | | // } |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | // |
| | | // 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); |
| | | |
| | | } |
| | | |
| | | } |
| | | //} |