苍穹外卖--用户下单功能实现
This commit is contained in:
13
sky-server/src/main/java/com/sky/service/OrderService.java
Normal file
13
sky-server/src/main/java/com/sky/service/OrderService.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.OrdersSubmitDTO;
|
||||
import com.sky.vo.OrderSubmitVO;
|
||||
|
||||
public interface OrderService {
|
||||
/**
|
||||
* 用户下单
|
||||
* @param ordersSubmitDTO
|
||||
* @return
|
||||
*/
|
||||
OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO);
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.sky.service.impl;
|
||||
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.context.BaseContext;
|
||||
import com.sky.dto.OrdersSubmitDTO;
|
||||
import com.sky.entity.AddressBook;
|
||||
import com.sky.entity.OrderDetail;
|
||||
import com.sky.entity.Orders;
|
||||
import com.sky.entity.ShoppingCart;
|
||||
import com.sky.exception.AddressBookBusinessException;
|
||||
import com.sky.exception.ShoppingCartBusinessException;
|
||||
import com.sky.mapper.AddressBookMapper;
|
||||
import com.sky.mapper.OrderDetailMapper;
|
||||
import com.sky.mapper.OrderMapper;
|
||||
import com.sky.mapper.ShoppingCartMapper;
|
||||
import com.sky.service.OrderService;
|
||||
import com.sky.vo.OrderSubmitVO;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
|
||||
@Autowired
|
||||
private AddressBookMapper addressBookMapper;
|
||||
|
||||
@Autowired
|
||||
private ShoppingCartMapper shoppingCartMapper;
|
||||
|
||||
@Autowired
|
||||
private OrderDetailMapper orderDetailMapper;
|
||||
|
||||
/**
|
||||
* 用户下单
|
||||
* @param ordersSubmitDTO
|
||||
* @return
|
||||
*/
|
||||
@Transactional //让订单表 订单明细表 购物车表的sql操作同时成功或失败
|
||||
@Override
|
||||
public OrderSubmitVO submit(OrdersSubmitDTO ordersSubmitDTO) {
|
||||
//1.处理各种业务异常(地址簿为空,购物车中数据为空)
|
||||
AddressBook addressBook = addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());
|
||||
if (addressBook == null) {
|
||||
throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
|
||||
}
|
||||
ShoppingCart cart = new ShoppingCart();
|
||||
cart.setUserId(BaseContext.getCurrentId());
|
||||
List<ShoppingCart> cartList = shoppingCartMapper.list(cart);
|
||||
if (cartList == null || cartList.size() == 0) {
|
||||
throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
|
||||
}
|
||||
|
||||
//2.向订单表orders中插入一条数据
|
||||
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(BaseContext.getCurrentId());
|
||||
orders.setAddress(addressBook.getProvinceName()+addressBook.getCityName()+addressBook.getDistrictName()+addressBook.getDetail());//详细地址:省市区+地址
|
||||
|
||||
orderMapper.insert(orders);
|
||||
|
||||
//3.向订单明细表插入N条商品明细记录
|
||||
ArrayList<OrderDetail> orderDetails = new ArrayList<>();//订单明细集合
|
||||
for (ShoppingCart shoppingCart : cartList) {
|
||||
OrderDetail orderDetail = new OrderDetail();
|
||||
BeanUtils.copyProperties(shoppingCart,orderDetail);
|
||||
//动态设置订单明细属于哪个订单
|
||||
orderDetail.setOrderId(orders.getId());
|
||||
orderDetails.add(orderDetail);
|
||||
}
|
||||
|
||||
//批量插入数据
|
||||
orderDetailMapper.insertBatch(orderDetails);
|
||||
|
||||
//4.清空当前用户的购物车数据
|
||||
shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
|
||||
|
||||
//5.封装一个标准OrderSubmitVO返回给前端显示内容
|
||||
OrderSubmitVO submitVO = OrderSubmitVO.builder()
|
||||
.id(orders.getId())
|
||||
.orderNumber(orders.getNumber())
|
||||
.orderAmount(orders.getAmount())
|
||||
.orderTime(orders.getOrderTime()).build();
|
||||
return submitVO;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user