文章目录
-
- 概要
- 整体架构流程
- 技术细节
- 小结
概要
订单搜索分页查询是电商、物流、零售等众多行业中的常见需求,主要用于管理和分析大量订单数据.
需求分析以及接口设计
技术细节
1.Controller层:
根据接口设计来写
@ApiOperation("订单搜索")
@GetMapping("/conditionSearch")
public Result<PageResult> orderSearch(OrdersPageQueryDTO ordersPageQueryDTO){
log.info("订单搜索:{}", ordersPageQueryDTO);
PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
return Result.success(pageResult);
}
2.Service层:
- 设置分页数据
- 查询所有订单页面对象
- 由于部分订单状态需要额外返回订单信息,需要将Orders转为OrderVO
- 遍历所有订单
- 将Orders的属性都copy到OrderVO中
- 设置OrderVO其他字段
- 依次添加到VOList集合中
- 返回VOList集合
public PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
//设置分页数据
PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
//查询到所有订单页面对象
Page<Orders> page = orderMapper.pageQuery(ordersPageQueryDTO);
//部分订单状态需要额外返回订单信息,将Orders转化为OrderVO
ArrayList<OrderVO> orderVOList = new ArrayList<>();
//获取到所有订单
List<Orders> ordersList = page.getResult();
if (!CollectionUtils.isEmpty(ordersList)) {
for (Orders orders : ordersList) {
OrderVO orderVO = new OrderVO();
BeanUtils.copyProperties(orders,orderVO);
//拼接订单菜品的字符串
//1.查询订单明细
List<OrderDetail> orderDetailList = orderDetailMapper.selectByOrderId(orders.getId());
//2.将每一条订单菜品信息拼接为字符串(格式:宫保鸡丁*3)
List<String> orderDishStrList = orderDetailList.stream().map(x -> {
String orderDishStr = x.getName() + "*" + x.getNumber() + ";";
return orderDishStr;
}).collect(Collectors.toList());
String orderDishStr1 = String.join("", orderDishStrList);
orderVO.setOrderDishes(orderDishStr1);
//3.依次将VO添加到集合
orderVOList.add(orderVO);
}
}
return new PageResult(page.getTotal(), ordersList);
}
3.Mapper层:
@Select("select * from `sky-take-out`.order_detail where order_id = #{orderId}")
List<OrderDetail> selectByOrderId(Long orderId);
<select id="pageQuery" resultType="com.sky.entity.Orders">
select * from `sky-take-out`.orders
<where>
<if test="number != null and number!=''">
and number like concat('%',#{number},'%')
</if>
<if test="phone != null and phone!=''">
and phone like concat('%',#{phone},'%')
</if>
<if test="userId != null">
and user_id = #{userId}
</if>
<if test="status != null">
and status = #{status}
</if>
<if test="beginTime != null">
and order_time >= #{beginTime}
</if>
<if test="endTime != null">
and order_time <= #{endTime}
</if>
</where>
order by order_time desc
</select>