-- =====================================================
-- 风控平台菜单及字典更新 SQL
-- 人员信息菜单 + 字典数据修正
-- =====================================================

-- 获取当前最大 menu_id
SET @max_menu_id = (SELECT IFNULL(MAX(menu_id), 0) FROM sys_menu);

-- 找到安全生产风控一级菜单ID
SET @parent_id = (SELECT menu_id FROM sys_menu WHERE menu_name = '安全生产风控' AND parent_id = 0 LIMIT 1);

-- 如果找不到，用最大ID+1作为父菜单
SET @parent_id = IFNULL(@parent_id, @max_menu_id + 1);

-- 添加人员信息菜单（如果不存在）
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT @max_menu_id + 100, '人员信息', @parent_id, 3, 'personInfo', 'riskctrl/personInfo/index', 1, 0, 'C', '0', '0', 'riskctrl:personInfo:list', 'el-icon-user-filled', 1, NOW(), 1, NOW(), '人员信息'
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE menu_name = '人员信息' AND parent_id = @parent_id);

-- 添加人员信息按钮权限
SET @person_menu_id = (SELECT menu_id FROM sys_menu WHERE menu_name = '人员信息' AND parent_id = @parent_id LIMIT 1);

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT @max_menu_id + 101, '人员新增', @person_menu_id, 1, '', NULL, 1, 0, 'F', '0', '0', 'riskctrl:personInfo:add', '#', 1, NOW(), 1, NOW(), NULL
FROM DUAL
WHERE @person_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'riskctrl:personInfo:add');

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT @max_menu_id + 102, '人员编辑', @person_menu_id, 2, '', NULL, 1, 0, 'F', '0', '0', 'riskctrl:personInfo:edit', '#', 1, NOW(), 1, NOW(), NULL
FROM DUAL
WHERE @person_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'riskctrl:personInfo:edit');

INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT @max_menu_id + 103, '人员删除', @person_menu_id, 3, '', NULL, 1, 0, 'F', '0', '0', 'riskctrl:personInfo:remove', '#', 1, NOW(), 1, NOW(), NULL
FROM DUAL
WHERE @person_menu_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'riskctrl:personInfo:remove');

-- ==================== 字典数据修正 ====================

-- 修正隐患类型字典（根据需求规格说明书）
DELETE FROM sys_dict_data WHERE dict_type = 'hazard_type';
INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, create_by, create_time, update_by, update_time, remark) VALUES
(@max_menu_id + 200, 1, '门店安全隐患', '门店安全隐患', 'hazard_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 201, 2, '物流安全隐患', '物流安全隐患', 'hazard_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 202, 3, '市场安全隐患', '市场安全隐患', 'hazard_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL);

-- 修正风险等级字典（根据需求规格说明书：高风险、中风险、低风险）
DELETE FROM sys_dict_data WHERE dict_type = 'risk_level';
INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, create_by, create_time, update_by, update_time, remark) VALUES
(@max_menu_id + 203, 1, '高风险', '高风险', 'risk_level', '', 'danger', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 204, 2, '中风险', '中风险', 'risk_level', '', 'warning', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 205, 3, '低风险', '低风险', 'risk_level', '', 'info', 'N', 1, NOW(), 1, NOW(), NULL);

-- 修正危险源类型字典
DELETE FROM sys_dict_data WHERE dict_type = 'danger_source_type';
INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, create_by, create_time, update_by, update_time, remark) VALUES
(@max_menu_id + 206, 1, '电气危险源', '电气危险源', 'danger_source_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 207, 2, '消防危险源', '消防危险源', 'danger_source_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 208, 3, '高空危险源', '高空危险源', 'danger_source_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 209, 4, '设备危险源', '设备危险源', 'danger_source_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 210, 5, '食品危险源', '食品危险源', 'danger_source_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL);

-- 修正作业类型字典
DELETE FROM sys_dict_data WHERE dict_type = 'work_type';
INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, create_by, create_time, update_by, update_time, remark) VALUES
(@max_menu_id + 211, 1, '临时施工上报', '临时施工上报', 'work_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 212, 2, '改造工程上报', '改造工程上报', 'work_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL);

-- 修正审计变动类型字典
DELETE FROM sys_dict_data WHERE dict_type = 'audit_change_type';
INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, create_by, create_time, update_by, update_time, remark) VALUES
(@max_menu_id + 213, 1, '调任', '调任', 'audit_change_type', '', 'primary', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 214, 2, '离职', '离职', 'audit_change_type', '', 'danger', 'N', 1, NOW(), 1, NOW(), NULL);

