-- ========================================
-- 团播机构 v1.2.0 防沉迷规则管理菜单配置脚本
-- 创建时间: 2025-01-25
-- 说明: 防沉迷规则管理菜单和权限配置
-- 数据库: 后台管理数据库 (admin)
-- ========================================

PRINT '开始执行 v1.2.0 防沉迷规则管理菜单配置脚本...'
PRINT '执行时间: ' + CONVERT(VARCHAR, GETDATE(), 120)

-- ========================================
-- 1. 插入防沉迷规则管理菜单
-- ========================================
PRINT '正在插入防沉迷规则管理菜单...'

-- 检查是否已存在
IF NOT EXISTS (SELECT 1 FROM sys_menu WHERE path = 'tantiaddictionrules' AND component = 'liveforum/tantiaddictionrules/index')
BEGIN
    -- 插入防沉迷规则管理菜单 (父菜单ID: 1233 - 系统配置)
    INSERT INTO sys_menu (menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
    VALUES ('防沉迷规则', 1233, 13, 'tantiaddictionrules', 'liveforum/tantiaddictionrules/index', 1, 0, 'C', '0', '0', 'tantiaddictionrules:list', 'time-range', 'admin', GETDATE(), 'admin', GETDATE(), '防沉迷规则配置管理');

    DECLARE @antiAddictionMenuId BIGINT = SCOPE_IDENTITY();
    PRINT '✓ 防沉迷规则管理菜单插入成功，menuId: ' + CAST(@antiAddictionMenuId AS VARCHAR);

    -- 插入权限按钮
    -- 查询权限
    INSERT INTO sys_menu (menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
    VALUES ('查询', @antiAddictionMenuId, 1, '#', NULL, 1, 0, 'F', '0', '0', 'tantiaddictionrules:query', '#', 'admin', GETDATE(), 'admin', GETDATE(), NULL);

    -- 新增权限
    INSERT INTO sys_menu (menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
    VALUES ('新增', @antiAddictionMenuId, 2, '#', NULL, 1, 0, 'F', '0', '0', 'tantiaddictionrules:add', '#', 'admin', GETDATE(), 'admin', GETDATE(), NULL);

    -- 编辑权限
    INSERT INTO sys_menu (menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
    VALUES ('编辑', @antiAddictionMenuId, 3, '#', NULL, 1, 0, 'F', '0', '0', 'tantiaddictionrules:edit', '#', 'admin', GETDATE(), 'admin', GETDATE(), NULL);

    -- 删除权限
    INSERT INTO sys_menu (menuName, parentId, orderNum, path, component, isFrame, isCache, menuType, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
    VALUES ('删除', @antiAddictionMenuId, 4, '#', NULL, 1, 0, 'F', '0', '0', 'tantiaddictionrules:delete', '#', 'admin', GETDATE(), 'admin', GETDATE(), NULL);

    PRINT '✓ 防沉迷规则权限按钮插入成功';

    -- 为管理员角色分配权限
    INSERT INTO sys_role_menu (Role_id, Menu_id, Create_time)
    SELECT 1, menuId, GETDATE()
    FROM sys_menu
    WHERE menuId >= @antiAddictionMenuId;

    PRINT '✓ 防沉迷规则菜单权限分配成功';
END
ELSE
BEGIN
    PRINT '⚠ 防沉迷规则管理菜单已存在，跳过插入';
END

-- ========================================
-- 2. 验证配置结果
-- ========================================
PRINT '正在验证配置结果...'

IF EXISTS (SELECT 1 FROM sys_menu WHERE path = 'tantiaddictionrules' AND component = 'liveforum/tantiaddictionrules/index')
    PRINT '✓ 防沉迷规则管理菜单验证通过'
ELSE
    PRINT '✗ 防沉迷规则管理菜单验证失败'

-- ========================================
-- 配置完成
-- ========================================
PRINT ''
PRINT '========================================='
PRINT 'v1.2.0 防沉迷规则管理菜单配置脚本执行完成！'
PRINT '完成时间: ' + CONVERT(VARCHAR, GETDATE(), 120)
PRINT '========================================='
PRINT ''
PRINT '配置内容总结:'
PRINT '1. ✓ 防沉迷规则管理菜单 (路径: tantiaddictionrules, 组件: liveforum/tantiaddictionrules/index)'
PRINT '2. ✓ 权限按钮: 查询、新增、编辑、删除'
PRINT '3. ✓ 管理员角色权限分配'
PRINT ''
PRINT '注意事项:'
PRINT '- 菜单位于"系统配置"目录下'
PRINT '- 需要刷新浏览器或重新登录才能看到新菜单'
PRINT '========================================='
