1.审计管理
2.谐波检测bug修改
This commit is contained in:
@@ -51,7 +51,7 @@ public class AuditController extends BaseController {
|
||||
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||
@PostMapping("/logFileWriter")
|
||||
@ApiOperation("日志文件备份")
|
||||
public HttpResult logFileWriter() {
|
||||
public HttpResult logFileWriter(){
|
||||
String methodDescribe = getMethodDescribe("logFileWriter");
|
||||
auditService.logFileWriter();
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,null,methodDescribe);
|
||||
@@ -94,6 +94,13 @@ public class AuditController extends BaseController {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
|
||||
}
|
||||
|
||||
|
||||
@OperateInfo(info = LogEnum.SYSTEM_COMMON)
|
||||
@PostMapping("/getMemoInfo")
|
||||
@ApiOperation("获取表空间大小")
|
||||
public HttpResult<Float> getMemoInfo() {
|
||||
String methodDescribe = getMethodDescribe("getMemoInfo");
|
||||
Float memoInfo = auditService.getMemoInfo();
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, memoInfo, methodDescribe);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.njcn.system.controller;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.njcn.common.pojo.annotation.OperateInfo;
|
||||
import com.njcn.common.pojo.constant.OperateType;
|
||||
@@ -11,6 +12,7 @@ import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.common.pojo.response.HttpResult;
|
||||
import com.njcn.common.utils.HttpResultUtil;
|
||||
import com.njcn.common.utils.LogUtil;
|
||||
import com.njcn.system.enums.SystemResponseEnum;
|
||||
import com.njcn.system.pojo.param.ConfigParam;
|
||||
import com.njcn.system.pojo.po.Config;
|
||||
import com.njcn.system.service.IConfigService;
|
||||
@@ -122,6 +124,13 @@ public class ConfigController extends BaseController {
|
||||
public HttpResult<Config> removeSysConfigById(@RequestParam("id") String id) {
|
||||
String methodDescribe = getMethodDescribe("removeSysConfigById");
|
||||
LogUtil.njcnDebug(log, "{}", methodDescribe, id);
|
||||
int count = iConfigService.count(new LambdaQueryWrapper<Config>()
|
||||
.eq(Config::getState, DataStateEnum.ENABLE.getCode())
|
||||
.ne(Config::getId, id));
|
||||
if(count==0){
|
||||
//不可更改当前激活状态,必须保留一个激活系统
|
||||
throw new BusinessException(SystemResponseEnum.ACTIVATED_STATE);
|
||||
}
|
||||
boolean res = iConfigService.removeById(id);
|
||||
if (res) {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.njcn.system.excel;
|
||||
|
||||
import com.alibaba.excel.context.AnalysisContext;
|
||||
import com.alibaba.excel.read.listener.ReadListener;
|
||||
import com.alibaba.excel.util.ListUtils;
|
||||
import com.njcn.system.mapper.UserLogMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
// 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
|
||||
public class DemoDataListener implements ReadListener<UserLogExcel> {
|
||||
|
||||
|
||||
/**
|
||||
* 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收
|
||||
*/
|
||||
private static final int BATCH_COUNT = 5000;
|
||||
/**
|
||||
* 缓存的数据
|
||||
*/
|
||||
private List<UserLogExcel> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
|
||||
/**
|
||||
* 假设这个是一个DAO,当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。
|
||||
*/
|
||||
private UserLogMapper userLogMapper;
|
||||
|
||||
/**
|
||||
* 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
|
||||
*
|
||||
* @param userLogMapper
|
||||
*/
|
||||
public DemoDataListener(UserLogMapper userLogMapper) {
|
||||
this.userLogMapper = userLogMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 这个每一条数据解析都会来调用
|
||||
*
|
||||
* @param data one row value. Is is same as {@link AnalysisContext#readRowHolder()}
|
||||
* @param context
|
||||
*/
|
||||
@Override
|
||||
public void invoke(UserLogExcel data, AnalysisContext context) {
|
||||
cachedDataList.add(data);
|
||||
// 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
|
||||
if (cachedDataList.size() >= BATCH_COUNT) {
|
||||
saveData();
|
||||
// 存储完成清理 list
|
||||
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 所有数据解析完成了 都会来调用
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext context) {
|
||||
// 这里也要保存数据,确保最后遗留的数据也存储到数据库
|
||||
saveData();
|
||||
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加上存储数据库
|
||||
*/
|
||||
public void saveData() {
|
||||
userLogMapper.insertBatch(cachedDataList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.njcn.system.excel;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.alibaba.excel.annotation.write.style.ColumnWidth;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @version 1.0.0
|
||||
* @author: chenchao
|
||||
* @date: 2022/07/18 11:04
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@EqualsAndHashCode
|
||||
public class UserLogExcel implements Serializable {
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "id主键")
|
||||
private String id;
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "登录名")
|
||||
private String loginName;
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "用户名")
|
||||
private String userName;
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "操作ip")
|
||||
private String ip;
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "操作内容")
|
||||
private String operate;
|
||||
|
||||
@ColumnWidth(15)
|
||||
@ExcelProperty(value = "操作类型")
|
||||
private String operateType;
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "操作结果")
|
||||
private Integer result;
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "失败原因")
|
||||
private String failReason;
|
||||
|
||||
@ColumnWidth(30)
|
||||
@ExcelProperty(value = "时间等级(严重度)")
|
||||
private Integer level;
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "事件类型")
|
||||
private Integer type;
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "模块名称")
|
||||
private String serviceName="暂态";
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "告警标志")
|
||||
private Integer state;
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "创建用户")
|
||||
private String createBy;
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "创建时间" )
|
||||
private String createTime;
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "更新用户")
|
||||
private String updateBy;
|
||||
|
||||
@ColumnWidth(20)
|
||||
@ExcelProperty(value = "更新时间" )
|
||||
private String updateTime;
|
||||
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.njcn.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.njcn.system.pojo.dto.excel.UserLogExcel;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.njcn.system.excel.UserLogExcel;
|
||||
import com.njcn.system.pojo.param.AuditParam;
|
||||
import com.njcn.system.pojo.po.UserLog;
|
||||
import com.njcn.system.pojo.vo.AuditLogCusVO;
|
||||
@@ -25,6 +26,8 @@ public interface AuditMapper {
|
||||
* 查询需要备份的审计日志
|
||||
*/
|
||||
List<UserLogExcel> queryExportUser(@Param("ew") QueryWrapper<UserLogExcel> queryWrapper);
|
||||
|
||||
Page<UserLogExcel> queryExportLimit(Page<UserLogExcel> page, @Param("startTime")String time,@Param("endTime")String endTime);
|
||||
/**
|
||||
* 获取审计日志统计列表
|
||||
*/
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package com.njcn.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.njcn.system.pojo.dto.AreaTreeDTO;
|
||||
import com.njcn.system.pojo.po.Area;
|
||||
import com.njcn.system.excel.UserLogExcel;
|
||||
import com.njcn.system.pojo.po.UserLog;
|
||||
import com.njcn.system.pojo.vo.AreaTreeVO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -20,5 +17,23 @@ import java.util.List;
|
||||
public interface UserLogMapper extends BaseMapper<UserLog> {
|
||||
|
||||
|
||||
/**
|
||||
* 日志批量插入
|
||||
* @param userLogs
|
||||
*/
|
||||
void insertBatch(@Param("list") List<UserLogExcel> userLogs);
|
||||
|
||||
/**
|
||||
* 根据id查询数据库是否存在
|
||||
* @param idList
|
||||
* @return
|
||||
*/
|
||||
List<String> ids(@Param("ids")List<String> idList);
|
||||
|
||||
/**
|
||||
* 查询表空间大小
|
||||
* @param schema
|
||||
* @return
|
||||
*/
|
||||
Float getMemoInfo(@Param("schema") String schema);
|
||||
}
|
||||
|
||||
@@ -91,13 +91,13 @@
|
||||
AND operate != "unknown operate"
|
||||
AND update_time >= #{auditParam.searchBeginTime}
|
||||
AND #{auditParam.searchEndTime} >= update_time
|
||||
<if test="auditParam.loginName!=null">
|
||||
<if test="auditParam.loginName!=null and auditParam.loginName!=''">
|
||||
and login_name = #{auditParam.loginName}
|
||||
</if>
|
||||
<if test="auditParam.type!=null">
|
||||
<if test="auditParam.type!=null and auditParam.type!=''">
|
||||
and type = #{auditParam.type}
|
||||
</if>
|
||||
<if test="auditParam.operateType!=null">
|
||||
<if test="auditParam.operateType!=null and auditParam.operateType!=''">
|
||||
and operate_type = #{auditParam.operateType}
|
||||
</if>
|
||||
GROUP BY login_name, operate_type
|
||||
@@ -118,5 +118,35 @@
|
||||
FROM sys_user_log
|
||||
GROUP BY operate_type
|
||||
</select>
|
||||
<select id="queryExportLimit" resultType="com.njcn.system.excel.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>
|
||||
<if test="startTime != null and startTime != ''">
|
||||
and create_time >= #{startTime}
|
||||
</if>
|
||||
<if test="endTime != null and endTime != ''">
|
||||
and create_time <= #{endTime}
|
||||
</if>
|
||||
</where>
|
||||
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -2,4 +2,59 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.njcn.system.mapper.UserLogMapper">
|
||||
|
||||
<insert id="insertBatch">
|
||||
INSERT INTO sys_user_log (
|
||||
id,
|
||||
login_name,
|
||||
user_name,
|
||||
ip,
|
||||
operate,
|
||||
operate_type,
|
||||
result,
|
||||
fail_reason,
|
||||
LEVEL,
|
||||
type,
|
||||
service_name,
|
||||
state,
|
||||
create_by,
|
||||
create_time,
|
||||
update_by,
|
||||
update_time
|
||||
)
|
||||
values
|
||||
<foreach collection="list" item="item" separator="," index="index">
|
||||
( #{item.id},
|
||||
#{item.loginName},
|
||||
#{item.userName},
|
||||
#{item.ip},
|
||||
#{item.operate},
|
||||
#{item.operateType},
|
||||
#{item.result},
|
||||
#{item.failReason},
|
||||
#{item.level},
|
||||
#{item.type},
|
||||
#{item.serviceName},
|
||||
#{item.state},
|
||||
#{item.createBy},
|
||||
#{item.createTime},
|
||||
#{item.updateBy},
|
||||
#{item.updateTime} )
|
||||
</foreach>
|
||||
|
||||
</insert>
|
||||
<select id="ids" resultType="java.lang.String">
|
||||
select id from sys_user_log
|
||||
where id in
|
||||
<foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
|
||||
#{item}
|
||||
</foreach>
|
||||
</select>
|
||||
<select id="getMemoInfo" resultType="java.lang.Float">
|
||||
SELECT
|
||||
TRUNCATE (data_length / 1024 / 1024, 2) AS '数据容量(MB)'
|
||||
FROM
|
||||
information_schema.tables
|
||||
where table_schema = #{schema} and table_name="sys_user_log"
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
||||
@@ -45,4 +45,10 @@ public interface AuditService extends IService<UserLog> {
|
||||
* 清空6个月前的历史日志
|
||||
*/
|
||||
void clearHistoryLog();
|
||||
|
||||
/**
|
||||
* 获取表空间大小
|
||||
* @return
|
||||
*/
|
||||
Float getMemoInfo();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
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 cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.ExcelWriter;
|
||||
import com.alibaba.excel.support.ExcelTypeEnum;
|
||||
import com.alibaba.excel.write.metadata.WriteSheet;
|
||||
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@@ -13,9 +19,10 @@ import com.google.common.collect.Lists;
|
||||
import com.njcn.common.config.GeneralInfo;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.system.enums.AuditLogEnum;
|
||||
import com.njcn.system.excel.DemoDataListener;
|
||||
import com.njcn.system.excel.UserLogExcel;
|
||||
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;
|
||||
@@ -23,20 +30,18 @@ import com.njcn.system.pojo.vo.AuditLogVO;
|
||||
import com.njcn.system.pojo.vo.LogParamVO;
|
||||
import com.njcn.system.pojo.vo.ValuePO;
|
||||
import com.njcn.system.service.AuditService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.compress.utils.IOUtils;
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
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.io.*;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
@@ -50,40 +55,33 @@ import java.util.stream.Collectors;
|
||||
* @date: 2022/07/11 20:56
|
||||
*/
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@RequiredArgsConstructor
|
||||
public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implements AuditService {
|
||||
|
||||
private final AuditMapper auditMapper;
|
||||
|
||||
private final UserLogMapper userLogMapper;
|
||||
|
||||
@Value("${spring.datasource.druid.url}")
|
||||
private String urls;
|
||||
private final GeneralInfo generalInfo;
|
||||
|
||||
@Override
|
||||
public Page<AuditLogVO> getAuditLog(AuditParam auditParam) {
|
||||
auditParam.setSearchBeginTime(auditParam.getSearchBeginTime() + StrUtil.SPACE + "00:00:00");
|
||||
auditParam.setSearchEndTime(auditParam.getSearchEndTime() + StrUtil.SPACE + "23:59:59");
|
||||
List<AuditLogVO> auditLogVOS = new ArrayList<>();
|
||||
|
||||
Page<AuditLogVO> page = new Page<>();
|
||||
page.setSize(auditParam.getPageSize());
|
||||
page.setCurrent(auditParam.getPageNum());
|
||||
|
||||
//待分页数据总量
|
||||
int count = auditMapper.getCount(auditParam);
|
||||
page.setTotal(count);
|
||||
//分页总页数
|
||||
int pages = (int)Math.ceil(count*1.0/auditParam.getPageSize());
|
||||
page.setPages(pages);
|
||||
|
||||
auditParam.setPageNum(auditParam.getPageSize()*(auditParam.getPageNum()-1));
|
||||
List<UserLog> userLogs = auditMapper.selectAuditLog(auditParam);
|
||||
if (!CollectionUtils.isEmpty(userLogs)) {
|
||||
for (UserLog userLog: userLogs) {
|
||||
Page<UserLog> info = this.page(new Page<>(auditParam.getPageNum(), auditParam.getPageSize()), new LambdaQueryWrapper<UserLog>()
|
||||
.eq(StrUtil.isNotBlank(auditParam.getLoginName()), UserLog::getLoginName, auditParam.getLoginName())
|
||||
.eq(auditParam.getType() != null, UserLog::getLoginName, auditParam.getType())
|
||||
.eq(StrUtil.isNotBlank(auditParam.getOperateType()), UserLog::getLoginName, auditParam.getLoginName())
|
||||
.ge(StrUtil.isNotBlank(auditParam.getSearchBeginTime()), UserLog::getCreateTime, DateUtil.beginOfDay(DateUtil.parse(auditParam.getSearchBeginTime())))
|
||||
.le(StrUtil.isNotBlank(auditParam.getSearchEndTime()), UserLog::getCreateTime, DateUtil.endOfDay(DateUtil.parse(auditParam.getSearchEndTime())))
|
||||
);
|
||||
Page<AuditLogVO> page = BeanUtil.copyProperties(info, Page.class);
|
||||
if (CollUtil.isNotEmpty(info.getRecords())) {
|
||||
for (UserLog userLog : info.getRecords()) {
|
||||
AuditLogVO auditLogVO = new AuditLogVO();
|
||||
String updateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(userLog.getUpdateTime());
|
||||
auditLogVO.setTime(updateTime);
|
||||
if (userLog.getUserName()==null || userLog.getUserName().equals("")) {
|
||||
if (userLog.getUserName() == null || userLog.getUserName().equals("")) {
|
||||
auditLogVO.setUserName(userLog.getLoginName());
|
||||
} else {
|
||||
auditLogVO.setUserName(userLog.getUserName());
|
||||
@@ -91,21 +89,23 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
auditLogVO.setOperate(userLog.getOperate());
|
||||
StringBuilder describe = new StringBuilder();
|
||||
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) {
|
||||
if (userLog.getResult() == 1) {
|
||||
describe.append("成功");
|
||||
}
|
||||
if (userLog.getResult()==0) {
|
||||
if (userLog.getResult() == 0) {
|
||||
describe.append("失败").append(",失败原因为").append(userLog.getFailReason());
|
||||
}
|
||||
auditLogVO.setDescribe(describe.toString());
|
||||
auditLogVO.setType(userLog.getType() == 0 ? "业务事件" : "系统事件");
|
||||
auditLogVO.setResult(userLog.getResult() == 0 ? "失败" : "成功");
|
||||
auditLogVO.setIp(userLog.getIp());
|
||||
if (userLog.getLevel()==0) {
|
||||
if (userLog.getLevel() == 0) {
|
||||
auditLogVO.setLevel("普通");
|
||||
}if (userLog.getLevel()==1) {
|
||||
}
|
||||
if (userLog.getLevel() == 1) {
|
||||
auditLogVO.setLevel("中等");
|
||||
}if (userLog.getLevel()==2) {
|
||||
}
|
||||
if (userLog.getLevel() == 2) {
|
||||
auditLogVO.setLevel("严重");
|
||||
}
|
||||
auditLogVOS.add(auditLogVO);
|
||||
@@ -118,37 +118,61 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
|
||||
@Override
|
||||
public void logFileWriter() {
|
||||
LocalDate nowDate = LocalDate.now();
|
||||
LocalDate agoDate = nowDate.minusMonths(6).with(TemporalAdjusters.firstDayOfMonth());
|
||||
LocalDateTime nowDate = LocalDateTime.now();
|
||||
LocalDateTime 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");
|
||||
//结束时间
|
||||
String endTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH_mm_ss").format(nowDate);
|
||||
//文件名
|
||||
QueryWrapper<UserLog> aa = new QueryWrapper<>();
|
||||
aa.ge("sys_user_log.update_time", date);
|
||||
aa.le("sys_user_log.update_time", endTime);
|
||||
String nowTime = date + "至" + endTime;
|
||||
//必须放到循环外,否则会刷新流
|
||||
ExcelWriter excelWriter = EasyExcel.write(generalInfo.getBusinessTempPath() + "//" + nowTime + ExcelTypeEnum.CSV.getValue(), UserLogExcel.class)
|
||||
.excelType(ExcelTypeEnum.CSV)
|
||||
.build();
|
||||
|
||||
QueryWrapper<UserLogExcel> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.ge("sys_user_log.update_time",date);
|
||||
List<UserLogExcel> excelList = auditMapper.queryExportUser(queryWrapper);
|
||||
try {
|
||||
//记录总数:实际中需要根据查询条件进行统计即可:一共多少条
|
||||
int totalCount = this.count(aa);
|
||||
//每一个Sheet存放100w条数据
|
||||
Integer sheetDataRows = 1000000;
|
||||
//每次写入的数据量20w,每页查询20W
|
||||
Integer writeDataRows = 200000;
|
||||
//计算需要的Sheet总数量
|
||||
Integer sheetNum = totalCount % sheetDataRows == 0 ? (totalCount / sheetDataRows) : (totalCount / sheetDataRows + 1);
|
||||
//计算一般情况下每一个Sheet需要写入的次数(一般情况不包含最后一个sheet,因为最后一个sheet不确定会写入多少条数据)
|
||||
Integer oneSheetWriteCount = sheetDataRows / writeDataRows;
|
||||
//计算最后一个sheet需要写入的次数
|
||||
Integer lastSheetWriteCount = 0;
|
||||
if (totalCount > sheetDataRows) {
|
||||
lastSheetWriteCount = totalCount % sheetDataRows == 0 ? oneSheetWriteCount : (totalCount % sheetDataRows % writeDataRows == 0 ? (totalCount / sheetDataRows / writeDataRows) : (totalCount / sheetDataRows / writeDataRows + 1));
|
||||
} else {
|
||||
lastSheetWriteCount = totalCount % sheetDataRows == 0 ? oneSheetWriteCount : oneSheetWriteCount + 1;
|
||||
}
|
||||
|
||||
LocalDateTime nowDateTime = LocalDateTime.now();
|
||||
String nowTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss").format(nowDateTime);
|
||||
//开始分批查询分次写入
|
||||
for (int i = 0; i < sheetNum; i++) {
|
||||
|
||||
File filePath = new File(generalInfo.getBusinessTempPath());
|
||||
filePath.mkdirs();
|
||||
File file = new File(filePath.getPath() + File.separator + nowTime +" sys_user_log.xlsx");
|
||||
// ExcelUtil.exportExcelWithTargetFile(file, UserLogExcel.class, excelList);
|
||||
//循环写入次数: j的自增条件是当不是最后一个Sheet的时候写入次数为正常的每个Sheet写入的次数,如果是最后一个就需要使用计算的次数lastSheetWriteCount
|
||||
for (int j = 0; j < (i != sheetNum - 1 ? oneSheetWriteCount : lastSheetWriteCount); j++) {
|
||||
//分页查询一次20w
|
||||
//查询分页列表---按照自己的业务查列表,分页这个一定要使用这个:page1.getPageNum(),page1.getPageSize()!!!
|
||||
Page<UserLogExcel> userLogExcelPage = auditMapper.queryExportLimit(new Page<>(j + 1 + oneSheetWriteCount * i, writeDataRows), date, endTime);
|
||||
List<UserLogExcel> list = userLogExcelPage.getRecords();
|
||||
//写入到excel:
|
||||
WriteSheet writeSheet = EasyExcel.writerSheet(i, "Sheet" + (i + 1)).head(UserLogExcel.class)
|
||||
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
|
||||
excelWriter.write(list, writeSheet);
|
||||
}
|
||||
}
|
||||
|
||||
//备份为txt
|
||||
// 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();
|
||||
// }
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
excelWriter.finish();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -173,48 +197,22 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
if (!newFile.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(newFile);
|
||||
//将Excel文件转成对象集合
|
||||
List<UserLogExcel> excelList = new ExcelImportService().importExcelByIs(in, UserLogExcel.class, params, false).getList();
|
||||
if (excelList.get(0).getId().isEmpty()) {
|
||||
throw new BusinessException(AuditLogEnum.LOG_EXCEPTION);
|
||||
}
|
||||
List<String> oldIds = excelList.stream().map(UserLogExcel::getId).collect(Collectors.toList());
|
||||
//查找该备份文件是否已存在
|
||||
List<UserLog> logList = userLogMapper.selectBatchIds(oldIds);
|
||||
if (CollectionUtils.isEmpty(logList)) {
|
||||
List<UserLog> userLogs = new ArrayList<>();
|
||||
for (UserLogExcel excel: excelList) {
|
||||
UserLog userLog = new UserLog();
|
||||
BeanUtil.copyProperties(excel,userLog);
|
||||
userLogs.add(userLog);
|
||||
}
|
||||
this.saveBatch(userLogs);
|
||||
} else {
|
||||
List<String> isIds = logList.stream().map(UserLog::getId).collect(Collectors.toList());
|
||||
List<UserLogExcel> needSaveUserLogData = excelList.stream().filter(userLogExcelTemp -> !isIds.contains(userLogExcelTemp.getId())).collect(Collectors.toList());
|
||||
if (!CollectionUtils.isEmpty(needSaveUserLogData)) {
|
||||
List<UserLog> userLogs = new ArrayList<>();
|
||||
for (UserLogExcel needSave: needSaveUserLogData) {
|
||||
UserLog userLog = new UserLog();
|
||||
BeanUtil.copyProperties(needSave,userLog);
|
||||
userLogs.add(userLog);
|
||||
}
|
||||
this.saveBatch(userLogs);
|
||||
// this.saveOrUpdateBatch(userLogs);
|
||||
}
|
||||
String name = newFile.getName();
|
||||
String[] split = name.split(ExcelTypeEnum.CSV.getValue());
|
||||
|
||||
String[] splitTime = split[0].split("至");
|
||||
if (StrUtil.isBlank(splitTime[0]) || StrUtil.isBlank(splitTime[1])) {
|
||||
throw new BusinessException(AuditLogEnum.LOG_EXCEPTIONTIME);
|
||||
}
|
||||
LambdaQueryWrapper<UserLog> le = new LambdaQueryWrapper<UserLog>()
|
||||
.ge(UserLog::getCreateTime, DateUtil.beginOfDay(DateUtil.parse(splitTime[0])))
|
||||
.le(UserLog::getCreateTime, splitTime[1].replace("_", ":"));
|
||||
this.remove(le);
|
||||
EasyExcel.read(newFile, UserLogExcel.class, new DemoDataListener(this.getBaseMapper()))
|
||||
.excelType(ExcelTypeEnum.CSV).doReadAll();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -222,19 +220,6 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
} 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);
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
@@ -249,7 +234,7 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
List<AuditLogCusVO> auditLogCusVOS = auditMapper.selectCensusAuditLog(auditParam);
|
||||
page.setTotal(auditLogCusVOS.size());
|
||||
//分页总页数
|
||||
int pages = (int)Math.ceil(page.getTotal()*1.0/auditParam.getPageSize());
|
||||
int pages = (int) Math.ceil(page.getTotal() * 1.0 / auditParam.getPageSize());
|
||||
page.setPages(pages);
|
||||
|
||||
if (!CollectionUtils.isEmpty(auditLogCusVOS)) {
|
||||
@@ -268,10 +253,16 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
String date = agoDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
|
||||
|
||||
QueryWrapper<UserLog> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.lt("sys_user_log.update_time",date);
|
||||
queryWrapper.lt("sys_user_log.create_time", date);
|
||||
auditMapper.deleteByTime(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getMemoInfo() {
|
||||
String schema = urls.substring(urls.lastIndexOf("/")+1,urls.lastIndexOf("?"));
|
||||
return this.baseMapper.getMemoInfo(schema);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LogParamVO saveLogParam() {
|
||||
LogParamVO logParamVO = new LogParamVO();
|
||||
@@ -279,15 +270,15 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
List<String> operateType = auditMapper.selectOperateType();
|
||||
List<ValuePO> loginNameList = new ArrayList<>();
|
||||
List<ValuePO> operateTypeList = new ArrayList<>();
|
||||
for (int i = 0; i<loginName.size(); i++) {
|
||||
for (int i = 0; i < loginName.size(); i++) {
|
||||
ValuePO valuePO = new ValuePO();
|
||||
valuePO.setName("选项"+(i+1));
|
||||
valuePO.setName("选项" + (i + 1));
|
||||
valuePO.setValue(loginName.get(i));
|
||||
loginNameList.add(valuePO);
|
||||
}
|
||||
for (int i = 0; i<operateType.size(); i++) {
|
||||
for (int i = 0; i < operateType.size(); i++) {
|
||||
ValuePO valuePO = new ValuePO();
|
||||
valuePO.setName("选项"+(i+1));
|
||||
valuePO.setName("选项" + (i + 1));
|
||||
valuePO.setValue(operateType.get(i));
|
||||
operateTypeList.add(valuePO);
|
||||
}
|
||||
@@ -296,28 +287,22 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
||||
return logParamVO;
|
||||
}
|
||||
|
||||
/**返回一个最新修改日期的文件*/
|
||||
/**
|
||||
* 返回一个最新修改日期的文件
|
||||
*/
|
||||
public File getLastFile() {
|
||||
File parentFile = new File(generalInfo.getBusinessTempPath());
|
||||
Date date = FileUtil.lastModifiedTime(parentFile);
|
||||
System.out.println(">>>>>>>>>>当前文件夹最后修改时间>>>>>>>>>"+date);
|
||||
// Date date = FileUtil.lastModifiedTime(parentFile);
|
||||
// System.out.println(">>>>>>>>>>当前文件夹最后修改时间>>>>>>>>>" + date);
|
||||
|
||||
//文件夹下的所有子文件数组
|
||||
File[] files = parentFile.listFiles();
|
||||
List<Date> list = Arrays.stream(files).map(tempFile -> FileUtil.lastModifiedTime(tempFile)).collect(Collectors.toList());
|
||||
//Date集合排序
|
||||
Collections.sort(list, new Comparator<Date>() {
|
||||
@Override
|
||||
public int compare(Date o1, Date o2) {
|
||||
return o2.compareTo(o1);//倒序排序
|
||||
// return o1.compareTo(o2);//正序排序
|
||||
}
|
||||
});
|
||||
Collections.sort(list, Comparator.reverseOrder());
|
||||
File newFile = Arrays.stream(files).filter(tempFile -> FileUtil.lastModifiedTime(tempFile).equals(list.get(0))).collect(Collectors.toList()).get(0);
|
||||
|
||||
return newFile;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -13,11 +13,9 @@ import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -85,8 +83,7 @@ public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> impleme
|
||||
@Override
|
||||
public List<Config> getList() {
|
||||
List<Config> list = this.baseMapper.getList();
|
||||
List<Config> filterList = list.stream().filter(t -> t.getLogTime() > 0 && t.getLogSize().compareTo(new BigDecimal(0))==1).collect(Collectors.toList());
|
||||
return filterList;
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user