import { RoleEntity, AITextEntity, TagEntity } from '../domain/Entities'; import { RoleItem, TagItem, TextItem } from '../domain/Item'; import { regenerateRole, applyRoleToShots, getRoleData } from '@/api/video_flow'; /** * 角色图编辑用例 * 负责角色图内容的初始化、修改和优化 */ export class RoleEditUseCase { constructor(private roleItem: RoleItem) { } /** * @description: 重新生成角色 * @param {TextItem} prompt * @param {TagItem[]} tags * @return {*} */ async AIgenerateRole(prompt: TextItem, tags: TagItem[]): Promise { const promptText = prompt.entity.content; const tagList = tags.map((tag) => tag.entity.content); // 调用重新生成角色接口 const response = await regenerateRole({ roleId: this.roleItem.entity.id||'', prompt: promptText, tagTypes: tagList, }); if (response.successful) { const roleEntity = response.data; this.roleItem.setEntity(roleEntity); return roleEntity; } else { throw new Error(`重新生成角色失败: ${response.message}`); } } /** * 应用此角色到指定分镜 * @param shotIds 分镜ID列表 * @returns 应用结果 */ async applyRole(shotIds: string[]) { const roleId = this.roleItem.entity.id; return await applyRoleToShots({ roleId, shotIds, }); } /** * 重新获取当前角色的数据 * @description 从服务器重新获取当前角色的AI文本和标签数据,并更新当前实体 * @returns Promise<{ text: AITextEntity; tags: TagEntity[] }> 角色相关的AI文本和标签数据 * @throws {Error} 当API调用失败时抛出错误 */ async refreshRoleData(): Promise<{ text: AITextEntity; tags: TagEntity[] }> { const roleId = this.roleItem.entity.id; if (!roleId) { throw new Error('角色ID不存在,无法获取角色数据'); } const response = await getRoleData({ roleId: roleId }); if (response.successful) { // 更新当前角色的实体数据 const { text, tags } = response.data; // 更新角色实体中的相关字段 const updatedRoleEntity = { ...this.roleItem.entity, generateTextId: text.id, // 更新AI文本ID tagIds: tags.map(tag => tag.id), // 更新标签ID列表 updatedAt: Date.now(), // 更新时间戳 }; // 更新当前UseCase中的实体 this.roleItem.setEntity(updatedRoleEntity); return response.data; } else { throw new Error(`获取角色数据失败: ${response.message}`); } } }