| | |
| | | package com.sinata.system.service.impl; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.sinata.common.annotation.DataScope; |
| | | import com.sinata.common.constant.UserConstants; |
| | | import com.sinata.common.core.domain.entity.SysMenu; |
| | | import com.sinata.common.core.domain.entity.SysRole; |
| | | import com.sinata.common.core.domain.entity.SysUser; |
| | | import com.sinata.common.core.domain.model.LoginUser; |
| | | import com.sinata.common.entity.PageDTO; |
| | | 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 com.sinata.system.domain.SysRoleDept; |
| | | import com.sinata.system.domain.SysRoleMenu; |
| | | import com.sinata.system.domain.SysUserRole; |
| | | import com.sinata.system.domain.dto.SysRoleDTO; |
| | | import com.sinata.system.domain.query.KeyWordQuery; |
| | | import com.sinata.system.domain.vo.SysRoleVO; |
| | | import com.sinata.system.mapper.SysMenuMapper; |
| | | import com.sinata.system.mapper.SysRoleDeptMapper; |
| | | import com.sinata.system.mapper.SysRoleMapper; |
| | | import com.sinata.system.mapper.SysRoleMenuMapper; |
| | | import com.sinata.system.mapper.SysUserRoleMapper; |
| | | import com.sinata.system.service.ISysMenuService; |
| | | import com.sinata.system.service.ISysRoleService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.ArrayList; |
| | | 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; |
| | | |
| | | /** |
| | | * 角色 业务层处理 |
| | |
| | | * @author ruoyi |
| | | */ |
| | | @Service |
| | | public class SysRoleServiceImpl implements ISysRoleService |
| | | public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService |
| | | { |
| | | @Autowired |
| | | private SysRoleMapper roleMapper; |
| | |
| | | |
| | | @Autowired |
| | | private SysRoleDeptMapper roleDeptMapper; |
| | | @Autowired |
| | | private ISysMenuService menuService; |
| | | @Autowired |
| | | private SysMenuMapper menuMapper; |
| | | |
| | | /** |
| | | * 根据条件分页查询角色数据 |
| | |
| | | } |
| | | return userRoleMapper.batchUserRole(list); |
| | | } |
| | | |
| | | /** |
| | | * 角色分页列表 |
| | | * |
| | | * @param query |
| | | * @return |
| | | */ |
| | | @Override |
| | | public PageDTO<SysRoleVO> pageList(KeyWordQuery query) { |
| | | Page<SysRoleVO> page = baseMapper.pageList(new Page<SysRoleVO>(query.getPageCurr(), query.getPageSize()), query.getKeyword()); |
| | | return PageDTO.of(page); |
| | | } |
| | | |
| | | /** |
| | | * 保存角色信息 |
| | | * |
| | | * @param dto |
| | | * @return |
| | | */ |
| | | @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); |
| | | } |
| | | |
| | | /** |
| | | * 获取角色详情 |
| | | * |
| | | * @param roleId |
| | | * @return |
| | | */ |
| | | @Override |
| | | public SysRoleVO getDetailInfo(Long roleId) { |
| | | SysRole sysRole = baseMapper.selectRoleById(roleId); |
| | | SysRoleVO sysRoleVO = BeanUtils.copyBean(sysRole, SysRoleVO.class); |
| | | //查询用户菜单id列表 |
| | | List<SysMenu> sysMenus = menuService.selectListByRoleId(roleId); |
| | | if (CollUtils.isNotEmpty(sysMenus)) { |
| | | sysRoleVO.setMenuIds(sysMenus.stream().map(SysMenu::getMenuId).collect(Collectors.toList())); |
| | | } |
| | | return sysRoleVO; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前登录用户可管理角色列表 |
| | | * |
| | | * @param loginUser |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<SysRoleVO> getManageRoleList(LoginUser loginUser) { |
| | | List<SysRoleVO> res = CollUtils.emptyList(); |
| | | Long userId = loginUser.getUserId(); |
| | | SysUserRole sysUserRole = userRoleMapper.selectOne(Wrappers.lambdaQuery(SysUserRole.class).eq(SysUserRole::getUserId, userId).last("LIMIT 1")); |
| | | if (Objects.nonNull(sysUserRole)) { |
| | | SysRole role = this.getById(sysUserRole.getRoleId()); |
| | | if (Objects.nonNull(role)) { |
| | | List<SysRole> list = this.lambdaQuery().in(SysRole::getRoleId, Arrays.asList(role.getManageRoleIdStr().split(","))).list(); |
| | | if (CollUtils.isNotEmpty(list)) { |
| | | res = BeanUtils.copyToList(list, SysRoleVO.class); |
| | | } |
| | | } |
| | | } |
| | | return res; |
| | | } |
| | | |
| | | /*@Override |
| | | public List<SysMenu> getMenuLevelList(List<Long> menusId) { |
| | | //获取当前的权限菜单 |
| | | List<SysMenu> all = menuMapper.getAllInIds(menusId); |
| | | // 第三级 |
| | | List<SysMenu> s3 = all.stream().filter(e -> e.getMenuType().equals("F")) |
| | | .collect(Collectors.toList()); |
| | | // 第二级 |
| | | List<SysMenu> s2 = all.stream().filter(e -> e.getMenuType().equals("C")) |
| | | .collect(Collectors.toList()); |
| | | // 第一级 |
| | | List<SysMenu> s1 = all.stream().filter(e -> e.getMenuType().equals("M")) |
| | | .collect(Collectors.toList()); |
| | | |
| | | for (SysMenu menu : s2) { |
| | | List<SysMenu> collect = s3.stream() |
| | | .filter(e -> e.getParentId().equals(menu.getMenuId())) |
| | | .collect(Collectors.toList()); |
| | | menu.setChildren(collect); |
| | | } |
| | | for (SysMenu menu : s1) { |
| | | List<SysMenu> collect = s2.stream() |
| | | .filter(e -> e.getParentId().equals(menu.getMenuId())) |
| | | .collect(Collectors.toList()); |
| | | 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 |
| | | public List<SysMenu> roleInfoFromUserId(Long userId) { |
| | | SysRole sysRole = roleMapper.selectRoleByUserId(userId); |
| | | // 获取当前角色的菜单列表 |
| | | List<SysMenu> menus = menuMapper.selectListByRoleId(sysRole.getRoleId()); |
| | | if (menus.size() == 0) { |
| | | return new ArrayList<>(); |
| | | } |
| | | List<Long> menusId = menus.stream().map(SysMenu::getMenuId).collect(Collectors.toList()); |
| | | // 获取当前的权限菜单(有层级) |
| | | return this.getMenuLevelList(menusId); |
| | | } |
| | | |
| | | /** |
| | | * 角色列表 |
| | | * |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<SysRoleVO> queryList() { |
| | | 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()); |
| | | } |
| | | } |