package com.engine.organization.entity.browser.bo;

import com.api.hrm.bean.TreeNode;
import com.engine.organization.entity.browser.enums.TreeNodeTypeEnum;
import com.engine.organization.entity.browser.po.CusBrowserTree;
import org.apache.commons.collections.CollectionUtils;

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

/**
 * @author:dxfeng
 * @createTime: 2022/07/15
 * @version: 1.0
 */
public class CusBowserTreeBO {

    /**
     * 树转换
     *
     * @param searchTree
     * @return
     */
    public static List<CusBrowserTree> convertSearchTreeToBorwserTree(List<TreeNode> searchTree) {
        List<CusBrowserTree> collect = searchTree.stream().map(item ->
                CusBrowserTree.builder()
                        .domid("sel_" + item.getId())
                        .hasChild(CollectionUtils.isNotEmpty(item.getSubs()))
                        .isLeaf(CollectionUtils.isEmpty(item.getSubs()))
                        .isopen(false)
                        .key(getKey(item))
                        .name(item.getName())
                        .build()).collect(Collectors.toList());
        return collect;
    }

    private static String getKey(TreeNode treeNode) {
        if (TreeNodeTypeEnum.TYPE_COMP.getValue().equals(treeNode.getType())) {
            return TreeNodeTypeEnum.TYPE_COMP.getName() + "_" + treeNode.getId();
        } else if (TreeNodeTypeEnum.TYPE_DEPT.getValue().equals(treeNode.getType())) {
            return TreeNodeTypeEnum.TYPE_DEPT.getName() + "_" + treeNode.getId();
        }
        return treeNode.getId();
    }
}
