diff --git a/apps/api/app/api/routes/edit_plans.py b/apps/api/app/api/routes/edit_plans.py index 46f2036d1..e5b5a3199 100644 --- a/apps/api/app/api/routes/edit_plans.py +++ b/apps/api/app/api/routes/edit_plans.py @@ -35,6 +35,9 @@ from packages.adapters.sqlalchemy_impl.generation_task_repository import ( from packages.adapters.sqlalchemy_impl.template_clip_config_repository import ( SQLAlchemyTemplateClipConfigRepository, ) +from packages.adapters.sqlalchemy_impl.template_repository import ( + SQLAlchemyTemplateRepository, +) from packages.application.generation_tasks import ( CreateGenerationTaskCommand, CreateGenerationTaskUseCase, @@ -470,20 +473,39 @@ def generate_plan( plan_id, plan_check.template_id, ) + # 优先从新模型 template_clip_configs 读取,若无则回退到旧模型 template_segments clip_config_repo = SQLAlchemyTemplateClipConfigRepository(db) configs = clip_config_repo.list_by_template(plan_check.template_id) - for cfg in configs: - svc.create_clip( - plan_id=plan_id, - clip_type=cfg.clip_type.value if hasattr(cfg.clip_type, "value") else cfg.clip_type, - order=cfg.order, - template_clip_config_id=cfg.id, - duration=cfg.default_duration, - transition_effect=( - cfg.transition_effect.value if hasattr(cfg.transition_effect, "value") else cfg.transition_effect - ), - ) - logger.info("自动兜底: plan=%s 从模板复制了 %d 个片段", plan_id, len(configs)) + if configs: + for cfg in configs: + svc.create_clip( + plan_id=plan_id, + clip_type=cfg.clip_type.value if hasattr(cfg.clip_type, "value") else cfg.clip_type, + order=cfg.order, + template_clip_config_id=cfg.id, + duration=cfg.default_duration, + transition_effect=( + cfg.transition_effect.value if hasattr(cfg.transition_effect, "value") else cfg.transition_effect + ), + ) + logger.info("自动兜底: plan=%s 从新模型 template_clip_configs 复制了 %d 个片段", plan_id, len(configs)) + else: + # 回退到旧模型 template_segments + tpl_repo = SQLAlchemyTemplateRepository(db) + segments = tpl_repo.list_segments(plan_check.template_id) + for seg in segments: + avg_duration = (seg.duration_min + seg.duration_max) / 2 + svc.create_clip( + plan_id=plan_id, + clip_type="main", # 旧模型无结构角色,统一为主体片段 + order=seg.segment_order, + duration=avg_duration, + config={ + "material_type": seg.material_type or "", + "template_segment_id": seg.id, + }, + ) + logger.info("自动兜底: plan=%s 从旧模型 template_segments 复制了 %d 个片段", plan_id, len(segments)) # 检查是否可生成 try: