补充根据身份id类型批量查询接口

This commit is contained in:
陈维伟 2023-09-11 11:31:22 +08:00
parent 34bc1fc106
commit 81ab60e44f
5 changed files with 168 additions and 112 deletions

View File

@ -1,6 +1,7 @@
package cn.axzo.tyr.client.feign;
import cn.axzo.framework.domain.web.result.ApiResult;
import cn.axzo.tyr.client.model.req.QueryByIdentityIdTypeReq;
import cn.axzo.tyr.client.model.req.QuerySaasRoleReq;
import cn.axzo.tyr.client.model.vo.SaasRoleVO;
import org.springframework.cloud.openfeign.FeignClient;
@ -10,6 +11,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.Map;
/**
* 角色
@ -39,10 +41,20 @@ public interface SaasRoleApi {
ApiResult<List<SaasRoleVO>> query(@RequestBody QuerySaasRoleReq req);
/**
* 根据身份id 身份类型查询权限列表
* 根据身份id身份类型查询权限列表批量
* @param identityType 身份类型 1:工人 2:班组长 3:从业人员 4:监管人员 5:运营人员
* @return
*/
@GetMapping("/api/saasRole/queryByIdentityIdType")
ApiResult<List<SaasRoleVO>> queryByIdentityIdType(@RequestParam(required = true) Long identityId,@RequestParam(required = true) Integer identityType,@RequestParam(required = true) Long workspaceId,@RequestParam(required = true) Long ouId);
/**
* 根据身份id身份类型查询权限列表批量
* @return
*/
@GetMapping("/api/saasRole/queryBatchByIdentityIdType")
ApiResult<Map<QueryByIdentityIdTypeReq, List<SaasRoleVO>>> queryBatchByIdentityIdType(@RequestBody List<QueryByIdentityIdTypeReq> req);
}

View File

@ -0,0 +1,22 @@
package cn.axzo.tyr.client.model.req;
import lombok.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@EqualsAndHashCode
public class QueryByIdentityIdTypeReq {
Long identityId;
/**
* 身份类型 1:工人 2:班组长 3:从业人员 4:监管人员 5:运营人员
*/
Integer identityType;
Long workspaceId;
Long ouId;
}

View File

