From b9848e452c4a7a7cfef92007d97ebf3c9a3396fd Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Thu, 31 Dec 2020 15:40:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AF=BC=E5=85=A5=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E6=9B=BF=E6=8D=A2=EF=BC=8C=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92=E5=88=A0=E9=99=A4=E5=85=B3?= =?UTF-8?q?=E8=81=94=E7=9A=84=E6=8E=A5=E5=8F=A3=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/service/ApiDefinitionService.java | 52 +++++++++++++------ .../api/service/ApiScenarioReportService.java | 14 +++++ .../mapper/ext/ExtTestPlanApiCaseMapper.java | 2 + .../mapper/ext/ExtTestPlanApiCaseMapper.xml | 6 +++ .../ext/ExtTestPlanScenarioCaseMapper.java | 2 + .../ext/ExtTestPlanScenarioCaseMapper.xml | 5 ++ .../controller/TestPlanApiCaseController.java | 6 +-- .../TestPlanScenarioCaseController.java | 6 +-- .../track/service/TestPlanApiCaseService.java | 14 +++-- .../service/TestPlanScenarioCaseService.java | 29 ++++++++--- .../track/service/TestPlanService.java | 2 + .../comonents/api/TestPlanApiCaseList.vue | 2 +- .../comonents/api/TestPlanApiScenarioList.vue | 2 +- 13 files changed, 109 insertions(+), 33 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 092cf07e13..5aa683ab4c 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -181,6 +181,21 @@ public class ApiDefinitionService { } } + private List getSameRequest(SaveApiDefinitionRequest request) { + ApiDefinitionExample example = new ApiDefinitionExample(); + if (request.getProtocol().equals(RequestType.HTTP)) { + example.createCriteria().andMethodEqualTo(request.getMethod()).andStatusNotEqualTo("Trash") + .andProtocolEqualTo(request.getProtocol()).andPathEqualTo(request.getPath()) + .andProjectIdEqualTo(request.getProjectId()).andIdNotEqualTo(request.getId()); + return apiDefinitionMapper.selectByExample(example); + } else { + example.createCriteria().andProtocolEqualTo(request.getProtocol()).andStatusNotEqualTo("Trash") + .andNameEqualTo(request.getName()).andProjectIdEqualTo(request.getProjectId()) + .andIdNotEqualTo(request.getId()); + return apiDefinitionMapper.selectByExample(example); + } + } + private ApiDefinition updateTest(SaveApiDefinitionRequest request) { checkNameExist(request); final ApiDefinitionWithBLOBs test = new ApiDefinitionWithBLOBs(); @@ -243,23 +258,30 @@ public class ApiDefinitionService { } } - private ApiDefinition createTest(ApiDefinitionResult request, ApiDefinitionMapper batchMapper) { + private ApiDefinition importCreate(ApiDefinitionResult request, ApiDefinitionMapper batchMapper) { SaveApiDefinitionRequest saveReq = new SaveApiDefinitionRequest(); BeanUtils.copyBean(saveReq, request); - checkNameExist(saveReq); - final ApiDefinitionWithBLOBs test = new ApiDefinitionWithBLOBs(); - BeanUtils.copyBean(test, request); - test.setCreateTime(System.currentTimeMillis()); - test.setUpdateTime(System.currentTimeMillis()); - test.setStatus(APITestStatus.Underway.name()); + final ApiDefinitionWithBLOBs apiDefinition = new ApiDefinitionWithBLOBs(); + BeanUtils.copyBean(apiDefinition, request); + apiDefinition.setCreateTime(System.currentTimeMillis()); + apiDefinition.setUpdateTime(System.currentTimeMillis()); + apiDefinition.setStatus(APITestStatus.Underway.name()); if (request.getUserId() == null) { - test.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); + apiDefinition.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); } else { - test.setUserId(request.getUserId()); + apiDefinition.setUserId(request.getUserId()); } - test.setDescription(request.getDescription()); - batchMapper.insert(test); - return test; + apiDefinition.setDescription(request.getDescription()); + + List sameRequest = getSameRequest(saveReq); + if (CollectionUtils.isEmpty(sameRequest)) { + batchMapper.insert(apiDefinition); + } else { + //如果存在则修改 + apiDefinition.setId(sameRequest.get(0).getId()); + apiDefinitionMapper.updateByPrimaryKey(apiDefinition); + } + return apiDefinition; } @@ -358,12 +380,12 @@ public class ApiDefinitionService { MSException.throwException(Translator.get("parse_data_error")); } if (request.isSaved()) { - importApiTest(request, apiImport); + importApi(request, apiImport); } return apiImport; } - private void importApiTest(ApiTestImportRequest request, ApiDefinitionImport apiImport) { + private void importApi(ApiTestImportRequest request, ApiDefinitionImport apiImport) { SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); ApiDefinitionMapper batchMapper = sqlSession.getMapper(ApiDefinitionMapper.class); List data = apiImport.getData(); @@ -377,7 +399,7 @@ public class ApiDefinitionService { item.setName(item.getName().substring(0, 255)); } item.setNum(num++); - createTest(item, batchMapper); + importCreate(item, batchMapper); if (i % 300 == 0) { sqlSession.flushStatements(); } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java index 27be8985dd..be7594f5ac 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java @@ -207,6 +207,20 @@ public class ApiScenarioReportService { apiScenarioReportMapper.deleteByPrimaryKey(request.getId()); } + public void delete(String id) { + apiScenarioReportDetailMapper.deleteByPrimaryKey(id); + apiScenarioReportMapper.deleteByPrimaryKey(id); + } + + public void deleteByIds(List ids) { + ApiScenarioReportExample example = new ApiScenarioReportExample(); + example.createCriteria().andIdIn(ids); + ApiScenarioReportDetailExample detailExample = new ApiScenarioReportDetailExample(); + detailExample.createCriteria().andReportIdIn(ids); + apiScenarioReportDetailMapper.deleteByExample(detailExample); + apiScenarioReportMapper.deleteByExample(example); + } + public void deleteAPIReportBatch(DeleteAPIReportRequest reportRequest) { List ids = reportRequest.getIds(); if (reportRequest.isSelectAllDate()) { diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.java index 5e3e69e9e3..fa3099b94f 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanApiCaseMapper.java @@ -13,4 +13,6 @@ public interface ExtTestPlanApiCaseMapper { List list(@Param("request") ApiTestCaseRequest request); List getExecResultByPlanId(String planId); + + List getIdsByPlanId(String planId); } \ No newline at end of file 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 d126e3372c..acbbbb07c3 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 @@ -97,4 +97,10 @@ where test_plan_id = #{planId} + + \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.java index edfa92898d..c60c731415 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.java @@ -19,4 +19,6 @@ public interface ExtTestPlanScenarioCaseMapper { List list(@Param("request") TestPlanScenarioRequest request); List getExecResultByPlanId(String planId); + + List getIdsByPlanId(String planId); } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.xml index 05a4cacd42..11767c5e7f 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.xml @@ -86,5 +86,10 @@ test_plan_api_scenario where test_plan_id = #{planId} + \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/track/controller/TestPlanApiCaseController.java b/backend/src/main/java/io/metersphere/track/controller/TestPlanApiCaseController.java index 421f585062..9039e564be 100644 --- a/backend/src/main/java/io/metersphere/track/controller/TestPlanApiCaseController.java +++ b/backend/src/main/java/io/metersphere/track/controller/TestPlanApiCaseController.java @@ -38,10 +38,10 @@ public class TestPlanApiCaseController { return PageUtils.setPageInfo(page, testPlanApiCaseService.relevanceList(request)); } - @GetMapping("/delete/{planId}/{id}") + @GetMapping("/delete/{id}") @RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR) - public int deleteTestCase(@PathVariable String planId, @PathVariable String id) { - return testPlanApiCaseService.delete(planId, id); + public int deleteTestCase(@PathVariable String id) { + return testPlanApiCaseService.delete(id); } @PostMapping("/batch/delete") diff --git a/backend/src/main/java/io/metersphere/track/controller/TestPlanScenarioCaseController.java b/backend/src/main/java/io/metersphere/track/controller/TestPlanScenarioCaseController.java index 75ef25f2b7..83bdf0ba9c 100644 --- a/backend/src/main/java/io/metersphere/track/controller/TestPlanScenarioCaseController.java +++ b/backend/src/main/java/io/metersphere/track/controller/TestPlanScenarioCaseController.java @@ -36,10 +36,10 @@ public class TestPlanScenarioCaseController { return PageUtils.setPageInfo(page, testPlanScenarioCaseService.relevanceList(request)); } - @GetMapping("/delete/{planId}/{id}") + @GetMapping("/delete/{id}") @RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR) - public int deleteTestCase(@PathVariable String planId, @PathVariable String id) { - return testPlanScenarioCaseService.delete(planId, id); + public int deleteTestCase(@PathVariable String id) { + return testPlanScenarioCaseService.delete(id); } @PostMapping("/batch/delete") 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 9e566df6e7..95eec7639d 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java @@ -58,18 +58,24 @@ public class TestPlanApiCaseService { return apiTestCaseService.listSimple(request); } - - - public int delete(String planId, String id) { + public int delete(String id) { apiDefinitionExecResultService.deleteByResourceId(id); TestPlanApiCaseExample example = new TestPlanApiCaseExample(); example.createCriteria() - .andTestPlanIdEqualTo(planId) .andIdEqualTo(id); return testPlanApiCaseMapper.deleteByExample(example); } + public int deleteByPlanId(String planId) { + List ids = extTestPlanApiCaseMapper.getIdsByPlanId(planId); + apiDefinitionExecResultService.deleteByResourceIds(ids); + TestPlanApiCaseExample example = new TestPlanApiCaseExample(); + example.createCriteria() + .andTestPlanIdEqualTo(planId); + return testPlanApiCaseMapper.deleteByExample(example); + } + public void deleteApiCaseBath(TestPlanApiCaseBatchRequest request) { apiDefinitionExecResultService.deleteByResourceIds(request.getIds()); TestPlanApiCaseExample example = new TestPlanApiCaseExample(); diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java index f2d5c74d91..cee39a7677 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java @@ -1,11 +1,14 @@ package io.metersphere.track.service; +import io.metersphere.api.dto.DeleteAPIReportRequest; import io.metersphere.api.dto.automation.ApiScenarioDTO; import io.metersphere.api.dto.automation.ApiScenarioRequest; import io.metersphere.api.dto.automation.RunScenarioRequest; import io.metersphere.api.dto.automation.TestPlanScenarioRequest; import io.metersphere.api.service.ApiAutomationService; +import io.metersphere.api.service.ApiScenarioReportService; import io.metersphere.base.domain.TestPlanApiCase; +import io.metersphere.base.domain.TestPlanApiCaseExample; import io.metersphere.base.domain.TestPlanApiScenario; import io.metersphere.base.domain.TestPlanApiScenarioExample; import io.metersphere.base.mapper.TestPlanApiScenarioMapper; @@ -16,6 +19,7 @@ import io.metersphere.track.request.testcase.TestPlanApiCaseBatchRequest; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; import javax.annotation.Resource; import java.util.ArrayList; @@ -33,6 +37,8 @@ public class TestPlanScenarioCaseService { TestPlanApiScenarioMapper testPlanApiScenarioMapper; @Resource ExtTestPlanScenarioCaseMapper extTestPlanScenarioCaseMapper; + @Resource + ApiScenarioReportService apiScenarioReportService; public List list(TestPlanScenarioRequest request) { request.setProjectId(null); @@ -53,22 +59,26 @@ public class TestPlanScenarioCaseService { return apiAutomationService.list(request); } - public int delete(String planId, String id) { -// apiDefinitionExecResultService.deleteByResourceId(id); + public int delete(String id) { + TestPlanApiScenario testPlanApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(id); + String reportId = testPlanApiScenario.getReportId(); + if (!StringUtils.isEmpty(reportId)) { + apiScenarioReportService.delete(reportId); + } TestPlanApiScenarioExample example = new TestPlanApiScenarioExample(); example.createCriteria() - .andTestPlanIdEqualTo(planId) .andIdEqualTo(id); return testPlanApiScenarioMapper.deleteByExample(example); } public void deleteApiCaseBath(TestPlanApiCaseBatchRequest request) { -// apiDefinitionExecResultService.deleteByResourceIds(request.getIds()); TestPlanApiScenarioExample example = new TestPlanApiScenarioExample(); example.createCriteria() - .andIdIn(request.getIds()) - .andTestPlanIdEqualTo(request.getPlanId()); + .andIdIn(request.getIds()); + List reportIds = testPlanApiScenarioMapper.selectByExample(example).stream() + .map(TestPlanApiScenario::getReportId).collect(Collectors.toList()); + apiScenarioReportService.deleteByIds(reportIds); testPlanApiScenarioMapper.deleteByExample(example); } @@ -93,4 +103,11 @@ public class TestPlanScenarioCaseService { public List getExecResultByPlanId(String planId) { return extTestPlanScenarioCaseMapper.getExecResultByPlanId(planId); } + + public void deleteByPlanId(String planId) { + TestPlanApiCaseBatchRequest request = new TestPlanApiCaseBatchRequest(); + List ids = extTestPlanScenarioCaseMapper.getIdsByPlanId(planId); + request.setIds(ids); + deleteApiCaseBath(request); + } } diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java index fa68a27d6c..72a403bc9b 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java @@ -287,6 +287,8 @@ public class TestPlanService { TestPlan testPlan = getTestPlan(planId); deleteTestCaseByPlanId(planId); testPlanProjectService.deleteTestPlanProjectByPlanId(planId); + testPlanApiCaseService.deleteByPlanId(planId); + testPlanScenarioCaseService.deleteByPlanId(planId); int num = testPlanMapper.deleteByPrimaryKey(planId); List relatedUsers = new ArrayList<>(); AddTestPlanRequest testPlans = new AddTestPlanRequest(); diff --git a/frontend/src/business/components/track/plan/view/comonents/api/TestPlanApiCaseList.vue b/frontend/src/business/components/track/plan/view/comonents/api/TestPlanApiCaseList.vue index b959694497..b308b3aee8 100644 --- a/frontend/src/business/components/track/plan/view/comonents/api/TestPlanApiCaseList.vue +++ b/frontend/src/business/components/track/plan/view/comonents/api/TestPlanApiCaseList.vue @@ -415,7 +415,7 @@ }); }, handleDelete(apiCase) { - this.$get('/test/plan/api/case/delete/' + this.planId + '/' + apiCase.id, () => { + this.$get('/test/plan/api/case/delete/' + apiCase.id, () => { this.$success(this.$t('test_track.cancel_relevance_success')); this.$emit('refresh'); this.initTable(); diff --git a/frontend/src/business/components/track/plan/view/comonents/api/TestPlanApiScenarioList.vue b/frontend/src/business/components/track/plan/view/comonents/api/TestPlanApiScenarioList.vue index 797c67c839..3b0107c7d4 100644 --- a/frontend/src/business/components/track/plan/view/comonents/api/TestPlanApiScenarioList.vue +++ b/frontend/src/business/components/track/plan/view/comonents/api/TestPlanApiScenarioList.vue @@ -192,7 +192,7 @@ this.reportId = row.reportId; }, remove(row) { - this.$get('/test/plan/scenario/case/delete/' + this.planId + '/' + row.id, () => { + this.$get('/test/plan/scenario/case/delete/' + row.id, () => { this.$success(this.$t('test_track.cancel_relevance_success')); this.$emit('refresh'); this.search();