|
1 | 1 | package com.weiziplus.pc.core.system.service;
|
2 | 2 |
|
| 3 | +import com.alibaba.excel.ExcelWriter; |
| 4 | +import com.alibaba.excel.metadata.Sheet; |
| 5 | +import com.alibaba.excel.metadata.Table; |
| 6 | +import com.alibaba.excel.support.ExcelTypeEnum; |
3 | 7 | import com.github.pagehelper.PageHelper;
|
4 | 8 | import com.weiziplus.common.base.BaseService;
|
| 9 | +import com.weiziplus.common.base.BaseWhere; |
| 10 | +import com.weiziplus.common.models.SysUserLog; |
| 11 | +import com.weiziplus.common.util.HttpRequestUtils; |
5 | 12 | import com.weiziplus.common.util.PageUtils;
|
6 | 13 | import com.weiziplus.common.util.ResultUtils;
|
| 14 | +import com.weiziplus.common.util.ToolUtils; |
| 15 | +import com.weiziplus.common.util.token.AdminTokenUtils; |
| 16 | +import com.weiziplus.pc.common.config.MyGlobalConfig; |
7 | 17 | import com.weiziplus.pc.core.system.mapper.SysUserLogMapper;
|
8 | 18 | import com.weiziplus.pc.core.system.vo.SysUserLogVo;
|
9 | 19 | import lombok.extern.slf4j.Slf4j;
|
10 | 20 | import org.springframework.beans.factory.annotation.Autowired;
|
11 | 21 | import org.springframework.stereotype.Service;
|
| 22 | +import org.springframework.web.context.request.RequestContextHolder; |
| 23 | +import org.springframework.web.context.request.ServletRequestAttributes; |
12 | 24 |
|
| 25 | +import javax.servlet.ServletOutputStream; |
| 26 | +import javax.servlet.http.HttpServletRequest; |
| 27 | +import javax.servlet.http.HttpServletResponse; |
| 28 | +import java.io.IOException; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Arrays; |
13 | 32 | import java.util.List;
|
14 | 33 |
|
15 | 34 | /**
|
@@ -54,4 +73,77 @@ public ResultUtils<PageUtils<List<SysUserLogVo>>> getPageList(Integer pageNum, I
|
54 | 73 | return ResultUtils.success(pageUtil);
|
55 | 74 | }
|
56 | 75 |
|
| 76 | + /** |
| 77 | + * 导出excel |
| 78 | + * |
| 79 | + * @param startTime |
| 80 | + * @param endTime |
| 81 | + */ |
| 82 | + public void exportExcel(String startTime, String endTime) { |
| 83 | + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); |
| 84 | + HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); |
| 85 | + Integer userIdByHttpServletRequest = AdminTokenUtils.getUserIdByHttpServletRequest(request); |
| 86 | + if (!MyGlobalConfig.SYS_USER_SUPER_ADMIN_ID.equals(userIdByHttpServletRequest)) { |
| 87 | + HttpRequestUtils.handleErrorResponse( |
| 88 | + response, ResultUtils.errorRole("只有超级管理员才可以导出系统日志"), "非超级管理员导出系统用户日志"); |
| 89 | + return; |
| 90 | + } |
| 91 | + List<SysUserLog> sysUserLogList = baseFindListByClassAndBaseWhereList(SysUserLog.class, new ArrayList<BaseWhere>(ToolUtils.initialCapacity(2)) {{ |
| 92 | + add(new BaseWhere(SysUserLog.COLUMN_CREATE_TIME, BaseWhere.Where.MORE_THAN.getValue(), startTime)); |
| 93 | + add(new BaseWhere(SysUserLog.COLUMN_CREATE_TIME, BaseWhere.Where.LESS_THAN_EQUAL.getValue(), endTime)); |
| 94 | + }}); |
| 95 | + HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); |
| 96 | + ServletOutputStream outputStream = null; |
| 97 | + try { |
| 98 | + outputStream = response.getOutputStream(); |
| 99 | + ExcelWriter writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLSX); |
| 100 | + Sheet sheet = new Sheet(1, 0); |
| 101 | + sheet.setSheetName("系统用户日志"); |
| 102 | + Table table = new Table(1); |
| 103 | + //表头 |
| 104 | + List<List<String>> titleList = new ArrayList<>(ToolUtils.initialCapacity(7)); |
| 105 | + titleList.add(Arrays.asList("请求路径")); |
| 106 | + titleList.add(Arrays.asList("请求参数")); |
| 107 | + titleList.add(Arrays.asList("请求类型")); |
| 108 | + titleList.add(Arrays.asList("操作描述")); |
| 109 | + titleList.add(Arrays.asList("浏览器")); |
| 110 | + titleList.add(Arrays.asList("操作系统")); |
| 111 | + titleList.add(Arrays.asList("创建时间")); |
| 112 | + table.setHead(titleList); |
| 113 | + //内容 |
| 114 | + List<List<String>> dataList = new ArrayList<>(); |
| 115 | + String[] typeArr = {null, "查询", "新增", "修改", "删除"}; |
| 116 | + for (SysUserLog sysUserLog : sysUserLogList) { |
| 117 | + dataList.add(Arrays.asList( |
| 118 | + sysUserLog.getUrl(), |
| 119 | + sysUserLog.getParam(), |
| 120 | + typeArr[sysUserLog.getType()], |
| 121 | + sysUserLog.getDescription(), |
| 122 | + sysUserLog.getBorderName(), |
| 123 | + sysUserLog.getOsName(), |
| 124 | + sysUserLog.getCreateTime() |
| 125 | + )); |
| 126 | + } |
| 127 | + writer.write0(dataList, sheet, table); |
| 128 | + // 下载EXCEL |
| 129 | + response.setHeader("Content-Disposition", "attachment"); |
| 130 | + response.setContentType("multipart/form-data"); |
| 131 | + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); |
| 132 | + writer.finish(); |
| 133 | + outputStream.flush(); |
| 134 | + } catch (IOException e) { |
| 135 | + log.warn("系统用户日志导出excel出错,详情:" + e); |
| 136 | + HttpRequestUtils.handleErrorResponse( |
| 137 | + response, ResultUtils.error("系统用户日志导出excel出错,详情:" + e), "系统用户日志导出excel出错"); |
| 138 | + } finally { |
| 139 | + if (null != outputStream) { |
| 140 | + try { |
| 141 | + outputStream.close(); |
| 142 | + } catch (Exception e) { |
| 143 | + log.warn("系统用户日志导出excel关闭流出错,详情:" + e); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
57 | 149 | }
|
0 commit comments