苍穹外卖--菜品管理-删除功能实现
This commit is contained in:
@@ -4,6 +4,8 @@ import com.sky.dto.DishDTO;
|
||||
import com.sky.dto.DishPageQueryDTO;
|
||||
import com.sky.result.PageResult;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface DishService {
|
||||
/*
|
||||
新增菜品和它的口味
|
||||
@@ -16,4 +18,10 @@ public interface DishService {
|
||||
* @return
|
||||
*/
|
||||
PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO);
|
||||
|
||||
/**
|
||||
* 菜品批量删除
|
||||
* @param ids
|
||||
*/
|
||||
void deleteBath(List<Long> ids);
|
||||
}
|
||||
|
||||
@@ -2,12 +2,16 @@ package com.sky.service.impl;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.constant.StatusConstant;
|
||||
import com.sky.dto.DishDTO;
|
||||
import com.sky.dto.DishPageQueryDTO;
|
||||
import com.sky.entity.Dish;
|
||||
import com.sky.entity.DishFlavor;
|
||||
import com.sky.exception.DeletionNotAllowedException;
|
||||
import com.sky.mapper.DishFlavorMapper;
|
||||
import com.sky.mapper.DishMapper;
|
||||
import com.sky.mapper.SetmealDishMapper;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.service.DishService;
|
||||
import com.sky.vo.DishVO;
|
||||
@@ -25,6 +29,8 @@ public class DishServiceImpl implements DishService {
|
||||
|
||||
@Autowired
|
||||
private DishFlavorMapper dishFlavorMapper;
|
||||
@Autowired
|
||||
private SetmealDishMapper setmealDishMapper;
|
||||
|
||||
|
||||
/*
|
||||
@@ -63,4 +69,32 @@ public class DishServiceImpl implements DishService {
|
||||
Page<DishVO> page = dishMapper.pageQuery(dto);
|
||||
return new PageResult(page.getTotal(),page.getResult());
|
||||
}
|
||||
|
||||
/**
|
||||
* 菜品批量删除
|
||||
* @param ids
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteBath(List<Long> ids) {
|
||||
//1.判断菜品的起售状态,如果是起售中,不能删除,程序立马停止,并提示用户
|
||||
//todo 当前是遍历查询菜品,执行了多次select语句,效率低,我们可以使用一条sql查询出所有菜品,再遍历菜品对象是否有起售状态
|
||||
ids.forEach(id->{
|
||||
Dish dish = dishMapper.getById(id);
|
||||
if (dish.getStatus() == StatusConstant.ENABLE) {
|
||||
//不能删除,程序立马停止,并提示用户
|
||||
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
|
||||
}
|
||||
});
|
||||
//2.判断菜品是否被套餐关联,如果被关联了那么不能删除,程序立马停止,并提示用户
|
||||
List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
|
||||
if (setmealIds != null && setmealIds.size() > 0) {
|
||||
//不能删除,程序立马停止,并提示用户
|
||||
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
|
||||
}
|
||||
//3.如果菜品可以删除,那么相关的口味也要删除掉
|
||||
//根据菜品id删除菜品和口味数据
|
||||
dishMapper.deleteBath(ids);
|
||||
dishFlavorMapper.deleteBath(ids);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user