oa_based/src/modules/system/log/services/task-log.service.ts

48 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-02-28 17:02:46 +08:00
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { LessThan, Repository } from 'typeorm';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { paginate } from '~/helper/paginate';
2024-02-28 08:32:35 +08:00
2024-02-28 17:02:46 +08:00
import { TaskLogQueryDto } from '../dto/log.dto';
import { TaskLogEntity } from '../entities/task-log.entity';
2024-02-28 08:32:35 +08:00
@Injectable()
export class TaskLogService {
constructor(
@InjectRepository(TaskLogEntity)
2024-02-28 17:02:46 +08:00
private taskLogRepository: Repository<TaskLogEntity>
2024-02-28 08:32:35 +08:00
) {}
2024-02-28 17:02:46 +08:00
async create(tid: number, status: number, time?: number, err?: string): Promise<number> {
2024-02-28 08:32:35 +08:00
const result = await this.taskLogRepository.save({
status,
detail: err,
time,
task: { id: tid },
2024-02-28 17:02:46 +08:00
});
return result.id;
2024-02-28 08:32:35 +08:00
}
async list({ page, pageSize }: TaskLogQueryDto) {
const queryBuilder = await this.taskLogRepository
.createQueryBuilder('task_log')
.leftJoinAndSelect('task_log.task', 'task')
2024-02-28 17:02:46 +08:00
.orderBy('task_log.id', 'DESC');
2024-02-28 08:32:35 +08:00
return paginate<TaskLogEntity>(queryBuilder, {
page,
pageSize,
2024-02-28 17:02:46 +08:00
});
2024-02-28 08:32:35 +08:00
}
async clearLog(): Promise<void> {
2024-02-28 17:02:46 +08:00
await this.taskLogRepository.clear();
2024-02-28 08:32:35 +08:00
}
async clearLogBeforeTime(time: Date): Promise<void> {
2024-02-28 17:02:46 +08:00
await this.taskLogRepository.delete({ createdAt: LessThan(time) });
2024-02-28 08:32:35 +08:00
}
}