baodan/api/insurance/db/migrate_024.py
wsb1224 f23b08eec6 07-29 PPT生成优化第一阶段完成:
已完成约 40%(阶段 1 + 阶段 2 + 阶段 3 基础编辑),覆盖了计划书中最核心的 P0 需求:

 真实预览(Canvas 方案替代了 LibreOffice 方案)
 质量检查(6 自动 + 4 人工)
 三栏结果工作台
 基础文字编辑
2026-07-29 21:26:48 +08:00

91 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""迁移 024: 注册三套场景 PPTX 模板资产。"""
import json
import logging
from sqlalchemy import text
logger = logging.getLogger(__name__)
TEMPLATES = [
{
"id": "scenario_single_savings",
"name": "单份储蓄计划|专业版",
"scenario_tag": "single_savings",
"plan_type": "savings",
"style_preset": "broker",
"asset_id": "builtin://single_savings.pptx",
},
{
"id": "scenario_multi_savings",
"name": "双储蓄险|专业对比",
"scenario_tag": "multi_savings_comparison",
"plan_type": "savings",
"style_preset": "broker",
"asset_id": "builtin://multi_savings_comparison.pptx",
},
{
"id": "scenario_savings_iul",
"name": "储蓄险 + IUL综合方案",
"scenario_tag": "savings_iul_comprehensive",
"plan_type": "savings",
"style_preset": "broker",
"asset_id": "builtin://savings_iul_comprehensive.pptx",
},
]
def migrate():
"""解析内置 PPTX并将模板元数据幂等写入模板表。"""
from insurance.db.compat import db
from insurance.ppt.template_asset_service import (
parse_template_pptx,
resolve_template_asset,
)
for seed in TEMPLATES:
file_path = resolve_template_asset(seed["asset_id"])
parsed = parse_template_pptx(file_path)
existing = db.session.execute(
text("SELECT id FROM insurance_ppt_templates WHERE id = :id"),
{"id": seed["id"]},
).first()
values = {
**seed,
"clone_renderer": "python-pptx-theme-v1",
"required_page_types_json": json.dumps(
parsed["requiredPageTypes"], ensure_ascii=False
),
"slides_config_json": json.dumps(
parsed["slidesConfig"], ensure_ascii=False
),
}
if existing:
db.session.execute(text(
"UPDATE insurance_ppt_templates SET "
"name = :name, scenario_tag = :scenario_tag, "
"source_template_asset_id = :asset_id, clone_ready = true, "
"clone_renderer = :clone_renderer, "
"required_page_types_json = :required_page_types_json, "
"slides_config_json = :slides_config_json "
"WHERE id = :id"
), values)
else:
db.session.execute(text(
"INSERT INTO insurance_ppt_templates "
"(id, name, scenario_tag, plan_type, style_preset, "
"source_template_asset_id, clone_ready, clone_renderer, "
"required_page_types_json, slides_config_json, status) "
"VALUES (:id, :name, :scenario_tag, :plan_type, :style_preset, "
":asset_id, true, :clone_renderer, :required_page_types_json, "
":slides_config_json, 1)"
), values)
logger.info(
"[migrate_024] 已注册模板 %s%s 页)",
seed["id"],
parsed["slideCount"],
)
db.session.commit()