/** * 添加购物车 * * @param shoppingCartDTO */ public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) { ShoppingCart shoppingCart = new ShoppingCart(); BeanUtils.copyProperties(shoppingCartDTO, shoppingCart); //只能查询自己的购物车数据 shoppingCart.setUserId(BaseContext.getCurrentId()); //判断当前商品是否在购物车中 List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart); if (shoppingCartList != null && shoppingCartList.size() == 1) { //如果已经存在,就更新数量,数量加1 shoppingCart = shoppingCartList.get(0); shoppingCart.setNumber(shoppingCart.getNumber() + 1); shoppingCartMapper.updateNumberById(shoppingCart); } else { //如果不存在,插入数据,数量就是1 //判断当前添加到购物车的是菜品还是套餐 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 { //添加到购物车的是套餐 Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId()); shoppingCart.setName(setmeal.getName()); shoppingCart.setImage(setmeal.getImage()); shoppingCart.setAmount(setmeal.getPrice()); } shoppingCart.setNumber(1); shoppingCart.setCreateTime(LocalDateTime.now()); shoppingCartMapper.insert(shoppingCart); } } /** * 从购物车减去 * * @param shoppingCartDTO */ public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) { ShoppingCart shoppingCart = new ShoppingCart(); BeanUtils.copyProperties(shoppingCartDTO, shoppingCart); //只能查询自己的购物车数据 shoppingCart.setUserId(BaseContext.getCurrentId()); //判断当前商品是否在购物车中 List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart); if (shoppingCartList != null && shoppingCartList.size() > 1) { //如果已经存在,就更新数量,数量-1 shoppingCart = shoppingCartList.get(0); shoppingCart.setNumber(shoppingCart.getNumber() - 1); shoppingCartMapper.updateNumberById(shoppingCart); } else { //如果不存在 shoppingCartMapper.delete(shoppingCart); } } /** * 查看购物车 * @return */ public List<ShoppingCart> showShoppingCart() { return shoppingCartMapper.list(ShoppingCart. builder(). userId(BaseContext.getCurrentId()). build()); } /** * 清空购物车商品 */ public void cleanShoppingCart() { shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId()); }
service
/** * 添加购物车 * @param shoppingCartDTO */ void addShoppingCart(ShoppingCartDTO shoppingCartDTO); /** * 从购物车减去 * @param shoppingCartDTO */ void subShoppingCart(ShoppingCartDTO shoppingCartDTO); /** * 查看购物车 * @return */ List<ShoppingCart> showShoppingCart(); /** * 清空购物车商品 */ void cleanShoppingCart();
XML
<?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" parameterType="ShoppingCart" resultType="ShoppingCart"> select * from shopping_cart <where> <if test="userId != null"> and user_id = #{userId} </if> <if test="dishId != null"> and dish_id = #{dishId} </if> <if test="setmealId != null"> and setmeal_id = #{setmealId} </if> <if test="dishFlavor != null"> and dish_flavor = #{dishFlavor} </if> </where> order by create_time desc </select> <insert id="insertBatch" parameterType="list"> insert into shopping_cart (name, image, user_id, dish_id, setmeal_id, dish_flavor, number, amount, create_time) values <foreach collection="shoppingCartList" item="sc" separator=","> (#{sc.name},#{sc.image},#{sc.userId},#{sc.dishId},#{sc.setmealId},#{sc.dishFlavor},#{sc.number},#{sc.amount},#{sc.createTime}) </foreach> </insert> </mapper>
controller
/** * 购物车 */ @RestController @RequestMapping("/user/shoppingCart") @Slf4j @Api(tags = "C端-购物车接口") public class ShoppingCartController { @Autowired private ShoppingCartService shoppingCartService; /** * 添加购物车 * @param shoppingCartDTO * @return */ @PostMapping("/add") @ApiOperation("添加购物车") public Result<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO){ log.info("添加购物车:{}", shoppingCartDTO); shoppingCartService.addShoppingCart(shoppingCartDTO);//后绪步骤实现 return Result.success(); } // 减菜品 /** * 从购物车减去 * @param shoppingCartDTO * @return */ @PostMapping("/sub") @ApiOperation("从购物车减去") public Result<String> sub(@RequestBody ShoppingCartDTO shoppingCartDTO){ log.info("从购物车减去:{}", shoppingCartDTO); shoppingCartService.subShoppingCart(shoppingCartDTO);//后绪步骤实现 return Result.success(); } /** * 查看购物车 * @return */ @GetMapping("/list") @ApiOperation("查看购物车") public Result<List<ShoppingCart>> list(){ return Result.success(shoppingCartService.showShoppingCart()); } /** * 清空购物车商品 * @return */ @DeleteMapping("/clean") @ApiOperation("清空购物车商品") public Result<String> clean(){ shoppingCartService.cleanShoppingCart(); return Result.success(); }
标签:逻辑,return,shoppingCartDTO,购物,购物车,shoppingCart,sc,public,Impl From: https://www.cnblogs.com/lllllzj/p/18251729