-- =====================================================
-- 数据库字符集修复脚本
-- 用途：修复已有数据库中 COMMENT 乱码问题
-- 原因：建表时未指定 DEFAULT CHARSET=utf8mb4，
--       导致表继承数据库默认字符集（如 latin1），
--       中文 COMMENT 在存储时被错误编码
-- 用法：连接 MySQL 后执行此脚本
-- 注意：建议先备份数据库！
-- =====================================================

-- 1. 先修改数据库默认字符集（影响后续新建表）
ALTER DATABASE risk_control CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

-- =====================================================
-- 2. 修改风控模块表（riskctrl.sql）
-- =====================================================
ALTER TABLE risk_rule          CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE risk_event         CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE risk_case          CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE risk_blacklist     CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE risk_whitelist     CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE risk_score         CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

-- =====================================================
-- 3. 修改系统核心表（init.sql / ry_vue_5.X.sql）
-- =====================================================
ALTER TABLE sys_social         CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_tenant         CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_tenant_package CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_dept           CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_user           CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_post           CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_role           CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_menu           CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_user_role      CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_role_menu      CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_role_dept      CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_user_post      CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_oper_log       CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_dict_type      CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_dict_data      CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_config         CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_logininfor     CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_notice         CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE gen_table          CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE gen_table_column   CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_oss            CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_oss_config     CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE sys_client         CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

-- =====================================================
-- 4. 修改安全/业务模块表（已有 utf8mb4 的可跳过，但执行也不会出错）
-- =====================================================
-- 安全隐患
ALTER TABLE safety_hazard      CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 危险源
ALTER TABLE danger_source      CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 作业上报
ALTER TABLE work_report        CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 人员变动
ALTER TABLE audit_person_change CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 风险警示
ALTER TABLE risk_warning       CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 特种设备
ALTER TABLE special_equipment  CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 设备维护
ALTER TABLE equipment_maintenance CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 舆情
ALTER TABLE public_opinion     CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 事故上报
ALTER TABLE accident_report    CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 巡检点
ALTER TABLE patrol_point       CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 巡检记录
ALTER TABLE patrol_record      CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 通知公告
ALTER TABLE notice_announcement CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 人员信息
ALTER TABLE person_info        CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
-- 安全培训
ALTER TABLE safety_training    CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
