package cn.novalon.manage.sys.audit.scheduler;

import cn.novalon.manage.sys.audit.service.AuditLogArchiveService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 审计日志归档定时任务
 * 
 * 文件定义：定时执行审计日志归档任务
 * 涉及业务：定期将历史审计日志移动到归档表
 * 算法：使用Spring Scheduler定时执行归档任务
 * 
 * @author 张翔
 * @date 2026-04-01
 */
@Component
public class AuditLogArchiveScheduler {

    private static final Logger logger = LoggerFactory.getLogger(AuditLogArchiveScheduler.class);

    private final AuditLogArchiveService auditLogArchiveService;

    public AuditLogArchiveScheduler(AuditLogArchiveService auditLogArchiveService) {
        this.auditLogArchiveService = auditLogArchiveService;
    }

    @Scheduled(cron = "0 0 2 * * ?")
    public void archiveOldLogs() {
        logger.info("开始执行审计日志归档定时任务");
        
        int daysToKeep = 30;
        
        auditLogArchiveService.archiveOldLogs(daysToKeep)
                .subscribe(
                        count -> logger.info("审计日志归档定时任务完成，共归档 {} 条记录", count),
                        error -> logger.error("审计日志归档定时任务失败: {}", error.getMessage())
                );
    }
}
