104 lines
3.2 KiB
Java
104 lines
3.2 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.SetmealDTO;
|
||
import com.sky.dto.SetmealPageQueryDTO;
|
||
import com.sky.entity.Dish;
|
||
import com.sky.entity.Setmeal;
|
||
import com.sky.entity.SetmealDish;
|
||
import com.sky.exception.DeletionNotAllowedException;
|
||
import com.sky.mapper.SetmealDishMapper;
|
||
import com.sky.mapper.SetmealMapper;
|
||
import com.sky.result.PageResult;
|
||
import com.sky.service.SetmealService;
|
||
import com.sky.vo.SetmealVO;
|
||
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 SetmealServiceImpl implements SetmealService {
|
||
|
||
@Autowired
|
||
private SetmealMapper setmealMapper;
|
||
@Autowired
|
||
private SetmealDishMapper setmealDishMapper;
|
||
|
||
/**
|
||
* 套餐分页查询
|
||
* @param setmealPageQueryDTO
|
||
* @return
|
||
*/
|
||
@Override
|
||
public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
|
||
//设置分页
|
||
PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
|
||
Page<Setmeal> page = setmealMapper.pageQuery(setmealPageQueryDTO);
|
||
return new PageResult(page.getTotal(),page.getResult());
|
||
}
|
||
|
||
/**
|
||
* 新增套餐
|
||
* @param setmealDTO
|
||
*/
|
||
@Override
|
||
@Transactional
|
||
public void insert(SetmealDTO setmealDTO) {
|
||
//插入套餐信息
|
||
Setmeal setmeal = new Setmeal();
|
||
BeanUtils.copyProperties(setmealDTO,setmeal);
|
||
setmealMapper.insert(setmeal);
|
||
|
||
//插入套餐包含菜品的信息
|
||
List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
|
||
//设置setmealId
|
||
setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
|
||
setmealDishMapper.insertBatch(setmealDishes);
|
||
}
|
||
|
||
/**
|
||
* 批量删除套餐
|
||
* @param ids
|
||
*/
|
||
@Override
|
||
@Transactional
|
||
public void deleteBatch(List<Long> ids) {
|
||
//查看套餐是否有在售状态
|
||
for (Long id : ids) {
|
||
Setmeal setmeal = setmealMapper.selectById(id);
|
||
if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
|
||
throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
|
||
}
|
||
}
|
||
|
||
//批量删除套餐(动态SQL)
|
||
//删除setmeal数据
|
||
setmealMapper.deleteBatch(ids);
|
||
//删除setmeal_dish数据
|
||
setmealDishMapper.deleteBySetmealIds(ids);
|
||
}
|
||
|
||
/**
|
||
* 根据id查询套餐(及关联菜品)
|
||
* @param id
|
||
* @return
|
||
*/
|
||
@Override
|
||
public SetmealVO getByIdWithSetmealDishes(Long id) {
|
||
SetmealVO setmealVO = new SetmealVO();
|
||
//查询基本套餐信息
|
||
Setmeal setmeal = setmealMapper.selectById(id);
|
||
BeanUtils.copyProperties(setmeal,setmealVO);
|
||
//查询对应菜品信息
|
||
List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
|
||
setmealVO.setSetmealDishes(setmealDishes);
|
||
return setmealVO;
|
||
}
|
||
}
|