mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 06:29:20 +08:00
perf(订单): 列表分页走时间索引与延迟关联,批量组装 VO,前端默认 30 天下单时间范围
This commit is contained in:
+7
@@ -5,6 +5,7 @@ import org.apache.ibatis.annotations.Mapper;
|
|||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
import org.apache.ibatis.annotations.Update;
|
import org.apache.ibatis.annotations.Update;
|
||||||
import org.apache.ibatis.annotations.Select;
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto.OrderListQueryParam;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.OrderShopStatDateRow;
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.OrderShopStatDateRow;
|
||||||
|
|
||||||
@@ -83,4 +84,10 @@ public interface OrderMapper extends BaseMapper<OrderPO> {
|
|||||||
List<OrderShopStatDateRow> selectOrphanOrderStatDatesBySpecCode(
|
List<OrderShopStatDateRow> selectOrphanOrderStatDatesBySpecCode(
|
||||||
@Param("companyId") Long companyId,
|
@Param("companyId") Long companyId,
|
||||||
@Param("specCode") String specCode);
|
@Param("specCode") String specCode);
|
||||||
|
|
||||||
|
/** 列表分页:走时间索引的 COUNT */
|
||||||
|
long countOrderList(OrderListQueryParam param);
|
||||||
|
|
||||||
|
/** 列表分页:先查 id 再回表,避免大结果集 filesort */
|
||||||
|
List<Long> selectOrderListIds(OrderListQueryParam param);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -21,9 +21,9 @@ public class OrderFilterDTO {
|
|||||||
private Long shopId;
|
private Long shopId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 平台订单号(模糊查询)
|
* 平台订单号:无 %/_ 时按前缀匹配(可走索引);含通配符时全模糊
|
||||||
*/
|
*/
|
||||||
@Schema(description = "平台订单号(模糊查询)", example = "TB2024")
|
@Schema(description = "平台订单号(默认前缀匹配;含%/_ 时全模糊)", example = "TB2024")
|
||||||
private String platformOrderNo;
|
private String platformOrderNo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+36
@@ -0,0 +1,36 @@
|
|||||||
|
package top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单列表分页查询参数(Mapper 动态 SQL,含 FORCE INDEX 提示)。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class OrderListQueryParam {
|
||||||
|
|
||||||
|
private Long companyId;
|
||||||
|
private Long shopId;
|
||||||
|
/** 平台单号:前缀匹配(无 %/_ 时) */
|
||||||
|
private String platformOrderNo;
|
||||||
|
private Boolean platformOrderNoPrefix;
|
||||||
|
private Long statusId;
|
||||||
|
private BigDecimal minReceivedAmount;
|
||||||
|
private BigDecimal maxReceivedAmount;
|
||||||
|
private LocalDateTime startOrderTime;
|
||||||
|
private LocalDateTime endOrderTime;
|
||||||
|
private Long importTaskId;
|
||||||
|
private Integer confirm;
|
||||||
|
private Long productId;
|
||||||
|
|
||||||
|
/** true → FORCE INDEX(idx_company_shop_time),否则 idx_company_order_time */
|
||||||
|
private boolean useShopTimeIndex;
|
||||||
|
|
||||||
|
/** 白名单排序片段,如 order_time DESC, id DESC */
|
||||||
|
private String orderByClause;
|
||||||
|
|
||||||
|
private long offset;
|
||||||
|
private long limit;
|
||||||
|
}
|
||||||
+112
-96
@@ -2,21 +2,19 @@ package top.xyquan.ecomdmbbackend.modules.ecom.order.service.impl;
|
|||||||
|
|
||||||
import top.xyquan.ecomdmbbackend.common.utils.BeanCopyUtils;
|
import top.xyquan.ecomdmbbackend.common.utils.BeanCopyUtils;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||||
import top.xyquan.ecomdmbbackend.common.model.dto.SortDTO;
|
|
||||||
import top.xyquan.ecomdmbbackend.common.utils.SortUtils;
|
|
||||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderMapper;
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderMapper;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto.BatchImportOrderDTO;
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto.BatchImportOrderDTO;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto.CreateOrderDTO;
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto.CreateOrderDTO;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto.OrderFilterDTO;
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto.OrderFilterDTO;
|
||||||
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto.OrderListQueryParam;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto.UpdateOrderDTO;
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto.UpdateOrderDTO;
|
||||||
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.support.OrderPageQuerySupport;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO;
|
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO;
|
||||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductSpecPO;
|
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductSpecPO;
|
||||||
@@ -39,10 +37,13 @@ import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
|||||||
|
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -312,73 +313,38 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderPO> implemen
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public PageResultVO<OrderVO> getOrderPage(PageQueryDTO<OrderFilterDTO> pageQueryDTO) {
|
public PageResultVO<OrderVO> getOrderPage(PageQueryDTO<OrderFilterDTO> pageQueryDTO) {
|
||||||
// 从token中获取公司ID
|
|
||||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||||
|
|
||||||
// 创建分页对象
|
|
||||||
Page<OrderPO> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
|
||||||
|
|
||||||
// 构建查询条件
|
|
||||||
LambdaQueryWrapper<OrderPO> wrapper = new LambdaQueryWrapper<>();
|
|
||||||
wrapper.eq(OrderPO::getCompanyId, companyId);
|
|
||||||
|
|
||||||
// 添加过滤条件
|
|
||||||
OrderFilterDTO filter = pageQueryDTO.getFilters();
|
OrderFilterDTO filter = pageQueryDTO.getFilters();
|
||||||
if (filter != null) {
|
if (filter == null) {
|
||||||
if (filter.getShopId() != null) {
|
filter = new OrderFilterDTO();
|
||||||
wrapper.eq(OrderPO::getShopId, filter.getShopId());
|
|
||||||
}
|
|
||||||
if (filter.getPlatformOrderNo() != null && !filter.getPlatformOrderNo().trim().isEmpty()) {
|
|
||||||
wrapper.like(OrderPO::getPlatformOrderNo, filter.getPlatformOrderNo());
|
|
||||||
}
|
|
||||||
if (filter.getStatusId() != null) {
|
|
||||||
wrapper.eq(OrderPO::getStatusId, filter.getStatusId());
|
|
||||||
}
|
|
||||||
if (filter.getMinReceivedAmount() != null) {
|
|
||||||
wrapper.ge(OrderPO::getReceivedAmount, filter.getMinReceivedAmount());
|
|
||||||
}
|
|
||||||
if (filter.getMaxReceivedAmount() != null) {
|
|
||||||
wrapper.le(OrderPO::getReceivedAmount, filter.getMaxReceivedAmount());
|
|
||||||
}
|
|
||||||
if (filter.getStartOrderTime() != null) {
|
|
||||||
wrapper.ge(OrderPO::getOrderTime, filter.getStartOrderTime());
|
|
||||||
}
|
|
||||||
if (filter.getEndOrderTime() != null) {
|
|
||||||
wrapper.le(OrderPO::getOrderTime, filter.getEndOrderTime());
|
|
||||||
}
|
|
||||||
if (filter.getImportTaskId() != null) {
|
|
||||||
wrapper.eq(OrderPO::getImportTaskId, filter.getImportTaskId());
|
|
||||||
}
|
|
||||||
if (filter.getConfirm() != null) {
|
|
||||||
wrapper.eq(OrderPO::getConfirm, filter.getConfirm());
|
|
||||||
}
|
|
||||||
if (filter.getProductId() != null) {
|
|
||||||
wrapper.apply(
|
|
||||||
"EXISTS (SELECT 1 FROM ec_product_spec s WHERE s.id = ec_order.product_spec_id AND s.product_id = {0})",
|
|
||||||
filter.getProductId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 排序(支持多字段自定义排序,默认按下单时间降序 + 创建时间降序)
|
int page = pageQueryDTO.getPage() != null ? pageQueryDTO.getPage() : 1;
|
||||||
List<SortDTO> sorts = pageQueryDTO.getSorts();
|
int pageSize = pageQueryDTO.getPageSize() != null ? pageQueryDTO.getPageSize() : 20;
|
||||||
if (sorts == null || sorts.isEmpty()) {
|
|
||||||
// 默认排序:下单时间降序,创建时间降序
|
OrderListQueryParam query = OrderPageQuerySupport.toQueryParam(
|
||||||
wrapper.orderByDesc(OrderPO::getOrderTime)
|
companyId, filter, pageQueryDTO.getSorts(), page, pageSize);
|
||||||
.orderByDesc(OrderPO::getCreateTime);
|
|
||||||
} else {
|
long total = baseMapper.countOrderList(query);
|
||||||
SortUtils.applySorts(wrapper, OrderPO.class, sorts);
|
if (total == 0) {
|
||||||
|
return PageResultVO.create(Collections.emptyList(), 0L, page, pageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行分页查询
|
List<Long> ids = baseMapper.selectOrderListIds(query);
|
||||||
IPage<OrderPO> pageResult = this.page(page, wrapper);
|
if (ids.isEmpty()) {
|
||||||
|
return PageResultVO.create(Collections.emptyList(), total, page, pageSize);
|
||||||
|
}
|
||||||
|
|
||||||
// 转换为VO
|
List<OrderPO> rows = listByIds(ids);
|
||||||
List<OrderVO> voList = pageResult.getRecords().stream()
|
Map<Long, OrderPO> rowMap = rows.stream()
|
||||||
.map(this::convertToVO)
|
.collect(Collectors.toMap(OrderPO::getId, Function.identity(), (a, b) -> a));
|
||||||
|
List<OrderPO> ordered = ids.stream()
|
||||||
|
.map(rowMap::get)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
// 构建分页结果
|
List<OrderVO> voList = convertToVOBatch(ordered);
|
||||||
return PageResultVO.create(voList, pageResult.getTotal(), pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
return PageResultVO.create(voList, total, page, pageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -449,51 +415,101 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderPO> implemen
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为VO
|
* 转换为VO(单条详情等仍用逐条加载)
|
||||||
*/
|
*/
|
||||||
private OrderVO convertToVO(OrderPO order) {
|
private OrderVO convertToVO(OrderPO order) {
|
||||||
OrderVO orderVO = new OrderVO();
|
return convertToVOBatch(List.of(order)).get(0);
|
||||||
BeanCopyUtils.copyProperties(order, orderVO);
|
}
|
||||||
|
|
||||||
// 设置状态信息
|
/**
|
||||||
if (order.getStatusId() != null) {
|
* 批量转换为 VO,关联数据一次查出,避免 N+1。
|
||||||
OrderStatusPO orderStatus = orderStatusService.getById(order.getStatusId());
|
*/
|
||||||
if (orderStatus != null) {
|
private List<OrderVO> convertToVOBatch(List<OrderPO> orders) {
|
||||||
orderVO.setStatusName(orderStatus.getStatusName());
|
if (orders == null || orders.isEmpty()) {
|
||||||
OrderStatusVO orderStatusVO = new OrderStatusVO();
|
return Collections.emptyList();
|
||||||
BeanCopyUtils.copyProperties(orderStatus, orderStatusVO);
|
}
|
||||||
orderVO.setOrderStatus(orderStatusVO);
|
|
||||||
|
Set<Long> statusIds = new HashSet<>();
|
||||||
|
Set<Long> shopIds = new HashSet<>();
|
||||||
|
Set<Long> specIds = new HashSet<>();
|
||||||
|
for (OrderPO o : orders) {
|
||||||
|
if (o.getStatusId() != null) {
|
||||||
|
statusIds.add(o.getStatusId());
|
||||||
|
}
|
||||||
|
if (o.getShopId() != null) {
|
||||||
|
shopIds.add(o.getShopId());
|
||||||
|
}
|
||||||
|
if (o.getProductSpecId() != null) {
|
||||||
|
specIds.add(o.getProductSpecId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置店铺名称
|
Map<Long, OrderStatusPO> statusMap = statusIds.isEmpty()
|
||||||
ShopPO shop = shopService.getById(order.getShopId());
|
? Map.of()
|
||||||
if (shop != null) {
|
: orderStatusService.listByIds(statusIds).stream()
|
||||||
orderVO.setShopName(shop.getName());
|
.collect(Collectors.toMap(OrderStatusPO::getId, Function.identity(), (a, b) -> a));
|
||||||
ShopVO shopVO = new ShopVO();
|
Map<Long, ShopPO> shopMap = shopIds.isEmpty()
|
||||||
BeanCopyUtils.copyProperties(shop, shopVO);
|
? Map.of()
|
||||||
orderVO.setShop(shopVO);
|
: shopService.listByIds(shopIds).stream()
|
||||||
}
|
.collect(Collectors.toMap(ShopPO::getId, Function.identity(), (a, b) -> a));
|
||||||
|
Map<Long, ProductSpecPO> specMap = specIds.isEmpty()
|
||||||
|
? Map.of()
|
||||||
|
: productSpecService.listByIds(specIds).stream()
|
||||||
|
.collect(Collectors.toMap(ProductSpecPO::getId, Function.identity(), (a, b) -> a));
|
||||||
|
|
||||||
// 设置关联商品规格与商品信息(商品经规格关联)
|
Set<Long> productIds = specMap.values().stream()
|
||||||
if (order.getProductSpecId() != null) {
|
.map(ProductSpecPO::getProductId)
|
||||||
ProductSpecPO productSpec = productSpecService.getById(order.getProductSpecId());
|
.filter(Objects::nonNull)
|
||||||
if (productSpec != null) {
|
.collect(Collectors.toSet());
|
||||||
ProductSpecVO productSpecVO = new ProductSpecVO();
|
Map<Long, ProductPO> productMap = productIds.isEmpty()
|
||||||
BeanCopyUtils.copyProperties(productSpec, productSpecVO);
|
? Map.of()
|
||||||
orderVO.setProductSpec(productSpecVO);
|
: productService.listByIds(productIds).stream()
|
||||||
if (productSpec.getProductId() != null) {
|
.collect(Collectors.toMap(ProductPO::getId, Function.identity(), (a, b) -> a));
|
||||||
ProductPO product = productService.getById(productSpec.getProductId());
|
|
||||||
if (product != null) {
|
List<OrderVO> result = new ArrayList<>(orders.size());
|
||||||
ProductVO productVO = new ProductVO();
|
for (OrderPO order : orders) {
|
||||||
BeanCopyUtils.copyProperties(product, productVO);
|
OrderVO orderVO = new OrderVO();
|
||||||
orderVO.setProduct(productVO);
|
BeanCopyUtils.copyProperties(order, orderVO);
|
||||||
|
|
||||||
|
if (order.getStatusId() != null) {
|
||||||
|
OrderStatusPO orderStatus = statusMap.get(order.getStatusId());
|
||||||
|
if (orderStatus != null) {
|
||||||
|
orderVO.setStatusName(orderStatus.getStatusName());
|
||||||
|
OrderStatusVO orderStatusVO = new OrderStatusVO();
|
||||||
|
BeanCopyUtils.copyProperties(orderStatus, orderStatusVO);
|
||||||
|
orderVO.setOrderStatus(orderStatusVO);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (order.getShopId() != null) {
|
||||||
|
ShopPO shop = shopMap.get(order.getShopId());
|
||||||
|
if (shop != null) {
|
||||||
|
orderVO.setShopName(shop.getName());
|
||||||
|
ShopVO shopVO = new ShopVO();
|
||||||
|
BeanCopyUtils.copyProperties(shop, shopVO);
|
||||||
|
orderVO.setShop(shopVO);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (order.getProductSpecId() != null) {
|
||||||
|
ProductSpecPO productSpec = specMap.get(order.getProductSpecId());
|
||||||
|
if (productSpec != null) {
|
||||||
|
ProductSpecVO productSpecVO = new ProductSpecVO();
|
||||||
|
BeanCopyUtils.copyProperties(productSpec, productSpecVO);
|
||||||
|
orderVO.setProductSpec(productSpecVO);
|
||||||
|
if (productSpec.getProductId() != null) {
|
||||||
|
ProductPO product = productMap.get(productSpec.getProductId());
|
||||||
|
if (product != null) {
|
||||||
|
ProductVO productVO = new ProductVO();
|
||||||
|
BeanCopyUtils.copyProperties(product, productVO);
|
||||||
|
orderVO.setProduct(productVO);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
result.add(orderVO);
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
return orderVO;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+126
@@ -0,0 +1,126 @@
|
|||||||
|
package top.xyquan.ecomdmbbackend.modules.ecom.order.support;
|
||||||
|
|
||||||
|
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||||
|
import top.xyquan.ecomdmbbackend.common.model.dto.SortDTO;
|
||||||
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto.OrderFilterDTO;
|
||||||
|
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.dto.OrderListQueryParam;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单列表查询:时间范围兜底、排序白名单、索引选择。
|
||||||
|
*/
|
||||||
|
public final class OrderPageQuerySupport {
|
||||||
|
|
||||||
|
/** 未传时间时默认查最近 N 天 */
|
||||||
|
public static final int DEFAULT_LIST_DAYS = 30;
|
||||||
|
/** 列表允许的最大时间跨度 */
|
||||||
|
public static final int MAX_LIST_DAYS = 366;
|
||||||
|
|
||||||
|
private static final Set<String> ALLOWED_SORT_COLUMNS = Set.of(
|
||||||
|
"order_time", "id", "received_amount", "shop_id", "status_id", "create_time"
|
||||||
|
);
|
||||||
|
|
||||||
|
private static final Map<String, String> SORT_FIELD_TO_COLUMN = Map.of(
|
||||||
|
"orderTime", "order_time",
|
||||||
|
"id", "id",
|
||||||
|
"receivedAmount", "received_amount",
|
||||||
|
"shopId", "shop_id",
|
||||||
|
"statusId", "status_id",
|
||||||
|
"createTime", "create_time"
|
||||||
|
);
|
||||||
|
|
||||||
|
private OrderPageQuerySupport() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规范化下单时间:禁止无范围扫全表;跨度超限则拒绝。
|
||||||
|
*/
|
||||||
|
public static void normalizeOrderTimeRange(OrderFilterDTO filter) {
|
||||||
|
if (filter == null) {
|
||||||
|
throw new BusinessException("请指定下单时间范围(默认查询最近 " + DEFAULT_LIST_DAYS + " 天)");
|
||||||
|
}
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
if (filter.getStartOrderTime() == null && filter.getEndOrderTime() == null) {
|
||||||
|
filter.setEndOrderTime(now);
|
||||||
|
filter.setStartOrderTime(now.minusDays(DEFAULT_LIST_DAYS).withHour(0).withMinute(0).withSecond(0).withNano(0));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (filter.getStartOrderTime() == null) {
|
||||||
|
filter.setStartOrderTime(filter.getEndOrderTime().minusDays(DEFAULT_LIST_DAYS));
|
||||||
|
}
|
||||||
|
if (filter.getEndOrderTime() == null) {
|
||||||
|
filter.setEndOrderTime(now);
|
||||||
|
}
|
||||||
|
if (filter.getEndOrderTime().isBefore(filter.getStartOrderTime())) {
|
||||||
|
throw new BusinessException("下单结束时间不能早于开始时间");
|
||||||
|
}
|
||||||
|
long days = ChronoUnit.DAYS.between(
|
||||||
|
filter.getStartOrderTime().toLocalDate(),
|
||||||
|
filter.getEndOrderTime().toLocalDate()) + 1;
|
||||||
|
if (days > MAX_LIST_DAYS) {
|
||||||
|
throw new BusinessException("下单时间跨度不能超过 " + MAX_LIST_DAYS + " 天,请缩小范围");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static OrderListQueryParam toQueryParam(Long companyId, OrderFilterDTO filter,
|
||||||
|
List<SortDTO> sorts, long page, long pageSize) {
|
||||||
|
normalizeOrderTimeRange(filter);
|
||||||
|
OrderListQueryParam p = new OrderListQueryParam();
|
||||||
|
p.setCompanyId(companyId);
|
||||||
|
p.setShopId(filter.getShopId());
|
||||||
|
if (filter.getPlatformOrderNo() != null && !filter.getPlatformOrderNo().trim().isEmpty()) {
|
||||||
|
String raw = filter.getPlatformOrderNo().trim();
|
||||||
|
p.setPlatformOrderNo(raw);
|
||||||
|
p.setPlatformOrderNoPrefix(!raw.contains("%") && !raw.contains("_"));
|
||||||
|
}
|
||||||
|
p.setStatusId(filter.getStatusId());
|
||||||
|
p.setMinReceivedAmount(filter.getMinReceivedAmount());
|
||||||
|
p.setMaxReceivedAmount(filter.getMaxReceivedAmount());
|
||||||
|
p.setStartOrderTime(filter.getStartOrderTime());
|
||||||
|
p.setEndOrderTime(filter.getEndOrderTime());
|
||||||
|
p.setImportTaskId(filter.getImportTaskId());
|
||||||
|
p.setConfirm(filter.getConfirm());
|
||||||
|
p.setProductId(filter.getProductId());
|
||||||
|
p.setUseShopTimeIndex(filter.getShopId() != null);
|
||||||
|
p.setOrderByClause(buildOrderByClause(sorts));
|
||||||
|
p.setOffset(Math.max(0, (page - 1) * pageSize));
|
||||||
|
p.setLimit(Math.max(1, pageSize));
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 排序仅允许白名单字段;默认 order_time DESC, id DESC(利于走时间索引)。
|
||||||
|
*/
|
||||||
|
public static String buildOrderByClause(List<SortDTO> sorts) {
|
||||||
|
if (sorts == null || sorts.isEmpty()) {
|
||||||
|
return "order_time DESC, id DESC";
|
||||||
|
}
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (SortDTO sort : sorts) {
|
||||||
|
if (sort == null || sort.getField() == null || sort.getField().isBlank()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String column = SORT_FIELD_TO_COLUMN.get(sort.getField().trim());
|
||||||
|
if (column == null || !ALLOWED_SORT_COLUMNS.contains(column)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
boolean asc = "asc".equalsIgnoreCase(sort.getOrder());
|
||||||
|
if (!sb.isEmpty()) {
|
||||||
|
sb.append(", ");
|
||||||
|
}
|
||||||
|
sb.append(column).append(asc ? " ASC" : " DESC");
|
||||||
|
}
|
||||||
|
if (sb.isEmpty()) {
|
||||||
|
return "order_time DESC, id DESC";
|
||||||
|
}
|
||||||
|
if (!sb.toString().contains("id ")) {
|
||||||
|
sb.append(", id DESC");
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderMapper">
|
||||||
|
|
||||||
|
<sql id="orderListFrom">
|
||||||
|
ec_order
|
||||||
|
<choose>
|
||||||
|
<when test="useShopTimeIndex">
|
||||||
|
FORCE INDEX (idx_company_shop_time)
|
||||||
|
</when>
|
||||||
|
<otherwise>
|
||||||
|
FORCE INDEX (idx_company_order_time)
|
||||||
|
</otherwise>
|
||||||
|
</choose>
|
||||||
|
</sql>
|
||||||
|
|
||||||
|
<sql id="orderListWhere">
|
||||||
|
WHERE company_id = #{companyId}
|
||||||
|
<if test="shopId != null">
|
||||||
|
AND shop_id = #{shopId}
|
||||||
|
</if>
|
||||||
|
<if test="platformOrderNo != null and platformOrderNo != ''">
|
||||||
|
<choose>
|
||||||
|
<when test="platformOrderNoPrefix">
|
||||||
|
AND platform_order_no LIKE CONCAT(#{platformOrderNo}, '%')
|
||||||
|
</when>
|
||||||
|
<otherwise>
|
||||||
|
AND platform_order_no LIKE CONCAT('%', #{platformOrderNo}, '%')
|
||||||
|
</otherwise>
|
||||||
|
</choose>
|
||||||
|
</if>
|
||||||
|
<if test="statusId != null">
|
||||||
|
AND status_id = #{statusId}
|
||||||
|
</if>
|
||||||
|
<if test="minReceivedAmount != null">
|
||||||
|
AND received_amount >= #{minReceivedAmount}
|
||||||
|
</if>
|
||||||
|
<if test="maxReceivedAmount != null">
|
||||||
|
AND received_amount <= #{maxReceivedAmount}
|
||||||
|
</if>
|
||||||
|
<if test="startOrderTime != null">
|
||||||
|
AND order_time >= #{startOrderTime}
|
||||||
|
</if>
|
||||||
|
<if test="endOrderTime != null">
|
||||||
|
AND order_time <= #{endOrderTime}
|
||||||
|
</if>
|
||||||
|
<if test="importTaskId != null">
|
||||||
|
AND import_task_id = #{importTaskId}
|
||||||
|
</if>
|
||||||
|
<if test="confirm != null">
|
||||||
|
AND confirm = #{confirm}
|
||||||
|
</if>
|
||||||
|
<if test="productId != null">
|
||||||
|
AND product_spec_id IN (
|
||||||
|
SELECT id FROM ec_product_spec WHERE product_id = #{productId}
|
||||||
|
)
|
||||||
|
</if>
|
||||||
|
</sql>
|
||||||
|
|
||||||
|
<select id="countOrderList" resultType="long">
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM <include refid="orderListFrom"/>
|
||||||
|
<include refid="orderListWhere"/>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="selectOrderListIds" resultType="long">
|
||||||
|
SELECT id
|
||||||
|
FROM <include refid="orderListFrom"/>
|
||||||
|
<include refid="orderListWhere"/>
|
||||||
|
ORDER BY ${orderByClause}
|
||||||
|
LIMIT #{limit} OFFSET #{offset}
|
||||||
|
</select>
|
||||||
|
</mapper>
|
||||||
@@ -75,7 +75,7 @@ export namespace OrderDTO {
|
|||||||
export interface Filters {
|
export interface Filters {
|
||||||
/** 店铺ID */
|
/** 店铺ID */
|
||||||
shopId?: number;
|
shopId?: number;
|
||||||
/** 平台订单号(模糊查询) */
|
/** 平台订单号(默认前缀匹配;含 %/_ 时全模糊) */
|
||||||
platformOrderNo?: string;
|
platformOrderNo?: string;
|
||||||
/** 订单状态ID */
|
/** 订单状态ID */
|
||||||
statusId?: number;
|
statusId?: number;
|
||||||
|
|||||||
@@ -80,16 +80,57 @@ const productSearchKeyword = ref('');
|
|||||||
const productLoaded = ref(false);
|
const productLoaded = ref(false);
|
||||||
let productSearchTimer: null | ReturnType<typeof setTimeout> = null;
|
let productSearchTimer: null | ReturnType<typeof setTimeout> = null;
|
||||||
|
|
||||||
// 搜索表单
|
/** 与后端 OrderPageQuerySupport 对齐 */
|
||||||
|
const DEFAULT_LIST_DAYS = 30;
|
||||||
|
const MAX_LIST_DAYS = 366;
|
||||||
|
|
||||||
|
function buildDefaultOrderTimeRange() {
|
||||||
|
const end = dayjs().endOf('day');
|
||||||
|
const start = dayjs()
|
||||||
|
.subtract(DEFAULT_LIST_DAYS - 1, 'day')
|
||||||
|
.startOf('day');
|
||||||
|
return {
|
||||||
|
start: start.format('YYYY-MM-DD HH:mm:ss'),
|
||||||
|
end: end.format('YYYY-MM-DD HH:mm:ss'),
|
||||||
|
range: [start, end] as [dayjs.Dayjs, dayjs.Dayjs],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const defaultOrderTime = buildDefaultOrderTimeRange();
|
||||||
|
|
||||||
|
// 搜索表单(默认近 30 天,避免无时间范围扫全表)
|
||||||
const searchForm = ref({
|
const searchForm = ref({
|
||||||
platformOrderNo: '',
|
platformOrderNo: '',
|
||||||
shopId: undefined as number | undefined,
|
shopId: undefined as number | undefined,
|
||||||
statusId: undefined as number | undefined,
|
statusId: undefined as number | undefined,
|
||||||
startOrderTime: undefined as string | undefined,
|
startOrderTime: defaultOrderTime.start as string | undefined,
|
||||||
endOrderTime: undefined as string | undefined,
|
endOrderTime: defaultOrderTime.end as string | undefined,
|
||||||
productId: undefined as number | undefined,
|
productId: undefined as number | undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function ensureOrderTimeFilters(): boolean {
|
||||||
|
if (!searchForm.value.startOrderTime || !searchForm.value.endOrderTime) {
|
||||||
|
const d = buildDefaultOrderTimeRange();
|
||||||
|
searchForm.value.startOrderTime = d.start;
|
||||||
|
searchForm.value.endOrderTime = d.end;
|
||||||
|
}
|
||||||
|
const start = dayjs(searchForm.value.startOrderTime);
|
||||||
|
const end = dayjs(searchForm.value.endOrderTime);
|
||||||
|
if (!start.isValid() || !end.isValid()) {
|
||||||
|
message.warning('请选择有效的下单时间范围');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (end.isBefore(start)) {
|
||||||
|
message.warning('下单结束时间不能早于开始时间');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (end.diff(start, 'day') + 1 > MAX_LIST_DAYS) {
|
||||||
|
message.warning(`下单时间跨度不能超过 ${MAX_LIST_DAYS} 天,请缩小范围`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// 当前排序状态
|
// 当前排序状态
|
||||||
const currentSort = ref<null | { field: string; order: 'asc' | 'desc' }>(null);
|
const currentSort = ref<null | { field: string; order: 'asc' | 'desc' }>(null);
|
||||||
|
|
||||||
@@ -347,6 +388,9 @@ const gridOptions: VxeGridProps<any> = {
|
|||||||
ajax: {
|
ajax: {
|
||||||
query: async ({ page, form }) => {
|
query: async ({ page, form }) => {
|
||||||
try {
|
try {
|
||||||
|
if (!ensureOrderTimeFilters()) {
|
||||||
|
return { items: [], total: 0 };
|
||||||
|
}
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
// 从表单获取搜索参数
|
// 从表单获取搜索参数
|
||||||
const searchParams = form || {};
|
const searchParams = form || {};
|
||||||
@@ -403,7 +447,7 @@ const formOptions = computed(() => ({
|
|||||||
{
|
{
|
||||||
component: 'Input',
|
component: 'Input',
|
||||||
componentProps: {
|
componentProps: {
|
||||||
placeholder: '请输入平台订单号(回车搜索)',
|
placeholder: '平台订单号(前缀匹配,回车搜索)',
|
||||||
onChange: (e: any) => {
|
onChange: (e: any) => {
|
||||||
searchForm.value.platformOrderNo = e.target.value;
|
searchForm.value.platformOrderNo = e.target.value;
|
||||||
},
|
},
|
||||||
@@ -483,29 +527,40 @@ const formOptions = computed(() => ({
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
component: 'RangePicker',
|
component: 'RangePicker',
|
||||||
|
defaultValue: defaultOrderTime.range,
|
||||||
componentProps: {
|
componentProps: {
|
||||||
placeholder: ['下单开始时间', '下单结束时间'],
|
placeholder: ['下单开始时间', '下单结束时间'],
|
||||||
allowClear: true,
|
allowClear: false,
|
||||||
showTime: { format: 'HH:mm:ss' },
|
showTime: { format: 'HH:mm:ss' },
|
||||||
format: 'YYYY-MM-DD HH:mm:ss',
|
format: 'YYYY-MM-DD HH:mm:ss',
|
||||||
style: { width: '100%' },
|
style: { width: '100%' },
|
||||||
onChange: (val: any) => {
|
onChange: (val: any) => {
|
||||||
searchForm.value.startOrderTime = val && val[0]
|
if (!val || !val[0] || !val[1]) {
|
||||||
? (val[0] as dayjs.Dayjs).format('YYYY-MM-DD HH:mm:ss')
|
const d = buildDefaultOrderTimeRange();
|
||||||
: undefined;
|
searchForm.value.startOrderTime = d.start;
|
||||||
searchForm.value.endOrderTime = val && val[1]
|
searchForm.value.endOrderTime = d.end;
|
||||||
? (val[1] as dayjs.Dayjs).format('YYYY-MM-DD HH:mm:ss')
|
} else {
|
||||||
: undefined;
|
searchForm.value.startOrderTime = (val[0] as dayjs.Dayjs).format(
|
||||||
|
'YYYY-MM-DD HH:mm:ss',
|
||||||
|
);
|
||||||
|
searchForm.value.endOrderTime = (val[1] as dayjs.Dayjs).format(
|
||||||
|
'YYYY-MM-DD HH:mm:ss',
|
||||||
|
);
|
||||||
|
}
|
||||||
gridReloadRef.value?.();
|
gridReloadRef.value?.();
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
fieldName: 'orderTimeRange',
|
fieldName: 'orderTimeRange',
|
||||||
label: '下单时间',
|
label: '下单时间',
|
||||||
|
rules: 'required',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
showDefaultActions: true,
|
showDefaultActions: true,
|
||||||
collapsed: true,
|
collapsed: true,
|
||||||
collapsedRows: 2,
|
collapsedRows: 2,
|
||||||
|
defaultValues: {
|
||||||
|
orderTimeRange: defaultOrderTime.range,
|
||||||
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// 刷新表格数据
|
// 刷新表格数据
|
||||||
@@ -618,9 +673,9 @@ function handleModalSuccess() {
|
|||||||
refreshTable();
|
refreshTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化
|
// 初始化:保证表单与 searchForm 时间一致
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
// 店铺、订单状态、商品选择器改为点击时懒加载,不再预加载
|
ensureOrderTimeFilters();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user