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

-- =====================================================
-- 9. 狱情收集表 (prison_situation)
-- =====================================================
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 AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='狱情收集表';

-- =====================================================
-- 10. 预警管理表 (prison_warning)
-- =====================================================
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 AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='预警管理表';

-- =====================================================
-- 11. 风险评估表 (prison_risk)
-- =====================================================
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 AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='风险评估表';

-- =====================================================
-- 菜单权限 SQL (2026-01-16)
-- 注意: 请将 5047 替换为实际的父菜单ID
-- =====================================================

-- 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. 预警管理菜单
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status, component_name)
VALUES ('预警管理', '', 2, 10, 5047, 'warning', '', 'prison/warning/index', 0, 'Warning');
SELECT @warningParentId := LAST_INSERT_ID();
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('预警查询', 'prison:warning:query', 3, 1, @warningParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('预警创建', 'prison:warning:create', 3, 2, @warningParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('预警更新', 'prison:warning:update', 3, 3, @warningParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('预警删除', 'prison:warning:delete', 3, 4, @warningParentId, '', '', '', 0);
INSERT INTO system_menu (name, permission, type, sort, parent_id, path, icon, component, status)
VALUES ('预警导出', 'prison:warning:export', 3, 5, @warningParentId, '', '', '', 0);

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

-- =====================================================
-- 字典数据 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());
