185 lines
5.1 KiB
Java
185 lines
5.1 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.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;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
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
|
||
@Slf4j
|
||
public class DishServiceImpl implements DishService {
|
||
|
||
@Autowired
|
||
private DishMapper dishMapper;
|
||
@Autowired
|
||
private DishFlavorMapper dishFlavorMapper;
|
||
@Autowired
|
||
private SetmealDishMapper setmealDishMapper;
|
||
|
||
/**
|
||
* 新增菜品
|
||
*
|
||
* @param dishDTO
|
||
*/
|
||
@Override
|
||
@Transactional
|
||
public void saveWithFlavor(DishDTO dishDTO) {
|
||
//向菜品表插入数据(一条)
|
||
Dish dish = new Dish();
|
||
BeanUtils.copyProperties(dishDTO, dish);
|
||
dishMapper.insert(dish);
|
||
|
||
//获取Insert语句生成的主键值
|
||
Long id = dish.getId();
|
||
|
||
//向口味表插入N条数据
|
||
List<DishFlavor> flavors = dishDTO.getFlavors();
|
||
if (flavors != null && !flavors.isEmpty()) {
|
||
//遍历flavors,插入id
|
||
flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
|
||
//批量插入
|
||
dishFlavorMapper.insertBatch(flavors);
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 菜品分页查询
|
||
*
|
||
* @param pageQueryDTO
|
||
* @return
|
||
*/
|
||
@Override
|
||
public PageResult pageQuery(DishPageQueryDTO pageQueryDTO) {
|
||
//设置分页
|
||
PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
|
||
Page<DishVO> page = dishMapper.pageQuery(pageQueryDTO);
|
||
return new PageResult(page.getTotal(), page.getResult());
|
||
}
|
||
|
||
/**
|
||
* 菜品批量删除
|
||
*
|
||
* @param ids
|
||
*/
|
||
@Override
|
||
@Transactional
|
||
public void deleteBatch(List<Long> ids) {
|
||
//判断菜品是否能够删除
|
||
//是否有起售中菜品
|
||
for (Long id : ids) {
|
||
Dish dish = dishMapper.selectById(id);
|
||
if (dish.getStatus().equals(StatusConstant.ENABLE)) {
|
||
throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
|
||
}
|
||
}
|
||
//是否关联套餐
|
||
List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
|
||
if (setmealIds != null && !setmealIds.isEmpty()) {
|
||
throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
|
||
}
|
||
//删除菜品
|
||
/*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);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 根据分类id查询菜品
|
||
* @param categoryId
|
||
* @return
|
||
*/
|
||
@Override
|
||
public List<Dish> selectByCategoryId(Long categoryId) {
|
||
return dishMapper.selectByCategoryId(categoryId);
|
||
}
|
||
|
||
/**
|
||
* 菜品起售停售
|
||
* @param status
|
||
* @param id
|
||
*/
|
||
@Override
|
||
@Transactional
|
||
//TODO 菜品关联套餐状态
|
||
public void startOrStop(Integer status, Long id) {
|
||
//更新菜品状态
|
||
Dish dish = Dish.builder()
|
||
.id(id)
|
||
.status(status)
|
||
.build();
|
||
dishMapper.update(dish);
|
||
//更新菜品所关联套餐的状态
|
||
|
||
}
|
||
}
|