我们先看看这个页面,剥离出本质其实就是CRUD!
这里面还有一个 “启售” 和 “停售”,在黑马视频中是没有的,我们这里也将其一起来实现一下!
这里我们基本上在前面都有讲过一些,所以这里就不做过多的分析了!
请求 | 说明 |
---|---|
请求方式 | GET |
请求路径 | /dish/page |
请求参数 | ?page=1&pageSize=10&name=xxx |
新增菜品
请求 | 说明 |
---|---|
请求方式 | POST |
请求路径 | /dish |
请求参数 | json格式 |
请求 | 说明 |
---|---|
请求方式 | GET |
请求路径 | /dish/{id} |
请求 | 说明 |
---|---|
请求方式 | PUT |
请求路径 | /dish |
请求参数 | json格式数据 |
- @RestController
- @Slf4j
- @RequestMapping("/dish")
- public class DishController {
-
- @Autowired
- private DishService dishService;
-
- /**
- * 新增菜品
- * @param dishDto
- * @return
- */
- @PostMapping
- public R
saveDishWithFlavor(@RequestBody DishDto dishDto) { - return dishService.saveDishWithFlavor(dishDto);
- }
-
- /**
- * 菜品管理-分页查询
- * @param page
- * @param pageSize
- * @param name
- * @return
- */
- @GetMapping("/page")
- public R
pageByDish(int page,int pageSize,String name) { - return dishService.pageByDish(page, pageSize,name);
- }
-
- /**
- * 修改菜品
- * @param dishDto
- * @return
- */
- @PutMapping
- public R
updateDishWithFlavor(@RequestBody DishDto dishDto) { - return dishService.updateDishWithFlavor(dishDto);
- }
-
- /**
- * 根据id查询菜品信息与对应的口味信息
- * @param id
- * @return
- */
- @GetMapping("/{id}")
- public R
getDishInfoById(@PathVariable Long id) { - return dishService.getDishInfoById(id);
- }
-
- /**
- * 删除菜品
- * @param ids
- * @return
- *
- * @@RequestParam: 用于将请求参数区数据映射到功能处理方法的参数上
- */
- @DeleteMapping
- public R
deleteDish(@RequestParam List ids) { - log.info("ids:{}",ids);
- return dishService.deleteDish(ids);
- }
-
- /**
- * 起售菜品、停售菜品
- * @param status
- * @param ids
- * @return
- *
- * http://localhost:8080/dish/status/0?ids=1413384757047271425
- */
- @PostMapping("/status/{status}")
- public R
sellStatus(@PathVariable("status") Integer status, @RequestParam List ids) { - return dishService.sellStatus(status, ids);
- }
- }
- @Service
- @Slf4j
- public class DishServiceImpl extends ServiceImpl
implements DishService { -
- @Autowired
- private DishFlavorMapper dishFlavorMapper;
-
- @Autowired
- private DishMapper dishMapper;
-
- @Autowired
- private CategoryMapper categoryMapper;
-
- @Autowired
- private DishFlavorService dishFlavorService;
-
- @Override
- @Transactional
- public R
saveDishWithFlavor(DishDto dishDto) { - this.save(dishDto);
-
- Long dishId = dishDto.getId();
-
- List
flavors = dishDto.getFlavors(); - for (DishFlavor flavor : flavors) {
- flavor.setDishId(dishId);
- dishFlavorMapper.insert(flavor);
- }
-
- return R.success("新增菜品成功!");
- }
-
- @Override
- public R
pageByDish(int page, int pageSize, String name) { - Page
pageInfo = new Page<>(page, pageSize); - Page
dishDtoPage = new Page<>(); -
- LambdaQueryWrapper
lambdaQueryWrapper = new LambdaQueryWrapper<>(); - lambdaQueryWrapper.like(name != null, Dish::getName, name);
- lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
- dishMapper.selectPage(pageInfo, lambdaQueryWrapper);
- BeanUtils.copyProperties(pageInfo, dishDtoPage, "records");
-
- List
records = pageInfo.getRecords(); -
- List
collect = records.stream().map((item) -> { - DishDto dishDto = new DishDto();
- BeanUtils.copyProperties(item,dishDto);
- Long categoryId = item.getCategoryId();
- Category category = categoryMapper.selectById(categoryId);
-
- if (category != null) {
- String categoryName = category.getName();
- dishDto.setCategoryName(categoryName);
- }
-
- return dishDto;
- }).collect(Collectors.toList());
-
- dishDtoPage.setRecords(collect);
-
- return R.success(dishDtoPage);
- }
-
- @Override
- @Transactional
- public R
updateDishWithFlavor(DishDto dishDto) { - this.updateById(dishDto);
- LambdaQueryWrapper
queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
- dishFlavorMapper.delete(queryWrapper);
-
- List
flavors = dishDto.getFlavors(); - for (DishFlavor flavor : flavors) {
- flavor.setDishId(dishDto.getId());
- dishFlavorMapper.insert(flavor);
- }
- return R.success("修改成功!");
- }
-
- @Override
- public R
getDishInfoById(Long id) { - Dish dish = this.getById(id);
-
- DishDto dishDto = new DishDto();
- BeanUtils.copyProperties(dish, dishDto);
-
- LambdaQueryWrapper
queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(DishFlavor::getDishId,dish.getId());
-
- List
flavors = dishFlavorMapper.selectList(queryWrapper); - dishDto.setFlavors(flavors);
- return R.success(dishDto);
- }
-
- @Override
- @Transactional
- public R
deleteDish(List ids) { - // 查菜品的状态,是否可以删除()
- LambdaQueryWrapper
dishQueryWrapper = new LambdaQueryWrapper<>(); - dishQueryWrapper.in(Dish::getId, ids);
- dishQueryWrapper.eq(Dish::getStatus, 1);
-
- int count = this.count(dishQueryWrapper);
- if(count > 0) {
- throw new CustomException("菜品正在售卖中!不能删除!");
- }
-
- // 删除dish
- dishMapper.deleteBatchIds(ids);
-
- // 删除dish_flavor
- LambdaQueryWrapper
dfQueryWrapper = new LambdaQueryWrapper<>(); - dfQueryWrapper.in(DishFlavor::getDishId, ids);
-
- dishFlavorMapper.delete(dfQueryWrapper);
- return R.success("删除菜品成功!");
- }
-
- @Override
- public R
sellStatus(Integer status, List ids) { - LambdaQueryWrapper
queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.in(ids != null, Dish::getId, ids);
-
- List
list = dishMapper.selectList(queryWrapper); -
- for (Dish dish : list) {
- if (dish != null) {
- dish.setStatus(status);
- dishMapper.updateById(dish);
- }
- }
-
- return R.success("售卖状态修改成功");
- }
- }