puzhibing
2024-05-06 2de95b175c2a68d7ce28fb453204b602a1a8fdb8
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.stylefeng.guns.modular.system.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.smallLogistics.model.OrderLogistics;
import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsService;
import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar;
import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService;
import com.stylefeng.guns.modular.system.dao.DriverActivityHistoryMapper;
import com.stylefeng.guns.modular.system.dao.DriverActivityOnlineMapper;
import com.stylefeng.guns.modular.system.dao.DriverOnlineMapper;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.IDriverAssessmentService;
import com.stylefeng.guns.modular.system.service.IDriverOnlineService;
import com.stylefeng.guns.modular.system.service.IDriverService;
import com.stylefeng.guns.modular.system.service.IDriverWorkService;
import com.stylefeng.guns.modular.system.util.PushUtil;
import com.stylefeng.guns.modular.system.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
public class DriverOnlineServiceImpl extends ServiceImpl<DriverOnlineMapper, DriverOnline> implements IDriverOnlineService {
 
    @Resource
    private DriverOnlineMapper driverOnlineMapper;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private IDriverWorkService driverWorkService;
 
    @Autowired
    private IDriverAssessmentService driverAssessmentService;
 
    @Autowired
    private IDriverService driverService;
    
    @Resource
    private DriverActivityOnlineMapper driverActivityOnlineMapper;
    
    @Resource
    private DriverActivityHistoryMapper driverActivityHistoryMapper;
    
    @Autowired
    private IOrderPrivateCarService orderPrivateCarService;
    
    @Autowired
    private IOrderLogisticsService orderLogisticsService;
    
    @Autowired
    private PushUtil pushUtil;
    
    
    
 
 
