xuhy
2025-09-05 016ddc332273c36e437a6f3b5f0a25574d5b7b78
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.stylefeng.guns.modular.system.service.impl;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar;
import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService;
import com.stylefeng.guns.modular.specialTrain.server.impl.OrderPrivateCarServiceImpl;
import com.stylefeng.guns.modular.system.dao.DriverMapper;
import com.stylefeng.guns.modular.system.dao.DriverServiceMapper;
import com.stylefeng.guns.modular.system.model.Company;
import com.stylefeng.guns.modular.system.model.Driver;
import com.stylefeng.guns.modular.system.model.DriverService;
import com.stylefeng.guns.modular.system.service.ICompanyCityService;
import com.stylefeng.guns.modular.system.service.IDriverService;
import com.stylefeng.guns.modular.system.util.GDMapElectricFenceUtil;
import com.stylefeng.guns.modular.system.util.GDMapGeocodingUtil;
import com.stylefeng.guns.modular.system.util.RedisUtil;
import com.stylefeng.guns.modular.system.warpper.BaseWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {
 
    @Resource
    private DriverMapper driverMapper;
 
    @Resource
    private DriverServiceMapper driverServiceMapper;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private GDMapElectricFenceUtil gdMapElectricFenceUtil;
 
    @Autowired
    private ICompanyCityService companyCityService;
    @Autowired
    private OrderPrivateCarServiceImpl orderPrivateCarServiceImpl;
 
    @Autowired
    private IOrderPrivateCarService orderPrivateCarService;
 
 
    /**
     * 获取distance公里内空闲司机数量
     * @param type  业务类型(1=专车,2=快车,3=城际,4=小件物流-同城,5=小件物流-跨城,6=公务用车)
     * @param lon
     * @param lat
     * @return
     * @throws Exception
     */
    @Override
    public List<Driver> queryIdleDriver(Integer type, Double lon, Double lat, Double distance, Integer companyId) throws Exception {
        List<Driver> drivers = driverMapper.queryIdleDriver(type, companyId);
        List<Driver> list = new ArrayList<>();
        for(Driver driver : drivers){
            String value = redisUtil.getValue("DRIVER" + String.valueOf(driver.getId()));
            if(null != value){
                /*Map<String, String> distance1 = gdMapElectricFenceUtil.getDistance(lon + "," + lat, value, 0);//计算距离
                double d = Double.valueOf(distance1.get("distance")).doubleValue();*/
                double d = GDMapGeocodingUtil.getDistance(lat,lon,Double.parseDouble(value.split(",")[1]),Double.parseDouble(value.split(",")[0]));
                if(d < (distance * 1000)){
                    list.add(driver);
                }
            }else{
//                System.err.println(driver.getName() + "-----------------没有上传经纬度----------------");
            }
        }
        return list;
    }
 
 
    /**
     * 获取给定车型且空闲的司机
     * @param type
     * @param serverCarModelId
     * @param lon
     * @param lat
     * @param distance
     * @param companyId
     * @return
     * @throws Exception
     */
    @Override
    public List<Driver> queryIdleDriver(Integer type, Integer serverCarModelId, Double lon, Double lat, Double distance, Integer companyId) throws Exception {
        List<Driver> drivers = driverMapper.queryIdleDriver_(type, serverCarModelId, companyId);
        List<Driver> list = new ArrayList<>();
        for(Driver driver : drivers){
            int count = orderPrivateCarService.selectCount(new EntityWrapper<OrderPrivateCar>().eq("driverId", driver.getId())
                    .in("state", Arrays.asList(2, 3, 4, 5, 6, 11)).eq("isDelete", 1));
            if(0 != count){
                continue;
            }
            String value = redisUtil.getValue("DRIVER" + String.valueOf(driver.getId()));
            if(null != value){
                /*Map<String, String> distance1 = gdMapElectricFenceUtil.getDistance(lon + "," + lat, value, 0);//计算距离
                double d = Double.valueOf(distance1.get("distance")).doubleValue();*/
//                double d = GDMapGeocodingUtil.getDistance(lat,lon,Double.parseDouble(value.split(",")[1]),Double.parseDouble(value.split(",")[0]));
                double d = gdMapElectricFenceUtil.getDrivingRoute(lat,lon,Double.parseDouble(value.split(",")[1]),Double.parseDouble(value.split(",")[0]));
                if(d < (distance * 1000)){
                    list.add(driver);
                }
            }
        }
        return list;
    }
    /**
     * 获取给定车型且空闲的司机
     * @param type
     * @param serverCarModelId
     * @param lon
     * @param lat
     * @param distance
     * @param companyId
     * @return
     */
    @Override
    public List<Driver> queryIdleDriver_(Integer type, Integer serverCarModelId, Double lon, Double lat, Double distance, Integer companyId) {
        List<Driver> drivers = driverMapper.queryIdleDriverService_(type, serverCarModelId, companyId);
        List<Driver> list = new ArrayList<>();
        for(Driver driver : drivers){
            int count = orderPrivateCarService.selectCount(new EntityWrapper<OrderPrivateCar>().eq("driverId", driver.getId())
                    .in("state", Arrays.asList(2, 3, 4, 11))
                    .ne("orderType",2)
                    .eq("isDelete", 1));
            if(0 != count){
                continue;
            }
            String value = redisUtil.getValue("DRIVER" + driver.getId());
            if(null != value){
                /*Map<String, String> distance1 = gdMapElectricFenceUtil.getDistance(lon + "," + lat, value, 0);//计算距离
                double d = Double.valueOf(distance1.get("distance")).doubleValue();*/
//                double d = GDMapGeocodingUtil.getDistance(lat,lon,Double.parseDouble(value.split(",")[1]),Double.parseDouble(value.split(",")[0]));
                double d = gdMapElectricFenceUtil.getDrivingRoute(lat,lon,Double.parseDouble(value.split(",")[1]),Double.parseDouble(value.split(",")[0]));
                if(d < (distance * 1000)){
                    list.add(driver);
                }
            }
        }
        return list;
    }
 
 
    /**
     * 根据订单id获取司机数据
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> queryOrderDriver(Integer orderId, Integer orderType) throws Exception {
        return driverMapper.queryOrderDriver(orderId, orderType);
    }
 
 
    /**
     * 获取司机详情
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> queryDriverInfo(Integer id) throws Exception {
        Map<String, Object> map = driverMapper.queryDriverInfo(id);
        return map;
    }
 
 
    /**
     * 获取司机的业务类型
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List<BaseWarpper> queryBusiness(Integer uid) throws Exception {
        List<DriverService> list = driverServiceMapper.queryBusiness(uid, null);
        List<BaseWarpper> maps = new ArrayList<>();
        for (DriverService d : list){
            BaseWarpper baseWarpper = new BaseWarpper();
            baseWarpper.setId(d.getType());
            switch (d.getType()){
                case 1:
                    baseWarpper.setName("专车");
                    break;
                case 2:
                    baseWarpper.setName("快车");
                    break;
                case 3:
                    baseWarpper.setName("机场专线");
                    break;
                case 4:
                    baseWarpper.setName("同城小件物流");
                    break;
                case 5:
                    baseWarpper.setName("跨城小件物流");
                    break;
            }
            maps.add(baseWarpper);
        }
        return maps;
    }
 
    @Override
    public List<Driver> queryDriverLimitTen(Integer type, Double lon, Double lat) {
        List<Driver> drivers = driverMapper.queryIdleDriver(type, null);
        if(CollectionUtils.isEmpty(drivers)){
            return new ArrayList<>();
        }
        Iterator<Driver> iterator = drivers.iterator();
        while (iterator.hasNext()){
            Driver driver = iterator.next();
            String value = redisUtil.getValue("DRIVER" + driver.getId());
            if(null != value){
                double d = GDMapGeocodingUtil.getDistance(lat,lon,Double.parseDouble(value.split(",")[1]),Double.parseDouble(value.split(",")[0]));
                driver.setLon(Double.parseDouble(value.split(",")[0]));
                driver.setLat(Double.parseDouble(value.split(",")[1]));
                driver.setDistance(d);
            }else {
                System.err.println(driver.getName() + "------没有上传经纬度------");
                iterator.remove();
            }
        }
        if(drivers.size() > 10){
            drivers.sort(Comparator.comparing(Driver::getDistance));
            return drivers.stream().limit(10).collect(Collectors.toList());
        }else {
            return drivers;
        }
    }
}