content: 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.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.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.DishItemVO;
  import com.sky.vo.DishVO;
  import com.sky.vo.SetmealVO;
  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.ArrayList;
  import java.util.List;
  
  /**
   * @version 1.0
   * @Author Imak
   * @Date 2024/8/8 13:45
   * @注释
   */
  @Service
  @Slf4j
  public class SetmealServiceImpl implements SetmealService {
      @Autowired
      private SetmealMapper setmealMapper;
      @Autowired
      private SetmealDishMapper setmealDishMapper;
      @Autowired
      private DishMapper dishMapper;
  
      /**
       * 套餐分页查询
       * @param setmealPageQueryDTO
       * @return
       */
      public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
          PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
          Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
          return new PageResult(page.getTotal(),page.getResult());
      }
  
      /**
       * 新增套餐，同时保存其包含的菜品
       * @param setmealDTO
       */
      @Transactional
      public void saveWithDish(SetmealDTO setmealDTO) {
          Setmeal setmeal=new Setmeal();
          BeanUtils.copyProperties(setmealDTO,setmeal);
  
          setmealMapper.insert(setmeal);
          Long setmealId=setmeal.getId();//插入套餐后获取对应id
  
          List<SetmealDish> setmealDishList=setmealDTO.getSetmealDishes();
          if(setmealDishList!=null&&setmealDishList.size()>0){
              setmealDishList.forEach(setmealDish -> {
                  setmealDish.setSetmealId(setmealId);
              });
              setmealDishMapper.insertBatch(setmealDishList);//循环向套餐菜品关系设置套餐id后批量插入
          }
  
  
  
      }
  
      /**
       * 套餐批量删除
       * @param ids
       */
      @Transactional
      public void deleteBatch(List<Long> ids) {
          //判断是否能删除
          //1.套餐是否在售
          for (Long id : ids) {
              Setmeal setmeal=setmealMapper.getById(id);
              if(setmeal.getStatus()== StatusConstant.ENABLE){
                  //在售
                  throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
              }
          }
  
          //2.删除套餐菜品关系表数据
          ids.forEach(setmealId -> {
              //删除套餐表中的数据
              setmealMapper.deleteById(setmealId);
              //删除套餐菜品关系表中的数据
              setmealDishMapper.deleteBySetmealId(setmealId);
          });
  
      }
  
      /**
       * 根据id查询套餐和其关联的菜品
       * @param id
       * @return
       */
      public SetmealVO getByIdWithDish(Long id) {
          Setmeal setmeal = setmealMapper.getById(id);
          List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
  
          SetmealVO setmealVO = new SetmealVO();
          BeanUtils.copyProperties(setmeal, setmealVO);
          setmealVO.setSetmealDishes(setmealDishes);
  
          return setmealVO;
      }
  
      /**
       * 修改套餐及其菜品
       * @param setmealDTO
       */
      public void updateWithDish(SetmealDTO setmealDTO) {
          Setmeal setmeal=new Setmeal();
          BeanUtils.copyProperties(setmealDTO, setmeal);
  
          //修改套餐表基本信息
          setmealMapper.update(setmeal);
  
          //删除原有的菜品数据
          setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
  
          //重新插入菜品数据
          List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
          if (setmealDishes != null && setmealDishes.size() > 0) {
              setmealDishes.forEach(setmealDish -> {
                  setmealDish.setSetmealId(setmealDTO.getId());
              });
              //向口味表插入n条数据
              setmealDishMapper.insertBatch(setmealDishes);
          }
      }
  
      /**
       * 起售停售套餐
       * @param status
       * @param id
       */
      @Transactional
      public void startOrStop(Integer status, Long id) {
  
          if (status == StatusConstant.ENABLE) {
              // 如果是起售操作，如果有不能起售的菜品则起售失败
              List<Dish> dishList = dishMapper.getBySetmealId(id);
              if(dishList != null && dishList.size() > 0){
                  dishList.forEach(dish -> {
                      if(StatusConstant.DISABLE == dish.getStatus()){
                          throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                      }
                  });
              }
          }
          Setmeal setmeal=Setmeal.builder().status(status).id(id).build();
          setmealMapper.update(setmeal);
      }
  
      /**
       * 条件查询
       * @param setmeal
       * @return
       */
      public List<Setmeal> list(Setmeal setmeal) {
          List<Setmeal> list = setmealMapper.list(setmeal);
          return list;
      }
  
      /**
       * 根据id查询菜品选项
       * @param id
       * @return
       */
      public List<DishItemVO> getDishItemById(Long id) {
          return setmealMapper.getDishItemBySetmealId(id);
      }
  
  
  }
  