package com.itheima.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.itheima.common.BaseContext; import com.itheima.common.R; import com.itheima.entity.ShoppingCart; import com.itheima.service.ShoppingCartService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.web.bind.annotation.*; import javax.xml.ws.soap.Addressing; import java.time.LocalDateTime; import java.util.List; @Slf4j @RestController @RequestMapping("/shoppingCart") public class ShoppingCartController { @Autowired private ShoppingCartService shoppingCartService; /** * 添加购物车 * @param shoppingCart * @return */ @PostMapping("/add") public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){ log.info("购物车数据:{}", shoppingCart); //设置用户id,指定当前是哪个用户的购物车数据 long currentId = BaseContext.getCurrentId(); shoppingCart.setUserId(currentId); //查询当前要添加菜品或者套餐是否已经存在购物车 Long dishId = shoppingCart.getDishId(); LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ShoppingCart::getUserId, currentId); if(dishId!=null){ //添加到购物车的是菜品 queryWrapper.eq(ShoppingCart::getDishId, dishId); }else{ //添加到购物车的是套餐 queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId()); } //查询当前菜品或套餐是否在购物车中 ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper); if(cartServiceOne!=null){ //如果已经存在,就在原来数量基础上加一 Integer number = cartServiceOne.getNumber(); cartServiceOne.setNumber(number+1); shoppingCartService.updateById(cartServiceOne); }else{ //如果不存在,则添加到购物车,数量默认就是一 shoppingCart.setNumber(1); shoppingCart.setCreateTime(LocalDateTime.now()); shoppingCartService.save(shoppingCart); cartServiceOne = shoppingCart; } return R.success(cartServiceOne); } /** * 将菜品或套餐数量减一 * @param shoppingCart * @return */ @PostMapping("/sub") public R<String> sub(@RequestBody ShoppingCart shoppingCart){ Long dishId = shoppingCart.getDishId(); LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId()); if(dishId!=null){ //找到给菜品 queryWrapper.eq(ShoppingCart::getDishId, dishId); ShoppingCart dish = shoppingCartService.getOne(queryWrapper); if(dish.getNumber()==1){ //数量为1时则删除该菜品 shoppingCartService.remove(queryWrapper); }else{ //否则将该菜品数量减一 dish.setNumber(dish.getNumber()-1); shoppingCartService.updateById(dish); } }else{ //找到套餐 queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId()); ShoppingCart setmeal = shoppingCartService.getOne(queryWrapper); if(setmeal.getNumber()==1){ //数量为1时则删除该套餐 shoppingCartService.remove(queryWrapper); }else{ setmeal.setNumber(setmeal.getNumber()-1); shoppingCartService.updateById(setmeal); } } return R.success("修改成功"); } /** * 查看购物车 * @return */ @GetMapping("/list") public R<List<ShoppingCart>> list(){ long currentId = BaseContext.getCurrentId(); LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ShoppingCart::getUserId, currentId); queryWrapper.orderByAsc(ShoppingCart::getCreateTime); List<ShoppingCart> list = shoppingCartService.list(queryWrapper); return R.success(list); } /** * 清空购物车 * @return */ @DeleteMapping("/clean") public R<String> clean(){ LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId()); shoppingCartService.remove(queryWrapper); return R.success("清空购物车成功"); } }
标签:queryWrapper,shoppingCartService,shoppingCart,购物车,ShoppingCart,添加,菜品,import From: https://www.cnblogs.com/fxzm/p/17222433.html