Files
cma-management/backend/scripts/seed_p2_kpis.py
T

121 lines
4.0 KiB
Python

"""P2阶段: 新增4个高级KPI"""
import sys, os
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
from app.database import get_engine
from sqlalchemy import text
NEW_KPIS = [
{
"kpi_code": "C_LTV",
"kpi_name": "客户生命周期价值",
"dimension": "customer",
"category": "customer_scale",
"objective": "提升客户长期价值",
"formula": "Σ(每年贡献利润×折现率)",
"formula_desc": "客户全周期内贡献的净利润折现总和",
"data_source_type": "manual",
"frequency": "quarterly",
"unit": "万元",
"target_value": 15.0,
"threshold_green": ">=10",
"threshold_yellow": ">=5",
"threshold_red": "<=5",
"responsible_dept": "市场营销部",
"responsible_user": "市场总监",
"epic": "Epic2",
"status": "active",
},
{
"kpi_code": "P_NEW_PROD_RATIO",
"kpi_name": "新产品收入占比",
"dimension": "process",
"category": "innovation",
"objective": "促进产品创新",
"formula": "新产品收入/总收入×100%",
"formula_desc": "近12个月上市的新产品收入占总收入比例",
"data_source_type": "erp",
"frequency": "monthly",
"unit": "%",
"target_value": 25.0,
"threshold_green": ">20",
"threshold_yellow": ">10",
"threshold_red": "<=10",
"responsible_dept": "产品研发部",
"responsible_user": "产品总监",
"epic": "Epic2",
"status": "active",
},
{
"kpi_code": "L_DATA_AUTO_RATE",
"kpi_name": "数据自动化率",
"dimension": "learning",
"category": "innovation",
"objective": "提升数据管理效率",
"formula": "自动报表数/总报表数×100%",
"formula_desc": "已实现自动化生成的管理报表占总报表数的比例",
"data_source_type": "manual",
"frequency": "monthly",
"unit": "%",
"target_value": 75.0,
"threshold_green": ">70",
"threshold_yellow": ">50",
"threshold_red": "<=50",
"responsible_dept": "信息技术部",
"responsible_user": "CIO",
"epic": "Epic2",
"status": "active",
},
{
"kpi_code": "L_SYS_COVERAGE",
"kpi_name": "系统覆盖率",
"dimension": "learning",
"category": "innovation",
"objective": "推动管理信息化",
"formula": "信息化覆盖业务环节数/总业务环节数×100%",
"formula_desc": "已实现信息系统支持的核心业务环节占总环节数的比例",
"data_source_type": "manual",
"frequency": "quarterly",
"unit": "%",
"target_value": 85.0,
"threshold_green": ">80",
"threshold_yellow": ">60",
"threshold_red": "<=60",
"responsible_dept": "信息技术部",
"responsible_user": "CIO",
"epic": "Epic2",
"status": "active",
},
]
def seed_p2_kpis():
engine = get_engine()
with engine.connect() as conn:
inserted = 0
skipped = 0
for kpi in NEW_KPIS:
exists = conn.execute(
text("SELECT id FROM kpi_definitions WHERE kpi_code=:code"),
{"code": kpi["kpi_code"]},
).fetchone()
if exists:
print(f" ⏭️ 跳过已存在: {kpi['kpi_code']} (id={exists[0]})")
skipped += 1
continue
cols = ", ".join(kpi.keys())
vals = ", ".join(f":{k}" for k in kpi.keys())
conn.execute(text(f"INSERT INTO kpi_definitions ({cols}) VALUES ({vals})"), kpi)
new_id = conn.execute(text("SELECT LAST_INSERT_ID()")).scalar()
print(f" ✅ 新增: {kpi['kpi_code']} - {kpi['kpi_name']} (id={new_id})")
inserted += 1
conn.commit()
print(f"\n总计: 新增{inserted}, 跳过{skipped}")
return inserted
if __name__ == "__main__":
seed_p2_kpis()