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); }