@ -2,6 +2,7 @@ package cn.axzo.tyr.server.controller.role;
import cn.axzo.framework.domain.web.result.ApiResult;
import cn.axzo.tyr.client.feign.SaasRoleApi;
import cn.axzo.tyr.client.model.req.QueryByIdentityIdTypeReq;
import cn.axzo.tyr.client.model.req.QuerySaasRoleReq;
import cn.axzo.tyr.client.model.vo.SaasRoleVO;
import cn.axzo.tyr.server.service.RoleService;
@ -11,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
/**
* 权限
@ -46,4 +48,10 @@ public class SaasRoleController implements SaasRoleApi {
public ApiResult<List<SaasRoleVO>> queryByIdentityIdType(Long identityId, Integer identityType,Long workspaceId,Long ouId) {
return ApiResult.ok(roleService.queryByIdentityIdType(identityId, identityType,workspaceId,ouId));
}
@Override
public ApiResult<Map<QueryByIdentityIdTypeReq, List<SaasRoleVO>>> queryBatchByIdentityIdType(List<QueryByIdentityIdTypeReq> req) {
return ApiResult.ok(roleService.queryBatchByIdentityIdType(req));
}
}

View File

@ -1,10 +1,11 @@
package cn.axzo.tyr.server.service;
import cn.axzo.framework.domain.web.result.ApiListResult;
import cn.axzo.tyr.client.model.req.QueryByIdentityIdTypeReq;
import cn.axzo.tyr.client.model.req.QuerySaasRoleReq;
import cn.axzo.tyr.client.model.vo.SaasRoleVO;
import java.util.List;
import java.util.Map;
/**
* 角色
@ -19,4 +20,6 @@ public interface RoleService {
List<SaasRoleVO> queryByIdentityIdType(Long identityId, Integer identityType,Long workspaceId,Long ouId);
List<SaasRoleVO> query(QuerySaasRoleReq req);
Map<QueryByIdentityIdTypeReq, List<SaasRoleVO>> queryBatchByIdentityIdType(List<QueryByIdentityIdTypeReq> req);
}

View File

@ -2,6 +2,7 @@ package cn.axzo.tyr.server.service.impl;
import cn.axzo.basics.common.constant.enums.TableIsDeleteEnum;
import cn.axzo.pokonyan.config.mybatisplus.BaseEntity;
import cn.axzo.tyr.client.model.req.QueryByIdentityIdTypeReq;
import cn.axzo.tyr.client.model.req.QuerySaasPermissionGroupReq;
import cn.axzo.tyr.client.model.req.QuerySaasRoleGroupReq;
import cn.axzo.tyr.client.model.req.QuerySaasRoleReq;
@ -34,118 +35,128 @@ import java.util.stream.Collectors;
@Service
public class RoleServiceImpl implements RoleService {
@Autowired
SaasRoleUserRelationDao roleUserRelationDao;
@Autowired
SaasRoleDao saasRoleDao;
@Autowired
PermissionGroupService permissionGroupService;
@Autowired
SaasPgroupRoleRelationDao permissionGroupRelation;
@Autowired
SaasRoleGroupDao saasRoleGroupDao;
@Autowired
SaasRoleGroupRelationDao roleGroupRelationDao;
@Autowired
SaasRoleUserRelationDao roleUserRelationDao;
@Autowired
SaasRoleDao saasRoleDao;
@Autowired
PermissionGroupService permissionGroupService;
@Autowired
SaasPgroupRoleRelationDao permissionGroupRelation;
@Autowired
SaasRoleGroupDao saasRoleGroupDao;
@Autowired
SaasRoleGroupRelationDao roleGroupRelationDao;
@Override
public List<SaasRoleVO> queryByIdentityIdType(Long identityId, Integer identityType,Long workspaceId,Long ouId) {
// 查询人关联的角色id
List<Long> roleIds = roleUserRelationDao.query(identityId, identityType,workspaceId,ouId).stream().map(SaasRoleUserRelation::getRoleId).collect(Collectors.toList());
if (CollectionUtils.isEmpty(roleIds)) {
return new ArrayList<>();
}
return getByIds(roleIds);
}
@Override
public List<SaasRoleVO> queryByIdentityIdType(Long identityId, Integer identityType, Long workspaceId, Long ouId) {
// 查询人关联的角色id
List<Long> roleIds = roleUserRelationDao.query(identityId, identityType, workspaceId, ouId).stream().map(SaasRoleUserRelation::getRoleId).collect(Collectors.toList());
if (CollectionUtils.isEmpty(roleIds)) {
return new ArrayList<>();
}
return getByIds(roleIds);
}
/**
* 根据id查询权限详情包含权限组合权限集(底层基础方法)
* @return
*/
public List<SaasRoleVO> getByIds(List<Long> roleIds) {
if (CollectionUtils.isEmpty(roleIds)) {
return new ArrayList<>();
}
// 查询角色信息
List<SaasRole> roles = saasRoleDao.lambdaQuery().in(BaseEntity::getId, roleIds).list();
// 查询权限集关联关系
List<SaasPgroupRoleRelation> saasPgroupRoleRelations = queryPermissionGroupRelation(roleIds);
// 查询权限集
Map<Long, List<SaasPgroupRoleRelation>> pgrouRelationMap = null;
Map<Long, List<SaasPermissionGroupVO>> pGroupMap = null;
if (CollectionUtils.isNotEmpty(saasPgroupRoleRelations)) {
// 转map<roleId,relation>
pgrouRelationMap = saasPgroupRoleRelations.stream().collect(Collectors.groupingBy(SaasPgroupRoleRelation::getRoleId));
// 查询权限集
pGroupMap = permissionGroupService.query(QuerySaasPermissionGroupReq.builder()
.ids(saasPgroupRoleRelations.stream().map(SaasPgroupRoleRelation::getGroupId).collect(Collectors.toList()))
.build())
// 转map<pgroupId>
.stream().collect(Collectors.groupingBy(SaasPermissionGroupVO::getId));
}
Map<Long, List<SaasPgroupRoleRelation>> finalPgrouRelationMap = pgrouRelationMap;
Map<Long, List<SaasPermissionGroupVO>> finalPGroupMap = pGroupMap;
return roles.stream().map(role -> {
// 获取对应的权限集
List<SaasPermissionGroupVO> pGroup = new ArrayList<>();
if (finalPgrouRelationMap != null && finalPgrouRelationMap.containsKey(role.getId())) {
pGroup = finalPgrouRelationMap.get(role.getId())
.stream()
.map(SaasPgroupRoleRelation::getGroupId)
.map(finalPGroupMap::get)
.filter(Objects::nonNull)
.flatMap(List::stream)
.collect(Collectors.toList());
}
SaasRoleVO saasRoleVO = BeanUtil.copyProperties(role, SaasRoleVO.class);
saasRoleVO.setPermissionGroup(pGroup);
return saasRoleVO;
}).collect(Collectors.toList());
}
/**
* 根据id查询权限详情包含权限组合权限集(底层基础方法)
*
* @return
*/
public List<SaasRoleVO> getByIds(List<Long> roleIds) {
if (CollectionUtils.isEmpty(roleIds)) {
return new ArrayList<>();
}
// 查询角色信息
List<SaasRole> roles = saasRoleDao.lambdaQuery().in(BaseEntity::getId, roleIds).list();
// 查询权限集关联关系
List<SaasPgroupRoleRelation> saasPgroupRoleRelations = queryPermissionGroupRelation(roleIds);
// 查询权限集
Map<Long, List<SaasPgroupRoleRelation>> pgrouRelationMap = null;
Map<Long, List<SaasPermissionGroupVO>> pGroupMap = null;
if (CollectionUtils.isNotEmpty(saasPgroupRoleRelations)) {
// 转map<roleId,relation>
pgrouRelationMap = saasPgroupRoleRelations.stream().collect(Collectors.groupingBy(SaasPgroupRoleRelation::getRoleId));
// 查询权限集
pGroupMap = permissionGroupService.query(QuerySaasPermissionGroupReq.builder()
.ids(saasPgroupRoleRelations.stream().map(SaasPgroupRoleRelation::getGroupId).collect(Collectors.toList()))
.build())
// 转map<pgroupId>
.stream().collect(Collectors.groupingBy(SaasPermissionGroupVO::getId));
}
Map<Long, List<SaasPgroupRoleRelation>> finalPgrouRelationMap = pgrouRelationMap;
Map<Long, List<SaasPermissionGroupVO>> finalPGroupMap = pGroupMap;
return roles.stream().map(role -> {
// 获取对应的权限集
List<SaasPermissionGroupVO> pGroup = new ArrayList<>();
if (finalPgrouRelationMap != null && finalPgrouRelationMap.containsKey(role.getId())) {
pGroup = finalPgrouRelationMap.get(role.getId())
.stream()
.map(SaasPgroupRoleRelation::getGroupId)
.map(finalPGroupMap::get)
.filter(Objects::nonNull)
.flatMap(List::stream)
.collect(Collectors.toList());
}
SaasRoleVO saasRoleVO = BeanUtil.copyProperties(role, SaasRoleVO.class);
saasRoleVO.setPermissionGroup(pGroup);
return saasRoleVO;
}).collect(Collectors.toList());
}
/**
* 通用查询
*
* @param req
* @return
*/
@Override
public List<SaasRoleVO> query(QuerySaasRoleReq req) {
// 根据工作台类型和单位类型查询角色分组
List<SaasRoleGroup> roleGroup = new ArrayList<>();
if (CollectionUtils.isNotEmpty(req.getWorkspaceTypeCode()) || CollectionUtils.isNotEmpty(req.getOuTypeCode())) {
roleGroup = saasRoleGroupDao.query(QuerySaasRoleGroupReq.builder()
.ids(req.getSassRoleGroupIds())
.workspaceTypeCode(req.getWorkspaceTypeCode())
.ouTypeCode(req.getOuTypeCode())
.build());
if (CollectionUtils.isEmpty(roleGroup)) {
return new ArrayList<>();
}
}
// 根据角色分组查询角色分组关联表
List<SaasRoleGroupRelation> groupRelation = new ArrayList<>();
if (CollectionUtils.isNotEmpty(roleGroup)) {
groupRelation = roleGroupRelationDao.lambdaQuery()
.in(SaasRoleGroupRelation::getSaasRoleGroupId, roleGroup.stream().map(BaseEntity::getId).collect(Collectors.toList()))
.list();
}
// 查询角色
List<SaasRole> list = saasRoleDao.lambdaQuery()
.in(CollectionUtils.isNotEmpty(req.getIds()), BaseEntity::getId, req.getIds())
.in(CollectionUtils.isNotEmpty(groupRelation), BaseEntity::getId, groupRelation.stream().map(SaasRoleGroupRelation::getRoleId).collect(Collectors.toList()))
.eq(StringUtils.isNotBlank(req.getRoleType()),SaasRole::getRoleType,req.getRoleType())
.orderByDesc(BaseEntity::getId)
.list();
return getByIds(list.stream().map(BaseEntity::getId).collect(Collectors.toList()));
}
/**
* 通用查询
*
* @param req
* @return
*/
@Override
public List<SaasRoleVO> query(QuerySaasRoleReq req) {
// 根据工作台类型和单位类型查询角色分组
List<SaasRoleGroup> roleGroup = new ArrayList<>();
if (CollectionUtils.isNotEmpty(req.getWorkspaceTypeCode()) || CollectionUtils.isNotEmpty(req.getOuTypeCode())) {
roleGroup = saasRoleGroupDao.query(QuerySaasRoleGroupReq.builder()
.ids(req.getSassRoleGroupIds())
.workspaceTypeCode(req.getWorkspaceTypeCode())
.ouTypeCode(req.getOuTypeCode())
.build());
if (CollectionUtils.isEmpty(roleGroup)) {
return new ArrayList<>();
}
}
// 根据角色分组查询角色分组关联表
List<SaasRoleGroupRelation> groupRelation = new ArrayList<>();
if (CollectionUtils.isNotEmpty(roleGroup)) {
groupRelation = roleGroupRelationDao.lambdaQuery()
.in(SaasRoleGroupRelation::getSaasRoleGroupId, roleGroup.stream().map(BaseEntity::getId).collect(Collectors.toList()))
.list();
}
// 查询角色
List<SaasRole> list = saasRoleDao.lambdaQuery()
.in(CollectionUtils.isNotEmpty(req.getIds()), BaseEntity::getId, req.getIds())
.in(CollectionUtils.isNotEmpty(groupRelation), BaseEntity::getId, groupRelation.stream().map(SaasRoleGroupRelation::getRoleId).collect(Collectors.toList()))
.eq(StringUtils.isNotBlank(req.getRoleType()), SaasRole::getRoleType, req.getRoleType())
.orderByDesc(BaseEntity::getId)
.list();
return getByIds(list.stream().map(BaseEntity::getId).collect(Collectors.toList()));
}
/**
* 根据角色id查询权限集关联关系
*/
public List<SaasPgroupRoleRelation> queryPermissionGroupRelation(List<Long> roleIds) {
return permissionGroupRelation.lambdaQuery()
.in(SaasPgroupRoleRelation::getRoleId, roleIds)
.eq(BaseEntity::getIsDelete, TableIsDeleteEnum.NORMAL.value)
.list();
}
@Override
public Map<QueryByIdentityIdTypeReq, List<SaasRoleVO>> queryBatchByIdentityIdType(List<QueryByIdentityIdTypeReq> req) {
HashMap<QueryByIdentityIdTypeReq, List<SaasRoleVO>> result = new HashMap<>();
req.forEach(e -> {
result.put(e,queryByIdentityIdType(e.getIdentityId(),e.getIdentityType(),e.getWorkspaceId(),e.getOuId()));
});
return result;
}
/**
* 根据角色id查询权限集关联关系
*/
public List<SaasPgroupRoleRelation> queryPermissionGroupRelation(List<Long> roleIds) {
return permissionGroupRelation.lambdaQuery()
.in(SaasPgroupRoleRelation::getRoleId, roleIds)
.eq(BaseEntity::getIsDelete, TableIsDeleteEnum.NORMAL.value)
.list();
}
}