excel导出功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package com.infosec.ztpdp.policycenter.module.audit.controller;
 
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
 
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import com.google.common.collect.Lists;
import com.infosec.ztpdp.policycenter.common.ModuleConverter;
import com.infosec.ztpdp.policycenter.common.dbbase.page.BaseQuery;
import com.infosec.ztpdp.policycenter.common.dbbase.page.Pagination;
import com.infosec.ztpdp.policycenter.common.dbbase.page.ParamField;
import com.infosec.ztpdp.policycenter.common.jsonresult.JsonErrotCode;
import com.infosec.ztpdp.policycenter.common.jsonresult.JsonResult;
import com.infosec.ztpdp.policycenter.common.jsonresult.JsonResultUtils;
import com.infosec.ztpdp.policycenter.common.util.AuditInternationKeyConst;
import com.infosec.ztpdp.policycenter.common.util.Const;
import com.infosec.ztpdp.policycenter.common.util.DateUtil;
import com.infosec.ztpdp.policycenter.common.util.deciphering.EncryptionFactory;
import com.infosec.ztpdp.policycenter.common.validate.PatternConst;
import com.infosec.ztpdp.policycenter.common.validate.ValidatedApiParamConst;
import com.infosec.ztpdp.policycenter.common.validate.ValidatedMsgConst;
import com.infosec.ztpdp.policycenter.component.LocaleMessageSourceService;
import com.infosec.ztpdp.policycenter.module.audit.entity.ManagerOperationLogBean;
import com.infosec.ztpdp.policycenter.module.audit.service.IManagerOperationLogService;
import com.infosec.ztpdp.policycenter.module.menumanager.menuconfig.entity.ModuleLogQueryDTO;
import com.infosec.ztpdp.policycenter.module.menumanager.menuconfig.service.SysMenuService;
 
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jxl.CellView;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import springfox.documentation.annotations.ApiIgnore;
 
/**
 *
 * <p>
 * 管理员操作日志
 * </p>
 *
 * <p>
 * 版权所有:北京信安世纪科技股份有限公司 (c) 2022
 * </p>
 *
 * @author jlcui
 * @date: 2023年8月30日 下午2:14:06
 *
 */
@ApiIgnore
@Api(tags = "审计,管理操作日志")
@Validated
@RestController
@RequestMapping("/audit")
public class ManageropeLogController {
 
    @Resource
    private LocaleMessageSourceService localeMessageSourceService;
 
    @Autowired
    private IManagerOperationLogService iManagerOperationLogService;
 
    @Autowired
    private SysMenuService sysMenuService;
 
    private static Map<Integer, String> operationList;
 
