插入购物车数据
This commit is contained in:
parent
d48f9bfe0a
commit
2ffc04318f
|
@ -0,0 +1,37 @@
|
|||
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.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user/shopping")
|
||||
@Slf4j
|
||||
@Api(tags = "C端购物车相关接口")
|
||||
public class ShoppingCartController {
|
||||
@Resource
|
||||
private ShoppingCartService shoppingCartService;
|
||||
|
||||
|
||||
/**
|
||||
* 添加购物车
|
||||
*
|
||||
* @param shoppingCartDTO ShoppingCartDTO
|
||||
* @return Result
|
||||
*/
|
||||
@ApiOperation("添加购物车")
|
||||
@PostMapping("add")
|
||||
public Result<String> add(ShoppingCartDTO shoppingCartDTO) {
|
||||
log.info("添加购物车:{}", shoppingCartDTO);
|
||||
shoppingCartService.addShoppingCart(shoppingCartDTO);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.sky.mapper;
|
||||
|
||||
import com.sky.entity.ShoppingCart;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface ShoppingCartMapper {
|
||||
/**
|
||||
* 添加购物车
|
||||
*
|
||||
* @param shoppingCart ShoppingCart
|
||||
* @return List<ShoppingCart>
|
||||
*/
|
||||
List<ShoppingCart> list(ShoppingCart shoppingCart);
|
||||
|
||||
/**
|
||||
* 如果已经存在,只需要将数量+1即可-修改商品数量
|
||||
*
|
||||
* @param shoppingCart ShoppingCart
|
||||
*/
|
||||
void updateNumberById(ShoppingCart shoppingCart);
|
||||
|
||||
/**
|
||||
* 插入购物车数据
|
||||
*
|
||||
* @param shoppingCart ShoppingCart
|
||||
*/
|
||||
void insert(ShoppingCart shoppingCart);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.ShoppingCartDTO;
|
||||
|
||||
public interface ShoppingCartService {
|
||||
/**
|
||||
* 添加购物车
|
||||
*
|
||||
* @param shoppingCartDTO 购物车参数
|
||||
*/
|
||||
void addShoppingCart(ShoppingCartDTO shoppingCartDTO);
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
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 lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ShoppingCartServiceImplImpl implements ShoppingCartService {
|
||||
@Resource
|
||||
private ShoppingCartMapper shoppingCartMapper;
|
||||
@Resource
|
||||
private DishMapper dishMapper;
|
||||
@Resource
|
||||
private SetmealMapper setmealMapper;
|
||||
|
||||
/**
|
||||
* 添加购物车
|
||||
*
|
||||
* @param shoppingCartDTO 购物车参数
|
||||
*/
|
||||
@Override
|
||||
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.isEmpty()) {
|
||||
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());
|
||||
} else {
|
||||
// 本次添加的是套餐
|
||||
Long setmealId = shoppingCartDTO.getSetmealId();
|
||||
Setmeal 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
<?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">
|
||||
<!-- 插入购物车数据 -->
|
||||
<insert id="insert">
|
||||
insert into shopping_cart (name, image, user_id, dish_id, setmeal_id, dish_flavor, number, amount,
|
||||
create_time)
|
||||
values (#{name}, #{image}, #{userId}, #{dishId}, #{setmealId}, #{dishFlavor}, #{number}, #{amount},
|
||||
#{createTime});
|
||||
</insert>
|
||||
|
||||
<!-- 如果已经存在,只需要将数量+1即可-修改商品数量 -->
|
||||
<update id="updateNumberById">
|
||||
update shopping_cart
|
||||
set number = #{number}
|
||||
where id = #{id};
|
||||
</update>
|
||||
|
||||
<!-- 添加购物车 -->
|
||||
<select id="list" resultType="com.sky.entity.ShoppingCart">
|
||||
select *
|
||||
from shopping_cart
|
||||
<where>
|
||||
<if test="userId != null">
|
||||
and userId = #{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>
|
Loading…
Reference in New Issue