package com.sysu.admin.support.system.user;

import java.util.ArrayList;
import java.util.List;

public class UserTreeNodeUtil {

    public static List<UserTreeNode> UserToTreeNode(List<User> userList){
        List<UserTreeNode> result = new ArrayList<UserTreeNode>(userList.size() + (userList.size() >> 1));
        List<String> organIdList = new ArrayList<>(userList.size() >> 1);
        for(User user : userList){
            String pId = "o_" + user.getOrganId();
            if(!organIdList.contains(pId)){
                organIdList.add(pId);
                UserTreeNode userTreeNode = new UserTreeNode();
                userTreeNode.setId(pId);
                userTreeNode.setCompany(user.getOrganName());
                result.add(userTreeNode);
            }
            UserTreeNode userTreeNode = new UserTreeNode();
            userTreeNode.setId(user.getId().toString());
            userTreeNode.setName(user.getUsername());
            userTreeNode.setParent(pId);
            userTreeNode.setRoleName(user.getRoleName());
            userTreeNode.setIsEnable(user.getIsEnable());
            userTreeNode.setCreateDate(user.getCreateDate());
            result.add(userTreeNode);
        }
        return result;
    }

}
