diff --git a/backend/src/main/java/io/metersphere/commons/constants/CustomFieldType.java b/backend/src/main/java/io/metersphere/commons/constants/CustomFieldType.java index 6493686d4f..7c0d81dfa7 100644 --- a/backend/src/main/java/io/metersphere/commons/constants/CustomFieldType.java +++ b/backend/src/main/java/io/metersphere/commons/constants/CustomFieldType.java @@ -1,25 +1,42 @@ package io.metersphere.commons.constants; +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; + public enum CustomFieldType { - INPUT("input"), - TEXTAREA("textarea"), - SELECT("select"), - MULTIPLE_SELECT("multipleSelect"), - RADIO("radio"), - CHECKBOX("checkbox"), - MEMBER("member"), - MULTIPLE_MEMBER("multipleMember"), - DATE("date"), - DATETIME("datetime"), - INT("int"), - FLOAT("float"), - MULTIPLE_INPUT("multipleInput"), - RICH_TEXT("richText"); + INPUT("input", false), + TEXTAREA("textarea", false), + SELECT("select", true), + MULTIPLE_SELECT("multipleSelect", true), + RADIO("radio", true), + CHECKBOX("checkbox", true), + MEMBER("member", true), + MULTIPLE_MEMBER("multipleMember", true), + DATE("date", false), + DATETIME("datetime", false), + INT("int", false), + FLOAT("float", false), + MULTIPLE_INPUT("multipleInput", false), + RICH_TEXT("richText", false); - String value; + private String value; + private Boolean hasOption; - CustomFieldType(String value) { + CustomFieldType(String value, Boolean hasOption) { this.value = value; + this.hasOption = hasOption; + } + + public Boolean getHasOption() { + return this.hasOption; + } + + public static Set getHasOptionValueSet() { + return Arrays.stream(CustomFieldType.values()) + .filter(CustomFieldType::getHasOption) + .map(CustomFieldType::getValue) + .collect(Collectors.toSet()); } public String getValue() { diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java index 2db7190813..7ba8075e6c 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -1561,7 +1561,8 @@ public class TestCaseService { customFieldList = testCaseTemplate.getCustomFields(); } - buildExportCustomFieldMap(customSelectValueMap, customNameMap, customFieldList); + Set textFields = new HashSet<>(); + buildExportCustomFieldMap(customSelectValueMap, customNameMap, customFieldList, textFields); for (int rowIndex = 0; rowIndex < testCaseList.size(); rowIndex++) { TestCaseDTO t = testCaseList.get(rowIndex); @@ -1573,7 +1574,7 @@ public class TestCaseService { BeanUtils.copyBean(data, t); buildExportCustomNum(isUseCustomId, t, data); buildExportStep(t, stepDescList, stepResultList, data); - buildExportCustomField(customSelectValueMap, customNameMap, t, data); + buildExportCustomField(customSelectValueMap, customNameMap, t, data, textFields); buildExportOtherField(data, t, otherHeaders); if (CollectionUtils.isNotEmpty(stepDescList)) { @@ -1641,7 +1642,8 @@ public class TestCaseService { } } - private void buildExportCustomField(Map> customSelectValueMap, Map customNameMap, TestCaseDTO t, TestCaseExcelData data) { + private void buildExportCustomField(Map> customSelectValueMap, + Map customNameMap, TestCaseDTO t, TestCaseExcelData data, Set textFields) { try { List fields = customFieldTestCaseService.getByResourceId(t.getId()); Map map = new HashMap<>(); @@ -1649,13 +1651,28 @@ public class TestCaseService { CustomFieldResource field = fields.get(index); //进行key value对换 String id = field.getFieldId(); + if (textFields.contains(id)) { + map.put(customNameMap.get(id), field.getTextValue()); + continue; + } if (StringUtils.isNotBlank(field.getValue())) { - String value = JSONObject.parse(field.getValue()).toString(); - if (customSelectValueMap.containsKey(id) - && customSelectValueMap.get(id).containsKey(value)) { - value = customSelectValueMap.get(id).get(value); + Object value = JSONObject.parse(field.getValue()); + Map optionMap = customSelectValueMap.get(id); + if (value instanceof String) { + if (MapUtils.isNotEmpty(optionMap) && optionMap.containsKey(value)) { + value = optionMap.get(value); + } + map.put(customNameMap.get(id), value.toString()); + } else if (value instanceof JSONArray) { + List results = new ArrayList<>(); + JSONArray values = (JSONArray) value; + values.forEach(item -> { + if (MapUtils.isNotEmpty(optionMap) && optionMap.containsKey(item.toString())) { + results.add(optionMap.get(item.toString())); + } + }); + map.put(customNameMap.get(id), results.toString()); } - map.put(customNameMap.get(id), value); } } data.setCustomData(map); @@ -1698,32 +1715,35 @@ public class TestCaseService { } } - private void buildExportCustomFieldMap(Map> customSelectValueMap, Map customNameMap, List customFieldList) { + private void buildExportCustomFieldMap(Map> customSelectValueMap, Map customNameMap, + List customFieldList, Set textFields) { for (CustomFieldDao dto : customFieldList) { Map map = new HashMap<>(); - if (StringUtils.equals("select", dto.getType())) { + if (CustomFieldType.getHasOptionValueSet().contains(dto.getType())) { try { - JSONArray optionsArr = JSONArray.parseArray(dto.getOptions()); - for (int i = 0; i < optionsArr.size(); i++) { - JSONObject obj = optionsArr.getJSONObject(i); - if (obj.containsKey("text") && obj.containsKey("value")) { - String value = obj.getString("value"); - String text = obj.getString("text"); - if (StringUtils.equals(text, "test_track.case.status_finished")) { - text = Translator.get("test_case_status_finished"); - } else if (StringUtils.equals(text, "test_track.case.status_prepare")) { - text = Translator.get("test_case_status_prepare"); - } else if (StringUtils.equals(text, "test_track.case.status_running")) { - text = Translator.get("test_case_status_running"); - } - if (StringUtils.isNotEmpty(value)) { - map.put(value, text); - } + List options = JSONArray.parseArray(dto.getOptions(), CustomFieldOption.class); + options.forEach(option -> { + String text = option.getText(); + String value = option.getValue(); + if (StringUtils.equals(text, "test_track.case.status_finished")) { + text = Translator.get("test_case_status_finished"); + } else if (StringUtils.equals(text, "test_track.case.status_prepare")) { + text = Translator.get("test_case_status_prepare"); + } else if (StringUtils.equals(text, "test_track.case.status_running")) { + text = Translator.get("test_case_status_running"); } - } + if (StringUtils.isNotEmpty(value)) { + map.put(value, text); + } + }); + } catch (Exception e) { + LogUtil.error(e); } } + if (StringUtils.equalsAny(dto.getType(), CustomFieldType.TEXTAREA.getValue(), CustomFieldType.RICH_TEXT.getValue())) { + textFields.add(dto.getId()); + } customSelectValueMap.put(dto.getId(), map); customNameMap.put(dto.getId(), dto.getName()); }