diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupApiReportDetailServiceImpl.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupApiReportDetailServiceImpl.java new file mode 100644 index 0000000000..5debcf31b1 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupApiReportDetailServiceImpl.java @@ -0,0 +1,114 @@ +package io.metersphere.api.service; + +import io.metersphere.api.domain.*; +import io.metersphere.api.mapper.*; +import jakarta.annotation.Resource; +import org.apache.commons.collections4.CollectionUtils; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Component +public class CleanupApiReportDetailServiceImpl { + + @Resource + private ApiReportMapper apiReportMapper; + @Resource + private ApiReportStepMapper apiReportStepMapper; + @Resource + private ApiReportDetailMapper apiReportDetailMapper; + @Resource + private ApiReportLogMapper apiReportLogMapper; + @Resource + private ApiScenarioReportMapper apiScenarioReportMapper; + @Resource + private ApiScenarioReportStepMapper apiScenarioReportStepMapper; + @Resource + private ApiScenarioReportDetailMapper apiScenarioReportDetailMapper; + @Resource + private ApiScenarioReportLogMapper apiScenarioReportLogMapper; + @Resource + private ApiScenarioReportDetailBlobMapper apiScenarioReportDetailBlobMapper; + @Resource + private ApiReportRelateTaskMapper apiReportRelateTaskMapper; + + @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) + public void cleanApiReportOrDetail(List cleanIds) { + ApiReportExample reportExample = new ApiReportExample(); + reportExample.createCriteria().andIdIn(cleanIds); + ApiReport report = new ApiReport(); + report.setDeleted(true); + apiReportMapper.updateByExampleSelective(report, reportExample); + // 任务执行结果存在报告,明细做保留 + List taskReportIds = getTaskReportIds(cleanIds); + cleanIds.removeAll(taskReportIds); + deleteApiReportDetail(cleanIds); + } + + @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) + public void cleanScenarioReportOrDetail(List cleanIds) { + ApiScenarioReportExample reportExample = new ApiScenarioReportExample(); + reportExample.createCriteria().andIdIn(cleanIds); + ApiScenarioReport report = new ApiScenarioReport(); + report.setDeleted(true); + apiScenarioReportMapper.updateByExampleSelective(report, reportExample); + // 任务执行结果存在报告,明细做保留 + List taskReportIds = getTaskReportIds(cleanIds); + cleanIds.removeAll(taskReportIds); + deleteScenarioReportDetail(cleanIds); + } + + /** + * 获取任务报告ID + * + * @param reportIds 报告ID集合 + * @return 任务报告ID集合 + */ + private List getTaskReportIds(List reportIds) { + ApiReportRelateTaskExample example = new ApiReportRelateTaskExample(); + example.createCriteria().andReportIdIn(reportIds); + List relateTasks = apiReportRelateTaskMapper.selectByExample(example); + return relateTasks.stream().map(ApiReportRelateTask::getReportId).toList(); + } + + /** + * 清理接口报告明细 + * @param ids 报告ID集合 + */ + private void deleteApiReportDetail(List ids) { + if (CollectionUtils.isNotEmpty(ids)) { + ApiReportStepExample stepExample = new ApiReportStepExample(); + stepExample.createCriteria().andReportIdIn(ids); + apiReportStepMapper.deleteByExample(stepExample); + ApiReportDetailExample detailExample = new ApiReportDetailExample(); + detailExample.createCriteria().andReportIdIn(ids); + apiReportDetailMapper.deleteByExample(detailExample); + ApiReportLogExample logExample = new ApiReportLogExample(); + logExample.createCriteria().andReportIdIn(ids); + apiReportLogMapper.deleteByExample(logExample); + } + } + + /** + * 清理场景报告明细 + * @param ids 报告ID集合 + */ + private void deleteScenarioReportDetail(List ids) { + if (CollectionUtils.isNotEmpty(ids)) { + ApiScenarioReportStepExample stepExample = new ApiScenarioReportStepExample(); + stepExample.createCriteria().andReportIdIn(ids); + apiScenarioReportStepMapper.deleteByExample(stepExample); + ApiScenarioReportDetailExample detailExample = new ApiScenarioReportDetailExample(); + detailExample.createCriteria().andReportIdIn(ids); + apiScenarioReportDetailMapper.deleteByExample(detailExample); + ApiScenarioReportDetailBlobExample blobExample = new ApiScenarioReportDetailBlobExample(); + blobExample.createCriteria().andReportIdIn(ids); + apiScenarioReportDetailBlobMapper.deleteByExample(blobExample); + ApiScenarioReportLogExample logExample = new ApiScenarioReportLogExample(); + logExample.createCriteria().andReportIdIn(ids); + apiScenarioReportLogMapper.deleteByExample(logExample); + } + } +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupApiReportServiceImpl.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupApiReportServiceImpl.java index fe314d5be1..c37a944bb7 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupApiReportServiceImpl.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupApiReportServiceImpl.java @@ -1,9 +1,10 @@ package io.metersphere.api.service; -import io.metersphere.api.domain.*; -import io.metersphere.api.mapper.*; +import io.metersphere.api.mapper.ExtApiReportMapper; +import io.metersphere.api.mapper.ExtApiScenarioReportMapper; import io.metersphere.sdk.constants.ProjectApplicationType; import io.metersphere.sdk.util.LogUtils; +import io.metersphere.sdk.util.SubListUtils; import io.metersphere.system.service.BaseCleanUpReport; import jakarta.annotation.Resource; import org.apache.commons.collections4.CollectionUtils; @@ -19,108 +20,31 @@ import static io.metersphere.sdk.util.ShareUtil.getCleanDate; @Transactional(rollbackFor = Exception.class) public class CleanupApiReportServiceImpl implements BaseCleanUpReport { - @Resource - private ApiReportMapper apiReportMapper; @Resource private ExtApiReportMapper extApiReportMapper; @Resource - private ApiReportStepMapper apiReportStepMapper; - @Resource - private ApiReportDetailMapper apiReportDetailMapper; - @Resource - private ApiReportLogMapper apiReportLogMapper; - @Resource - private ApiScenarioReportMapper apiScenarioReportMapper; - @Resource private ExtApiScenarioReportMapper extApiScenarioReportMapper; @Resource - private ApiScenarioReportStepMapper apiScenarioReportStepMapper; - @Resource - private ApiScenarioReportDetailMapper apiScenarioReportDetailMapper; - @Resource - private ApiScenarioReportLogMapper apiScenarioReportLogMapper; - @Resource - private ApiScenarioReportDetailBlobMapper apiScenarioReportDetailBlobMapper; - @Resource - private ApiReportRelateTaskMapper apiReportRelateTaskMapper; + private CleanupApiReportDetailServiceImpl cleanupApiReportDetailService; @Override public void cleanReport(Map map, String projectId) { LogUtils.info("清理当前项目[" + projectId + "]相关接口测试报告"); String expr = map.get(ProjectApplicationType.API.API_CLEAN_REPORT.name()); long timeMills = getCleanDate(expr); - int apiReportCount = extApiReportMapper.countApiReportByTime(timeMills, projectId); - while (apiReportCount > 0) { - List ids = extApiReportMapper.selectApiReportByProjectIdAndTime(timeMills, projectId); - ApiReportExample reportExample = new ApiReportExample(); - reportExample.createCriteria().andIdIn(ids); - ApiReport report = new ApiReport(); - report.setDeleted(true); - apiReportMapper.updateByExampleSelective(report, reportExample); - // 任务执行结果存在报告,明细做保留 - List taskReportIds = getTaskReportIds(ids); - ids.removeAll(taskReportIds); - if (CollectionUtils.isNotEmpty(ids)) { - deleteApiReport(ids); - } - apiReportCount = extApiReportMapper.countApiReportByTime(timeMills, projectId); + List apiReportIds = extApiReportMapper.selectApiReportByProjectIdAndTime(timeMills, projectId); + List scenarioReportIds = extApiScenarioReportMapper.selectApiReportByProjectIdAndTime(timeMills, projectId); + LogUtils.info("清理当前项目[" + projectId + "]相关接口测试报告, 共[" + (apiReportIds.size() + scenarioReportIds.size()) + "]条"); + if (CollectionUtils.isNotEmpty(apiReportIds)) { + SubListUtils.dealForSubList(apiReportIds, 100, (subIds) -> { + cleanupApiReportDetailService.cleanApiReportOrDetail(subIds); + }); } - int scenarioReportCount = extApiScenarioReportMapper.countScenarioReportByTime(timeMills, projectId); - while (scenarioReportCount > 0) { - List ids = extApiScenarioReportMapper.selectApiReportByProjectIdAndTime(timeMills, projectId); - ApiScenarioReportExample reportExample = new ApiScenarioReportExample(); - reportExample.createCriteria().andIdIn(ids); - ApiScenarioReport report = new ApiScenarioReport(); - report.setDeleted(true); - apiScenarioReportMapper.updateByExampleSelective(report, reportExample); - // 任务执行结果存在报告,明细做保留 - List taskReportIds = getTaskReportIds(ids); - ids.removeAll(taskReportIds); - if (CollectionUtils.isNotEmpty(ids)) { - deleteScenarioReport(ids); - } - scenarioReportCount = extApiScenarioReportMapper.countScenarioReportByTime(timeMills, projectId); + if (CollectionUtils.isNotEmpty(scenarioReportIds)) { + SubListUtils.dealForSubList(scenarioReportIds, 100, (subIds) -> { + cleanupApiReportDetailService.cleanScenarioReportOrDetail(subIds); + }); } + LogUtils.info("清理当前项目[" + projectId + "]相关接口测试报告结束!"); } - - /** - * 获取任务报告ID - * - * @param reportIds 报告ID集合 - * @return 任务报告ID集合 - */ - private List getTaskReportIds(List reportIds) { - ApiReportRelateTaskExample example = new ApiReportRelateTaskExample(); - example.createCriteria().andReportIdIn(reportIds); - List relateTasks = apiReportRelateTaskMapper.selectByExample(example); - return relateTasks.stream().map(ApiReportRelateTask::getReportId).toList(); - } - - private void deleteApiReport(List ids) { - ApiReportStepExample stepExample = new ApiReportStepExample(); - stepExample.createCriteria().andReportIdIn(ids); - apiReportStepMapper.deleteByExample(stepExample); - ApiReportDetailExample detailExample = new ApiReportDetailExample(); - detailExample.createCriteria().andReportIdIn(ids); - apiReportDetailMapper.deleteByExample(detailExample); - ApiReportLogExample logExample = new ApiReportLogExample(); - logExample.createCriteria().andReportIdIn(ids); - apiReportLogMapper.deleteByExample(logExample); - } - - private void deleteScenarioReport(List ids) { - ApiScenarioReportStepExample stepExample = new ApiScenarioReportStepExample(); - stepExample.createCriteria().andReportIdIn(ids); - apiScenarioReportStepMapper.deleteByExample(stepExample); - ApiScenarioReportDetailExample detailExample = new ApiScenarioReportDetailExample(); - detailExample.createCriteria().andReportIdIn(ids); - apiScenarioReportDetailMapper.deleteByExample(detailExample); - ApiScenarioReportDetailBlobExample blobExample = new ApiScenarioReportDetailBlobExample(); - blobExample.createCriteria().andReportIdIn(ids); - apiScenarioReportDetailBlobMapper.deleteByExample(blobExample); - ApiScenarioReportLogExample logExample = new ApiScenarioReportLogExample(); - logExample.createCriteria().andReportIdIn(ids); - apiScenarioReportLogMapper.deleteByExample(logExample); - } - } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupTaskResultServiceImpl.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupTaskResultServiceImpl.java index d02469e727..224c6dcbc9 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupTaskResultServiceImpl.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupTaskResultServiceImpl.java @@ -51,6 +51,7 @@ public class CleanupTaskResultServiceImpl implements BaseCleanUpReport { } List cleanTaskItemIds = execTaskItems.stream().map(ExecTaskItem::getId).toList(); List cleanIds = ListUtils.union(cleanTaskIds, cleanTaskItemIds); + LogUtils.info("清理当前项目[" + projectId + "]任务中心执行结果, 共[" + cleanIds.size() + "]条"); if (CollectionUtils.isNotEmpty(cleanIds)) { // 清理任务-报告关系表 SubListUtils.dealForSubList(cleanIds, 100, ids -> { @@ -59,6 +60,6 @@ public class CleanupTaskResultServiceImpl implements BaseCleanUpReport { apiReportRelateTaskMapper.deleteByExample(example); }); } - LogUtils.info("清理当前项目[" + projectId + "]任务中心执行结果结束!"); + LogUtils.info("清理当前项目[" + projectId + "]任务中心执行结果结束!"); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupTaskServiceImpl.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupTaskServiceImpl.java index 0b2c3859d1..2de90cb4ee 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupTaskServiceImpl.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/CleanupTaskServiceImpl.java @@ -49,6 +49,7 @@ public class CleanupTaskServiceImpl implements BaseCleanUpReport { execTaskItems = execTaskItemMapper.selectByExample(itemExample); } List cleanTaskItemIds = execTaskItems.stream().map(ExecTaskItem::getId).toList(); + LogUtils.info("清理当前项目[" + projectId + "]即时任务, 共[" + (cleanTaskIds.size() + cleanTaskItemIds.size()) + "]条"); if (CollectionUtils.isNotEmpty(cleanTaskIds)) { ExecTaskExample example = new ExecTaskExample(); example.createCriteria().andIdIn(cleanTaskIds); @@ -63,6 +64,6 @@ public class CleanupTaskServiceImpl implements BaseCleanUpReport { execTaskItem.setDeleted(true); execTaskItemMapper.updateByExampleSelective(execTaskItem, example); } - LogUtils.info("清理当前项目[" + projectId + "]即时任务结束!"); + LogUtils.info("清理当前项目[" + projectId + "]即时任务结束!"); } } diff --git a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/CleanupTestPlanReportServiceImpl.java b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/CleanupTestPlanReportServiceImpl.java index 2ed47888b5..ebd41aaae8 100644 --- a/backend/services/test-plan/src/main/java/io/metersphere/plan/service/CleanupTestPlanReportServiceImpl.java +++ b/backend/services/test-plan/src/main/java/io/metersphere/plan/service/CleanupTestPlanReportServiceImpl.java @@ -24,12 +24,12 @@ public class CleanupTestPlanReportServiceImpl implements BaseCleanUpReport { @Override public void cleanReport(Map map, String projectId) { - LogUtils.info("清理当前项目[" + projectId + "]相关测试计划报告开始:"); + LogUtils.info("清理当前项目[" + projectId + "]测试计划报告"); String expr = map.get(ProjectApplicationType.TEST_PLAN.TEST_PLAN_CLEAN_REPORT.name()); long timeMills = getCleanDate(expr); List ids = extTestPlanReportMapper.selectReportIdByProjectIdAndTime(timeMills, projectId); - LogUtils.info("清理当前项目[" + projectId + "]相关测试计划报告,共[" + ids.size() + "]条"); + LogUtils.info("清理当前项目[" + projectId + "]测试计划报告, 共[" + ids.size() + "]条"); testPlanReportService.cleanAndDeleteReport(ids); - LogUtils.info("清理当前项目[" + projectId + "]相关测试计划报告结束!"); + LogUtils.info("清理当前项目[" + projectId + "]测试计划报告结束!"); } }