From f74866652f0929a939bc2e585b7840a0a20721f0 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 26 Dec 2022 19:39:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E6=A8=A1=E5=9D=97=E6=A0=91=E5=A4=9A?= =?UTF-8?q?=E6=AC=A1=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/definition/ApiModuleService.java | 38 +++++++++++++------ .../scenario/ApiScenarioModuleService.java | 36 ++++++++++++------ 2 files changed, 51 insertions(+), 23 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiModuleService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiModuleService.java index 89a38609f7..268f31dcef 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiModuleService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiModuleService.java @@ -31,6 +31,7 @@ import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.jetbrains.annotations.NotNull; import org.mybatis.spring.SqlSessionUtils; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -189,6 +190,7 @@ public class ApiModuleService extends NodeTreeService { public List getNodeTreeByCondition(String projectId, String protocol, String versionId, ApiDefinitionRequest request) { // 判断当前项目下是否有默认模块,没有添加默认模块 + this.getDefaultNode(projectId, protocol); List apiModules = getApiModulesByProjectAndPro(projectId, protocol); request.setProjectId(projectId); request.setProtocol(protocol); @@ -573,18 +575,30 @@ public class ApiModuleService extends NodeTreeService { example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()).andParentIdIsNull(); List list = apiModuleMapper.selectByExample(example); if (CollectionUtils.isEmpty(list)) { - ApiModule record = new ApiModule(); - record.setId(UUID.randomUUID().toString()); - record.setName(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()); - record.setProtocol(protocol); - record.setPos(1.0); - record.setLevel(1); - record.setCreateTime(System.currentTimeMillis()); - record.setUpdateTime(System.currentTimeMillis()); - record.setProjectId(projectId); - record.setCreateUser(SessionUtils.getUserId()); - apiModuleMapper.insert(record); - return record; + return saveDefault(projectId, protocol); + } else { + return list.get(0); + } + } + + @Async + public synchronized ApiModule saveDefault(String projectId, String protocol) { + ApiModuleExample example = new ApiModuleExample(); + example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()).andParentIdIsNull(); + List list = apiModuleMapper.selectByExample(example); + if (CollectionUtils.isEmpty(list)) { + ApiModule module = new ApiModule(); + module.setId(UUID.randomUUID().toString()); + module.setName(ProjectModuleDefaultNodeEnum.API_MODULE_DEFAULT_NODE.getNodeName()); + module.setProtocol(protocol); + module.setPos(1.0); + module.setLevel(1); + module.setCreateTime(System.currentTimeMillis()); + module.setUpdateTime(System.currentTimeMillis()); + module.setProjectId(projectId); + module.setCreateUser(SessionUtils.getUserId()); + apiModuleMapper.insert(module); + return module; } else { return list.get(0); } diff --git a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioModuleService.java b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioModuleService.java index 74f29fc546..e14f733843 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioModuleService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/scenario/ApiScenarioModuleService.java @@ -30,6 +30,7 @@ import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionUtils; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; @@ -103,6 +104,7 @@ public class ApiScenarioModuleService extends NodeTreeService getNodeTreeByProjectId(String projectId, ApiScenarioRequest request) { // 判断当前项目下是否有默认模块,没有添加默认模块 + this.getDefaultNode(projectId); List nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId); request.setProjectId(projectId); List list = new ArrayList<>(); @@ -510,17 +512,29 @@ public class ApiScenarioModuleService extends NodeTreeService list = apiScenarioModuleMapper.selectByExample(example); if (CollectionUtils.isEmpty(list)) { - ApiScenarioModule record = new ApiScenarioModule(); - record.setId(UUID.randomUUID().toString()); - record.setName(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName()); - record.setPos(1.0); - record.setLevel(1); - record.setCreateTime(System.currentTimeMillis()); - record.setUpdateTime(System.currentTimeMillis()); - record.setProjectId(projectId); - record.setCreateUser(SessionUtils.getUserId()); - apiScenarioModuleMapper.insert(record); - return record; + return saveDefault(projectId); + } else { + return list.get(0); + } + } + + @Async + public synchronized ApiScenarioModule saveDefault(String projectId) { + ApiScenarioModuleExample example = new ApiScenarioModuleExample(); + example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName()).andParentIdIsNull(); + List list = apiScenarioModuleMapper.selectByExample(example); + if (CollectionUtils.isEmpty(list)) { + ApiScenarioModule module = new ApiScenarioModule(); + module.setId(UUID.randomUUID().toString()); + module.setName(ProjectModuleDefaultNodeEnum.API_SCENARIO_DEFAULT_NODE.getNodeName()); + module.setPos(1.0); + module.setLevel(1); + module.setCreateTime(System.currentTimeMillis()); + module.setUpdateTime(System.currentTimeMillis()); + module.setProjectId(projectId); + module.setCreateUser(SessionUtils.getUserId()); + apiScenarioModuleMapper.insert(module); + return module; } else { return list.get(0); }