-- 奖品等级设置菜单 (Admin数据库)
-- 在商品管理下添加奖品等级设置菜单

-- 查找商品管理的菜单ID
DECLARE @GoodsMenuId BIGINT;
SELECT @GoodsMenuId = Id FROM menus WHERE Name = N'商品管理' AND MenuType = 1;

-- 如果商品管理菜单存在，则添加奖品等级设置子菜单
IF @GoodsMenuId IS NOT NULL
BEGIN
    -- 检查是否已存在
    IF NOT EXISTS (SELECT 1 FROM menus WHERE Name = N'奖品等级设置' AND ParentId = @GoodsMenuId)
    BEGIN
        INSERT INTO menus (ParentId, Name, Path, Component, Permission, Icon, SortOrder, MenuType, Status, IsExternal, IsCache, CreatedAt)
        VALUES (@GoodsMenuId, N'奖品等级设置', '/business/goods/prize-level', 'business/goods/prize-level', 'goods:prize-level', 'Medal', 30, 2, 1, 0, 1, GETDATE());
        
        PRINT N'奖品等级设置菜单添加成功';
        
        -- 获取新插入的菜单ID
        DECLARE @NewMenuId BIGINT = SCOPE_IDENTITY();
        
        -- 分配给超级管理员角色
        DECLARE @SuperAdminRoleId BIGINT;
        SELECT @SuperAdminRoleId = Id FROM roles WHERE Code = 'super_admin';
        
        IF @SuperAdminRoleId IS NOT NULL
        BEGIN
            INSERT INTO role_menus (RoleId, MenuId) VALUES (@SuperAdminRoleId, @NewMenuId);
            PRINT N'已分配给超级管理员角色';
        END
    END
    ELSE
    BEGIN
        PRINT N'奖品等级设置菜单已存在';
    END
END
ELSE
BEGIN
    PRINT N'商品管理菜单不存在，请先创建商品管理菜单';
END
