diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.xml index d34981b39b..c63a7c4114 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.xml @@ -56,8 +56,7 @@ a.protocol, t.status execResult, a.user_id, - a.version_id versionId, - project_version.name versionName + a.version_id versionId from test_plan_api_case t inner join @@ -70,7 +69,7 @@ api_definition a on c.api_definition_id = a.id - inner join project_version on a.version_id = project_version.id and a.project_id = project_version.project_id + where 1 and a.protocol = #{request.protocol} @@ -91,7 +90,6 @@ and t.status = #{request.status} - where 1 and a.projectId = #{request.projectId} diff --git a/backend/src/main/java/io/metersphere/excel/listener/TestCaseNoModelDataListener.java b/backend/src/main/java/io/metersphere/excel/listener/TestCaseNoModelDataListener.java index d40bb981c9..67e928d936 100644 --- a/backend/src/main/java/io/metersphere/excel/listener/TestCaseNoModelDataListener.java +++ b/backend/src/main/java/io/metersphere/excel/listener/TestCaseNoModelDataListener.java @@ -47,7 +47,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener importCreateNum = new ThreadLocal<>(); + private ThreadLocal beforeImportCreateNum = new ThreadLocal<>(); + private void setNode(TestCaseWithBLOBs testCase) { if (StringUtils.isEmpty(testCase.getNodeId()) || "default-module".equals(testCase.getNodeId())) { TestCaseNodeExample example = new TestCaseNodeExample(); @@ -251,16 +254,11 @@ public class TestCaseService { } private void checkCustomNumExist(TestCaseWithBLOBs testCase) { - String id = testCase.getId(); - TestCaseWithBLOBs testCaseWithBLOBs = testCaseMapper.selectByPrimaryKey(id); TestCaseExample example = new TestCaseExample(); - TestCaseExample.Criteria criteria = example.createCriteria(); - criteria.andCustomNumEqualTo(testCase.getCustomNum()) + example.createCriteria() + .andCustomNumEqualTo(testCase.getCustomNum()) .andProjectIdEqualTo(testCase.getProjectId()) .andIdNotEqualTo(testCase.getId()); - if (testCaseWithBLOBs != null && StringUtils.isNotBlank(testCaseWithBLOBs.getRefId())) { - criteria.andRefIdNotEqualTo(testCaseWithBLOBs.getRefId()); - } List list = testCaseMapper.selectByExample(example); if (CollectionUtils.isNotEmpty(list)) { MSException.throwException(Translator.get("custom_num_is_exist")); @@ -729,6 +727,9 @@ public class TestCaseService { if (StringUtils.isBlank(request.getVersionId()) && StringUtils.equals(request.getImportType(), FunctionCaseImportEnum.Create.name())) { // 创建如果没选版本就创建最新版本,更新时没选就更新最近版本的用例 request.setVersionId(extProjectVersionMapper.getDefaultVersion(request.getProjectId())); + int nextNum = getNextNum(request.getProjectId()); + importCreateNum.set(nextNum); + beforeImportCreateNum.set(nextNum); } if (multipartFile.getOriginalFilename().endsWith(".xmind")) { return testCaseXmindImport(multipartFile, request, httpRequest); @@ -917,36 +918,38 @@ public class TestCaseService { SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); Project project = projectService.getProjectById(projectId); TestCaseMapper mapper = sqlSession.getMapper(TestCaseMapper.class); + try { - Long nextOrder = ServiceUtils.getNextOrder(projectId, extTestCaseMapper::getLastOrder); if (!testCases.isEmpty()) { - AtomicInteger sort = new AtomicInteger(); - AtomicInteger num = new AtomicInteger(); - num.set(getNextNum(projectId) + testCases.size()); - for (TestCaseWithBLOBs testcase : testCases) { - testcase.setId(UUID.randomUUID().toString()); - testcase.setCreateUser(SessionUtils.getUserId()); - testcase.setCreateTime(System.currentTimeMillis()); - testcase.setUpdateTime(System.currentTimeMillis()); - testcase.setNodeId(nodePathMap.get(testcase.getNodePath())); - testcase.setSort(sort.getAndIncrement()); - int number = num.incrementAndGet(); - testcase.setNum(number); - if (project.getCustomNum() && StringUtils.isBlank(testcase.getCustomNum())) { - testcase.setCustomNum(String.valueOf(number)); + Integer num = importCreateNum.get(); + Integer beforeInsertId = beforeImportCreateNum.get(); + + for (int i = testCases.size() - 1; i > - 1; i--) { // 反向遍历,保持和文件顺序一致 + TestCaseWithBLOBs testCase = testCases.get(i); + testCase.setId(UUID.randomUUID().toString()); + testCase.setCreateUser(SessionUtils.getUserId()); + testCase.setCreateTime(System.currentTimeMillis()); + testCase.setUpdateTime(System.currentTimeMillis()); + testCase.setNodeId(nodePathMap.get(testCase.getNodePath())); + testCase.setNum(num); + if (project.getCustomNum() && StringUtils.isBlank(testCase.getCustomNum())) { + testCase.setCustomNum(String.valueOf(num)); } - testcase.setReviewStatus(TestCaseReviewStatus.Prepare.name()); - testcase.setStatus(TestCaseReviewStatus.Prepare.name()); - testcase.setOrder(nextOrder); - testcase.setRefId(testcase.getId()); - testcase.setVersionId(request.getVersionId()); - testcase.setLatest(true); - mapper.insert(testcase); - nextOrder += ServiceUtils.ORDER_STEP; + num++; + testCase.setReviewStatus(TestCaseReviewStatus.Prepare.name()); + testCase.setStatus(TestCaseReviewStatus.Prepare.name()); + testCase.setOrder(new Long(testCases.size() - (num - beforeInsertId)) * ServiceUtils.ORDER_STEP); + testCase.setRefId(testCase.getId()); + testCase.setVersionId(request.getVersionId()); + testCase.setLatest(true); + mapper.insert(testCase); } + + importCreateNum.set(num); } - sqlSession.flushStatements(); } finally { + sqlSession.commit(); + sqlSession.clearCache(); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); } } @@ -1671,7 +1674,6 @@ public class TestCaseService { } } - /** * 导入用例前,检查数据库是否存在此用例 * diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java index 3c86e63c16..2439cd071a 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java @@ -80,6 +80,7 @@ public class TestPlanApiCaseService { request.setProjectId(null); request.setOrders(ServiceUtils.getDefaultSortOrder(request.getOrders())); List apiTestCases = extTestPlanApiCaseMapper.list(request); + ServiceUtils.buildVersionInfo(apiTestCases); if (CollectionUtils.isEmpty(apiTestCases)) { return apiTestCases; } @@ -244,6 +245,7 @@ public class TestPlanApiCaseService { ApiTestCaseRequest selectReq = new ApiTestCaseRequest(); selectReq.setIds(ids); List returnList = extTestPlanApiCaseMapper.list(selectReq); + ServiceUtils.buildVersionInfo(returnList); return returnList; }