xuhy
2025-02-27 c474556f44163526700fd7c99a88e37e4297e23f
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
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());
                LocalDate payableFeesTime = tBill.getPayableFeesTime();
                LocalDateTime now = LocalDateTime.now();
                // 计算两个时间相差多少个小时
                long hours = ChronoUnit.HOURS.between(payableFeesTime, now);
                long l = hours / 72;
                if (l>0){
                    // 违约金比例
                    BigDecimal proportion = contract.getProportion();
                    // 应缴违约金
                    BigDecimal money = tBill.getOutstandingMoney().multiply(proportion);
                    TBill changeBill = new TBill();
                    changeBill.setId(tBill.getId());
                    changeBill.setPayableFeesPenalty(money);
                    billService.lockAndUpdateInfo(changeBill,2);
                }
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
 
    // 每天凌晨00点执行的定时任务 根据应缴费日期修改账单状态
    @Scheduled(cron = "0 0 0 * * ?")
    public void dayOfEndBill() {
        try {
            List<TBill> list = billService.lambdaQuery().eq(TBill::getPayFeesStatus, "2").list();
            for (TBill tBill : list) {
                if (tBill.getPayableFeesTime().equals(LocalDate.now())){
                    tBill.setPayFeesStatus("1");
                }
            }
            billService.updateBatchById(list);
        } 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);
//
//        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);
 
    }
 
}