package com.xxl.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxl.reggie.common.R;
import com.xxl.reggie.dto.DishDto;
import com.xxl.reggie.entity.Category;
import com.xxl.reggie.entity.Dish;
import com.xxl.reggie.entity.DishFlavor;
import com.xxl.reggie.service.CategoryService;
import com.xxl.reggie.service.DishFlavorService;
import com.xxl.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
@Autowired
private CategoryService categoryService;
@Autowired
private DishService dishService;
@Autowired
private DishFlavorService dishFlavorService;
@Autowired
private RedisTemplate redisTemplate;
/**
* 新增菜品
* @param dto
* @return
*/
@PostMapping
public R<String> save(@RequestBody DishDto dto){
// log.info("dto:{}",dto.toString());
dishService.saveWithFlavor(dto);
// 清理某个分类下的菜品缓存数据
String key = "dish_"+dto.getCategoryId()+"_1";
redisTemplate.delete(key);
return R.success("新增菜品成功");
}
@GetMapping("/page")
public R<Page> page(int page,int pageSize,String name){
// 构造分页构造器对象
Page<Dish> pageInfo = new Page<>(page,pageSize);
Page<DishDto> dishDtoPage = new Page<>();
// 条件构造器
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
// 添加过滤条件
queryWrapper.like(name!=null,Dish::getName,name);
// 添加排序条件
queryWrapper.orderByDesc(Dish::getUpdateTime);
// 执行分页查询
dishService.page(pageInfo,queryWrapper);
// 对象拷贝
BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
List<Dish> records = pageInfo.getRecords();
List<DishDto> list = records.stream().map((item)->{
DishDto dto = new DishDto();
BeanUtils.copyProperties(item,dto);
Long categoryId = item.getCategoryId();
// 根据id查询分类对象
Category category = categoryService.getById(categoryId);
if (category!=null){
String categoryName = category.getName();
dto.setCategoryName(categoryName);
}
return dto;
}).collect(Collectors.toList());
dishDtoPage.setRecords(list);
return R.success(dishDtoPage);
}
/**
* 根据id查询菜品信息和对应的口味信息
* @param id
* @return
*/
@GetMapping("/{id}")
public R<DishDto> get(@PathVariable Long id){
//
DishDto dishDto= dishService.getByIdWithFlavor(id);
return R.success(dishDto);
}
/**
* 修改菜品
* @param dto
* @return
*/
@PutMapping
public R<String> update(@RequestBody DishDto dto){
// log.info(dto.toString());
dishService.updateWithFlavor(dto);
// 清理所有菜品的缓存
// Set key = redisTemplate.keys("dish_*");
// redisTemplate.delete(key);
// 清理某个分类下的菜品缓存数据
String key = "dish_"+dto.getCategoryId()+"_1";
redisTemplate.delete(key);
return R.success("菜品修改成功");
}
/**
* 修改启停售状态
* @param ids
* @return
*/
@PostMapping("/status/{status}")
public R<String> updateStatusDish(@PathVariable Integer status,@RequestParam List<Long> ids){
// log.info(ids.toString());
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<Dish>();
queryWrapper.in(ids!=null,Dish::getId,ids);
List<Dish> list = dishService.list(queryWrapper);
for (Dish dish:list
) {
if (dish!=null){
dish.setStatus(status);
dishService.updateById(dish);
}
}
return R.success("状态修改成功");
}
/**
* 删除菜品
* @param ids
* @return
*/
@DeleteMapping
public R<String> deleteDish(@RequestParam List<Long> ids){
dishService.deleteWithFlavor(ids);
return R.success("删除成功");
}
// 查询菜品
@GetMapping("/list")
public R<List<DishDto>> getDishList(Dish dish){
List<DishDto> listDto =null;
// 动态设置缓存的key
String key = "dish_"+dish.getCategoryId()+"_"+dish.getStatus();
// 先从缓存中获取数据
listDto = (List<DishDto>) redisTemplate.opsForValue().get(key);
// 如果存在,则直接返回,无需拆线呢数据库
if (listDto!=null){
return R.success(listDto);
}
// 构造查询条件
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId()).or().like(dish.getName()!=null,Dish::getName,dish.getName());
// 添加条件,查询状态为1的在售的菜品
queryWrapper.eq(Dish::getStatus,1);
// 添加排序条件
queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
List<Dish> list = dishService.list(queryWrapper);
listDto = list.stream().map((item)->{
DishDto dto = new DishDto();
BeanUtils.copyProperties(item,dto);
Long categoryId = item.getCategoryId();
// 根据id查询分类对象
Category category = categoryService.getById(categoryId);
if (category!=null){
String categoryName = category.getName();
dto.setCategoryName(categoryName);
}
// 获取菜品id
Long dishId = item.getId();
LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(dishId!=null,DishFlavor::getDishId,dishId);
// lambdaQueryWrapper.eq(DishFlavor::get,1);
List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);
dto.setFlavors(dishFlavorList);
return dto;
}).collect(Collectors.toList());
// 如果不存在,则需要查询数据库,将查询到的菜品数据添加到redis
redisTemplate.opsForValue().set(key,listDto,60, TimeUnit.MINUTES);
return R.success(listDto);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
我的瑞吉项目基于springboot-vue (831个子文件)
v1.0 671B
v1.0 576B
v1.0 41B
v1.0 41B
005804f2539f2a23ce5b88f95739b031b89729 3KB
00737ce70aa9f76140a5a3603c0474d94c08b4 246B
00d1d8348e53ca64e3c84b10f7c93751b6701f 173B
00e13370e7af8019e5307b93c5ab38f159c17c 3KB
00ffab7828f4818589659c804ec2cfd99baed3 170B
011404dda7c5d39b61bfb89dc175b97422ab05 175B
017be3dc5ee70806decfd2e707e787469cdef8 2KB
019aa64c330805f6c4a5364eca144aafc44cb1 491B
0227e47c6728e15743ce2aaf435a009b446b5a 47B
03c1602b215d46bb0d98ce1f52ca55af05d839 64B
03eeba3a48c57b9877cac15faf62cecb1ef2b8 2KB
0417f1a261e89739f37fd9eb63c4d350811667 1KB
04b60d12eb653d0ef308e3c58d99861c3c8a9b 356B
059acc7ba8395c2ef4135e4e05205b08a84092 3KB
05ac4bb42da0d51fab0cb6c62de5ebf0e19613 4KB
05da66fe26ddd6ae8bb39fab717a0d2568dcb5 174B
06f83891a2696fc87b95d07683ddef7207bbcc 2KB
06fd9f3d4c117cd8ac6b11862edf9a82a579d6 239B
077a01e6135df91da832b89f69098c26a614d9 509B
07923f6abf5afaf1a9faebc4a4b390977e7331 2KB
0795b94ebd1aa1b5d1329304299e633d6e319b 254B
079e28cc1513907e947b1e6139be0b2e02a046 357B
0885513a61e805cbdf7e0c071690566b0437f6 175B
096e4420df088f449714594d443415cd40195b 57KB
0a047163531e8ec4f48d44e16b44f393a83ffd 54B
0a2c455b847d997902d4dcb0a19dc5d0adab3a 140B
0a3896852a574ddc6a75a828b100e1ddf1d047 335B
0b08ea90fdbcb96e765998147e02da4cdc9c66 3KB
0bde1d9b22aed6b6aea442fb419f64bd621bc6 112B
0d1296847be4917236bc3299684e6bc5a8234d 2KB
0d16a64d6f659001dd39eb1ead2865da219fd2 180B
0df1600efeff5dedb2915b133cc1261d444f5a 150B
0ea6902842c5b07f9a7ce9936bb1b659be40e9 6KB
0f98730aab7b57e7a050db14134325d6173fda 4KB
v1.1 333B
v1.1 144B
v1.1 41B
v1.1 41B
103634db054434ef592a3d4221445969c9f18a 15KB
104114816c4ac7b242d5e91d263dc0973dd5af 2KB
119254976926420e44071bbf08d6c3df381c9e 4KB
1321c5d601f9666ad35ce309cada5c44514896 445B
137c6543466210ce6797afa76bc07d40d5eb5a 85B
1417306f8fc570ecfbfd1a9b2e6c04cdffe1ec 322B
1510b9e0d8dd5f701a286974f240ce80a6caaf 370B
16337c5375e7534d889e3ad7a379c1b7187028 1KB
16c71f6b69b3e5aa6140a9e933b3d891cc4d3d 2KB
1744f714f97e8705f556548e639594e0b5b72d 90B
17a0c6d94b9bda5cf46840e2f57d9a80c10cc7 820B
1898dcb7d0f8511fc7aab30da4fd58d35e0590 49B
1a22deae0f9e5ec03bed39592324816df533c4 89B
1af43e681c0d6b978dce5c442a4384dbf053cd 83B
1b5e113f9d12919003a14f3ae2aab96b4bceca 30KB
1bd5fc2d1a9d4338bcb511b22827c0f63e9138 194B
1c1b0dc139a9848f7f1b3ba3b6db63d0d32f7f 2KB
1cd4fb3cf5d2cf59e7310decde30d8819e6505 79B
1d7ea7411ccd00cff0e818b345fb9fca2f8e68 215B
1d7f5c02640596b7dccb893545b7c493fbc1bf 395B
1eba2c852cea9f7c8f29e14cedbb3c70ba1d29 179B
1f00d34be4b8522088feef98cc8f57768ba76e 46B
1f4d0825879ac88cf52fded7de8f4dfdcbd2a8 2KB
1fa4b7f6fbe0a32cd4b07f6a42d417f97d2e84 1KB
2067c7df607466c51727da3fe535ab1d5ab898 681B
20a7f9338b1c3c586f10dcfa15540c897dc585 833B
21151e6db15d8c10bbb7dbc7a0069104618afe 82B
2136758b317aa21b1ca9b68e262281c9af695f 8KB
2184a9edc10fdc4b382617d225418ec4f98b8a 261B
21fd7a9ddf5e792cfb8cdbea6f66213d600095 232B
22a8fc50180709d5fffc951c2d9f65b495c649 2KB
2418b1e9d9dc69882a06a12bc8c8383d12eeff 3KB
245f6773128d394e950ba4d689181d4aec1d0c 43B
248d17315771f9322076fe154cf24a4ed84f9b 944B
249f368c6cbb6de56a6b093bc689324f4b5171 55B
25a0b90508626a1101084843e7233fb4a32b3d 66KB
27ab41d5fbd01298000d6e80038caa97eff892 111B
281d09013e0a2c5f8e699a0a6038d9480291e5 5KB
28ba28c661916e296439192a113dc62704d4ca 2KB
2944a8c4ad5d3fd8564ffd94699e5d53c851da 176B
29c8e24cee5b0b4271d43a478e4c9b3da9e560 1KB
2a89ef55065d8eb27845dc7162253f5939b4b5 216B
2a9aa7fbe3bd7fef3647b2d15265b008ec2d5e 150B
2ab9d985c20018a0c97b93d2148ac1ffe588a5 300B
2b851ed0b461ca296f943a066b5608e041c18e 52KB
2d2c28ee0c7ec4aa8cb4e19ed827431950fc28 386B
2d52c41841a8dda20a1e5ab9ba26e32fec953d 314B
2e68e87cb6e13104c45c64eebd0f14eea66ad5 15KB
2f21134a79aff007354b9193a01783d4948170 413B
2fbea9c9e654d6e4d9bc8bb4ef4a8736ff7fa5 1003B
300a86ce8d66b5fb7fbf5bd42013f44158ee3c 3KB
307fce17e3be10b78f6861d6edff437931baab 762B
3103f88f9b39ab90ea97922bd835f3daf9c98a 2KB
31e037d25c8eb85f0c73fd966624267be93da5 5KB
3394eddf72ac48af9bd717b483fb4e1107446c 540B
33c4411fa387a57431ff181fa1fa4c336b0dc2 566B
3428f5c5852d3ff5e956f7e5b41e2cc834f3ad 3KB
34670861ff258c6a3cb11126a6673481dc1b9d 255B
共 831 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
我是一只小小小小龙
- 粉丝: 76
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 小区监控视频监控方案.doc
- 某小区视频监控系统设计方案.doc
- 数电期末练习题.doc
- 数电期末试题.doc
- 数电习题试卷.doc
- 进程调度模拟算法.doc
- 操作系统模拟进程调度算法.doc
- C语言程序设计期末考试试题含答案.doc
- 数电期末试卷及答案.doc
- 汇编实验课程综合设计.doc
- 汇编实验子程序程序设计.doc
- 汇编实验算数运算程序设计.docx
- 多元统计分析重点.docx
- 基于卷积-长短期记忆网络加注意力机制(CNN-LSTM-Attention)的时间序列预测程序,预测精度很高 可用于做风电功率预测,电力负荷预测等等 标记注释清楚,可直接数据运行 代码实现训练与测
- C++语言程序设计期末考试试题及答案.doc
- Linux期末考试复习试题含答案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功