-- =============================================
-- Business Module Permissions Seed Script
-- 学业邑规划 - MiAssessment
-- 
-- This script inserts business module permissions
-- into the Admin database permissions table
--
-- Database: SQL Server 2022
-- =============================================

USE [MiAssessment_Admin];
GO

-- =============================================
-- Insert Business Module Permissions
-- 权限编码格式: module:action
-- =============================================

PRINT N'Seeding business module permissions...';

-- 系统配置模块 (config)
IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'config:view')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'查看配置', 'config:view', N'系统配置', N'查看系统配置列表和详情', GETDATE());
    PRINT N'Inserted permission: config:view';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'config:update')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'更新配置', 'config:update', N'系统配置', N'修改系统配置项', GETDATE());
    PRINT N'Inserted permission: config:update';
END

-- 内容管理模块 (content)
IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'content:view')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'查看内容', 'content:view', N'内容管理', N'查看轮播图和宣传图列表', GETDATE());
    PRINT N'Inserted permission: content:view';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'content:create')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'创建内容', 'content:create', N'内容管理', N'创建轮播图和宣传图', GETDATE());
    PRINT N'Inserted permission: content:create';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'content:update')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'更新内容', 'content:update', N'内容管理', N'修改轮播图和宣传图', GETDATE());
    PRINT N'Inserted permission: content:update';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'content:delete')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'删除内容', 'content:delete', N'内容管理', N'删除轮播图和宣传图', GETDATE());
    PRINT N'Inserted permission: content:delete';
END

-- 测评管理模块 (assessment)
IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'assessment:view')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'查看测评', 'assessment:view', N'测评管理', N'查看测评类型、题库、报告分类和结论', GETDATE());
    PRINT N'Inserted permission: assessment:view';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'assessment:create')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'创建测评', 'assessment:create', N'测评管理', N'创建测评类型、题目、分类和结论', GETDATE());
    PRINT N'Inserted permission: assessment:create';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'assessment:update')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'更新测评', 'assessment:update', N'测评管理', N'修改测评类型、题目、分类和结论', GETDATE());
    PRINT N'Inserted permission: assessment:update';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'assessment:delete')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'删除测评', 'assessment:delete', N'测评管理', N'删除测评类型、题目、分类和结论', GETDATE());
    PRINT N'Inserted permission: assessment:delete';
END

-- 用户管理模块 (user)
IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'user:view')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'查看用户', 'user:view', N'用户管理', N'查看C端用户列表和详情', GETDATE());
    PRINT N'Inserted permission: user:view';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'user:update')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'更新用户', 'user:update', N'用户管理', N'修改用户状态和等级', GETDATE());
    PRINT N'Inserted permission: user:update';
END

-- 订单管理模块 (order)
IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'order:view')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'查看订单', 'order:view', N'订单管理', N'查看订单列表和详情', GETDATE());
    PRINT N'Inserted permission: order:view';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'order:update')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'更新订单', 'order:update', N'订单管理', N'处理订单退款', GETDATE());
    PRINT N'Inserted permission: order:update';
END

-- 规划师管理模块 (planner)
IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'planner:view')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'查看规划师', 'planner:view', N'规划师管理', N'查看规划师列表和预约记录', GETDATE());
    PRINT N'Inserted permission: planner:view';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'planner:create')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'创建规划师', 'planner:create', N'规划师管理', N'创建规划师信息', GETDATE());
    PRINT N'Inserted permission: planner:create';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'planner:update')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'更新规划师', 'planner:update', N'规划师管理', N'修改规划师信息和预约状态', GETDATE());
    PRINT N'Inserted permission: planner:update';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'planner:delete')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'删除规划师', 'planner:delete', N'规划师管理', N'删除规划师信息', GETDATE());
    PRINT N'Inserted permission: planner:delete';
END

-- 分销管理模块 (distribution)
IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'distribution:view')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'查看分销', 'distribution:view', N'分销管理', N'查看邀请码、佣金和提现记录', GETDATE());
    PRINT N'Inserted permission: distribution:view';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'distribution:create')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'创建分销', 'distribution:create', N'分销管理', N'生成和分配邀请码', GETDATE());
    PRINT N'Inserted permission: distribution:create';
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'distribution:update')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'更新分销', 'distribution:update', N'分销管理', N'审核提现申请', GETDATE());
    PRINT N'Inserted permission: distribution:update';
END

-- 数据统计仪表盘模块 (dashboard)
IF NOT EXISTS (SELECT 1 FROM [dbo].[permissions] WHERE [Code] = 'dashboard:view')
BEGIN
    INSERT INTO [dbo].[permissions] ([Name], [Code], [Module], [Description], [CreatedAt])
    VALUES (N'查看仪表盘', 'dashboard:view', N'数据统计', N'查看数据统计和趋势图表', GETDATE());
    PRINT N'Inserted permission: dashboard:view';
END

PRINT N'Business module permissions seeded successfully!';
GO

-- =============================================
-- Verify inserted permissions
-- =============================================
SELECT 
    [Id],
    [Name],
    [Code],
    [Module],
    [Description],
    [CreatedAt]
FROM [dbo].[permissions]
WHERE [Code] LIKE 'config:%'
   OR [Code] LIKE 'content:%'
   OR [Code] LIKE 'assessment:%'
   OR [Code] LIKE 'user:%'
   OR [Code] LIKE 'order:%'
   OR [Code] LIKE 'planner:%'
   OR [Code] LIKE 'distribution:%'
   OR [Code] LIKE 'dashboard:%'
ORDER BY [Module], [Code];
GO
