|
|
|
@ -0,0 +1,260 @@
|
|
|
|
|
// 定义包名,表示该类属于com.sky.service.impl包
|
|
|
|
|
package com.sky.service.impl;
|
|
|
|
|
|
|
|
|
|
// 导入所需的类和接口
|
|
|
|
|
import com.alibaba.druid.support.json.JSONUtils;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.github.pagehelper.Page;
|
|
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
|
|
import com.sky.constant.MessageConstant;
|
|
|
|
|
import com.sky.context.BaseContext;
|
|
|
|
|
import com.sky.dto.*;
|
|
|
|
|
import com.sky.entity.*;
|
|
|
|
|
import com.sky.exception.AddressBookBusinessException;
|
|
|
|
|
import com.sky.exception.OrderBusinessException;
|
|
|
|
|
import com.sky.exception.ShoppingCartBusinessException;
|
|
|
|
|
import com.sky.mapper.*;
|
|
|
|
|
import com.sky.result.PageResult;
|
|
|
|
|
import com.sky.service.OrderService;
|
|
|
|
|
import com.sky.utils.WeChatPayUtil;
|
|
|
|
|
import com.sky.vo.OrderPaymentVO;
|
|
|
|
|
import com.sky.vo.OrderStatisticsVO;
|
|
|
|
|
import com.sky.vo.OrderSubmitVO;
|
|
|
|
|
import com.sky.vo.OrderVO;
|
|
|
|
|
import com.sky.webSocket.WebSocketServer;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
// 使用@Service注解标记这是一个Spring服务组件
|
|
|
|
|
@Service
|
|
|
|
|
public class OrderServiceImpl implements OrderService {
|
|
|
|
|
// 自动注入OrderMapper
|
|
|
|
|
@Autowired
|
|
|
|
|
private OrderMapper orderMapper;
|
|
|
|
|
// 自动注入OrderDetailMapper
|
|
|
|
|
@Autowired
|
|
|
|
|
private OrderDetailMapper orderDetailMapper;
|
|
|
|
|
// 自动注入AddressBookMapper
|
|
|
|
|
@Autowired
|
|
|
|
|
private AddressBookMapper addressBookMapper;
|
|
|
|
|
// 自动注入ShoppingCartMapper
|
|
|
|
|
@Autowired
|
|
|
|
|
private ShoppingCartMapper shoppingCartMapper;
|
|
|
|
|
// 自动注入UserMapper
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserMapper userMapper;
|
|
|
|
|
// 自动注入WeChatPayUtil
|
|
|
|
|
@Autowired
|
|
|
|
|
private WeChatPayUtil weChatPayUtil;
|
|
|
|
|
// 自动注入WebSocketServer
|
|
|
|
|
@Autowired
|
|
|
|
|
private WebSocketServer webSocketServer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 完成订单
|
|
|
|
|
*
|
|
|
|
|
* @param id 订单ID
|
|
|
|
|
*/
|
|
|
|
|
public void complete(Long id) {
|
|
|
|
|
// 根据id查询订单
|
|
|
|
|
Orders ordersDB = orderMapper.getById(id);
|
|
|
|
|
|
|
|
|
|
// 校验订单是否存在,并且状态为派送中
|
|
|
|
|
if (ordersDB == null || !ordersDB.getStatus().equals(Orders.DELIVERY_IN_PROGRESS)) {
|
|
|
|
|
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Orders orders = new Orders();
|
|
|
|
|
orders.setId(ordersDB.getId());
|
|
|
|
|
// 更新订单状态,状态转为完成
|
|
|
|
|
orders.setStatus(Orders.COMPLETED);
|
|
|
|
|
orders.setDeliveryTime(LocalDateTime.now());
|
|
|
|
|
|
|
|
|
|
orderMapper.update(orders);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 派送订单
|
|
|
|
|
*
|
|
|
|
|
* @param id 订单ID
|
|
|
|
|
*/
|
|
|
|
|
public void delivery(Long id) {
|
|
|
|
|
// 根据id查询订单
|
|
|
|
|
Orders ordersDB = orderMapper.getById(id);
|
|
|
|
|
|
|
|
|
|
// 校验订单是否存在,并且状态为已接单
|
|
|
|
|
if (ordersDB == null || !ordersDB.getStatus().equals(Orders.CONFIRMED)) {
|
|
|
|
|
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Orders orders = new Orders();
|
|
|
|
|
orders.setId(ordersDB.getId());
|
|
|
|
|
// 更新订单状态,状态转为派送中
|
|
|
|
|
orders.setStatus(Orders.DELIVERY_IN_PROGRESS);
|
|
|
|
|
|
|
|
|
|
orderMapper.update(orders);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 取消订单
|
|
|
|
|
*
|
|
|
|
|
* @param ordersCancelDTO 包含取消订单信息的DTO
|
|
|
|
|
*/
|
|
|
|
|
public void cancel(OrdersCancelDTO ordersCancelDTO) throws Exception {
|
|
|
|
|
// 根据id查询订单
|
|
|
|
|
Orders ordersDB = orderMapper.getById(ordersCancelDTO.getId());
|
|
|
|
|
|
|
|
|
|
// 支付状态
|
|
|
|
|
Integer payStatus = ordersDB.getPayStatus();
|
|
|
|
|
if (payStatus == 1) {
|
|
|
|
|
// 用户已支付,需要退款
|
|
|
|
|
// 调用微信支付退款接口
|
|
|
|
|
// String refund = weChatPayUtil.refund(
|
|
|
|
|
// ordersDB.getNumber(),
|
|
|
|
|
// ordersDB.getNumber(),
|
|
|
|
|
// new BigDecimal(0.01),
|
|
|
|
|
// new BigDecimal(0.01));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 管理端取消订单需要退款,根据订单id更新订单状态、取消原因、取消时间
|
|
|
|
|
Orders orders = new Orders();
|
|
|
|
|
orders.setId(ordersCancelDTO.getId());
|
|
|
|
|
orders.setStatus(Orders.CANCELLED);
|
|
|
|
|
orders.setCancelReason(ordersCancelDTO.getCancelReason());
|
|
|
|
|
orders.setCancelTime(LocalDateTime.now());
|
|
|
|
|
orderMapper.update(orders);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 拒单
|
|
|
|
|
*
|
|
|
|
|
* @param ordersRejectionDTO 包含拒单信息的DTO
|
|
|
|
|
*/
|
|
|
|
|
public void rejection(OrdersRejectionDTO ordersRejectionDTO) throws Exception {
|
|
|
|
|
// 根据id查询订单
|
|
|
|
|
Orders ordersDB = orderMapper.getById(ordersRejectionDTO.getId());
|
|
|
|
|
|
|
|
|
|
// 订单只有存在且状态为待接单才可以拒单
|
|
|
|
|
if (ordersDB == null || !ordersDB.getStatus().equals(Orders.TO_BE_CONFIRMED)) {
|
|
|
|
|
throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 支付状态
|
|
|
|
|
Integer payStatus = ordersDB.getPayStatus();
|
|
|
|
|
if (Objects.equals(payStatus, Orders.PAID)) {
|
|
|
|
|
// 用户已支付,需要退款
|
|
|
|
|
// 调用微信支付退款接口
|
|
|
|
|
// String refund = weChatPayUtil.refund(
|
|
|
|
|
// ordersDB.getNumber(),
|
|
|
|
|
// ordersDB.getNumber(),
|
|
|
|
|
// new BigDecimal(0.01),
|
|
|
|
|
// new BigDecimal(0.01));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 拒单需要退款,根据订单id更新订单状态、拒单原因、取消时间
|
|
|
|
|
Orders orders = new Orders();
|
|
|
|
|
orders.setId(ordersDB.getId());
|
|
|
|
|
orders.setStatus(Orders.CANCELLED);
|
|
|
|
|
orders.setRejectionReason(ordersRejectionDTO.getRejectionReason());
|
|
|
|
|
orders.setCancelTime(LocalDateTime.now());
|
|
|
|
|
|
|
|
|
|
orderMapper.update(orders);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 接单
|
|
|
|
|
*
|
|
|
|
|
* @param ordersConfirmDTO 包含接单信息的DTO
|
|
|
|
|
*/
|
|
|
|
|
public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
|
|
|
|
|
Orders orders = Orders.builder()
|
|
|
|
|
.id(ordersConfirmDTO.getId())
|
|
|
|
|
.status(Orders.CONFIRMED)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
orderMapper.update(orders);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 各个状态的订单数量统计
|
|
|
|
|
*
|
|
|
|
|
* @return 订单统计视图对象
|
|
|
|
|
*/
|
|
|
|
|
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 orderStatisticsVO = new OrderStatisticsVO();
|
|
|
|
|
orderStatisticsVO.setToBeConfirmed(toBeConfirmed);
|
|
|
|
|
orderStatisticsVO.setConfirmed(confirmed);
|
|
|
|
|
orderStatisticsVO.setDeliveryInProgress(deliveryInProgress);
|
|
|
|
|
return orderStatisticsVO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 订单搜索
|
|
|
|
|
*
|
|
|
|
|
* @param ordersPageQueryDTO 分页查询参数
|
|
|
|
|
* @return 分页查询结果
|
|
|
|
|
*/
|
|
|
|
|
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) {
|
|
|
|
|
// 需要返回订单菜品信息,自定义OrderVO响应结果
|
|
|
|
|
List<OrderVO> orderVOList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
List<Orders> 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据订单id获取菜品信息字符串
|
|
|
|
|
*
|
|
|
|
|
* @param orders 订单对象
|
|
|
|
|
* @return 订单菜品信息字符串
|
|
|
|
|
*/
|
|
|
|
|
private String getOrderDishesStr(Orders orders) {
|
|
|
|
|
// 查询订单菜品详情信息(订单中的菜品和数量)
|
|
|
|
|
List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderId(orders.getId());
|
|
|
|
|
|
|
|
|
|
// 将每一条订单菜品信息拼接为字符串(格式:宫保鸡丁*3;)
|
|
|
|
|
List<String> orderDishList = orderDetailList.stream().map(x -> {
|
|
|
|
|
String orderDish = x.getName() + "*" + x.getNumber() + ";";
|
|
|
|
|
return orderDish;
|
|
|
|
|
}).collect
|