• 瑞吉外卖09-菜品模块的CRUD与启售、停售


    需求分析

    我们先看看这个页面,剥离出本质其实就是CRUD! 

    这里面还有一个 “启售” 和 “停售”,在黑马视频中是没有的,我们这里也将其一起来实现一下!

    这里我们基本上在前面都有讲过一些,所以这里就不做过多的分析了!

    请求参数

    菜品分页查询

    请求说明
    请求方式GET
    请求路径/dish/page
    请求参数?page=1&pageSize=10&name=xxx

    新增菜品

    请求说明
    请求方式POST
    请求路径/dish
    请求参数json格式

    删除菜品

    菜品修改

    根据ID查询菜品及菜品口味信息

    请求说明
    请求方式GET
    请求路径/dish/{id}

    修改菜品及菜品口味信息  

    请求说明
    请求方式PUT
    请求路径/dish
    请求参数json格式数据

    起售停售

    代码实现 

    Controller层

    1. @RestController
    2. @Slf4j
    3. @RequestMapping("/dish")
    4. public class DishController {
    5. @Autowired
    6. private DishService dishService;
    7. /**
    8. * 新增菜品
    9. * @param dishDto
    10. * @return
    11. */
    12. @PostMapping
    13. public R saveDishWithFlavor(@RequestBody DishDto dishDto) {
    14. return dishService.saveDishWithFlavor(dishDto);
    15. }
    16. /**
    17. * 菜品管理-分页查询
    18. * @param page
    19. * @param pageSize
    20. * @param name
    21. * @return
    22. */
    23. @GetMapping("/page")
    24. public R pageByDish(int page,int pageSize,String name) {
    25. return dishService.pageByDish(page, pageSize,name);
    26. }
    27. /**
    28. * 修改菜品
    29. * @param dishDto
    30. * @return
    31. */
    32. @PutMapping
    33. public R updateDishWithFlavor(@RequestBody DishDto dishDto) {
    34. return dishService.updateDishWithFlavor(dishDto);
    35. }
    36. /**
    37. * 根据id查询菜品信息与对应的口味信息
    38. * @param id
    39. * @return
    40. */
    41. @GetMapping("/{id}")
    42. public R getDishInfoById(@PathVariable Long id) {
    43. return dishService.getDishInfoById(id);
    44. }
    45. /**
    46. * 删除菜品
    47. * @param ids
    48. * @return
    49. *
    50. * @@RequestParam: 用于将请求参数区数据映射到功能处理方法的参数上
    51. */
    52. @DeleteMapping
    53. public R deleteDish(@RequestParam List ids) {
    54. log.info("ids:{}",ids);
    55. return dishService.deleteDish(ids);
    56. }
    57. /**
    58. * 起售菜品、停售菜品
    59. * @param status
    60. * @param ids
    61. * @return
    62. *
    63. * http://localhost:8080/dish/status/0?ids=1413384757047271425
    64. */
    65. @PostMapping("/status/{status}")
    66. public R sellStatus(@PathVariable("status") Integer status, @RequestParam List ids) {
    67. return dishService.sellStatus(status, ids);
    68. }
    69. }

    Service层

    1. @Service
    2. @Slf4j
    3. public class DishServiceImpl extends ServiceImpl implements DishService {
    4. @Autowired
    5. private DishFlavorMapper dishFlavorMapper;
    6. @Autowired
    7. private DishMapper dishMapper;
    8. @Autowired
    9. private CategoryMapper categoryMapper;
    10. @Autowired
    11. private DishFlavorService dishFlavorService;
    12. @Override
    13. @Transactional
    14. public R saveDishWithFlavor(DishDto dishDto) {
    15. this.save(dishDto);
    16. Long dishId = dishDto.getId();
    17. List flavors = dishDto.getFlavors();
    18. for (DishFlavor flavor : flavors) {
    19. flavor.setDishId(dishId);
    20. dishFlavorMapper.insert(flavor);
    21. }
    22. return R.success("新增菜品成功!");
    23. }
    24. @Override
    25. public R pageByDish(int page, int pageSize, String name) {
    26. Page pageInfo = new Page<>(page, pageSize);
    27. Page dishDtoPage = new Page<>();
    28. LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
    29. lambdaQueryWrapper.like(name != null, Dish::getName, name);
    30. lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
    31. dishMapper.selectPage(pageInfo, lambdaQueryWrapper);
    32. BeanUtils.copyProperties(pageInfo, dishDtoPage, "records");
    33. List records = pageInfo.getRecords();
    34. List collect = records.stream().map((item) -> {
    35. DishDto dishDto = new DishDto();
    36. BeanUtils.copyProperties(item,dishDto);
    37. Long categoryId = item.getCategoryId();
    38. Category category = categoryMapper.selectById(categoryId);
    39. if (category != null) {
    40. String categoryName = category.getName();
    41. dishDto.setCategoryName(categoryName);
    42. }
    43. return dishDto;
    44. }).collect(Collectors.toList());
    45. dishDtoPage.setRecords(collect);
    46. return R.success(dishDtoPage);
    47. }
    48. @Override
    49. @Transactional
    50. public R updateDishWithFlavor(DishDto dishDto) {
    51. this.updateById(dishDto);
    52. LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
    53. queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
    54. dishFlavorMapper.delete(queryWrapper);
    55. List flavors = dishDto.getFlavors();
    56. for (DishFlavor flavor : flavors) {
    57. flavor.setDishId(dishDto.getId());
    58. dishFlavorMapper.insert(flavor);
    59. }
    60. return R.success("修改成功!");
    61. }
    62. @Override
    63. public R getDishInfoById(Long id) {
    64. Dish dish = this.getById(id);
    65. DishDto dishDto = new DishDto();
    66. BeanUtils.copyProperties(dish, dishDto);
    67. LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
    68. queryWrapper.eq(DishFlavor::getDishId,dish.getId());
    69. List flavors = dishFlavorMapper.selectList(queryWrapper);
    70. dishDto.setFlavors(flavors);
    71. return R.success(dishDto);
    72. }
    73. @Override
    74. @Transactional
    75. public R deleteDish(List ids) {
    76. // 查菜品的状态,是否可以删除()
    77. LambdaQueryWrapper dishQueryWrapper = new LambdaQueryWrapper<>();
    78. dishQueryWrapper.in(Dish::getId, ids);
    79. dishQueryWrapper.eq(Dish::getStatus, 1);
    80. int count = this.count(dishQueryWrapper);
    81. if(count > 0) {
    82. throw new CustomException("菜品正在售卖中!不能删除!");
    83. }
    84. // 删除dish
    85. dishMapper.deleteBatchIds(ids);
    86. // 删除dish_flavor
    87. LambdaQueryWrapper dfQueryWrapper = new LambdaQueryWrapper<>();
    88. dfQueryWrapper.in(DishFlavor::getDishId, ids);
    89. dishFlavorMapper.delete(dfQueryWrapper);
    90. return R.success("删除菜品成功!");
    91. }
    92. @Override
    93. public R sellStatus(Integer status, List ids) {
    94. LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
    95. queryWrapper.in(ids != null, Dish::getId, ids);
    96. List list = dishMapper.selectList(queryWrapper);
    97. for (Dish dish : list) {
    98. if (dish != null) {
    99. dish.setStatus(status);
    100. dishMapper.updateById(dish);
    101. }
    102. }
    103. return R.success("售卖状态修改成功");
    104. }
    105. }

    Gitee仓库​​​​​​​

    瑞吉外卖: 瑞吉外卖项目完整代码,使用Sprinboot,SSM,MP,MySQL,Redis,Nginx等技术。 - Gitee.comicon-default.png?t=M85Bhttps://gitee.com/Harmony_TL/reggie_take_out/tree/reggie_v9_dishInfo

  • 相关阅读:
    共同见证丨酷雷曼武汉运营中心成立2周年
    MongoDB内部的存储原理
    SpringMvc 如何同时支持 Jsp 和 Json 接口?
    轻松搞定Spring集成缓存,让你的应用程序飞起来!
    金仓数据库KingbaseES 绑定变量窥探机制
    SVN 服务器建立
    Python基于php+MySQL的英语学习交流平台
    C语言实现DFS和BFS
    hadoop配置文件workers
    CVE-2023-5129 libwebp堆缓冲区溢出漏洞影响分析
  • 原文地址:https://blog.csdn.net/weixin_43715214/article/details/127739000