diff --git a/test-track/backend/src/main/java/io/metersphere/service/IssuesService.java b/test-track/backend/src/main/java/io/metersphere/service/IssuesService.java index e0ece349bc..3361aaea0b 100644 --- a/test-track/backend/src/main/java/io/metersphere/service/IssuesService.java +++ b/test-track/backend/src/main/java/io/metersphere/service/IssuesService.java @@ -87,6 +87,7 @@ import java.io.IOException; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -159,6 +160,8 @@ public class IssuesService { private BasePluginService basePluginService; @Resource private MdFileService mdFileService; + @Resource + private CustomFieldMapper customFieldMapper; private static final String SYNC_THIRD_PARTY_ISSUES_KEY = "ISSUE:SYNC"; private static final String SYNC_THIRD_PARTY_ISSUES_ERROR_KEY = "ISSUE:SYNC:ERROR"; @@ -757,19 +760,64 @@ public class IssuesService { } Map> fieldMap = customFieldIssuesService.getMapByResourceIds(data.stream().map(IssuesDao::getId).collect(Collectors.toList())); + + Map globalProjectIdMap = getGlobalProjectIdMap(data.get(0).getProjectId()); + + fieldMap.values().forEach(fields -> + fields.forEach(field -> { + // 如果是全局字段, 并且项目中有对应的字段, 则替换为项目字段 + if (globalProjectIdMap.containsKey(field.getId())) { + field.setId(globalProjectIdMap.get(field.getId())); + } + }) + ); + data.forEach(i -> i.setFields(fieldMap.get(i.getId()))); } + + private Map getGlobalProjectIdMap(String projectId) { + // 查询全局的内置字段 + CustomFieldExample example = new CustomFieldExample(); + example.createCriteria().andGlobalEqualTo(true).andSystemEqualTo(true).andSceneEqualTo(CustomFieldScene.ISSUE.name()); + List globalSystemFiles = customFieldMapper.selectByExample(example); + + // 查询对应的项目下字段 + example.clear(); + example.createCriteria().andGlobalEqualTo(false) + .andSystemEqualTo(true).andSceneEqualTo(CustomFieldScene.ISSUE.name()) + .andProjectIdEqualTo(projectId); + List projectSystemFiles = customFieldMapper.selectByExample(example); + Map projectSystemFilesMap = projectSystemFiles.stream().collect(Collectors.toMap(CustomField::getName, Function.identity())); + + // 全局字段与项目字段的ID映射 + Map globalProjectIdMap = new HashMap<>(globalSystemFiles.size()); + for (CustomField globalSystemFile : globalSystemFiles) { + String fileName = globalSystemFile.getName(); + if (projectSystemFilesMap.get(fileName) != null) { + globalProjectIdMap.put(globalSystemFile.getId(), projectSystemFilesMap.get(fileName).getId()); + } + } + return globalProjectIdMap; + } + private void buildCustomField(IssuesDao data) { CustomFieldIssuesExample example = new CustomFieldIssuesExample(); example.createCriteria().andResourceIdEqualTo(data.getId()); List customFieldTestCases = customFieldIssuesMapper.selectByExample(example); List fields = new ArrayList<>(); + + Map globalProjectIdMap = getGlobalProjectIdMap(data.getProjectId()); + customFieldTestCases.forEach(i -> { CustomFieldDao customFieldDao = new CustomFieldDao(); customFieldDao.setId(i.getFieldId()); customFieldDao.setValue(i.getValue()); customFieldDao.setTextValue(i.getTextValue()); + if (globalProjectIdMap.containsKey(i.getFieldId())) { + // 如果是全局字段, 并且项目中有对应的字段, 则替换为项目字段 + customFieldDao.setId(globalProjectIdMap.get(i.getFieldId())); + } fields.add(customFieldDao); }); data.setFields(fields); @@ -842,6 +890,16 @@ public class IssuesService { } } + Map globalProjectIdMap = getGlobalProjectIdMap(data.get(0).getProjectId()); + fieldMap.values().forEach(fields -> + fields.forEach(field -> { + // 如果是全局字段, 并且项目中有对应的字段, 则替换为项目字段 + if (globalProjectIdMap.containsKey(field.getId())) { + field.setId(globalProjectIdMap.get(field.getId())); + } + }) + ); + data.forEach(i -> i.setFields(fieldMap.get(i.getId()))); } catch (Exception e) { MSException.throwException(e.getMessage());