-- =====================================================
-- XL监狱综合管理平台 - 问卷系统增强脚本
-- 执行日期：2026-01-13
-- =====================================================

-- 切换到目标数据库
USE xlcp_dev;

-- =====================================================
-- 1. 问题表 (prison_question) 新增字段
-- =====================================================
ALTER TABLE prison_question
ADD COLUMN part_name VARCHAR(100) COMMENT '分区名称（Part名称），用于将问题分组' AFTER is_required,
ADD COLUMN part_sort INT DEFAULT 0 COMMENT '分区排序（同一问卷内的分区序号）' AFTER part_name,
ADD COLUMN help_text VARCHAR(500) COMMENT '帮助说明文字' AFTER part_sort,
ADD COLUMN placeholder VARCHAR(200) COMMENT '占位提示（填空题/数字题显示）' AFTER help_text,
ADD COLUMN default_value VARCHAR(200) COMMENT '默认值' AFTER placeholder,
ADD COLUMN auto_fill_type VARCHAR(20) DEFAULT 'NONE' COMMENT '自动填充类型：NONE-无 AUTO-系统自动填充 MANUAL-手动输入' AFTER default_value,
ADD COLUMN auto_fill_source VARCHAR(100) COMMENT '自动填充来源：dict:字典类型 / field:字段名 示例：dict:system_user_sex 或 field:prisonerName' AFTER auto_fill_type,
ADD COLUMN display_condition VARCHAR(500) COMMENT '显示条件JSON：{\"field\":\"字段名\",\"operator\":\">=\",\"value\":\"值\"} 示例：{\"field\":\"riskLevel\",\"operator\":\">=\",\"value\":\"3\"}' AFTER auto_fill_source,
ADD COLUMN min_value INT COMMENT '最小值（数字/评分题）' AFTER display_condition,
ADD COLUMN max_value INT COMMENT '最大值（数字/评分题）' AFTER min_value;

-- =====================================================
-- 2. 问卷表 (prison_questionnaire) 新增字段
-- =====================================================
ALTER TABLE prison_questionnaire
ADD COLUMN cover_image VARCHAR(500) COMMENT '封面图片URL' AFTER status,
ADD COLUMN instruction VARCHAR(1000) COMMENT '填写说明' AFTER cover_image,
ADD COLUMN estimated_time INT COMMENT '预计耗时（分钟）' AFTER instruction,
ADD COLUMN part_count INT DEFAULT 0 COMMENT '分区数量' AFTER estimated_time,
ADD COLUMN allow_anonymous TINYINT(1) DEFAULT 1 COMMENT '是否允许匿名：0-否 1-是' AFTER part_count;

-- =====================================================
-- 3. 验证脚本 - 查看新增字段
-- =====================================================
SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_COMMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'xlcp_dev'
  AND TABLE_NAME = 'prison_question'
  AND COLUMN_NAME LIKE 'part_%'
   OR COLUMN_NAME = 'help_text'
   OR COLUMN_NAME = 'placeholder'
   OR COLUMN_NAME = 'default_value'
   OR COLUMN_NAME LIKE 'auto_%'
   OR COLUMN_NAME = 'display_condition'
   OR COLUMN_NAME IN ('min_value', 'max_value')
ORDER BY ORDINAL_POSITION;

SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_COMMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'xlcp_dev'
  AND TABLE_NAME = 'prison_questionnaire'
  AND COLUMN_NAME IN ('cover_image', 'instruction', 'estimated_time', 'part_count', 'allow_anonymous')
ORDER BY ORDINAL_POSITION;

-- =====================================================
-- 4. 问题类型字典数据（如果需要）
-- =====================================================
-- 问题类型：1-单选 2-多选 3-填空 4-评分 5-日期 6-数字
-- INSERT INTO system_dict_data (dict_type, dict_value, dict_label, sort_order, status)
-- VALUES ('prison_question_type', '5', '日期', 5, '0'),
--        ('prison_question_type', '6', '数字', 6, '0');

-- =====================================================
-- 5. 自动填充类型字典数据
-- =====================================================
-- INSERT INTO system_dict_data (dict_type, dict_value, dict_label, sort_order, status)
-- VALUES ('question_auto_fill_type', 'NONE', '无', 1, '0'),
--        ('question_auto_fill_type', 'AUTO', '系统自动填充', 2, '0'),
--        ('question_auto_fill_type', 'MANUAL', '手动输入', 3, '0');

SELECT '脚本执行完成！请验证字段是否添加成功。' AS RESULT;
