二. 用户下单
OrderController
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;
/**
* @author Jie.
* @description: TODO
* @date 2024/10/10
* @version: 1.0
*/
@RestController
@RequestMapping("/user/order")
@Api(tags = "订单接口")
@Slf4j
public class OrderController {
@Autowired
private OrderService orderService;
/**
* 用户下单
*/
@PostMapping("/submit")
@ApiOperation(value = "用户下单")
public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
log.info("用户下单:{}", ordersSubmitDTO);
OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
return Result.success(orderSubmitVO);
}
}
OrderService
package com.sky.service;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.vo.OrderSubmitVO;
/**
* @author Jie.
* @description: TODO
* @date 2024/10/10
* @version: 1.0
*/
public interface OrderService {
/**
* 用户下单
*/
OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO);
}
OrderMapper
package com.sky.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
/**
* @author Jie.
* @description: TODO
* @date 2024/10/10
* @version: 1.0
*/
@Mapper
public interface OrderMapper extends BaseMapper<Orders> {
}
OrderDetailMapper
package com.sky.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.entity.OrderDetail;
import org.apache.ibatis.annotations.Mapper;
/**
* @author Jie.
* @description: TODO
* @date 2024/10/10
* @version: 1.0
*/
@Mapper
public interface OrderDetailMapper extends BaseMapper<OrderDetail> {
}
package com.sky.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.entity.OrderDetail;
import org.apache.ibatis.annotations.Mapper;
/**
* @author Jie.
* @description: TODO
* @date 2024/10/10
* @version: 1.0
*/
@Mapper
public interface OrderDetailMapper extends BaseMapper<OrderDetail> {
}
OrderServiceImpl
package com.sky.service.impl;
import com.baomidou.mybatisplus.core.batch.MybatisBatch;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.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 lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
/**
-
@author Jie.
-
@description: TODO
-
@date 2024/10/10
-
@version: 1.0
*/
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {@Autowired
private OrderMapper orderMapper;@Autowired
private AddressBookMapper addressBookMapper;@Autowired
private ShoppingCartMapper shoppingCartMapper;@Autowired
private SqlSessionFactory sqlSessionFactory;/**
-
用户下单
*/
@Override
public OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO) {
//处理各种业务异常(地址簿为空,购物车为空,商品库存不足等)
AddressBook addressBook = addressBookMapper.selectById(ordersSubmitDTO.getAddressBookId());
if (addressBook == null) {
throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
}Long currentId = BaseContext.getCurrentId();
ShoppingCart shoppingCart = new ShoppingCart();
shoppingCart.setUserId(currentId);
List ShoppingCartList = shoppingCartMapper.list(shoppingCart);
if (ShoppingCartList == null || ShoppingCartList.isEmpty()) {
throw new AddressBookBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
}
//向订单表插入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.getPhone());
orders.setConsignee(addressBook.getConsignee());
orders.setUserId(currentId);orderMapper.insert(orders);
List orderDetailList = new ArrayList<>();
//向订单详情表插入多条数据
ShoppingCartList.forEach(cart -> {
OrderDetail orderDetail = new OrderDetail();
BeanUtils.copyProperties(cart, orderDetail);
orderDetail.setOrderId(orders.getId());
orderDetailList.add(orderDetail);
});
MybatisBatch mybatisBatch = new MybatisBatch<>(sqlSessionFactory, orderDetailList);
MybatisBatch.Method method = new MybatisBatch.Method<>(OrderDetailMapper.class);
mybatisBatch.execute(method.insert());
//如果下单成功,清空购物车数据
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId, currentId);
shoppingCartMapper.delete(queryWrapper);
//封装返回数据
OrderSubmitVO orderSubmitVO = OrderSubmitVO.builder()
.id(orders.getId())
.orderTime(orders.getOrderTime())
.orderNumber(orders.getNumber())
.orderAmount(orders.getAmount())
.build();
return orderSubmitVO;
}
}
-
package com.sky.service.impl;
import com.baomidou.mybatisplus.core.batch.MybatisBatch;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.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 lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
/**
* @author Jie.
* @description: TODO
* @date 2024/10/10
* @version: 1.0
*/
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private AddressBookMapper addressBookMapper;
@Autowired
private ShoppingCartMapper shoppingCartMapper;
@Autowired
private SqlSessionFactory sqlSessionFactory;
/**
* 用户下单
*/
@Override
public OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO) {
//处理各种业务异常(地址簿为空,购物车为空,商品库存不足等)
AddressBook addressBook = addressBookMapper.selectById(ordersSubmitDTO.getAddressBookId());
if (addressBook == null) {
throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
}
Long currentId = BaseContext.getCurrentId();
ShoppingCart shoppingCart = new ShoppingCart();
shoppingCart.setUserId(currentId);
List<ShoppingCart> ShoppingCartList = shoppingCartMapper.list(shoppingCart);
if (ShoppingCartList == null || ShoppingCartList.isEmpty()) {
throw new AddressBookBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
}
//向订单表插入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.getPhone());
orders.setConsignee(addressBook.getConsignee());
orders.setUserId(currentId);
orderMapper.insert(orders);
List<OrderDetail> orderDetailList = new ArrayList<>();
//向订单详情表插入多条数据
ShoppingCartList.forEach(cart -> {
OrderDetail orderDetail = new OrderDetail();
BeanUtils.copyProperties(cart, orderDetail);
orderDetail.setOrderId(orders.getId());
orderDetailList.add(orderDetail);
});
MybatisBatch<OrderDetail> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, orderDetailList);
MybatisBatch.Method<OrderDetail> method = new MybatisBatch.Method<>(OrderDetailMapper.class);
mybatisBatch.execute(method.insert());
//如果下单成功,清空购物车数据
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId, currentId);
shoppingCartMapper.delete(queryWrapper);
//封装返回数据
OrderSubmitVO orderSubmitVO = OrderSubmitVO.builder()
.id(orders.getId())
.orderTime(orders.getOrderTime())
.orderNumber(orders.getNumber())
.orderAmount(orders.getAmount())
.build();
return orderSubmitVO;
}
}