AdminRiderController.java
12.2 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
package com.diligrp.rider.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.diligrp.rider.common.result.Result;
import com.diligrp.rider.dto.AdminRiderAddDTO;
import com.diligrp.rider.entity.Orders;
import com.diligrp.rider.entity.Rider;
import com.diligrp.rider.mapper.OrdersMapper;
import com.diligrp.rider.service.AdminRiderService;
import com.diligrp.rider.vo.DeliveryOrderCreateVO;
import com.diligrp.rider.vo.OrderListPageVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/admin/rider")
@RequiredArgsConstructor
public class AdminRiderController {
private final AdminRiderService adminRiderService;
private final OrdersMapper ordersMapper;
/** 手动新增骑手(分站管理员自动绑定本城市) */
@PostMapping("/add")
public Result<Void> add(@Valid @RequestBody AdminRiderAddDTO dto, HttpServletRequest request) {
Long cityId = "substation".equals(request.getAttribute("role"))
? (Long) request.getAttribute("cityId")
: dto.getCityId();
adminRiderService.add(dto, cityId);
return Result.success();
}
/** 骑手列表(分站管理员仅看本城市,超管看全部) */
@GetMapping("/list")
public Result<List<Rider>> list(
@RequestParam(required = false) String keyword,
@RequestParam(required = false) Integer userStatus,
HttpServletRequest request) {
Long cityId = "substation".equals(request.getAttribute("role"))
? (Long) request.getAttribute("cityId")
: null;
return Result.success(adminRiderService.list(keyword, userStatus, cityId));
}
/** 指派骑手候选列表 */
@GetMapping("/order/candidates")
public Result<List<Rider>> designateCandidates(@RequestParam Long orderId, HttpServletRequest request) {
Long cityId = "substation".equals(request.getAttribute("role"))
? (Long) request.getAttribute("cityId")
: null;
return Result.success(adminRiderService.designateCandidates(orderId, cityId));
}
/** 审核骑手:status=0拒绝 1通过 */
@PostMapping("/setStatus")
public Result<Void> setStatus(@RequestParam Long riderId,
@RequestParam int status,
HttpServletRequest request) {
adminRiderService.setStatus(riderId, status, resolveScopedCityId(request));
return Result.success();
}
/** 设置骑手等级,levelId 为空时使用默认等级 */
@PostMapping("/setLevel")
public Result<Void> setLevel(@RequestParam Long riderId,
@RequestParam(required = false) Long levelId,
HttpServletRequest request) {
Long cityId = "substation".equals(request.getAttribute("role"))
? (Long) request.getAttribute("cityId")
: null;
adminRiderService.setLevel(riderId, levelId, cityId);
return Result.success();
}
/** 启用/禁用骑手账号:status=0禁用 1启用 */
@PostMapping("/setEnableStatus")
public Result<Void> setEnableStatus(@RequestParam Long riderId,
@RequestParam int status,
HttpServletRequest request) {
adminRiderService.setEnableStatus(riderId, status, resolveScopedCityId(request));
return Result.success();
}
/** 切换骑手类型:type=1兼职 2全职 */
@PostMapping("/setType")
public Result<Void> setType(@RequestParam Long riderId,
@RequestParam int type,
HttpServletRequest request) {
adminRiderService.setType(riderId, type, resolveScopedCityId(request));
return Result.success();
}
/** 设置个人持单上限:0=不限制 */
@PostMapping("/setHoldOrderLimit")
public Result<Void> setHoldOrderLimit(@RequestParam Long riderId,
@RequestParam int holdOrderLimit,
HttpServletRequest request) {
adminRiderService.setHoldOrderLimit(riderId, holdOrderLimit, resolveScopedCityId(request));
return Result.success();
}
/** 指派骑手接单 */
@PostMapping("/order/designate")
public Result<Void> designate(@RequestParam Long orderId,
@RequestParam Long riderId,
HttpServletRequest request) {
adminRiderService.designate(orderId, riderId, resolveScopedCityId(request));
return Result.success();
}
/** 处理转单申请:trans=1通过 3拒绝 */
@PostMapping("/order/setTrans")
public Result<Void> setTrans(@RequestParam Long orderId,
@RequestParam int trans,
HttpServletRequest request) {
adminRiderService.setTrans(orderId, trans, resolveScopedCityId(request));
return Result.success();
}
/**
* 订单列表(分站管理员查看本城市订单 / 超管查看所有)
* 分站管理员:cityId 从 token 自动注入,无需传参
* 超管:可传 cityId 筛选某城市
*/
@GetMapping("/order/list")
public Result<OrderListPageVO> orderList(
@RequestParam(required = false) Long cityId,
@RequestParam(required = false) Integer status,
@RequestParam(required = false) Integer isTrans,
@RequestParam(required = false) String keyword,
@RequestParam(defaultValue = "1") int page,
HttpServletRequest request) {
final int pageSize = 20;
int currentPage = Math.max(page, 1);
int offset = (currentPage - 1) * pageSize;
Long scopedCityId = resolveQueryCityId(request, cityId);
LambdaQueryWrapper<Orders> countWrapper = buildOrderListWrapper(scopedCityId, status, isTrans, keyword, false);
long total = ordersMapper.selectCount(countWrapper);
LambdaQueryWrapper<Orders> listWrapper = buildOrderListWrapper(scopedCityId, status, isTrans, keyword, true);
listWrapper.last("LIMIT " + offset + "," + pageSize);
OrderListPageVO result = new OrderListPageVO();
result.setList(ordersMapper.selectList(listWrapper));
result.setPage(currentPage);
result.setPageSize(pageSize);
result.setTotal(total);
result.setStatusSummary(buildStatusSummary(scopedCityId, isTrans, keyword));
return Result.success(result);
}
/**
* 配送订单列表(平台管理员查看外部系统推入的订单)
* 按 appKey / outOrderNo 查询
*/
@GetMapping("/order/delivery/list")
public Result<OrderListPageVO> deliveryOrderList(
@RequestParam(required = false) Long cityId,
@RequestParam(required = false) String appKey,
@RequestParam(required = false) String outOrderNo,
@RequestParam(required = false) Integer status,
@RequestParam(defaultValue = "1") int page,
HttpServletRequest request) {
final int pageSize = 20;
int currentPage = Math.max(page, 1);
int offset = (currentPage - 1) * pageSize;
Long scopedCityId = resolveQueryCityId(request, cityId);
LambdaQueryWrapper<Orders> countWrapper = buildDeliveryOrderListWrapper(scopedCityId, appKey, outOrderNo, status, false);
long total = ordersMapper.selectCount(countWrapper);
LambdaQueryWrapper<Orders> listWrapper = buildDeliveryOrderListWrapper(scopedCityId, appKey, outOrderNo, status, true);
listWrapper.last("LIMIT " + offset + "," + pageSize);
OrderListPageVO result = new OrderListPageVO();
result.setList(ordersMapper.selectList(listWrapper));
result.setPage(currentPage);
result.setPageSize(pageSize);
result.setTotal(total);
return Result.success(result);
}
@GetMapping("/order/delivery/detail")
public Result<DeliveryOrderCreateVO> deliveryDetail(@RequestParam Long orderId, HttpServletRequest request) {
return Result.success(adminRiderService.deliveryDetail(orderId, resolveScopedCityId(request)));
}
@PostMapping("/order/delivery/cancel")
public Result<Void> cancelDeliveryOrder(@RequestParam Long orderId, HttpServletRequest request) {
adminRiderService.cancelDeliveryOrder(orderId, resolveScopedCityId(request));
return Result.success();
}
private Long resolveScopedCityId(HttpServletRequest request) {
return "substation".equals(request.getAttribute("role"))
? (Long) request.getAttribute("cityId")
: null;
}
private Long resolveQueryCityId(HttpServletRequest request, Long queryCityId) {
Long scopedCityId = resolveScopedCityId(request);
return scopedCityId != null ? scopedCityId : queryCityId;
}
private LambdaQueryWrapper<Orders> buildOrderListWrapper(Long cityId,
Integer status,
Integer isTrans,
String keyword,
boolean withOrder) {
LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<Orders>()
.eq(Orders::getIsDel, 0);
if (withOrder) {
wrapper.orderByDesc(Orders::getId);
}
if (cityId != null) {
wrapper.eq(Orders::getCityId, cityId);
}
if (status != null) {
wrapper.eq(Orders::getStatus, status);
}
if (isTrans != null) {
wrapper.eq(Orders::getIsTrans, isTrans);
}
if (keyword != null && !keyword.isBlank()) {
wrapper.and(w -> w.like(Orders::getOrderNo, keyword)
.or().like(Orders::getOutOrderNo, keyword)
.or().like(Orders::getRecipName, keyword)
.or().like(Orders::getRecipPhone, keyword));
}
return wrapper;
}
private Map<String, Long> buildStatusSummary(Long cityId, Integer isTrans, String keyword) {
Map<String, Long> summary = new LinkedHashMap<>();
summary.put("all", countOrders(cityId, null, isTrans, keyword));
summary.put("2", countOrders(cityId, 2, isTrans, keyword));
summary.put("3", countOrders(cityId, 3, isTrans, keyword));
summary.put("4", countOrders(cityId, 4, isTrans, keyword));
summary.put("6", countOrders(cityId, 6, isTrans, keyword));
summary.put("7", countOrders(cityId, 7, isTrans, keyword));
summary.put("10", countOrders(cityId, 10, isTrans, keyword));
return summary;
}
private long countOrders(Long cityId, Integer status, Integer isTrans, String keyword) {
return ordersMapper.selectCount(buildOrderListWrapper(cityId, status, isTrans, keyword, false));
}
private LambdaQueryWrapper<Orders> buildDeliveryOrderListWrapper(Long cityId,
String appKey,
String outOrderNo,
Integer status,
boolean withOrder) {
LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<Orders>()
.eq(Orders::getIsDel, 0);
if (withOrder) {
wrapper.orderByDesc(Orders::getId);
}
if (cityId != null) {
wrapper.eq(Orders::getCityId, cityId);
}
if (appKey != null && !appKey.isBlank()) {
wrapper.eq(Orders::getAppKey, appKey);
}
if (outOrderNo != null && !outOrderNo.isBlank()) {
wrapper.like(Orders::getOutOrderNo, outOrderNo);
}
if (status != null) {
wrapper.eq(Orders::getStatus, status);
}
return wrapper;
}
}