用户下单
This commit is contained in:
parent
ec49bba3b3
commit
d24c17e1a9
4
pom.xml
4
pom.xml
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>sky-take-out</artifactId>
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package com.sky.controller.admin;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController("adminOrderController")
|
||||
@RequestMapping("/admin/order")
|
||||
@Api(tags = "管理端订单接口")
|
||||
public class orderController {
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
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.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;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@RestController("userOrderController")
|
||||
@RequestMapping("/user/order")
|
||||
@Api(tags = "用户端订单相关接口")
|
||||
@Slf4j
|
||||
public class OrderController {
|
||||
@Resource
|
||||
private OrderService orderService;
|
||||
|
||||
/**
|
||||
* 用户下单
|
||||
*
|
||||
* @param ordersSubmitDTO 订单请求数据
|
||||
* @return Result<OrderSubmitVO>
|
||||
*/
|
||||
@ApiOperation("用户下单")
|
||||
@PostMapping("/submit")
|
||||
public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
|
||||
log.info("用户下单:{}", ordersSubmitDTO);
|
||||
OrderSubmitVO orderSubmitVO = orderService.submitOrder(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 java.util.ArrayList;
|
||||
|
||||
@Mapper
|
||||
public interface OrderDetailMapper {
|
||||
/**
|
||||
* 批量插入订单明细数据
|
||||
*
|
||||
* @param orderDetailArrayList ArrayList<OrderDetail>
|
||||
*/
|
||||
void insertBatch(ArrayList<OrderDetail> orderDetailArrayList);
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.sky.mapper;
|
||||
|
||||
import com.sky.entity.Orders;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface OrderMapper {
|
||||
// 向订单中插入1条数据
|
||||
void insert(Orders orders);
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.OrdersSubmitDTO;
|
||||
import com.sky.vo.OrderSubmitVO;
|
||||
|
||||
public interface OrderService {
|
||||
/**
|
||||
* 用户下单
|
||||
*
|
||||
* @param ordersSubmitDTO 用户下单请求数据
|
||||
* @return OrderSubmitVO
|
||||
*/
|
||||
OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO);
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
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.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
@Resource
|
||||
private OrderMapper orderMapper;
|
||||
@Resource
|
||||
private OrderDetailMapper orderDetailMapper;
|
||||
@Resource
|
||||
private AddressBookMapper addressBookMapper;
|
||||
@Resource
|
||||
private ShoppingCartMapper shoppingCartMapper;
|
||||
|
||||
/**
|
||||
* 用户下单
|
||||
*
|
||||
* @param ordersSubmitDTO 用户下单请求数据
|
||||
* @return OrderSubmitVO
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO) {
|
||||
// 处理各种异常 (地址簿为空,购物车为空)
|
||||
AddressBook addressBook = addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());
|
||||
if (addressBook == null) {
|
||||
// 抛出异常,购物车为空
|
||||
throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
|
||||
}
|
||||
|
||||
// 1. 查询当前用户的购物车数据
|
||||
ShoppingCart shoppingCart = new ShoppingCart();
|
||||
Long currentId = BaseContext.getCurrentId();
|
||||
shoppingCart.setUserId(currentId);
|
||||
List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
|
||||
if (shoppingCartList == null || shoppingCartList.isEmpty()) {
|
||||
throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
|
||||
}
|
||||
|
||||
// 2. 向订单中插入1条数据
|
||||
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.getConsignee());
|
||||
orders.setUserId(currentId);
|
||||
orderMapper.insert(orders);
|
||||
|
||||
ArrayList<OrderDetail> orderDetailArrayList = new ArrayList<>();
|
||||
// 3. 向订单数据插入n条数据
|
||||
for (ShoppingCart cart : shoppingCartList) {
|
||||
OrderDetail orderDetail = new OrderDetail();
|
||||
BeanUtils.copyProperties(cart, orderDetail);
|
||||
orderDetail.setOrderId(orders.getId());
|
||||
orderDetailArrayList.add(orderDetail);
|
||||
}
|
||||
orderDetailMapper.insertBatch(orderDetailArrayList);
|
||||
|
||||
// 4. 清空当前用户购物车数据
|
||||
shoppingCartMapper.deleteByUserId(currentId);
|
||||
// 5. 封装返回结果
|
||||
return OrderSubmitVO.builder()
|
||||
.id(orders.getId())
|
||||
.orderTime(orders.getOrderTime())
|
||||
.orderNumber(orders.getNumber())
|
||||
.orderAmount(orders.getAmount()).build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<?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">
|
||||
insert into order_detail ( name, image, order_id, dish_id, setmeal_id, dish_flavor, number, amount)
|
||||
values
|
||||
<foreach collection="orderDetailArrayList" item="od" separator=",">
|
||||
( #{od.name}, #{od.image}, #{od.orderId}, #{od.dishId}, #{od.setmealId}, #{od.dishFlavor}, #{od.number},
|
||||
#{od.amount})
|
||||
</foreach>
|
||||
</insert>
|
||||
</mapper>
|
|
@ -0,0 +1,16 @@
|
|||
<?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">
|
||||
|
||||
<!-- 向订单中插入1条数据 -->
|
||||
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
|
||||
insert into orders (number, status, user_id, address_book_id, order_time, checkout_time, pay_method,
|
||||
pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason,
|
||||
rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time,
|
||||
pack_amount, tableware_number, tableware_status)
|
||||
values (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod},
|
||||
#{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{userName}, #{consignee}, #{cancelReason},
|
||||
#{rejectionReason}, #{cancelTime}, #{estimatedDeliveryTime}, #{deliveryStatus}, #{deliveryTime},
|
||||
#{packAmount}, #{tablewareNumber}, #{tablewareStatus});
|
||||
</insert>
|
||||
</mapper>
|
Loading…
Reference in New Issue