elderly-health-platform/sql/mysql/elderly_phase3a.sql
2026-10-16 20:17:14 +08:00

104 lines
5.7 KiB
SQL
Raw Permalink 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.

-- ============================================================================
-- Phase 3A:统一健康评估框架(评估记录 + 逐题答案)—— 数据库增量升级脚本
--
-- 设计依据:docs/phase3a_assessment_schema_audit.md(Phase 3A-0 审计报告)
-- 采用审计报告推荐的「方案 B:统一评估记录 + 逐题答案」:
-- 1. elderly_assessment_record 一次评估一条记录(含分数与分级)
-- 2. elderly_assessment_answer 该次评估的逐题作答与每题得分
--
-- 重要:
-- 1. 本脚本是增量升级脚本,只做 CREATE TABLE IF NOT EXISTS + 菜单 ADD,
-- 不会 DROP / 清空 Phase 0~2 的任何数据
-- 2. 不创建 assessment_scale / assessment_question / assessment_option 等
-- 动态定义表:量表定义以 Java 常量 + 每量表一个 Scorer 实现(见实施文档)
-- ============================================================================
SET NAMES utf8mb4;
-- ----------------------------
-- Table structure for elderly_assessment_record
--
-- 说明:
-- 不同量表使用的字段不同,因此 screening_score / full_score / need_full
-- 允许为 NULL(例如单阶段的 FALL_CDC_12 不使用这三个字段)
-- ----------------------------
CREATE TABLE IF NOT EXISTS `elderly_assessment_record` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
`elderly_id` bigint NOT NULL COMMENT '老人档案编号(elderly_person.id)',
`member_user_id` bigint NOT NULL COMMENT 'App 会员用户编号(member_user.id)',
`assessment_type` varchar(32) NOT NULL COMMENT '评估类型编码,例如 FALL_CDC_12',
`scale_version` varchar(32) NOT NULL COMMENT '量表版本号(项目内部版本号,非官方版本号)',
`assessment_time` datetime NOT NULL COMMENT '评估时间',
`screening_score` decimal(6,2) NULL DEFAULT NULL COMMENT '初筛得分(非两阶段量表为 NULL)',
`full_score` decimal(6,2) NULL DEFAULT NULL COMMENT '完整评估得分(非两阶段量表为 NULL)',
`total_score` decimal(6,2) NULL DEFAULT NULL COMMENT '总分(量表不适用时为 NULL)',
`level` varchar(32) NULL DEFAULT NULL COMMENT '分级编码(各量表自定义,例如 HIGH_RISK)',
`need_full` bit(1) NULL DEFAULT NULL COMMENT '是否需要完成完整评估(非两阶段量表为 NULL)',
`is_full_assessment` bit(1) NOT NULL DEFAULT b'0' COMMENT '本次评估是否已完整完成',
`remark` varchar(500) NULL DEFAULT NULL COMMENT '备注',
`creator` varchar(64) NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
`tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
PRIMARY KEY (`id`),
KEY `idx_elderly_id` (`elderly_id`),
KEY `idx_member_user_id` (`member_user_id`),
KEY `idx_assessment_type` (`assessment_type`),
KEY `idx_assessment_time` (`assessment_time`),
KEY `idx_tenant_id` (`tenant_id`)
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COMMENT = '老人健康评估记录表';
-- ----------------------------
-- Table structure for elderly_assessment_answer
--
-- 说明:UNIQUE(assessment_id, question_code) 保证同一次评估同一题不会重复作答。
-- Phase 3A 不提供答案的修改与删除(每次正式提交创建新的评估记录),
-- 因此唯一索引与逻辑删除不冲突。
-- ----------------------------
CREATE TABLE IF NOT EXISTS `elderly_assessment_answer` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
`assessment_id` bigint NOT NULL COMMENT '评估记录编号(elderly_assessment_record.id)',
`question_code` varchar(64) NOT NULL COMMENT '题目编码,例如 FALL_CDC_Q01',
`question_no` int NOT NULL COMMENT '题目序号(从 1 开始)',
`answer_value` varchar(64) NULL DEFAULT NULL COMMENT '作答编码,例如 YES / NO',
`answer_label` varchar(255) NULL DEFAULT NULL COMMENT '作答文字,便于展示与追溯',
`score` decimal(6,2) NOT NULL DEFAULT 0.00 COMMENT '该题得分(由后端计算)',
`value_source` varchar(32) NULL DEFAULT NULL COMMENT '取值来源:measured_now / from_health_record 等',
`source_record_id` bigint NULL DEFAULT NULL COMMENT '取值来源记录编号(如 elderly_health_record.id)',
`creator` varchar(64) NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
`tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
PRIMARY KEY (`id`),
UNIQUE KEY `uk_assessment_question` (`assessment_id`, `question_code`),
KEY `idx_question_code` (`question_code`),
KEY `idx_tenant_id` (`tenant_id`)
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COMMENT = '老人健康评估逐题答案表';
-- ============================================================================
-- 管理后台菜单权限:老人档案 → 健康评估(只读)
-- ============================================================================
DELETE FROM `system_menu` WHERE `id` IN (6504);
INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`,
`component`, `component_name`, `status`, `creator`, `updater`) VALUES
(6504, '健康评估', 'elderly:assessment:query', 3, 2, 6502, '', '',
NULL, NULL, 0, 'admin', 'admin');