package com.bonus.canteen.core.menu.service.impl.sys;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.common.utils.TreeNodeUtil;
import com.bonus.canteen.core.data.dataset.Executors;
import com.bonus.canteen.core.menu.entity.SysMenuMaterialCategory;
import com.bonus.canteen.core.menu.mapper.SysMenuMaterialCategoryMapper;
import com.bonus.canteen.core.menu.vo.MenuCategoryTreeVO;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SysMenuMaterialCategoryServiceImpl extends ServiceImpl<SysMenuMaterialCategoryMapper, SysMenuMaterialCategory> {
   private static final Logger log = LoggerFactory.getLogger(SysMenuMaterialCategoryServiceImpl.class);

   public List<MenuCategoryTreeVO> getCategoryTreeList() {
      List<MenuCategoryTreeVO> sysMenuMaterialCategories = Executors.readInSystem(() -> {
         return this.list(Wrappers.emptyWrapper()).stream().map((x) -> {
            MenuCategoryTreeVO menuCategoryTreeVO = BeanUtil.copyProperties(x, MenuCategoryTreeVO.class, new String[0]);
            menuCategoryTreeVO.setId(x.getCategoryId());
            return menuCategoryTreeVO;
         }).collect(Collectors.toList());
      });
      return TreeNodeUtil.assembleTree(sysMenuMaterialCategories);
   }

   public Map<Long, String> getCategoryIdMap(Set<Long> categoryIds) {
      return (ObjectUtils.isEmpty(categoryIds) ? new HashMap<>() : Executors.readInSystem(() -> {
         return this.list(Wrappers.emptyWrapper()).stream()
                 .collect(Collectors.toMap(SysMenuMaterialCategory::getCategoryId, SysMenuMaterialCategory::getCategoryName));
      }));
   }
}
