All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.addplus.server.security.service.utils.MenuFunctionTreeUtils Maven / Gradle / Ivy

The newest version!
package com.addplus.server.security.service.utils;

import com.addplus.server.core.model.authority.data.SysMenuFunction;
import com.addplus.server.core.model.authority.datatransfer.SysMenuFunctionUserDTO;
import com.addplus.server.core.model.authority.business.SysMenuTreeBO;
import org.springframework.beans.BeanUtils;

import java.util.*;

/**
 * 类名: MenuFunctionTreeUtils
 *
 * @author zhangjiehang
 * @version V1.0
 * @date 2017/12/5 下午3:29
 * @description 类描述: 菜单树形工具类
 */
public class MenuFunctionTreeUtils {

    /**
     * 方法描述:后置遍历菜单树形,从最低层向上进行遍历。
     *
     * @param sysMenuFunctions
     * @return
     * @throws Exception
     * @author zhangjiehang
     * @date 2017/12/5 下午5:20
     */
    public static List getMenuFunctionTree(List sysMenuFunctions) {
        if (sysMenuFunctions != null && !sysMenuFunctions.isEmpty()) {
            List menuList = new ArrayList<>();
            HashMap hashMap = new HashMap<>();
            SysMenuFunctionUserDTO menuFunctionUser = null;
            for (SysMenuFunction sysMenuFunction : sysMenuFunctions) {
                menuFunctionUser = new SysMenuFunctionUserDTO();
                BeanUtils.copyProperties(sysMenuFunction, menuFunctionUser, SysMenuFunction.class);
                hashMap.put(sysMenuFunction.getId(), menuFunctionUser);
                menuList.add(sysMenuFunction.getId());
            }
            HashMap hashMapAll= new HashMap<>();
            hashMapAll.putAll(hashMap);
            for (Long num : menuList) {
                menuFunctionUser = hashMap.get(num);
                if (hashMapAll.containsKey(menuFunctionUser.getPid())) {
                    SysMenuFunctionUserDTO menuFunctionUserFather = hashMapAll.get(menuFunctionUser.getPid());
                    List functionChirldrenList = null;
                    if (menuFunctionUserFather.getMenuFunctionChilders() != null && !menuFunctionUserFather.getMenuFunctionChilders().isEmpty()) {
                        functionChirldrenList = menuFunctionUserFather.getMenuFunctionChilders();
                    } else {
                        functionChirldrenList = new ArrayList();
                    }
                    functionChirldrenList.add(menuFunctionUser);
                    menuFunctionUserFather.setMenuFunctionChilders(functionChirldrenList);
                    hashMap.put(menuFunctionUserFather.getId(), menuFunctionUserFather);
                    hashMap.remove(menuFunctionUser.getId());
                }
            }
            List menuTree = new ArrayList();
            for (Map.Entry o : hashMap.entrySet()) {
                menuTree.add(o.getValue());
            }
            if (!menuTree.isEmpty()) {
                Collections.sort(menuTree);
                //进行排序
                for (SysMenuFunctionUserDTO functionUser : menuTree) {
                    List menuFunctionChilders = functionUser.getMenuFunctionChilders();
                    if (menuFunctionChilders != null && !menuFunctionChilders.isEmpty()) {
                        Collections.sort(menuFunctionChilders);
                    }
                }
                return menuTree;
            }
        }
        return null;
    }

    public static List getSysMenuTreeBO(List sysMenuFunctions) {
        if (sysMenuFunctions != null && !sysMenuFunctions.isEmpty()) {
            List menuList = new ArrayList<>();
            HashMap hashMap = new HashMap<>();
            SysMenuTreeBO sysMenuTree = null;
            for (SysMenuFunction sysMenuFunction : sysMenuFunctions) {
                sysMenuTree = new SysMenuTreeBO();
                sysMenuTree.setKey(sysMenuFunction.getId());
                sysMenuTree.setPid(sysMenuFunction.getPid());
                sysMenuTree.setTitle(sysMenuFunction.getName());
                if (sysMenuFunction.getDept()==1){
                    sysMenuTree.setIsLeaf(false);
                }else {
                    sysMenuTree.setIsLeaf(true);
                }
                sysMenuTree.setSort(sysMenuFunction.getSort());
                hashMap.put(sysMenuFunction.getId(), sysMenuTree);
                menuList.add(sysMenuFunction.getId());
            }
            HashMap hashMapAll= new HashMap<>();
            hashMapAll.putAll(hashMap);
            for (Long num : menuList) {
                sysMenuTree = hashMap.get(num);
                if (hashMapAll.containsKey(sysMenuTree.getPid())) {
                    SysMenuTreeBO sysMenuTreeFather = hashMapAll.get(sysMenuTree.getPid());
                    List functionChirldrenList = null;
                    if (sysMenuTreeFather.getChildren() != null && !sysMenuTreeFather.getChildren().isEmpty()) {
                        functionChirldrenList = sysMenuTreeFather.getChildren();
                    } else {
                        functionChirldrenList = new ArrayList();
                    }
                    functionChirldrenList.add(sysMenuTree);
                    sysMenuTreeFather.setChildren(functionChirldrenList);
                    hashMap.put(sysMenuTreeFather.getKey(), sysMenuTreeFather);
                    hashMap.remove(sysMenuTree.getKey());
                }
            }
            List menuTree = new ArrayList();
            for (Map.Entry o : hashMap.entrySet()) {
                menuTree.add(o.getValue());
            }
            if (!menuTree.isEmpty()) {
                Collections.sort(menuTree);
                //进行排序
                for (SysMenuTreeBO functionUser : menuTree) {
                    List menuFunctionChilders = functionUser.getChildren();
                    if (menuFunctionChilders != null && !menuFunctionChilders.isEmpty()) {
                        Collections.sort(menuFunctionChilders);
                    }
                }
                return menuTree;
            }
        }
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy