SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- 重组系统管理菜单结构
-- 1. 删除版本管理独立菜单（1282）及其子菜单（1283-1287）
-- 2. 创建APP管理菜单（系统管理下）
-- 3. 创建群众消息菜单（系统管理下）
-- 4. 重命名协议管理为APP协议管理
-- ----------------------------
BEGIN;

-- 1. 删除版本管理独立菜单及其子菜单的权限关联
DELETE FROM system_role_menu WHERE menu_id IN (1282, 1283, 1284, 1285, 1286, 1287);

-- 2. 删除版本管理独立菜单及其子菜单
DELETE FROM system_menu WHERE id IN (1282, 1283, 1284, 1285, 1286, 1287);

-- 3. 创建APP管理菜单（系统管理下，parent_id=1）
INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES 
(2080, 'APP管理', 'system:app:query', 2, 12, 1, 'app', 'ep:mobile', 'system/app/index', 'SystemApp', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0')
ON DUPLICATE KEY UPDATE
    `name` = VALUES(`name`),
    `permission` = VALUES(`permission`),
    `type` = VALUES(`type`),
    `sort` = VALUES(`sort`),
    `parent_id` = VALUES(`parent_id`),
    `path` = VALUES(`path`),
    `icon` = VALUES(`icon`),
    `component` = VALUES(`component`),
    `component_name` = VALUES(`component_name`),
    `status` = VALUES(`status`),
    `visible` = VALUES(`visible`),
    `keep_alive` = VALUES(`keep_alive`),
    `always_show` = VALUES(`always_show`),
    `updater` = 'admin',
    `update_time` = NOW(),
    `deleted` = b'0';

-- 4. 创建APP管理的按钮权限（复用版本管理的权限）
INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES 
(2081, '版本查询', 'system:app:query', 3, 1, 2080, '', '', NULL, NULL, 0, b'0', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(2082, '版本新增', 'system:app:create', 3, 2, 2080, '', '', NULL, NULL, 0, b'0', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(2083, '版本修改', 'system:app:update', 3, 3, 2080, '', '', NULL, NULL, 0, b'0', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(2084, '版本删除', 'system:app:delete', 3, 4, 2080, '', '', NULL, NULL, 0, b'0', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(2085, '版本导出', 'system:app:export', 3, 5, 2080, '', '', NULL, NULL, 0, b'0', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0')
ON DUPLICATE KEY UPDATE
    `name` = VALUES(`name`),
    `permission` = VALUES(`permission`),
    `type` = VALUES(`type`),
    `sort` = VALUES(`sort`),
    `parent_id` = VALUES(`parent_id`),
    `updater` = 'admin',
    `update_time` = NOW(),
    `deleted` = b'0';

-- 5. 创建群众消息菜单（系统管理下，parent_id=1）
INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES 
(2086, '群众消息', 'system:message:query', 2, 13, 1, 'message', 'ep:message', 'system/message/index', 'SystemMessage', 0, b'1', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0')
ON DUPLICATE KEY UPDATE
    `name` = VALUES(`name`),
    `permission` = VALUES(`permission`),
    `type` = VALUES(`type`),
    `sort` = VALUES(`sort`),
    `parent_id` = VALUES(`parent_id`),
    `path` = VALUES(`path`),
    `icon` = VALUES(`icon`),
    `component` = VALUES(`component`),
    `component_name` = VALUES(`component_name`),
    `status` = VALUES(`status`),
    `visible` = VALUES(`visible`),
    `keep_alive` = VALUES(`keep_alive`),
    `always_show` = VALUES(`always_show`),
    `updater` = 'admin',
    `update_time` = NOW(),
    `deleted` = b'0';

-- 6. 创建群众消息的按钮权限
INSERT INTO `system_menu` (`id`, `name`, `permission`, `type`, `sort`, `parent_id`, `path`, `icon`, `component`, `component_name`, `status`, `visible`, `keep_alive`, `always_show`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES 
(2087, '消息查询', 'system:message:query', 3, 1, 2086, '', '', NULL, NULL, 0, b'0', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(2088, '消息新增', 'system:message:create', 3, 2, 2086, '', '', NULL, NULL, 0, b'0', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(2089, '消息修改', 'system:message:update', 3, 3, 2086, '', '', NULL, NULL, 0, b'0', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0'),
(2090, '消息删除', 'system:message:delete', 3, 4, 2086, '', '', NULL, NULL, 0, b'0', b'1', b'1', 'admin', NOW(), 'admin', NOW(), b'0')
ON DUPLICATE KEY UPDATE
    `name` = VALUES(`name`),
    `permission` = VALUES(`permission`),
    `type` = VALUES(`type`),
    `sort` = VALUES(`sort`),
    `parent_id` = VALUES(`parent_id`),
    `updater` = 'admin',
    `update_time` = NOW(),
    `deleted` = b'0';

-- 7. 重命名协议管理为APP协议管理（id=2073）
UPDATE system_menu SET 
    `name` = 'APP协议管理',
    `sort` = 14,
    `updater` = 'admin',
    `update_time` = NOW()
WHERE id = 2073;

-- 8. 确保所有菜单权限分配给超级管理员角色
INSERT INTO system_role_menu (role_id, menu_id) 
SELECT 1, id FROM system_menu WHERE id IN (2080, 2081, 2082, 2083, 2084, 2085, 2086, 2087, 2088, 2089, 2090, 2073)
AND id NOT IN (SELECT menu_id FROM system_role_menu WHERE role_id = 1);

COMMIT;
SET FOREIGN_KEY_CHECKS = 1;

