-- ==========================================
-- 团播机构 v1.2.0 管理库 (admin) 完整菜单配置脚本
-- 整合时间: 2025-01-25
-- 说明: 整合所有 v1.2.0 后台管理菜单和权限配置
-- 数据库: 后台管理数据库 (admin)
-- ==========================================
-- 包含模块:
--   1. CDK 管理菜单 + 权限按钮
--   2. 发帖回复时间间隔配置菜单 + 权限按钮
--   3. 防沉迷规则管理菜单 + 权限按钮
-- ==========================================

PRINT '=========================================='
PRINT '开始执行 v1.2.0 管理库完整菜单配置脚本'
PRINT '执行时间: ' + CONVERT(VARCHAR, GETDATE(), 120)
PRINT '=========================================='
PRINT ''

-- ##########################################################
-- 模块1: CDK 管理菜单
-- ##########################################################
PRINT '>>> 模块1: CDK 管理菜单'
PRINT ''

IF NOT EXISTS (SELECT 1 FROM sys_menu WHERE path = 'tcdk' AND component = 'liveforum/tcdk')
BEGIN
    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 ('CDK管理', 1233, 10, 'tcdk', 'liveforum/tcdk', 1, 0, 'C', '0', '0', 'cdk:list', 'key', 'admin', GETDATE(), 'admin', GETDATE(), 'CDK激活码管理');

    DECLARE @cdkMenuId BIGINT = SCOPE_IDENTITY();
    PRINT '✓ CDK 管理菜单插入成功，menuId: ' + CAST(@cdkMenuId 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 ('查询', @cdkMenuId, 1, '#', NULL, 1, 0, 'F', '0', '0', 'cdk: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 ('生成', @cdkMenuId, 2, '#', NULL, 1, 0, 'F', '0', '0', 'cdk:generate', '#', '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 ('删除', @cdkMenuId, 3, '#', NULL, 1, 0, 'F', '0', '0', 'cdk:delete', '#', '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 ('导出', @cdkMenuId, 4, '#', NULL, 1, 0, 'F', '0', '0', 'cdk:export', '#', '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 ('设置', @cdkMenuId, 5, '#', NULL, 1, 0, 'F', '0', '0', 'settings: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 ('查询设置', @cdkMenuId, 6, '#', NULL, 1, 0, 'F', '0', '0', 'settings:query', '#', 'admin', GETDATE(), 'admin', GETDATE(), NULL);

    PRINT '✓ CDK 权限按钮插入成功 (查询/生成/删除/导出/设置/查询设置)'

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

    PRINT '✓ CDK 菜单权限已分配给管理员角色'
END
ELSE
    PRINT '⚠ CDK 管理菜单已存在，跳过'

PRINT ''

-- ##########################################################
-- 模块2: 发帖回复时间间隔配置菜单
-- ##########################################################
PRINT '>>> 模块2: 发帖回复时间间隔配置菜单'
PRINT ''

IF NOT EXISTS (SELECT 1 FROM sys_menu WHERE path = 'tpostreplyintervals' AND component = 'liveforum/tpostreplyintervals')
BEGIN
    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, 12, 'tpostreplyintervals', 'liveforum/tpostreplyintervals', 1, 0, 'C', '0', '0', 'tpostreplyintervals:list', 'time-range', 'admin', GETDATE(), 'admin', GETDATE(), '发帖回复时间间隔配置');

    DECLARE @intervalMenuId BIGINT = SCOPE_IDENTITY();
    PRINT '✓ 时间间隔配置菜单插入成功，menuId: ' + CAST(@intervalMenuId 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 ('查询', @intervalMenuId, 1, '', NULL, 1, 0, 'F', '0', '0', 'tpostreplyintervals:list', '#', '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 ('编辑', @intervalMenuId, 2, '', NULL, 1, 0, 'F', '0', '0', 'tpostreplyintervals:edit', '#', '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 >= @intervalMenuId;

    PRINT '✓ 时间间隔配置菜单权限已分配给管理员角色'
END
ELSE
    PRINT '⚠ 时间间隔配置菜单已存在，跳过'

PRINT ''

-- ##########################################################
-- 模块3: 防沉迷规则管理菜单
-- ##########################################################
PRINT '>>> 模块3: 防沉迷规则管理菜单'
PRINT ''

IF NOT EXISTS (SELECT 1 FROM sys_menu WHERE path = 'tantiaddictionrules' AND component = 'liveforum/tantiaddictionrules/index')
BEGIN
    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
    PRINT '⚠ 防沉迷规则管理菜单已存在，跳过'

PRINT ''

-- ##########################################################
-- 配置完成
-- ##########################################################
PRINT '=========================================='
PRINT 'v1.2.0 管理库完整菜单配置脚本执行完成！'
PRINT '完成时间: ' + CONVERT(VARCHAR, GETDATE(), 120)
PRINT '=========================================='
PRINT ''
PRINT '配置内容总结:'
PRINT '  模块1: CDK 管理菜单 (6个权限按钮: 查询/生成/删除/导出/设置/查询设置)'
PRINT '  模块2: 时间间隔配置菜单 (2个权限按钮: 查询/编辑)'
PRINT '  模块3: 防沉迷规则管理菜单 (4个权限按钮: 查询/新增/编辑/删除)'
PRINT ''
PRINT '注意: 所有菜单位于"系统配置"(parentId=1233)目录下'
PRINT '      需要刷新浏览器或重新登录才能看到新菜单'
PRINT '=========================================='
