diff --git a/pqs-system/system-api/src/main/java/com/njcn/system/enums/AuditLogEnum.java b/pqs-system/system-api/src/main/java/com/njcn/system/enums/AuditLogEnum.java new file mode 100644 index 000000000..1e0b030fd --- /dev/null +++ b/pqs-system/system-api/src/main/java/com/njcn/system/enums/AuditLogEnum.java @@ -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; + } + +} diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/controller/AuditController.java b/pqs-system/system-boot/src/main/java/com/njcn/system/controller/AuditController.java index ff53a5635..7670d435c 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/controller/AuditController.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/controller/AuditController.java @@ -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> censusAuditLog(@RequestBody @Validated AuditParam auditParam){ - String methodDescribe = getMethodDescribe("censusAuditLog"); - Page 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> censusAuditLog(@RequestBody @Validated AuditParam auditParam){ + // String methodDescribe = getMethodDescribe("censusAuditLog"); + // Page result = auditService.censusAuditLog(auditParam); + // return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,result,methodDescribe); + // } + } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/AuditMapper.java b/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/AuditMapper.java index 427938150..70aa80913 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/AuditMapper.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/AuditMapper.java @@ -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 selectAuditLog(@Param("auditParam") AuditParam auditParam); int getCount(@Param("auditParam") AuditParam auditParam); + /** + * 查询需要备份的审计日志 + */ + List queryExportUser(@Param("ew") QueryWrapper queryWrapper); } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/mapping/AuditMapper.xml b/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/mapping/AuditMapper.xml index e91163b5b..840e54371 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/mapping/AuditMapper.xml +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/mapper/mapping/AuditMapper.xml @@ -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} + + + \ No newline at end of file diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/service/AuditService.java b/pqs-system/system-boot/src/main/java/com/njcn/system/service/AuditService.java index da93995a4..7da549a11 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/service/AuditService.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/service/AuditService.java @@ -24,6 +24,11 @@ public interface AuditService extends IService { */ void logFileWriter(); + /** + * 日志文件数据恢复 + */ + boolean recoverLogFile(); + /** * 分页获取审计日志统计列表 * @param auditParam diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/AuditServiceImpl.java b/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/AuditServiceImpl.java index b9783cc4f..0fe1293fd 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/AuditServiceImpl.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/AuditServiceImpl.java @@ -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 implem private final UserLogMapper userLogMapper; + private final SystemBaseConfig SystemBaseConfig; + @Override public Page getAuditLog(AuditParam auditParam) { List auditLogVOS = new ArrayList<>(); @@ -58,10 +76,14 @@ public class AuditServiceImpl extends ServiceImpl 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 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 queryWrapper = new QueryWrapper<>(); + QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.ge("sys_user_log.update_time",date); - List 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 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 queryWrapper = new QueryWrapper<>(); + // queryWrapper.ge("sys_user_log.update_time",date); + // List 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 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 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; }