sky-take-out/sky-server/src/main/java/com/sky/controller/user/DishController.java

62 lines
2.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.sky.controller.user;
import com.alibaba.fastjson.JSON;
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;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
@RestController("userDishController")
@RequestMapping("/user/dish")
@Slf4j
@Api(tags = "C端-菜品浏览接口")
public class DishController {
@Resource
private DishService dishService;
@Resource
private RedisTemplate redisTemplate;
/**
* 根据分类id查询菜品
*
* @param categoryId Long
* @return Result<List < DishVO>>
*/
@GetMapping("/list")
@ApiOperation("根据分类id查询菜品")
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中没有将值放入
Dish dish = new Dish();
dish.setCategoryId(categoryId);
dish.setStatus(StatusConstant.ENABLE);// 查询起售中的菜品
// 将这个数据保存到redis中
list = dishService.listWithFlavor(dish);
redisTemplate.opsForValue().set(key, JSON.toJSON(list), 7, TimeUnit.DAYS);
// 如果Redis中没有返回这个数据并保存这个数据
return Result.success(list);
}
}