    @ApiOperation(value = "获取管理操作日志")
    @GetMapping("/get/manageropelog")
    public JsonResult<Pagination<ManagerOperationLogBean>> page(
            @Min(value = 1, message = ValidatedMsgConst.INTEGER_MSG) @NotNull(message = ValidatedMsgConst.NULL_MSG) @ApiParam(value = "页码,"
                    + ValidatedApiParamConst.INTEGER_MSG + ","
                    + ValidatedApiParamConst.NULL_MSG, required = true) @RequestParam(value = "currentPage", required = true) Integer currentPage,
            @Min(value = 1, message = ValidatedMsgConst.INTEGER_MSG) @NotNull(message = ValidatedMsgConst.NULL_MSG) @ApiParam(value = "每页显示最大数量,"
                    + ValidatedApiParamConst.INTEGER_MSG + ","
                    + ValidatedApiParamConst.NULL_MSG, required = true) @RequestParam(value = "pageSize", required = true) Integer pageSize,
            @Length(max = 30, message = ValidatedMsgConst.LENGTH_LIMIT_MSG) @ApiParam(value = "用户姓名,"
                    + ValidatedApiParamConst.LENGTH_LIMIT_MSG) @RequestParam(value = "username", required = false) String username,
            @Length(max = 30, message = ValidatedMsgConst.LENGTH_LIMIT_MSG) @ApiParam(value = "登录帐号,"
                    + ValidatedApiParamConst.LENGTH_LIMIT_MSG) @RequestParam(value = "loginname", required = false) String loginname,
            @Length(max = 30, message = ValidatedMsgConst.LENGTH_LIMIT_MSG) @ApiParam("用户IP,"
                    + ValidatedApiParamConst.IP_MSG) @RequestParam(value = "userip", required = false) String userip,
            @Length(max = 30, message = ValidatedMsgConst.LENGTH_LIMIT_MSG) @ApiParam(value = "功能模块,"
                    + ValidatedApiParamConst.LENGTH_LIMIT_MSG) @RequestParam(value = "module", required = false) String module,
            @Length(max = 30000, message = ValidatedMsgConst.LENGTH_LIMIT_THIRTY_THOUSAND_MSG) @ApiParam(value = "详细信息,"
                    + ValidatedApiParamConst.LENGTH_LIMIT_THIRTY_THOUSAND_MSG) @RequestParam(value = "details", required = false) String details,
            @Length(max = 30, message = ValidatedMsgConst.LENGTH_LIMIT_MSG) @ApiParam(value = "操作,"
                    + ValidatedApiParamConst.LENGTH_LIMIT_MSG) @RequestParam(value = "operation", required = false) String operation,
            @Pattern(regexp = PatternConst.DATE_PATTERN_PN, message = ValidatedMsgConst.DATE_MSG) @ApiParam(value = "起始时间,"
                    + ValidatedApiParamConst.DATE_MSG) @RequestParam(value = "fromDate", required = false) String fromDate,
            @Pattern(regexp = PatternConst.DATE_PATTERN_PN, message = ValidatedMsgConst.DATE_MSG) @ApiParam(value = "结束时间,"
                    + ValidatedApiParamConst.DATE_MSG) @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request) {
        JsonResult<Pagination<ManagerOperationLogBean>> jsonResult = JsonResultUtils.getFailObj();
        try {
            Pagination<ManagerOperationLogBean> pagination = new Pagination<ManagerOperationLogBean>(currentPage,
                    pageSize);
            BaseQuery query = new BaseQuery();
 
            if (!StringUtils.isEmpty(details)) {
                details = details.replaceAll("%", "%");
                details = details.replaceAll("'", "'");
                details = URLDecoder.decode(details, "utf-8");
                query.addParamFiled("details", ParamField.LIKE, details);
            }
            if (!StringUtils.isEmpty(username)) {
                query.addParamFiled("username", ParamField.LIKE, username);
            }
            if (!StringUtils.isEmpty(loginname)) {
                query.addParamFiled("loginname", ParamField.LIKE, loginname);
 
            }
            if (!StringUtils.isEmpty(userip)) {
                query.addParamFiled("user_ip", ParamField.LIKE, userip);
            }
            if (!StringUtils.isEmpty(module)) {
                query.addParamFiled("module", ParamField.DENGYU, module);
            }
            if (!StringUtils.isEmpty(operation)) {
                query.addParamFiled("handle_type", ParamField.DENGYU, operation);
            }
            if (!StringUtils.isEmpty(fromDate)) {
                query.addParamFiled("create_date", ParamField.DAYU_DENGYU, fromDate);
            }
            if (!StringUtils.isEmpty(endDate)) {
                query.addParamFiled("create_date", ParamField.XIAOYU_DENGYU, endDate);
            }
            query.addOrderField("id", Const.ORDER_TYPE_DESC);
            pagination.setPageSize(pageSize);
            pagination = iManagerOperationLogService.pageByParam(pagination, query);
            List<ManagerOperationLogBean> list = pagination.getList();
            if (list != null && list.size() > 0) {
                for (ManagerOperationLogBean log : list) {
                    String data = getDataEncryption(log);
                    if (!data.equals(log.getSignature())) {
                        log.setDatacheck(false);
                    } else {
                        log.setDatacheck(true);
                    }
                }
            }
            jsonResult.setResult(pagination);
            jsonResult.setCode(JsonErrotCode.SUCCESS_CODE);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg(localeMessageSourceService.getMessage(AuditInternationKeyConst.DEPTOPERATIONFAILED));
        }
 
        return jsonResult;
    }
 
