xuhy
2023-05-15 17a21eb623864ad92ca41087dfcd8814641a9587
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
130
131
132
package com.stylefeng.guns.modular.system.util;
 
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.modular.system.enums.UserTypeEnum;
import com.stylefeng.guns.modular.system.model.TAppUser;
import com.stylefeng.guns.modular.system.model.TDriver;
import com.stylefeng.guns.modular.system.model.TDriverWork;
import com.stylefeng.guns.modular.system.model.TOrder;
import com.stylefeng.guns.modular.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
 
/**
 * 定时任务工具类
 */
@Component
public class TaskUtil {
 
    @Autowired
    private ITLocationService locationService;
    @Autowired
    private ITDriverService driverService;
    @Autowired
    private ITAppUserService appUserService;
    @Autowired
    private ITOrderService orderService;
    @Autowired
    private ITDriverWorkService tDriverWorkService;
 
 
 
    /**
     * 每隔一分钟去处理的定时任务
     */
    @Scheduled(fixedRate = 1000 * 60)
    public void taskMinute(){
        try {
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     * 每天的凌晨执行的任务
     */
    @Scheduled(cron = "0 0 0 * * *")
    public void taskDay(){
        try {
            locationService.updateFence();//更新线上电子围栏
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
    /**
     * 每半天检测用户是否有异常
     */
    @Scheduled(cron = "0 0 0,12 * * ? ")
    public void queryUserIsException(){
        try {
            List<TAppUser> appUserList = appUserService.selectList(new EntityWrapper<TAppUser>().ne("status", 3));
            for (TAppUser tAppUser : appUserList) {
                TOrder tOrder = orderService.selectOne(new EntityWrapper<TOrder>()
                        .eq("userId", tAppUser.getId())
                        .last("LIMIT 1"));
                // 客户一个月未下单,状态异常
                Period period = Period.between(DateUtil.dateToLocalDate(tOrder.getCreateTime()), LocalDate.now());
                int day = Math.abs(period.getDays());
                if(day > 29){
                    tAppUser.setIsException(2);
                }
            }
            appUserService.updateBatchById(appUserList);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
    /**
     * 每半天检测司机是否有异常
     */
    @Scheduled(cron = "0 0 9,21 * * ? ")
    public void queryDriverIsException(){
        try {
            List<TDriver> driverList = driverService.selectList(new EntityWrapper<TDriver>().ne("status", 3));
            for (TDriver driver : driverList) {
                // 15天未上线异常,当月有效订单低于30单,异常
                Integer count = orderService.getValidOrderCount(driver.getId(),new BigDecimal(14),new SimpleDateFormat("yyyy-MM").format(new Date()));
                if(count < 30){
                    driver.setIsException(2);
                }
                TDriverWork tDriverWork = tDriverWorkService.selectOne(new EntityWrapper<TDriverWork>()
                        .eq("driverId", driver.getId())
                        .orderBy("workTime", false)
                        .last("LIMIT 1"));
                if(Objects.nonNull(tDriverWork)){
                    // 如果是下班状态,计算未上线天数,,如果为上班状态,则设置为0
                    if(tDriverWork.getStatus() == 2){
                        Period period = Period.between(DateUtil.dateToLocalDate(tDriverWork.getOffWorkTime()), LocalDate.now());
                        int day = Math.abs(period.getDays());
                        if(day>14){
                            driver.setIsException(2);
                        }
                    }
                }else {
                    // 没有上班记录,计算审核时间
                    Period period = Period.between(DateUtil.dateToLocalDate(driver.getApprovalTime()), LocalDate.now());
                    int day = Math.abs(period.getDays());
                    if(day>14){
                        driver.setIsException(2);
                    }
                }
            }
            driverService.updateBatchById(driverList);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
}