需求分析:
代码开发-梳理交互过程:
重写接口:
public interface SetmealService extends IService<Setmeal> {
/**
* 新增套餐,同时需要保存套餐和菜品的关联
* @param setmealDto
*/
public void saveWithDish(SetmealDto setmealDto);
/**
* 删除套餐,同时需要删除套餐和菜品的关联数据
* @param ids
*/
public void removeWithDish(List<Long> ids);
}
重写方法:
/**
* 删除套餐,同时需要删除套餐和菜品的关联数据
* @param ids
*/
@Override
public void removeWithDish(List<Long> ids) {
//select count(*) from setmeal where id in(1,2,3) and status = 1;
//查询套餐状态,确定是否可用删除
LambdaQueryWrapper<Setmeal> eq = Wrappers.lambdaQuery(Setmeal.class)
.in(Setmeal::getId, ids)
.eq(Setmeal::getStatus, 1);
int count = this.count(eq);
if(count > 0){
//如果不能删除,抛出一个业务异常
throw new CustomException("套餐正在售卖中,不能删除");
}
//如果可以删除,先删除套餐表中的数据---setmeal
this.removeByIds(ids);
//delete from setmeal_dish where setmeal_id in(1,2,3);
LambdaQueryWrapper<SetmealDish> in = Wrappers.lambdaQuery(SetmealDish.class)
.in(SetmealDish::getSetmealId, ids);
//删除关系表中的数据---setmeal_dish
setmealDishService.remove(in);
}
调用重写的方法:
/**
* 删除套餐
* @param ids
* @return
*/
@DeleteMapping
public R<String> delete(@RequestParam List<Long> ids){
setmealService.removeWithDish(ids);
return R.success("套餐数据删除成功");
}