refactor(接口命名): 统一接口命名规范并重构相关实现
将接口命名统一调整为以I开头,并重构相关实现类和服务调用 重构审计日志和网关路由服务接口,优化代码结构 删除旧接口文件,更新依赖接口的类
This commit is contained in:
+3
-3
@@ -3,7 +3,7 @@ package cn.novalon.manage.sys.audit.controller;
|
||||
import cn.novalon.manage.sys.audit.domain.AuditLog;
|
||||
import cn.novalon.manage.sys.audit.dto.AuditLogQueryRequest;
|
||||
import cn.novalon.manage.sys.audit.dto.AuditLogStatistics;
|
||||
import cn.novalon.manage.sys.audit.service.AuditLogService;
|
||||
import cn.novalon.manage.sys.audit.service.IAuditLogService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@@ -29,9 +29,9 @@ import java.time.LocalDateTime;
|
||||
@Tag(name = "审计日志", description = "审计日志查询和统计接口")
|
||||
public class AuditLogController {
|
||||
|
||||
private final AuditLogService auditLogService;
|
||||
private final IAuditLogService auditLogService;
|
||||
|
||||
public AuditLogController(AuditLogService auditLogService) {
|
||||
public AuditLogController(IAuditLogService auditLogService) {
|
||||
this.auditLogService = auditLogService;
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
package cn.novalon.manage.sys.audit.scheduler;
|
||||
|
||||
import cn.novalon.manage.sys.audit.service.AuditLogArchiveService;
|
||||
import cn.novalon.manage.sys.audit.service.IAuditLogArchiveService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
@@ -21,9 +21,9 @@ public class AuditLogArchiveScheduler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AuditLogArchiveScheduler.class);
|
||||
|
||||
private final AuditLogArchiveService auditLogArchiveService;
|
||||
private final IAuditLogArchiveService auditLogArchiveService;
|
||||
|
||||
public AuditLogArchiveScheduler(AuditLogArchiveService auditLogArchiveService) {
|
||||
public AuditLogArchiveScheduler(IAuditLogArchiveService auditLogArchiveService) {
|
||||
this.auditLogArchiveService = auditLogArchiveService;
|
||||
}
|
||||
|
||||
|
||||
-95
@@ -1,95 +0,0 @@
|
||||
package cn.novalon.manage.sys.audit.service;
|
||||
|
||||
import cn.novalon.manage.sys.audit.domain.AuditLog;
|
||||
import cn.novalon.manage.sys.audit.domain.AuditLogArchive;
|
||||
import cn.novalon.manage.sys.audit.repository.IAuditLogArchiveRepository;
|
||||
import cn.novalon.manage.sys.audit.repository.IAuditLogRepository;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 审计日志归档服务
|
||||
*
|
||||
* 文件定义:封装审计日志归档的业务逻辑
|
||||
* 涉及业务:审计日志的归档、查询、清理等操作
|
||||
* 算法:定期将历史审计日志移动到归档表
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-04-01
|
||||
*/
|
||||
@Service
|
||||
public class AuditLogArchiveService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AuditLogArchiveService.class);
|
||||
|
||||
private final IAuditLogRepository auditLogRepository;
|
||||
private final IAuditLogArchiveRepository auditLogArchiveRepository;
|
||||
|
||||
public AuditLogArchiveService(IAuditLogRepository auditLogRepository,
|
||||
IAuditLogArchiveRepository auditLogArchiveRepository) {
|
||||
this.auditLogRepository = auditLogRepository;
|
||||
this.auditLogArchiveRepository = auditLogArchiveRepository;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Mono<Long> archiveOldLogs(int daysToKeep) {
|
||||
LocalDateTime archiveBefore = LocalDateTime.now().minusDays(daysToKeep);
|
||||
|
||||
logger.info("开始归档审计日志,归档时间点: {}", archiveBefore);
|
||||
|
||||
return auditLogRepository.findByOperationTimeBetween(
|
||||
LocalDateTime.MIN,
|
||||
archiveBefore
|
||||
)
|
||||
.flatMap(this::archiveLog)
|
||||
.count()
|
||||
.doOnSuccess(count -> logger.info("审计日志归档完成,共归档 {} 条记录", count))
|
||||
.doOnError(error -> logger.error("审计日志归档失败: {}", error.getMessage()));
|
||||
}
|
||||
|
||||
private Mono<Void> archiveLog(AuditLog auditLog) {
|
||||
AuditLogArchive archive = new AuditLogArchive();
|
||||
archive.setEntityType(auditLog.getEntityType());
|
||||
archive.setEntityId(auditLog.getEntityId());
|
||||
archive.setOperationType(auditLog.getOperationType());
|
||||
archive.setOperator(auditLog.getOperator());
|
||||
archive.setOperationTime(auditLog.getOperationTime());
|
||||
archive.setBeforeData(auditLog.getBeforeData());
|
||||
archive.setAfterData(auditLog.getAfterData());
|
||||
archive.setChangedFields(auditLog.getChangedFields());
|
||||
archive.setIpAddress(auditLog.getIpAddress());
|
||||
archive.setUserAgent(auditLog.getUserAgent());
|
||||
archive.setDescription(auditLog.getDescription());
|
||||
archive.setCreatedAt(auditLog.getCreatedAt());
|
||||
archive.setArchivedAt(LocalDateTime.now());
|
||||
|
||||
return auditLogArchiveRepository.save(archive)
|
||||
.flatMap(savedArchive -> auditLogRepository.deleteById(auditLog.getId()))
|
||||
.doOnSuccess(v -> logger.debug("归档审计日志成功: ID={}", auditLog.getId()))
|
||||
.doOnError(error -> logger.error("归档审计日志失败: ID={}, 错误: {}",
|
||||
auditLog.getId(), error.getMessage()))
|
||||
.then();
|
||||
}
|
||||
|
||||
public Flux<AuditLogArchive> findArchivedLogs(String entityType, LocalDateTime startTime, LocalDateTime endTime) {
|
||||
if (entityType != null) {
|
||||
return auditLogArchiveRepository.findByEntityType(entityType);
|
||||
} else if (startTime != null && endTime != null) {
|
||||
return auditLogArchiveRepository.findByArchivedAtBetween(startTime, endTime);
|
||||
}
|
||||
return Flux.empty();
|
||||
}
|
||||
|
||||
public Mono<Long> countArchivedLogs(String entityType) {
|
||||
if (entityType != null) {
|
||||
return auditLogArchiveRepository.countByEntityType(entityType);
|
||||
}
|
||||
return auditLogArchiveRepository.count();
|
||||
}
|
||||
}
|
||||
-93
@@ -1,93 +0,0 @@
|
||||
package cn.novalon.manage.sys.audit.service;
|
||||
|
||||
import cn.novalon.manage.sys.audit.domain.AuditLog;
|
||||
import cn.novalon.manage.sys.audit.repository.IAuditLogRepository;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* 审计日志服务
|
||||
*
|
||||
* 文件定义:封装审计日志的业务逻辑
|
||||
* 涉及业务:审计日志的保存、查询、统计等操作
|
||||
* 算法:使用异步线程池处理审计日志,不阻塞主流程
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-04-01
|
||||
*/
|
||||
@Service
|
||||
public class AuditLogService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AuditLogService.class);
|
||||
|
||||
private final IAuditLogRepository auditLogRepository;
|
||||
private final Executor auditLogExecutor;
|
||||
|
||||
public AuditLogService(IAuditLogRepository auditLogRepository,
|
||||
Executor auditLogExecutor) {
|
||||
this.auditLogRepository = auditLogRepository;
|
||||
this.auditLogExecutor = auditLogExecutor;
|
||||
}
|
||||
|
||||
@Async("auditLogExecutor")
|
||||
public Mono<AuditLog> saveAsync(AuditLog auditLog) {
|
||||
logger.debug("异步保存审计日志: {} - {}", auditLog.getEntityType(), auditLog.getOperationType());
|
||||
|
||||
return auditLogRepository.save(auditLog)
|
||||
.doOnSuccess(saved -> logger.debug("审计日志保存成功: ID={}", saved.getId()))
|
||||
.doOnError(error -> logger.error("审计日志保存失败: {}", error.getMessage()))
|
||||
.subscribeOn(Schedulers.fromExecutor(auditLogExecutor));
|
||||
}
|
||||
|
||||
public Mono<AuditLog> findById(Long id) {
|
||||
return auditLogRepository.findById(id);
|
||||
}
|
||||
|
||||
public Flux<AuditLog> findByEntityType(String entityType) {
|
||||
return auditLogRepository.findByEntityType(entityType);
|
||||
}
|
||||
|
||||
public Flux<AuditLog> findByEntityId(Long entityId) {
|
||||
return auditLogRepository.findByEntityId(entityId);
|
||||
}
|
||||
|
||||
public Flux<AuditLog> findByOperator(String operator) {
|
||||
return auditLogRepository.findByOperator(operator);
|
||||
}
|
||||
|
||||
public Flux<AuditLog> findByOperationType(String operationType) {
|
||||
return auditLogRepository.findByOperationType(operationType);
|
||||
}
|
||||
|
||||
public Flux<AuditLog> findByOperationTimeBetween(LocalDateTime startTime, LocalDateTime endTime) {
|
||||
return auditLogRepository.findByOperationTimeBetween(startTime, endTime);
|
||||
}
|
||||
|
||||
public Flux<AuditLog> findByEntityTypeAndEntityId(String entityType, Long entityId) {
|
||||
return auditLogRepository.findByEntityTypeAndEntityId(entityType, entityId);
|
||||
}
|
||||
|
||||
public Mono<Long> countByEntityType(String entityType) {
|
||||
return auditLogRepository.countByEntityType(entityType);
|
||||
}
|
||||
|
||||
public Mono<Long> countByOperationType(String operationType) {
|
||||
return auditLogRepository.countByOperationType(operationType);
|
||||
}
|
||||
|
||||
public Mono<Long> countByOperator(String operator) {
|
||||
return auditLogRepository.countByOperator(operator);
|
||||
}
|
||||
|
||||
public Mono<Long> countByOperationTimeBetween(LocalDateTime startTime, LocalDateTime endTime) {
|
||||
return auditLogRepository.countByOperationTimeBetween(startTime, endTime);
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package cn.novalon.manage.sys.audit.service;
|
||||
|
||||
import cn.novalon.manage.sys.audit.domain.AuditLog;
|
||||
import cn.novalon.manage.sys.audit.domain.AuditLogArchive;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 审计日志归档服务接口
|
||||
*
|
||||
* 文件定义:定义审计日志归档的业务逻辑接口
|
||||
* 涉及业务:审计日志的归档、查询、清理等操作
|
||||
* 算法:定期将历史审计日志移动到归档表
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-04-14
|
||||
*/
|
||||
public interface IAuditLogArchiveService {
|
||||
|
||||
Mono<Long> archiveOldLogs(int daysToKeep);
|
||||
|
||||
Mono<AuditLogArchive> archiveLog(AuditLog auditLog);
|
||||
|
||||
Flux<AuditLogArchive> findArchivedLogsByDateRange(LocalDateTime startDate, LocalDateTime endDate);
|
||||
|
||||
Flux<AuditLogArchive> findArchivedLogsByEntityType(String entityType);
|
||||
|
||||
Mono<AuditLogArchive> findArchivedLogById(Long id);
|
||||
|
||||
Mono<Long> countArchivedLogs();
|
||||
|
||||
Mono<Long> countArchivedLogsByDateRange(LocalDateTime startDate, LocalDateTime endDate);
|
||||
|
||||
Mono<Void> deleteArchivedLogsOlderThan(LocalDateTime date);
|
||||
|
||||
Mono<Long> getArchiveStatistics();
|
||||
|
||||
Mono<Boolean> isLogArchived(Long auditLogId);
|
||||
}
|
||||
+48
-9
@@ -1,9 +1,14 @@
|
||||
package cn.novalon.manage.sys.audit.service;
|
||||
|
||||
import cn.novalon.manage.sys.audit.domain.AuditLog;
|
||||
import cn.novalon.manage.common.dto.PageRequest;
|
||||
import cn.novalon.manage.common.dto.PageResponse;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 审计日志服务接口
|
||||
*
|
||||
@@ -11,20 +16,54 @@ import reactor.core.publisher.Mono;
|
||||
* @date 2026-04-08
|
||||
*/
|
||||
public interface IAuditLogService {
|
||||
|
||||
Mono<AuditLog> save(AuditLog auditLog);
|
||||
|
||||
|
||||
Mono<AuditLog> findById(Long id);
|
||||
|
||||
|
||||
Flux<AuditLog> findAll();
|
||||
|
||||
|
||||
Flux<AuditLog> findAll(boolean includeDeleted);
|
||||
|
||||
Mono<PageResponse<AuditLog>> findAuditLogsByPage(PageRequest pageRequest);
|
||||
|
||||
Mono<Long> count();
|
||||
|
||||
Flux<AuditLog> findByEntityType(String entityType);
|
||||
|
||||
|
||||
Flux<AuditLog> findByEntityId(Long entityId);
|
||||
|
||||
|
||||
Flux<AuditLog> findByEntityTypeAndEntityId(String entityType, Long entityId);
|
||||
|
||||
|
||||
Flux<AuditLog> findByOperator(String operator);
|
||||
|
||||
|
||||
Flux<AuditLog> findByOperationType(String operationType);
|
||||
|
||||
Flux<AuditLog> findByOperationTimeBetween(LocalDateTime startTime, LocalDateTime endTime);
|
||||
|
||||
Flux<AuditLog> findByEntityTypeAndOperationTimeBetween(String entityType, LocalDateTime startTime,
|
||||
LocalDateTime endTime);
|
||||
|
||||
Flux<AuditLog> findByOperatorAndOperationTimeBetween(String operator, LocalDateTime startTime,
|
||||
LocalDateTime endTime);
|
||||
|
||||
Mono<Long> countByEntityType(String entityType);
|
||||
|
||||
Mono<Long> countByOperationType(String operationType);
|
||||
|
||||
Mono<Long> countByOperator(String operator);
|
||||
|
||||
Mono<Long> countByOperationTimeBetween(LocalDateTime startTime, LocalDateTime endTime);
|
||||
|
||||
Mono<AuditLog> save(AuditLog auditLog);
|
||||
|
||||
Mono<AuditLog> saveAsync(AuditLog auditLog);
|
||||
|
||||
Mono<Void> deleteById(Long id);
|
||||
|
||||
Mono<Void> logicalDeleteById(Long id);
|
||||
|
||||
Mono<Void> logicalDeleteByIds(List<Long> ids);
|
||||
|
||||
Mono<Void> restoreById(Long id);
|
||||
|
||||
Mono<Void> restoreByIds(List<Long> ids);
|
||||
}
|
||||
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package cn.novalon.manage.sys.audit.service.impl;
|
||||
|
||||
import cn.novalon.manage.sys.audit.domain.AuditLog;
|
||||
import cn.novalon.manage.sys.audit.domain.AuditLogArchive;
|
||||
import cn.novalon.manage.sys.audit.repository.IAuditLogArchiveRepository;
|
||||
import cn.novalon.manage.sys.audit.repository.IAuditLogRepository;
|
||||
import cn.novalon.manage.sys.audit.service.IAuditLogArchiveService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 审计日志归档服务实现类
|
||||
*
|
||||
* 文件定义:实现审计日志归档的业务逻辑
|
||||
* 涉及业务:审计日志的归档、查询、清理等操作
|
||||
* 算法:定期将历史审计日志移动到归档表
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-04-14
|
||||
*/
|
||||
@Service
|
||||
public class AuditLogArchiveService implements IAuditLogArchiveService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AuditLogArchiveService.class);
|
||||
|
||||
private final IAuditLogRepository auditLogRepository;
|
||||
private final IAuditLogArchiveRepository auditLogArchiveRepository;
|
||||
|
||||
public AuditLogArchiveService(IAuditLogRepository auditLogRepository,
|
||||
IAuditLogArchiveRepository auditLogArchiveRepository) {
|
||||
this.auditLogRepository = auditLogRepository;
|
||||
this.auditLogArchiveRepository = auditLogArchiveRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Mono<Long> archiveOldLogs(int daysToKeep) {
|
||||
LocalDateTime archiveBefore = LocalDateTime.now().minusDays(daysToKeep);
|
||||
|
||||
logger.info("开始归档审计日志,归档时间点: {}", archiveBefore);
|
||||
|
||||
return auditLogRepository.findByOperationTimeBetween(LocalDateTime.MIN, archiveBefore)
|
||||
.flatMap(this::archiveLog)
|
||||
.count()
|
||||
.doOnSuccess(count -> logger.info("归档完成,共归档 {} 条日志", count))
|
||||
.doOnError(error -> logger.error("归档失败: {}", error.getMessage()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Mono<AuditLogArchive> archiveLog(AuditLog auditLog) {
|
||||
AuditLogArchive archive = convertToArchive(auditLog);
|
||||
|
||||
return auditLogArchiveRepository.save(archive)
|
||||
.doOnSuccess(saved -> {
|
||||
logger.debug("审计日志归档成功: ID={}, 操作类型={}",
|
||||
saved.getId(), saved.getOperationType());
|
||||
|
||||
auditLogRepository.deleteById(auditLog.getId())
|
||||
.doOnSuccess(v -> logger.debug("原始日志删除成功: ID={}", auditLog.getId()))
|
||||
.doOnError(error -> logger.error("原始日志删除失败: ID={}, {}",
|
||||
auditLog.getId(), error.getMessage()))
|
||||
.subscribe();
|
||||
})
|
||||
.doOnError(error -> logger.error("审计日志归档失败: ID={}, {}",
|
||||
auditLog.getId(), error.getMessage()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<AuditLogArchive> findArchivedLogsByDateRange(LocalDateTime startDate, LocalDateTime endDate) {
|
||||
return auditLogArchiveRepository.findByOperationTimeBetween(startDate, endDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<AuditLogArchive> findArchivedLogsByEntityType(String entityType) {
|
||||
return auditLogArchiveRepository.findByEntityType(entityType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<AuditLogArchive> findArchivedLogById(Long id) {
|
||||
return auditLogArchiveRepository.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Long> countArchivedLogs() {
|
||||
return auditLogArchiveRepository.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Long> countArchivedLogsByDateRange(LocalDateTime startDate, LocalDateTime endDate) {
|
||||
return auditLogArchiveRepository.findByOperationTimeBetween(startDate, endDate)
|
||||
.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Mono<Void> deleteArchivedLogsOlderThan(LocalDateTime date) {
|
||||
return auditLogArchiveRepository.findByOperationTimeBetween(LocalDateTime.MIN, date)
|
||||
.flatMap(archive -> auditLogArchiveRepository.deleteById(archive.getId()))
|
||||
.then()
|
||||
.doOnSuccess(v -> logger.info("删除早于 {} 的归档日志完成", date))
|
||||
.doOnError(error -> logger.error("删除归档日志失败: {}", error.getMessage()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Long> getArchiveStatistics() {
|
||||
return auditLogArchiveRepository.count()
|
||||
.doOnNext(count -> logger.info("归档日志统计: {} 条记录", count));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Boolean> isLogArchived(Long auditLogId) {
|
||||
return auditLogArchiveRepository.findAll()
|
||||
.filter(archive -> archive.getEntityId() != null && archive.getEntityId().equals(auditLogId))
|
||||
.hasElements()
|
||||
.doOnNext(archived -> logger.debug("日志 ID={} 是否已归档: {}", auditLogId, archived));
|
||||
}
|
||||
|
||||
private AuditLogArchive convertToArchive(AuditLog auditLog) {
|
||||
AuditLogArchive archive = new AuditLogArchive();
|
||||
archive.setEntityType(auditLog.getEntityType());
|
||||
archive.setEntityId(auditLog.getEntityId());
|
||||
archive.setOperationType(auditLog.getOperationType());
|
||||
archive.setOperator(auditLog.getOperator());
|
||||
archive.setOperationTime(auditLog.getOperationTime());
|
||||
archive.setIpAddress(auditLog.getIpAddress());
|
||||
archive.setUserAgent(auditLog.getUserAgent());
|
||||
archive.setArchivedAt(LocalDateTime.now());
|
||||
|
||||
if (auditLog.getDescription() != null) {
|
||||
archive.setDescription(auditLog.getDescription());
|
||||
}
|
||||
|
||||
return archive;
|
||||
}
|
||||
}
|
||||
+144
-6
@@ -3,15 +3,28 @@ package cn.novalon.manage.sys.audit.service.impl;
|
||||
import cn.novalon.manage.sys.audit.domain.AuditLog;
|
||||
import cn.novalon.manage.sys.audit.repository.IAuditLogRepository;
|
||||
import cn.novalon.manage.sys.audit.service.IAuditLogService;
|
||||
import cn.novalon.manage.common.dto.PageRequest;
|
||||
import cn.novalon.manage.common.dto.PageResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* 审计日志服务实现类
|
||||
*
|
||||
* 文件定义:实现审计日志管理的核心业务逻辑
|
||||
* 涉及业务:审计日志的保存、查询、统计、删除等操作
|
||||
* 算法:使用R2DBC进行响应式数据库操作,支持分页查询、条件查询、批量操作
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-04-08
|
||||
*/
|
||||
@@ -21,14 +34,12 @@ public class AuditLogService implements IAuditLogService {
|
||||
private static final Logger logger = LoggerFactory.getLogger(AuditLogService.class);
|
||||
|
||||
private final IAuditLogRepository auditLogRepository;
|
||||
private final Executor auditLogExecutor;
|
||||
|
||||
public AuditLogService(IAuditLogRepository auditLogRepository) {
|
||||
public AuditLogService(IAuditLogRepository auditLogRepository,
|
||||
Executor auditLogExecutor) {
|
||||
this.auditLogRepository = auditLogRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<AuditLog> save(AuditLog auditLog) {
|
||||
return auditLogRepository.save(auditLog);
|
||||
this.auditLogExecutor = auditLogExecutor;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -41,6 +52,38 @@ public class AuditLogService implements IAuditLogService {
|
||||
return auditLogRepository.findAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<AuditLog> findAll(boolean includeDeleted) {
|
||||
if (includeDeleted) {
|
||||
return auditLogRepository.findAll();
|
||||
} else {
|
||||
return auditLogRepository.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<PageResponse<AuditLog>> findAuditLogsByPage(PageRequest pageRequest) {
|
||||
return auditLogRepository.findAll()
|
||||
.collectList()
|
||||
.map(auditLogs -> {
|
||||
int total = auditLogs.size();
|
||||
int pageSize = pageRequest.getSize();
|
||||
int pageNumber = pageRequest.getPage();
|
||||
int fromIndex = pageNumber * pageSize;
|
||||
int toIndex = Math.min(fromIndex + pageSize, total);
|
||||
|
||||
List<AuditLog> pageContent = auditLogs.subList(fromIndex, toIndex);
|
||||
int totalPages = (int) Math.ceil((double) total / pageSize);
|
||||
return new PageResponse<>(pageContent, totalPages, total, pageNumber, pageSize);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Long> count() {
|
||||
return auditLogRepository.findAll()
|
||||
.count();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<AuditLog> findByEntityType(String entityType) {
|
||||
return auditLogRepository.findByEntityType(entityType);
|
||||
@@ -65,4 +108,99 @@ public class AuditLogService implements IAuditLogService {
|
||||
public Flux<AuditLog> findByOperationType(String operationType) {
|
||||
return auditLogRepository.findByOperationType(operationType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<AuditLog> findByOperationTimeBetween(LocalDateTime startTime, LocalDateTime endTime) {
|
||||
return auditLogRepository.findByOperationTimeBetween(startTime, endTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<AuditLog> findByEntityTypeAndOperationTimeBetween(String entityType, LocalDateTime startTime, LocalDateTime endTime) {
|
||||
return auditLogRepository.findByEntityTypeAndOperationTimeBetween(entityType, startTime, endTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<AuditLog> findByOperatorAndOperationTimeBetween(String operator, LocalDateTime startTime, LocalDateTime endTime) {
|
||||
return auditLogRepository.findByOperatorAndOperationTimeBetween(operator, startTime, endTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Long> countByEntityType(String entityType) {
|
||||
return auditLogRepository.countByEntityType(entityType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Long> countByOperationType(String operationType) {
|
||||
return auditLogRepository.countByOperationType(operationType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Long> countByOperator(String operator) {
|
||||
return auditLogRepository.countByOperator(operator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Long> countByOperationTimeBetween(LocalDateTime startTime, LocalDateTime endTime) {
|
||||
return auditLogRepository.countByOperationTimeBetween(startTime, endTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<AuditLog> save(AuditLog auditLog) {
|
||||
return auditLogRepository.save(auditLog);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Async("auditLogExecutor")
|
||||
public Mono<AuditLog> saveAsync(AuditLog auditLog) {
|
||||
logger.debug("异步保存审计日志: {} - {}", auditLog.getEntityType(), auditLog.getOperationType());
|
||||
|
||||
return auditLogRepository.save(auditLog)
|
||||
.doOnSuccess(saved -> logger.debug("审计日志保存成功: ID={}", saved.getId()))
|
||||
.doOnError(error -> logger.error("审计日志保存失败: {}", error.getMessage()))
|
||||
.subscribeOn(Schedulers.fromExecutor(auditLogExecutor));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Mono<Void> deleteById(Long id) {
|
||||
return auditLogRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Mono<Void> logicalDeleteById(Long id) {
|
||||
return auditLogRepository.findById(id)
|
||||
.flatMap(auditLog -> {
|
||||
auditLog.setDeletedAt(LocalDateTime.now());
|
||||
return auditLogRepository.save(auditLog);
|
||||
})
|
||||
.then();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Mono<Void> logicalDeleteByIds(List<Long> ids) {
|
||||
return Flux.fromIterable(ids)
|
||||
.flatMap(this::logicalDeleteById)
|
||||
.then();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Mono<Void> restoreById(Long id) {
|
||||
return auditLogRepository.findById(id)
|
||||
.flatMap(auditLog -> {
|
||||
auditLog.setDeletedAt(null);
|
||||
return auditLogRepository.save(auditLog);
|
||||
})
|
||||
.then();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public Mono<Void> restoreByIds(List<Long> ids) {
|
||||
return Flux.fromIterable(ids)
|
||||
.flatMap(this::restoreById)
|
||||
.then();
|
||||
}
|
||||
}
|
||||
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
package cn.novalon.manage.sys.config;
|
||||
|
||||
import cn.novalon.manage.common.handler.ExceptionLogService;
|
||||
import cn.novalon.manage.sys.handler.ExceptionLogServiceImpl;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 异常日志配置类
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-03-13
|
||||
*/
|
||||
@Configuration
|
||||
public class ExceptionLogConfig {
|
||||
|
||||
@Bean
|
||||
public ExceptionLogService exceptionLogService(ExceptionLogServiceImpl exceptionLogServiceImpl) {
|
||||
return exceptionLogServiceImpl;
|
||||
}
|
||||
}
|
||||
-42
@@ -1,42 +0,0 @@
|
||||
package cn.novalon.manage.sys.handler;
|
||||
|
||||
import cn.novalon.manage.common.handler.ExceptionLogService;
|
||||
import cn.novalon.manage.sys.core.domain.SysExceptionLog;
|
||||
import cn.novalon.manage.sys.core.service.ISysExceptionLogService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* 异常日志服务实现
|
||||
*
|
||||
* 文件定义:实现异常日志记录接口,使用sys模块的异常日志服务
|
||||
* 涉及业务:异常日志记录、错误追踪
|
||||
* 算法:使用响应式编程实现异步日志记录
|
||||
*
|
||||
* @author 张翔
|
||||
* @date 2026-03-13
|
||||
*/
|
||||
@Service
|
||||
public class ExceptionLogServiceImpl implements ExceptionLogService {
|
||||
|
||||
private final ISysExceptionLogService exceptionLogService;
|
||||
|
||||
public ExceptionLogServiceImpl(ISysExceptionLogService exceptionLogService) {
|
||||
this.exceptionLogService = exceptionLogService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> logException(String title, String exceptionName, String exceptionMsg,
|
||||
String methodName, String ip, String stackTrace) {
|
||||
SysExceptionLog exceptionLog = new SysExceptionLog();
|
||||
exceptionLog.setTitle(title);
|
||||
exceptionLog.setExceptionName(exceptionName);
|
||||
exceptionLog.setExceptionMsg(exceptionMsg);
|
||||
exceptionLog.setMethodName(methodName);
|
||||
exceptionLog.setIp(ip);
|
||||
exceptionLog.setCreateTime(java.time.LocalDateTime.now());
|
||||
exceptionLog.setExceptionStack(stackTrace);
|
||||
|
||||
return exceptionLogService.save(exceptionLog).then();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user