|
|
|
|
@ -2,19 +2,22 @@ |
|
|
|
|
package org.springblade.modules.system.service.impl; |
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
|
import org.springblade.common.cache.UserCache; |
|
|
|
|
import org.springblade.core.log.model.LogApi; |
|
|
|
|
import org.springblade.modules.system.dto.LogDTO; |
|
|
|
|
import org.springblade.modules.system.entity.User; |
|
|
|
|
import org.springblade.modules.system.mapper.LogApiMapper; |
|
|
|
|
import org.springblade.modules.system.service.ILogApiService; |
|
|
|
|
import org.springblade.modules.system.vo.LogRecord; |
|
|
|
|
import org.springblade.modules.system.vo.LogVO; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
|
import java.text.ParseException; |
|
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
|
import java.util.Comparator; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.TreeMap; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger; |
|
|
|
|
import java.util.concurrent.atomic.AtomicReference; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -25,19 +28,112 @@ import java.util.stream.Collectors; |
|
|
|
|
@Service |
|
|
|
|
public class LogApiServiceImpl extends ServiceImpl<LogApiMapper, LogApi> implements ILogApiService { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Map<String, List<LogVO>> logOnList(LogDTO log) { |
|
|
|
|
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
|
|
|
public List<LogRecord> logOnList(LogDTO log) { |
|
|
|
|
|
|
|
|
|
List<LogRecord> logRecords = new LinkedList<>(); |
|
|
|
|
LogRecord logRecord = new LogRecord(); |
|
|
|
|
|
|
|
|
|
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
|
|
|
// yyyy-MM年月日志标识
|
|
|
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); |
|
|
|
|
|
|
|
|
|
// 按照月份查询日志所有数据
|
|
|
|
|
List<LogVO> logs = baseMapper.logOnList(log); |
|
|
|
|
|
|
|
|
|
logs.forEach(logVO -> { |
|
|
|
|
logVO.setTime(formatter2.format(logVO.getCreateTime())); |
|
|
|
|
logVO.setTitle(logVO.getCreateBy() + ":" + logVO.getTitle()); |
|
|
|
|
}); |
|
|
|
|
// 记录年月
|
|
|
|
|
AtomicReference<String> yearMonth = new AtomicReference<>(null); |
|
|
|
|
// logs遍历计数器
|
|
|
|
|
AtomicInteger count = new AtomicInteger(0); |
|
|
|
|
// logs总长度
|
|
|
|
|
int totalLogs = logs.size(); |
|
|
|
|
|
|
|
|
|
// 遍历日志数据
|
|
|
|
|
// logs.forEach(logInfo -> {
|
|
|
|
|
for (LogVO logInfo:logs){ |
|
|
|
|
|
|
|
|
|
count.incrementAndGet(); |
|
|
|
|
// 当前年月
|
|
|
|
|
String currentYearMonth = formatter.format(logInfo.getCreateTime()); |
|
|
|
|
|
|
|
|
|
// 记录年月是否为空,空则插入
|
|
|
|
|
if (yearMonth.get() == null) { |
|
|
|
|
// 当前年月赋值给记录年月
|
|
|
|
|
yearMonth.set(currentYearMonth); |
|
|
|
|
|
|
|
|
|
// logRecord的当前年月是否为空,空则插入当前年月
|
|
|
|
|
if(logRecord.getTime() == null){ |
|
|
|
|
logRecord.setTime(currentYearMonth); |
|
|
|
|
} |
|
|
|
|
// 将该条数据插入logs
|
|
|
|
|
if(logRecord.getLogs() == null){ |
|
|
|
|
logRecord.setLogs(new ArrayList<>()); |
|
|
|
|
} |
|
|
|
|
logRecord.getLogs().add(logInfo); |
|
|
|
|
} else { |
|
|
|
|
// 当前年月等于记录年月
|
|
|
|
|
if (yearMonth.get().equals(currentYearMonth)) { |
|
|
|
|
// 将该条数据插入logs
|
|
|
|
|
logRecord.getLogs().add(logInfo); |
|
|
|
|
|
|
|
|
|
}else{ // 当前年月不等于记录年月
|
|
|
|
|
// 将当前年月赋值给记录年月
|
|
|
|
|
yearMonth.set(currentYearMonth); |
|
|
|
|
// 将上一组数据存入
|
|
|
|
|
for (LogVO logVO : logRecord.getLogs()){ |
|
|
|
|
User user = UserCache.getUser(logVO.getTenantId(), logVO.getCreateBy()); |
|
|
|
|
if(user != null){ |
|
|
|
|
logVO.setRealName(user.getRealName()); |
|
|
|
|
logVO.setTitle(user.getRealName() + logVO.getTitle()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// formatter1.format(logVO.getCreateTime()).substring(8);
|
|
|
|
|
String substring = formatter1.format(logVO.getCreateTime()).substring(8); |
|
|
|
|
logVO.setTime(substring.substring(0,2) + "日" + substring.substring(2)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
logRecords.add(logRecord); |
|
|
|
|
|
|
|
|
|
// 一组年月的日志已插入,logRecord对象创建一个新的
|
|
|
|
|
logRecord = new LogRecord(); |
|
|
|
|
// logRecord的当前年月是否为空,空则插入当前年月
|
|
|
|
|
logRecord.setTime(currentYearMonth); |
|
|
|
|
|
|
|
|
|
// 将该条数据插入logs
|
|
|
|
|
logRecord.setLogs(new LinkedList<>(Collections.singletonList(logInfo))); |
|
|
|
|
|
|
|
|
|
// // 将logs内容创建新对象
|
|
|
|
|
// logRecord.setLogs(new LinkedList<>());
|
|
|
|
|
// // 将该条数据插入logs
|
|
|
|
|
// logRecord.getLogs().add(logInfo);
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (count.get() == totalLogs) { |
|
|
|
|
for (LogVO logVO : logRecord.getLogs()){ |
|
|
|
|
User user = UserCache.getUser(logVO.getTenantId(), logVO.getCreateBy()); |
|
|
|
|
if(user != null){ |
|
|
|
|
logVO.setRealName(user.getRealName()); |
|
|
|
|
logVO.setTitle(user.getRealName() + logVO.getTitle()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String substring = formatter1.format(logVO.getCreateTime()).substring(8); |
|
|
|
|
logVO.setTime(substring.substring(0,2) + "日" + substring.substring(2)); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
// 最后一次执行的逻辑,将logRecord保存
|
|
|
|
|
logRecords.add(logRecord); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// logs.forEach(logVO -> {
|
|
|
|
|
// logVO.setTime(formatter2.format(logVO.getCreateTime()));
|
|
|
|
|
// logVO.setTitle(logVO.getCreateBy() + ":" + logVO.getTitle());
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
Map<String, List<LogVO>> groupedByMonth = logs.stream().collect(Collectors.groupingBy(date -> date.getTime().substring(0, 7))); |
|
|
|
|
// Map<String, List<LogVO>> groupedByMonth = logs.stream().collect(Collectors.groupingBy(date -> date.getTime().substring(0, 7)));
|
|
|
|
|
|
|
|
|
|
return groupedByMonth; |
|
|
|
|
return logRecords; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|