-- 添加礼物管理菜单到后台管理系统

-- 1. 查看现有菜单，找到合适的插入位置
SELECT id, pid, name, component, sort, menu_type FROM eb_system_menu WHERE is_delte = 0 ORDER BY sort;

-- 2. 添加礼物管理父菜单（目录类型）
INSERT INTO eb_system_menu (pid, name, icon, perms, component, menu_type, sort, is_show, is_delte, create_time, update_time) 
VALUES (0, '礼物管理', 'el-icon-present', '', 'Layout', 'M', 100, 1, 0, NOW(), NOW());

-- 3. 获取刚插入的父菜单ID
SET @parent_id = LAST_INSERT_ID();

-- 4. 添加礼物打赏子菜单（菜单类型）
INSERT INTO eb_system_menu (pid, name, icon, perms, component, menu_type, sort, is_show, is_delte, create_time, update_time) 
VALUES (@parent_id, '礼物打赏', 'el-icon-present', 'admin:gift:records', 'gift/records/index', 'C', 1, 1, 0, NOW(), NOW());

-- 5. 验证菜单是否添加成功
SELECT 
    m1.id as parent_id, 
    m1.name as parent_name, 
    m1.menu_type as parent_type,
    m2.id as child_id, 
    m2.name as child_name, 
    m2.component,
    m2.menu_type as child_type
FROM eb_system_menu m1
LEFT JOIN eb_system_menu m2 ON m1.id = m2.pid
WHERE m1.name = '礼物管理' AND m1.is_delte = 0;

-- 6. 查看所有礼物相关菜单
SELECT id, pid, name, icon, component, menu_type, sort, is_show 
FROM eb_system_menu 
WHERE name LIKE '%礼物%' AND is_delte = 0 
ORDER BY pid, sort;

-- 7. 如果需要给管理员角色分配权限（假设管理员角色ID为1）
-- 先查看角色ID
SELECT * FROM eb_system_role;

-- 获取礼物菜单的ID
SELECT @gift_parent_id := id FROM eb_system_menu WHERE name = '礼物管理' AND is_delte = 0 LIMIT 1;
SELECT @gift_child_id := id FROM eb_system_menu WHERE name = '礼物打赏' AND is_delte = 0 LIMIT 1;

-- 给管理员角色（rid=1）分配礼物管理权限
INSERT INTO eb_system_role_menu (rid, menu_id) VALUES (1, @gift_parent_id);
INSERT INTO eb_system_role_menu (rid, menu_id) VALUES (1, @gift_child_id);

-- 8. 验证角色权限
SELECT r.id as role_id, r.role_name, m.id as menu_id, m.name as menu_name
FROM eb_system_role r
JOIN eb_system_role_menu rm ON r.id = rm.rid
JOIN eb_system_menu m ON rm.menu_id = m.id
WHERE m.name LIKE '%礼物%';

