审计日志文件备份、文件恢复
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package com.njcn.system.enums;
|
||||
|
||||
/**
|
||||
* @version 1.0.0
|
||||
* @author: chenchao
|
||||
* @date: 2022/07/15 16:21
|
||||
*/
|
||||
public enum AuditLogEnum {
|
||||
|
||||
/**
|
||||
* 审计日志模块异常响应码
|
||||
*/
|
||||
NOT_FIND_FILE("A0300", "文件未备份,请先备份文件"),
|
||||
LOG_EXCEPTION("A0301", "导入旧日志文件异常")
|
||||
;
|
||||
|
||||
|
||||
private final String code;
|
||||
|
||||
private final String message;
|
||||
|
||||
AuditLogEnum(String code, String message) {
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||
import com.njcn.common.pojo.response.HttpResult;
|
||||
import com.njcn.common.utils.HttpResultUtil;
|
||||
import com.njcn.system.pojo.param.AuditParam;
|
||||
import com.njcn.system.pojo.vo.AuditLogCusVO;
|
||||
import com.njcn.system.pojo.vo.AuditLogVO;
|
||||
import com.njcn.system.service.AuditService;
|
||||
import com.njcn.web.controller.BaseController;
|
||||
@@ -57,15 +56,24 @@ public class AuditController extends BaseController {
|
||||
}
|
||||
|
||||
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||
@PostMapping("/censusAuditLog")
|
||||
@ApiOperation("审计日志统计")
|
||||
@ApiImplicitParam(name = "auditParam", value = "审计日志参数", required = true)
|
||||
public HttpResult<Page<AuditLogCusVO>> censusAuditLog(@RequestBody @Validated AuditParam auditParam){
|
||||
String methodDescribe = getMethodDescribe("censusAuditLog");
|
||||
Page<AuditLogCusVO> result = auditService.censusAuditLog(auditParam);
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,result,methodDescribe);
|
||||
@PostMapping("/recoverLogFile")
|
||||
@ApiOperation("日志文件恢复")
|
||||
public HttpResult recoverLogFile() {
|
||||
String methodDescribe = getMethodDescribe("recoverLogFile");
|
||||
boolean res = auditService.recoverLogFile();
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,res,methodDescribe);
|
||||
}
|
||||
|
||||
// @OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||
// @PostMapping("/censusAuditLog")
|
||||
// @ApiOperation("审计日志统计")
|
||||
// @ApiImplicitParam(name = "auditParam", value = "审计日志参数", required = true)
|
||||
// public HttpResult<Page<AuditLogCusVO>> censusAuditLog(@RequestBody @Validated AuditParam auditParam){
|
||||
// String methodDescribe = getMethodDescribe("censusAuditLog");
|
||||
// Page<AuditLogCusVO> result = auditService.censusAuditLog(auditParam);
|
||||
// return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,result,methodDescribe);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.njcn.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.njcn.system.pojo.dto.excel.UserLogExcel;
|
||||
import com.njcn.system.pojo.param.AuditParam;
|
||||
import com.njcn.system.pojo.po.UserLog;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
@@ -18,5 +20,9 @@ public interface AuditMapper {
|
||||
List<UserLog> selectAuditLog(@Param("auditParam") AuditParam auditParam);
|
||||
|
||||
int getCount(@Param("auditParam") AuditParam auditParam);
|
||||
/**
|
||||
* 查询需要备份的审计日志
|
||||
*/
|
||||
List<UserLogExcel> queryExportUser(@Param("ew") QueryWrapper<UserLogExcel> queryWrapper);
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
SELECT
|
||||
id,
|
||||
user_name userName,
|
||||
login_name loginName,
|
||||
ip,
|
||||
operate,
|
||||
operate_type operateType,
|
||||
@@ -43,4 +44,27 @@
|
||||
AND id >= (select id from sys_user_log order by id limit #{auditParam.pageNum}, 1) limit #{auditParam.pageSize}
|
||||
</select>
|
||||
|
||||
<select id="queryExportUser" resultType="UserLogExcel">
|
||||
SELECT
|
||||
id,
|
||||
login_name loginName,
|
||||
user_name userName,
|
||||
ip,
|
||||
operate,
|
||||
operate_type operateType,
|
||||
result,
|
||||
fail_reason failReason,
|
||||
level,
|
||||
type,
|
||||
service_name serviceName,
|
||||
state,
|
||||
create_by createBy,
|
||||
create_time createTime,
|
||||
update_by updateBy,
|
||||
update_time updateTime
|
||||
FROM sys_user_log
|
||||
Where ${ew.sqlSegment}
|
||||
</select>
|
||||
|
||||
|
||||
</mapper>
|
||||
@@ -24,6 +24,11 @@ public interface AuditService extends IService<UserLog> {
|
||||
*/
|
||||
void logFileWriter();
|
||||
|
||||
/**
|
||||
* 日志文件数据恢复
|
||||
*/
|
||||
boolean recoverLogFile();
|
||||
|
||||
/**
|
||||
* 分页获取审计日志统计列表
|
||||
* @param auditParam
|
||||
|
||||
@@ -1,22 +1,38 @@
|
||||
package com.njcn.system.service.impl;
|
||||
|
||||
import cn.afterturn.easypoi.excel.entity.ImportParams;
|
||||
import cn.afterturn.easypoi.excel.imports.ExcelImportService;
|
||||
import cn.afterturn.easypoi.exception.excel.ExcelImportException;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.poi.excel.ExcelUtil;
|
||||
import com.njcn.system.config.SystemBaseConfig;
|
||||
import com.njcn.system.enums.AuditLogEnum;
|
||||
import com.njcn.system.mapper.AuditMapper;
|
||||
import com.njcn.system.mapper.UserLogMapper;
|
||||
import com.njcn.system.pojo.dto.excel.UserLogExcel;
|
||||
import com.njcn.system.pojo.param.AuditParam;
|
||||
import com.njcn.system.pojo.po.UserLog;
|
||||
import com.njcn.system.pojo.vo.AuditLogCusVO;
|
||||
import com.njcn.system.pojo.vo.AuditLogVO;
|
||||
import com.njcn.system.service.AuditService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.compress.utils.IOUtils;
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
@@ -36,6 +52,8 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
|
||||
private final UserLogMapper userLogMapper;
|
||||
|
||||
private final SystemBaseConfig SystemBaseConfig;
|
||||
|
||||
@Override
|
||||
public Page<AuditLogVO> getAuditLog(AuditParam auditParam) {
|
||||
List<AuditLogVO> auditLogVOS = new ArrayList<>();
|
||||
@@ -58,10 +76,14 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
AuditLogVO auditLogVO = new AuditLogVO();
|
||||
String updateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(userLog.getUpdateTime());
|
||||
auditLogVO.setTime(updateTime);
|
||||
auditLogVO.setUserName(userLog.getUserName());
|
||||
if (userLog.getUserName().isEmpty()) {
|
||||
auditLogVO.setUserName(userLog.getLoginName());
|
||||
} else {
|
||||
auditLogVO.setUserName(userLog.getUserName());
|
||||
}
|
||||
auditLogVO.setOperate(userLog.getOperate());
|
||||
StringBuilder describe = new StringBuilder();
|
||||
describe.append(userLog.getUserName()).append("在").append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(userLog.getUpdateTime())).append("在").append(userLog.getIp()).append("执行了").append(userLog.getOperate()).append(",结果为");
|
||||
describe.append(auditLogVO.getUserName()).append("在").append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(userLog.getUpdateTime())).append("在").append(userLog.getIp()).append("执行了").append(userLog.getOperate()).append(",结果为");
|
||||
if (userLog.getResult()==1) {
|
||||
describe.append("成功");
|
||||
}
|
||||
@@ -89,27 +111,101 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
|
||||
@Override
|
||||
public void logFileWriter() {
|
||||
SystemBaseConfig SystemBaseConfig = null;
|
||||
LocalDate nowDate = LocalDate.now();
|
||||
LocalDate agoDate = nowDate.minusMonths(6).with(TemporalAdjusters.firstDayOfMonth());
|
||||
String date = agoDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
|
||||
|
||||
// String time = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(agoDate);
|
||||
// queryWrapper.between("sys_user_log.update_time","2022-02-01","2022-07-31");
|
||||
|
||||
QueryWrapper<UserLog> queryWrapper = new QueryWrapper<>();
|
||||
QueryWrapper<UserLogExcel> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.ge("sys_user_log.update_time",date);
|
||||
List<UserLog> userLogs = userLogMapper.selectList(queryWrapper);
|
||||
try {
|
||||
File file = new File(SystemBaseConfig.getTempPath()+ File.separator + nowDate +" sys_user_log.txt");
|
||||
//将list写入文件并生成路径
|
||||
FileUtils.writeLines(file, userLogs, true);
|
||||
//读取文件大小
|
||||
long sizeOf = FileUtils.sizeOf(new File(file.getPath()));
|
||||
List<UserLogExcel> excelList = auditMapper.queryExportUser(queryWrapper);
|
||||
|
||||
File filePath = new File(SystemBaseConfig.getTempPath());
|
||||
filePath.mkdirs();
|
||||
File file = new File(filePath.getPath() + File.separator + nowDate +" sys_user_log.xlsx");
|
||||
ExcelUtil.exportExcelWithTargetFile(file, UserLogExcel.class, excelList);
|
||||
|
||||
// QueryWrapper<UserLog> queryWrapper = new QueryWrapper<>();
|
||||
// queryWrapper.ge("sys_user_log.update_time",date);
|
||||
// List<UserLog> userLogs = userLogMapper.selectList(queryWrapper);
|
||||
// try {
|
||||
// File file = new File(SystemBaseConfig.getTempPath()+ File.separator + nowDate +" sys_user_log.txt");
|
||||
// //将list写入文件并生成路径
|
||||
// FileUtils.writeLines(file, userLogs, true);
|
||||
// //读取文件大小
|
||||
// long sizeOf = FileUtils.sizeOf(new File(file.getPath()));
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
public static MultipartFile getMultipartFile(File file) {
|
||||
FileItem item = new DiskFileItemFactory().createItem("file"
|
||||
, MediaType.MULTIPART_FORM_DATA_VALUE
|
||||
, true
|
||||
, file.getName());
|
||||
try (InputStream input = new FileInputStream(file);
|
||||
OutputStream os = item.getOutputStream()) {
|
||||
// 流转移
|
||||
IOUtils.copy(input, os);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new IllegalArgumentException("Invalid file: " + e, e);
|
||||
}
|
||||
return new CommonsMultipartFile(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean recoverLogFile() {
|
||||
LocalDate nowDate = LocalDate.now();
|
||||
File file = new File(SystemBaseConfig.getTempPath()+ File.separator + nowDate +" sys_user_log.xlsx");
|
||||
if (!file.exists()) {
|
||||
throw new BusinessException(AuditLogEnum.NOT_FIND_FILE);
|
||||
}
|
||||
|
||||
ImportParams params = new ImportParams();
|
||||
params.setHeadRows(1);//表头
|
||||
params.setTitleRows(0);//标题行数
|
||||
params.setNeedVerify(false);
|
||||
//第一个sheet为台账信息
|
||||
params.setStartSheetIndex(0);
|
||||
params.setSheetNum(1);
|
||||
|
||||
FileInputStream in = null;
|
||||
|
||||
try {
|
||||
in = new FileInputStream(file);
|
||||
List<UserLogExcel> excelList = new ExcelImportService().importExcelByIs(in, UserLogExcel.class, params, false).getList();
|
||||
if (excelList.get(0).getId().isEmpty()) {
|
||||
throw new BusinessException(AuditLogEnum.LOG_EXCEPTION);
|
||||
}
|
||||
for (UserLogExcel excel: excelList) {
|
||||
UserLog userLog = new UserLog();
|
||||
BeanUtil.copyProperties(excel,userLog);
|
||||
this.saveOrUpdate(userLog);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new ExcelImportException(e.getMessage(), e);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(in);
|
||||
}
|
||||
|
||||
|
||||
// try {
|
||||
// // FileInputStream in = new FileInputStream(file);
|
||||
// MultipartFile multipartFile = getMultipartFile(file);
|
||||
//
|
||||
// List<Object> list = ExcelImportUtil.importExcelMore(multipartFile.getInputStream(), UserLogExcel.class, params).getList();
|
||||
//
|
||||
// System.out.println(list);
|
||||
//
|
||||
// // this.saveOrUpdate(excelList.get(0));
|
||||
// } catch (Exception e) {
|
||||
// throw new BusinessException(AuditLogEnum.LOG_EXCEPTION);
|
||||
// }
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user