RiderLocationServiceImpl.java
12.4 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package com.diligrp.rider.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.diligrp.rider.dto.DeliveryPricingConfigDTO;
import com.diligrp.rider.dto.LocationDTO;
import com.diligrp.rider.entity.Orders;
import com.diligrp.rider.entity.Rider;
import com.diligrp.rider.entity.RiderLocation;
import com.diligrp.rider.mapper.OrdersMapper;
import com.diligrp.rider.mapper.RiderLocationMapper;
import com.diligrp.rider.mapper.RiderMapper;
import com.diligrp.rider.service.CityService;
import com.diligrp.rider.service.DispatchRuleService;
import com.diligrp.rider.service.RiderLocationService;
import com.diligrp.rider.util.GeoUtil;
import com.diligrp.rider.vo.AdminRiderDashboardVO;
import com.diligrp.rider.vo.AdminRiderLocationVO;
import com.diligrp.rider.vo.DispatchRuleTemplateVO;
import com.diligrp.rider.vo.NearbyRiderVO;
import com.diligrp.rider.websocket.LocationPushService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Service
@RequiredArgsConstructor
@Slf4j
public class RiderLocationServiceImpl implements RiderLocationService {
private static final long ACTIVE_WINDOW_SECONDS = 300;
private final RiderLocationMapper locationMapper;
private final RiderMapper riderMapper;
private final OrdersMapper ordersMapper;
private final CityService cityService;
private final DispatchRuleService dispatchRuleService;
private final LocationPushService locationPushService;
/**
* 按骑手维度更新最新位置。
*/
@Override
public void updateLocation(Long riderId, LocationDTO dto) {
long now = System.currentTimeMillis() / 1000;
log.debug("更新骑手位置,参数 riderId={} lng={} lat={} updateTime={}",
riderId, dto.getLng(), dto.getLat(), now);
Rider rider = riderMapper.selectById(riderId);
locationMapper.upsertLocation(
riderId,
dto.getLng().stripTrailingZeros().toPlainString(),
dto.getLat().stripTrailingZeros().toPlainString(),
now);
if (rider != null && rider.getCityId() != null) {
AdminRiderLocationVO location = new AdminRiderLocationVO();
location.setRiderId(riderId);
location.setRiderName(rider.getUserNickname());
location.setCityId(rider.getCityId());
location.setLng(dto.getLng());
location.setLat(dto.getLat());
location.setUpdateTime(now);
locationPushService.pushRiderLocation(location);
}
}
/**
* 查询当前骑手最近一次上报的位置。
*/
@Override
public LocationDTO getLocation(Long riderId) {
log.debug("查询骑手当前位置,参数 riderId={}", riderId);
RiderLocation loc = locationMapper.selectOne(new LambdaQueryWrapper<RiderLocation>()
.eq(RiderLocation::getUid, riderId));
if (loc == null) {
log.debug("未查询到骑手位置,riderId={}", riderId);
return null;
}
LocationDTO dto = new LocationDTO();
dto.setLng(new BigDecimal(loc.getLng()));
dto.setLat(new BigDecimal(loc.getLat()));
return dto;
}
/**
* 查询指定商铺最近活跃的骑手位置列表。
*/
@Override
public List<AdminRiderLocationVO> listActiveByCity(Long cityId) {
List<AdminRiderLocationVO> result = new ArrayList<>();
if (cityId == null || cityId < 1) {
return result;
}
List<Rider> riders = riderMapper.selectList(new LambdaQueryWrapper<Rider>()
.eq(Rider::getCityId, cityId)
.eq(Rider::getIsRest, 0)
.eq(Rider::getUserStatus, 1));
if (riders.isEmpty()) {
return result;
}
List<Long> riderIds = riders.stream().map(Rider::getId).toList();
long minUpdateTime = System.currentTimeMillis() / 1000 - ACTIVE_WINDOW_SECONDS;
List<RiderLocation> locations = locationMapper.selectList(
new LambdaQueryWrapper<RiderLocation>()
.in(RiderLocation::getUid, riderIds)
.ge(RiderLocation::getUpdateTime, minUpdateTime));
if (locations.isEmpty()) {
return result;
}
java.util.Map<Long, Rider> riderMap = riders.stream()
.collect(java.util.stream.Collectors.toMap(Rider::getId, rider -> rider));
for (RiderLocation location : locations) {
Rider rider = riderMap.get(location.getUid());
if (rider == null) {
continue;
}
try {
AdminRiderLocationVO vo = new AdminRiderLocationVO();
vo.setRiderId(rider.getId());
vo.setRiderName(rider.getUserNickname());
vo.setCityId(rider.getCityId());
vo.setLng(new BigDecimal(location.getLng()));
vo.setLat(new BigDecimal(location.getLat()));
vo.setUpdateTime(location.getUpdateTime());
result.add(vo);
} catch (Exception ignored) {
}
}
log.debug("查询商铺活跃骑手位置完成,cityId={} count={}", cityId, result.size());
return result;
}
/**
* 查询指定商铺骑手看板数据。
*/
@Override
public List<AdminRiderDashboardVO> listDashboardByCity(Long cityId) {
List<AdminRiderDashboardVO> result = new ArrayList<>();
log.debug("开始查询商铺骑手看板,参数 cityId={}", cityId);
if (cityId == null || cityId < 1) {
return result;
}
DispatchRuleTemplateVO rule = dispatchRuleService.getActiveRule(cityId);
Integer maxHoldOrderCount = normalizeMaxHoldOrderCount(rule);
List<Rider> riders = riderMapper.selectList(new LambdaQueryWrapper<Rider>()
.eq(Rider::getCityId, cityId)
.orderByDesc(Rider::getId));
if (riders.isEmpty()) {
log.debug("商铺下没有骑手,cityId={}", cityId);
return result;
}
List<Long> riderIds = riders.stream().map(Rider::getId).toList();
List<RiderLocation> locations = locationMapper.selectList(
new LambdaQueryWrapper<RiderLocation>().in(RiderLocation::getUid, riderIds));
List<Orders> holdingOrders = ordersMapper.selectList(new LambdaQueryWrapper<Orders>()
.eq(Orders::getCityId, cityId)
.in(Orders::getStatus, List.of(3, 4))
.in(Orders::getRiderId, riderIds)
.eq(Orders::getIsDel, 0));
log.debug("商铺骑手看板基础数据查询完成,cityId={} riderCount={} locationCount={} holdingOrderCount={} maxHoldOrderCount={}",
cityId, riders.size(), locations.size(), holdingOrders.size(), maxHoldOrderCount);
java.util.Map<Long, RiderLocation> locationMap = new java.util.HashMap<>();
for (RiderLocation location : locations) {
RiderLocation current = locationMap.get(location.getUid());
if (current == null || (current.getUpdateTime() != null
&& location.getUpdateTime() != null
&& location.getUpdateTime() > current.getUpdateTime())) {
locationMap.put(location.getUid(), location);
}
}
java.util.Map<Long, Integer> holdCountMap = new java.util.HashMap<>();
for (Orders order : holdingOrders) {
holdCountMap.merge(order.getRiderId(), 1, Integer::sum);
}
for (Rider rider : riders) {
AdminRiderDashboardVO vo = new AdminRiderDashboardVO();
vo.setRiderId(rider.getId());
vo.setRiderName(rider.getUserNickname());
vo.setMobile(rider.getMobile());
vo.setStatus(buildRiderStatus(rider, locationMap.get(rider.getId())));
int holdOrderCount = holdCountMap.getOrDefault(rider.getId(), 0);
vo.setHoldOrderCount(holdOrderCount);
vo.setMaxHoldOrderCount(maxHoldOrderCount);
vo.setLoadRate(calcLoadRate(holdOrderCount, maxHoldOrderCount));
RiderLocation location = locationMap.get(rider.getId());
if (location != null) {
vo.setLastLocationTime(location.getUpdateTime());
try {
vo.setLng(new BigDecimal(location.getLng()));
vo.setLat(new BigDecimal(location.getLat()));
} catch (Exception ignored) {
}
}
result.add(vo);
}
log.debug("查询骑手看板完成,cityId={} count={}", cityId, result.size());
return result;
}
/**
* 查询商铺范围内最近活跃的附近骑手。
*/
@Override
public List<NearbyRiderVO> getNearby(Long cityId, BigDecimal lng, BigDecimal lat) {
List<NearbyRiderVO> result = new ArrayList<>();
log.debug("开始查询附近骑手,参数 cityId={} lng={} lat={}", cityId, lng, lat);
if (cityId == null || cityId < 1 || lng == null || lat == null) {
return result;
}
DeliveryPricingConfigDTO config = cityService.getConfig(cityId);
if (config == null) {
return result;
}
BigDecimal limitKm = config.getRiderDistance();
if (limitKm == null || limitKm.compareTo(BigDecimal.ZERO) <= 0) {
limitKm = BigDecimal.valueOf(3);
}
List<Rider> riders = riderMapper.selectList(new LambdaQueryWrapper<Rider>()
.eq(Rider::getCityId, cityId)
.eq(Rider::getIsRest, 0)
.eq(Rider::getUserStatus, 1));
if (riders.isEmpty()) {
log.debug("当前商铺没有在线骑手,cityId={}", cityId);
return result;
}
List<Long> riderIds = riders.stream().map(Rider::getId).toList();
long minUpdateTime = System.currentTimeMillis() / 1000 - ACTIVE_WINDOW_SECONDS;
List<RiderLocation> locations = locationMapper.selectList(
new LambdaQueryWrapper<RiderLocation>()
.in(RiderLocation::getUid, riderIds)
.ge(RiderLocation::getUpdateTime, minUpdateTime));
double queryLat = lat.doubleValue();
double queryLng = lng.doubleValue();
double limitD = limitKm.doubleValue();
for (RiderLocation loc : locations) {
try {
double dist = GeoUtil.calcDistanceKm(
queryLat, queryLng,
Double.parseDouble(loc.getLat()),
Double.parseDouble(loc.getLng()));
if (dist <= limitD) {
NearbyRiderVO vo = new NearbyRiderVO();
vo.setLng(loc.getLng());
vo.setLat(loc.getLat());
vo.setDistance(dist);
result.add(vo);
}
} catch (Exception ignored) {
}
}
log.debug("附近骑手查询完成,cityId={} 在线骑手数={} 位置记录数={} 命中数={}",
cityId, riders.size(), locations.size(), result.size());
return result;
}
private String buildRiderStatus(Rider rider, RiderLocation location) {
if (rider.getUserStatus() == null || rider.getUserStatus() != 1) {
return "审核中";
}
if (rider.getStatus() == null || rider.getStatus() != 1) {
return "已禁用";
}
if (rider.getIsRest() != null && rider.getIsRest() == 1) {
return "休息中";
}
long now = System.currentTimeMillis() / 1000;
if (location == null || location.getUpdateTime() == null
|| now - location.getUpdateTime() > ACTIVE_WINDOW_SECONDS) {
return "离线";
}
return "在线";
}
private Integer normalizeMaxHoldOrderCount(DispatchRuleTemplateVO rule) {
if (rule == null || rule.getGrabMaxPerRider() == null || rule.getGrabMaxPerRider() <= 0) {
return null;
}
return rule.getGrabMaxPerRider();
}
private Integer calcLoadRate(int holdOrderCount, Integer maxHoldOrderCount) {
if (maxHoldOrderCount == null || maxHoldOrderCount <= 0) {
return null;
}
int rate = holdOrderCount * 100 / maxHoldOrderCount;
return Math.min(rate, 100);
}
}