""" 初始化数据库并创建测试数据 """ import asyncio from sqlalchemy import text from app.core.database import engine, Base, async_session_maker from app.models.models import Department, Staff, Indicator, User from app.core.security import get_password_hash async def init_db(): """初始化数据库""" # 创建所有表 async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) print("Database tables created!") # 创建测试数据 async with async_session_maker() as session: # 检查是否已有数据 result = await session.execute(text("SELECT COUNT(*) FROM departments")) if result.scalar() > 0: print("Data exists, skipping...") return # 创建科室(使用新的 9 种科室类型) departments = [ Department(code="KS001", name="内科", dept_type="clinical_nonsurgical_ward", level=1), Department(code="KS002", name="外科", dept_type="clinical_surgical", level=1), Department(code="KS003", name="妇产科", dept_type="clinical_nonsurgical_ward", level=1), Department(code="KS004", name="儿科", dept_type="clinical_nonsurgical_ward", level=1), Department(code="KS005", name="放射科", dept_type="medical_tech", level=1), Department(code="KS006", name="检验科", dept_type="medical_tech", level=1), Department(code="KS007", name="财务科", dept_type="finance", level=1), Department(code="KS008", name="人事科", dept_type="admin", level=1), ] session.add_all(departments) await session.flush() # 创建员工 staff_list = [ Staff(employee_id="E001", name="张三", department_id=1, position="主治医师", title="副主任医师", base_salary=8000, performance_ratio=1.2), Staff(employee_id="E002", name="李四", department_id=1, position="住院医师", title="主治医师", base_salary=6000, performance_ratio=1.0), Staff(employee_id="E003", name="王五", department_id=2, position="主治医师", title="副主任医师", base_salary=8500, performance_ratio=1.3), Staff(employee_id="E004", name="赵六", department_id=2, position="住院医师", title="主治医师", base_salary=6500, performance_ratio=1.0), Staff(employee_id="E005", name="钱七", department_id=3, position="主治医师", title="主任医师", base_salary=10000, performance_ratio=1.5), Staff(employee_id="E006", name="孙八", department_id=4, position="住院医师", title="医师", base_salary=5000, performance_ratio=0.8), Staff(employee_id="E007", name="周九", department_id=5, position="技师", title="主管技师", base_salary=7000, performance_ratio=1.1), Staff(employee_id="E008", name="吴十", department_id=6, position="检验师", title="主管技师", base_salary=7000, performance_ratio=1.1), ] session.add_all(staff_list) await session.flush() # 创建考核指标 indicators = [ Indicator(code="ZB001", name="门诊人次", indicator_type="quantity", bs_dimension="financial", weight=1.0, max_score=100, target_unit="人次"), Indicator(code="ZB002", name="住院人次", indicator_type="quantity", bs_dimension="financial", weight=1.2, max_score=100, target_unit="人次"), Indicator(code="ZB003", name="手术台次", indicator_type="quantity", bs_dimension="financial", weight=1.5, max_score=100, target_unit="台"), Indicator(code="ZB004", name="医疗质量合格率", indicator_type="quality", bs_dimension="internal_process", weight=2.0, max_score=100, target_unit="%"), Indicator(code="ZB005", name="患者满意度", indicator_type="service", bs_dimension="customer", weight=1.5, max_score=100, target_unit="%"), Indicator(code="ZB006", name="平均住院日", indicator_type="efficiency", bs_dimension="internal_process", weight=1.0, max_score=100, target_unit="天"), Indicator(code="ZB007", name="药占比控制", indicator_type="cost", bs_dimension="financial", weight=1.0, max_score=100, target_unit="%"), Indicator(code="ZB008", name="病历书写合格率", indicator_type="quality", bs_dimension="internal_process", weight=1.2, max_score=100, target_unit="%"), ] session.add_all(indicators) await session.flush() # 创建管理员用户 admin = User( username="admin", password_hash=get_password_hash("admin123"), role="admin", is_active=True ) session.add(admin) await session.commit() print("Test data created!") if __name__ == "__main__": asyncio.run(init_db())