-- =============================================
-- 业务模块权限初始化脚本
-- 用于向已有数据库添加业务模块权限
-- =============================================

-- 检查并插入业务模块权限（如果不存在）
-- 使用 MERGE 语句避免重复插入

-- 系统配置权限
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'config:view')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'查看配置', 'config:view', N'系统配置', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'config:edit')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'编辑配置', 'config:edit', N'系统配置', GETDATE());

-- 用户管理权限（业务）
-- 注意：user:list 可能已存在于管理员管理模块，这里使用不同的模块名
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'user:view')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'用户详情', 'user:view', N'用户管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'user:money')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'资金变动', 'user:money', N'用户管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'user:status')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'状态管理', 'user:status', N'用户管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'user:test')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'测试账号', 'user:test', N'用户管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'user:clear')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'清空绑定', 'user:clear', N'用户管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'user:gift')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'赠送礼品', 'user:gift', N'用户管理', GETDATE());

-- VIP管理权限
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'vip:list')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'VIP列表', 'vip:list', N'VIP管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'vip:edit')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'VIP编辑', 'vip:edit', N'VIP管理', GETDATE());

-- 商品管理权限
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'goods:list')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'商品列表', 'goods:list', N'商品管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'goods:view')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'商品详情', 'goods:view', N'商品管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'goods:add')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'添加商品', 'goods:add', N'商品管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'goods:edit')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'编辑商品', 'goods:edit', N'商品管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'goods:delete')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'删除商品', 'goods:delete', N'商品管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'goods:status')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'商品状态', 'goods:status', N'商品管理', GETDATE());

-- 订单管理权限
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'order:list')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'订单列表', 'order:list', N'订单管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'order:view')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'订单详情', 'order:view', N'订单管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'order:ship')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'发货管理', 'order:ship', N'订单管理', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'order:export')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'订单导出', 'order:export', N'订单管理', GETDATE());

-- 财务管理权限
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'finance:view')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'财务查看', 'finance:view', N'财务管理', GETDATE());

-- 仪表盘权限
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'dashboard:view')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'仪表盘查看', 'dashboard:view', N'仪表盘', GETDATE());
IF NOT EXISTS (SELECT 1 FROM Permissions WHERE Code = 'dashboard:edit')
    INSERT INTO Permissions (Name, Code, Module, CreatedAt) VALUES (N'仪表盘编辑', 'dashboard:edit', N'仪表盘', GETDATE());

-- 为超级管理员角色分配新增的业务权限
DECLARE @SuperAdminRoleId BIGINT;
SELECT @SuperAdminRoleId = Id FROM Roles WHERE Code = 'super_admin';

IF @SuperAdminRoleId IS NOT NULL
BEGIN
    -- 插入新权限到角色权限关联表（如果不存在）
    INSERT INTO RolePermissions (RoleId, PermissionId)
    SELECT @SuperAdminRoleId, p.Id
    FROM Permissions p
    WHERE p.Code IN (
        'config:view', 'config:edit',
        'user:view', 'user:money', 'user:status', 'user:test', 'user:clear', 'user:gift',
        'vip:list', 'vip:edit',
        'goods:list', 'goods:view', 'goods:add', 'goods:edit', 'goods:delete', 'goods:status',
        'order:list', 'order:view', 'order:ship', 'order:export',
        'finance:view',
        'dashboard:view', 'dashboard:edit'
    )
    AND NOT EXISTS (
        SELECT 1 FROM RolePermissions rp 
        WHERE rp.RoleId = @SuperAdminRoleId AND rp.PermissionId = p.Id
    );
    
    PRINT N'已为超级管理员角色分配业务模块权限';
END
ELSE
BEGIN
    PRINT N'警告：未找到超级管理员角色，请手动分配权限';
END

-- 显示新增的权限数量
SELECT COUNT(*) AS NewPermissionsCount FROM Permissions 
WHERE Code IN (
    'config:view', 'config:edit',
    'user:view', 'user:money', 'user:status', 'user:test', 'user:clear', 'user:gift',
    'vip:list', 'vip:edit',
    'goods:list', 'goods:view', 'goods:add', 'goods:edit', 'goods:delete', 'goods:status',
    'order:list', 'order:view', 'order:ship', 'order:export',
    'finance:view',
    'dashboard:view', 'dashboard:edit'
);

PRINT N'业务模块权限初始化完成';
