购物车相关

This commit is contained in:
zhangsan 2024-04-19 13:45:29 +08:00
parent 66dac4464d
commit e7f3e1dde9
7 changed files with 214 additions and 4 deletions

View File

@ -45,7 +45,7 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
.excludePathPatterns("/admin/employee/login");
registry.addInterceptor(jwtTokenUserInterceptor)
.addPathPatterns("user/**")
.addPathPatterns("/user/**")
.excludePathPatterns("/user/user/login")
.excludePathPatterns("/user/shop/status");
}

View File

@ -0,0 +1,52 @@
package com.sky.controller.user;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
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.*;
import java.util.List;
@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端-购物车相关接口")
public class ShoppingCartController {
@Autowired
private ShoppingCartService shoppingCartService;
@PostMapping("/add")
@ApiOperation("添加购物车")
public Result addCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
log.info("添加购物车:{}", shoppingCartDTO);
shoppingCartService.addCart(shoppingCartDTO);
return Result.success();
}
@GetMapping("/list")
@ApiOperation("查看购物车")
public Result<List<ShoppingCart>> list(){
return Result.success(shoppingCartService.showShoppingCart());
}
/**
* 清空购物车商品
* @return
*/
@DeleteMapping("/clean")
@ApiOperation("清空购物车商品")
public Result<String> clean(){
log.info("清空购物车");
shoppingCartService.cleanShoppingCart();
return Result.success();
}
@PostMapping("/sub")
@ApiOperation("减少购物车商品")
public Result subCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
log.info("减少购物车:{}",shoppingCartDTO);
shoppingCartService.subCart(shoppingCartDTO);
return Result.success();
}
}

View File

@ -41,14 +41,14 @@ public class JwtTokenUserInterceptor implements HandlerInterceptor {
}
//1从请求头中获取令牌
String token = request.getHeader(jwtProperties.getAdminTokenName());
String token = request.getHeader(jwtProperties.getUserTokenName());
//2校验令牌
try {
log.info("jwt校验:{}", token);
Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
Long userId = Long.valueOf(claims.get(JwtClaimsConstant.USER_ID).toString());
log.info("当前员工id", userId);
log.info("当前用户id", userId);
BaseContext.setCurrentId(userId);
//3通过放行
return true;

View File

@ -0,0 +1,21 @@
package com.sky.mapper;
import com.sky.entity.ShoppingCart;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface ShoppingCartMapper {
List<ShoppingCart> list(ShoppingCart shoppingCart);
void updateNumberById(ShoppingCart shoppingCart);
void insert(ShoppingCart shoppingCart);
@Delete("delete from shopping_cart where user_id = #{userId}")
void deleteByUserId(Long currentId);
@Delete("delete from shopping_cart where id = #{id}")
void deleteById(Long id);
}

View File

@ -0,0 +1,16 @@
package com.sky.service;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import java.util.List;
public interface ShoppingCartService {
void addCart(ShoppingCartDTO shoppingCartDTO);
List<ShoppingCart> showShoppingCart();
void cleanShoppingCart();
void subCart(ShoppingCartDTO shoppingCartDTO);
}

View File

@ -0,0 +1,98 @@
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 DishMapper dishMapper;
@Autowired
private SetmealMapper setmealMapper;
@Override
public void addCart(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.queryById(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);
}
}
@Override
/**
* 查看购物车
* @return
*/
public List<ShoppingCart> showShoppingCart() {
return shoppingCartMapper.list(ShoppingCart.
builder().
userId(BaseContext.getCurrentId()).
build());
}
@Override
public void cleanShoppingCart() {
shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
}
@Override
public void subCart(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) {
//如果已经存在就更新数量
shoppingCart = shoppingCartList.get(0);
if(shoppingCart.getNumber() -1==0)
//删除某一项
shoppingCartMapper.deleteById(shoppingCart.getId());
else {
shoppingCart.setNumber(shoppingCart.getNumber() - 1);
shoppingCartMapper.updateNumberById(shoppingCart);
}
}
}
}

View File

@ -0,0 +1,23 @@
<?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, user_id, dish_id, setmeal_id, dish_flavor, number, amount, image, create_time)
values (#{name},#{userId},#{dishId},#{setmealId},#{dishFlavor},#{number},#{amount},#{image},#{createTime})
</insert>
<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>
user_id=#{userId}
<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>
</mapper>