146 lines
5.1 KiB
TypeScript
146 lines
5.1 KiB
TypeScript
import {
|
||
Entity,
|
||
Column,
|
||
PrimaryGeneratedColumn,
|
||
CreateDateColumn,
|
||
UpdateDateColumn,
|
||
ManyToOne,
|
||
JoinColumn,
|
||
Index,
|
||
} from 'typeorm';
|
||
import { N8nTemplateEntity } from './n8n-template.entity';
|
||
import { PlatformType } from './platform-user.entity';
|
||
|
||
/**
|
||
* 模板执行状态枚举
|
||
* 定义AI内容生成任务的生命周期状态
|
||
*/
|
||
export enum ExecutionStatus {
|
||
PENDING = 'pending', // 待处理 - 任务已提交,等待执行
|
||
PROCESSING = 'processing', // 处理中 - 任务正在执行,AI模型生成中
|
||
COMPLETED = 'completed', // 已完成 - 任务成功完成,内容已生成
|
||
FAILED = 'failed', // 执行失败 - 任务执行出错,生成失败
|
||
CANCELLED = 'cancelled', // 已取消 - 用户主动取消或系统超时取消
|
||
}
|
||
|
||
/**
|
||
* 执行类型枚举
|
||
* 定义模板执行的内容输出类型
|
||
*/
|
||
export enum ExecutionType {
|
||
IMAGE = 'image', // 图片生成 - 输出图片内容
|
||
VIDEO = 'video', // 视频生成 - 输出视频内容
|
||
}
|
||
|
||
/**
|
||
* 模板执行记录实体类
|
||
* 记录用户使用AI模板生成内容的完整执行过程
|
||
* 包含执行状态、性能指标、积分消耗和结果数据
|
||
* 支持图片和视频两种类型的AI生成任务追踪
|
||
*/
|
||
@Entity('template_executions')
|
||
@Index(['templateId']) // 模板ID索引 - 快速查询特定模板的执行记录
|
||
@Index(['userId']) // 用户ID索引 - 快速查询用户的执行历史
|
||
@Index(['platform']) // 平台索引 - 按平台筛选执行记录
|
||
@Index(['status']) // 状态索引 - 快速筛选不同状态的任务
|
||
@Index(['type']) // 类型索引 - 按执行类型(图片/视频)筛选
|
||
@Index(['creditTransactionId']) // 积分交易ID索引 - 关联积分消费记录
|
||
export class TemplateExecutionEntity {
|
||
/** 主键 - 模板执行记录的唯一标识符 */
|
||
@PrimaryGeneratedColumn()
|
||
id: number;
|
||
|
||
/** 模板ID - 关联的N8N模板ID,标识使用的模板配置 */
|
||
@Column({ name: 'template_id' })
|
||
templateId: number;
|
||
|
||
/** 用户ID - 发起执行任务的用户标识 */
|
||
@Column({ name: 'user_id' })
|
||
userId: string;
|
||
|
||
/** 执行平台 - 任务发起的平台来源 */
|
||
@Column({ type: 'enum', enum: PlatformType, enumName: 'PlatformType', })
|
||
platform: PlatformType;
|
||
|
||
/** 执行类型 - 标识生成内容的类型(图片或视频) */
|
||
@Column({ type: 'enum', enum: ExecutionType })
|
||
type: ExecutionType;
|
||
|
||
/** 用户提示词 - 用户输入的内容生成描述,作为AI模型的输入 */
|
||
@Column({ type: 'text' })
|
||
prompt: string;
|
||
|
||
/** 输入图片URL - 作为参考或处理基础的输入图片地址(可选) */
|
||
@Column({ name: 'input_image_url', length: 500, nullable: true })
|
||
inputImageUrl: string;
|
||
|
||
/** 输出内容URL - 生成完成后的图片或视频文件地址 */
|
||
@Column({ name: 'output_url', length: 500, nullable: true })
|
||
outputUrl: string;
|
||
|
||
/** 缩略图URL - 生成内容的预览缩略图地址,用于快速预览 */
|
||
@Column({ name: 'thumbnail_url', length: 500, nullable: true })
|
||
thumbnailUrl: string;
|
||
|
||
/** 执行状态 - 当前任务的处理状态 */
|
||
@Column({
|
||
type: 'enum',
|
||
enum: ExecutionStatus,
|
||
default: ExecutionStatus.PENDING,
|
||
})
|
||
status: ExecutionStatus;
|
||
|
||
/** 执行进度 - 任务完成百分比(0-100),用于显示进度条 */
|
||
@Column({ default: 0 })
|
||
progress: number;
|
||
|
||
/** 错误信息 - 任务失败时的详细错误描述 */
|
||
@Column({ name: 'error_message', type: 'text', nullable: true })
|
||
errorMessage: string;
|
||
|
||
/** 执行结果 - AI模型返回的完整结果数据,包含生成参数和元信息 */
|
||
@Column({ name: 'execution_result', type: 'json', nullable: true })
|
||
executionResult: any;
|
||
|
||
/** 积分消耗 - 本次执行实际消耗的用户积分数量 */
|
||
@Column({ name: 'credit_cost', default: 0 })
|
||
creditCost: number;
|
||
|
||
/** 积分交易ID - 关联的积分消费记录ID,用于财务对账和退款处理 */
|
||
@Column({ name: 'credit_transaction_id', length: 100, nullable: true })
|
||
creditTransactionId: string;
|
||
|
||
/** 输入参数 - 用户提供的完整输入参数,包含提示词、样式、尺寸等配置 */
|
||
@Column({ name: 'input_params', type: 'json', nullable: true })
|
||
inputParams: any;
|
||
|
||
/** 执行配置 - 模板执行的技术配置,包含模型参数、生成选项等 */
|
||
@Column({ name: 'execution_config', type: 'json', nullable: true })
|
||
executionConfig: any;
|
||
|
||
/** 开始执行时间 - 任务实际开始处理的时间戳 */
|
||
@Column({ name: 'started_at', nullable: true })
|
||
startedAt: Date;
|
||
|
||
/** 完成时间 - 任务执行完成(成功或失败)的时间戳 */
|
||
@Column({ name: 'completed_at', nullable: true })
|
||
completedAt: Date;
|
||
|
||
/** 执行时长 - 任务的实际处理时间(毫秒),用于性能分析和优化 */
|
||
@Column({ name: 'execution_duration', nullable: true })
|
||
executionDuration: number;
|
||
|
||
/** 创建时间 - 执行记录的创建时间戳 */
|
||
@CreateDateColumn({ name: 'created_at' })
|
||
createdAt: Date;
|
||
|
||
/** 更新时间 - 记录最后修改时间,用于状态变更追踪 */
|
||
@UpdateDateColumn({ name: 'updated_at' })
|
||
updatedAt: Date;
|
||
|
||
/** 模板关系 - 与N8N模板实体的多对一关系 */
|
||
@ManyToOne(() => N8nTemplateEntity)
|
||
@JoinColumn({ name: 'template_id' })
|
||
template: N8nTemplateEntity;
|
||
}
|