-- =====================================================
-- XL监狱综合管理平台 - 监狱模块数据库脚本
-- 生成时间: 2026-01-12
-- =====================================================

-- 注意: 执行前请确保已经创建了 prison 模块的基础表 prison_prisoner
-- 此脚本包含 8 个子模块的表结构和菜单权限

-- =====================================================
-- 1. 监区信息表 (prison_area)
-- =====================================================
CREATE TABLE IF NOT EXISTS `prison_area` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '监区ID',
    `name` varchar(100) NOT NULL COMMENT '监区名称',
    `code` varchar(50) NOT NULL COMMENT '监区编码',
    `type` tinyint NOT NULL DEFAULT 1 COMMENT '监区类型：1-普通监区 2-严管监区 3-医院 4-禁闭室',
    `capacity` int NOT NULL DEFAULT 0 COMMENT '容纳人数',
    `current_count` int NOT NULL DEFAULT 0 COMMENT '当前人数',
    `sort` int NOT NULL DEFAULT 0 COMMENT '排序',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1-启用 2-禁用',
    `remark` varchar(500) DEFAULT NULL COMMENT '备注',
    `creator` varchar(64) DEFAULT '' COMMENT '创建者',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updater` varchar(64) 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_code` (`code`),
    KEY `idx_prison_area_code` (`code`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='监区信息表';

-- =====================================================
-- 2. 监室信息表 (prison_cell)
-- =====================================================
CREATE TABLE IF NOT EXISTS `prison_cell` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '监室ID',
    `area_id` bigint NOT NULL COMMENT '所属监区ID',
    `name` varchar(100) NOT NULL COMMENT '监室名称',
    `code` varchar(50) NOT NULL COMMENT '监室编码',
    `capacity` int NOT NULL DEFAULT 0 COMMENT '床位数量',
    `current_count` int NOT NULL DEFAULT 0 COMMENT '当前人数',
    `sort` int NOT NULL DEFAULT 0 COMMENT '排序',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1-启用 2-禁用',
    `remark` varchar(500) DEFAULT NULL COMMENT '备注',
    `creator` varchar(64) DEFAULT '' COMMENT '创建者',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updater` varchar(64) 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_code` (`code`),
    KEY `idx_prison_cell_area_id` (`area_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='监室信息表';

-- =====================================================
-- 3. 消费订单表 (prison_consumption)
-- =====================================================
CREATE TABLE IF NOT EXISTS `prison_consumption` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '消费ID',
    `prisoner_id` bigint NOT NULL COMMENT '罪犯ID',
    `prisoner_no` varchar(50) NOT NULL COMMENT '罪犯编号',
    `order_no` varchar(64) DEFAULT NULL COMMENT '订单号',
    `type` tinyint NOT NULL DEFAULT 1 COMMENT '类型：1-购物 2-餐饮 3-医疗 4-通讯 5-其他',
    `total_amount` decimal(10,2) NOT NULL COMMENT '订单总金额',
    `balance` decimal(10,2) NOT NULL COMMENT '账户余额（消费后）',
    `trade_time` datetime NOT NULL COMMENT '交易时间',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1-成功 2-失败',
    `remark` varchar(500) DEFAULT NULL COMMENT '备注',
    `creator` varchar(64) DEFAULT '' COMMENT '创建者',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updater` varchar(64) 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_prison_consumption_prisoner_id` (`prisoner_id`),
    KEY `idx_prison_consumption_prisoner_no` (`prisoner_no`),
    KEY `idx_prison_consumption_order_no` (`order_no`),
    KEY `idx_prison_consumption_trade_time` (`trade_time`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='消费订单表';

-- =====================================================
-- 3.1 消费明细表 (prison_consumption_detail)
-- =====================================================
CREATE TABLE IF NOT EXISTS `prison_consumption_detail` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '明细ID',
    `consumption_id` bigint NOT NULL COMMENT '消费订单ID',
    `prisoner_id` bigint NOT NULL COMMENT '罪犯ID（冗余，便于查询）',
    `goods_name` varchar(100) NOT NULL COMMENT '商品名称',
    `goods_code` varchar(50) DEFAULT NULL COMMENT '商品编码',
    `goods_price` decimal(10,2) NOT NULL COMMENT '商品单价',
    `goods_count` int NOT NULL COMMENT '商品数量',
    `subtotal` decimal(10,2) NOT NULL COMMENT '小计金额',
    `creator` varchar(64) DEFAULT '' COMMENT '创建者',
    `create_time` datetime NOT NULL DEFAULT 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_consumption_detail_consumption_id` (`consumption_id`),
    KEY `idx_consumption_detail_prisoner_id` (`prisoner_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='消费明细表';

-- =====================================================
-- 4. 问卷模板表 (prison_questionnaire)
-- =====================================================
CREATE TABLE IF NOT EXISTS `prison_questionnaire` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '问卷ID',
    `title` varchar(200) NOT NULL COMMENT '问卷标题',
    `description` varchar(500) DEFAULT NULL COMMENT '问卷描述',
    `type` tinyint NOT NULL DEFAULT 1 COMMENT '问卷类型：1-心理测评 2-风险评估 3-日常调查',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1-草稿 2-已发布 3-已停用',
    `creator` varchar(64) DEFAULT '' COMMENT '创建者',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updater` varchar(64) 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_prison_questionnaire_status` (`status`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='问卷模板表';

-- =====================================================
-- 5. 问卷问题表 (prison_question)
-- =====================================================
CREATE TABLE IF NOT EXISTS `prison_question` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '问题ID',
    `questionnaire_id` bigint NOT NULL COMMENT '问卷ID',
    `title` varchar(500) NOT NULL COMMENT '问题标题',
    `type` tinyint NOT NULL DEFAULT 1 COMMENT '问题类型：1-单选 2-多选 3-问答 4-评分',
    `options` text COMMENT '选项，JSON格式：[{ "label": "选项1", "value": "1" }]',
    `score` int DEFAULT 0 COMMENT '分值',
    `sort` int NOT NULL DEFAULT 0 COMMENT '排序',
    `creator` varchar(64) DEFAULT '' COMMENT '创建者',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updater` varchar(64) 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_prison_question_questionnaire_id` (`questionnaire_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='问卷问题表';

-- =====================================================
-- 6. 问卷答题记录表 (prison_questionnaire_record)
-- =====================================================
CREATE TABLE IF NOT EXISTS `prison_questionnaire_record` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '记录ID',
    `questionnaire_id` bigint NOT NULL COMMENT '问卷ID',
    `prisoner_id` bigint NOT NULL COMMENT '罪犯ID',
    `prisoner_no` varchar(50) NOT NULL COMMENT '罪犯编号',
    `answers` text NOT NULL COMMENT '答案，JSON格式',
    `score` int DEFAULT 0 COMMENT '得分',
    `result` text COMMENT '评估结果',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1-待评估 2-已完成',
    `answer_time` datetime NOT NULL COMMENT '答题时间',
    `creator` varchar(64) DEFAULT '' COMMENT '创建者',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updater` varchar(64) 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_prison_questionnaire_record_questionnaire_id` (`questionnaire_id`),
    KEY `idx_prison_questionnaire_record_prisoner_id` (`prisoner_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='问卷答题记录表';

-- =====================================================
-- 7. 危险评估表 (prison_risk_assessment)
-- =====================================================
CREATE TABLE IF NOT EXISTS `prison_risk_assessment` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '评估ID',
    `prisoner_id` bigint NOT NULL COMMENT '罪犯ID',
    `prisoner_no` varchar(50) NOT NULL COMMENT '罪犯编号',
    `assessment_type` tinyint NOT NULL DEFAULT 1 COMMENT '评估类型：1-入监评估 2-定期评估 3-出监评估',
    `risk_level` tinyint NOT NULL COMMENT '风险等级：1-高风险 2-中风险 3-低风险',
    `risk_score` int DEFAULT 0 COMMENT '风险评分',
    `factors` text COMMENT '风险因素，JSON格式',
    `measures` text COMMENT '管控措施',
    `assessor_id` bigint DEFAULT NULL COMMENT '评估人ID',
    `assessor_name` varchar(50) DEFAULT NULL COMMENT '评估人姓名',
    `assessment_date` date NOT NULL COMMENT '评估日期',
    `next_date` date DEFAULT NULL COMMENT '下次评估日期',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1-有效 2-已过期',
    `creator` varchar(64) DEFAULT '' COMMENT '创建者',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updater` varchar(64) 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_prison_risk_assessment_prisoner_id` (`prisoner_id`),
    KEY `idx_prison_risk_assessment_assessment_date` (`assessment_date`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='危险评估表';

-- =====================================================
-- 8. 计分考核表 (prison_score)
-- =====================================================
CREATE TABLE IF NOT EXISTS `prison_score` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '记录ID',
    `prisoner_id` bigint NOT NULL COMMENT '罪犯ID',
    `prisoner_no` varchar(50) NOT NULL COMMENT '罪犯编号',
    `year` int NOT NULL COMMENT '考核年份',
    `month` int NOT NULL COMMENT '考核月份',
    `base_score` decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT '基础分',
    `reward_score` decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT '加分',
    `penalty_score` decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT '扣分',
    `total_score` decimal(10,2) NOT NULL COMMENT '总分',
    `level` tinyint DEFAULT NULL COMMENT '考核等级：1-优秀 2-良好 3-合格 4-不合格',
    `assessor_id` bigint DEFAULT NULL COMMENT '考核人ID',
    `assessor_name` varchar(50) DEFAULT NULL COMMENT '考核人姓名',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1-待审核 2-已通过 3-已驳回',
    `remark` varchar(500) DEFAULT NULL COMMENT '备注',
    `prison_area_id` bigint DEFAULT NULL COMMENT '监区ID',
    `prison_cell_id` bigint DEFAULT NULL COMMENT '监室ID',
    `creator` varchar(64) DEFAULT '' COMMENT '创建者',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updater` varchar(64) 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_prisoner_year_month` (`prisoner_no`, `year`, `month`),
    KEY `idx_prison_score_prisoner_id` (`prisoner_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='计分考核表';

-- =====================================================
-- 菜单权限 SQL
-- 注意: 请将 ${table.parentMenuId} 替换为实际的父菜单ID
-- 监狱管理模块的父菜单ID通常为 5047 (系统管理) 或对应的监狱模块菜单ID
-- =====================================================

-- 获取监狱模块父菜单ID (假设为 5047，请根据实际情况调整)
-- SELECT @parentId := 5047;

-- 1. 监区信息管理菜单
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status, component_name)
VALUES ('监区信息管理', '', 2, 1, 5047, 'area', '', 'prison/area/index', 0, 'Area');
SELECT @areaParentId := LAST_INSERT_ID();
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('监区信息查询', 'prison:area:query', 3, 1, @areaParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('监区信息创建', 'prison:area:create', 3, 2, @areaParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('监区信息更新', 'prison:area:update', 3, 3, @areaParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('监区信息删除', 'prison:area:delete', 3, 4, @areaParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('监区信息导出', 'prison:area:export', 3, 5, @areaParentId, '', '', '', 0);

-- 2. 监室信息管理菜单
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status, component_name)
VALUES ('监室信息管理', '', 2, 2, 5047, 'cell', '', 'prison/cell/index', 0, 'Cell');
SELECT @cellParentId := LAST_INSERT_ID();
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('监室信息查询', 'prison:cell:query', 3, 1, @cellParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('监室信息创建', 'prison:cell:create', 3, 2, @cellParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('监室信息更新', 'prison:cell:update', 3, 3, @cellParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('监室信息删除', 'prison:cell:delete', 3, 4, @cellParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('监室信息导出', 'prison:cell:export', 3, 5, @cellParentId, '', '', '', 0);

-- 3. 消费记录管理菜单
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status, component_name)
VALUES ('消费记录管理', '', 2, 3, 5047, 'consumption', '', 'prison/consumption/index', 0, 'Consumption');
SELECT @consumptionParentId := LAST_INSERT_ID();
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('消费记录查询', 'prison:consumption:query', 3, 1, @consumptionParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('消费记录创建', 'prison:consumption:create', 3, 2, @consumptionParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('消费记录更新', 'prison:consumption:update', 3, 3, @consumptionParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('消费记录删除', 'prison:consumption:delete', 3, 4, @consumptionParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('消费记录导出', 'prison:consumption:export', 3, 5, @consumptionParentId, '', '', '', 0);

-- 4. 问卷模板管理菜单
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status, component_name)
VALUES ('问卷模板管理', '', 2, 4, 5047, 'questionnaire', '', 'prison/questionnaire/index', 0, 'Questionnaire');
SELECT @questionnaireParentId := LAST_INSERT_ID();
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('问卷模板查询', 'prison:questionnaire:query', 3, 1, @questionnaireParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('问卷模板创建', 'prison:questionnaire:create', 3, 2, @questionnaireParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('问卷模板更新', 'prison:questionnaire:update', 3, 3, @questionnaireParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('问卷模板删除', 'prison:questionnaire:delete', 3, 4, @questionnaireParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('问卷模板导出', 'prison:questionnaire:export', 3, 5, @questionnaireParentId, '', '', '', 0);

-- 5. 问卷问题管理菜单 (已移除独立页面，问题管理集成在问卷模板页面内)
-- INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status, component_name)
-- VALUES ('问卷问题管理', '', 2, 5, 5047, 'question', '', 'prison/question/index', 0, 'Question');
-- SELECT @questionParentId := LAST_INSERT_ID();
-- INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
-- VALUES ('问卷问题查询', 'prison:question:query', 3, 1, @questionParentId, '', '', '', 0);
-- INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
-- VALUES ('问卷问题创建', 'prison:question:create', 3, 2, @questionParentId, '', '', '', 0);
-- INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
-- VALUES ('问卷问题更新', 'prison:question:update', 3, 3, @questionParentId, '', '', '', 0);
-- INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
-- VALUES ('问卷问题删除', 'prison:question:delete', 3, 4, @questionParentId, '', '', '', 0);
-- INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
-- VALUES ('问卷问题导出', 'prison:question:export', 3, 5, @questionParentId, '', '', '', 0);

-- 5. 问卷答题记录管理菜单
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status, component_name)
VALUES ('问卷答题记录管理', '', 2, 5, 5047, 'questionnaire-record', '', 'prison/questionnairerecord/index', 0, 'QuestionnaireRecord');
SELECT @recordParentId := LAST_INSERT_ID();
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('问卷答题记录查询', 'prison:questionnaire-record:query', 3, 1, @recordParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('问卷答题记录创建', 'prison:questionnaire-record:create', 3, 2, @recordParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('问卷答题记录更新', 'prison:questionnaire-record:update', 3, 3, @recordParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('问卷答题记录删除', 'prison:questionnaire-record:delete', 3, 4, @recordParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('问卷答题记录导出', 'prison:questionnaire-record:export', 3, 5, @recordParentId, '', '', '', 0);

-- 6. 危险评估管理菜单
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status, component_name)
VALUES ('危险评估管理', '', 2, 6, 5047, 'risk-assessment', '', 'prison/riskassessment/index', 0, 'RiskAssessment');
SELECT @riskParentId := LAST_INSERT_ID();
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('危险评估查询', 'prison:risk-assessment:query', 3, 1, @riskParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('危险评估创建', 'prison:risk-assessment:create', 3, 2, @riskParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('危险评估更新', 'prison:risk-assessment:update', 3, 3, @riskParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('危险评估删除', 'prison:risk-assessment:delete', 3, 4, @riskParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('危险评估导出', 'prison:risk-assessment:export', 3, 5, @riskParentId, '', '', '', 0);

-- 8. 计分考核管理菜单
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status, component_name)
VALUES ('计分考核管理', '', 2, 8, 5047, 'score', '', 'prison/score/index', 0, 'Score');
SELECT @scoreParentId := LAST_INSERT_ID();
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('计分考核查询', 'prison:score:query', 3, 1, @scoreParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('计分考核创建', 'prison:score:create', 3, 2, @scoreParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('计分考核更新', 'prison:score:update', 3, 3, @scoreParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('计分考核删除', 'prison:score:delete', 3, 4, @scoreParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('计分考核导出', 'prison:score:export', 3, 5, @scoreParentId, '', '', '', 0);

-- 9. 狱情收集管理菜单
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status, component_name)
VALUES ('狱情收集管理', '', 2, 9, 5047, 'situation', '', 'prison/situation/index', 0, 'Situation');
SELECT @situationParentId := LAST_INSERT_ID();
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('狱情收集查询', 'prison:situation:query', 3, 1, @situationParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('狱情收集创建', 'prison:situation:create', 3, 2, @situationParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('狱情收集更新', 'prison:situation:update', 3, 3, @situationParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('狱情收集删除', 'prison:situation:delete', 3, 4, @situationParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('狱情收集导出', 'prison:situation:export', 3, 5, @situationParentId, '', '', '', 0);

-- =====================================================
-- 10. 数据结构迁移 SQL (2026-01-20)
-- =====================================================
-- 补充 prison_prisoner 表缺失字段
-- 执行此迁移前请备份数据库
ALTER TABLE `prison_prisoner`
    ADD COLUMN IF NOT EXISTS `release_type` tinyint DEFAULT 0 COMMENT '释放类型：0-未知 1-刑满释放 2-假释 3-保外就医 4-减刑 5-暂予监外执行 6-特赦 7-死亡 8-其他',
    ADD COLUMN IF NOT EXISTS `release_reason` varchar(500) DEFAULT NULL COMMENT '释放原因',
    ADD COLUMN IF NOT EXISTS `photo` varchar(512) DEFAULT NULL COMMENT '照片URL',
    ADD COLUMN IF NOT EXISTS `sub_area_id` bigint DEFAULT NULL COMMENT '分区ID',
    ADD COLUMN IF NOT EXISTS `marital_status` tinyint DEFAULT NULL COMMENT '婚姻状态：1-未婚 2-已婚 3-离异 4-丧偶',
    ADD COLUMN IF NOT EXISTS `crime_type` varchar(100) DEFAULT NULL COMMENT '罪名类型',
    ADD COLUMN IF NOT EXISTS `sentence` varchar(100) DEFAULT NULL COMMENT '刑期';

-- =====================================================
-- 10. 字典数据 SQL
-- =====================================================
-- 监室状态字典
INSERT INTO system_dict_type (name, type, status, creator, create_time) VALUES ('监室状态', 'prison_cell_status', 0, 'admin', NOW());
INSERT INTO system_dict_data (dict_type, sort, label, value, color_type, css_class, status, creator, create_time) VALUES
('prison_cell_status', 1, '启用', '1', 'success', '', 0, 'admin', NOW()),
('prison_cell_status', 2, '禁用', '2', 'danger', '', 0, 'admin', NOW());

-- =====================================================
-- 11. 狱情收集表 (prison_situation) - 新增 2026-01-16
-- =====================================================
CREATE TABLE IF NOT EXISTS `prison_situation` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '狱情ID',
    `title` varchar(200) NOT NULL COMMENT '标题',
    `content` text COMMENT '详情内容',
    `category` tinyint NOT NULL DEFAULT 1 COMMENT '分类：1-监管安全 2-教育改造 3-生活卫生 4-生产安全 5-狱内案件 6-其他',
    `level` tinyint NOT NULL DEFAULT 1 COMMENT '等级：1-一般 2-重要 3-紧急',
    `source` tinyint NOT NULL DEFAULT 1 COMMENT '来源：1-民警报告 2-监控系统 3-举报 4-罪犯自首 5-其他',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态：1-待处理 2-处理中 3-已处理',
    `area_id` bigint DEFAULT NULL COMMENT '关联监区ID',
    `cell_id` bigint DEFAULT NULL COMMENT '关联监室ID',
    `reporter` varchar(50) DEFAULT NULL COMMENT '报告人',
    `handler` varchar(50) DEFAULT NULL COMMENT '处理人',
    `handle_time` datetime DEFAULT NULL COMMENT '处理时间',
    `handle_result` text COMMENT '处理结果',
    `occur_time` datetime DEFAULT NULL COMMENT '发生时间',
    `remark` varchar(500) DEFAULT NULL COMMENT '备注',
    `creator` varchar(64) DEFAULT '' COMMENT '创建者',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updater` varchar(64) 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_prison_situation_status` (`status`),
    KEY `idx_prison_situation_category` (`category`),
    KEY `idx_prison_situation_level` (`level`),
    KEY `idx_prison_situation_area_id` (`area_id`),
    KEY `idx_prison_situation_cell_id` (`cell_id`),
    KEY `idx_prison_situation_occur_time` (`occur_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='狱情收集表';

-- =====================================================
-- 12. 预警管理表 (prison_warning) - 新增 2026-01-16
-- =====================================================
CREATE TABLE IF NOT EXISTS `prison_warning` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '预警ID',
    `title` varchar(200) NOT NULL COMMENT '预警标题',
    `content` text COMMENT '预警内容',
    `type` tinyint NOT NULL DEFAULT 1 COMMENT '预警类型：1-安全预警 2-监管预警 3-改造预警 4-生产预警 5-生活卫生预警 6-其他',
    `level` tinyint NOT NULL DEFAULT 1 COMMENT '预警等级：1-一般 2-重要 3-紧急 4-严重',
    `status` tinyint NOT NULL DEFAULT 1 COMMENT '预警状态：1-待核实 2-已核实 3-已处置 4-已解除',
    `source` tinyint NOT NULL DEFAULT 1 COMMENT '预警来源：1-民警报告 2-监控系统 3-举报 4-罪犯自首 5-智能分析 6-其他',
    `situation_id` bigint DEFAULT NULL COMMENT '关联狱情ID',
    `area_id` bigint DEFAULT NULL COMMENT '关联监区ID',
    `cell_id` bigint DEFAULT NULL COMMENT '关联监室ID',
    `alert_time` datetime DEFAULT NULL COMMENT '预警时间',
    `verify_time` datetime DEFAULT NULL COMMENT '核实时间',
    `verifier` varchar(50) DEFAULT NULL COMMENT '核实人',
    `verify_result` text COMMENT '核实结果',
    `handle_time` datetime DEFAULT NULL COMMENT '处置时间',
    `handler` varchar(50) DEFAULT NULL COMMENT '处置人',
    `handle_method` varchar(200) DEFAULT NULL COMMENT '处置方式',
    `handle_result` text COMMENT '处置结果',
    `release_time` datetime DEFAULT NULL COMMENT '解除时间',
    `releaser` varchar(50) DEFAULT NULL COMMENT '解除人',
    `release_reason` text COMMENT '解除原因',
    `occur_time` datetime DEFAULT NULL COMMENT '发生时间',
    `remark` varchar(500) DEFAULT NULL COMMENT '备注',
    `creator` varchar(64) DEFAULT '' COMMENT '创建者',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updater` varchar(64) 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_prison_warning_status` (`status`),
    KEY `idx_prison_warning_level` (`level`),
    KEY `idx_prison_warning_type` (`type`),
    KEY `idx_prison_warning_situation_id` (`situation_id`),
    KEY `idx_prison_warning_area_id` (`area_id`),
    KEY `idx_prison_warning_cell_id` (`cell_id`),
    KEY `idx_prison_warning_alert_time` (`alert_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='预警管理表';

-- =====================================================
-- 13. 风险评估表 (prison_risk) - 新增 2026-01-16
-- =====================================================
CREATE TABLE IF NOT EXISTS `prison_risk` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '评估ID',
    `prisoner_id` bigint NOT NULL COMMENT '罪犯ID',
    `prisoner_no` varchar(50) NOT NULL COMMENT '罪犯编号',
    `prisoner_name` varchar(50) DEFAULT NULL COMMENT '罪犯姓名',
    `assessment_type` tinyint NOT NULL DEFAULT 1 COMMENT '评估类型：1-入监评估 2-定期评估 3-专项评估 4-出监评估',
    `assessment_date` date NOT NULL COMMENT '评估日期',
    `overall_score` decimal(5,2) DEFAULT NULL COMMENT '综合风险得分',
    `risk_level` tinyint DEFAULT NULL COMMENT '风险等级：1-低风险 2-中风险 3-高风险 4-极高风险',
    `mental_state` varchar(500) DEFAULT NULL COMMENT '精神状态评估',
    `escape_risk` varchar(500) DEFAULT NULL COMMENT '脱逃风险评估',
    `violence_risk` varchar(500) DEFAULT NULL COMMENT '暴力倾向评估',
    `revolt_risk` varchar(500) DEFAULT NULL COMMENT '抗改风险评估',
    `self_harm_risk` varchar(500) DEFAULT NULL COMMENT '自杀自伤风险评估',
    `recommendation` text COMMENT '评估建议',
    `assessor` varchar(50) DEFAULT NULL COMMENT '评估人',
    `assess_method` tinyint DEFAULT NULL COMMENT '评估方式：1-问卷评估 2-量表评估 3-综合评估',
    `item_scores` text COMMENT '评估项目得分JSON',
    `conclusion` text COMMENT '评估结论',
    `remark` varchar(500) DEFAULT NULL COMMENT '备注',
    `creator` varchar(64) DEFAULT '' COMMENT '创建者',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updater` varchar(64) 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_prison_risk_prisoner_id` (`prisoner_id`),
    KEY `idx_prison_risk_prisoner_no` (`prisoner_no`),
    KEY `idx_prison_risk_assessment_type` (`assessment_type`),
    KEY `idx_prison_risk_assessment_date` (`assessment_date`),
    KEY `idx_prison_risk_risk_level` (`risk_level`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='风险评估表';

-- =====================================================
-- 14. 字典数据 SQL (2026-01-16)
-- =====================================================

-- 狱情分类字典
INSERT INTO system_dict_type (name, type, status, creator, create_time) VALUES ('狱情分类', 'prison_situation_category', 0, 'admin', NOW());
INSERT INTO system_dict_data (dict_type, sort, label, value, color_type, css_class, status, creator, create_time) VALUES
('prison_situation_category', 1, '监管安全', '1', 'danger', '', 0, 'admin', NOW()),
('prison_situation_category', 2, '教育改造', '2', 'warning', '', 0, 'admin', NOW()),
('prison_situation_category', 3, '生活卫生', '3', 'success', '', 0, 'admin', NOW()),
('prison_situation_category', 4, '生产安全', '4', 'info', '', 0, 'admin', NOW()),
('prison_situation_category', 5, '狱内案件', '5', 'danger', '', 0, 'admin', NOW()),
('prison_situation_category', 6, '其他', '6', '', '', 0, 'admin', NOW());

-- 狱情等级字典
INSERT INTO system_dict_type (name, type, status, creator, create_time) VALUES ('狱情等级', 'prison_situation_level', 0, 'admin', NOW());
INSERT INTO system_dict_data (dict_type, sort, label, value, color_type, css_class, status, creator, create_time) VALUES
('prison_situation_level', 1, '一般', '1', 'success', '', 0, 'admin', NOW()),
('prison_situation_level', 2, '重要', '2', 'warning', '', 0, 'admin', NOW()),
('prison_situation_level', 3, '紧急', '3', 'danger', '', 0, 'admin', NOW());

-- 狱情来源字典
INSERT INTO system_dict_type (name, type, status, creator, create_time) VALUES ('狱情来源', 'prison_situation_source', 0, 'admin', NOW());
INSERT INTO system_dict_data (dict_type, sort, label, value, color_type, css_class, status, creator, create_time) VALUES
('prison_situation_source', 1, '民警报告', '1', '', '', 0, 'admin', NOW()),
('prison_situation_source', 2, '监控系统', '2', '', '', 0, 'admin', NOW()),
('prison_situation_source', 3, '举报', '3', '', '', 0, 'admin', NOW()),
('prison_situation_source', 4, '罪犯自首', '4', '', '', 0, 'admin', NOW()),
('prison_situation_source', 5, '其他', '5', '', '', 0, 'admin', NOW());

-- 狱情状态字典
INSERT INTO system_dict_type (name, type, status, creator, create_time) VALUES ('狱情状态', 'prison_situation_status', 0, 'admin', NOW());
INSERT INTO system_dict_data (dict_type, sort, label, value, color_type, css_class, status, creator, create_time) VALUES
('prison_situation_status', 1, '待处理', '1', 'warning', '', 0, 'admin', NOW()),
('prison_situation_status', 2, '处理中', '2', 'info', '', 0, 'admin', NOW()),
('prison_situation_status', 3, '已处理', '3', 'success', '', 0, 'admin', NOW());

-- 预警类型字典
INSERT INTO system_dict_type (name, type, status, creator, create_time) VALUES ('预警类型', 'prison_warning_type', 0, 'admin', NOW());
INSERT INTO system_dict_data (dict_type, sort, label, value, color_type, css_class, status, creator, create_time) VALUES
('prison_warning_type', 1, '安全预警', '1', 'danger', '', 0, 'admin', NOW()),
('prison_warning_type', 2, '监管预警', '2', 'warning', '', 0, 'admin', NOW()),
('prison_warning_type', 3, '改造预警', '3', 'info', '', 0, 'admin', NOW()),
('prison_warning_type', 4, '生产预警', '4', '', '', 0, 'admin', NOW()),
('prison_warning_type', 5, '生活卫生预警', '5', 'success', '', 0, 'admin', NOW()),
('prison_warning_type', 6, '其他', '6', '', '', 0, 'admin', NOW());

-- 预警等级字典
INSERT INTO system_dict_type (name, type, status, creator, create_time) VALUES ('预警等级', 'prison_warning_level', 0, 'admin', NOW());
INSERT INTO system_dict_data (dict_type, sort, label, value, color_type, css_class, status, creator, create_time) VALUES
('prison_warning_level', 1, '一般', '1', 'success', '', 0, 'admin', NOW()),
('prison_warning_level', 2, '重要', '2', 'warning', '', 0, 'admin', NOW()),
('prison_warning_level', 3, '紧急', '3', 'danger', '', 0, 'admin', NOW()),
('prison_warning_level', 4, '严重', '4', 'danger', '', 0, 'admin', NOW());

-- 预警状态字典
INSERT INTO system_dict_type (name, type, status, creator, create_time) VALUES ('预警状态', 'prison_warning_status', 0, 'admin', NOW());
INSERT INTO system_dict_data (dict_type, sort, label, value, color_type, css_class, status, creator, create_time) VALUES
('prison_warning_status', 1, '待核实', '1', 'warning', '', 0, 'admin', NOW()),
('prison_warning_status', 2, '已核实', '2', 'info', '', 0, 'admin', NOW()),
('prison_warning_status', 3, '已处置', '3', 'success', '', 0, 'admin', NOW()),
('prison_warning_status', 4, '已解除', '4', '', '', 0, 'admin', NOW());

-- 预警来源字典
INSERT INTO system_dict_type (name, type, status, creator, create_time) VALUES ('预警来源', 'prison_warning_source', 0, 'admin', NOW());
INSERT INTO system_dict_data (dict_type, sort, label, value, color_type, css_class, status, creator, create_time) VALUES
('prison_warning_source', 1, '民警报告', '1', '', '', 0, 'admin', NOW()),
('prison_warning_source', 2, '监控系统', '2', '', '', 0, 'admin', NOW()),
('prison_warning_source', 3, '举报', '3', '', '', 0, 'admin', NOW()),
('prison_warning_source', 4, '罪犯自首', '4', '', '', 0, 'admin', NOW()),
('prison_warning_source', 5, '智能分析', '5', '', '', 0, 'admin', NOW()),
('prison_warning_source', 6, '其他', '6', '', '', 0, 'admin', NOW());

-- 风险评估类型字典
INSERT INTO system_dict_type (name, type, status, creator, create_time) VALUES ('风险评估类型', 'prison_risk_assessment_type', 0, 'admin', NOW());
INSERT INTO system_dict_data (dict_type, sort, label, value, color_type, css_class, status, creator, create_time) VALUES
('prison_risk_assessment_type', 1, '入监评估', '1', 'info', '', 0, 'admin', NOW()),
('prison_risk_assessment_type', 2, '定期评估', '2', 'success', '', 0, 'admin', NOW()),
('prison_risk_assessment_type', 3, '专项评估', '3', 'warning', '', 0, 'admin', NOW()),
('prison_risk_assessment_type', 4, '出监评估', '4', '', '', 0, 'admin', NOW());

-- 风险等级字典
INSERT INTO system_dict_type (name, type, status, creator, create_time) VALUES ('风险等级', 'prison_risk_level', 0, 'admin', NOW());
INSERT INTO system_dict_data (dict_type, sort, label, value, color_type, css_class, status, creator, create_time) VALUES
('prison_risk_level', 1, '低风险', '1', 'success', '', 0, 'admin', NOW()),
('prison_risk_level', 2, '中风险', '2', 'warning', '', 0, 'admin', NOW()),
('prison_risk_level', 3, '高风险', '3', 'danger', '', 0, 'admin', NOW()),
('prison_risk_level', 4, '极高风险', '4', 'danger', '', 0, 'admin', NOW());

-- 评估方式字典
INSERT INTO system_dict_type (name, type, status, creator, create_time) VALUES ('评估方式', 'prison_risk_assess_method', 0, 'admin', NOW());
INSERT INTO system_dict_data (dict_type, sort, label, value, color_type, css_class, status, creator, create_time) VALUES
('prison_risk_assess_method', 1, '问卷评估', '1', '', '', 0, 'admin', NOW()),
('prison_risk_assess_method', 2, '量表评估', '2', '', '', 0, 'admin', NOW()),
('prison_risk_assess_method', 3, '综合评估', '3', '', '', 0, 'admin', NOW());
