课外开发苍穹外卖苍穹外卖学习笔记(九)
Jie用户端历史订单模块:
查询历史订单
OrderController
1 2 3 4 5 6 7 8 9
|
@GetMapping("/historyOrders") @ApiOperation(value = "历史订单") public Result<PageResult> page(int page, int pageSize, Integer status) { PageResult pageResult = orderService.pageQuery4User(page, pageSize, status); return Result.success(pageResult); }
|
OrderService
1 2 3 4 5
|
PageResult pageQuery4User(int page, int pageSize, Integer status);
|
OrderServiceImpl
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
|
@Override public PageResult pageQuery4User(int pageNum, int pageSize, Integer status) { Page<Orders> page = new Page<>(pageNum, pageSize); OrdersPageQueryDTO ordersPageQueryDTO = new OrdersPageQueryDTO(); ordersPageQueryDTO.setUserId(BaseContext.getCurrentId()); ordersPageQueryDTO.setStatus(status); LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
if (status != null) { queryWrapper.eq(Orders::getStatus, status); } if (ordersPageQueryDTO.getNumber() != null && !ordersPageQueryDTO.getNumber().isEmpty()) { queryWrapper.like(Orders::getNumber, ordersPageQueryDTO.getNumber()); } if (ordersPageQueryDTO.getPhone() != null && !ordersPageQueryDTO.getPhone().isEmpty()) { queryWrapper.like(Orders::getPhone, ordersPageQueryDTO.getPhone()); } if (ordersPageQueryDTO.getBeginTime() != null) { queryWrapper.ge(Orders::getOrderTime, ordersPageQueryDTO.getBeginTime()); } if (ordersPageQueryDTO.getEndTime() != null) { queryWrapper.le(Orders::getOrderTime, ordersPageQueryDTO.getEndTime()); }
Page<Orders> ordersPage = orderMapper.selectPage(page, queryWrapper); List<OrderVO> list = new ArrayList<>(); if (ordersPage != null && ordersPage.getTotal() > 0) { for (Orders orders : ordersPage.getRecords()) { Long orderId = orders.getId();
LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>(); orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, orderId); List<OrderDetail> orderDetails = orderDetailMapper.selectList(orderDetailLambdaQueryWrapper);
OrderVO orderVO = new OrderVO(); BeanUtils.copyProperties(orders, orderVO); orderVO.setOrderDetailList(orderDetails);
list.add(orderVO); } } assert ordersPage != null; return new PageResult(ordersPage.getTotal(), list); }
|
查询订单详情
OrderController
1 2 3 4 5 6 7 8 9
|
@GetMapping("/orderDetail/{id}") @ApiOperation(value = "订单详情") public Result<OrderVO> details(@PathVariable Long id) { OrderVO orderVO = orderService.details(id); return Result.success(orderVO); }
|
OrderService
1 2 3 4
|
OrderVO details(Long id);
|
OrderServiceImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@Override public OrderVO details(Long id) { Orders orders = orderMapper.selectById(id); LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(OrderDetail::getOrderId, id); List<OrderDetail> orderDetailList = orderDetailMapper.selectList(queryWrapper); OrderVO orderVO = new OrderVO(); BeanUtils.copyProperties(orders, orderVO); orderVO.setOrderDetailList(orderDetailList); return orderVO; }
|
取消订单
OrderController
1 2 3 4 5 6 7 8 9
|
@PutMapping("/cancel/{id}") @ApiOperation("取消订单") public Result cancel(@PathVariable("id") Long id) throws Exception { orderService.userCancelById(id); return Result.success(); }
|
OrderService
1 2 3 4 5
|
void userCancelById(Long id) throws Exception;
|
OrderServiceImpl
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
|
@Transactional @Override public void userCancelById(Long id) throws Exception { Orders ordersDB = orderMapper.selectById(id); if (ordersDB == null) { throw new OrderBusinessException(MessageConstant.ORDER_NOT_FOUND); } if (ordersDB.getStatus() > 2) { throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR); } Orders orders = Orders.builder() .id(ordersDB.getId()) .status(Orders.CANCELLED) .cancelReason("用户取消") .cancelTime(LocalDateTime.now()) .build();
if (ordersDB.getStatus().equals(Orders.TO_BE_CONFIRMED)) {
orders.setPayStatus(Orders.REFUND); }
orderMapper.update(orders, new LambdaQueryWrapper<Orders>().eq(Orders::getId, id)); }
|
再来一单
OrderController
1 2 3 4 5 6 7 8 9
|
@PostMapping("/repetition/{id}") @ApiOperation("再来一单") public Result repetition(@PathVariable Long id) { orderService.repetition(id); return Result.success(); }
|
OrderService
1 2 3 4
|
void repetition(Long id);
|
OrderServiceImpl
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
|
@Override @Transactional public void repetition(Long id) { Long userId = BaseContext.getCurrentId(); LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(OrderDetail::getOrderId, id); List<OrderDetail> orderDetailList = orderDetailMapper.selectList(queryWrapper); List<ShoppingCart> shoppingCartList = orderDetailList.stream().map(x -> { ShoppingCart shoppingCart = new ShoppingCart();
BeanUtils.copyProperties(x, shoppingCart, "id"); shoppingCart.setUserId(userId); shoppingCart.setCreateTime(LocalDateTime.now());
return shoppingCart; }).collect(Collectors.toList());
MybatisBatch<ShoppingCart> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, shoppingCartList); MybatisBatch.Method<ShoppingCart> method = new MybatisBatch.Method<>(ShoppingCartMapper.class); mybatisBatch.execute(method.insert()); }
|
商家端订单管理模块:
订单搜索
OrderController
1 2 3 4 5 6 7 8 9
|
@GetMapping("/conditionSearch") @ApiOperation(value = "订单搜索") public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) { PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO); return Result.success(pageResult); }
|
OrderService
1 2 3 4
|
PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO);
|
OrderServiceImpl
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
|
@Override public PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) { Page<Orders> page = new Page<>(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize()); LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>(); if (ordersPageQueryDTO.getStatus() != null) { queryWrapper.eq(Orders::getStatus, ordersPageQueryDTO.getStatus()); } if (ordersPageQueryDTO.getNumber() != null && !ordersPageQueryDTO.getNumber().isEmpty()) { queryWrapper.like(Orders::getNumber, ordersPageQueryDTO.getNumber()); } if (ordersPageQueryDTO.getPhone() != null && !ordersPageQueryDTO.getPhone().isEmpty()) { queryWrapper.like(Orders::getPhone, ordersPageQueryDTO.getPhone()); } if (ordersPageQueryDTO.getBeginTime() != null) { queryWrapper.ge(Orders::getOrderTime, ordersPageQueryDTO.getBeginTime()); } if (ordersPageQueryDTO.getEndTime() != null) { queryWrapper.le(Orders::getOrderTime, ordersPageQueryDTO.getEndTime()); } Page<Orders> ordersPage = orderMapper.selectPage(page, queryWrapper); List<OrderVO> orderVOList = getOrderVOList(ordersPage); return new PageResult(page.getTotal(), orderVOList); }
private List<OrderVO> getOrderVOList(Page<Orders> page) { List<OrderVO> orderVOList = new ArrayList<>();
List<Orders> ordersList = page.getRecords(); if (!CollectionUtils.isEmpty(ordersList)) { for (Orders orders : ordersList) { OrderVO orderVO = new OrderVO(); BeanUtils.copyProperties(orders, orderVO); String orderDishes = getOrderDishesStr(orders); orderVO.setOrderDishes(orderDishes); orderVOList.add(orderVO); } } return orderVOList; }
private String getOrderDishesStr(Orders orders) { LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(OrderDetail::getOrderId, orders.getId()); List<OrderDetail> orderDetailList = orderDetailMapper.selectList(queryWrapper);
List<String> orderDishList = orderDetailList.stream().map(x -> { String orderDish = x.getName() + "*" + x.getNumber() + ";"; return orderDish; }).toList(); return String.join("", orderDishList); }
|
各个状态的订单数量统计
OrderController
1 2 3 4 5 6 7 8 9
|
@GetMapping("/statistics") @ApiOperation("各个状态的订单数量统计") public Result<OrderStatisticsVO> statistics() { OrderStatisticsVO orderStatisticsVO = orderService.statistics(); return Result.success(orderStatisticsVO); }
|
OrderService
1 2 3 4
|
OrderStatisticsVO statistics();
|
OrderServiceImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
@Override public OrderStatisticsVO statistics() { Integer toBeConfirmed = orderMapper.countStatus(Orders.TO_BE_CONFIRMED); Integer confirmed = orderMapper.countStatus(Orders.CONFIRMED); Integer deliveryInProgress = orderMapper.countStatus(Orders.DELIVERY_IN_PROGRESS);
OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO(); orderStatisticsVO.setToBeConfirmed(toBeConfirmed); orderStatisticsVO.setConfirmed(confirmed); orderStatisticsVO.setDeliveryInProgress(deliveryInProgress); return orderStatisticsVO; }
|
orderMapper
1 2 3 4 5
|
@Select("select count(id) from orders where status = #{status}") Integer countStatus(Integer toBeConfirmed);
|
查询订单详情
OrderController
1 2 3 4 5 6 7 8 9
|
@GetMapping("/details/{id}") @ApiOperation("订单详情") public Result<OrderVO> details(@PathVariable Long id) { OrderVO orderVO = orderService.details(id); return Result.success(orderVO); }
|
接单
OrderController
1 2 3 4 5 6 7 8 9
|
@PutMapping("/confirm") @ApiOperation("接单") public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) { orderService.confirm(ordersConfirmDTO); return Result.success(); }
|
OrderService
1 2 3 4
|
void confirm(OrdersConfirmDTO ordersConfirmDTO);
|
OrderServiceImpl
1 2 3 4 5 6 7 8 9 10 11
|
@Override public void confirm(OrdersConfirmDTO ordersConfirmDTO) { Orders orders = Orders.builder() .id(ordersConfirmDTO.getId()) .status(Orders.CONFIRMED) .build(); orderMapper.updateById(orders); }
|
拒单
OrderController
1 2 3 4 5 6 7 8 9
|
@PutMapping("/rejection") @ApiOperation("拒单") public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception { orderService.rejection(ordersRejectionDTO); return Result.success(); }
|
OrderService
1 2 3 4
|
void rejection(OrdersRejectionDTO ordersRejectionDTO) throws Exception;
|
OrderServiceImpl
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
|
@Override public void rejection(OrdersRejectionDTO ordersRejectionDTO) throws Exception{ Orders ordersDB = orderMapper.selectById(ordersRejectionDTO.getId());
if (ordersDB == null || !ordersDB.getStatus().equals(Orders.TO_BE_CONFIRMED)) { throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR); }
Integer payStatus = ordersDB.getPayStatus(); if (payStatus.equals(Orders.PAID)) {
}
Orders orders = new Orders(); orders.setId(ordersDB.getId()); orders.setStatus(Orders.CANCELLED); orders.setRejectionReason(ordersRejectionDTO.getRejectionReason()); orders.setCancelTime(LocalDateTime.now());
orderMapper.updateById(orders); }
|
取消订单
OrderController
1 2 3 4 5 6 7 8 9
|
@PutMapping("/cancel") @ApiOperation("取消订单") public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception { orderService.cancel(ordersCancelDTO); return Result.success(); }
|
OrderService
1 2 3 4
|
void cancel(OrdersCancelDTO ordersCancelDTO) throws Exception;
|
OrderServiceImpl
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
|
@Override public void cancel(OrdersCancelDTO ordersCancelDTO) throws Exception { Orders ordersDB = orderMapper.selectById(ordersCancelDTO.getId());
Integer payStatus = ordersDB.getPayStatus(); if (payStatus.equals(Orders.PAID)) {
}
Orders orders = new Orders(); orders.setId(ordersCancelDTO.getId()); orders.setStatus(Orders.CANCELLED); orders.setCancelReason(ordersCancelDTO.getCancelReason()); orders.setCancelTime(LocalDateTime.now()); orderMapper.updateById(orders); }
|
派送订单
OrderController
1 2 3 4 5 6 7 8 9
|
@PutMapping("/delivery/{id}") @ApiOperation("派送订单") public Result delivery(@PathVariable("id") Long id) { orderService.delivery(id); return Result.success(); }
|
OrderService
1 2 3 4
|
void delivery(Long id);
|
OrderServiceImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
@Override public void delivery(Long id) { Orders ordersDB = orderMapper.selectById(id);
if (ordersDB == null || !ordersDB.getStatus().equals(Orders.CONFIRMED)) { throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR); }
Orders orders = Orders.builder() .id(ordersDB.getId()) .status(Orders.DELIVERY_IN_PROGRESS) .build(); orderMapper.updateById(orders); }
|
完成订单
OrderController
1 2 3 4 5 6 7 8 9
|
@PutMapping("/complete/{id}") @ApiOperation("完成订单") public Result complete (@PathVariable("id") Long id) { orderService.complete(id); return Result.success(); }
|
OrderService
1 2 3 4
|
void complete(Long id);
|
OrderServiceImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
@Override public void complete(Long id) { Orders ordersDB = orderMapper.selectById(id);
if (ordersDB == null || !ordersDB.getStatus().equals(Orders.DELIVERY_IN_PROGRESS)) { throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR); }
Orders orders = Orders.builder() .id(ordersDB.getId()) .status(Orders.COMPLETED) .deliveryTime(LocalDateTime.now()) .build(); orderMapper.updateById(orders); }
|
校验收货地址是否超出配送范围
登录百度地图开放平台:https://lbsyun.baidu.com/
相关接口:
https://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
https://lbsyun.baidu.com/index.php?title=webapi/directionlite-v1
application
1 2 3 4
| shop: address: ${sky.shop.address} baidu: ak: ${sky.baidu.ak}
|
OrderServiceImpl
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
| @Value("${sky.shop.address}") private String shopAddress;
@Value("${sky.baidu.ak}") private String ak;
private void checkOutOfRange(String address) { Map map = new HashMap(); map.put("address",shopAddress); map.put("output","json"); map.put("ak",ak);
String shopCoordinate = HttpClientUtil.doGet("https://api.map.baidu.com/geocoding/v3", map);
JSONObject jsonObject = JSON.parseObject(shopCoordinate); if(!jsonObject.getString("status").equals("0")){ throw new OrderBusinessException("店铺地址解析失败"); }
JSONObject location = jsonObject.getJSONObject("result").getJSONObject("location"); String lat = location.getString("lat"); String lng = location.getString("lng"); String shopLngLat = lat + "," + lng;
map.put("address",address); String userCoordinate = HttpClientUtil.doGet("https://api.map.baidu.com/geocoding/v3", map);
jsonObject = JSON.parseObject(userCoordinate); if(!jsonObject.getString("status").equals("0")){ throw new OrderBusinessException("收货地址解析失败"); }
location = jsonObject.getJSONObject("result").getJSONObject("location"); lat = location.getString("lat"); lng = location.getString("lng"); String userLngLat = lat + "," + lng;
map.put("origin",shopLngLat); map.put("destination",userLngLat); map.put("steps_info","0");
String json = HttpClientUtil.doGet("https://api.map.baidu.com/directionlite/v1/driving", map);
jsonObject = JSON.parseObject(json); if(!jsonObject.getString("status").equals("0")){ throw new OrderBusinessException("配送路线规划失败"); }
JSONObject result = jsonObject.getJSONObject("result"); JSONArray jsonArray = (JSONArray) result.get("routes"); Integer distance = (Integer) ((JSONObject) jsonArray.get(0)).get("distance");
if(distance > 5000){ throw new OrderBusinessException("超出配送范围"); }
|
改进用户下单
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
|
@Override @Transactional public OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO) { AddressBook addressBook = addressBookMapper.selectById(ordersSubmitDTO.getAddressBookId()); if (addressBook == null) { throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL); }
checkOutOfRange(addressBook.getProvinceName() + addressBook.getDistrictName() + addressBook.getDetail());
Long currentId = BaseContext.getCurrentId(); ShoppingCart shoppingCart = new ShoppingCart(); shoppingCart.setUserId(currentId); List<ShoppingCart> ShoppingCartList = shoppingCartMapper.list(shoppingCart); if (ShoppingCartList == null || ShoppingCartList.isEmpty()) { throw new AddressBookBusinessException(MessageConstant.SHOPPING_CART_IS_NULL); } Orders orders = new Orders(); BeanUtils.copyProperties(ordersSubmitDTO, orders); orders.setOrderTime(LocalDateTime.now()); orders.setPayStatus(Orders.UN_PAID); orders.setStatus(Orders.PENDING_PAYMENT); orders.setNumber(String.valueOf(System.currentTimeMillis())); orders.setPhone(addressBook.getPhone()); orders.setConsignee(addressBook.getConsignee()); orders.setUserId(currentId); orders.setAddress(addressBook.getProvinceName() + addressBook.getDistrictName() + addressBook.getDetail()); orders.setTablewareNumber(ordersSubmitDTO.getTablewareNumber()); orders.setTablewareStatus(ordersSubmitDTO.getTablewareStatus()); orders.setRemark(ordersSubmitDTO.getRemark()); orderMapper.insert(orders);
List<OrderDetail> orderDetailList = new ArrayList<>(); ShoppingCartList.forEach(cart -> { OrderDetail orderDetail = new OrderDetail(); BeanUtils.copyProperties(cart, orderDetail); orderDetail.setOrderId(orders.getId()); orderDetailList.add(orderDetail); }); MybatisBatch<OrderDetail> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, orderDetailList); MybatisBatch.Method<OrderDetail> method = new MybatisBatch.Method<>(OrderDetailMapper.class); mybatisBatch.execute(method.insert()); LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ShoppingCart::getUserId, currentId); shoppingCartMapper.delete(queryWrapper); OrderSubmitVO orderSubmitVO = OrderSubmitVO.builder() .id(orders.getId()) .orderTime(orders.getOrderTime()) .orderNumber(orders.getNumber()) .orderAmount(orders.getAmount()) .build(); return orderSubmitVO; }
|