|
|
@@ -1,23 +1,32 @@
|
|
|
package cn.tr.module.sys.user.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.tr.core.exception.ServiceException;
|
|
|
import cn.tr.core.exception.TRExcCode;
|
|
|
+import cn.tr.module.sys.mapper.user.SysMenuMapper;
|
|
|
import cn.tr.module.sys.mapper.user.SysRoleMapper;
|
|
|
+import cn.tr.module.sys.user.constant.MenuConstants;
|
|
|
+import cn.tr.module.sys.user.dto.SysMenuDTO;
|
|
|
import cn.tr.module.sys.user.dto.SysRoleDTO;
|
|
|
import cn.tr.module.sys.user.dto.SysRoleQueryDTO;
|
|
|
+import cn.tr.module.sys.user.enums.MenuEnum;
|
|
|
import cn.tr.module.sys.user.po.SysRolePO;
|
|
|
import cn.tr.module.sys.user.repository.SysRoleRepository;
|
|
|
+import cn.tr.module.sys.user.service.ISysMenuService;
|
|
|
import cn.tr.module.sys.user.service.ISysRoleService;
|
|
|
+import cn.tr.module.sys.user.service.ISysUserRoleService;
|
|
|
+import cn.tr.module.sys.user.vo.RouteItemVO;
|
|
|
+import cn.tr.module.sys.user.vo.RouteMetoVO;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import lombok.AllArgsConstructor;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.util.Collection;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Optional;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @ClassName : SysRoleServiceImpl
|
|
|
@@ -26,9 +35,20 @@ import java.util.Optional;
|
|
|
* @Date: 2023年03月31日
|
|
|
*/
|
|
|
@Service
|
|
|
-@AllArgsConstructor
|
|
|
public class SysRoleServiceImpl implements ISysRoleService {
|
|
|
- private final SysRoleRepository roleRepository;
|
|
|
+ @Autowired
|
|
|
+ private SysRoleRepository roleRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @Lazy
|
|
|
+ private ISysMenuService menuService;
|
|
|
+ @Autowired
|
|
|
+ @Lazy
|
|
|
+ private ISysUserRoleService userRoleService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @Lazy
|
|
|
+ private ISysRoleService self;
|
|
|
@Override
|
|
|
public List<SysRoleDTO> selectSysRoleList(SysRoleQueryDTO query) {
|
|
|
return SysRoleMapper.INSTANCE.toSysRoleDTOList(roleRepository.selectList(new LambdaQueryWrapper<SysRolePO>()
|
|
|
@@ -38,6 +58,33 @@ public class SysRoleServiceImpl implements ISysRoleService {
|
|
|
));
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Set<SysMenuDTO> findUserMenus(String userId){
|
|
|
+ List<SysRoleDTO> roles = self.findAllRolesByUserId(userId);
|
|
|
+ if (CollectionUtil.isEmpty(roles)) {
|
|
|
+ return new HashSet<>();
|
|
|
+ }
|
|
|
+ return roles.stream()
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .filter(role->Boolean.FALSE.equals(role.getDisable()))
|
|
|
+ .map(SysRoleDTO::getId)
|
|
|
+ .map(menuService::findAllMenuByRoleId)
|
|
|
+ .flatMap(Collection::stream)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<RouteItemVO> findUserRouteMenus(String userId) {
|
|
|
+ Set<SysMenuDTO> userMenus = findUserMenus(userId);
|
|
|
+ return userMenus.stream()
|
|
|
+ .filter(menu->!StrUtil.equals(MenuEnum.button.name(),menu.getMenuType()))
|
|
|
+ .map(this::convertToRoute)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ ;
|
|
|
+
|
|
|
@Override
|
|
|
public SysRoleDTO selectSysRoleById(String id) {
|
|
|
return SysRoleMapper.INSTANCE.toSysRoleDTO(roleRepository.selectById(id));
|
|
|
@@ -47,6 +94,8 @@ public class SysRoleServiceImpl implements ISysRoleService {
|
|
|
public boolean updateSysRoleById(SysRoleDTO source) {
|
|
|
SysRolePO po = SysRoleMapper.INSTANCE.toSysRolePO(source);
|
|
|
validateRoleSource(po);
|
|
|
+ Set<String> userIds = userRoleService.findUserByRoleId(source.getId());
|
|
|
+ userIds.parallelStream().forEach(self::delUserRoleCache);
|
|
|
return roleRepository.updateById(po)!=0;
|
|
|
}
|
|
|
|
|
|
@@ -60,9 +109,14 @@ public class SysRoleServiceImpl implements ISysRoleService {
|
|
|
|
|
|
@Override
|
|
|
public int deleteSysRoleByIds(Collection<String> ids) {
|
|
|
- //todo 校验
|
|
|
- List<SysRolePO> role = roleRepository.selectBatchIds(ids);
|
|
|
- Optional<SysRolePO> adminRole = role.stream()
|
|
|
+ List<SysRolePO> roles = roleRepository.selectBatchIds(ids);
|
|
|
+ for (SysRolePO role : roles) {
|
|
|
+ Set<String> userIds = userRoleService.findUserByRoleId(role.getId());
|
|
|
+ if(CollectionUtil.isNotEmpty(userIds)){
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,String.format("角色编码{%s}已被使用,无法删除",role.getRoleCode()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Optional<SysRolePO> adminRole = roles.stream()
|
|
|
.filter(po->Boolean.TRUE.equals(po.getAdmin()))
|
|
|
.findFirst();
|
|
|
if(adminRole.isPresent()){
|
|
|
@@ -71,6 +125,11 @@ public class SysRoleServiceImpl implements ISysRoleService {
|
|
|
return roleRepository.deleteBatchIds(ids);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public List<SysRoleDTO> findAllRolesByUserId(String userId) {
|
|
|
+ return SysRoleMapper.INSTANCE.toSysRoleDTOList(roleRepository.findAllRoleByUserId(userId));
|
|
|
+ }
|
|
|
+
|
|
|
private void validateRoleSource(SysRolePO source){
|
|
|
SysRolePO role = roleRepository.selectOne(new LambdaQueryWrapper<SysRolePO>()
|
|
|
.eq(SysRolePO::getRoleName, source.getRoleName())
|
|
|
@@ -90,4 +149,39 @@ public class SysRoleServiceImpl implements ISysRoleService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ private RouteItemVO convertToRoute(SysMenuDTO item) {
|
|
|
+ RouteItemVO node = SysMenuMapper.INSTANCE.toRouteItemVO(item);
|
|
|
+ node.setPath(item.getRoutePath());
|
|
|
+ node.setName(item.getRoutePath());
|
|
|
+ // 一级目录
|
|
|
+ if (Objects.equals(item.getMenuType(), MenuEnum.dir.name()) && (StrUtil.equals(item.getParentId(),"0")||StrUtil.isEmpty(item.getParentId()))) {
|
|
|
+ node.setPath("/" + item.getRoutePath());
|
|
|
+ node.setComponent(MenuConstants.LAYOUT);
|
|
|
+ }
|
|
|
+ // 外部链接
|
|
|
+ if (Objects.equals(item.getMenuType(), MenuEnum.menu.name()) && Boolean.TRUE.equals(item.getLinkExternal())) {
|
|
|
+ node.setComponent(MenuConstants.IFRAME);
|
|
|
+ }
|
|
|
+ RouteMetoVO routeMetoVO = new RouteMetoVO();
|
|
|
+ routeMetoVO.setTitle(item.getName());
|
|
|
+ routeMetoVO.setIcon(item.getIcon());
|
|
|
+ routeMetoVO.setHideMenu(!Boolean.TRUE.equals(item.getVisible()));
|
|
|
+ // 菜单
|
|
|
+ if (Objects.equals(item.getMenuType(), MenuEnum.menu.name())) {
|
|
|
+ routeMetoVO.setIgnoreKeepAlive(!Boolean.TRUE.equals(item.getKeepalive()));
|
|
|
+ }
|
|
|
+ // 外部链接
|
|
|
+ if (Objects.equals(item.getMenuType(), MenuEnum.menu.name()) && Boolean.TRUE.equals(item.getLinkExternal())) {
|
|
|
+ // 内嵌
|
|
|
+ if (Boolean.TRUE.equals(item.getFrame())) {
|
|
|
+ routeMetoVO.setFrameSrc(item.getLinkUrl());
|
|
|
+ }else {
|
|
|
+ node.setPath(item.getLinkUrl());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ node.setMeta(routeMetoVO);
|
|
|
+ return node;
|
|
|
+ }
|
|
|
}
|