苍穹外卖--添加购物车代码实现
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package com.sky.controller.user;
|
||||
|
||||
import com.sky.dto.ShoppingCartDTO;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.ShoppingCartService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RequestMapping("/user/shoppingCart")
|
||||
@RestController
|
||||
@Slf4j
|
||||
@Api(tags = "C端购物车相关接口")
|
||||
public class ShoppingCartController {
|
||||
|
||||
@Autowired
|
||||
private ShoppingCartService shoppingCartService;
|
||||
|
||||
@PostMapping("/add")
|
||||
@ApiOperation("添加购物车")
|
||||
public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
|
||||
log.info("添加购物车:商品信息:{}",shoppingCartDTO);
|
||||
//调用购物车业务层的添加购物车功能
|
||||
shoppingCartService.addShoppingCart(shoppingCartDTO);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.sky.dto.ShoppingCartDTO;
|
||||
import com.sky.entity.ShoppingCart;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface ShoppingCartMapper {
|
||||
List<ShoppingCart> list(ShoppingCart shoppingCart);
|
||||
|
||||
/*
|
||||
* 修改对应购物车商品的数量
|
||||
* */
|
||||
@Update("update shopping_cart set number = #{number} where id = #{id}")
|
||||
void updateNumberById(ShoppingCart cart);
|
||||
|
||||
@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 cart);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.ShoppingCartDTO;
|
||||
|
||||
public interface ShoppingCartService {
|
||||
/*
|
||||
添加购物车
|
||||
*/
|
||||
void addShoppingCart(ShoppingCartDTO shoppingCartDTO);
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.sky.service.impl;
|
||||
|
||||
import com.sky.context.BaseContext;
|
||||
import com.sky.dto.ShoppingCartDTO;
|
||||
import com.sky.entity.Dish;
|
||||
import com.sky.entity.Setmeal;
|
||||
import com.sky.entity.ShoppingCart;
|
||||
import com.sky.mapper.DishMapper;
|
||||
import com.sky.mapper.SetmealMapper;
|
||||
import com.sky.mapper.ShoppingCartMapper;
|
||||
import com.sky.service.ShoppingCartService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class ShoppingCartServiceImpl implements ShoppingCartService {
|
||||
|
||||
@Autowired
|
||||
private ShoppingCartMapper shoppingCartMapper;
|
||||
|
||||
@Autowired
|
||||
private SetmealMapper setmealMapper;
|
||||
|
||||
@Autowired
|
||||
private DishMapper dishMapper;
|
||||
|
||||
/*
|
||||
添加购物车
|
||||
*/
|
||||
@Override
|
||||
public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
|
||||
//封装一个用来查询购物车的实体类对象
|
||||
ShoppingCart shoppingCart = new ShoppingCart();
|
||||
BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
|
||||
//设置用户id进行区分
|
||||
shoppingCart.setUserId(BaseContext.getCurrentId());
|
||||
|
||||
//1.判断当前用户的购物车中,是否有已经存在该商品(有可能是菜品或者套餐)(使用一条动态sql查询即可)
|
||||
List<ShoppingCart> cartList = shoppingCartMapper.list(shoppingCart);
|
||||
|
||||
//2. 如果已经存在,则数量+1(update)
|
||||
ShoppingCart cart = null;
|
||||
if (cartList != null && cartList.size() > 0) {
|
||||
cart = cartList.get(0);
|
||||
cart.setNumber(cart.getNumber()+1);
|
||||
shoppingCartMapper.updateNumberById(cart);
|
||||
}else{
|
||||
|
||||
//3.如果不存在,则向购物车表中插入一条数据(insert)
|
||||
//判断当前添加的是菜品还是套餐
|
||||
if (shoppingCartDTO.getSetmealId() != null) {
|
||||
//添加的是套餐
|
||||
Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
|
||||
//封装一个保存套餐的购物车对象
|
||||
cart = ShoppingCart.builder()
|
||||
.name(setmeal.getName())
|
||||
.image(setmeal.getImage())
|
||||
// .userId(BaseContext.getCurrentId())
|
||||
.setmealId(shoppingCartDTO.getSetmealId())
|
||||
// .number(1)
|
||||
.amount(setmeal.getPrice())
|
||||
// .createTime(LocalDateTime.now())
|
||||
.build();
|
||||
// shoppingCartMapper.insert(setmealCart);
|
||||
}else{
|
||||
//添加的是菜品
|
||||
|
||||
Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
|
||||
//封装一个保存套餐的购物车对象
|
||||
cart = ShoppingCart.builder()
|
||||
.name(dish.getName())
|
||||
.image(dish.getImage())
|
||||
// .userId(BaseContext.getCurrentId())
|
||||
.dishId(shoppingCartDTO.getDishId())
|
||||
.dishFlavor(shoppingCartDTO.getDishFlavor())
|
||||
// .number(1)
|
||||
.amount(dish.getPrice())
|
||||
// .createTime(LocalDateTime.now())
|
||||
.build();
|
||||
// shoppingCartMapper.insert(dishCart);
|
||||
}
|
||||
//对不管是菜品还是套餐的相同操作,统一处理
|
||||
cart.setNumber(1);
|
||||
cart.setUserId(BaseContext.getCurrentId());
|
||||
cart.setCreateTime(LocalDateTime.now());
|
||||
shoppingCartMapper.insert(cart);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
21
sky-server/src/main/resources/mapper/ShoppingCartMapper.xml
Normal file
21
sky-server/src/main/resources/mapper/ShoppingCartMapper.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?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="dishId != null">
|
||||
and dish_id = #{dishId}
|
||||
</if>
|
||||
<if test="dishFlavor != null">
|
||||
and dish_flavor = #{dishFlavor}
|
||||
</if>
|
||||
<if test="setmealId != null">
|
||||
and setmeal_id = #{setmealId}
|
||||
</if>
|
||||
<if test="userId != null">
|
||||
and user_id = #{userId}
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user