diff --git a/backend/src/main/java/io/metersphere/job/sechedule/IssuesJob.java b/backend/src/main/java/io/metersphere/job/sechedule/IssuesJob.java index 997628f972..2dacdf3c13 100644 --- a/backend/src/main/java/io/metersphere/job/sechedule/IssuesJob.java +++ b/backend/src/main/java/io/metersphere/job/sechedule/IssuesJob.java @@ -1,17 +1,12 @@ package io.metersphere.job.sechedule; -import com.alibaba.fastjson.JSON; -import com.fit2cloud.quartz.anno.QuartzScheduled; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; -import io.metersphere.base.domain.IssuesDao; import io.metersphere.base.domain.TestPlanTestCase; import io.metersphere.base.domain.TestPlanTestCaseWithBLOBs; import io.metersphere.commons.utils.LogUtil; -import io.metersphere.track.service.IssuesService; +import io.metersphere.track.service.TestCaseIssueService; import io.metersphere.track.service.TestPlanTestCaseService; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -19,10 +14,10 @@ import java.util.List; @Component public class IssuesJob { - @Resource - private IssuesService issuesService; @Resource private TestPlanTestCaseService testPlanTestCaseService; + @Resource + private TestCaseIssueService testCaseIssueService; //@QuartzScheduled(fixedDelay = 3600 * 1000) // @Scheduled(fixedDelay = 120 * 1000) @@ -35,16 +30,7 @@ public class IssuesJob { List list = testPlanTestCaseService.listAll(); pages = page.getPages();// 替换成真实的值 list.forEach(l -> { - try { - List issues = issuesService.getIssues(l.getCaseId()); - if (CollectionUtils.isEmpty(issues)) { - return; - } - int issuesCount = issues.size(); - testPlanTestCaseService.updateIssues(issuesCount, l.getPlanId(), l.getCaseId(), JSON.toJSONString(issues)); - } catch (Exception e) { - LogUtil.error("定时任务处理bug数量报错planId: {}, message: {}", l.getPlanId(), ExceptionUtils.getStackTrace(e)); - } + testCaseIssueService.updateIssuesCount(l.getCaseId()); }); } LogUtil.info("测试计划-测试用例同步缺陷信息结束"); diff --git a/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java b/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java index 2ebb6956d5..5fa2dd22dc 100644 --- a/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java +++ b/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java @@ -219,13 +219,23 @@ public class PerformanceTestService { } } - private LoadTestWithBLOBs saveLoadTest(SaveTestPlanRequest request) { - - LoadTestExample example = new LoadTestExample(); - example.createCriteria().andNameEqualTo(request.getName()).andProjectIdEqualTo(request.getProjectId()); - if (loadTestMapper.countByExample(example) > 0) { - MSException.throwException(Translator.get("load_test_already_exists")); + private void checkExist(TestPlanRequest request) { + if (request.getName() != null) { + LoadTestExample example = new LoadTestExample(); + LoadTestExample.Criteria criteria = example.createCriteria(); + criteria.andNameEqualTo(request.getName()) + .andProjectIdEqualTo(request.getProjectId()); + if (StringUtils.isNotBlank(request.getId())) { + criteria.andIdNotEqualTo(request.getId()); + } + if (loadTestMapper.selectByExample(example).size() > 0) { + MSException.throwException(Translator.get("plan_name_already_exists")); + } } + } + + private LoadTestWithBLOBs saveLoadTest(SaveTestPlanRequest request) { + checkExist(request); final LoadTestWithBLOBs loadTest = new LoadTestWithBLOBs(); loadTest.setUserId(SessionUtils.getUser().getId()); @@ -250,7 +260,7 @@ public class PerformanceTestService { public LoadTest edit(EditTestPlanRequest request, List files) { checkQuota(request, false); - // + checkExist(request); String testId = request.getId(); LoadTestWithBLOBs loadTest = loadTestMapper.selectByPrimaryKey(testId); if (loadTest == null) { diff --git a/backend/src/main/java/io/metersphere/track/controller/TestCaseIssuesController.java b/backend/src/main/java/io/metersphere/track/controller/TestCaseIssuesController.java index 7d7d879c53..d0a8237fae 100644 --- a/backend/src/main/java/io/metersphere/track/controller/TestCaseIssuesController.java +++ b/backend/src/main/java/io/metersphere/track/controller/TestCaseIssuesController.java @@ -1,16 +1,10 @@ package io.metersphere.track.controller; -import com.alibaba.fastjson.JSON; -import io.metersphere.base.domain.IssuesDao; -import io.metersphere.base.mapper.ext.ExtTestPlanTestCaseMapper; import io.metersphere.commons.constants.OperLogConstants; -import io.metersphere.commons.utils.LogUtil; import io.metersphere.log.annotation.MsAuditLog; import io.metersphere.track.dto.TestCaseDTO; import io.metersphere.track.request.issues.IssuesRelevanceRequest; -import io.metersphere.track.service.IssuesService; import io.metersphere.track.service.TestCaseIssueService; -import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -25,10 +19,6 @@ public class TestCaseIssuesController { @Resource private TestCaseIssueService testCaseIssueService; - @Resource - private IssuesService issuesService; - @Resource - private ExtTestPlanTestCaseMapper extTestPlanTestCaseMapper; @PostMapping("/list") public List list(@RequestBody IssuesRelevanceRequest request) { @@ -39,19 +29,5 @@ public class TestCaseIssuesController { @MsAuditLog(module = "track_test_case", type = OperLogConstants.ASSOCIATE_ISSUE, content = "#msClass.getLogDetails(#request)", msClass = TestCaseIssueService.class) public void relate(@RequestBody IssuesRelevanceRequest request) { testCaseIssueService.relate(request); - try { - List issues = issuesService.getIssues(request.getCaseId()); - if (org.apache.commons.collections4.CollectionUtils.isEmpty(issues)) { - LogUtil.error(request.getCaseId() + "下的缺陷为空"); - } - int issuesCount = issues.size(); - this.updateIssues(issuesCount, "", request.getCaseId(), JSON.toJSONString(issues)); - } catch (Exception e) { - LogUtil.error("处理bug数量报错caseId: {}, message: {}", request.getCaseId(), ExceptionUtils.getStackTrace(e)); - } - } - - public void updateIssues(int issuesCount, String id, String caseId, String issues) { - extTestPlanTestCaseMapper.update(issuesCount, id, caseId, issues); } } diff --git a/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java b/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java index c454aa1eeb..b1d71197d7 100644 --- a/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java +++ b/backend/src/main/java/io/metersphere/track/issue/AbstractIssuePlatform.java @@ -21,6 +21,7 @@ import io.metersphere.service.ResourceService; import io.metersphere.service.UserService; import io.metersphere.track.request.testcase.IssuesRequest; import io.metersphere.track.request.testcase.IssuesUpdateRequest; +import io.metersphere.track.service.TestCaseIssueService; import io.metersphere.track.service.TestCaseService; import org.apache.commons.lang3.StringUtils; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; @@ -44,12 +45,14 @@ import java.util.Optional; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; public abstract class AbstractIssuePlatform implements IssuesPlatform { private static RestTemplate restTemplate; protected IntegrationService integrationService; + protected TestCaseIssueService testCaseIssueService; protected TestCaseIssuesMapper testCaseIssuesMapper; protected ProjectService projectService; protected TestCaseService testCaseService; @@ -107,6 +110,7 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { this.issuesMapper = CommonBeanFactory.getBean(IssuesMapper.class); this.extIssuesMapper = CommonBeanFactory.getBean(ExtIssuesMapper.class); this.resourceService = CommonBeanFactory.getBean(ResourceService.class); + this.testCaseIssueService = CommonBeanFactory.getBean(TestCaseIssueService.class); this.restTemplateIgnoreSSL = restTemplate; } @@ -151,6 +155,7 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { testCaseIssues.setIssuesId(issuesId); testCaseIssues.setTestCaseId(caseId); testCaseIssuesMapper.insert(testCaseIssues); + testCaseIssueService.updateIssuesCount(caseId); } } @@ -164,17 +169,23 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { String issuesId = issuesRequest.getId(); if (StringUtils.isNotBlank(issuesRequest.getTestCaseId())) { insertTestCaseIssues(issuesId, issuesRequest.getTestCaseId()); - } else { + } else { List testCaseIds = issuesRequest.getTestCaseIds(); TestCaseIssuesExample example = new TestCaseIssuesExample(); example.createCriteria().andIssuesIdEqualTo(issuesId); - testCaseIssuesMapper.deleteByExample(example); + List testCaseIssues = testCaseIssuesMapper.selectByExample(example); + List deleteCaseIds = testCaseIssues.stream().map(TestCaseIssues::getTestCaseId).collect(Collectors.toList()); + if (!CollectionUtils.isEmpty(testCaseIds)) { + deleteCaseIds.removeAll(testCaseIds); + } + testCaseIssuesMapper.deleteByExample(example); + deleteCaseIds.forEach(testCaseIssueService::updateIssuesCount); if (!CollectionUtils.isEmpty(testCaseIds)) { testCaseIds.forEach(caseId -> { insertTestCaseIssues(issuesId, caseId); }); } - } + } } protected void insertIssuesWithoutContext(String id, IssuesUpdateRequest issuesRequest) { diff --git a/backend/src/main/java/io/metersphere/track/service/IssuesService.java b/backend/src/main/java/io/metersphere/track/service/IssuesService.java index 6df3bc81ba..3ee4bf09b9 100644 --- a/backend/src/main/java/io/metersphere/track/service/IssuesService.java +++ b/backend/src/main/java/io/metersphere/track/service/IssuesService.java @@ -22,7 +22,6 @@ import io.metersphere.notice.service.NoticeSendService; import io.metersphere.service.IntegrationService; import io.metersphere.service.IssueTemplateService; import io.metersphere.service.ProjectService; -import io.metersphere.service.SystemParameterService; import io.metersphere.track.dto.PlanReportIssueDTO; import io.metersphere.track.dto.TestCaseReportStatusResultDTO; import io.metersphere.track.dto.TestPlanFunctionResultReportDTO; @@ -36,7 +35,6 @@ import io.metersphere.track.request.testcase.IssuesUpdateRequest; import io.metersphere.track.request.testcase.TestCaseBatchRequest; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -77,7 +75,7 @@ public class IssuesService { @Resource private TestCaseMapper testCaseMapper; @Resource - private SystemParameterService systemParameterService; + private TestCaseIssueService testCaseIssueService; @Resource private TestPlanTestCaseService testPlanTestCaseService; @@ -95,16 +93,7 @@ public class IssuesService { platform.addIssue(issuesRequest); }); issuesRequest.getTestCaseIds().forEach(l -> { - try { - List issues = this.getIssues(l); - if (org.apache.commons.collections4.CollectionUtils.isEmpty(issues)) { - LogUtil.error(l + "下的缺陷为空"); - } - int issuesCount = issues.size(); - testPlanTestCaseService.updateIssues(issuesCount, "", l, JSON.toJSONString(issues)); - } catch (Exception e) { - LogUtil.error("处理bug数量报错caseId: {}, message: {}", l, ExceptionUtils.getStackTrace(e)); - } + testCaseIssueService.updateIssuesCount(l); }); } @@ -153,10 +142,10 @@ public class IssuesService { String userId = testCase.getMaintainer(); issueRequest.setOrganizationId(orgId); issueRequest.setUserId(userId); - return getIssuesByProject(issueRequest, project); + return getIssuesByProjectIdOrCaseId(issueRequest); } - public List getIssuesByProject(IssuesRequest issueRequest, Project project) { + public List getIssuesByProjectIdOrCaseId(IssuesRequest issueRequest) { List issues; if (StringUtils.isNotBlank(issueRequest.getProjectId())) { issues = extIssuesMapper.getIssues(issueRequest); @@ -291,6 +280,7 @@ public class IssuesService { TestCaseIssuesExample example = new TestCaseIssuesExample(); example.createCriteria().andTestCaseIdEqualTo(caseId).andIssuesIdEqualTo(id); testCaseIssuesMapper.deleteByExample(example); + testCaseIssueService.updateIssuesCount(caseId); } public void delete(String id) { @@ -404,16 +394,7 @@ public class IssuesService { List list = testPlanTestCaseService.listAll(); pages = page.getPages();// 替换成真实的值 list.forEach(l -> { - try { - List issues = this.getIssues(l.getCaseId()); - if (org.apache.commons.collections4.CollectionUtils.isEmpty(issues)) { - return; - } - int issuesCount = issues.size(); - testPlanTestCaseService.updateIssues(issuesCount, l.getPlanId(), l.getCaseId(), JSON.toJSONString(issues)); - } catch (Exception e) { - LogUtil.error("定时任务处理bug数量报错planId: {}, message: {}", l.getPlanId(), ExceptionUtils.getStackTrace(e)); - } + testCaseIssueService.updateIssuesCount(l.getCaseId()); }); } LogUtil.info("测试计划-测试用例同步缺陷信息结束"); diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseIssueService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseIssueService.java index fd005ef304..7edaa53445 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseIssueService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseIssueService.java @@ -1,9 +1,11 @@ package io.metersphere.track.service; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.IssuesMapper; import io.metersphere.base.mapper.TestCaseIssuesMapper; +import io.metersphere.base.mapper.TestPlanTestCaseMapper; import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.track.dto.TestCaseDTO; import io.metersphere.track.request.issues.IssuesRelevanceRequest; @@ -30,6 +32,11 @@ public class TestCaseIssueService { private TestCaseService testCaseService; @Resource private IssuesMapper issuesMapper; + @Resource + @Lazy + private IssuesService issuesService; + @Resource + private TestPlanTestCaseMapper testPlanTestCaseMapper; public void delTestCaseIssues(String testCaseId) { @@ -80,6 +87,21 @@ public class TestCaseIssueService { }); } } + + updateIssuesCount(request.getCaseId()); + } + + public void updateIssuesCount(String caseId) { + List issues = issuesService.getIssues(caseId); + int issuesCount = issues.size(); + TestPlanTestCaseExample example = new TestPlanTestCaseExample(); + example.createCriteria().andCaseIdEqualTo(caseId); + TestPlanTestCaseWithBLOBs testPlanTestCase = new TestPlanTestCaseWithBLOBs(); + testPlanTestCase.setIssuesCount(issuesCount); + if (!CollectionUtils.isEmpty(issues)) { + testPlanTestCase.setIssues(JSONObject.toJSONString(issues)); + } + testPlanTestCaseMapper.updateByExampleSelective(testPlanTestCase, example); } public void create(String caseId, String issueId) { diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanTestCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanTestCaseService.java index 2326e41f56..79dc991719 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanTestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanTestCaseService.java @@ -71,7 +71,7 @@ public class TestPlanTestCaseService { } public void updateIssues(int issuesCount, String id, String caseId, String issues) { - extTestPlanTestCaseMapper.update(issuesCount, id, caseId, issues); + extTestPlanTestCaseMapper.update(issuesCount, id, caseId, issues);//to } public List list(QueryTestPlanCaseRequest request) {