import { Repository } from 'typeorm'; import { N8nTemplateEntity, TemplateType } from '../entities/n8n-template.entity'; import { N8nImageGenerateTemplate, N8nVideoGenerateTemplate } from './n8nTemplate'; /** * 动态图片模板实现 * 从数据库加载配置,实现N8nImageGenerateTemplate抽象类 */ export class DynamicN8nImageTemplate extends N8nImageGenerateTemplate { private config: N8nTemplateEntity | null = null; constructor( private templateId: number, private templateRepository: Repository ) { super(); } /** * 初始化模板配置,从数据库加载数据 */ async onInit(): Promise { this.config = await this.templateRepository.findOne({ where: { id: this.templateId, templateType: TemplateType.IMAGE, isActive: true } }); if (!this.config) { throw new Error(`Image template with id ${this.templateId} not found or inactive`); } return this; } // 从数据库动态获取配置 - 实现Template抽象类的属性 get code(): string { return this.config?.code || ''; } get name(): string { return this.config?.name || ''; } get description(): string { return this.config?.description || ''; } get creditCost(): number { return this.config?.creditCost || 0; } get version(): string { return this.config?.version || ''; } get input(): string { return this.config?.inputExampleUrl || ''; } get output(): string { return this.config?.outputExampleUrl || ''; } get tags(): string[] { return this.config?.tags || []; } // 实现N8nImageGenerateTemplate抽象类的属性 get imageModel(): string { return this.config?.imageModel || ''; } get imagePrompt(): string { return this.config?.imagePrompt || ''; } // execute方法自动继承自N8nImageGenerateTemplate,无需重写 } /** * 动态视频模板实现 * 从数据库加载配置,实现N8nVideoGenerateTemplate抽象类 */ export class DynamicN8nVideoTemplate extends N8nVideoGenerateTemplate { private config: N8nTemplateEntity | null = null; constructor( private templateId: number, private templateRepository: Repository ) { super(); } /** * 初始化模板配置,从数据库加载数据 */ async onInit(): Promise { this.config = await this.templateRepository.findOne({ where: { id: this.templateId, templateType: TemplateType.VIDEO, isActive: true } }); if (!this.config) { throw new Error(`Video template with id ${this.templateId} not found or inactive`); } return this; } // 从数据库动态获取配置 - 实现Template抽象类的属性 get code(): string { return this.config?.code || ''; } get name(): string { return this.config?.name || ''; } get description(): string { return this.config?.description || ''; } get creditCost(): number { return this.config?.creditCost || 0; } get version(): string { return this.config?.version || ''; } get input(): string { return this.config?.inputExampleUrl || ''; } get output(): string { return this.config?.outputExampleUrl || ''; } get tags(): string[] { return this.config?.tags || []; } // 实现N8nImageGenerateTemplate抽象类的属性(视频模板也需要图片生成) get imageModel(): string { return this.config?.imageModel || ''; } get imagePrompt(): string { return this.config?.imagePrompt || ''; } // 实现N8nVideoGenerateTemplate抽象类的属性 get videoModel(): string { return this.config?.videoModel || ''; } get videoPrompt(): string { return this.config?.videoPrompt || ''; } get duration(): number { return this.config?.duration || 6; } get aspectRatio(): string { return this.config?.aspectRatio || '9:16'; } // execute方法自动继承自N8nVideoGenerateTemplate,无需重写 }