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.DishMapper; 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.ArrayList; import java.util.List; @Service public class SetmealServiceImpl implements SetmealService { @Autowired private SetmealMapper setmealMapper; @Autowired private SetmealDishMapper setmealDishMapper; @Autowired private DishMapper dishMapper; /** * 套餐分页查询 * * @param setmealPageQueryDTO * @return */ @Override public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) { //设置分页 PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize()); Page 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 setmealDishes = setmealDTO.getSetmealDishes(); //设置setmealId setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId())); setmealDishMapper.insertBatch(setmealDishes); } /** * 批量删除套餐 * * @param ids */ @Override @Transactional public void deleteBatch(List 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 setmealDishes = setmealDishMapper.selectBySetmealId(id); setmealVO.setSetmealDishes(setmealDishes); return setmealVO; } /** * 修改套餐 * * @param setmealDTO */ @Override @Transactional public void updateWithSetmealDishes(SetmealDTO setmealDTO) { //更新setmeal基本数据 Setmeal setmeal = new Setmeal(); BeanUtils.copyProperties(setmealDTO, setmeal); setmealMapper.update(setmeal); //更新setmeal_dish数据 //删除原setmeal_dish相关数据 setmealDishMapper.deleteBySetmealId(setmealDTO.getId()); //添加setmeal_id List setmealDishes = setmealDTO.getSetmealDishes(); setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId())); //插入新的数据 setmealDishMapper.insertBatch(setmealDTO.getSetmealDishes()); } /** * 套餐起售停售 * * @param status * @param id */ @Override public void startOrStop(Integer status, Long id) { //看是否为起售,如果为起售,需要判断其关联的菜品有停售状态 if (status.equals(StatusConstant.ENABLE)) { /* 获取其关联的菜品信息--setmeal_dish--setmealDishes--遍历->getDishId 先把dishId放到一个集合中,批量查询该dish_id对应且status为0的菜品信息 如果查询到,则抛出删除失败异常 如果没有,则进行下一步,改变状态 */ List setmealDishes = setmealDishMapper.selectBySetmealId(id); List dishIds = new ArrayList<>(); setmealDishes.forEach(setmealDish -> dishIds.add(setmealDish.getDishId())); List dishes = dishMapper.selectByDishIdsAndStatusDisable(dishIds); if (dishes != null && !dishes.isEmpty()){ throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED); } } //不为起售则直接更改状态 Setmeal setmeal = Setmeal.builder() .status(status) .id(id) .build(); setmealMapper.update(setmeal); } }