package com.search.supersearch.service.mysql.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.search.supersearch.domain.IamDataCategory;
import com.search.supersearch.mapper.mysql.IamDataCategoryMapper;
import com.search.supersearch.model.vo.IamDataCategoryVO;
import com.search.supersearch.service.mysql.IamDataCategoryService;
import org.springframework.stereotype.Service;

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

/**
 *
 */
@Service
public class IamDataCategoryServiceImpl extends ServiceImpl<IamDataCategoryMapper, IamDataCategory>
        implements IamDataCategoryService {

    /**
     * 获取菜单列表
     *
     * @param level@return {@link List <  IamDataCategoryVO >}
     * @author 王瑞
     * @date 2023/10/17
     */
    @Override
    public List<IamDataCategoryVO> getCategory(int level) {
        List<IamDataCategory> list = this.lambdaQuery().orderByAsc(IamDataCategory::getWeightNum).orderByAsc(IamDataCategory::getSortNum).list();
        List<IamDataCategoryVO> result = list.stream()
                .filter(v -> v.getLevel() == 1)
                .sorted(Comparator.comparing(IamDataCategory::getSortNum))
                .map(v -> getIamDataCategoryVO(list, v))
                .collect(Collectors.toList());
        return result;
    }

    private IamDataCategoryVO getIamDataCategoryVO(List<IamDataCategory> list, IamDataCategory v) {
        IamDataCategoryVO vo = BeanUtil.copyProperties(v, IamDataCategoryVO.class);
        List<IamDataCategory> collect = list.stream().filter(v2 -> v2.getParentId().equals(v.getId())).sorted(Comparator.comparing(IamDataCategory::getSortNum)).collect(Collectors.toList());
        for (IamDataCategory iamDataCategory : collect) {
            getIamDataCategoryVO(list, iamDataCategory);
        }
        vo.setChildData(collect);
        return vo;
    }
}





