修改菜品
This commit is contained in:
@@ -3,6 +3,7 @@ package com.sky.service;
|
||||
import com.sky.dto.DishDTO;
|
||||
import com.sky.dto.DishPageQueryDTO;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.vo.DishVO;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
@@ -28,4 +29,17 @@ public interface DishService {
|
||||
* @param ids
|
||||
*/
|
||||
void deleteBatch(List<Long> ids);
|
||||
|
||||
/**
|
||||
* 根据id查询菜品及口味
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
DishVO getByIdWithFlavor(Long id);
|
||||
|
||||
/**
|
||||
* 修改菜品基本信息(及口味)
|
||||
* @param dishDTO
|
||||
*/
|
||||
void updateWithFlavor(DishDTO dishDTO);
|
||||
}
|
||||
|
||||
@@ -64,6 +64,7 @@ public class DishServiceImpl implements DishService {
|
||||
|
||||
/**
|
||||
* 菜品分页查询
|
||||
*
|
||||
* @param pageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
@@ -72,11 +73,12 @@ public class DishServiceImpl implements DishService {
|
||||
//设置分页
|
||||
PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
||||
Page<DishVO> page = dishMapper.pageQuery(pageQueryDTO);
|
||||
return new PageResult(page.getTotal(),page.getResult());
|
||||
return new PageResult(page.getTotal(), page.getResult());
|
||||
}
|
||||
|
||||
/**
|
||||
* 菜品批量删除
|
||||
*
|
||||
* @param ids
|
||||
*/
|
||||
@Override
|
||||
@@ -86,20 +88,69 @@ public class DishServiceImpl implements DishService {
|
||||
//是否有起售中菜品
|
||||
for (Long id : ids) {
|
||||
Dish dish = dishMapper.selectById(id);
|
||||
if (dish.getStatus().equals(StatusConstant.ENABLE)){
|
||||
if (dish.getStatus().equals(StatusConstant.ENABLE)) {
|
||||
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
|
||||
}
|
||||
}
|
||||
//是否关联套餐
|
||||
List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
|
||||
if (setmealIds != null && !setmealIds.isEmpty()){
|
||||
if (setmealIds != null && !setmealIds.isEmpty()) {
|
||||
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
|
||||
}
|
||||
//删除菜品
|
||||
for (Long id : ids) {
|
||||
/*for (Long id : ids) {
|
||||
dishMapper.deleteById(id);
|
||||
//删除菜品口味
|
||||
dishFlavorMapper.deleteByDishId(id);
|
||||
}*/
|
||||
dishMapper.deleteByIds(ids);
|
||||
dishFlavorMapper.deleteByDishIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id查询菜品及口味
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public DishVO getByIdWithFlavor(Long id) {
|
||||
//根据id查询菜品
|
||||
Dish dish = dishMapper.selectById(id);
|
||||
|
||||
//根据菜品id查询口味
|
||||
List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
|
||||
|
||||
//封装到DishVO对象
|
||||
DishVO dishVO = new DishVO();
|
||||
BeanUtils.copyProperties(dish, dishVO);
|
||||
dishVO.setFlavors(dishFlavors);
|
||||
|
||||
return dishVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改菜品基本信息(及口味)
|
||||
*
|
||||
* @param dishDTO
|
||||
*/
|
||||
@Override
|
||||
public void updateWithFlavor(DishDTO dishDTO) {
|
||||
//修改菜品基本信息
|
||||
Dish dish = new Dish();
|
||||
BeanUtils.copyProperties(dishDTO, dish);
|
||||
dishMapper.update(dish);
|
||||
|
||||
//删除原有口味数据
|
||||
dishFlavorMapper.deleteByDishId(dishDTO.getId());
|
||||
|
||||
//重新插入新的口味数据
|
||||
List<DishFlavor> flavors = dishDTO.getFlavors();
|
||||
if (flavors != null && !flavors.isEmpty()) {
|
||||
//设置dishId
|
||||
flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
|
||||
//插入新的口味数据
|
||||
dishFlavorMapper.insertBatch(flavors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user