157 lines
5.3 KiB
Java
157 lines
5.3 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.Setmeal;
|
||
import com.sky.entity.SetmealDish;
|
||
import com.sky.exception.DeletionNotAllowedException;
|
||
import com.sky.exception.SetmealEnableFailedException;
|
||
import com.sky.mapper.DishFlavorMapper;
|
||
import com.sky.mapper.DishMapper;
|
||
import com.sky.mapper.SetmealMapper;
|
||
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.ArrayList;
|
||
import java.util.List;
|
||
|
||
@Service
|
||
public class DishServiceImpl implements DishService {
|
||
@Autowired
|
||
private DishMapper dishMapper;
|
||
@Autowired
|
||
private DishFlavorMapper dishFlavorMapper;
|
||
@Autowired
|
||
private Setmeal_dishMapper setmeal_dishMapper;
|
||
@Autowired
|
||
private SetmealMapper setmealMapper;
|
||
@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{
|
||
List<Long> setmealids=setmeal_dishMapper.queryByDishId(id);
|
||
if(setmealids!=null && setmealids.size() > 0)
|
||
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);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void startOrStop(Integer status, Long id) {
|
||
Dish dish=new Dish();
|
||
dish.setId(id);
|
||
dish.setStatus(status);
|
||
if(status==StatusConstant.DISABLE){ //如果菜品停售,正在起售的套餐也要停售
|
||
List<Long> setmealids=setmeal_dishMapper.queryByDishId(id);
|
||
for(Long setmealid:setmealids){
|
||
Setmeal setmeal=setmealMapper.getById(setmealid);
|
||
if(StatusConstant.ENABLE == setmeal.getStatus()){ //若菜品停售,则套餐起售失败!
|
||
setmeal.setStatus(StatusConstant.DISABLE);
|
||
setmealMapper.update(setmeal);
|
||
}
|
||
}
|
||
}
|
||
dishMapper.update(dish);
|
||
}
|
||
|
||
@Override
|
||
public List<Dish> list(Dish dish) {
|
||
List<Dish> dishList=dishMapper.listByType(dish);
|
||
return dishList;
|
||
}
|
||
|
||
@Override
|
||
/**
|
||
* 条件查询菜品和口味
|
||
* @param dish
|
||
* @return
|
||
*/
|
||
public List<DishVO> listWithFlavor(Dish dish) {
|
||
List<Dish> dishList = dishMapper.listByType(dish);
|
||
|
||
List<DishVO> dishVOList = new ArrayList<>();
|
||
|
||
for (Dish d : dishList) {
|
||
DishVO dishVO = new DishVO();
|
||
BeanUtils.copyProperties(d,dishVO);
|
||
|
||
//根据菜品id查询对应的口味
|
||
List<DishFlavor> flavors = dishFlavorMapper.queryByDishId(d.getId());
|
||
|
||
dishVO.setFlavors(flavors);
|
||
dishVOList.add(dishVO);
|
||
}
|
||
|
||
return dishVOList;
|
||
}
|
||
|
||
}
|