diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/parse/ApiScenarioImportUtil.java b/backend/src/main/java/io/metersphere/api/dto/automation/parse/ApiScenarioImportUtil.java index 2fe45151ce..4423496a7b 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/parse/ApiScenarioImportUtil.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/parse/ApiScenarioImportUtil.java @@ -271,7 +271,7 @@ public class ApiScenarioImportUtil { apiTestCase.setUpdateTime(System.currentTimeMillis()); apiTestCase.setVersionId(apiDefinition.getVersionId()); apiTestCase.setPriority("P0"); - apiTestCase.setNum(testCaseService.getNextNum(apiTestCase.getApiDefinitionId(), apiDefinition.getNum() + i)); + apiTestCase.setNum(testCaseService.getNextNum(apiTestCase.getApiDefinitionId(), apiDefinition.getNum() + i, projectId)); object.put("id", apiTestCase.getId()); object.put("resourceId", apiTestCase.getId()); object.put("projectId", projectId); 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 fef6b01e15..0861af263d 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -1113,7 +1113,7 @@ public class ApiDefinitionService { if (sameCase == null) { apiTestCase.setId(UUID.randomUUID().toString()); apiTestCase.setStatus(""); - apiTestCase.setNum(apiTestCaseService.getNextNum(apiTestCase.getApiDefinitionId(), apiDefinition.getNum())); + apiTestCase.setNum(apiTestCaseService.getNextNum(apiTestCase.getApiDefinitionId(), apiDefinition.getNum(), apiDefinition.getProjectId())); apiTestCase.setCreateTime(System.currentTimeMillis()); apiTestCase.setUpdateTime(System.currentTimeMillis()); apiTestCase.setCreateUserId(SessionUtils.getUserId()); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java index 2940421227..2d9492ebba 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java @@ -111,6 +111,8 @@ public class ApiTestCaseService { private TcpApiParamService tcpApiParamService; @Resource private ObjectMapper mapper; + @Resource + private ExtApiDefinitionMapper extApiDefinitionMapper; private static final String BODY_FILE_DIR = FileUtils.BODY_FILE_DIR; @@ -471,25 +473,43 @@ public class ApiTestCaseService { public int getNextNum(String definitionId) { ApiTestCase apiTestCase = extApiTestCaseMapper.getNextNum(definitionId); + ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionMapper.selectByPrimaryKey(definitionId); if (apiTestCase == null) { - int n = apiDefinitionMapper.selectByPrimaryKey(definitionId).getNum(); + int n = apiDefinitionWithBLOBs.getNum(); return n * 1000 + 1; } else { return Optional.of(apiTestCase.getNum() + 1) - .orElse(apiDefinitionMapper.selectByPrimaryKey(definitionId).getNum() * 1000 + 1); + .orElse(apiDefinitionWithBLOBs.getNum() * 1000 + 1); } } - public int getNextNum(String definitionId, Integer definitionNum) { + public int getNextNum(String definitionId, Integer definitionNum, String projectId) { ApiTestCase apiTestCase = extApiTestCaseMapper.getNextNum(definitionId); + ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionMapper.selectByPrimaryKey(definitionId); + int apiDefinitionNum; + if (apiDefinitionWithBLOBs != null && apiDefinitionWithBLOBs.getNum() != null) { + apiDefinitionNum = apiDefinitionWithBLOBs.getNum() * 1000 + 1; + } else { + apiDefinitionNum = getNextNumByProjectId(projectId); + } if (apiTestCase == null) { if (definitionNum == null) { - return apiDefinitionMapper.selectByPrimaryKey(definitionId).getNum() * 1000 + 1; + return apiDefinitionNum; + } return definitionNum * 1000 + 1; } else { return Optional.of(apiTestCase.getNum() + 1) - .orElse(apiDefinitionMapper.selectByPrimaryKey(definitionId).getNum() * 1000 + 1); + .orElse(apiDefinitionNum); + } + } + + public int getNextNumByProjectId(String projectId) { + ApiDefinition apiDefinition = extApiDefinitionMapper.getNextNum(projectId); + if (apiDefinition == null || apiDefinition.getNum() == null) { + return 100001; + } else { + return Optional.of(apiDefinition.getNum() + 1).orElse(100001); } } 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 14a73ce78e..7371ac8e4d 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 @@ -1022,6 +1022,7 @@ #{versionId} and protocol = #{protocol} + and status != 'Trash'