添加购物车业务代码
思路:
1.判断当前购物车商品是否存在了
2.判断本次添加的是菜品表还是套餐表
ShoppingCartControlle
@RestController @RequestMapping("/user/shoppingCart") @Slf4j @Api(tags = "c段购物车接口") public class ShoppingCartControlle { @Autowired private ShoppingCartService shoppingCartService; /** * 添加购物车 * * @param shoppingCartDTO * @return */ @PostMapping("/add") @ApiOperation("添加购物车") public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) { log.info("添加购物车,商品信息为:{}", shoppingCartDTO); shoppingCartService.addShoppingCart(shoppingCartDTO); return Result.success(); } }
ShoppingCartService
public interface ShoppingCartService { /** * 添加购物车方法 * @param shoppingCartDTO */ void addShoppingCart(ShoppingCartDTO shoppingCartDTO); }
ShoppingCartServiceImpl
@Service @Slf4j public class ShoppingCartServiceImpl implements ShoppingCartService { @Autowired private ShoppingCartMapper shoppingCartMapper; @Autowired private DishMapper dishMapper; @Autowired private SetmealMapper setmealMapper; /** * 添加购物车 * @param shoppingCartDTO */ public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) { //判断当前购物车商品是否存在了 ShoppingCart shoppingCart = new ShoppingCart(); BeanUtils.copyProperties(shoppingCartDTO, shoppingCart); //获得用户ID Long userId = BaseContext.getCurrentId(); //添加进去 shoppingCart.setUserId(userId); List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart); //如果存在,那么数量+1就行了 if (list != null && list.size() > 0) { //list集合只会有一种数据 ShoppingCart cart = list.get(0); cart.setNumber(cart.getNumber() + 1); shoppingCartMapper.updateUnmberById(cart); }else { //如果不存在,需要来插入一条购物车数据 //判断本次添加的是菜品表还是套餐表 Long dishId = shoppingCartDTO.getDishId(); if (dishId != null) { //本次添加的是菜品表 Dish dish = dishMapper.getById(dishId); shoppingCart.setName(dish.getName()); shoppingCart.setImage(dish.getImage()); shoppingCart.setAmount(dish.getPrice()); }else { //本次添加的是套餐 Long setmalId = shoppingCartDTO.getSetmealId(); Setmeal setmeal = setmealMapper.getId(setmalId); shoppingCart.setName(setmeal.getName()); shoppingCart.setImage(setmeal.getImage()); shoppingCart.setAmount(setmeal.getPrice()); } shoppingCart.setNumber( 1); shoppingCart.setCreateTime(LocalDateTime.now()); shoppingCartMapper.insert(shoppingCart); } } }
ShoppingCartMapper
@Mapper public interface ShoppingCartMapper { /** * 动态条件查询 * @param shoppingCart * @return */ List<ShoppingCart> list(ShoppingCart shoppingCart); /** * 修改菜品数量 * @param cart */ @Update("update sky_take_out.shopping_cart set number=#{number} where id=#{id} ") void updateUnmberById(ShoppingCart cart); /** * 插入购物车数据 * @param shoppingCart */ @Insert("insert into sky_take_out.shopping_cart(name, user_id, dish_id, setmeal_id, dish_flavor, number, amount, image,create_time) values" + " (#{name},#{userId},#{dishId},#{setmealId},#{dishFlavor},#{number},#{amount},#{image},#{createTime})") void insert(ShoppingCart shoppingCart); }
ShoppingCartMapper
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.sky.mapper.ShoppingCartMapper"> <select id="list" resultType="com.sky.entity.ShoppingCart"> select * from sky_take_out.shopping_cart <where> <if test="userId != null"> and user_id = #{userId} </if> <if test="setmealId !=null"> and setmeal_id =#{setmealId} </if> <if test="dishId !=null"> and dish_id =#{dishId} </if> <if test="dishFlavor!=null"> and dish_flavor =#{dishFlavor} </if> </where> </select> </mapper>