2024-01-08 16:56:43 +08:00
|
|
|
|
package com.sky.controller.user;
|
|
|
|
|
|
2024-01-09 14:30:20 +08:00
|
|
|
|
import com.alibaba.fastjson.JSON;
|
2024-01-08 16:56:43 +08:00
|
|
|
|
import com.sky.constant.StatusConstant;
|
|
|
|
|
import com.sky.entity.Dish;
|
|
|
|
|
import com.sky.result.Result;
|
|
|
|
|
import com.sky.service.DishService;
|
|
|
|
|
import com.sky.vo.DishVO;
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2024-01-09 14:30:20 +08:00
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
2024-01-08 16:56:43 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
2024-01-09 14:30:20 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
2024-01-08 16:56:43 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
2024-01-09 14:30:20 +08:00
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
2024-01-08 16:56:43 +08:00
|
|
|
|
import java.util.List;
|
2024-01-09 14:30:20 +08:00
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2024-01-08 16:56:43 +08:00
|
|
|
|
|
|
|
|
|
@RestController("userDishController")
|
|
|
|
|
@RequestMapping("/user/dish")
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Api(tags = "C端-菜品浏览接口")
|
|
|
|
|
public class DishController {
|
2024-01-09 14:30:20 +08:00
|
|
|
|
@Resource
|
2024-01-08 16:56:43 +08:00
|
|
|
|
private DishService dishService;
|
2024-01-09 14:30:20 +08:00
|
|
|
|
@Resource
|
|
|
|
|
private RedisTemplate redisTemplate;
|
2024-01-08 16:56:43 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据分类id查询菜品
|
|
|
|
|
*
|
2024-01-09 14:30:20 +08:00
|
|
|
|
* @param categoryId Long
|
|
|
|
|
* @return Result<List < DishVO>>
|
2024-01-08 16:56:43 +08:00
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/list")
|
|
|
|
|
@ApiOperation("根据分类id查询菜品")
|
2024-01-09 14:30:20 +08:00
|
|
|
|
public Result<List<DishVO>> list(@RequestParam Long categoryId) {
|
|
|
|
|
// 设置Redis中key
|
|
|
|
|
String key = "dish_" + categoryId;
|
|
|
|
|
// 查询Redis中数据
|
|
|
|
|
List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
|
|
|
|
|
// 如果不为空返回集合
|
|
|
|
|
if (list != null && !list.isEmpty()) {
|
|
|
|
|
return Result.success(list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果Redis中没有,将值放入
|
2024-01-08 16:56:43 +08:00
|
|
|
|
Dish dish = new Dish();
|
|
|
|
|
dish.setCategoryId(categoryId);
|
2024-01-09 14:30:20 +08:00
|
|
|
|
dish.setStatus(StatusConstant.ENABLE);// 查询起售中的菜品
|
|
|
|
|
// 将这个数据保存到redis中
|
|
|
|
|
list = dishService.listWithFlavor(dish);
|
|
|
|
|
redisTemplate.opsForValue().set(key, JSON.toJSON(list), 7, TimeUnit.DAYS);
|
|
|
|
|
// 如果Redis中没有,返回这个数据并保存这个数据
|
2024-01-08 16:56:43 +08:00
|
|
|
|
return Result.success(list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|