Pu Zhibing
1 天以前 a07a063b9e2fbe1ea41b520274dd9821af5ee03a
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.stylefeng.guns.modular.system.util;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar;
import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService;
import com.stylefeng.guns.modular.system.dao.DriverWorkMapper;
import com.stylefeng.guns.modular.system.model.Driver;
import com.stylefeng.guns.modular.system.model.DriverWork;
import com.stylefeng.guns.modular.system.service.IDriverService;
import com.stylefeng.guns.modular.system.service.IDriverWorkService;
import com.stylefeng.guns.modular.system.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
 
/**
 * 定时任务工具类
 */
@Component
public class TaskUtil {
 
    @Autowired
    private IDriverService driverService;
 
    @Autowired
    private IOrderService orderService;
 
    @Autowired
    private JGPushUtil jgPushUtil;
 
    public Set<Integer> driverIds = new HashSet<>();//存储需要提醒司机预约单的司机id
    @Autowired
    private DriverWorkMapper driverWorkMapper;
 
    @Autowired
    private IDriverWorkService driverWorkService;
 
    @Autowired
    private IOrderPrivateCarService orderPrivateCarService;
 
 
    /**
     * 每隔一分钟去处理的定时任务
     */
    @Scheduled(fixedRate = 1000 * 60)
    public void taskMinute(){
        try {
 
            //修改满足活动条件的数据(在线时长,订单量)
//            driverService.taskMinute();
//            处理车载端断电后的自动下班
//            driverService.taskOffWork();
//            处理结束订单后30分钟解绑小号功能
//            orderService.taskMidAxbUnBindSend();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 每隔一分钟去处理的定时任务
     */
    @Scheduled(fixedRate = 1000 * 60)
    public void taskMinute2(){
        try {
            updateDriverState();
            //处于预约单
            orderService.reservationOrder();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
 
 
 
    /**
     * 每隔五分钟去处理的定时任务
     */
    @Scheduled(fixedRate = 1000 * 60 * 5)
    public void taskFiveMinute(){
        try {
            //处理未支付订单发送短信通知
            orderService.taskSmsSend();
//            for (Integer id: driverIds){
//                jgPushUtil.push(1, "您的预约订单出行时间就要到了,请尽快前往预约地点接乘客", "DRIVER" + id);
//            }
            this.driverIds.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
 
    /**
     * 每天的凌晨执行的任务
     */
    @Scheduled(cron = "0 0 0 * * *")
    public void taskDay(){
        try {
            //生成当天的司机活动
            driverService.addTodayActivity();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
 
//
//    /**
//     * 每月第一天的1点执行的任务
//     */
//    @Scheduled(cron = "0 0 1 1 * *")
//    public void taskMonth(){
//        try {
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
 
 
 
 
    private void updateDriverState(){
        List<Driver> drivers = driverService.selectList(new EntityWrapper<Driver>().eq("authState", 2).ne("flag", 3));
        for (Driver driver : drivers) {
            Integer state = driver.getState();
            int driverWork = driverWorkService.selectCount(new EntityWrapper<DriverWork>().eq("driverId", driver.getId()).eq("state", 1));
            int count = orderPrivateCarService.selectCount(new EntityWrapper<OrderPrivateCar>().eq("driverId", driver.getId())
                    .in("state", Arrays.asList(2, 3, 4, 5, 11)).eq("isDelete", 1).last(" and (orderType = 1 or (orderType = 2 and UNIX_TIMESTAMP(travelTime) - UNIX_TIMESTAMP(NOW()) < 1800))"));
            if(1 == state){
                if(count > 0){
                    driver.setState(3);
                } else if (count == 0 && driverWork > 0) {
                    driver.setState(2);
                }
            }
            if(2 == state){
                if(count > 0){
                    driver.setState(3);
                } else if (0 == driverWork) {
                    driver.setState(1);
                }
            }
            if(3 == state){
                if(count == 0 && driverWork > 0){
                    driver.setState(2);
                }
                if (count == 0 && 0 == driverWork) {
                    driver.setState(1);
                }
            }
        }
        if(!drivers.isEmpty()){
            driverService.updateBatchById(drivers);
        }
    }
}