101 lines
3.5 KiB
Java
101 lines
3.5 KiB
Java
|
|
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.entity.SetmealDish;
|
|||
|
|
import com.sky.exception.DeletionNotAllowedException;
|
|||
|
|
import com.sky.mapper.DishFlavorMapper;
|
|||
|
|
import com.sky.mapper.DishMapper;
|
|||
|
|
import com.sky.mapper.Setmeal_dishMapper;
|
|||
|
|
import com.sky.result.PageResult;
|
|||
|
|
import com.sky.service.DishService;
|
|||
|
|
import com.sky.vo.DishVO;
|
|||
|
|
import org.springframework.beans.BeanUtils;
|
|||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|||
|
|
import org.springframework.stereotype.Service;
|
|||
|
|
import org.springframework.transaction.annotation.Transactional;
|
|||
|
|
|
|||
|
|
import java.util.List;
|
|||
|
|
|
|||
|
|
@Service
|
|||
|
|
public class DishServiceImpl implements DishService {
|
|||
|
|
@Autowired
|
|||
|
|
private DishMapper dishMapper;
|
|||
|
|
@Autowired
|
|||
|
|
private DishFlavorMapper dishFlavorMapper;
|
|||
|
|
@Autowired
|
|||
|
|
private Setmeal_dishMapper setmeal_dishMapper;
|
|||
|
|
@Override
|
|||
|
|
public void addDish(DishDTO dishDTO) {
|
|||
|
|
Dish dish=new Dish();
|
|||
|
|
BeanUtils.copyProperties(dishDTO,dish);
|
|||
|
|
dishMapper.insert(dish);
|
|||
|
|
Long dishId=dish.getId();
|
|||
|
|
List<DishFlavor> flavorList=dishDTO.getFlavors();
|
|||
|
|
for(DishFlavor flavor:flavorList){
|
|||
|
|
flavor.setDishId(dishId);
|
|||
|
|
}
|
|||
|
|
dishFlavorMapper.insertBatch(flavorList);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
|
|||
|
|
PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
|
|||
|
|
List<DishVO> dishVOList=dishMapper.list(dishPageQueryDTO);
|
|||
|
|
Page<DishVO> p= (Page<DishVO>) dishVOList;
|
|||
|
|
PageResult pageResult=new PageResult(p.getTotal(),p.getResult());
|
|||
|
|
return pageResult;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
@Transactional
|
|||
|
|
public void deleteBatch(Long[] ids) {
|
|||
|
|
for(Long id:ids){
|
|||
|
|
Dish dish=dishMapper.queryById(id);
|
|||
|
|
if(dish.getStatus()== StatusConstant.ENABLE)
|
|||
|
|
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
|
|||
|
|
else{
|
|||
|
|
SetmealDish setmealDish=setmeal_dishMapper.queryByDishId(id);
|
|||
|
|
if(setmealDish!=null)
|
|||
|
|
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
|
|||
|
|
else {
|
|||
|
|
dishMapper.deleteById(id);
|
|||
|
|
dishFlavorMapper.deleteByDishId(id);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public DishVO queryByIdWithFlavor(Long id) {
|
|||
|
|
Dish dish=dishMapper.queryById(id);
|
|||
|
|
DishVO dishVO=new DishVO();
|
|||
|
|
BeanUtils.copyProperties(dish,dishVO);
|
|||
|
|
List<DishFlavor> dishFlavors=dishFlavorMapper.queryByDishId(id);
|
|||
|
|
dishVO.setFlavors(dishFlavors);
|
|||
|
|
return dishVO;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void update(DishDTO dishDTO) {
|
|||
|
|
Dish dish=new Dish();
|
|||
|
|
BeanUtils.copyProperties(dishDTO,dish);
|
|||
|
|
dishMapper.update(dish);
|
|||
|
|
dishFlavorMapper.deleteByDishId(dishDTO.getId()); //先删再添加口味,不要在原来的上面修改!原来可能2种口味,现在3种,也无法修改!
|
|||
|
|
List<DishFlavor> flavors = dishDTO.getFlavors();
|
|||
|
|
if (flavors != null && flavors.size() > 0) {
|
|||
|
|
flavors.forEach(dishFlavor -> {
|
|||
|
|
dishFlavor.setDishId(dishDTO.getId());
|
|||
|
|
});
|
|||
|
|
//向口味表插入n条数据
|
|||
|
|
dishFlavorMapper.insertBatch(flavors);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|