From 0cf34d4f25e8ccabf23eca28fb3f63b4fcb091e1 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Wed, 15 Feb 2023 15:15:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=A1=B9=E7=9B=AE=E6=8A=A5=E5=91=8A):?= =?UTF-8?q?=20=E9=87=8D=E6=9E=84=E6=8A=A5=E5=91=8A=E5=8F=91=E9=80=81?= =?UTF-8?q?=E9=82=AE=E4=BB=B6=E7=9A=84=E4=BB=A3=E7=A0=81=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构报告发送邮件的代码处理逻辑 --- .../service/EnterpriseTestReportService.java | 174 +++++++----------- .../service/ReportStatisticsService.java | 6 +- .../utils/EmailPageInfoUtil.java | 77 ++++++++ 3 files changed, 145 insertions(+), 112 deletions(-) create mode 100644 report-stat/backend/src/main/java/io/metersphere/reportstatistics/utils/EmailPageInfoUtil.java diff --git a/report-stat/backend/src/main/java/io/metersphere/reportstatistics/service/EnterpriseTestReportService.java b/report-stat/backend/src/main/java/io/metersphere/reportstatistics/service/EnterpriseTestReportService.java index 8817735185..bade9257b5 100644 --- a/report-stat/backend/src/main/java/io/metersphere/reportstatistics/service/EnterpriseTestReportService.java +++ b/report-stat/backend/src/main/java/io/metersphere/reportstatistics/service/EnterpriseTestReportService.java @@ -12,7 +12,6 @@ import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.*; import io.metersphere.dto.UserDTO; import io.metersphere.dto.UserGroupInfoDTO; -import io.metersphere.i18n.Translator; import io.metersphere.log.utils.ReflexObjectUtil; import io.metersphere.log.vo.DetailColumn; import io.metersphere.log.vo.OperatingLogDetails; @@ -25,15 +24,14 @@ import io.metersphere.reportstatistics.dto.request.EnterpriseTestReportRequest; import io.metersphere.reportstatistics.dto.request.emun.EnterpriseReportStatus; import io.metersphere.reportstatistics.dto.response.EnterpriseTestReportDTO; import io.metersphere.reportstatistics.dto.response.UserGroupResponse; -import io.metersphere.reportstatistics.dto.table.TestCaseCountTableDataDTO; -import io.metersphere.reportstatistics.dto.table.TestCaseCountTableItemDataDTO; -import io.metersphere.reportstatistics.dto.table.TestCaseCountTableRowDTO; import io.metersphere.reportstatistics.job.SendReportJob; +import io.metersphere.reportstatistics.utils.EmailPageInfoUtil; import io.metersphere.reportstatistics.utils.JSONUtil; import io.metersphere.reportstatistics.utils.ScheduleUtil; import io.metersphere.request.ScheduleRequest; import io.metersphere.service.BaseScheduleService; import io.metersphere.service.BaseUserService; +import jakarta.annotation.Resource; import org.apache.commons.codec.binary.Base64; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; @@ -43,7 +41,6 @@ import org.springframework.core.io.FileSystemResource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import jakarta.annotation.Resource; import java.io.File; import java.io.InputStream; import java.net.URL; @@ -345,140 +342,99 @@ public class EnterpriseTestReportService { mailNoticeSender.sendExternalMail(context, noticeModel); } - private String genReportContent(EnterpriseTestReportWithBLOBs report, boolean isSchedule) throws Exception { - StringBuffer returnReportContentBuffer = new StringBuffer(); - List stepList = JSONUtil.parseArray(report.getReportContent(), EnterpriseReportContentStep.class); + private String formatPreviewContent(String previewContent) { + previewContent = StringUtils.replace(previewContent, "class=\"hljs-center\"", "style=\"text-align: center\""); + previewContent = StringUtils.replace(previewContent, "class=\"hljs-right\"", "style=\"text-align: right\""); + previewContent = StringUtils.replace(previewContent, "class=\"hljs-left\"", "style=\"text-align: left\""); + return previewContent; + } + private boolean isReportStep(String stepType) { + return StringUtils.equalsIgnoreCase(stepType, EmailPageInfoUtil.STEP_TYPE_REPORT); + } + + private boolean isRichTextStep(String stepType) { + return StringUtils.equalsIgnoreCase(stepType, EmailPageInfoUtil.STEP_TYPE_TXT); + } + + private String genReportContent(EnterpriseTestReportWithBLOBs report, boolean isSchedule) { + StringBuilder reportContent = new StringBuilder(); UserDTO user = baseUserService.getUserDTO(report.getCreateUser()); + List stepList = JSONUtil.parseArray(report.getReportContent(), EnterpriseReportContentStep.class); Map syncReportMap = this.getSyncReportMap(stepList, user, isSchedule); - for (EnterpriseReportContentStep step : stepList) { - String title = "
\n" + - "" + step.getName() + StringUtils.LF + - "\n" + - "
"; - returnReportContentBuffer.append(title); - if (StringUtils.equalsIgnoreCase(step.getType(), "txt")) { - String previweContent = step.getPreviewContext(); - previweContent = StringUtils.replace(previweContent, "class=\"hljs-center\"", "style=\"text-align: center\""); - previweContent = StringUtils.replace(previweContent, "class=\"hljs-right\"", "style=\"text-align: right\""); - previweContent = StringUtils.replace(previweContent, "class=\"hljs-left\"", "style=\"text-align: left\""); - if (StringUtils.contains(previweContent, "\"")) 1) { - for (int i = 1; i < previewContentArr.length; i++) { - String itemStr = previewContentArr[i]; - int containsIndex = StringUtils.indexOf(itemStr, "\" alt=\""); - if (containsIndex > 0) { - String fileNameStr = StringUtils.substring(itemStr, 0, containsIndex); - String fileName = fileNameStr; - if (StringUtils.startsWith(fileName, "?fileName=")) { - fileName = fileName.substring(10, fileName.length()); - } - String encodedFileName = URLDecoder.decode(fileName, "UTF-8"); - String dataType = "data:image/jpeg;"; - if (StringUtils.endsWithIgnoreCase(encodedFileName, ".png")) { - dataType = "data:image/png;"; - } - try { - String fileContent = "\"""; - returnReportContentBuffer.append(reportPic); + if (StringUtils.isNotEmpty(step.getRecordImageContent())) { + reportContent.append(EmailPageInfoUtil.generatePicInfo(step.getReportRecordId(), step.getRecordImageContent())); + } + if (step.getReportRecordData() != null && step.getReportRecordData().containsKey("showTable")) { - StringBuffer tableBuffer = new StringBuffer(); - tableBuffer.append(""); - try { - String showTableJsonStr = JSON.toJSONString(step.getReportRecordData().get("showTable")); - TestCaseCountTableDataDTO showTable = JSON.parseObject(showTableJsonStr, TestCaseCountTableDataDTO.class); - tableBuffer.append(""); - for (TestCaseCountTableItemDataDTO itemData : showTable.getHeads()) { - String tableHeadValue = itemData.getValue(); - switch (tableHeadValue) { - case "testCase": - tableHeadValue = Translator.get("test_case"); - break; - case "apiCase": - tableHeadValue = Translator.get("api_case"); - break; - case "scenarioCase": - tableHeadValue = Translator.get("scenario_case"); - break; - case "performanceCase": - tableHeadValue = Translator.get("performance_case"); - break; - case "creator": - tableHeadValue = Translator.get("create_user"); - break; - case "casetype": - tableHeadValue = Translator.get("test_case_type"); - break; - case "casestatus": - tableHeadValue = Translator.get("test_case_status"); - break; - case "caselevel": - tableHeadValue = Translator.get("test_case_priority"); - break; - case "Count": - tableHeadValue = Translator.get("count"); - break; - } - - tableBuffer.append(""); - } - tableBuffer.append(""); - for (TestCaseCountTableRowDTO row : showTable.getData()) { - tableBuffer.append(""); - for (TestCaseCountTableItemDataDTO itemData : row.getTableDatas()) { - tableBuffer.append(""); - } - tableBuffer.append(""); - - } - } catch (Exception e) { - LogUtil.error("解析表格数据出错!", e); - } - tableBuffer.append("
" + tableHeadValue + "
" + itemData.getValue() + "
"); - returnReportContentBuffer.append(tableBuffer); + reportContent.append(EmailPageInfoUtil.generateTableInfo(step.getReportRecordData())); } } } - return returnReportContentBuffer.toString(); + return reportContent.toString(); + } + + private String formatStepImageInfo(String previewContent) { + String[] previewContentArr = StringUtils.splitByWholeSeparator(StringUtils.SPACE + previewContent, EmailPageInfoUtil.IMG_DATA_PREFIX); + if (previewContentArr.length > 1) { + for (int i = 1; i < previewContentArr.length; i++) { + String itemStr = previewContentArr[i]; + int containsIndex = StringUtils.indexOf(itemStr, EmailPageInfoUtil.IMG_DATA_CONTAINS); + if (containsIndex > 0) { + String fileNameStr = StringUtils.substring(itemStr, 0, containsIndex); + String decodeFileName = StringUtils.startsWith(fileNameStr, "?fileName=") ? fileNameStr.substring(10) : fileNameStr; + String encodedFileName = URLDecoder.decode(decodeFileName, StandardCharsets.UTF_8); + String dataType = StringUtils.endsWithIgnoreCase(encodedFileName, ".png") ? + EmailPageInfoUtil.IMG_DATA_TYPE_PNG : EmailPageInfoUtil.IMG_DATA_TYPE_JPG; + try { + String fileContent = " getSyncReportMap(List stepList, User user, boolean isSchedule) { Map returnMap = new HashMap<>(); if (isSchedule) { + //定时任务触发的数据,需要检查图片是否需要重新生成 String language = null; if (user != null) { language = user.getLanguage(); } List bloBsList = new ArrayList<>(); for (EnterpriseReportContentStep step : stepList) { - //定时任务触发的数据,需要检查图片是否需要重新生成 ReportStatisticsWithBLOBs reportStatisticsWithBLOBs = reportStatisticsService.selectById(step.getReportRecordId(), false); if (reportStatisticsWithBLOBs != null) { - boolean needSyncImage = reportStatisticsService.isReportNeedUpdate(reportStatisticsWithBLOBs); + boolean needSyncImage = reportStatisticsService.reportHasUpdated(reportStatisticsWithBLOBs); if (needSyncImage) { bloBsList.add(reportStatisticsWithBLOBs); } diff --git a/report-stat/backend/src/main/java/io/metersphere/reportstatistics/service/ReportStatisticsService.java b/report-stat/backend/src/main/java/io/metersphere/reportstatistics/service/ReportStatisticsService.java index 6e544146f0..2520827e00 100644 --- a/report-stat/backend/src/main/java/io/metersphere/reportstatistics/service/ReportStatisticsService.java +++ b/report-stat/backend/src/main/java/io/metersphere/reportstatistics/service/ReportStatisticsService.java @@ -13,12 +13,12 @@ import io.metersphere.reportstatistics.dto.table.TestCaseCountTableItemDataDTO; import io.metersphere.reportstatistics.dto.table.TestCaseCountTableRowDTO; import io.metersphere.reportstatistics.utils.ChromeUtil; import io.metersphere.service.SystemParameterService; +import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import jakarta.annotation.Resource; import java.util.*; /** @@ -83,7 +83,7 @@ public class ReportStatisticsService { Map selectOption = JSON.parseObject(blob.getSelectOption(), Map.class); Map dataOption = JSON.parseObject(blob.getDataOption(), Map.class); - boolean isReportNeedUpdate = this.isReportNeedUpdate(blob); + boolean isReportNeedUpdate = this.reportHasUpdated(blob); if (isReportNeedUpdate) { TestCaseCountRequest countRequest = JSON.parseObject(blob.getSelectOption(), TestCaseCountRequest.class); Map returnDataOption = this.reloadData(countRequest, dataOption.get("chartType").toString()); @@ -298,7 +298,7 @@ public class ReportStatisticsService { return returnMap; } - public boolean isReportNeedUpdate(ReportStatisticsWithBLOBs model) { + public boolean reportHasUpdated(ReportStatisticsWithBLOBs model) { Map selectOption = JSON.parseObject(model.getSelectOption(), Map.class); return selectOption.containsKey("timeType") && StringUtils.equalsIgnoreCase("dynamicTime", String.valueOf(selectOption.get("timeType"))); } diff --git a/report-stat/backend/src/main/java/io/metersphere/reportstatistics/utils/EmailPageInfoUtil.java b/report-stat/backend/src/main/java/io/metersphere/reportstatistics/utils/EmailPageInfoUtil.java new file mode 100644 index 0000000000..f44ef1e600 --- /dev/null +++ b/report-stat/backend/src/main/java/io/metersphere/reportstatistics/utils/EmailPageInfoUtil.java @@ -0,0 +1,77 @@ +package io.metersphere.reportstatistics.utils; + +import io.metersphere.commons.utils.JSON; +import io.metersphere.commons.utils.LogUtil; +import io.metersphere.i18n.Translator; +import io.metersphere.reportstatistics.dto.table.TestCaseCountTableDataDTO; +import io.metersphere.reportstatistics.dto.table.TestCaseCountTableItemDataDTO; +import io.metersphere.reportstatistics.dto.table.TestCaseCountTableRowDTO; +import org.apache.commons.lang3.StringUtils; + +import java.util.Map; + +public class EmailPageInfoUtil { + public static final String STEP_TYPE_TXT = "txt"; + public static final String STEP_TYPE_REPORT = "report"; + + //数据前缀--图片 + public static final String IMG_DATA_PREFIX = "\"";", StringUtils.LF, + "", + stepName, StringUtils.LF, + "", StringUtils.LF, + "" + ); + } + + public static String generateTableInfo(Map reportRecordData) { + StringBuilder tableBuffer = new StringBuilder(); + tableBuffer.append(""); + try { + String showTableJsonStr = JSON.toJSONString(reportRecordData.get("showTable")); + TestCaseCountTableDataDTO showTable = JSON.parseObject(showTableJsonStr, TestCaseCountTableDataDTO.class); + tableBuffer.append(""); + for (TestCaseCountTableItemDataDTO itemData : showTable.getHeads()) { + String tableHeadValue = switch (itemData.getValue()) { + case "testCase" -> Translator.get("test_case"); + case "apiCase" -> Translator.get("api_case"); + case "scenarioCase" -> Translator.get("scenario_case"); + case "performanceCase" -> Translator.get("performance_case"); + case "creator" -> Translator.get("create_user"); + case "casetype" -> Translator.get("test_case_type"); + case "casestatus" -> Translator.get("test_case_status"); + case "caselevel" -> Translator.get("test_case_priority"); + case "Count" -> Translator.get("count"); + default -> itemData.getValue(); + }; + + tableBuffer.append(""); + } + tableBuffer.append(""); + for (TestCaseCountTableRowDTO row : showTable.getData()) { + tableBuffer.append(""); + for (TestCaseCountTableItemDataDTO itemData : row.getTableDatas()) { + tableBuffer.append(""); + } + tableBuffer.append(""); + + } + } catch (Exception e) { + LogUtil.error("解析表格数据出错!", e); + } + tableBuffer.append("
").append(tableHeadValue).append("
").append(itemData.getValue()).append("
"); + return tableBuffer.toString(); + } + + public static String generatePicInfo(String reportRecordId, String imageContent) { + return ""; + } +}