完成商品缓存、购物车功能
This commit is contained in:
parent
25d44499d5
commit
8e5c17c144
@ -3,11 +3,13 @@ package com.sky;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableTransactionManagement //开启注解方式的事务管理
|
||||
@Slf4j
|
||||
@EnableCaching //开启注解方式的缓存管理
|
||||
public class SkyApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SkyApplication.class, args);
|
||||
|
@ -11,6 +11,7 @@ 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.cache.annotation.CacheEvict;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@ -34,6 +35,7 @@ public class DishController {
|
||||
*/
|
||||
@PostMapping
|
||||
@ApiOperation("新增菜品")
|
||||
@CacheEvict(cacheNames = "dishCache", key = "#dishDTO.categoryId")
|
||||
public Result save(@RequestBody DishDTO dishDTO) {
|
||||
log.info("新增菜品: {}", dishDTO);
|
||||
dishService.saveWithFlavor(dishDTO);
|
||||
@ -61,6 +63,7 @@ public class DishController {
|
||||
*/
|
||||
@DeleteMapping
|
||||
@ApiOperation("菜品批量删除")
|
||||
@CacheEvict(cacheNames = "dishCache", allEntries = true)
|
||||
public Result delete(@RequestParam List<Long> ids) {
|
||||
log.info("菜品批量删除:{}", ids);
|
||||
dishService.deleteBatch(ids);
|
||||
@ -87,6 +90,7 @@ public class DishController {
|
||||
*/
|
||||
@PutMapping
|
||||
@ApiOperation("更新菜品")
|
||||
@CacheEvict(cacheNames = "dishCache", allEntries = true)
|
||||
public Result update(@RequestBody DishDTO dishDTO) {
|
||||
log.info("更新菜品:{}", dishDTO);
|
||||
dishService.updateWithFlavor(dishDTO);
|
||||
@ -101,6 +105,7 @@ public class DishController {
|
||||
*/
|
||||
@PostMapping("/status/{status}")
|
||||
@ApiOperation("启用禁用分菜品")
|
||||
@CacheEvict(cacheNames = "dishCache", allEntries = true)
|
||||
public Result<String> startOrStop(@PathVariable("status") Integer status, Long id){
|
||||
dishService.startOrStop(status,id);
|
||||
return Result.success();
|
||||
|
@ -10,6 +10,8 @@ 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.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@ -33,6 +35,7 @@ public class SetmealController {
|
||||
*/
|
||||
@PostMapping
|
||||
@ApiOperation("新增套餐")
|
||||
@CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
|
||||
public Result save(@RequestBody SetmealDTO setmealDTO) {
|
||||
log.info("新增套餐:{}", setmealDTO);
|
||||
setmealService.saveWithDish(setmealDTO);
|
||||
@ -58,6 +61,7 @@ public class SetmealController {
|
||||
*/
|
||||
@DeleteMapping
|
||||
@ApiOperation("批量删除套餐")
|
||||
@CacheEvict(cacheNames = "setmealCache", allEntries = true)
|
||||
public Result delete(@RequestParam List<Long> ids){
|
||||
setmealService.deleteBatch(ids);
|
||||
return Result.success();
|
||||
@ -83,6 +87,7 @@ public class SetmealController {
|
||||
*/
|
||||
@PutMapping
|
||||
@ApiOperation("修改套餐")
|
||||
@CacheEvict(cacheNames = "setmealCache", allEntries = true)
|
||||
public Result update(@RequestBody SetmealDTO setmealDTO) {
|
||||
log.info("修改套餐:{}", setmealDTO);
|
||||
setmealService.update(setmealDTO);
|
||||
@ -97,6 +102,7 @@ public class SetmealController {
|
||||
*/
|
||||
@PostMapping("/status/{status}")
|
||||
@ApiOperation("启用停售套餐")
|
||||
@CacheEvict(cacheNames = "setmealCache", allEntries = true)
|
||||
public Result startOrStop(@PathVariable Integer status, Long id) {
|
||||
log.info("启用停售套餐:status={}, id={}", status, id);
|
||||
setmealService.startOrStop(status, id);
|
||||
|
@ -9,6 +9,7 @@ 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.cache.annotation.Cacheable;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@ -30,6 +31,7 @@ public class DishController {
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
@ApiOperation("根据分类id查询菜品")
|
||||
@Cacheable(cacheNames = "dishCache", key = "#categoryId")
|
||||
public Result<List<DishVO>> list(Long categoryId) {
|
||||
Dish dish = new Dish();
|
||||
dish.setCategoryId(categoryId);
|
||||
|
@ -8,6 +8,7 @@ import com.sky.vo.DishItemVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@ -29,6 +30,7 @@ public class SetmealController {
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
@ApiOperation("根据分类id查询套餐")
|
||||
@Cacheable(cacheNames = "setmealCache", key = "#categoryId") //key:setmealCache::categoryId
|
||||
public Result<List<Setmeal>> list(Long categoryId) {
|
||||
Setmeal setmeal = new Setmeal();
|
||||
setmeal.setCategoryId(categoryId);
|
||||
|
@ -0,0 +1,75 @@
|
||||
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")
|
||||
@Api(tags = "C端口购物车相关接口")
|
||||
@Slf4j
|
||||
public class ShoppingCartController {
|
||||
|
||||
@Autowired
|
||||
private ShoppingCartService shoppingCartService;
|
||||
|
||||
/**
|
||||
* 添加购物车
|
||||
*
|
||||
* @param shoppingCartDTO
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/add")
|
||||
@ApiOperation("添加购物车")
|
||||
public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
|
||||
log.info("添加购物车,商品信息为: {}", shoppingCartDTO);
|
||||
shoppingCartService.addShoppingCart(shoppingCartDTO);
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看购物车列表
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
@ApiOperation("查看购物车列表")
|
||||
public Result<List<ShoppingCart>> list() {
|
||||
log.info("查看购物车列表");
|
||||
List<ShoppingCart> list = shoppingCartService.showShoppingCart();
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空购物车
|
||||
* @return
|
||||
*/
|
||||
@DeleteMapping("/clean")
|
||||
@ApiOperation("清空购物车")
|
||||
public Result clean() {
|
||||
log.info("清空购物车");
|
||||
shoppingCartService.cleanShoppingCart();
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除购物车一个商品
|
||||
* @param shoppingCartDTO
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/sub")
|
||||
@ApiOperation("删除购物车一个商品")
|
||||
public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO) {
|
||||
log.info("删除购物车一个商品,商品信息为: {}", shoppingCartDTO);
|
||||
shoppingCartService.subShoppingCart(shoppingCartDTO);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.sky.entity.ShoppingCart;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
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 {
|
||||
|
||||
/**
|
||||
* 动态条件查询购物车列表
|
||||
* @param shoppingCart
|
||||
* @return
|
||||
*/
|
||||
List<ShoppingCart> list(ShoppingCart shoppingCart);
|
||||
|
||||
/**
|
||||
* 通过id更新商品数量
|
||||
* @param shoppingCart
|
||||
*/
|
||||
@Update("update shopping_cart set number = #{number} where id = #{id}")
|
||||
void updateNumberById(ShoppingCart shoppingCart);
|
||||
|
||||
/**
|
||||
* 新增购物车商品
|
||||
* @param shoppingCart
|
||||
*/
|
||||
@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})")
|
||||
void insert(ShoppingCart shoppingCart);
|
||||
|
||||
/**
|
||||
* 根据用户id清空购物车商品
|
||||
* @param userId
|
||||
*/
|
||||
@Delete("delete from shopping_cart where user_id = #{userId}")
|
||||
void deleteByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 根据id删除购物车商品
|
||||
* @param id
|
||||
*/
|
||||
@Delete("delete from shopping_cart where id = #{id}")
|
||||
void deleteById(Long id);
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.ShoppingCartDTO;
|
||||
import com.sky.entity.ShoppingCart;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ShoppingCartService {
|
||||
|
||||
/**
|
||||
* 添加购物车
|
||||
* @param shoppingCartDTO
|
||||
*/
|
||||
void addShoppingCart(ShoppingCartDTO shoppingCartDTO);
|
||||
|
||||
/**
|
||||
* 查看购物车
|
||||
* @return
|
||||
*/
|
||||
List<ShoppingCart> showShoppingCart();
|
||||
|
||||
/**
|
||||
* 清空购物车
|
||||
* @return
|
||||
*/
|
||||
void cleanShoppingCart();
|
||||
|
||||
/**
|
||||
* 删除购物车一个商品
|
||||
* @param shoppingCartDTO
|
||||
*/
|
||||
void subShoppingCart(ShoppingCartDTO shoppingCartDTO);
|
||||
}
|
@ -61,7 +61,6 @@ public class SetmealServiceImpl implements SetmealService {
|
||||
|
||||
//保存套餐和菜品的关联信息
|
||||
setmealDishMapper.insertBatch(setmealDishes);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,126 @@
|
||||
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.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ShoppingCartServiceImpl implements ShoppingCartService {
|
||||
|
||||
@Autowired
|
||||
private ShoppingCartMapper shoppingCartMapper;
|
||||
|
||||
@Autowired
|
||||
private DishMapper dishMapper;
|
||||
|
||||
@Autowired
|
||||
private 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);
|
||||
|
||||
//如果存在,则数量+1
|
||||
if (list != null && list.size() > 0) {
|
||||
shoppingCart = list.get(0);
|
||||
shoppingCart.setNumber(shoppingCart.getNumber() + 1);
|
||||
shoppingCartMapper.updateNumberById(shoppingCart);
|
||||
} 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示购物车
|
||||
* @return
|
||||
*/
|
||||
public List<ShoppingCart> showShoppingCart() {
|
||||
//获取当前微信用户id
|
||||
Long userId = BaseContext.getCurrentId();
|
||||
ShoppingCart shoppingCart = ShoppingCart.builder()
|
||||
.userId(userId)
|
||||
.build();
|
||||
List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空购物车
|
||||
*/
|
||||
public void cleanShoppingCart() {
|
||||
//获取当前微信用户id
|
||||
Long userId = BaseContext.getCurrentId();
|
||||
shoppingCartMapper.deleteByUserId(userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除购物车一个商品
|
||||
* @param shoppingCartDTO
|
||||
*/
|
||||
public void subShoppingCart(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 = list.get(0);
|
||||
|
||||
Integer number = shoppingCart.getNumber();
|
||||
if (number == 1) {
|
||||
//如果数量为1,则直接删除该条数据
|
||||
shoppingCartMapper.deleteById(shoppingCart.getId());
|
||||
} else {
|
||||
//如果数量大于1,则数量-1
|
||||
shoppingCart.setNumber(number - 1);
|
||||
shoppingCartMapper.updateNumberById(shoppingCart);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
24
sky-server/src/main/resources/mapper/ShoppingCartMapper.xml
Normal file
24
sky-server/src/main/resources/mapper/ShoppingCartMapper.xml
Normal file
@ -0,0 +1,24 @@
|
||||
<?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>
|
Loading…
Reference in New Issue
Block a user