import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { OperationLog } from '../entities/operation-log.entity'; @Injectable() export class OperationLogsService { constructor(@InjectRepository(OperationLog) private repo: Repository) {} async log(params: { userId?: number; username?: string; module: string; action: string; targetId?: number; targetType?: string; detail?: string; ipAddress?: string; userAgent?: string; status?: string; }) { const entry = this.repo.create(params); return this.repo.save(entry); } async findAll(query?: { module?: string; userId?: number; startDate?: string; endDate?: string; page?: number; pageSize?: number; }) { const qb = this.repo.createQueryBuilder('log').orderBy('log.createdAt', 'DESC'); if (query?.module) qb.andWhere('log.module = :module', { module: query.module }); if (query?.userId) qb.andWhere('log.userId = :userId', { userId: query.userId }); if (query?.startDate) qb.andWhere('log.createdAt >= :startDate', { startDate: query.startDate }); if (query?.endDate) qb.andWhere('log.createdAt <= :endDate', { endDate: query.endDate + ' 23:59:59' }); const page = query?.page || 1; const pageSize = query?.pageSize || 50; const [data, total] = await qb .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); return { data, total, page, pageSize }; } }