本篇博客主要是用户端的功能完善
主要是三个功能
1.导入地址簿 2.点击去结算弹出结算页面 3.微信支付功能
这个地址簿就是一个表的CRUD,所以我们直接导入了
一个业务功能可能对应多个接口,比如修改地址这个业务功能
其实对应 查询回显+修改 两个接口
看下数据库设计,具体的接口设计就不展示了
@RestController("userOrderController")//起个bean的别名因为等会再admin也会创建一个controller
@Slf4j
@RequestMapping("/user/order")
@Api(tags = "用户端订单相关接口")
public class OrderController {
@Autowired
private OrderService orderService;
/**
* 用户下单
* @param ordersSubmitDTO
* @return
*/
@PostMapping("/submit")
@ApiOperation("用户下单")
public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
log.info("用户下单,参数为:{}",ordersSubmitDTO);
OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
return Result.success(orderSubmitVO);
}
}
service重量级
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderDetailMapper orderDetailMapper;
@Autowired
private AddressBookMapper addressBookMapper;
@Autowired
private ShoppingCartMapper shoppingCartMapper;
/**
* 用户下单
* @param ordersSubmitDTO
* @return
*/
public OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO) {
//业务异常处理(为空地址簿)
AddressBook addressBook = addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());
if(addressBook == null){
throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
}
//业务异常处理(购物车数据为空)
ShoppingCart shoppingCart = new ShoppingCart();
Long userId = BaseContext.getCurrentId();
shoppingCart.setUserId(userId);
List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);//至少查询对应用户的购物车
if(list == null){
throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
}
//1.向订单表插入一条数据
Orders orders = new Orders();//自己去看下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(userId);
orderMapper.insert(orders);
List<OrderDetail> orderDetailList = new ArrayList<>();
//2.向订单明细表插入n条数据
for (ShoppingCart cart : list) {
OrderDetail orderDetail = new OrderDetail();//订单明细
BeanUtils.copyProperties(cart,orderDetail);
orderDetail.setOrderId(orders.getId());//设置当前订单明细关联的订单id
orderDetailList.add(orderDetail);
}
orderDetailMapper.insertBatch(orderDetailList);//批量插入
//3.清空用户的购物车数据
shoppingCartMapper.deleteByUserId(userId);
//4.封装VO返回结果
OrderSubmitVO orderSubmitVO = OrderSubmitVO.builder()
.id(orders.getId())
.orderTime(orders.getOrderTime())
.orderNumber(orders.getNumber())
.orderAmount(orders.getAmount())
.build();
return orderSubmitVO;
}
}
orderDetailMapper.xml
<insert id="insertBatch">
insert into order_detail (name, image, order_id, dish_id, setmeal_id, dish_flavor,number,amount)
values
<foreach collection="orderDetailList" item="od" separator=",">
(#{od.name},#{od.image},#{od.orderId},#{od.dishId},#{od.setmealId},#{od.dishFlavor},#{od.number},#{od.amount})
foreach>
insert>
orderMapper.xml
<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,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},
#{estimatedDeliveryTime},#{deliveryStatus},#{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})
-- 细心一点
</insert>