IndexPatternService 获取索引下用户和团队 getIndexPatternUserAndDepartment分析
好的,根据您提供的文档内容,我提取了 getIndexPatternUserAndDepartment
方法,并对其进行详细分析。以下是提取的方法及其相关部分:
提取的 getIndexPatternUserAndDepartment
方法
@Override
public GetIndexPatternUserAndDepartmentResponse getIndexPatternUserAndDepartment(GetIndexPatternUserAndDepartmentRequest request) {
String indexPatternName = request.getIndexPatternName();
String indexName = request.getIndexPatternName();
if (StringUtils.isEmpty(indexName)) {
throw new TitanException("所传参数为空!");
}
// 创建查询条件,获取索引模式
Example example = new Example(IndexPattern.class);
Example.Criteria criteria = example.createCriteria();
criteria.orEqualTo("indexPatternName", indexPatternName);
IndexPattern indexPattern = indexPatternMapper.selectOneByExample(example);
// 获取索引模式下的用户权限
Example userExample = new Example(UserPermission.class);
Example.Criteria userCriteria = userExample.createCriteria();
userCriteria.orEqualTo("indexPatternId", indexPattern.getId());
List<UserPermission> userPermissionList = userPermissionMapper.selectByExample(userExample);
List<String> userNameList = new ArrayList<>();
for (UserPermission userPermission : userPermissionList) {
if (Boolean.TRUE.equals(userPermission.getPermanentFlag()) ||
(Boolean.FALSE.equals(userPermission.getPermanentFlag()) &&
userPermission.getEffectiveDate() != null &&
userPermission.getEffectiveDate().getTime() > new Date().getTime())) {
userNameList.add(userPermission.getUserName());
}
}
// 获取索引模式下的部门权限
Example departmentExample = new Example(DepartmentPermission.class);
Example.Criteria departmentCriteria = departmentExample.createCriteria();
departmentCriteria.orEqualTo("indexPatternId", indexPattern.getId());
List<DepartmentPermission> departmentPermissionList = permissionMapper.selectByExample(departmentExample);
List<String> departmentIdList = new ArrayList<>();
for (DepartmentPermission departmentPermission : departmentPermissionList) {
if (Boolean.TRUE.equals(departmentPermission.getPermanentFlag()) ||
(Boolean.FALSE.equals(departmentPermission.getPermanentFlag()) &&
departmentPermission.getEffectiveDate() != null &&
departmentPermission.getEffectiveDate().getTime() > new Date().getTime())) {
departmentIdList.add(departmentPermission.getDepartmentId());
}
}
// 构建响应对象
GetIndexPatternUserAndDepartmentResponse response = new GetIndexPatternUserAndDepartmentResponse();
response.setUserNameList(userNameList);
response.setDepartmentIdList(departmentIdList);
RespUtils.setSuccess(response);
return response;
}
方法分析
输入参数
GetIndexPatternUserAndDepartmentRequest request
:包含请求参数,主要是indexPatternName
(索引模式名称)。
输出参数
GetIndexPatternUserAndDepartmentResponse response
:包含查询结果的响应对象,包括用户的用户名列表和部门ID列表。
方法步骤
-
参数校验:
String indexPatternName = request.getIndexPatternName(); String indexName = request.getIndexPatternName(); if (StringUtils.isEmpty(indexName)) { throw new TitanException("所传参数为空!"); }
- 获取请求中的
indexPatternName
。 - 检查
indexName
是否为空,如果为空则抛出异常。
- 获取请求中的
-
查询索引模式:
Example example = new Example(IndexPattern.class); Example.Criteria criteria = example.createCriteria(); criteria.orEqualTo("indexPatternName", indexPatternName); IndexPattern indexPattern = indexPatternMapper.selectOneByExample(example);
- 创建一个
Example
对象,用于构建查询条件。 - 设置查询条件为
indexPatternName
等于请求中的indexPatternName
。 - 调用
indexPatternMapper.selectOneByExample
方法查询符合条件的IndexPattern
对象。
- 创建一个
-
查询用户权限:
Example userExample = new Example(UserPermission.class); Example.Criteria userCriteria = userExample.createCriteria(); userCriteria.orEqualTo("indexPatternId", indexPattern.getId()); List<UserPermission> userPermissionList = userPermissionMapper.selectByExample(userExample); List<String> userNameList = new ArrayList<>(); for (UserPermission userPermission : userPermissionList) { if (Boolean.TRUE.equals(userPermission.getPermanentFlag()) || (Boolean.FALSE.equals(userPermission.getPermanentFlag()) && userPermission.getEffectiveDate() != null && userPermission.getEffectiveDate().getTime() > new Date().getTime())) { userNameList.add(userPermission.getUserName()); } }
- 创建一个
Example
对象,用于构建查询条件。 - 设置查询条件为
indexPatternId
等于indexPattern
的 ID。 - 调用
userPermissionMapper.selectByExample
方法查询符合条件的UserPermission
对象列表。 - 遍历查询结果,筛选出永久有效的用户权限或有效期未过期的用户权限,并将用户名添加到
userNameList
中。
- 创建一个
-
查询部门权限:
Example departmentExample = new Example(DepartmentPermission.class); Example.Criteria departmentCriteria = departmentExample.createCriteria(); departmentCriteria.orEqualTo("indexPatternId", indexPattern.getId()); List<DepartmentPermission> departmentPermissionList = permissionMapper.selectByExample(departmentExample); List<String> departmentIdList = new ArrayList<>(); for (DepartmentPermission departmentPermission : departmentPermissionList) { if (Boolean.TRUE.equals(departmentPermission.getPermanentFlag()) || (Boolean.FALSE.equals(departmentPermission.getPermanentFlag()) && departmentPermission.getEffectiveDate() != null && departmentPermission.getEffectiveDate().getTime() > new Date().getTime())) { departmentIdList.add(departmentPermission.getDepartmentId()); } }
- 创建一个
Example
对象,用于构建查询条件。 - 设置查询条件为
indexPatternId
等于indexPattern
的 ID。 - 调用
permissionMapper.selectByExample
方法查询符合条件的DepartmentPermission
对象列表。 - 遍历查询结果,筛选出永久有效的部门权限或有效期未过期的部门权限,并将部门ID添加到
departmentIdList
中。
- 创建一个
-
构建响应对象:
GetIndexPatternUserAndDepartmentResponse response = new GetIndexPatternUserAndDepartmentResponse(); response.setUserNameList(userNameList); response.setDepartmentIdList(departmentIdList); RespUtils.setSuccess(response);
- 创建一个新的
GetIndexPatternUserAndDepartmentResponse
对象。 - 设置响应对象中的
userNameList
和departmentIdList
。 - 使用
RespUtils.setSuccess
方法设置响应的成功状态。
- 创建一个新的
-
返回响应对象:
return response;
总结
getIndexPatternUserAndDepartment
方法的主要功能是根据索引模式名称查询相关的用户权限和部门权限,并将结果封装在响应对象中返回。具体步骤包括:
- 参数校验:确保传入的索引模式名称不为空。
- 查询索引模式:根据索引模式名称查询对应的
IndexPattern
对象。 - 查询用户权限:查询与索引模式相关的用户权限,并筛选出有效的用户权限。
- 查询部门权限:查询与索引模式相关的部门权限,并筛选出有效的部门权限。
- 构建响应对象:将查询结果封装在响应对象中,并设置成功状态。
- 返回响应对象:返回包含查询结果的响应对象。