baodan/api/insurance/db/migrate_014.py

112 lines
4.0 KiB
Python

"""迁移 014: 导入 PPT 配置数据。"""
import os
import json
import logging
logger = logging.getLogger(__name__)
# 配置目录(已迁移到 insurance 模块内)
CONFIG_DIR = os.path.join(os.path.dirname(__file__), "..", "ppt", "config")
def _read_json_file(filepath):
try:
with open(filepath, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return None
def _read_json_files_in_dir(dirpath):
results = []
if not os.path.isdir(dirpath):
return results
for root, _, files in os.walk(dirpath):
for fname in files:
if fname.endswith(".json"):
data = _read_json_file(os.path.join(root, fname))
if data is not None:
results.append(data)
return results
def migrate():
"""迁移入口。"""
from insurance.db.compat import db
from insurance.models.ppt_config import PptCompany, PptProduct, PptTemplate, PptBundle
# 公司
companies_dir = os.path.join(CONFIG_DIR, "companies")
count = 0
for d in _read_json_files_in_dir(companies_dir):
if not d.get("id") or PptCompany.query.get(d["id"]):
continue
db.session.add(PptCompany(
id=d["id"],
display_name=d.get("displayName", ""),
aliases_json=json.dumps(d.get("aliases", []), ensure_ascii=False),
tenant_id=d.get("tenantId", "default"),
knowledge_directories_json=json.dumps(d.get("knowledgeDirectories", []), ensure_ascii=False),
evidence_ranking_json=json.dumps(d.get("evidenceRanking", []), ensure_ascii=False),
company_intro=d.get("companyIntro", ""),
company_highlights_json=json.dumps(d.get("companyHighlights", []), ensure_ascii=False),
))
count += 1
db.session.commit()
logger.info(f"[migrate_014] 导入 {count} 家公司")
# 产品
products_dir = os.path.join(CONFIG_DIR, "products")
count = 0
for d in _read_json_files_in_dir(products_dir):
if not d.get("id") or PptProduct.query.get(d["id"]):
continue
db.session.add(PptProduct(
id=d["id"],
company_id=d.get("companyId", ""),
plan_type=d.get("planType", "savings"),
display_name=d.get("displayName", ""),
aliases_json=json.dumps(d.get("aliases", []), ensure_ascii=False),
required_modules_json=json.dumps(d.get("requiredModules", []), ensure_ascii=False),
))
count += 1
db.session.commit()
logger.info(f"[migrate_014] 导入 {count} 个产品")
# 模板
templates_dir = os.path.join(CONFIG_DIR, "templates")
count = 0
for d in _read_json_files_in_dir(templates_dir):
if not d.get("id") or PptTemplate.query.get(d["id"]):
continue
db.session.add(PptTemplate(
id=d["id"],
plan_type=d.get("planType", "savings"),
style_preset=d.get("stylePreset", "broker"),
source_template_asset_id=d.get("sourceTemplateAssetId"),
clone_ready=d.get("cloneReady", False),
clone_renderer=d.get("cloneRenderer"),
required_page_types_json=json.dumps(d.get("requiredPageTypes", []), ensure_ascii=False),
))
count += 1
db.session.commit()
logger.info(f"[migrate_014] 导入 {count} 个模板")
# Bundle
bundles_dir = os.path.join(CONFIG_DIR, "bundles")
count = 0
for d in _read_json_files_in_dir(bundles_dir):
if not d.get("id") or PptBundle.query.get(d["id"]):
continue
db.session.add(PptBundle(
id=d["id"],
display_name=d.get("displayName", ""),
products_json=json.dumps(d.get("products", []), ensure_ascii=False),
template_family=d.get("templateFamily", "bundle"),
status=d.get("status", "active"),
modules_json=json.dumps(d.get("modules", []), ensure_ascii=False),
))
count += 1
db.session.commit()
logger.info(f"[migrate_014] 导入 {count} 个 Bundle")