-- 设置字符集
SET NAMES utf8mb4;
SET CHARACTER SET utf8mb4;

-- 1. 添加权限管理父菜单
INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `route_url`, `page_url`, `authority`, `icon`, `sort`, `is_delete`, `create_time`)
VALUES (0, 1, '权限管理', '/permission', null, 'permission:view', 'el-icon-lock', 20, 0, NOW());

SET @permission_parent_id = LAST_INSERT_ID();

-- 2. 添加菜单权限管理子菜单
INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `route_url`, `page_url`, `authority`, `icon`, `sort`, `is_delete`, `create_time`)
VALUES (@permission_parent_id, 1, '菜单权限管理', '/permission/menu', 'permission/menuPermission', 'permission:menu:view', 'el-icon-menu', 1, 0, NOW());

SET @menu_permission_id = LAST_INSERT_ID();

-- 菜单权限管理的按钮权限
INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `authority`, `sort`, `is_delete`, `create_time`) VALUES
(@menu_permission_id, 2, '菜单查询', 'permission:menu:list', 1, 0, NOW()),
(@menu_permission_id, 2, '菜单新增', 'permission:menu:add', 2, 0, NOW()),
(@menu_permission_id, 2, '菜单编辑', 'permission:menu:edit', 3, 0, NOW()),
(@menu_permission_id, 2, '菜单删除', 'permission:menu:delete', 4, 0, NOW()),
(@menu_permission_id, 2, '角色分配', 'permission:menu:assign', 5, 0, NOW());

-- 3. 添加操作权限管理子菜单
INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `route_url`, `page_url`, `authority`, `icon`, `sort`, `is_delete`, `create_time`)
VALUES (@permission_parent_id, 1, '操作权限管理', '/permission/operation', 'permission/operationPermission', 'permission:operation:view', 'el-icon-setting', 2, 0, NOW());

SET @operation_permission_id = LAST_INSERT_ID();

-- 操作权限管理的按钮权限
INSERT INTO `sys_menu` (`parent_id`, `type`, `name`, `authority`, `sort`, `is_delete`, `create_time`) VALUES
(@operation_permission_id, 2, '权限查询', 'permission:operation:list', 1, 0, NOW()),
(@operation_permission_id, 2, '权限分配', 'permission:operation:assign', 2, 0, NOW()),
(@operation_permission_id, 2, '角色管理', 'permission:operation:role', 3, 0, NOW());

-- 4. 为超级管理员分配所有菜单权限
DELETE FROM `sys_role_menu` WHERE `role_id` = 1;
INSERT INTO `sys_role_menu` (`role_id`, `menu_id`)
SELECT 1, id FROM `sys_menu` WHERE `is_delete` = 0;

-- 验证
SELECT '菜单创建完成' as status;
SELECT id, parent_id, name, route_url FROM sys_menu WHERE id >= 17 ORDER BY id;

