From cddde4a4f97beeff9c828bd72f1a51a20715a602 Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Fri, 27 May 2022 13:30:46 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/dataease/dto/log/LogExcel.java | 20 ---- .../dataease/service/sys/log/LogService.java | 102 ++++++++++++------ .../resources/i18n/messages_en_US.properties | 1 + .../resources/i18n/messages_zh_CN.properties | 1 + .../resources/i18n/messages_zh_TW.properties | 1 + 5 files changed, 74 insertions(+), 51 deletions(-) delete mode 100644 backend/src/main/java/io/dataease/dto/log/LogExcel.java diff --git a/backend/src/main/java/io/dataease/dto/log/LogExcel.java b/backend/src/main/java/io/dataease/dto/log/LogExcel.java deleted file mode 100644 index a2eba8363b..0000000000 --- a/backend/src/main/java/io/dataease/dto/log/LogExcel.java +++ /dev/null @@ -1,20 +0,0 @@ -package io.dataease.dto.log; - -import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; - -@Data -public class LogExcel { - - @ExcelProperty(value = {"optype"}, index = 0) - private String optype; - - @ExcelProperty(value = {"detail"}, index = 1) - private String detail; - - @ExcelProperty(value = {"user"}, index = 2) - private String user; - - @ExcelProperty(value = {"time"}, index = 3) - private String time; -} diff --git a/backend/src/main/java/io/dataease/service/sys/log/LogService.java b/backend/src/main/java/io/dataease/service/sys/log/LogService.java index d296f0df47..634f6f3305 100644 --- a/backend/src/main/java/io/dataease/service/sys/log/LogService.java +++ b/backend/src/main/java/io/dataease/service/sys/log/LogService.java @@ -2,22 +2,19 @@ package io.dataease.service.sys.log; import cn.hutool.core.date.DateUtil; -import com.alibaba.excel.EasyExcel; -import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.write.metadata.WriteSheet; + import com.google.gson.Gson; import io.dataease.auth.api.dto.CurrentUserDto; import io.dataease.commons.constants.SysLogConstants; import io.dataease.commons.utils.AuthUtils; import io.dataease.commons.utils.BeanUtils; -import io.dataease.commons.utils.CustomCellWriteUtil; import io.dataease.commons.utils.ServletUtils; import io.dataease.controller.sys.base.BaseGridRequest; import io.dataease.controller.sys.base.ConditionEntity; import io.dataease.dto.SysLogDTO; import io.dataease.dto.SysLogGridDTO; import io.dataease.dto.log.FolderItem; -import io.dataease.dto.log.LogExcel; +import io.dataease.exception.DataEaseException; import io.dataease.ext.ExtSysLogMapper; import io.dataease.ext.query.GridExample; import io.dataease.i18n.Translator; @@ -26,10 +23,12 @@ import io.dataease.plugins.common.base.mapper.SysLogMapper; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.poi.hssf.usermodel.*; +import org.apache.poi.ss.usermodel.*; import org.springframework.stereotype.Service; - import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; +import java.io.OutputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -57,8 +56,8 @@ public class LogService { // 授权相关操作 private static Integer[] AUTH_OPERATE = {6, 7}; - // 授权相关资源 数据源 仪表板 数据集 - private static Integer[] AUTH_SOURCE = {1, 2, 3}; + // 授权相关资源 数据源 仪表板 数据集 菜单 + private static Integer[] AUTH_SOURCE = {1, 2, 3, 11}; @@ -212,33 +211,74 @@ public class LogService { sysLogMapper.insert(sysLogWithBLOBs); } - public void exportExcel(BaseGridRequest request) throws Exception{ + + + public void exportExcel(BaseGridRequest request) throws Exception { request = detailRequest(request); HttpServletResponse response = ServletUtils.response(); + OutputStream outputStream = response.getOutputStream(); + try { + GridExample gridExample = request.convertExample(); + List lists = extSysLogMapper.query(gridExample); + List details = lists.stream().map(item -> { + String operateTypeName = SysLogConstants.operateTypeName(item.getOperateType()); + String sourceTypeName = SysLogConstants.sourceTypeName(item.getSourceType()); + String[] row = new String[4]; + row[0] = Translator.get(operateTypeName) + " " + Translator.get(sourceTypeName); + row[1] = logManager.detailInfo(item); + row[2] = item.getNickName(); + row[3] = DateUtil.formatDateTime(new Date(item.getTime())); + return row; + }).collect(Collectors.toList()); + String[] headArr = {"操作类型", "详情", "用户", "时间"}; + details.add(0, headArr); - GridExample gridExample = request.convertExample(); - List lists = extSysLogMapper.query(gridExample); - List excels = lists.stream().map(item -> { - LogExcel logExcel = new LogExcel(); - String operateTypeName = SysLogConstants.operateTypeName(item.getOperateType()); - String sourceTypeName = SysLogConstants.sourceTypeName(item.getSourceType()); - logExcel.setOptype(Translator.get(operateTypeName) + " " + Translator.get(sourceTypeName)); - logExcel.setDetail(logManager.detailInfo(item)); - logExcel.setUser(item.getNickName()); - logExcel.setTime(DateUtil.formatDateTime(new Date(item.getTime()))); - return logExcel; - }).collect(Collectors.toList()); - // 导出时候会出现中⽂⽆法识别问题,需要转码 - String name = "log.xlsx"; - String fileName = new String(name.getBytes("gb2312"),"ISO8859-1"); - response.setContentType("application/vnd.ms-excel;chartset=utf-8"); - response.setHeader("Content-Disposition","attachment;filename=" + fileName); - //调⽤⼯具类 - ExcelWriter writer = EasyExcel.write(response.getOutputStream()).build(); - WriteSheet sheet = EasyExcel.writerSheet(0,"sheet").head(LogExcel.class).registerWriteHandler(new CustomCellWriteUtil()).build(); - writer.write(excels,sheet); - writer.finish(); // 使⽤完毕之后要关闭 + HSSFWorkbook wb = new HSSFWorkbook(); + //明细sheet + HSSFSheet detailsSheet = wb.createSheet("数据"); + + //给单元格设置样式 + CellStyle cellStyle = wb.createCellStyle(); + Font font = wb.createFont(); + //设置字体大小 + font.setFontHeightInPoints((short) 12); + //设置字体加粗 + font.setBold(true); + //给字体设置样式 + cellStyle.setFont(font); + //设置单元格背景颜色 + cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); + //设置单元格填充样式(使用纯色背景颜色填充) + cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + + if (CollectionUtils.isNotEmpty(details)) { + for (int i = 0; i < details.size(); i++) { + HSSFRow row = detailsSheet.createRow(i); + String[] rowData = details.get(i); + if (rowData != null) { + for (int j = 0; j < rowData.length; j++) { + HSSFCell cell = row.createCell(j); + cell.setCellValue(rowData[j]); + if (i == 0) {// 头部 + cell.setCellStyle(cellStyle); + //设置列的宽度 + detailsSheet.setColumnWidth(j, 255 * 20); + } + } + } + } + } + + response.setContentType("application/vnd.ms-excel"); + //文件名称 + response.setHeader("Content-disposition", "attachment;filename=log.xlsx"); + wb.write(outputStream); + outputStream.flush(); + outputStream.close(); + } catch (Exception e) { + DataEaseException.throwException(e); + } } diff --git a/backend/src/main/resources/i18n/messages_en_US.properties b/backend/src/main/resources/i18n/messages_en_US.properties index 6a8e33aa06..91c15e3406 100644 --- a/backend/src/main/resources/i18n/messages_en_US.properties +++ b/backend/src/main/resources/i18n/messages_en_US.properties @@ -152,5 +152,6 @@ SOURCE_TYPE_DEPT=ORG SOURCE_TYPE_ROLE=ROLE SOURCE_TYPE_DRIVER=DRIVER SOURCE_TYPE_DRIVER_FILE=DRIVER FILE +SOURCE_TYPE_MENU=MENU I18N_DRIVER_NOT_DELETE=Drivers in use cannot be deleted \ No newline at end of file diff --git a/backend/src/main/resources/i18n/messages_zh_CN.properties b/backend/src/main/resources/i18n/messages_zh_CN.properties index 703d559bdf..b0f7600640 100644 --- a/backend/src/main/resources/i18n/messages_zh_CN.properties +++ b/backend/src/main/resources/i18n/messages_zh_CN.properties @@ -150,6 +150,7 @@ SOURCE_TYPE_DEPT=组织 SOURCE_TYPE_ROLE=角色 SOURCE_TYPE_DRIVER=驱动 SOURCE_TYPE_DRIVER_FILE=驱动文件 +SOURCE_TYPE_MENU=菜单 I18N_OPERATE_TYPE=操作类型 I18N_DETAIL=操作详情 diff --git a/backend/src/main/resources/i18n/messages_zh_TW.properties b/backend/src/main/resources/i18n/messages_zh_TW.properties index 3190caa51b..f69b3e047b 100644 --- a/backend/src/main/resources/i18n/messages_zh_TW.properties +++ b/backend/src/main/resources/i18n/messages_zh_TW.properties @@ -151,5 +151,6 @@ SOURCE_TYPE_DEPT=組織 SOURCE_TYPE_ROLE=角色 SOURCE_TYPE_DRIVER=驅動 SOURCE_TYPE_DRIVER_FILE=驅動文件 +SOURCE_TYPE_MENU=菜單 I18N_DRIVER_NOT_DELETE=使用中的驅動不允許删除 \ No newline at end of file