    /**
     * 添加数据
     * @param driverId
     * @throws Exception
     */
    @Override
    public void addDriverOnline(Integer driverId) throws Exception {
        DriverWork driverWork = driverWorkService.selectOne(new EntityWrapper<DriverWork>().eq("driverId", driverId).eq("state", 1));
        if(null == driverWork){//非上班状态不记录
            for (int i = 1; i < 5; i++) {
                redisUtil.remove("ONLINE_" + i + "_" + driverId);
            }
            return;
        }
        String[] split1 = driverWork.getType().split(",");
        for (String ty : split1) {
            //查询数据及条件判断
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            DriverOnline query = driverOnlineMapper.query(driverId, sdf.format(new Date()), Integer.valueOf(ty), 0);//正常记录数据
            DriverOnline query1 = null;//考勤范围内的数据
            DriverAssessment driverAssessment = driverAssessmentService.selectOne(new EntityWrapper<DriverAssessment>()
                    .eq("companyId", driverService.selectById(driverId).getCompanyId()));
            Date s = null;
            Date e = null;
            long now = System.currentTimeMillis();
            if(null != driverAssessment){//在考勤范围内记录数据
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String[] split = driverAssessment.getAssessment().split(" - ");
                s = sdf1.parse(sdf.format(new Date()) + " " + split[0]);
                e = sdf1.parse(sdf.format(new Date()) + " " + split[1]);
                if(now > s.getTime() && now <= e.getTime()){
                    query1 = driverOnlineMapper.query(driverId, sdf.format(new Date()), Integer.valueOf(ty), 1);
                }
            }
    
            //开始修改数据
            if(query != null){
                String value = redisUtil.getValue("ONLINE_" + ty + "_" + driverId);
                if(ToolUtil.isEmpty(value)){
                    redisUtil.setStrValue("ONLINE_" + ty + "_" + driverId, String.valueOf(now / 1000));//存入秒
                    return;
                }
                Long ss = (now / 1000) - Long.valueOf(value);
                if(ss.longValue() >= 600){//间隔时间大于10分钟则为离线
                    redisUtil.remove("ONLINE_" + ty + "_" + driverId);
                    return;
                }
        
                query.setDuration(query.getDuration() + ss);
                driverOnlineMapper.updateById(query);
        
                //记录考勤范围内的数据
                if(null != s && null != e && now > s.getTime() && now <= e.getTime()){
                    if(null != query1){
                        query1.setDuration(query1.getDuration() + ss);
                        driverOnlineMapper.updateById(query1);
                    }else{
                        query1 = new DriverOnline();
                        query1.setDriverId(driverId);
                        query1.setType(Integer.valueOf(ty));
                        query1.setDate(new Date());
                        query1.setAssessment(1);
                        query1.setDuration(ss);
                        driverOnlineMapper.insert(query1);
                    }
                }
            }else{
                DriverOnline driverOnline = new DriverOnline();
                driverOnline.setDriverId(driverId);
                driverOnline.setType(Integer.valueOf(ty));
                driverOnline.setDate(new Date());
                driverOnline.setAssessment(0);
                driverOnline.setDuration(0L);
                driverOnlineMapper.insert(driverOnline);
            }
            redisUtil.setStrValue("ONLINE_" + ty + "_" + driverId, String.valueOf(now / 1000));//存入秒
        }
    }
 
 
    /**
     * 获取给定日期范围的在线时长
     * @param driverId
     * @param assessment
     * @param start yyyy-MM-dd
     * @param end   yyyy-MM-dd
     * @return
     */
    @Override
    public Integer querySumTime(Integer driverId, Integer assessment, Date start, Date end) {
        return driverOnlineMapper.querySumTime(driverId, assessment, start, end);
    }
    
    
    /**
     * 定时检测司机是否连续接单
     */
    @Override
    public void deductionDuration() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date());
        List<DriverActivityHistory> driverActivityHistories = driverActivityHistoryMapper.selectList(new EntityWrapper<DriverActivityHistory>().eq("DATE_FORMAT(day, '%Y-%m-%d')", format).eq("type", 3).eq("carryOut", 1));
        List<Integer> collect = driverActivityHistories.stream().map(DriverActivityHistory::getDriverId).collect(Collectors.toList());
        if(collect.size() == 0){
            return;
        }
        for (DriverActivityHistory driverActivityHistory : driverActivityHistories) {
            DriverWork driverWork = driverWorkService.selectOne(new EntityWrapper<DriverWork>().eq("driverId", driverActivityHistory.getDriverId()).eq("state", 1));
            if(null == driverWork){
                continue;
            }
            DriverActivityOnline driverActivityOnline = driverActivityOnlineMapper.selectById(driverActivityHistory.getActivityId());
            DriverOnline driverOnline = this.selectOne(new EntityWrapper<DriverOnline>().eq("DATE_FORMAT(date, '%Y-%m-%d')", format)
                    .eq("driverId", driverActivityHistory.getDriverId()).eq("type", driverActivityOnline.getType()));
            if(null == driverOnline){
                continue;
            }
            Integer driverId = driverOnline.getDriverId();
            long m = Double.valueOf(driverActivityOnline.getOfflineTime() * 3600000L).longValue();
            
            //找出最后一次接单的时间
            long mm = 0L;
            OrderPrivateCar orderPrivateCar = orderPrivateCarService.selectOne(new EntityWrapper<OrderPrivateCar>().eq("driverId", driverId).eq("isDelete", 1).ne("state", 10));
            Date snatchOrderTime = null;
            if(null != orderPrivateCar){
                snatchOrderTime = orderPrivateCar.getSnatchOrderTime();
                mm = snatchOrderTime.getTime();
            }
            OrderLogistics orderLogistics = orderLogisticsService.selectOne(new EntityWrapper<OrderLogistics>().eq("driverId", driverId).eq("isDelete", 1).ne("state", 10));
            if(null != orderLogistics && snatchOrderTime.before(orderLogistics.getSnatchOrderTime())){
                mm = orderLogistics.getSnatchOrderTime().getTime();
            }
            
            //超时不接单,直接将之前的在线时长清零
            //推送司机下班提醒
            if(mm >= m){
                this.deleteById(driverOnline.getId());
                driverWork.setState(2);
                driverWork.setEndTime(new Date());
                driverWorkService.updateById(driverWork);
                Driver driver = driverService.selectById(driverWork.getDriverId());
                driver.setState(1);
                driverService.updateById(driver);
                // TODO 待翻译
                pushUtil.pushOffline(driverOnline.getDriverId(), 2, "您已连续" + driverActivityOnline.getOfflineTime() + "小时未接单,系统将强制更改您的状态为:下班");
            }
            
        }
        
        
    }
}