前言
本篇博客用于记录苍穹外卖Day09用户端历史订单、商家端订单管理的学习,由于课程中直接跳过该部分的讲解,所有在这我会全过程进行分析以及编码。
注意:将查询历史订单不显示的问题我放在最前面了,因为一开始提供给我们的资料时间是没有格式化的,我们只需将时间格式化,订单数据即可显示在你的小程序端,在你的Orders实体类中下单时间和结账时间加上以下代码即可
//下单时间
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime orderTime;
//结账时间
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime checkoutTime;
任务是:完成用户端历史订单模块、商家端订单管理模块相关业务新功能开发和已有功能优化,具体如下:
用户端历史订单模块:
- 查询历史订单
- 查询订单详情
- 取消订单
- 再来一单
商家端订单管理模块:
- 订单搜索
- 各个状态的订单数量统计
- 查询订单详情
- 接单
- 拒单
- 取消订单
- 派送订单
- 完成订单
功能优化:
优化用户下单功能,加入校验逻辑,如果用户的收货地址距离商家门店超出配送范围(配送范围为5公里内),则下单失败。
用户端
查询历史订单
效果图:
接口文档:
代码:
- controller
@GetMapping("/historyOrders")
@ApiOperation("分页查询历史订单")
public Result<PageResult> page(OrdersPageQueryDTO ordersPageQueryDTO){
log.info("分页查询历史订单:{}",ordersPageQueryDTO);
PageResult pageResult=orderService.page(ordersPageQueryDTO);
return Result.success(pageResult);
}
- serviceImpl
//分页查询订单数据
public PageResult page(OrdersPageQueryDTO ordersPageQueryDTO) {
PageHelper.startPage(ordersPageQueryDTO.getPage(),ordersPageQueryDTO.getPageSize());
ordersPageQueryDTO.setUserId(BaseContext.getCurrentId());
Page<Orders> page=orderMapper.pageQuery(ordersPageQueryDTO);
//将查询到的orders封装成orderVO
//通过ordersId查出ordersDetails的数据存进orderVO
List<OrderVO> list=new ArrayList<>();
if(page!=null&&page.size()>0){
for (Orders orders : page) {
log.info("查看Orders什么情况:{}",orders);
Long id=orders.getId();
List<OrderDetail> detailList=orderDetailMapper.getById(id);
OrderVO orderVO=new OrderVO();
BeanUtils.copyProperties(orders,orderVO);
orderVO.setOrderDetailList(detailList);
log.info("查看OrderVO什么情况:{}",orderVO);
list.add(orderVO);
}
}
return new PageResult(page.getTotal(), list);
}
查询订单详情
效果图:
接口文档:
代码:
- controller
@GetMapping("/orderDetail/{id}")
@ApiOperation("查询订单详情")
public Result<OrderVO> details(@PathVariable Long id){
log.info("根据id查询订单:{}",id);
OrderVO orderVO=orderService.details(id);
return Result.success(orderVO);
}
- serviceImpl
@Override
public OrderVO details(Long id) {
Orders orders=orderMapper.details(id);
List<OrderDetail> orderDetail= orderDetailMapper.getById(orders.getId());
OrderVO orderVO=new OrderVO();
BeanUtils.copyProperties(orders,orderVO);
orderVO.setOrderDetailList(orderDetail);
return orderVO;
}
取消订单
效果图:
接口文档:
代码:
- controller
@PutMapping("/cancel/{id}")
@ApiOperation("取消订单")
public Result cancel(@PathVariable Long id){
log.info("取消订单:{}",id);
orderService.cancel(id);
return Result.success();
}
- serviceImpl
@Override
public void cancel(Long id) {
//根据id查询订单
Orders orders=orderMapper.getById(id);
//查询是否存在
if(orders==null){
throw new OrderBusinessException(MessageConstant.ORDER_NOT_FOUND);
}
Orders orders1=new Orders();
//处于待接单状态,退款
if (orders.getStatus().equals(Orders.TO_BE_CONFIRMED)) {
log.info("退款");
//支付状态修改为 退款
orders.setPayStatus(Orders.REFUND);
}
//修改订单信息
orders1.setStatus(Orders.CANCELLED);
orders1.setCancelReason("用户取消");
orders1.setCancelTime(LocalDateTime.now());
orderMapper.update(orders1);
}
再来一单
效果图:
接口文档:
代码:
- controller
@PostMapping("/repetition/{id}")
@ApiOperation("再来一单")
public Result again(@PathVariable Long id){
log.info("再来一单:{}",id);
orderService.again(id);
return Result.success();
}
- serviceImpl
@Override
public void again(Long id) {
//根据id查询订单详情
List<OrderDetail> orderDetailList = orderDetailMapper.getById(id);
// 将订单详情对象转换为购物车对象
List<ShoppingCart> shoppingCartList = orderDetailList.stream().map(x -> {
ShoppingCart shoppingCart = new ShoppingCart();
// 将原订单详情里面的菜品信息重新复制到购物车对象中
BeanUtils.copyProperties(x, shoppingCart, "id");
shoppingCart.setUserId(BaseContext.getCurrentId());
shoppingCart.setCreateTime(LocalDateTime.now());
return shoppingCart;
}).collect(Collectors.toList());
// 将购物车对象批量添加到数据库
shoppingCartMapper.insertBatch(shoppingCartList);
}
管理端
订单搜索
- OrderController
@GetMapping("/conditionSearch")
@ApiOperation("订单搜索")
public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO){
log.info("订单搜索:{}",ordersPageQueryDTO);
PageResult pageResult=orderService.conditionSearch(ordersPageQueryDTO);
return Result.success(pageResult);
}
- OrderServiceImpl
//订单搜索
public PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
Page<Orders> page=orderMapper.pageQuery(ordersPageQueryDTO);
//将Orders转化成OrderVO
List<OrderVO> orderVOList=getOrderVOList(page);
return new PageResult(page.getTotal(), orderVOList);
}
private List<OrderVO> getOrderVOList(Page<Orders> page){
List<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);
//获取订单的菜品信息并将其设置进OrderVO
String orderDish=getOrderDishesStr(orders);
orderVO.setOrderDishes(orderDish);
orderVOList.add(orderVO);
}
}
return orderVOList;
}
//获取菜品信息
private String getOrderDishesStr(Orders orders){
// 查询订单菜品详情信息(订单中的菜品和数量)
List<OrderDetail> orderDetailList = orderDetailMapper.getById(orders.getId());
// 将每一条订单菜品信息拼接为字符串(格式:宫保鸡丁*3;)
List<String> orderDishList = orderDetailList.stream().map(x -> {
String orderDish = x.getName() + "*" + x.getNumber() + ";";
return orderDish;
}).collect(Collectors.toList());
// 将该订单对应的所有菜品信息拼接在一起
return String.join("", orderDishList);
}
各个状态的订单数量统计
- OrderController
@GetMapping("/statistics")
@ApiOperation("各个状态订单数量的统计")
public Result<OrderStatisticsVO> statistics(){
log.info("各个状态订单的数量统计");
OrderStatisticsVO orderStatisticsVO=orderService.count();
return Result.success(orderStatisticsVO);
}
- OrderServiceImpl
//各个状态订单数量的统计
public OrderStatisticsVO count() {
Integer toBeConfirmed=orderMapper.toBeConfirmed();
Integer confirmed=orderMapper.confirmed();
Integer deliveryInProgress=orderMapper.deliveryInProgress();
OrderStatisticsVO orderStatisticsVO=new OrderStatisticsVO();
orderStatisticsVO.setConfirmed(confirmed);
orderStatisticsVO.setToBeConfirmed(toBeConfirmed);
orderStatisticsVO.setDeliveryInProgress(deliveryInProgress);
return orderStatisticsVO;
}
查询订单详情
- OrderController
@GetMapping("/details/{id}")
@ApiOperation("查询订单详情")
public Result<OrderVO> details(@PathVariable Long id){
log.info("查询订单详情:{}",id);
OrderVO orderVO=orderService.details(id);
return Result.success(orderVO);
}
接单
- OrderController
@PutMapping("/confirm")
@ApiOperation("接单")
public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO){
log.info("接单:{}",ordersConfirmDTO);
orderService.confirm(ordersConfirmDTO);
return Result.success();
}
- OrderServiceImpl
@Override
public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
Orders orders=new Orders();
orders.setStatus(Orders.CONFIRMED);
orders.setId(ordersConfirmDTO.getId());
orderMapper.update(orders);
}
拒单
- OrderController
@PutMapping("/rejection")
@ApiOperation("拒单")
public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO){
log.info("拒单:{}",ordersRejectionDTO);
orderService.rejection(ordersRejectionDTO);
return Result.success();
}
- OrderServiceImpl
@Override
public void rejection(OrdersRejectionDTO ordersRejectionDTO) {
//订单只有在存在且状态为待接单时才能拒绝
Orders orders1=orderMapper.getById(ordersRejectionDTO.getId());
if(orders1==null || !orders1.getStatus().equals(Orders.TO_BE_CONFIRMED)){
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
}
//要填写拒单原因等
Orders orders=new Orders();
orders.setStatus(Orders.CANCELLED);
orders.setId(ordersRejectionDTO.getId());
orders.setRejectionReason(ordersRejectionDTO.getRejectionReason());
orders.setCancelTime(LocalDateTime.now());
orderMapper.update(orders);
}
取消订单
- OrderController
@PutMapping("/cancel")
@ApiOperation("取消订单")
public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO){
log.info("取消订单:{}",ordersCancelDTO);
orderService.adminCancel(ordersCancelDTO);
return Result.success();
}
- OrderServiceImpl
@Override
public void adminCancel(OrdersCancelDTO ordersCancelDTO) {
Orders orders=new Orders();
orders.setCancelTime(LocalDateTime.now());
orders.setId(ordersCancelDTO.getId());
orders.setCancelReason(ordersCancelDTO.getCancelReason());
orders.setStatus(Orders.CANCELLED);
orderMapper.update(orders);
}
派送订单
- OrderController
@PutMapping("/delivery/{id}")
@ApiOperation("派送订单")
public Result delivery(@PathVariable Long id){
orderService.delivery(id);
return Result.success();
}
- OrderServiceImpl
@Override
public void delivery(Long id) {
//订单要存在且状态为待派送
Orders orders1=orderMapper.getById(id);
if (orders1 == null || !orders1.getStatus().equals(Orders.CONFIRMED)) {
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
}
Orders orders=new Orders();
orders.setId(id);
orders.setStatus(Orders.DELIVERY_IN_PROGRESS);
orderMapper.update(orders);
}
完成订单
- OrderController
@PutMapping("/complete/{id}")
@ApiOperation("完成订单")
public Result complete(@PathVariable Long id){
orderService.complete(id);
return Result.success();
}
- OrderServiceImpl
@Override
public void complete(Long id) {
// 根据id查询订单
Orders orders1= orderMapper.getById(id);
// 校验订单是否存在,并且状态为4
if (orders1 == null || !orders1.getStatus().equals(Orders.DELIVERY_IN_PROGRESS)) {
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
}
Orders orders = new Orders();
orders.setId(orders1.getId());
// 更新订单状态,状态转为完成
orders.setStatus(Orders.COMPLETED);
orders.setDeliveryTime(LocalDateTime.now());
orderMapper.update(orders);
}