Merge pull request #2334 from dataease/pr@dev@perf_log

perf: 优化操作日志
This commit is contained in:
fit2cloud-chenyw 2022-05-27 13:31:51 +08:00 committed by GitHub
commit 3c594429a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 51 deletions

View File

@ -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;
}

View File

@ -2,22 +2,19 @@ package io.dataease.service.sys.log;
import cn.hutool.core.date.DateUtil; 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 com.google.gson.Gson;
import io.dataease.auth.api.dto.CurrentUserDto; import io.dataease.auth.api.dto.CurrentUserDto;
import io.dataease.commons.constants.SysLogConstants; import io.dataease.commons.constants.SysLogConstants;
import io.dataease.commons.utils.AuthUtils; import io.dataease.commons.utils.AuthUtils;
import io.dataease.commons.utils.BeanUtils; import io.dataease.commons.utils.BeanUtils;
import io.dataease.commons.utils.CustomCellWriteUtil;
import io.dataease.commons.utils.ServletUtils; import io.dataease.commons.utils.ServletUtils;
import io.dataease.controller.sys.base.BaseGridRequest; import io.dataease.controller.sys.base.BaseGridRequest;
import io.dataease.controller.sys.base.ConditionEntity; import io.dataease.controller.sys.base.ConditionEntity;
import io.dataease.dto.SysLogDTO; import io.dataease.dto.SysLogDTO;
import io.dataease.dto.SysLogGridDTO; import io.dataease.dto.SysLogGridDTO;
import io.dataease.dto.log.FolderItem; 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.ExtSysLogMapper;
import io.dataease.ext.query.GridExample; import io.dataease.ext.query.GridExample;
import io.dataease.i18n.Translator; 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.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -57,8 +56,8 @@ public class LogService {
// 授权相关操作 // 授权相关操作
private static Integer[] AUTH_OPERATE = {6, 7}; 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); sysLogMapper.insert(sysLogWithBLOBs);
} }
public void exportExcel(BaseGridRequest request) throws Exception{
public void exportExcel(BaseGridRequest request) throws Exception {
request = detailRequest(request); request = detailRequest(request);
HttpServletResponse response = ServletUtils.response(); HttpServletResponse response = ServletUtils.response();
OutputStream outputStream = response.getOutputStream();
try {
GridExample gridExample = request.convertExample();
List<SysLogWithBLOBs> lists = extSysLogMapper.query(gridExample);
List<String[]> 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<SysLogWithBLOBs> lists = extSysLogMapper.query(gridExample);
List<LogExcel> 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);
}
} }

View File

@ -152,5 +152,6 @@ SOURCE_TYPE_DEPT=ORG
SOURCE_TYPE_ROLE=ROLE SOURCE_TYPE_ROLE=ROLE
SOURCE_TYPE_DRIVER=DRIVER SOURCE_TYPE_DRIVER=DRIVER
SOURCE_TYPE_DRIVER_FILE=DRIVER FILE SOURCE_TYPE_DRIVER_FILE=DRIVER FILE
SOURCE_TYPE_MENU=MENU
I18N_DRIVER_NOT_DELETE=Drivers in use cannot be deleted I18N_DRIVER_NOT_DELETE=Drivers in use cannot be deleted

View File

@ -150,6 +150,7 @@ SOURCE_TYPE_DEPT=组织
SOURCE_TYPE_ROLE=角色 SOURCE_TYPE_ROLE=角色
SOURCE_TYPE_DRIVER=驱动 SOURCE_TYPE_DRIVER=驱动
SOURCE_TYPE_DRIVER_FILE=驱动文件 SOURCE_TYPE_DRIVER_FILE=驱动文件
SOURCE_TYPE_MENU=菜单
I18N_OPERATE_TYPE=操作类型 I18N_OPERATE_TYPE=操作类型
I18N_DETAIL=操作详情 I18N_DETAIL=操作详情

View File

@ -151,5 +151,6 @@ SOURCE_TYPE_DEPT=組織
SOURCE_TYPE_ROLE=角色 SOURCE_TYPE_ROLE=角色
SOURCE_TYPE_DRIVER=驅動 SOURCE_TYPE_DRIVER=驅動
SOURCE_TYPE_DRIVER_FILE=驅動文件 SOURCE_TYPE_DRIVER_FILE=驅動文件
SOURCE_TYPE_MENU=菜單
I18N_DRIVER_NOT_DELETE=使用中的驅動不允許删除 I18N_DRIVER_NOT_DELETE=使用中的驅動不允許删除