Files
hospital_performance/backend/create_new_tables.py
2026-02-28 15:06:52 +08:00

134 lines
5.5 KiB
Python

"""
创建新功能所需的数据库表
包括:
1. 科室类型BSC维度权重配置表
2. 满意度调查模块相关表
"""
import asyncio
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from sqlalchemy import text
from app.core.database import engine, Base
from app.models.models import (
DeptTypeDimensionWeight, Survey, SurveyQuestion,
SurveyResponse, SurveyAnswer
)
async def create_tables():
"""创建新表"""
print("开始创建新功能数据库表...")
async with engine.begin() as conn:
# 创建科室类型维度权重配置表
await conn.execute(text("""
CREATE TABLE IF NOT EXISTS dept_type_dimension_weights (
id INTEGER PRIMARY KEY AUTOINCREMENT,
dept_type VARCHAR(50) NOT NULL,
financial_weight NUMERIC(5, 2) DEFAULT 0.60,
customer_weight NUMERIC(5, 2) DEFAULT 0.15,
internal_process_weight NUMERIC(5, 2) DEFAULT 0.20,
learning_growth_weight NUMERIC(5, 2) DEFAULT 0.05,
description TEXT,
is_active BOOLEAN DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
"""))
print(" ✓ 创建 dept_type_dimension_weights 表")
# 创建满意度调查问卷表
await conn.execute(text("""
CREATE TABLE IF NOT EXISTS surveys (
id INTEGER PRIMARY KEY AUTOINCREMENT,
survey_name VARCHAR(200) NOT NULL,
survey_code VARCHAR(50) NOT NULL UNIQUE,
survey_type VARCHAR(50) NOT NULL,
description TEXT,
target_departments TEXT,
total_questions INTEGER DEFAULT 0,
status VARCHAR(20) DEFAULT 'draft',
start_date DATETIME,
end_date DATETIME,
is_anonymous BOOLEAN DEFAULT 1,
is_active BOOLEAN DEFAULT 1,
created_by INTEGER,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
"""))
print(" ✓ 创建 surveys 表")
# 创建调查问卷题目表
await conn.execute(text("""
CREATE TABLE IF NOT EXISTS survey_questions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
survey_id INTEGER NOT NULL,
question_text TEXT NOT NULL,
question_type VARCHAR(50) NOT NULL,
options TEXT,
score_max INTEGER DEFAULT 5,
is_required BOOLEAN DEFAULT 1,
sort_order INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (survey_id) REFERENCES surveys(id)
)
"""))
print(" ✓ 创建 survey_questions 表")
# 创建调查问卷回答记录表
await conn.execute(text("""
CREATE TABLE IF NOT EXISTS survey_responses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
survey_id INTEGER NOT NULL,
department_id INTEGER,
respondent_type VARCHAR(20) DEFAULT 'patient',
respondent_id INTEGER,
respondent_phone VARCHAR(20),
total_score NUMERIC(5, 2) DEFAULT 0,
max_score NUMERIC(5, 2) DEFAULT 0,
satisfaction_rate NUMERIC(5, 2) DEFAULT 0,
ip_address VARCHAR(50),
user_agent VARCHAR(500),
submitted_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (survey_id) REFERENCES surveys(id),
FOREIGN KEY (department_id) REFERENCES departments(id)
)
"""))
print(" ✓ 创建 survey_responses 表")
# 创建调查问卷回答明细表
await conn.execute(text("""
CREATE TABLE IF NOT EXISTS survey_answers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
response_id INTEGER NOT NULL,
question_id INTEGER NOT NULL,
answer_value TEXT,
score NUMERIC(5, 2) DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (response_id) REFERENCES survey_responses(id),
FOREIGN KEY (question_id) REFERENCES survey_questions(id)
)
"""))
print(" ✓ 创建 survey_answers 表")
# 创建索引
await conn.execute(text("CREATE INDEX IF NOT EXISTS idx_weight_dept_type ON dept_type_dimension_weights(dept_type)"))
await conn.execute(text("CREATE INDEX IF NOT EXISTS idx_survey_type ON surveys(survey_type)"))
await conn.execute(text("CREATE INDEX IF NOT EXISTS idx_survey_status ON surveys(status)"))
await conn.execute(text("CREATE INDEX IF NOT EXISTS idx_question_survey ON survey_questions(survey_id)"))
await conn.execute(text("CREATE INDEX IF NOT EXISTS idx_response_survey ON survey_responses(survey_id)"))
await conn.execute(text("CREATE INDEX IF NOT EXISTS idx_response_dept ON survey_responses(department_id)"))
print(" ✓ 创建索引")
print("数据库表创建完成!")
if __name__ == "__main__":
asyncio.run(create_tables())