|
|
|
|
@@ -5,12 +5,15 @@ 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.date.StopWatch;
|
|
|
|
|
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.context.AnalysisContext;
|
|
|
|
|
import com.alibaba.excel.event.AnalysisEventListener;
|
|
|
|
|
import com.alibaba.excel.support.ExcelTypeEnum;
|
|
|
|
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
|
|
|
|
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
|
|
|
|
@@ -43,6 +46,7 @@ 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.transaction.annotation.Transactional;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
|
|
|
|
@@ -79,34 +83,34 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
|
|
|
|
|
Page<UserLog> info = this.page(new Page<>(auditParam.getPageNum(), auditParam.getPageSize()), new LambdaQueryWrapper<UserLog>()
|
|
|
|
|
.like(StrUtil.isNotBlank(auditParam.getLoginName()), UserLog::getLoginName, auditParam.getLoginName())
|
|
|
|
|
.ne(UserLog::getLoginName,"unknown user")
|
|
|
|
|
.ne(UserLog::getLoginName,"")
|
|
|
|
|
.ne(UserLog::getOperate,"unknown user")
|
|
|
|
|
.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 ->
|
|
|
|
|
.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")
|
|
|
|
|
.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)
|
|
|
|
|
.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())) {
|
|
|
|
|
@@ -164,12 +168,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 file = new File(generalInfo.getBusinessTempPath() + File.separator + OssPath.LOGBAK);
|
|
|
|
|
if (!file.exists() && !file.isDirectory()) {
|
|
|
|
|
file.mkdir();
|
|
|
|
|
}
|
|
|
|
|
//必须放到循环外,否则会刷新流
|
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(generalInfo.getBusinessTempPath() + "/"+ OssPath.LOGBAK + nowTime + ExcelTypeEnum.XLSX.getValue(), UserLogExcel.class)
|
|
|
|
|
ExcelWriter excelWriter = EasyExcel.write(generalInfo.getBusinessTempPath() + File.separator + OssPath.LOGBAK + nowTime + ExcelTypeEnum.XLSX.getValue(), UserLogExcel.class)
|
|
|
|
|
// .excelType(ExcelTypeEnum.CSV)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
@@ -207,6 +211,7 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
excelWriter.write(list, writeSheet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
redisUtil.delete("recoverLogFile");
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
@@ -233,6 +238,7 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = {Exception.class})
|
|
|
|
|
public void recoverLogFile() {
|
|
|
|
|
String logFileWriter = redisUtil.getStringByKey("recoverLogFile");
|
|
|
|
|
if (StrUtil.isNotBlank(logFileWriter) || ObjectUtil.equals(logFileWriter, "1")) {
|
|
|
|
|
@@ -246,20 +252,23 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
FileInputStream in = null;
|
|
|
|
|
try {
|
|
|
|
|
in = new FileInputStream(newFile);
|
|
|
|
|
String name = newFile.getName();
|
|
|
|
|
String[] split = name.split(ExcelTypeEnum.XLSX.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 DataListener(this.getBaseMapper()))
|
|
|
|
|
.excelType(ExcelTypeEnum.XLSX).doReadAll();
|
|
|
|
|
Set<String> logIndex = new HashSet<>(this.baseMapper.getAllIndex());
|
|
|
|
|
List<UserLog> userLogs = new ArrayList<>();
|
|
|
|
|
EasyExcel.read(newFile.getPath(), UserLog.class, new AnalysisEventListener<UserLog>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void invoke(UserLog userLog, AnalysisContext analysisContext) {
|
|
|
|
|
// 将读取到的每一行存入reportDetails集合中
|
|
|
|
|
if (!logIndex.contains(userLog.getId())) {
|
|
|
|
|
userLogs.add(userLog);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
|
|
|
|
}
|
|
|
|
|
}).sheet().doRead();
|
|
|
|
|
// 执行批量插入
|
|
|
|
|
saveLogsBatch(userLogs, 1500);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
redisUtil.delete("recoverLogFile");
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
@@ -271,6 +280,41 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
* 批量插入日志
|
|
|
|
|
* @author hongawen
|
|
|
|
|
* @date 2023/5/30 15:56
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public boolean saveLogsBatch(Collection<UserLog> userLogs, int batchSize) {
|
|
|
|
|
try {
|
|
|
|
|
int size = userLogs.size();
|
|
|
|
|
int idxLimit = Math.min(batchSize, size);
|
|
|
|
|
List<UserLog> userLogTemp = new ArrayList<>(userLogs);
|
|
|
|
|
//保存单批提交的数据集合
|
|
|
|
|
if (idxLimit == batchSize) {
|
|
|
|
|
int times = size / idxLimit + 1;
|
|
|
|
|
for (int i = 1; i <= times; i++) {
|
|
|
|
|
if (size > idxLimit) {
|
|
|
|
|
List<UserLog> temp = userLogTemp.subList(0, idxLimit);
|
|
|
|
|
this.baseMapper.insertBatchSomeColumn(temp);
|
|
|
|
|
temp.clear();
|
|
|
|
|
size = size - idxLimit;
|
|
|
|
|
} else {
|
|
|
|
|
this.baseMapper.insertBatchSomeColumn(userLogTemp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.baseMapper.insertBatchSomeColumn(userLogTemp);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("saveBatch fail", e);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Page<AuditLogCusVO> censusAuditLog(AuditParam auditParam) {
|
|
|
|
|
//待分页数据总量
|
|
|
|
|
@@ -314,7 +358,7 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
for (User datum : data) {
|
|
|
|
|
vo = BeanUtil.copyProperties(datum, OnlineUsersVO.class);
|
|
|
|
|
Long integer = time.get(datum.getId());
|
|
|
|
|
DateTime date = DateUtil.date(integer*1000);
|
|
|
|
|
DateTime date = DateUtil.date(integer * 1000);
|
|
|
|
|
vo.setTime(date.toDateStr());
|
|
|
|
|
info.add(vo);
|
|
|
|
|
}
|
|
|
|
|
@@ -350,7 +394,7 @@ public class AuditServiceImpl extends ServiceImpl<UserLogMapper, UserLog> implem
|
|
|
|
|
* 返回一个最新修改日期的文件
|
|
|
|
|
*/
|
|
|
|
|
public File getLastFile() {
|
|
|
|
|
File parentFile = new File(generalInfo.getBusinessTempPath()+"/"+ OssPath.LOGBAK);
|
|
|
|
|
File parentFile = new File(generalInfo.getBusinessTempPath() + File.separator + OssPath.LOGBAK);
|
|
|
|
|
|
|
|
|
|
//文件夹下的所有子文件数组
|
|
|
|
|
File[] files = parentFile.listFiles();
|
|
|
|
|
|