refactor_接口定义查询优化

This commit is contained in:
chenjianxing 2022-01-21 17:45:51 +08:00 committed by zhangdahai112
parent 52d7103d2b
commit 7b3e754035
2 changed files with 33 additions and 23 deletions

View File

@ -131,10 +131,38 @@ public class ApiDefinitionService {
public List<ApiDefinitionResult> list(ApiDefinitionRequest request) {
request = this.initRequest(request, true, true);
List<ApiDefinitionResult> resList = extApiDefinitionMapper.list(request);
buildUserInfo(resList);
buildProjectInfo(resList, request.getProjectId());
calculateResult(resList, request.getProjectId());
return resList;
}
public void buildUserInfo(List<ApiDefinitionResult> apis) {
if (CollectionUtils.isEmpty(apis)) { return; }
Set<String> userIds = new HashSet<>();
apis.forEach(i -> {
userIds.add(i.getUserId());
userIds.add(i.getDeleteUserId());
userIds.add(i.getCreateUser());
});
if (!org.apache.commons.collections.CollectionUtils.isEmpty(userIds)) {
Map<String, String> userMap = ServiceUtils.getUserNameMap(new ArrayList<>(userIds));
apis.forEach(caseResult -> {
caseResult.setCreateUser(userMap.get(caseResult.getCreateUser()));
caseResult.setDeleteUser(userMap.get(caseResult.getDeleteUserId()));
caseResult.setUserName(userMap.get(caseResult.getUserId()));
});
}
}
public void buildProjectInfo(List<ApiDefinitionResult> apis, String projectId) {
Project project = projectMapper.selectByPrimaryKey(projectId);
apis.forEach(i -> {
i.setProjectName(project.getName());
i.setVersionEnable(project.getVersionEnable());
});
}
public List<ApiDefinitionResult> weekList(ApiDefinitionRequest request) {
//获取7天之前的日期
Date startDay = DateUtils.dateSum(new Date(), -6);
@ -1750,7 +1778,7 @@ public class ApiDefinitionService {
List<ApiDefinition> apiDefinitions = apiDefinitionMapper.selectByExample(example);
Map<String, ApiDefinition> apiMap = apiDefinitions.stream().collect(Collectors.toMap(ApiDefinition::getId, i -> i));
List<RelationshipEdgeDTO> results = new ArrayList<>();
buildUserInfo(apiDefinitions);
Map<String, String> userNameMap = ServiceUtils.getUserNameMap(apiDefinitions.stream().map(ApiDefinition::getUserId).collect(Collectors.toList()));
for (RelationshipEdge relationshipEdge : relationshipEdges) {
RelationshipEdgeDTO relationshipEdgeDTO = new RelationshipEdgeDTO();
BeanUtils.copyBean(relationshipEdgeDTO, relationshipEdge);
@ -1764,7 +1792,7 @@ public class ApiDefinitionService {
continue;
}
relationshipEdgeDTO.setTargetName(apiDefinition.getName());
relationshipEdgeDTO.setCreator(apiDefinition.getUserId());
relationshipEdgeDTO.setCreator(userNameMap.get(apiDefinition.getUserId()));
relationshipEdgeDTO.setTargetNum(apiDefinition.getNum());
relationshipEdgeDTO.setStatus(apiDefinition.getStatus());
relationshipEdgeDTO.setVersionId(apiDefinition.getVersionId());
@ -1775,21 +1803,6 @@ public class ApiDefinitionService {
return new ArrayList<>();
}
public void buildUserInfo(List<? extends ApiDefinition> apis) {
List<String> userIds = new ArrayList();
userIds.addAll(apis.stream().map(ApiDefinition::getCreateUser).collect(Collectors.toList()));
userIds.addAll(apis.stream().map(ApiDefinition::getDeleteUserId).collect(Collectors.toList()));
userIds.addAll(apis.stream().map(ApiDefinition::getUserId).collect(Collectors.toList()));
if (!org.apache.commons.collections.CollectionUtils.isEmpty(userIds)) {
Map<String, String> userMap = ServiceUtils.getUserNameMap(userIds);
apis.forEach(caseResult -> {
caseResult.setCreateUser(userMap.get(caseResult.getCreateUser()));
caseResult.setDeleteUserId(userMap.get(caseResult.getDeleteUserId()));
caseResult.setUserId(userMap.get(caseResult.getUserId()));
});
}
}
public Pager<List<ApiDefinitionResult>> getRelationshipRelateList(ApiDefinitionRequest request, int goPage, @PathVariable int pageSize) {
request = this.initRequest(request, true, true);
// 排除同一个api的不同版本

View File

@ -245,14 +245,11 @@
select api_definition.id, api_definition.project_id, api_definition.num, api_definition.tags,api_definition.original_state,
api_definition.name,api_definition.protocol,api_definition.path,api_definition.module_id,api_definition.module_path,api_definition.method,
api_definition.description,api_definition.request,api_definition.response,api_definition.environment_id,
api_definition.status, api_definition.user_id, api_definition.create_time, api_definition.update_time, project.name as
project_name, user.name as user_name,deleteUser.name AS delete_user,api_definition.delete_time, api_definition.remark, api_definition.version_id,
project_version.name as version_name, api_definition.ref_id, project.version_enable
api_definition.status, api_definition.user_id, api_definition.create_time, api_definition.update_time, api_definition.delete_user_id, api_definition.create_user,api_definition.delete_time, api_definition.remark, api_definition.version_id,
project_version.name as version_name, api_definition.ref_id, user.name as user_name,
from api_definition
left join project on api_definition.project_id = project.id
left join user on api_definition.user_id = user.id
left join user deleteUser on api_definition.delete_user_id = deleteUser.id
LEFT JOIN project_version on project.id = project_version.project_id AND project_version.id = api_definition.version_id
LEFT JOIN project_version ON project_version.id = api_definition.version_id AND project_version.project_id = #{request.projectId}
<include refid="queryWhereCondition"/>
<if test="request.orders != null and request.orders.size() > 0">
order by