购物车相关
This commit is contained in:
parent
66dac4464d
commit
e7f3e1dde9
@ -45,7 +45,7 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
|||||||
.excludePathPatterns("/admin/employee/login");
|
.excludePathPatterns("/admin/employee/login");
|
||||||
|
|
||||||
registry.addInterceptor(jwtTokenUserInterceptor)
|
registry.addInterceptor(jwtTokenUserInterceptor)
|
||||||
.addPathPatterns("user/**")
|
.addPathPatterns("/user/**")
|
||||||
.excludePathPatterns("/user/user/login")
|
.excludePathPatterns("/user/user/login")
|
||||||
.excludePathPatterns("/user/shop/status");
|
.excludePathPatterns("/user/shop/status");
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -41,14 +41,14 @@ public class JwtTokenUserInterceptor implements HandlerInterceptor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//1、从请求头中获取令牌
|
//1、从请求头中获取令牌
|
||||||
String token = request.getHeader(jwtProperties.getAdminTokenName());
|
String token = request.getHeader(jwtProperties.getUserTokenName());
|
||||||
|
|
||||||
//2、校验令牌
|
//2、校验令牌
|
||||||
try {
|
try {
|
||||||
log.info("jwt校验:{}", token);
|
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());
|
Long userId = Long.valueOf(claims.get(JwtClaimsConstant.USER_ID).toString());
|
||||||
log.info("当前员工id:", userId);
|
log.info("当前用户id:", userId);
|
||||||
BaseContext.setCurrentId(userId);
|
BaseContext.setCurrentId(userId);
|
||||||
//3、通过,放行
|
//3、通过,放行
|
||||||
return true;
|
return true;
|
||||||
|
@ -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);
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
sky-server/src/main/resources/mapper/ShoppingCartMapper.xml
Normal file
23
sky-server/src/main/resources/mapper/ShoppingCartMapper.xml
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user