四.删除菜品
业务规则:
- 可以一次删除一个菜品,也可以一次删除多个菜品
- 起售中的菜品不能删除
- 被套餐关联得菜品不能删除
- 删除菜品后,关联得口味数据也需要删除掉
一共需要操作三个表,注意加@Transactional事物注解 - Controller
/**
* 删除菜品
*/
@DeleteMapping
@ApiOperation("删除菜品")
public Result delete(@RequestParam List<Long> ids) {//@RequestParam接收请求参数
log.info("删除菜品:{}", ids);
dishService.deleteBatch(ids);
return Result.success();
}
- Service
/**
* 删除菜品
*/
void deleteBatch(List<Long> ids);
- Impl
/**
* 删除菜品
*/
@Override
@Transactional
public void deleteBatch(List<Long> ids) {
// 判断当前菜品是否可以删除-是否存在启售菜品
List<Dish> dishes = dishMapper.selectBatchIds(ids);
for (Dish dish : dishes) {
if (dish.getStatus().equals(StatusConstant.ENABLE)) {
throw new RuntimeException(MessageConstant.DISH_ON_SALE);
}
}
// 判断当前菜品是否可以删除-是否存在套餐
List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
if (setmealIds != null && !setmealIds.isEmpty()) {
throw new RuntimeException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
}
// 删除菜品
dishMapper.deleteBatchIds(ids);
// 删除口味
LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(DishFlavor::getDishId, ids);
dishFlavorMapper.delete(queryWrapper);
}
- mapper
@Mapper
public interface SetmealDishMapper extends BaseMapper<SetmealDish> {
@Select.List({
@Select("SELECT setmeal_id FROM setmeal_dish WHERE dish_id IN (#{ids})")
})
List<Long> getSetmealIdsByDishIds(List<Long> ids);
}
五.修改菜品
- 根据id查询菜品
- 根据类型查询分类(已实现)
- 文件上传(已实现)
- 修改菜品
加上@Transactional事务注解 - Controller
/**
* 根据id查询菜品
*/
@GetMapping("/{id}")
@ApiOperation("根据id查询菜品")
public Result<DishVO> getById(@PathVariable Long id) {//@PathVariable接收请求路径中的参数
log.info("根据id查询菜品:{}", id);
DishVO dishVO = dishService.getByIdWithFlavor(id);
return Result.success(dishVO);
}
/**
* 修改菜品
*/
@PutMapping
@ApiOperation("修改菜品")
public Result update(@RequestBody DishDTO dishDTO) {
log.info("修改菜品:{}", dishDTO);
dishService.updateWithFlavor(dishDTO);
return Result.success();
}
- Service
/**
* 根据id查询菜品和口味
*/
DishVO getByIdWithFlavor(Long id);
/**
* 更新菜品和口味
*/
void updateWithFlavor(DishDTO dishDTO);
- Impl
/**
* 根据id查询菜品和口味
*/
@Override
@Transactional
public DishVO getByIdWithFlavor(Long id) {
// 查询菜品
Dish dish = dishMapper.selectById(id);
// 查询口味
LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DishFlavor::getDishId, id);
List<DishFlavor> flavors = dishFlavorMapper.selectList(queryWrapper);
// 封装结果
DishVO dishVO = new DishVO();
BeanUtils.copyProperties(dish, dishVO);
dishVO.setFlavors(flavors);
return dishVO;
}
/**
* 更新菜品和口味
*/
@Override
@Transactional
public void updateWithFlavor(DishDTO dishDTO) {
Dish dish = new Dish();
BeanUtils.copyProperties(dishDTO, dish);
dishMapper.updateById(dish);
// 删除原有口味
LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DishFlavor::getDishId, dish.getId());
dishFlavorMapper.delete(queryWrapper);
// 插入新口味
List<DishFlavor> flavors = dishDTO.getFlavors();
if (flavors != null && !flavors.isEmpty()) {
flavors.forEach(flavor -> flavor.setDishId(dish.getId()));
// 批量插入
MybatisBatch<DishFlavor> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, flavors);
MybatisBatch.Method<DishFlavor> method = new MybatisBatch.Method<>(DishFlavorMapper.class);
mybatisBatch.execute(method.insert());
}
}
六. 状态
- Controller
/**
* 修改菜品状态
*/
@PostMapping("/status/{status}")
@ApiOperation("修改菜品状态")
public Result updateStatus(@RequestParam Long id, @PathVariable Integer status) {//RequestParam接收请求参数,PathVariable接收请求路径中的参数
log.info("修改菜品状态:{}", id);
dishService.updateStatus(id, status);
return Result.success();
}
/**
* 根据分类id查询菜品
*/
@GetMapping("/list")
@ApiOperation("根据分类id查询菜品")
public Result<List<Dish>> listResult(@RequestParam Long categoryId) {
log.info("根据分类id查询菜品:{}", categoryId);
List<Dish> list = dishService.listByCategoryId(categoryId);
return Result.success(list);
}
- Service
/**
* 更新菜品状态
*/
void updateStatus(Long id, Integer status);
/**
* 根据分类id查询菜品
*/
List<Dish> listByCategoryId(Long categoryId);
- Impl
/**
* 更新菜品状态
*/
@Override
@Transactional
public void updateStatus(Long id, Integer status) {
Dish dish = dishMapper.selectById(id);
if (dish == null) {
throw new RuntimeException(MessageConstant.DISH_NOT_FOUND);
}
dish.setStatus(status);
dishMapper.updateById(dish);
if (Objects.equals(status, StatusConstant.DISABLE)){
// 如果是停售操作,还需要将包含当前菜品的套餐也停售
List<Long> ids = new ArrayList<>();
ids.add(id);
// 查询包含当前菜品的套餐
LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(SetmealDish::getDishId, ids);
List<Long> setmealIds = setmealDishMapper.selectList(queryWrapper).stream().map(SetmealDish::getSetmealId).distinct().toList();
if (!setmealIds.isEmpty()) {
throw new RuntimeException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
}
}
}
/**
* 根据分类id查询菜品
*/
@Override
@Transactional
public List<Dish> listByCategoryId(Long categoryId) {
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Dish::getCategoryId, categoryId);
List<Dish> dishes = dishMapper.selectList(queryWrapper);
log.info("根据分类id查询菜品:{}", dishes);
return dishes;
}
标签:queryWrapper,List,ids,笔记,外卖,菜品,dish,苍穹,id
From: https://blog.csdn.net/qq_73340809/article/details/142301880