导入用户地址簿相关功能代码
菜品展示
购物车模块
加入购物车:
@PostMapping("/add") public R add(@RequestBody ShoppingCart shoppingCart){ //获取当前线程用户的id并设置到shoppingCart中 Long currentId = BaseContext.getCurrentId(); shoppingCart.setUserId(currentId); //首先我们要判断当前购物车是否已经有同样的dish或者setmeal LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>(); //先判断 lambdaQueryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId()); //如果有dishID说明是添加的是菜品,没有则表示是套餐 if (shoppingCart.getDishId()!=null){ lambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId()); }else { lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId()); } ShoppingCart shoppingCart1 = shoppingCartService.getOne(lambdaQueryWrapper); //如果查出来了说明当前已经有了,我们把number+1 if (shoppingCart1!=null){ shoppingCart1.setNumber(shoppingCart1.getNumber()+1); shoppingCartService.updateById(shoppingCart1); }else { //如果没查出来说明没有,我们存储数据 初始化number为1 shoppingCart.setNumber(1); //给控的shoppingCart1赋值,保持两种情况返回的都是shoppingCart1 shoppingCart1=shoppingCart; shoppingCartService.save(shoppingCart1); } return new R(1,"添加菜品或套餐成功",shoppingCart1);
数量减少
@PostMapping("/sub") public R sub(@RequestBody ShoppingCart shoppingCart){ //首先获取当前用户id Long currentId = BaseContext.getCurrentId(); LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>(); queryWrapper.eq(ShoppingCart::getUserId,currentId); //判断是dish还是setmeal if (shoppingCart.getDishId()!=null){ queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId()); }else { queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId()); } ShoppingCart one = shoppingCartService.getOne(queryWrapper); int number=one.getNumber(); //如果number>1就减,如果是1就删除数据 if (number>1){ one.setNumber(number-1); shoppingCartService.updateById(one); }else if(number==1){ one.setNumber(0); shoppingCartService.removeById(one); } return new R(1,"减少成功",one); }
清空购物车
@DeleteMapping("clean") public R clean(){ LambdaQueryWrapper<ShoppingCart> queryWrapper =new LambdaQueryWrapper<>(); queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId()); shoppingCartService.remove(queryWrapper); return new R(1,"清空购物车成功"); }
用户下单
前面三个我们之前都已经完成了,只需要完成第四个内容
@Service public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService { @Autowired private ShoppingCartService shoppingCartService; @Autowired private UserService userService; @Autowired private AddressBookService addressBookService; @Autowired private OrderDetailService orderDetailService; @Override public void submit(Orders orders) { //1.查询当前用户id Long currentId = BaseContext.getCurrentId(); //2.查询当前用户购物车信息 LambdaQueryWrapper<ShoppingCart> queryWrapper =new LambdaQueryWrapper<>(); queryWrapper.eq(ShoppingCart::getUserId,currentId); List<ShoppingCart> shoppingCarts = shoppingCartService.list(queryWrapper); if (shoppingCarts==null||shoppingCarts.size()==0){ throw new CustomException("购物车信息为0"); } //查询用户信息 User user = userService.getById(currentId); //查询地址信息 Long addressBookId = orders.getAddressBookId(); AddressBook addressBook = addressBookService.getById(addressBookId); if (addressBook==null){ throw new CustomException("地址信息有误,不能下单"); } //用baomidou生成一个订单id long orderId = IdWorker.getId(); //定义初始为0的总额,原子操作保证线程安全 AtomicInteger amount =new AtomicInteger(0); List<OrderDetail> orderDetails =shoppingCarts.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; }).toList(); //3.向订单表插一条数据 orders.setId(orderId); orders.setOrderTime(LocalDateTime.now()); orders.setCheckoutTime(LocalDateTime.now()); orders.setStatus(2);//2代表待发货 orders.setAmount(new BigDecimal(amount.get()));//总金额 orders.setUserId(currentId); 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); //4.向订单明细表插入多条数据 orderDetailService.saveBatch(orderDetails); //5.清空购物车 shoppingCartService.remove(queryWrapper); }