Commit 33eee1ba596424838e9b4a79b3179712734dd722

Authored by shaofan
1 parent 9db921f5

优化订单列表分页与状态统计逻辑,新增 `OrderListPageVO` 用于规范返回数据格式

src/main/java/com/diligrp/rider/controller/AdminRiderController.java
... ... @@ -8,12 +8,15 @@ import com.diligrp.rider.entity.Rider;
8 8 import com.diligrp.rider.mapper.OrdersMapper;
9 9 import com.diligrp.rider.service.AdminRiderService;
10 10 import com.diligrp.rider.vo.DeliveryOrderCreateVO;
  11 +import com.diligrp.rider.vo.OrderListPageVO;
11 12 import jakarta.servlet.http.HttpServletRequest;
12 13 import jakarta.validation.Valid;
13 14 import lombok.RequiredArgsConstructor;
14 15 import org.springframework.web.bind.annotation.*;
15 16  
  17 +import java.util.LinkedHashMap;
16 18 import java.util.List;
  19 +import java.util.Map;
17 20  
18 21 @RestController
19 22 @RequestMapping("/api/admin/rider")
... ... @@ -126,34 +129,31 @@ public class AdminRiderController {
126 129 * 超管:可传 cityId 筛选某城市
127 130 */
128 131 @GetMapping("/order/list")
129   - public Result<List<Orders>> orderList(
  132 + public Result<OrderListPageVO> orderList(
130 133 @RequestParam(required = false) Long cityId,
131 134 @RequestParam(required = false) Integer status,
132 135 @RequestParam(required = false) Integer isTrans,
133 136 @RequestParam(required = false) String keyword,
134 137 @RequestParam(defaultValue = "1") int page,
135 138 HttpServletRequest request) {
  139 + final int pageSize = 20;
  140 + int currentPage = Math.max(page, 1);
  141 + int offset = (currentPage - 1) * pageSize;
136 142 Long scopedCityId = resolveQueryCityId(request, cityId);
137 143  
138   - LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<Orders>()
139   - .eq(Orders::getIsDel, 0)
140   - .orderByDesc(Orders::getId);
  144 + LambdaQueryWrapper<Orders> countWrapper = buildOrderListWrapper(scopedCityId, status, isTrans, keyword, false);
  145 + long total = ordersMapper.selectCount(countWrapper);
141 146  
142   - if (scopedCityId != null) {
143   - wrapper.eq(Orders::getCityId, scopedCityId);
144   - }
145   - if (status != null) wrapper.eq(Orders::getStatus, status);
146   - if (isTrans != null) wrapper.eq(Orders::getIsTrans, isTrans);
147   - if (keyword != null && !keyword.isBlank()) {
148   - wrapper.and(w -> w.like(Orders::getOrderNo, keyword)
149   - .or().like(Orders::getOutOrderNo, keyword)
150   - .or().like(Orders::getRecipName, keyword)
151   - .or().like(Orders::getRecipPhone, keyword));
152   - }
153   - int offset = (page - 1) * 20;
154   - wrapper.last("LIMIT " + offset + ",20");
  147 + LambdaQueryWrapper<Orders> listWrapper = buildOrderListWrapper(scopedCityId, status, isTrans, keyword, true);
  148 + listWrapper.last("LIMIT " + offset + "," + pageSize);
155 149  
156   - return Result.success(ordersMapper.selectList(wrapper));
  150 + OrderListPageVO result = new OrderListPageVO();
  151 + result.setList(ordersMapper.selectList(listWrapper));
  152 + result.setPage(currentPage);
  153 + result.setPageSize(pageSize);
  154 + result.setTotal(total);
  155 + result.setStatusSummary(buildStatusSummary(scopedCityId, isTrans, keyword));
  156 + return Result.success(result);
157 157 }
158 158  
159 159 /**
... ... @@ -161,26 +161,30 @@ public class AdminRiderController {
161 161 * 按 appKey / outOrderNo 查询
162 162 */
163 163 @GetMapping("/order/delivery/list")
164   - public Result<List<Orders>> deliveryOrderList(
  164 + public Result<OrderListPageVO> deliveryOrderList(
165 165 @RequestParam(required = false) Long cityId,
166 166 @RequestParam(required = false) String appKey,
167 167 @RequestParam(required = false) String outOrderNo,
168 168 @RequestParam(required = false) Integer status,
169 169 @RequestParam(defaultValue = "1") int page,
170 170 HttpServletRequest request) {
171   - LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<Orders>()
172   - .eq(Orders::getIsDel, 0)
173   - .orderByDesc(Orders::getId);
  171 + final int pageSize = 20;
  172 + int currentPage = Math.max(page, 1);
  173 + int offset = (currentPage - 1) * pageSize;
174 174 Long scopedCityId = resolveQueryCityId(request, cityId);
175   - if (scopedCityId != null) {
176   - wrapper.eq(Orders::getCityId, scopedCityId);
177   - }
178   - if (appKey != null && !appKey.isBlank()) wrapper.eq(Orders::getAppKey, appKey);
179   - if (outOrderNo != null && !outOrderNo.isBlank()) wrapper.like(Orders::getOutOrderNo, outOrderNo);
180   - if (status != null) wrapper.eq(Orders::getStatus, status);
181   - int offset = (page - 1) * 20;
182   - wrapper.last("LIMIT " + offset + ",20");
183   - return Result.success(ordersMapper.selectList(wrapper));
  175 +
  176 + LambdaQueryWrapper<Orders> countWrapper = buildDeliveryOrderListWrapper(scopedCityId, appKey, outOrderNo, status, false);
  177 + long total = ordersMapper.selectCount(countWrapper);
  178 +
  179 + LambdaQueryWrapper<Orders> listWrapper = buildDeliveryOrderListWrapper(scopedCityId, appKey, outOrderNo, status, true);
  180 + listWrapper.last("LIMIT " + offset + "," + pageSize);
  181 +
  182 + OrderListPageVO result = new OrderListPageVO();
  183 + result.setList(ordersMapper.selectList(listWrapper));
  184 + result.setPage(currentPage);
  185 + result.setPageSize(pageSize);
  186 + result.setTotal(total);
  187 + return Result.success(result);
184 188 }
185 189  
186 190 @GetMapping("/order/delivery/detail")
... ... @@ -204,4 +208,73 @@ public class AdminRiderController {
204 208 Long scopedCityId = resolveScopedCityId(request);
205 209 return scopedCityId != null ? scopedCityId : queryCityId;
206 210 }
  211 +
  212 + private LambdaQueryWrapper<Orders> buildOrderListWrapper(Long cityId,
  213 + Integer status,
  214 + Integer isTrans,
  215 + String keyword,
  216 + boolean withOrder) {
  217 + LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<Orders>()
  218 + .eq(Orders::getIsDel, 0);
  219 + if (withOrder) {
  220 + wrapper.orderByDesc(Orders::getId);
  221 + }
  222 + if (cityId != null) {
  223 + wrapper.eq(Orders::getCityId, cityId);
  224 + }
  225 + if (status != null) {
  226 + wrapper.eq(Orders::getStatus, status);
  227 + }
  228 + if (isTrans != null) {
  229 + wrapper.eq(Orders::getIsTrans, isTrans);
  230 + }
  231 + if (keyword != null && !keyword.isBlank()) {
  232 + wrapper.and(w -> w.like(Orders::getOrderNo, keyword)
  233 + .or().like(Orders::getOutOrderNo, keyword)
  234 + .or().like(Orders::getRecipName, keyword)
  235 + .or().like(Orders::getRecipPhone, keyword));
  236 + }
  237 + return wrapper;
  238 + }
  239 +
  240 + private Map<String, Long> buildStatusSummary(Long cityId, Integer isTrans, String keyword) {
  241 + Map<String, Long> summary = new LinkedHashMap<>();
  242 + summary.put("all", countOrders(cityId, null, isTrans, keyword));
  243 + summary.put("2", countOrders(cityId, 2, isTrans, keyword));
  244 + summary.put("3", countOrders(cityId, 3, isTrans, keyword));
  245 + summary.put("4", countOrders(cityId, 4, isTrans, keyword));
  246 + summary.put("6", countOrders(cityId, 6, isTrans, keyword));
  247 + summary.put("7", countOrders(cityId, 7, isTrans, keyword));
  248 + summary.put("10", countOrders(cityId, 10, isTrans, keyword));
  249 + return summary;
  250 + }
  251 +
  252 + private long countOrders(Long cityId, Integer status, Integer isTrans, String keyword) {
  253 + return ordersMapper.selectCount(buildOrderListWrapper(cityId, status, isTrans, keyword, false));
  254 + }
  255 +
  256 + private LambdaQueryWrapper<Orders> buildDeliveryOrderListWrapper(Long cityId,
  257 + String appKey,
  258 + String outOrderNo,
  259 + Integer status,
  260 + boolean withOrder) {
  261 + LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<Orders>()
  262 + .eq(Orders::getIsDel, 0);
  263 + if (withOrder) {
  264 + wrapper.orderByDesc(Orders::getId);
  265 + }
  266 + if (cityId != null) {
  267 + wrapper.eq(Orders::getCityId, cityId);
  268 + }
  269 + if (appKey != null && !appKey.isBlank()) {
  270 + wrapper.eq(Orders::getAppKey, appKey);
  271 + }
  272 + if (outOrderNo != null && !outOrderNo.isBlank()) {
  273 + wrapper.like(Orders::getOutOrderNo, outOrderNo);
  274 + }
  275 + if (status != null) {
  276 + wrapper.eq(Orders::getStatus, status);
  277 + }
  278 + return wrapper;
  279 + }
207 280 }
... ...
src/main/java/com/diligrp/rider/vo/OrderListPageVO.java 0 → 100644
  1 +package com.diligrp.rider.vo;
  2 +
  3 +import com.diligrp.rider.entity.Orders;
  4 +import lombok.Data;
  5 +
  6 +import java.util.List;
  7 +import java.util.Map;
  8 +
  9 +@Data
  10 +public class OrderListPageVO {
  11 + private List<Orders> list;
  12 + private Integer page;
  13 + private Integer pageSize;
  14 + private Long total;
  15 + private Map<String, Long> statusSummary;
  16 +}
... ...