外卖项目day03-删除菜品-需求分析和功能实现---2
This commit is contained in:
@@ -2,6 +2,7 @@ package com.sky.handler;
|
||||
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.exception.BaseException;
|
||||
import com.sky.exception.DeletionNotAllowedException;
|
||||
import com.sky.result.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
@@ -36,4 +37,16 @@ public class GlobalExceptionHandler {
|
||||
return Result.error(MessageConstant.UNKNOWN_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 捕获不允许删除异常处理
|
||||
* @param ex
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler
|
||||
// public Result exceptionHandler1(DeletionNotAllowedException ex){
|
||||
public Result exceptionHandler1(BaseException ex){//多态!!!用来处理所有的自定义业务异常!!!
|
||||
log.error("异常信息:{}", ex.getMessage());
|
||||
return Result.error(ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.sky.entity.DishFlavor;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
@@ -12,4 +13,11 @@ public interface DishFlavorMapper {
|
||||
* @param flavors
|
||||
*/
|
||||
void insertBath(List<DishFlavor> flavors);
|
||||
|
||||
/**
|
||||
* 根据菜品id删除口味
|
||||
* @param dishId
|
||||
*/
|
||||
@Delete("delete from dish_flavor where dish_id = #{dishId}")
|
||||
void deleteByDishId(Long dishId);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.sky.entity.Dish;
|
||||
import com.sky.entity.Employee;
|
||||
import com.sky.enumeration.OperationType;
|
||||
import com.sky.vo.DishVO;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
@@ -42,4 +43,11 @@ public interface DishMapper {
|
||||
*/
|
||||
@Select("select * from dish where id = #{id}")
|
||||
Dish getDishById(Long id);
|
||||
|
||||
/**
|
||||
* 根据id删除菜品
|
||||
* @param dishId
|
||||
*/
|
||||
@Delete("delete from dish where id = #{dishId}")
|
||||
void deleteById(Long dishId);
|
||||
}
|
||||
|
||||
@@ -80,6 +80,7 @@ public class DishServiceImpl implements DishService {
|
||||
* @param ids 要删除的菜品id
|
||||
*/
|
||||
@Override
|
||||
@Transactional //将当前的业务方法中的sql都处于同一个事务中全成功或全失败
|
||||
public void deleteBatch(List<Long> ids) {
|
||||
//1.传入的菜品的起售状态,如果有起售,提示用户不能删除,删除失败
|
||||
ids.forEach(id->{
|
||||
@@ -95,6 +96,14 @@ public class DishServiceImpl implements DishService {
|
||||
//说明菜品是被关联了,那么就批量删除失败
|
||||
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
|
||||
}
|
||||
//3.删除菜品后,相关的菜品口味也要删除
|
||||
//3.1 批量删除菜品
|
||||
//3.2 批量删除菜品口味
|
||||
for (Long dishId : ids) {
|
||||
//删除菜品与它相关的口味
|
||||
dishMapper.deleteById(dishId);//根据菜品id删除菜品
|
||||
dishFlavorMapper.deleteByDishId(dishId);//根据菜品id删除菜品口味
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user