| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | |
| | | public List<SysMenu> roleInfoFromUserId(Long userId) { |
| | | SysRole sysRole = roleMapper.selectRoleByUserId(userId); |
| | | // 获取当前角色的菜单列表 |
| | | List<SysMenu> menus = menuMapper.selectListByRoleId(sysRole.getRoleId()); |
| | | if(menus.size()==0){ |
| | | List<SysRoleMenu> roleMenus = menuMapper.selectRoleMenuByRoleId(sysRole.getRoleId()); |
| | | if(CollectionUtils.isEmpty(roleMenus)){ |
| | | return new ArrayList<>(); |
| | | } |
| | | List<Long> menusId = menus.stream().map(SysMenu::getMenuId).collect(Collectors.toList()); |
| | | List<Long> menusId = roleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList()); |
| | | // 获取当前的权限菜单(有层级) |
| | | return this.getMenuLevelList(menusId); |
| | | return this.getResourcesLevelList(menusId); |
| | | } |
| | | |
| | | public List<SysMenu> getResourcesLevelList(List<Long> menusId) { |
| | | //获取当前的权限菜单 |
| | | List<SysMenu> allResources = menuMapper.getAllInIds(menusId); |
| | | SysMenu item; |
| | | //获取根节点的集合 |
| | | List<SysMenu> root = new ArrayList<>(); |
| | | Map<Long, SysMenu> tempMap = new HashMap<>(3); |
| | | //子节点集合 |
| | | SysMenu parent; |
| | | for (SysMenu resources : allResources) { |
| | | item = com.ruoyi.system.utils.util.SpringUtils.beanCopy(resources, SysMenu.class); |
| | | //每次循环都将集合重新循环添加到父集合中 |
| | | tempMap.put(resources.getMenuId(), item); |
| | | parent = tempMap.get(resources.getParentId()); |
| | | if (Objects.nonNull(parent)) { |
| | | //父集合不为空时,获取下级集合 |
| | | //子节点集合 |
| | | parent.getChildren().add(item); |
| | | // 对子集排序,升序 |
| | | // parent.getChildren().sort(comparing(SysMenu::getSort)); |
| | | } else { |
| | | //根集合 |
| | | root.add(item); |
| | | // 对根集合排序,升序 |
| | | // root.sort(comparing(SysMenu::getSort)); |
| | | } |
| | | } |
| | | return root; |
| | | } |
| | | |
| | | @Override |