功能开发-订单功能
一、需求分析
二、数据模型
当购物车付款时候,购物车所有清空,而明细表就类似购物车购买后就迁移到明细表,而订单表不会过多描写下单了什么菜,只会描述金额地址之类
三、准备工作
导入 AddressBook实体类,mapper,service,controllerr,
编写增删改查功能,
查询所有功能,查询默认功能
只需要OrderController就行了,都是在这实现
四、代码开发
4.1、OrderService
package com.xxxit.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xxxit.reggie.entity.Orders;
public interface OrderService extends IService<Orders> {
// 用户下单
public void submit(Orders orders);
}
4.2、OrderServiceImpl
package com.xxxit.reggie.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxit.reggie.common.BaseContext;
import com.xxxit.reggie.common.CustomException;
import com.xxxit.reggie.common.R;
import com.xxxit.reggie.common.User;
import com.xxxit.reggie.entity.AddressBook;
import com.xxxit.reggie.entity.OrderDetail;
import com.xxxit.reggie.entity.Orders;
import com.xxxit.reggie.entity.ShoppingCart;
import com.xxxit.reggie.mapper.OrderMapper;
import com.xxxit.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
@Autowired
private OrderDetailService orderDetailService;
@Autowired
private ShoppingCartService shoppingCartService;
@Autowired
private UserService userService;
@Autowired
private AddressBookService addressBookService;
/**
* 用户下单
* @param orders
* @return
*/
public void submit(Orders orders){
// 获取当前用户id
Long userId = BaseContext.getCurrentId();
// 查询当前购物车数据
LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
List<ShoppingCart> shoppingCartList = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
// 如果购物车为空,不能下单
if (shoppingCartList == null){
throw new CustomException("购物车为空,不能下单");
}
// 查询当前地址是否存在
Long addressBookId = orders.getAddressBookId();
AddressBook addressBook = addressBookService.getById(addressBookId);
if (addressBook == null){
throw new CustomException("地址有误,不能下单");
}
// 订单id和订单号使用同一个
long orderId = IdWorker.getId();
// 计算总金额并放在线程错误
AtomicInteger amount = new AtomicInteger(0);
// 新增订单明细,相当于把购物车的单搬来这里
List<OrderDetail> orderDetails = shoppingCartList.stream().map((item) -> {
OrderDetail orderDetail = new OrderDetail();
orderDetail.setOrderId(orderId);
orderDetail.setNumber(item.getNumber());
orderDetail.setDishFlavor(item.getDishFlavor());
orderDetail.setDishId(item.getDishId());
orderDetail.setSetmealId(item.getSetmealId());
orderDetail.setName(item.getName());
orderDetail.setImage(item.getImage());
orderDetail.setAmount(item.getAmount());
amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
return orderDetail;
}).collect(Collectors.toList());
//查询用户数据
User user = userService.getById(userId);
// 新增订单
orders.setId(orderId);
orders.setOrderTime(LocalDateTime.now());
orders.setCheckoutTime(LocalDateTime.now());
orders.setStatus(2);
orders.setAmount(new BigDecimal(amount.get()));//总金额
orders.setUserId(userId);
orders.setNumber(String.valueOf(orderId));
orders.setUserName(user.getName());
orders.setConsignee(addressBook.getConsignee());
orders.setPhone(addressBook.getPhone());
orders.setAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
+ (addressBook.getCityName() == null ? "" : addressBook.getCityName())
+ (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
+ (addressBook.getDetail() == null ? "" : addressBook.getDetail()));
//向订单表插入数据,一条数据
this.save(orders);
//向订单明细表插入数据,多条数据
orderDetailService.saveBatch(orderDetails);
//清空购物车数据
shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
}
}
4.3、Controller
package com.xxxit.reggie.controller;
import com.xxxit.reggie.common.R;
import com.xxxit.reggie.entity.Orders;
import com.xxxit.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping("/submit")
public R<String> submit(@RequestBody Orders orders){
log.info("订单数据:{}",orders.toString());
orderService.submit(orders);
return R.success("下单成功");
}
}