查询历史订单
service层代码
@Override
public PageResult pageQuery(Integer page, Integer pageSize, Integer status) {
// 开启分页查询
PageHelper.startPage(page, pageSize);
OrdersPageQueryDTO ordersPageQueryDTO = new OrdersPageQueryDTO();
ordersPageQueryDTO.setStatus(status);
Long userId = BaseContext.getCurrentId();
ordersPageQueryDTO.setUserId(userId);
Page orders = orderMapper.pageQuery(ordersPageQueryDTO);
List list = new ArrayList<>();
// 查询出订单明细,并封装入OrderVO进行响应
if (orders != null && orders.getTotal() > 0) {
for (Orders order : orders) {
Long orderId = order.getId(); // 每个订单的id
// 查询订单明细
List orderDetails = orderDetailMapper.getByOrderId(orderId);
OrderVO orderVO = new OrderVO();
BeanUtils.copyProperties(order, orderVO);
orderVO.setOrderDetailList(orderDetails);
list.add(orderVO);
}
}
long total = orders.getTotal();
return new PageResult(total, list);
}
查询订单详情
取消订单(用户端)
service层代码
@Override
public void cancel(Long id) {
// 根据id查询订单
Orders orderDB = orderMapper.getById(id);
// 校验订单是否存在
if (orderDB == null) {
throw new OrderBusinessException(MessageConstant.ORDER_NOT_FOUND);
}
if (orderDB.getStatus() > 2) {
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
}
Orders orders = new Orders();
orders.setId(orderDB.getId());
if (orderDB.getStatus().equals(Orders.TO_BE_CONFIRMED)) {
// 订单处于待接单状态,需要给用户退款
try {
// 调用微信支付退款接口
weChatPayUtil.refund(
orderDB.getNumber(),
orderDB.getNumber(),
new BigDecimal(String.valueOf(orderDB.getAmount())),
new BigDecimal(String.valueOf(orderDB.getAmount()))
);
// 支付状态修改为 退款
orders.setStatus(Orders.REFUND);
} catch (Exception e) {
throw new OrderBusinessException("退款失败");
}
}
// 更新订单状态、取消原因、取消时间
orders.setStatus(Orders.CANCELLED);
orders.setCancelReason("用户取消");
orders.setCancelTime(LocalDateTime.now());
orderMapper.update(orders);
}
再来一单
service层代码
@Override
public void repetition(Long id) {
// 根据orderId查询订单详细数据
List orderDetailList = orderDetailMapper.getByOrderId(id);
Long userId = BaseContext.getCurrentId();
// 将订单详情对象转换为购物车对象
List 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());
// 批量插入
shoppingCartMapper.insertBatch(shoppingCartList);
}
订单搜索
service层代码
@Override
public PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
// 开启分页
PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
Page page = orderMapper.pageQuery(ordersPageQueryDTO);
// 部分订单状态,需要额外返回订单菜品信息,将Orders转化为OrderVO
List orderVOList = getOrderVOList(page);
return new PageResult(page.getTotal(), orderVOList);
}
private List getOrderVOList (Page page) {
// 需要返回订单菜品信息,自定义OrderVO的响应结果
List orderVOList = new ArrayList<>();
List ordersList = page.getResult();
if (!CollectionUtils.isEmpty(ordersList)) {
for (Orders orders : ordersList) {
// 将共同字段复制到OrderVO
OrderVO orderVO = new OrderVO();
BeanUtils.copyProperties(orders, orderVO);
String orderDishes = getOrderDishesStr(orders);
// 将订单菜品信息封装到orderVO中,并添加到orderVOList
orderVO.setOrderDishes(orderDishes);
orderVOList.add(orderVO);
}
}
return orderVOList;
}
private String getOrderDishesStr(Orders orders) {
// 查询订单菜品详情信息(订单中的菜品和数量)
List orderDetailList = orderDetailMapper.getByOrderId(orders.getId());
// 将每一条订单菜品信息拼接为字符串(格式name*number)
List orderDishList = orderDetailList.stream().map(x -> {
return x.getName() + "*" + x.getNumber() + ";";
}).collect(Collectors.toList());
// 将该订单对应的所有菜品信息拼接到在一起
return String.join("", orderDishList);
}
订单状态统计
service层代码
@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;
}
接单
service层代码
@Override
public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
Orders orders = new Orders();
orders.setId(ordersConfirmDTO.getId());
orders.setStatus(Orders.CONFIRMED);
// 修改订单状态为接单
orderMapper.update(orders);
}
拒单
service层代码
@Override
public void rejection(OrdersRejectionDTO ordersRejectionDTO) {
// 根据id查询订单
Orders ordersDB = orderMapper.getById(ordersRejectionDTO.getId());
// 订单只有存在且状态为2(待接单)才可以拒单
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)) {
// 若用户已支付,需要退款
try {
String refund = weChatPayUtil.refund(
ordersDB.getNumber(),
ordersDB.getNumber(),
new BigDecimal(String.valueOf(ordersDB.getAmount())),
new BigDecimal(String.valueOf(ordersDB.getAmount())));
log.info("申请退款:{}", refund);
} catch (Exception e) {
throw new OrderBusinessException("退款失败");
}
}
// 更新订单状态
Orders orders = Orders.builder()
.id(ordersRejectionDTO.getId())
.rejectionReason(ordersRejectionDTO.getRejectionReason())
.build();
orders.setStatus(Orders.CANCELLED);
orderMapper.update(orders);
}
取消订单(管理端)
service层代码
@Override
public void cancelOrder(OrdersCancelDTO ordersCancelDTO) {
Orders orderDB = orderMapper.getById(ordersCancelDTO.getId());
// 如果用户已完成支付,需要退款
if (orderDB.getPayStatus().equals(Orders.PAID)) {
try {
String refund = weChatPayUtil.refund(
orderDB.getNumber(),
orderDB.getNumber(),
new BigDecimal(String.valueOf(orderDB.getAmount())),
new BigDecimal(String.valueOf(orderDB.getAmount())));
log.info("申请退款:{}", refund);
} catch (Exception e) {
throw new OrderBusinessException("退款失败");
}
}
// 更新订单状态
Orders orders = new Orders();
orders.setId(ordersCancelDTO.getId());
orders.setStatus(Orders.CANCELLED);
orders.setCancelReason(ordersCancelDTO.getCancelReason());
orders.setCancelTime(LocalDateTime.now());
orderMapper.update(orders);
}
派送订单
service层代码
@Override
public void delivery(Long id) {
Orders ordersDB = orderMapper.getById(id);
// 订单状态是否为待派送
if (ordersDB == null || !ordersDB.getStatus().equals(Orders.CONFIRMED)) {
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
}
Orders orders = Orders.builder()
.id(id)
.status(Orders.DELIVERY_IN_PROGRESS)
.build();
// 更新订单状态
orderMapper.update(orders);
}
完成订单