苍穹外卖-购物车部分
1.创建shopping_cart表
2.添加到购物车
ShoppingCartController.java
@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端-购物车接口")
public class ShoppingCartController {@Autowiredprivate ShoppingCartService shoppingCartService;/*** 添加购物车* @param shoppingCartDTO* @return*/@PostMapping("/add")@ApiOperation("添加购物车")public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {log.info("添加购物车:",shoppingCartDTO);shoppingCartService.addShoppingcart(shoppingCartDTO);return Result.success();}
Service.java
@Service
public interface ShoppingCartService {/*** 添加购物车* @param shoppingCartDTO*/void addShoppingcart(ShoppingCartDTO shoppingCartDTO);
方法概述
addShoppingcart
方法的主要逻辑如下:
-
检查购物车中是否已存在该商品:
-
如果存在,增加商品的数量。
-
如果不存在,创建一个新的购物车项并插入到数据库中。
-
-
处理菜品和套餐:
-
如果是菜品,从
Dish
表中获取相关信息。 -
如果是套餐,从
Setmeal
表中获取相关信息。
-
-
更新或插入购物车项:
-
如果购物车项已存在,更新其数量。
-
如果购物车项不存在,插入新的购物车项。
-
ServiceImpl.java
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {@Autowiredprivate ShoppingCartMapper shoppingCartMapper;@Autowiredprivate DishMapper dishMapper;@Autowiredprivate SetmealMapper setmealMapper;/*** 添加购物车* @param shoppingCartDTO*/public void addShoppingcart(ShoppingCartDTO shoppingCartDTO) {//判断是否存在ShoppingCart shoppingCart = new ShoppingCart();BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);Long userId = BaseContext.getCurrentId();shoppingCart.setUserId(userId);List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);//如果已经存在if(list != null && list.size() >0){ShoppingCart cart = list.get(0);cart.setNumber(cart.getNumber()+1);shoppingCartMapper.updateNumberById(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());shoppingCart.setNumber(1);shoppingCart.setCreateTime(LocalDateTime.now());}else{//是套餐Long setmealId = shoppingCartDTO.getSetmealId();SetmealVO setmeal = setmealMapper.getById(setmealId);shoppingCart.setName(setmeal.getName());shoppingCart.setImage(setmeal.getImage());shoppingCart.setAmount(setmeal.getPrice());}shoppingCart.setNumber(1);shoppingCart.setCreateTime(LocalDateTime.now());shoppingCartMapper.insert(shoppingCart);}}
-
BeanUtils.copyProperties
:将ShoppingCartDTO
的属性复制到ShoppingCart
对象中。 -
BaseContext.getCurrentId
:获取当前用户的 ID,并设置到ShoppingCart
对象中。 -
shoppingCartMapper.list
:查询购物车中是否存在该商品。 -
list.get(0)
:获取第一个匹配的购物车项。 -
cart.setNumber(cart.getNumber() + 1)
:将数量加 1。 -
shoppingCartMapper.updateNumberById
:调用 Mapper 方法更新数据库中的数量。 -
dishMapper.getById
:根据dishId
查询菜品信息。 -
setmealMapper.getById
:根据setmealId
查询套餐信息。 -
shoppingCart.setNumber(1)
:设置数量为 1。 -
shoppingCart.setCreateTime(LocalDateTime.now())
:设置创建时间为当前时间。 -
shoppingCartMapper.insert
:将新的购物车项插入到数据库中。
Mapper.java
@Mapper
public interface ShoppingCartMapper {//插入购物车@Insert("insert into 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);
Mapper.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" resultType="com.sky.entity.ShoppingCart">select * from 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>
效果如图
3.购物车余下业务逻辑(减去,清空,查看):
Controller
/*** 购物车减去商品*/@PostMapping("/sub")@ApiOperation("购物车减去商品")public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){log.info("购物车减去商品:",shoppingCartDTO);shoppingCartService.sub(shoppingCartDTO);return Result.success();}/*** 查看购物车* @return*/@GetMapping("/list")@ApiOperation("查看购物车")public Result<List<ShoppingCart>> list(){List<ShoppingCart> list = shoppingCartService.showShoppingCart();return Result.success(list);}/*** 清空购物车*/@DeleteMapping("/clean")@ApiOperation("清空购物车")public Result clean(){shoppingCartService.clean();return Result.success();}
Service
/*** 显示购物车*/List<ShoppingCart> showShoppingCart();/*** 清空购物车*/void clean();/*** 减去购物车* @param shoppingCartDTO*/void sub(ShoppingCartDTO shoppingCartDTO);
}
Impl
@Overridepublic void sub(ShoppingCartDTO shoppingCartDTO) {Long dishId = shoppingCartDTO.getDishId();ShoppingCart shoppingCart = new ShoppingCart();if(dishId != null){List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);ShoppingCart cart = list.get(0);cart.setNumber(cart.getNumber()-1);if(cart.getNumber() == 0){Long userId = BaseContext.getCurrentId();shoppingCartMapper.delete(userId);}shoppingCartMapper.updateNumberById(cart);}else{//套餐Long setmealId = shoppingCartDTO.getSetmealId();List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);ShoppingCart cart = list.get(0);cart.setNumber(cart.getNumber()-1);if(cart.getNumber() == 0){Long userId = BaseContext.getCurrentId();shoppingCartMapper.delete(userId);}shoppingCartMapper.updateNumberById(cart);}}@Overridepublic List<ShoppingCart> showShoppingCart() {Long userId = BaseContext.getCurrentId();ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);return list;}@Overridepublic void clean() {Long userId = BaseContext.getCurrentId();shoppingCartMapper.delete(userId);}
Mapper
@Mapper
public interface ShoppingCartMapper {//动态条件查询List<ShoppingCart> list(ShoppingCart shoppingCart);//根据id修改商品数量@Update("update shopping_cart set number = #{number} where id = #{id}")void updateNumberById(ShoppingCart shoppingCart);//插入购物车@Insert("insert into 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);//清空购物车@Delete("delete from shopping_cart where user_id = #{userId}")void delete(Long userId);//减去购物车@Update("update shopping_cart set number = #{number} where id = #{id}")void sub(Long dishId);
}
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" resultType="com.sky.entity.ShoppingCart">select * from 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>