From 26502d879179775c70a7d8e131a1db09f17935e4 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Mon, 20 Jun 2022 15:16:01 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=87=AA=E5=AE=9A=E4=B9=89=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E9=80=89=E9=A1=B9=E5=90=8E=EF=BC=8C=E6=A8=A1=E6=9D=BF=E9=87=8C?= =?UTF-8?q?=E7=9A=84=E9=80=89=E9=A1=B9=E6=B2=A1=E6=9C=89=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1013993 --user=陈建星 【测试跟踪】github #14654项目设置-模板管理-缺陷模板管理:系统自带的状态字段-添加了自定义字段应用到default缺陷模板中,新增字段未同步 https://www.tapd.cn/55049933/s/1186786 --- .../io/metersphere/dto/CustomFieldDao.java | 2 + .../service/CustomFieldService.java | 11 ++- .../service/CustomFieldTemplateService.java | 39 ++++++-- .../service/IssueTemplateService.java | 89 ++++++++++--------- .../service/TestCaseTemplateService.java | 78 ++++++++-------- 5 files changed, 129 insertions(+), 90 deletions(-) diff --git a/backend/src/main/java/io/metersphere/dto/CustomFieldDao.java b/backend/src/main/java/io/metersphere/dto/CustomFieldDao.java index bdab1f71dd..5c64c67a98 100644 --- a/backend/src/main/java/io/metersphere/dto/CustomFieldDao.java +++ b/backend/src/main/java/io/metersphere/dto/CustomFieldDao.java @@ -17,5 +17,7 @@ public class CustomFieldDao extends CustomField { private String customData; + private String originGlobalId; + private String key; } diff --git a/backend/src/main/java/io/metersphere/service/CustomFieldService.java b/backend/src/main/java/io/metersphere/service/CustomFieldService.java index ed0b8d00ab..b72966c8c8 100644 --- a/backend/src/main/java/io/metersphere/service/CustomFieldService.java +++ b/backend/src/main/java/io/metersphere/service/CustomFieldService.java @@ -103,12 +103,15 @@ public class CustomFieldService { public void update(CustomField customField) { if (customField.getGlobal() != null && customField.getGlobal()) { - // 如果是全局字段,则创建对应工作空间字段 - add(customField); + CustomFieldDao customFieldDao = new CustomFieldDao(); + BeanUtils.copyBean(customFieldDao, customField); + customFieldDao.setOriginGlobalId(customField.getId()); + // 如果是全局字段,则创建对应项目字段 + add(customFieldDao); if (StringUtils.equals(customField.getScene(), TemplateConstants.FieldTemplateScene.TEST_CASE.name())) { - testCaseTemplateService.handleSystemFieldCreate(customField); + testCaseTemplateService.handleSystemFieldCreate(customFieldDao); } else { - issueTemplateService.handleSystemFieldCreate(customField); + issueTemplateService.handleSystemFieldCreate(customFieldDao); } } else { checkExist(customField); diff --git a/backend/src/main/java/io/metersphere/service/CustomFieldTemplateService.java b/backend/src/main/java/io/metersphere/service/CustomFieldTemplateService.java index c4f6cf5585..1041264555 100644 --- a/backend/src/main/java/io/metersphere/service/CustomFieldTemplateService.java +++ b/backend/src/main/java/io/metersphere/service/CustomFieldTemplateService.java @@ -21,7 +21,6 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; import java.util.UUID; -import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) @@ -72,17 +71,19 @@ public class CustomFieldTemplateService { } } - public List getSystemFieldCreateTemplate(CustomField customField, String scene) { - List globalField = customFieldService.getGlobalField(scene); - for (int i = 0; i < globalField.size(); i++) { - // 替换全局的字段 - if (StringUtils.equals(globalField.get(i).getName(), customField.getName())) { - globalField.set(i, customField); + public List getSystemFieldCreateTemplate(CustomFieldDao customField, String templateId) { + CustomFieldTemplateExample example = new CustomFieldTemplateExample(); + example.createCriteria().andTemplateIdEqualTo(templateId); + // 获取全局模板的关联关系 + List fieldTemplates = customFieldTemplateMapper.selectByExample(example); + for (int i = 0; i < fieldTemplates.size(); i++) { + // 将全局字段替换成项目下的字段 + if (StringUtils.equals(fieldTemplates.get(i).getFieldId(), customField.getOriginGlobalId())) { + fieldTemplates.get(i).setFieldId(customField.getId()); break; } } - List fieldIds = globalField.stream().map(CustomField::getId).collect(Collectors.toList()); - return getFieldTemplateByFieldIds(fieldIds); + return fieldTemplates; } public void updateFieldIdByTemplate(String templateId, String originId , String fieldId) { @@ -135,4 +136,24 @@ public class CustomFieldTemplateService { String fieldId = customFieldTemplate.getFieldId(); return customFieldMapper.selectByPrimaryKey(fieldId); } + + /** + * 将原来全局字段与模板的关联 + * 改为项目下字段与模板的关联 + * @param customField + * @param templateIds + * @return + */ + public int updateProjectTemplateGlobalField(CustomFieldDao customField, List templateIds) { + if (CollectionUtils.isEmpty(templateIds)) { + return 0; + } + CustomFieldTemplateExample example = new CustomFieldTemplateExample(); + example.createCriteria() + .andFieldIdEqualTo(customField.getOriginGlobalId()) + .andTemplateIdIn(templateIds); + CustomFieldTemplate customFieldTemplate = new CustomFieldTemplate(); + customFieldTemplate.setFieldId(customField.getId()); + return customFieldTemplateMapper.updateByExampleSelective(customFieldTemplate, example); + } } diff --git a/backend/src/main/java/io/metersphere/service/IssueTemplateService.java b/backend/src/main/java/io/metersphere/service/IssueTemplateService.java index 2cd60d437d..0ac89d6f46 100644 --- a/backend/src/main/java/io/metersphere/service/IssueTemplateService.java +++ b/backend/src/main/java/io/metersphere/service/IssueTemplateService.java @@ -26,10 +26,8 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.UUID; +import java.util.*; +import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) @@ -89,7 +87,7 @@ public class IssueTemplateService extends TemplateBaseService { public void update(UpdateIssueTemplateRequest request) { if (request.getGlobal() != null && request.getGlobal()) { String originId = request.getId(); - // 如果是全局字段,则创建对应工作空间字段 + // 如果是全局字段,则创建对应项目字段 String id = add(request); projectService.updateIssueTemplate(originId, id, request.getProjectId()); } else { @@ -105,51 +103,58 @@ public class IssueTemplateService extends TemplateBaseService { } /** - * 获取该工作空间的系统模板 - * - 如果没有,则创建该工作空间模板,并关联默认的字段 + * 获取该项目下的系统模板 + * - 如果没有,则创建该工项目模板,并关联默认的字段 * - 如果有,则更新原来关联的 fieldId * * @param customField */ - public void handleSystemFieldCreate(CustomField customField) { - IssueTemplate workspaceSystemTemplate = getWorkspaceSystemTemplate(customField.getProjectId()); - if (workspaceSystemTemplate == null) { - createTemplateWithUpdateField(customField.getProjectId(), customField); - } else { - updateRelateWithUpdateField(workspaceSystemTemplate, customField); - } - } - - private IssueTemplate getWorkspaceSystemTemplate(String workspaceId) { + public void handleSystemFieldCreate(CustomFieldDao customField) { IssueTemplateExample example = new IssueTemplateExample(); example.createCriteria() - .andProjectIdEqualTo(workspaceId) - .andSystemEqualTo(true); + .andGlobalEqualTo(true); + example.or(example.createCriteria() + .andProjectIdEqualTo(customField.getProjectId())); List issueTemplates = issueTemplateMapper.selectByExampleWithBLOBs(example); - if (CollectionUtils.isNotEmpty(issueTemplates)) { - return issueTemplates.get(0); + + Map> templatesMap = issueTemplates.stream() + .collect(Collectors.groupingBy(IssueTemplate::getGlobal)); + + // 获取全局模板 + List globalTemplates = templatesMap.get(true); + // 获取当前工作空间下模板 + List projectTemplates = templatesMap.get(false); + + globalTemplates.forEach(global -> { + List projectTemplate = null; + if (CollectionUtils.isNotEmpty(projectTemplates)) { + projectTemplate = projectTemplates.stream() + .filter(i -> i.getName().equals(global.getName())) + .collect(Collectors.toList()); + } + // 如果没有项目级别的模板就创建 + if (CollectionUtils.isEmpty(projectTemplate)) { + IssueTemplate template = new IssueTemplate(); + BeanUtils.copyBean(template, global); + template.setId(UUID.randomUUID().toString()); + template.setCreateTime(System.currentTimeMillis()); + template.setUpdateTime(System.currentTimeMillis()); + template.setCreateUser(SessionUtils.getUserId()); + template.setGlobal(false); + template.setProjectId(customField.getProjectId()); + issueTemplateMapper.insert(template); + + List customFieldTemplate = + customFieldTemplateService.getSystemFieldCreateTemplate(customField, global.getId()); + + customFieldTemplateService.create(customFieldTemplate, template.getId(), + TemplateConstants.FieldTemplateScene.ISSUE.name()); + } + }); + if (CollectionUtils.isNotEmpty(projectTemplates)) { + customFieldTemplateService.updateProjectTemplateGlobalField(customField, + projectTemplates.stream().map(IssueTemplate::getId).collect(Collectors.toList())); } - return null; - } - - private void createTemplateWithUpdateField(String projectId, CustomField customField) { - UpdateIssueTemplateRequest request = new UpdateIssueTemplateRequest(); - IssueTemplate issueTemplate = new IssueTemplate(); - issueTemplate.setName("default"); - issueTemplate.setPlatform(TemplateConstants.IssueTemplatePlatform.metersphere.name()); - issueTemplate.setGlobal(false); - issueTemplate.setSystem(true); - issueTemplate.setProjectId(projectId); - BeanUtils.copyBean(request, issueTemplate); - List systemFieldCreateTemplate = - customFieldTemplateService.getSystemFieldCreateTemplate(customField, TemplateConstants.FieldTemplateScene.ISSUE.name()); - request.setCustomFields(systemFieldCreateTemplate); - add(request); - } - - private void updateRelateWithUpdateField(IssueTemplate template, CustomField customField) { - CustomField globalField = customFieldService.getGlobalFieldByName(customField.getName()); - customFieldTemplateService.updateFieldIdByTemplate(template.getId(), globalField.getId(), customField.getId()); } private void checkExist(IssueTemplate issueTemplate) { diff --git a/backend/src/main/java/io/metersphere/service/TestCaseTemplateService.java b/backend/src/main/java/io/metersphere/service/TestCaseTemplateService.java index 0dfd16cb2b..89629f5a58 100644 --- a/backend/src/main/java/io/metersphere/service/TestCaseTemplateService.java +++ b/backend/src/main/java/io/metersphere/service/TestCaseTemplateService.java @@ -27,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; +import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) @@ -101,45 +102,52 @@ public class TestCaseTemplateService extends TemplateBaseService { * - 如果有,则更新原来关联的 fieldId * @param customField */ - public void handleSystemFieldCreate(CustomField customField) { - TestCaseTemplateWithBLOBs workspaceSystemTemplate = getWorkspaceSystemTemplate(customField.getProjectId()); - if (workspaceSystemTemplate == null) { - createTemplateWithUpdateField(customField.getProjectId(), customField); - } else { - updateRelateWithUpdateField(workspaceSystemTemplate, customField); - } - } - - private void createTemplateWithUpdateField(String projectId, CustomField customField) { - UpdateCaseFieldTemplateRequest request = new UpdateCaseFieldTemplateRequest(); - TestCaseTemplate testCaseTemplate = new TestCaseTemplate(); - testCaseTemplate.setName("default"); - testCaseTemplate.setType(TemplateConstants.TestCaseTemplateScene.functional.name()); - testCaseTemplate.setGlobal(false); - testCaseTemplate.setSystem(true); - testCaseTemplate.setProjectId(projectId); - BeanUtils.copyBean(request, testCaseTemplate); - List systemFieldCreateTemplate = - customFieldTemplateService.getSystemFieldCreateTemplate(customField, TemplateConstants.FieldTemplateScene.TEST_CASE.name()); - request.setCustomFields(systemFieldCreateTemplate); - add(request); - } - - private void updateRelateWithUpdateField(TestCaseTemplateWithBLOBs template, CustomField customField) { - CustomField globalField = customFieldService.getGlobalFieldByName(customField.getName()); - customFieldTemplateService.updateFieldIdByTemplate(template.getId(), globalField.getId(), customField.getId()); - } - - private TestCaseTemplateWithBLOBs getWorkspaceSystemTemplate(String workspaceId) { + public void handleSystemFieldCreate(CustomFieldDao customField) { TestCaseTemplateExample example = new TestCaseTemplateExample(); example.createCriteria() - .andProjectIdEqualTo(workspaceId) - .andSystemEqualTo(true); + .andGlobalEqualTo(true); + example.or(example.createCriteria() + .andProjectIdEqualTo(customField.getProjectId())); List testCaseTemplates = testCaseTemplateMapper.selectByExampleWithBLOBs(example); - if (CollectionUtils.isNotEmpty(testCaseTemplates)) { - return testCaseTemplates.get(0); + + Map> templatesMap = testCaseTemplates.stream() + .collect(Collectors.groupingBy(TestCaseTemplateWithBLOBs::getGlobal)); + + // 获取全局模板 + List globalTemplates = templatesMap.get(true); + // 获取当前工作空间下模板 + List projectTemplates = templatesMap.get(false); + + globalTemplates.forEach(global -> { + List projectTemplate = null; + if (CollectionUtils.isNotEmpty(projectTemplates)) { + projectTemplate = projectTemplates.stream() + .filter(i -> i.getName().equals(global.getName())) + .collect(Collectors.toList()); + } + // 如果没有项目级别的模板就创建 + if (CollectionUtils.isEmpty(projectTemplate)) { + TestCaseTemplateWithBLOBs template = new TestCaseTemplateWithBLOBs(); + BeanUtils.copyBean(template, global); + template.setId(UUID.randomUUID().toString()); + template.setCreateTime(System.currentTimeMillis()); + template.setUpdateTime(System.currentTimeMillis()); + template.setCreateUser(SessionUtils.getUserId()); + template.setGlobal(false); + template.setProjectId(customField.getProjectId()); + testCaseTemplateMapper.insert(template); + + List customFieldTemplate = + customFieldTemplateService.getSystemFieldCreateTemplate(customField, global.getId()); + + customFieldTemplateService.create(customFieldTemplate, template.getId(), + TemplateConstants.FieldTemplateScene.TEST_CASE.name()); + } + }); + if (CollectionUtils.isNotEmpty(projectTemplates)) { + customFieldTemplateService.updateProjectTemplateGlobalField(customField, + projectTemplates.stream().map(TestCaseTemplateWithBLOBs::getId).collect(Collectors.toList())); } - return null; } private void checkExist(TestCaseTemplateWithBLOBs testCaseTemplate) {