java 瑞吉外卖day6 移动端 套餐 菜品展示 购物车加减,清空
导入用户地址簿相关功能代码
菜品展示
购物车模块
加入购物车:
@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,"清空购物车成功"); }
用户下单