-- 修正警示类型字典
DELETE FROM sys_dict_data WHERE dict_type = 'warning_type';
INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, create_by, create_time, update_by, update_time, remark) VALUES
(@max_menu_id + 215, 1, '安全警示', '安全警示', 'warning_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 216, 2, '食安警示', '食安警示', 'warning_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 217, 3, '社会警示', '社会警示', 'warning_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL);

-- 修正警示等级字典
DELETE FROM sys_dict_data WHERE dict_type = 'warning_level';
INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, create_by, create_time, update_by, update_time, remark) VALUES
(@max_menu_id + 218, 1, '重大风险', '重大风险', 'warning_level', '', 'danger', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 219, 2, '较大风险', '较大风险', 'warning_level', '', 'warning', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 220, 3, '一般风险', '一般风险', 'warning_level', '', 'primary', 'N', 1, NOW(), 1, NOW(), NULL);

-- 修正设备类型字典
DELETE FROM sys_dict_data WHERE dict_type = 'equipment_type';
INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, create_by, create_time, update_by, update_time, remark) VALUES
(@max_menu_id + 221, 1, '电梯', '电梯', 'equipment_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 222, 2, '压力容器', '压力容器', 'equipment_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 223, 3, '起重机械', '起重机械', 'equipment_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 224, 4, '叉车', '叉车', 'equipment_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 225, 5, '锅炉', '锅炉', 'equipment_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL);

-- 修正舆情来源字典
DELETE FROM sys_dict_data WHERE dict_type = 'opinion_source';
INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, create_by, create_time, update_by, update_time, remark) VALUES
(@max_menu_id + 226, 1, '微博', '微博', 'opinion_source', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 227, 2, '微信', '微信', 'opinion_source', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 228, 3, '新闻网站', '新闻网站', 'opinion_source', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 229, 4, '论坛', '论坛', 'opinion_source', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 230, 5, '短视频', '短视频', 'opinion_source', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL);

-- 修正舆情风险等级字典
DELETE FROM sys_dict_data WHERE dict_type = 'opinion_risk_level';
INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, create_by, create_time, update_by, update_time, remark) VALUES
(@max_menu_id + 231, 1, '重大', '重大', 'opinion_risk_level', '', 'danger', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 232, 2, '较大', '较大', 'opinion_risk_level', '', 'warning', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 233, 3, '一般', '一般', 'opinion_risk_level', '', 'primary', 'N', 1, NOW(), 1, NOW(), NULL);

-- 修正事故等级字典
DELETE FROM sys_dict_data WHERE dict_type = 'accident_level';
INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, create_by, create_time, update_by, update_time, remark) VALUES
(@max_menu_id + 234, 1, '特大事故', '特大事故', 'accident_level', '', 'danger', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 235, 2, '重大事故', '重大事故', 'accident_level', '', 'warning', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 236, 3, '较大事故', '较大事故', 'accident_level', '', 'primary', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 237, 4, '一般事故', '一般事故', 'accident_level', '', 'info', 'N', 1, NOW(), 1, NOW(), NULL);

-- 修正事故类型字典
DELETE FROM sys_dict_data WHERE dict_type = 'accident_type';
INSERT INTO sys_dict_data (dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, create_by, create_time, update_by, update_time, remark) VALUES
(@max_menu_id + 238, 1, '高处坠落', '高处坠落', 'accident_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 239, 2, '物体打击', '物体打击', 'accident_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 240, 3, '机械伤害', '机械伤害', 'accident_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 241, 4, '触电', '触电', 'accident_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 242, 5, '火灾', '火灾', 'accident_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL),
(@max_menu_id + 243, 6, '坍塌', '坍塌', 'accident_type', '', 'default', 'N', 1, NOW(), 1, NOW(), NULL);

-- 更新字典类型名称
UPDATE sys_dict_type SET dict_name = '隐患类型' WHERE dict_type = 'hazard_type';
UPDATE sys_dict_type SET dict_name = '风险等级' WHERE dict_type = 'risk_level';
UPDATE sys_dict_type SET dict_name = '危险源类型' WHERE dict_type = 'danger_source_type';
UPDATE sys_dict_type SET dict_name = '作业类型' WHERE dict_type = 'work_type';
UPDATE sys_dict_type SET dict_name = '人员变动类型' WHERE dict_type = 'audit_change_type';
UPDATE sys_dict_type SET dict_name = '警示类型' WHERE dict_type = 'warning_type';
UPDATE sys_dict_type SET dict_name = '警示等级' WHERE dict_type = 'warning_level';
UPDATE sys_dict_type SET dict_name = '设备类型' WHERE dict_type = 'equipment_type';
UPDATE sys_dict_type SET dict_name = '舆情来源' WHERE dict_type = 'opinion_source';
UPDATE sys_dict_type SET dict_name = '舆情风险等级' WHERE dict_type = 'opinion_risk_level';
UPDATE sys_dict_type SET dict_name = '事故等级' WHERE dict_type = 'accident_level';
UPDATE sys_dict_type SET dict_name = '事故类型' WHERE dict_type = 'accident_type';
