66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
|
|
import { Injectable, NotFoundException } from '@nestjs/common';
|
||
|
|
import { ImportCandidateRepository } from './import-candidate.repository';
|
||
|
|
import { KnowledgeItemsRepository } from '../knowledge-items/knowledge-items.repository';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class ImportCandidateService {
|
||
|
|
constructor(
|
||
|
|
private readonly repository: ImportCandidateRepository,
|
||
|
|
private readonly itemsRepo: KnowledgeItemsRepository,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
async findBySource(sourceId: string) {
|
||
|
|
return this.repository.findBySource(sourceId);
|
||
|
|
}
|
||
|
|
|
||
|
|
async findOne(id: string) {
|
||
|
|
const c = await this.repository.findById(id);
|
||
|
|
if (!c) throw new NotFoundException('候选知识点不存在');
|
||
|
|
return c;
|
||
|
|
}
|
||
|
|
|
||
|
|
async accept(id: string) {
|
||
|
|
const candidate = await this.repository.findById(id);
|
||
|
|
if (!candidate) throw new NotFoundException('候选知识点不存在');
|
||
|
|
|
||
|
|
await this.repository.updateStatus(id, 'ACCEPTED');
|
||
|
|
|
||
|
|
// 生成 KnowledgeItem
|
||
|
|
await this.itemsRepo.create(candidate.userId, candidate.knowledgeBaseId, {
|
||
|
|
title: candidate.title,
|
||
|
|
content: (candidate.content as string) ?? '',
|
||
|
|
itemType: 'ai_generated',
|
||
|
|
orderIndex: candidate.orderIndex,
|
||
|
|
});
|
||
|
|
|
||
|
|
return { status: 'ACCEPTED' };
|
||
|
|
}
|
||
|
|
|
||
|
|
async reject(id: string) {
|
||
|
|
const candidate = await this.repository.findById(id);
|
||
|
|
if (!candidate) throw new NotFoundException('候选知识点不存在');
|
||
|
|
return this.repository.updateStatus(id, 'REJECTED');
|
||
|
|
}
|
||
|
|
|
||
|
|
async batchAccept(sourceId: string) {
|
||
|
|
const candidates = await this.repository.findBySource(sourceId);
|
||
|
|
const pending = candidates.filter(c => c.status === 'PENDING');
|
||
|
|
|
||
|
|
for (const c of pending) {
|
||
|
|
await this.accept(c.id);
|
||
|
|
}
|
||
|
|
|
||
|
|
return { accepted: pending.length };
|
||
|
|
}
|
||
|
|
|
||
|
|
async update(id: string, dto: any) {
|
||
|
|
const candidate = await this.repository.findById(id);
|
||
|
|
if (!candidate) throw new NotFoundException('候选知识点不存在');
|
||
|
|
return this.repository.update(id, dto);
|
||
|
|
}
|
||
|
|
|
||
|
|
async createCandidates(userId: string, knowledgeBaseId: string, sourceId: string, importId: string, candidates: Array<any>) {
|
||
|
|
return this.repository.createMany(userId, knowledgeBaseId, sourceId, importId, candidates);
|
||
|
|
}
|
||
|
|
}
|