    /**
     *
     * <p>
     * 导出管理操作日志报表
     * </p>
     *
     * @Title: exportReport
     * @param: @param  username
     * @param: @param  loginname
     * @param: @param  userip
     * @param: @param  module
     * @param: @param  details
     * @param: @param  operation
     * @param: @param  fromDate
     * @param: @param  endDate
     * @param: @param  request
     * @param: @param  response
     * @param: @throws Exception
     * @author: jlcui
     * @date: 2023年8月30日 下午3:33:26
     */
    @ApiIgnore
    @ApiOperation(value = "导出管理操作日志报表")
    @GetMapping("/get/manageropelog/export-report")
    public void exportReport(@Length(max = 30, message = ValidatedMsgConst.LENGTH_LIMIT_MSG) @ApiParam(value = "用户姓名,"
            + ValidatedApiParamConst.LENGTH_LIMIT_MSG) @RequestParam(value = "username", required = false) String username,
            @Length(max = 30, message = ValidatedMsgConst.LENGTH_LIMIT_MSG) @ApiParam(value = "登录帐号,"
                    + ValidatedApiParamConst.LENGTH_LIMIT_MSG) @RequestParam(value = "loginname", required = false) String loginname,
            @Length(max = 30, message = ValidatedMsgConst.LENGTH_LIMIT_MSG) @ApiParam("用户IP,"
                    + ValidatedApiParamConst.IP_MSG) @RequestParam(value = "userip", required = false) String userip,
            @Length(max = 30, message = ValidatedMsgConst.LENGTH_LIMIT_MSG) @ApiParam(value = "功能模块,"
                    + ValidatedApiParamConst.LENGTH_LIMIT_MSG) @RequestParam(value = "module", required = false) String module,
            @Length(max = 30000, message = ValidatedMsgConst.LENGTH_LIMIT_THIRTY_THOUSAND_MSG) @ApiParam(value = "详细信息,"
                    + ValidatedApiParamConst.LENGTH_LIMIT_THIRTY_THOUSAND_MSG) @RequestParam(value = "details", required = false) String details,
            @Length(max = 30, message = ValidatedMsgConst.LENGTH_LIMIT_MSG) @ApiParam(value = "操作,"
                    + ValidatedApiParamConst.LENGTH_LIMIT_MSG) @RequestParam(value = "operation", required = false) String operation,
            @Pattern(regexp = PatternConst.DATE_PATTERN_PN, message = ValidatedMsgConst.DATE_MSG) @ApiParam(value = "起始时间,"
                    + ValidatedApiParamConst.DATE_MSG) @RequestParam(value = "fromDate", required = false) String fromDate,
            @Pattern(regexp = PatternConst.DATE_PATTERN_PN, message = ValidatedMsgConst.DATE_MSG) @ApiParam(value = "结束时间,"
                    + ValidatedApiParamConst.DATE_MSG) @RequestParam(value = "endDate", required = false) String endDate,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
 
        int handleType = 0;
        String allOper = "所有操作";
        if (!StringUtils.isEmpty(operation) && !allOper.equals(operation)) {
            handleType = Integer.parseInt(operation);
        }
        String allModu = "所有模块";
        if (module == null || allModu.equals(module)) {
            module = "";
        }
 
        BaseQuery query = new BaseQuery();
 
        if (!StringUtils.isEmpty(details)) {
            details = details.replaceAll("%", "%");
            details = details.replaceAll("'", "'");
            details = URLDecoder.decode(details, "utf-8");
            query.addParamFiled("details", ParamField.LIKE, details);
        }
        if (!StringUtils.isEmpty(username)) {
            query.addParamFiled("username", ParamField.LIKE, username);
        }
        if (!StringUtils.isEmpty(loginname)) {
            query.addParamFiled("loginname", ParamField.LIKE, loginname);
 
        }
        if (!StringUtils.isEmpty(userip)) {
            query.addParamFiled("user_ip", ParamField.LIKE, userip);
        }
        if (!StringUtils.isEmpty(module)) {
            query.addParamFiled("module", ParamField.DENGYU, module);
        }
        if (!StringUtils.isEmpty(operation)) {
            query.addParamFiled("handle_type", ParamField.DENGYU, operation);
        }
        if (!StringUtils.isEmpty(fromDate)) {
            query.addParamFiled("create_date", ParamField.DAYU_DENGYU, fromDate);
        }
 
        OutputStream output = null;
        try {
            response.setContentType("text/html;charset=utf-8");
            String fileName = "manageropeLog.xls";
            response.reset();
            response.setContentType("application/x-msdownload");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            output = response.getOutputStream();
 
            List<ManagerOperationLogBean> list = iManagerOperationLogService.findListByParam(query);
            export(output, list);
 
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.flush();
                    output.close();
                    output = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    /**
     *
     * <p>
     * 创建excel 表格
     * </p>
     *
     * @Title: export
     * @param: @param os
     * @param: @param vos
     * @author: jlcui
     * @date: 2023年8月30日 下午3:35:41
     */
    private void export(OutputStream os, List<ManagerOperationLogBean> vos) {
        try {
            SimpleDateFormat sdf1 = DateUtil.getBjMinsFormat();
            Map<String, String> modulMap = new HashMap<String, String>();
            // 功能模块
            List<ModuleLogQueryDTO> moduleList = sysMenuService.getModuleList();
            if (!CollectionUtils.isEmpty(moduleList)) {
                for (ModuleLogQueryDTO bean : moduleList) {
                    modulMap.put(bean.getModuleCode(), bean.getModuleName());
                }
            }
            // 初始化报表风格
            WritableWorkbook wbook = Workbook.createWorkbook(os);
            // 这里先判断数据是否为空,是:值创建表格的表头后返回
            if (vos != null && vos.size() > 0) {
                List<List<ManagerOperationLogBean>> partition = Lists.partition(vos, 50000);
                for (int indexs = 0; indexs < partition.size(); indexs++) {
                    List<ManagerOperationLogBean> list = partition.get(indexs);
                    WritableSheet wsheet = wbook.createSheet("table" + indexs, indexs);
                    int cols = creatWSheetHead(wsheet);
                    int rows = 0;
                    Label lable;
 
                    for (int i = 0; i < list.size(); i++) {
                        cols = 0;
                        rows++;
                        ManagerOperationLogBean log = list.get(i);
                        // 序号
                        lable = new Label(cols, rows, String.valueOf(i + 1));
                        wsheet.addCell(lable);
                        // 用户登录名
                        lable = new Label(cols + 1, rows, log.getLoginname());
                        wsheet.addCell(lable);
                        // 用户姓名
                        lable = new Label(cols + 2, rows, log.getUsername());
                        wsheet.addCell(lable);
                        // 功能模块
                        String moduleName = modulMap.get(list.get(i).getModule());
                        lable = new Label(cols + 3, rows, moduleName);
                        wsheet.addCell(lable);
                        // 操作
                        if (null == operationList) {
                            operationList = ModuleConverter.getOperation();
                        }
                        String handtype = operationList.get(list.get(i).getHandleType());
                        lable = new Label(cols + 4, rows, handtype);
                        wsheet.addCell(lable);
                        // 时间
                        String dateStr = sdf1.format(list.get(i).getCreateDate());
                        lable = new Label(cols + 5, rows, dateStr);
                        wsheet.addCell(lable);
                        // 用户ip
                        lable = new Label(cols + 6, rows, list.get(i).getUserip());
                        wsheet.addCell(lable);
                        // 详细信息
                        lable = new Label(cols + 7, rows, list.get(i).getDetails());
                        wsheet.addCell(lable);
                        // 访问结果
                        String result = Optional.ofNullable(list.get(i).getResult()).map(x -> x == 0 ? "成功" : "失败")
                                .get();
                        lable = new Label(cols + 8, rows, result);
                        wsheet.addCell(lable);
                    }
                }
            } else {
                WritableSheet wsheet = wbook.createSheet("table", 0);
                creatWSheetHead(wsheet);
            }
            wbook.write();
            wbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private int creatWSheetHead(WritableSheet wsheet) throws WriteException {
        CellView cellView = new CellView();
        // 设置自动大小
        cellView.setAutosize(true);
        wsheet.setColumnView(2, cellView);
 
        WritableFont font1 = new WritableFont(WritableFont.ARIAL, 8, WritableFont.BOLD, false,
                jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
        WritableCellFormat format1 = new WritableCellFormat(font1);
        format1.setAlignment(jxl.format.Alignment.CENTRE);
        format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
        format1.setWrap(true);
        int rows = 0;
        int cols = 0;
        Label lable = null;
        lable = new Label(cols, rows, "序号");
        wsheet.addCell(lable);
        cols++;
        lable = new Label(cols, rows, "登录帐号");
        wsheet.addCell(lable);
        cols++;
        lable = new Label(cols, rows, "用户姓名");
        wsheet.addCell(lable);
        cols++;
        lable = new Label(cols, rows, "功能模块");
        wsheet.addCell(lable);
        cols++;
        lable = new Label(cols, rows, "操作");
        wsheet.addCell(lable);
        cols++;
        lable = new Label(cols, rows, "时间");
        wsheet.addCell(lable);
        cols++;
        lable = new Label(cols, rows, "用户IP");
        wsheet.addCell(lable);
        cols++;
        lable = new Label(cols, rows, "详细信息");
        wsheet.addCell(lable);
        cols++;
        lable = new Label(cols, rows, "访问结果");
        wsheet.addCell(lable);
        return cols;
    }
 
    /**
     * 获取功能模块和操作模块信息
     *
     * @param @param request
     * @return JsonResult result moduleList operationList pageNumber
     * @author liyy
     * @date 2019年6月11日下午6:27:00
     */
    @ApiOperation(value = "获取功能模块和操作模块信息")
    @GetMapping("/get/improperhandle/get-module-and-oper")
    public JsonResult<Map<String, Object>> getModuleAndOper(HttpServletRequest request) {
        JsonResult<Map<String, Object>> jsonResult = JsonResultUtils.getFailObj();
        try {
            Map<String, Object> modelMap = new HashMap<String, Object>(16);
            List<ModuleLogQueryDTO> moduleList = sysMenuService.getModuleList();
            operationList = ModuleConverter.getOperation();
 
            modelMap.put("moduleList", moduleList);
            modelMap.put("operationList", operationList);
            jsonResult.setResult(modelMap);
            jsonResult.setCode(JsonErrotCode.SUCCESS_CODE);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg(localeMessageSourceService.getMessage(AuditInternationKeyConst.DEPTOPERATIONFAILED));
        }
 
        return jsonResult;
    }
 
    // 数据加密
    public String getDataEncryption(ManagerOperationLogBean bean) {
        String dataEncryption = bean.getLoginname() + bean.getUsername() + bean.getLogcode() + bean.getUserip()
                + bean.getDetails() + bean.getHandleType() + bean.getModule() + bean.getResult();
        return EncryptionFactory.getEncryptions(Const.AUDIT_ENCRYPTION_MODE).encrypt(dataEncryption);
    }
 
}

  

posted @   JLCUI  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示