package com.bonus.canteen.core.supply.purchase.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.menu.domain.BaseTreeNode;
import com.bonus.canteen.core.menu.enums.MenuSalesTypeEnum;
import com.bonus.canteen.core.menu.mapper.MenuDishesMapper;
import com.bonus.canteen.core.menu.mapper.MenuMaterialCategoryMapper;
import com.bonus.canteen.core.supply.purchase.domain.bo.MenuDishesMaterialModel;
import com.bonus.canteen.core.supply.purchase.domain.bo.MenuDishesSizeModel;
import com.bonus.canteen.core.utils.TreeNodeUtil;
import com.bonus.common.houqin.constant.DelFlagEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuDishesApi {
   private static final Logger log = LoggerFactory.getLogger(MenuDishesApi.class);
   @Autowired
   @Lazy
   private MenuDishesMapper menuDishesMapper;
   @Autowired
   @Lazy
   private MenuMaterialCategoryMapper menuMaterialCategoryMapper;

   public List<MenuDishesSizeModel> listDishesSizeModel() {
      return this.menuDishesMapper.listDishesSizeModel(MenuSalesTypeEnum.PORTION_ON.key(), DelFlagEnum.DEL_FALSE.key());
   }

   public List<MenuDishesMaterialModel> listDishesMaterial(List<Long> dishesIdList) {
      return this.menuDishesMapper.listDishesMaterial(dishesIdList, MenuSalesTypeEnum.PORTION_ON.key());
   }

   public List<Long> listAllCategoryIdById(List<Long> categoryIdList) {
      if (CollUtil.isEmpty(categoryIdList)) {
         return categoryIdList;
      } else {
         List<BaseTreeNode> categoryTreeVOList = this.menuMaterialCategoryMapper.listIdAndParentId();
         List<BaseTreeNode> childList = TreeNodeUtil.getChildList(categoryTreeVOList, categoryIdList);
         if (ObjectUtil.isNotEmpty(childList)) {
            assert childList != null;

            categoryIdList = childList.stream().map(BaseTreeNode::getId).collect(Collectors.toList());
         }

         return categoryIdList;
      }
   }

}
