| | |
| | | import com.sinata.common.exception.ServiceException; |
| | | import com.sinata.common.utils.BeanUtils; |
| | | import com.sinata.common.utils.CollUtils; |
| | | import com.sinata.common.utils.DateUtils; |
| | | import com.sinata.common.utils.SecurityUtils; |
| | | import com.sinata.common.utils.StringUtils; |
| | | import com.sinata.common.utils.spring.SpringUtils; |
| | |
| | | import java.util.Arrays; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | |
| | | @Override |
| | | public void saveRole(SysRoleDTO dto) { |
| | | SysRole sysRole = BeanUtils.copyBean(dto, SysRole.class); |
| | | |
| | | if (!this.checkRoleNameUnique(sysRole)) { |
| | | throw new ServiceException("保存角色'" + sysRole.getRoleName() + "'失败,角色名称已存在"); |
| | | } |
| | | if (Objects.isNull(sysRole.getRoleId())) { |
| | | sysRole.setCreateBy(SecurityUtils.getUserId().toString()); |
| | | sysRole.setCreateTime(DateUtils.getNowDate()); |
| | | baseMapper.insert(sysRole); |
| | | } else { |
| | | // 删除角色与菜单关联 |
| | | roleMenuMapper.deleteRoleMenuByRoleId(sysRole.getRoleId()); |
| | | sysRole.setUpdateBy(SecurityUtils.getUserId().toString()); |
| | | sysRole.setUpdateTime(DateUtils.getNowDate()); |
| | | baseMapper.updateById(sysRole); |
| | | } |
| | | insertRoleMenu(sysRole); |
| | |
| | | return res; |
| | | } |
| | | |
| | | @Override |
| | | /*@Override |
| | | public List<SysMenu> getMenuLevelList(List<Long> menusId) { |
| | | //获取当前的权限菜单 |
| | | List<SysMenu> all = menuMapper.getAllInIds(menusId); |
| | |
| | | menu.setChildren(collect); |
| | | } |
| | | return s1; |
| | | }*/ |
| | | @Override |
| | | public List<SysMenu> getMenuLevelList(List<Long> menusId) { |
| | | //获取当前的权限菜单 |
| | | List<SysMenu> all = menuMapper.getAllInIds(menusId); |
| | | |
| | | // 第一级 |
| | | List<SysMenu> s1 = all.stream().filter(e -> e.getMenuType().equals("M")) |
| | | .collect(Collectors.toList()); |
| | | // 构建以 parentId 为键的 Map |
| | | Map<Long, List<SysMenu>> menuMap = all.stream() |
| | | .collect(Collectors.groupingBy(SysMenu::getParentId)); |
| | | //封装树形层级 |
| | | buildMenuTree(menuMap, s1); |
| | | return s1; |
| | | } |
| | | |
| | | /** |
| | | * 封装树形层级 |
| | | * |
| | | * @param menuMap |
| | | * @param s1 |
| | | */ |
| | | public void buildMenuTree(Map<Long, List<SysMenu>> menuMap, List<SysMenu> s1) { |
| | | for (SysMenu sysMenu : s1) { |
| | | // 获取当前菜单的子级 |
| | | List<SysMenu> children = menuMap.getOrDefault(sysMenu.getMenuId(), new ArrayList<>()); |
| | | sysMenu.setChildren(children); |
| | | //移除当前菜单 |
| | | menuMap.remove(sysMenu.getMenuId()); |
| | | // 递归设置子级菜单 |
| | | buildMenuTree(menuMap, children); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | List<SysRole> list = list(); |
| | | return BeanUtils.copyToList(list, SysRoleVO.class); |
| | | } |
| | | |
| | | /** |
| | | * 用户菜单权限集合 |
| | | * |
| | | * @param userId |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Set<Long> getMenuIdSets(Long userId) { |
| | | SysRole sysRole = roleMapper.selectRoleByUserId(userId); |
| | | // 获取当前角色的菜单列表 |
| | | List<SysMenu> menus = menuMapper.selectListByRoleId(sysRole.getRoleId()); |
| | | return menus.stream().map(SysMenu::getMenuId).collect(Collectors.toSet()); |
| | | } |
| | | } |