From 7b3e754035666ada558d2ce0d07aba2a21469330 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Fri, 21 Jan 2022 17:45:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor=5F=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/service/ApiDefinitionService.java | 47 ++++++++++++------- .../mapper/ext/ExtApiDefinitionMapper.xml | 9 ++-- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 90ab6d7020..51f6e33df7 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -131,10 +131,38 @@ public class ApiDefinitionService { public List list(ApiDefinitionRequest request) { request = this.initRequest(request, true, true); List resList = extApiDefinitionMapper.list(request); + buildUserInfo(resList); + buildProjectInfo(resList, request.getProjectId()); calculateResult(resList, request.getProjectId()); return resList; } + public void buildUserInfo(List apis) { + if (CollectionUtils.isEmpty(apis)) { return; } + Set 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 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 apis, String projectId) { + Project project = projectMapper.selectByPrimaryKey(projectId); + apis.forEach(i -> { + i.setProjectName(project.getName()); + i.setVersionEnable(project.getVersionEnable()); + }); + } + public List weekList(ApiDefinitionRequest request) { //获取7天之前的日期 Date startDay = DateUtils.dateSum(new Date(), -6); @@ -1750,7 +1778,7 @@ public class ApiDefinitionService { List apiDefinitions = apiDefinitionMapper.selectByExample(example); Map apiMap = apiDefinitions.stream().collect(Collectors.toMap(ApiDefinition::getId, i -> i)); List results = new ArrayList<>(); - buildUserInfo(apiDefinitions); + Map 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 apis) { - List 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 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> getRelationshipRelateList(ApiDefinitionRequest request, int goPage, @PathVariable int pageSize) { request = this.initRequest(request, true, true); // 排除同一个api的不同版本 diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml index 7a7f9200e8..059d0cad73 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionMapper.xml @@ -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} order by