import { Injectable, Logger } from '@nestjs/common'; import { Cron, CronExpression } from '@nestjs/schedule'; import { SettingsService } from '../settings/settings.service'; import { OperationLogService } from './operation-log.service'; @Injectable() export class OperationLogRetentionService { private readonly logger = new Logger(OperationLogRetentionService.name); constructor( private readonly settings: SettingsService, private readonly logs: OperationLogService, ) {} @Cron(CronExpression.EVERY_DAY_AT_4AM) async cleanup(): Promise { const raw = await this.settings.get('operationLogRetentionDays'); const days = Number(raw ?? 365); if (!days || days <= 0) return; const count = await this.logs.cleanup(days); this.logger.log(`操作日志清理完成:${count} 条,保留 ${days} 天`); } }