苍穹外卖--用户下单功能实现
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.sky.controller.user;
|
||||
|
||||
|
||||
import com.sky.dto.OrdersSubmitDTO;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.OrderService;
|
||||
import com.sky.vo.OrderSubmitVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@Slf4j
|
||||
@RequestMapping("/user/order")
|
||||
@Api(tags = "用户端订单相关的接口")
|
||||
public class OrderController {
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
|
||||
@PostMapping("/submit")
|
||||
@ApiOperation("用户下单")
|
||||
public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
|
||||
log.info("用户下单:{}",ordersSubmitDTO);
|
||||
//调用业务端接口
|
||||
OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO);
|
||||
return Result.success(orderSubmitVO);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.sky.entity.OrderDetail;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Mapper
|
||||
public interface OrderDetailMapper {
|
||||
/**
|
||||
* 订单明细表,批量插入
|
||||
* @param orderDetailList
|
||||
*/
|
||||
void insertBatch(ArrayList<OrderDetail> orderDetailList);
|
||||
}
|
||||
19
sky-server/src/main/java/com/sky/mapper/OrderMapper.java
Normal file
19
sky-server/src/main/java/com/sky/mapper/OrderMapper.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.sky.entity.Orders;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Options;
|
||||
|
||||
@Mapper
|
||||
public interface OrderMapper {
|
||||
|
||||
@Insert("insert into orders (number, status, user_id, address_book_id, order_time, checkout_time," +
|
||||
" pay_method, pay_status, amount, remark," +"phone, address, consignee, estimated_delivery_time," +
|
||||
" delivery_status, pack_amount,tableware_number,tableware_status) values (#{number}, #{status}," +
|
||||
" #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod},#{payStatus}, " +
|
||||
"#{amount}, #{remark}, #{phone}, #{address}, #{consignee},#{estimatedDeliveryTime}, " +
|
||||
"#{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})")
|
||||
@Options(useGeneratedKeys = true,keyProperty = "id")
|
||||
void insert(Orders orders);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
10
sky-server/src/main/resources/mapper/OrderDetailMapper.xml
Normal file
10
sky-server/src/main/resources/mapper/OrderDetailMapper.xml
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.sky.mapper.OrderDetailMapper">
|
||||
<insert id="insertBatch" parameterType="java.util.List">
|
||||
insert into order_detail (name, image, order_id, dish_id, setmeal_id, dish_flavor, number, amount) VALUES
|
||||
<foreach collection="orderDetailList" separator="," item="od">
|
||||
(#{od.name},#{od.image},#{od.orderId},#{od.dishId},#{od.setmealId},#{od.dishFlavor},#{od.number},#{od.amount})
|
||||
</foreach>
|
||||
</insert>
|
||||
</mapper>
|
||||
4
sky-server/src/main/resources/mapper/OrderMapper.xml
Normal file
4
sky-server/src/main/resources/mapper/OrderMapper.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.sky.mapper.OrderMapper">
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user