From ab6071d12e5805315c0223c96ecdd45ec9774f16 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Wed, 17 Aug 2022 11:27:30 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):=20?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E5=AF=BC=E5=87=BA=E7=BC=BA=E5=B0=91=E8=B4=A3?= =?UTF-8?q?=E4=BB=BB=E4=BA=BA=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/constants/TestCaseImportFiled.java | 52 ++++++++++++++++--- .../excel/domain/TestCaseExcelData.java | 2 +- .../track/service/TestCaseService.java | 50 +++++------------- 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/backend/src/main/java/io/metersphere/excel/constants/TestCaseImportFiled.java b/backend/src/main/java/io/metersphere/excel/constants/TestCaseImportFiled.java index 5c7d27bc3e..8578692a6e 100644 --- a/backend/src/main/java/io/metersphere/excel/constants/TestCaseImportFiled.java +++ b/backend/src/main/java/io/metersphere/excel/constants/TestCaseImportFiled.java @@ -1,28 +1,64 @@ package io.metersphere.excel.constants; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.nacos.common.utils.CollectionUtils; +import io.metersphere.commons.utils.LogUtil; +import io.metersphere.excel.domain.TestCaseExcelData; +import org.apache.commons.lang3.StringUtils; + import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.function.Function; public enum TestCaseImportFiled { - ID("ID", "ID", "ID"), NAME("用例名称", "用例名稱", "Name"), MODULE("所属模块", "所屬模塊", "Module"), - TAG("标签", "標簽", "Tag"), PREREQUISITE("前置条件", "前置條件", "Prerequisite"), REMARK("备注", "備註", "Remark"), - STEP_DESC("步骤描述", "步驟描述", "Step description"), STEP_RESULT("预期结果", "預期結果", "Step result"), STEP_MODEL("编辑模式", "編輯模式", "Edit Model"), - STATUS("用例状态", "用例狀態", "Case status"), MAINTAINER("责任人", "責任人", "Maintainer"), PRIORITY("用例等级", "用例等級", "Priority"); + ID("ID", "ID", "ID", TestCaseExcelData::getCustomNum), + NAME("用例名称", "用例名稱", "Name", TestCaseExcelData::getName), + MODULE("所属模块", "所屬模塊", "Module", TestCaseExcelData::getNodePath), + TAG("标签", "標簽", "Tag", TestCaseImportFiled::parseTags), + PREREQUISITE("前置条件", "前置條件", "Prerequisite", TestCaseExcelData::getPrerequisite), + REMARK("备注", "備註", "Remark", TestCaseExcelData::getRemark), + STEP_DESC("步骤描述", "步驟描述", "Step description", TestCaseExcelData::getStepDesc), + STEP_RESULT("预期结果", "預期結果", "Step result", TestCaseExcelData::getStepResult), + STEP_MODEL("编辑模式", "編輯模式", "Edit Model", TestCaseExcelData::getStepModel), + STATUS("用例状态", "用例狀態", "Case status", TestCaseExcelData::getStatus), + MAINTAINER("责任人", "責任人", "Maintainer", TestCaseExcelData::getMaintainer), + PRIORITY("用例等级", "用例等級", "Priority", TestCaseExcelData::getPriority); private Map filedLangMap; + private Function parseFunc; public Map getFiledLangMap() { return this.filedLangMap; } - TestCaseImportFiled(String zn, String chineseTw, String us) { + TestCaseImportFiled(String zn, String chineseTw, String us, Function parseFunc) { this.filedLangMap = new HashMap<>() {{ - put(Locale.SIMPLIFIED_CHINESE, zn); - put(Locale.TRADITIONAL_CHINESE, chineseTw); - put(Locale.US, us); + put(Locale.SIMPLIFIED_CHINESE, zn); + put(Locale.TRADITIONAL_CHINESE, chineseTw); + put(Locale.US, us); }}; + this.parseFunc = parseFunc; + } + + public String parseExcelDataValue(TestCaseExcelData excelData) { + return parseFunc.apply(excelData); + } + + private static String parseTags(TestCaseExcelData excelData) { + String tags = ""; + try { + if (excelData.getTags() != null) { + JSONArray arr = JSONArray.parseArray(excelData.getTags()); + if (CollectionUtils.isNotEmpty(arr)) { + tags = StringUtils.joinWith(",", arr.toArray()); + } + } + } catch (Exception e) { + LogUtil.error(e); + } + return tags; } } diff --git a/backend/src/main/java/io/metersphere/excel/domain/TestCaseExcelData.java b/backend/src/main/java/io/metersphere/excel/domain/TestCaseExcelData.java index f15c985532..f65ac76d98 100644 --- a/backend/src/main/java/io/metersphere/excel/domain/TestCaseExcelData.java +++ b/backend/src/main/java/io/metersphere/excel/domain/TestCaseExcelData.java @@ -81,7 +81,7 @@ public class TestCaseExcelData { if (StringUtils.equalsAny(dto.getName(), TestCaseImportFiled.PRIORITY.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE), TestCaseImportFiled.STATUS.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE), - TestCaseImportFiled.MAINTAINER.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE).concat("(ID)"))) { + TestCaseImportFiled.MAINTAINER.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE))) { continue; } heads.add(new ArrayList<>() {{ 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 e513bfa1f2..81605c7c25 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -24,13 +24,13 @@ import io.metersphere.base.mapper.ext.ExtProjectVersionMapper; import io.metersphere.base.mapper.ext.ExtTestCaseMapper; import io.metersphere.commons.constants.*; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.user.SessionUser; import io.metersphere.commons.utils.*; import io.metersphere.controller.request.OrderRequest; import io.metersphere.controller.request.ProjectVersionRequest; import io.metersphere.controller.request.ResetOrderRequest; import io.metersphere.controller.request.member.QueryMemberRequest; import io.metersphere.dto.*; +import io.metersphere.excel.constants.TestCaseImportFiled; import io.metersphere.excel.domain.*; import io.metersphere.excel.handler.FunctionCaseMergeWriteHandler; import io.metersphere.excel.handler.FunctionCaseTemplateWriteHandler; @@ -1422,46 +1422,24 @@ public class TestCaseService { for (TestCaseExcelData model : data) { List fields = new ArrayList<>(); Map customDataMaps = Optional.ofNullable(model.getCustomDatas()).orElse(new HashMap<>()); + TestCaseImportFiled[] importFields = TestCaseImportFiled.values(); for (String head : headList) { - if (StringUtils.equalsAnyIgnoreCase(head, "ID")) { - fields.add(model.getCustomNum()); - } else if (StringUtils.equalsAnyIgnoreCase(head, "Name", "用例名稱", "用例名称")) { - fields.add(model.getName()); - } else if (StringUtils.equalsAnyIgnoreCase(head, "Module", "所屬模塊", "所属模块")) { - fields.add(model.getNodePath()); - } else if (StringUtils.equalsAnyIgnoreCase(head, "Tag", "標簽", "标签")) { - String tags = ""; - try { - if (model.getTags() != null) { - JSONArray arr = JSONArray.parseArray(model.getTags()); - for (int i = 0; i < arr.size(); i++) { - tags += arr.getString(i) + ","; - } - } - } catch (Exception e) { + boolean isSystemField = false; + for (TestCaseImportFiled importFiled : importFields) { + if (importFiled.getFiledLangMap().values().contains(head)) { + fields.add(importFiled.parseExcelDataValue(model)); + isSystemField = true; + } + } + if (!isSystemField) { + String value = customDataMaps.get(head); + if (value == null) { + value = ""; } - fields.add(tags); - } else if (StringUtils.equalsAnyIgnoreCase(head, "Prerequisite", "前置條件", "前置条件")) { - fields.add(model.getPrerequisite()); - } else if (StringUtils.equalsAnyIgnoreCase(head, "Remark", "備註", "备注")) { - fields.add(model.getRemark()); - } else if (StringUtils.equalsAnyIgnoreCase(head, "Step description", "步驟描述", "步骤描述")) { - fields.add(model.getStepDesc()); - } else if (StringUtils.equalsAnyIgnoreCase(head, "Step result", "預期結果", "预期结果")) { - fields.add(model.getStepResult()); - } else if (StringUtils.equalsAnyIgnoreCase(head, "Edit Model", "編輯模式", "编辑模式")) { - fields.add(model.getStepModel()); - } else if (StringUtils.equalsAnyIgnoreCase(head, "Priority", "用例等級", "用例等级")) { - fields.add(model.getPriority()); - } else if (StringUtils.equalsAnyIgnoreCase(head, "Case status", "用例状态", "用例狀態")) { - fields.add(model.getStatus()); - } else if (StringUtils.equalsAnyIgnoreCase(head, "Maintainer(ID)", "责任人(ID)", "維護人(ID)")) { - fields.add(model.getMaintainer()); - } else { - String value = Optional.ofNullable(customDataMaps.get(head)).orElse(""); fields.add(value); } + } result.add(fields); }