|
|
|
|
@@ -3,10 +3,12 @@ package com.njcn.system.service.impl;
|
|
|
|
|
import cn.afterturn.easypoi.exception.excel.ExcelImportException;
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
|
|
import com.alibaba.excel.ExcelWriter;
|
|
|
|
|
import com.alibaba.excel.support.ExcelTypeEnum;
|
|
|
|
|
@@ -16,8 +18,12 @@ 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;
|
|
|
|
|
import com.nimbusds.jose.JWSObject;
|
|
|
|
|
import com.njcn.common.config.GeneralInfo;
|
|
|
|
|
import com.njcn.common.pojo.constant.SecurityConstants;
|
|
|
|
|
import com.njcn.common.pojo.dto.UserTokenInfo;
|
|
|
|
|
import com.njcn.common.pojo.exception.BusinessException;
|
|
|
|
|
import com.njcn.oss.constant.OssPath;
|
|
|
|
|
import com.njcn.redis.utils.RedisUtil;
|
|
|
|
|
import com.njcn.system.enums.AuditLogEnum;
|
|
|
|
|
import com.njcn.system.excel.DataListener;
|
|
|
|
|
@@ -26,11 +32,10 @@ import com.njcn.system.mapper.AuditMapper;
|
|
|
|
|
import com.njcn.system.mapper.UserLogMapper;
|
|
|
|
|
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.pojo.vo.LogParamVO;
|
|
|
|
|
import com.njcn.system.pojo.vo.ValuePO;
|
|
|
|
|
import com.njcn.system.pojo.vo.*;
|
|
|
|
|
import com.njcn.system.service.AuditService;
|
|
|
|
|
import com.njcn.user.api.UserFeignClient;
|
|
|
|
|
import com.njcn.user.pojo.po.User;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import org.apache.commons.compress.utils.IOUtils;
|
|
|
|
|
import org.apache.commons.fileupload.FileItem;
|
|
|
|
|
@@ -42,6 +47,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
@@ -65,16 +71,42 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
private final GeneralInfo generalInfo;
|
|
|
|
|
|
|
|
|
|
private final RedisUtil redisUtil;
|
|
|
|
|
private final UserFeignClient userFeignClient;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Page<AuditLogVO> getAuditLog(AuditParam auditParam) {
|
|
|
|
|
List<AuditLogVO> auditLogVOS = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
Page<UserLog> info = this.page(new Page<>(auditParam.getPageNum(), auditParam.getPageSize()), new LambdaQueryWrapper<UserLog>()
|
|
|
|
|
.eq(StrUtil.isNotBlank(auditParam.getLoginName()), UserLog::getLoginName, auditParam.getLoginName())
|
|
|
|
|
.like(StrUtil.isNotBlank(auditParam.getLoginName()), UserLog::getLoginName, auditParam.getLoginName())
|
|
|
|
|
.ne(UserLog::getLoginName,"unknown user")
|
|
|
|
|
.ne(UserLog::getLoginName,"")
|
|
|
|
|
.ne(UserLog::getOperate,"unknown user")
|
|
|
|
|
.eq(auditParam.getType() != null, UserLog::getType, auditParam.getType())
|
|
|
|
|
.eq(StrUtil.isNotBlank(auditParam.getOperateType()), UserLog::getOperateType, auditParam.getOperateType())
|
|
|
|
|
.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())))
|
|
|
|
|
.or(StrUtil.isNotBlank(auditParam.getLoginName()),qw ->
|
|
|
|
|
qw.like(StrUtil.isNotBlank(auditParam.getLoginName()), UserLog::getIp, auditParam.getLoginName())
|
|
|
|
|
.ne(UserLog::getLoginName,"unknown user")
|
|
|
|
|
.ne(UserLog::getLoginName,"")
|
|
|
|
|
.ne(UserLog::getOperate,"unknown user")
|
|
|
|
|
.eq(auditParam.getType() != null, UserLog::getType, auditParam.getType())
|
|
|
|
|
.eq(StrUtil.isNotBlank(auditParam.getOperateType()), UserLog::getOperateType, auditParam.getOperateType())
|
|
|
|
|
.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())))
|
|
|
|
|
)
|
|
|
|
|
.or(StrUtil.isNotBlank(auditParam.getLoginName()),qw ->
|
|
|
|
|
qw.like(StrUtil.isNotBlank(auditParam.getLoginName()), UserLog::getCreateTime, auditParam.getLoginName())
|
|
|
|
|
.ne(UserLog::getLoginName,"unknown user")
|
|
|
|
|
.ne(UserLog::getLoginName,"")
|
|
|
|
|
.ne(UserLog::getOperate,"unknown user")
|
|
|
|
|
.eq(auditParam.getType() != null, UserLog::getType, auditParam.getType())
|
|
|
|
|
.eq(StrUtil.isNotBlank(auditParam.getOperateType()), UserLog::getOperateType, auditParam.getOperateType())
|
|
|
|
|
.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())))
|
|
|
|
|
)
|
|
|
|
|
.orderByDesc( UserLog::getCreateTime)
|
|
|
|
|
);
|
|
|
|
|
Page<AuditLogVO> page = BeanUtil.copyProperties(info, Page.class);
|
|
|
|
|
if (CollUtil.isNotEmpty(info.getRecords())) {
|
|
|
|
|
@@ -82,11 +114,7 @@ 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);
|
|
|
|
|
if (userLog.getUserName() == null || userLog.getUserName().equals("")) {
|
|
|
|
|
auditLogVO.setUserName(userLog.getLoginName());
|
|
|
|
|
} else {
|
|
|
|
|
auditLogVO.setUserName(userLog.getUserName());
|
|
|
|
|
}
|
|
|
|
|
auditLogVO.setUserName(userLog.getLoginName());
|
|
|
|
|
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(",结果为");
|
|
|
|
|
@@ -121,10 +149,10 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
public void logFileWriter() {
|
|
|
|
|
//限制多次点击
|
|
|
|
|
String logFileWriter = redisUtil.getStringByKey("logFileWriter");
|
|
|
|
|
if(StrUtil.isNotBlank(logFileWriter)|| ObjectUtil.equals(logFileWriter,"1")){
|
|
|
|
|
if (StrUtil.isNotBlank(logFileWriter) || ObjectUtil.equals(logFileWriter, "1")) {
|
|
|
|
|
throw new BusinessException(AuditLogEnum.MULTIPLE_CLICKS_LOGFILEWRITER);
|
|
|
|
|
}
|
|
|
|
|
redisUtil.saveByKey("logFileWriter","1");
|
|
|
|
|
redisUtil.saveByKey("logFileWriter", "1");
|
|
|
|
|
LocalDateTime nowDate = LocalDateTime.now();
|
|
|
|
|
LocalDateTime agoDate = nowDate.minusMonths(6).with(TemporalAdjusters.firstDayOfMonth());
|
|
|
|
|
String date = agoDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
|
|
|
|
|
@@ -136,8 +164,12 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
aa.ge("sys_user_log.update_time", date);
|
|
|
|
|
aa.le("sys_user_log.update_time", endTime);
|
|
|
|
|
String nowTime = date + "至" + endTime;
|
|
|
|
|
File file=new File(generalInfo.getBusinessTempPath() + "/" + OssPath.LOGBAK);
|
|
|
|
|
if(!file.exists() && !file .isDirectory()){
|
|
|
|
|
file.mkdir();
|
|
|
|
|
}
|
|
|
|
|
//必须放到循环外,否则会刷新流
|
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(generalInfo.getBusinessTempPath() + "//" + nowTime + ExcelTypeEnum.XLSX.getValue(), UserLogExcel.class)
|
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(generalInfo.getBusinessTempPath() + "/"+ OssPath.LOGBAK + nowTime + ExcelTypeEnum.XLSX.getValue(), UserLogExcel.class)
|
|
|
|
|
// .excelType(ExcelTypeEnum.CSV)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
@@ -175,7 +207,6 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
excelWriter.write(list, writeSheet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
redisUtil.delete("recoverLogFile");
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
@@ -204,10 +235,10 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
@Override
|
|
|
|
|
public void recoverLogFile() {
|
|
|
|
|
String logFileWriter = redisUtil.getStringByKey("recoverLogFile");
|
|
|
|
|
if(StrUtil.isNotBlank(logFileWriter)|| ObjectUtil.equals(logFileWriter,"1")){
|
|
|
|
|
if (StrUtil.isNotBlank(logFileWriter) || ObjectUtil.equals(logFileWriter, "1")) {
|
|
|
|
|
throw new BusinessException(AuditLogEnum.MULTIPLE_CLICKS_LOGFILEWRITER);
|
|
|
|
|
}
|
|
|
|
|
redisUtil.saveByKey("recoverLogFile","1");
|
|
|
|
|
redisUtil.saveByKey("recoverLogFile", "1");
|
|
|
|
|
File newFile = getLastFile();
|
|
|
|
|
if (FileUtil.isEmpty(newFile)) {
|
|
|
|
|
throw new BusinessException(AuditLogEnum.NOT_FIND_FILE);
|
|
|
|
|
@@ -264,6 +295,33 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
return this.baseMapper.getMemoInfo(schema);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<OnlineUsersVO> getOnlineUsers() throws ParseException {
|
|
|
|
|
List<OnlineUsersVO> info = new ArrayList<>();
|
|
|
|
|
List<String> ids = new ArrayList<>();
|
|
|
|
|
Map<String, Long> time = new HashMap<>();
|
|
|
|
|
List<UserTokenInfo> listAllValues = (List<UserTokenInfo>) redisUtil.getLikeListAllValues(SecurityConstants.TOKEN_ONLINE_PREFIX);
|
|
|
|
|
for (UserTokenInfo listAllValue : listAllValues) {
|
|
|
|
|
String payload = StrUtil.toString(JWSObject.parse(listAllValue.getRefreshToken()).getPayload());
|
|
|
|
|
cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(payload);
|
|
|
|
|
time.put(jsonObject.getStr(SecurityConstants.USER_INDEX_KEY), Long.valueOf(jsonObject.getStr("exp")));
|
|
|
|
|
ids.add(jsonObject.getStr(SecurityConstants.USER_INDEX_KEY));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<User> data = userFeignClient.getUserByIdList(ids).getData();
|
|
|
|
|
OnlineUsersVO vo;
|
|
|
|
|
if (CollUtil.isNotEmpty(data)) {
|
|
|
|
|
for (User datum : data) {
|
|
|
|
|
vo = BeanUtil.copyProperties(datum, OnlineUsersVO.class);
|
|
|
|
|
Long integer = time.get(datum.getId());
|
|
|
|
|
DateTime date = DateUtil.date(integer*1000);
|
|
|
|
|
vo.setTime(date.toDateStr());
|
|
|
|
|
info.add(vo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public LogParamVO saveLogParam() {
|
|
|
|
|
LogParamVO logParamVO = new LogParamVO();
|
|
|
|
|
@@ -292,7 +350,7 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
* 返回一个最新修改日期的文件
|
|
|
|
|
*/
|
|
|
|
|
public File getLastFile() {
|
|
|
|
|
File parentFile = new File(generalInfo.getBusinessTempPath());
|
|
|
|
|
File parentFile = new File(generalInfo.getBusinessTempPath()+"/"+ OssPath.LOGBAK);
|
|
|
|
|
|
|
|
|
|
//文件夹下的所有子文件数组
|
|
|
|
|
File[] files = parentFile.listFiles();
|
|
|
|
|
|