-- =============================================
-- Business Dictionary Data Initialization Script
-- MiAssessment Admin Frontend Business Modules
-- 
-- This script inserts all dictionary types and items
-- required by the admin frontend business modules
--
-- Requirements: 20.1, 20.2, 20.3
-- =============================================

USE [MiAssessment_Admin];
GO

-- =============================================
-- 1. Insert Business Dictionary Types
-- =============================================
PRINT N'Inserting business dictionary types...';

-- banner_link_type - 轮播图跳转类型
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'banner_link_type')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('banner_link_type', N'轮播图跳转类型', N'轮播图点击后的跳转方式', 1, 1, 10, GETDATE());
    PRINT N'  - banner_link_type created';
END

-- common_status - 通用状态
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'common_status')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('common_status', N'通用状态', N'通用的启用/禁用状态', 1, 1, 11, GETDATE());
    PRINT N'  - common_status created';
END

-- promotion_position - 宣传图位置
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'promotion_position')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('promotion_position', N'宣传图位置', N'宣传图展示位置', 1, 1, 12, GETDATE());
    PRINT N'  - promotion_position created';
END

-- assessment_status - 测评状态
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'assessment_status')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('assessment_status', N'测评状态', N'测评类型的上下线状态', 1, 1, 13, GETDATE());
    PRINT N'  - assessment_status created';
END

-- user_level - 用户等级
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'user_level')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('user_level', N'用户等级', N'小程序用户等级', 1, 1, 14, GETDATE());
    PRINT N'  - user_level created';
END

-- order_type - 订单类型
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'order_type')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('order_type', N'订单类型', N'订单的业务类型', 1, 1, 15, GETDATE());
    PRINT N'  - order_type created';
END

-- order_status - 订单状态
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'order_status')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('order_status', N'订单状态', N'订单的流转状态', 1, 1, 16, GETDATE());
    PRINT N'  - order_status created';
END

-- pay_type - 支付方式
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'pay_type')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('pay_type', N'支付方式', N'订单支付方式', 1, 1, 17, GETDATE());
    PRINT N'  - pay_type created';
END

-- booking_status - 预约状态
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'booking_status')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('booking_status', N'预约状态', N'规划师预约状态', 1, 1, 18, GETDATE());
    PRINT N'  - booking_status created';
END

-- invite_code_status - 邀请码状态
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'invite_code_status')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('invite_code_status', N'邀请码状态', N'邀请码的使用状态', 1, 1, 19, GETDATE());
    PRINT N'  - invite_code_status created';
END

-- commission_level - 佣金层级
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'commission_level')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('commission_level', N'佣金层级', N'分销佣金的层级', 1, 1, 20, GETDATE());
    PRINT N'  - commission_level created';
END

-- commission_status - 佣金状态
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'commission_status')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('commission_status', N'佣金状态', N'佣金的结算状态', 1, 1, 21, GETDATE());
    PRINT N'  - commission_status created';
END

-- withdrawal_status - 提现状态
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'withdrawal_status')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('withdrawal_status', N'提现状态', N'提现申请的审核状态', 1, 1, 22, GETDATE());
    PRINT N'  - withdrawal_status created';
END

-- category_type - 分类类型
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'category_type')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('category_type', N'分类类型', N'测评报告分类类型', 1, 1, 23, GETDATE());
    PRINT N'  - category_type created';
END

-- score_rule - 计分规则
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'score_rule')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('score_rule', N'计分规则', N'测评题目的计分规则', 1, 1, 24, GETDATE());
    PRINT N'  - score_rule created';
END

-- conclusion_type - 结论类型
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'conclusion_type')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('conclusion_type', N'结论类型', N'测评报告结论类型', 1, 1, 25, GETDATE());
    PRINT N'  - conclusion_type created';
END

-- student_grade - 学生年级
IF NOT EXISTS (SELECT 1 FROM [dbo].[dict_types] WHERE [code] = 'student_grade')
BEGIN
    INSERT INTO [dbo].[dict_types] ([code], [name], [description], [source_type], [status], [sort], [created_at])
    VALUES ('student_grade', N'学生年级', N'学生所在年级', 1, 1, 26, GETDATE());
    PRINT N'  - student_grade created';
END

GO

-- =============================================
-- 2. Insert Dictionary Items
-- =============================================
PRINT N'Inserting dictionary items...';

-- Get type IDs
DECLARE @banner_link_type_id INT, @common_status_id INT, @promotion_position_id INT;
DECLARE @assessment_status_id INT, @user_level_id INT, @order_type_id INT;
DECLARE @order_status_id INT, @pay_type_id INT, @booking_status_id INT;
DECLARE @invite_code_status_id INT, @commission_level_id INT, @commission_status_id INT;
DECLARE @withdrawal_status_id INT, @category_type_id INT, @score_rule_id INT;
DECLARE @conclusion_type_id INT, @student_grade_id INT;

SELECT @banner_link_type_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'banner_link_type';
SELECT @common_status_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'common_status';
SELECT @promotion_position_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'promotion_position';
SELECT @assessment_status_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'assessment_status';
SELECT @user_level_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'user_level';
SELECT @order_type_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'order_type';
SELECT @order_status_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'order_status';
SELECT @pay_type_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'pay_type';
SELECT @booking_status_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'booking_status';
SELECT @invite_code_status_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'invite_code_status';
SELECT @commission_level_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'commission_level';
SELECT @commission_status_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'commission_status';
SELECT @withdrawal_status_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'withdrawal_status';
SELECT @category_type_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'category_type';
SELECT @score_rule_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'score_rule';
SELECT @conclusion_type_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'conclusion_type';
SELECT @student_grade_id = [Id] FROM [dbo].[dict_types] WHERE [code] = 'student_grade';

-- banner_link_type items
IF @banner_link_type_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @banner_link_type_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@banner_link_type_id, N'内部页面', '1', 'primary', 1, 1, GETDATE()),
        (@banner_link_type_id, N'外部链接', '2', 'success', 1, 2, GETDATE()),
        (@banner_link_type_id, N'小程序', '3', 'warning', 1, 3, GETDATE());
    PRINT N'  - banner_link_type items inserted';
END

-- common_status items
IF @common_status_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @common_status_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@common_status_id, N'禁用', '0', 'danger', 1, 1, GETDATE()),
        (@common_status_id, N'启用', '1', 'success', 1, 2, GETDATE());
    PRINT N'  - common_status items inserted';
END

-- promotion_position items
IF @promotion_position_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @promotion_position_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@promotion_position_id, N'首页底部', '1', 'primary', 1, 1, GETDATE()),
        (@promotion_position_id, N'产品页', '2', 'success', 1, 2, GETDATE());
    PRINT N'  - promotion_position items inserted';
END

-- assessment_status items
IF @assessment_status_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @assessment_status_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@assessment_status_id, N'下线', '0', 'info', 1, 1, GETDATE()),
        (@assessment_status_id, N'上线', '1', 'success', 1, 2, GETDATE()),
        (@assessment_status_id, N'即将上线', '2', 'warning', 1, 3, GETDATE());
    PRINT N'  - assessment_status items inserted';
END

-- user_level items
IF @user_level_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @user_level_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@user_level_id, N'普通用户', '1', '', 1, 1, GETDATE()),
        (@user_level_id, N'合伙人', '2', 'primary', 1, 2, GETDATE()),
        (@user_level_id, N'渠道商', '3', 'warning', 1, 3, GETDATE());
    PRINT N'  - user_level items inserted';
END

-- order_type items
IF @order_type_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @order_type_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@order_type_id, N'测评订单', '1', 'primary', 1, 1, GETDATE()),
        (@order_type_id, N'规划订单', '2', 'success', 1, 2, GETDATE());
    PRINT N'  - order_type items inserted';
END

-- order_status items
IF @order_status_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @order_status_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@order_status_id, N'待支付', '1', 'warning', 1, 1, GETDATE()),
        (@order_status_id, N'已支付', '2', 'primary', 1, 2, GETDATE()),
        (@order_status_id, N'已完成', '3', 'success', 1, 3, GETDATE()),
        (@order_status_id, N'退款中', '4', 'info', 1, 4, GETDATE()),
        (@order_status_id, N'已退款', '5', 'danger', 1, 5, GETDATE()),
        (@order_status_id, N'已取消', '6', 'info', 1, 6, GETDATE());
    PRINT N'  - order_status items inserted';
END

-- pay_type items
IF @pay_type_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @pay_type_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@pay_type_id, N'微信支付', '1', 'success', 1, 1, GETDATE());
    PRINT N'  - pay_type items inserted';
END

-- booking_status items
IF @booking_status_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @booking_status_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@booking_status_id, N'待确认', '1', 'warning', 1, 1, GETDATE()),
        (@booking_status_id, N'已确认', '2', 'primary', 1, 2, GETDATE()),
        (@booking_status_id, N'已完成', '3', 'success', 1, 3, GETDATE()),
        (@booking_status_id, N'已取消', '4', 'info', 1, 4, GETDATE());
    PRINT N'  - booking_status items inserted';
END

-- invite_code_status items
IF @invite_code_status_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @invite_code_status_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@invite_code_status_id, N'未分配', '1', 'info', 1, 1, GETDATE()),
        (@invite_code_status_id, N'已分配', '2', 'primary', 1, 2, GETDATE()),
        (@invite_code_status_id, N'已使用', '3', 'success', 1, 3, GETDATE());
    PRINT N'  - invite_code_status items inserted';
END

-- commission_level items
IF @commission_level_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @commission_level_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@commission_level_id, N'直接下级', '1', 'primary', 1, 1, GETDATE()),
        (@commission_level_id, N'间接下级', '2', 'success', 1, 2, GETDATE());
    PRINT N'  - commission_level items inserted';
END

-- commission_status items
IF @commission_status_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @commission_status_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@commission_status_id, N'待结算', '1', 'warning', 1, 1, GETDATE()),
        (@commission_status_id, N'已结算', '2', 'success', 1, 2, GETDATE());
    PRINT N'  - commission_status items inserted';
END

-- withdrawal_status items
IF @withdrawal_status_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @withdrawal_status_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@withdrawal_status_id, N'待审核', '1', 'warning', 1, 1, GETDATE()),
        (@withdrawal_status_id, N'处理中', '2', 'primary', 1, 2, GETDATE()),
        (@withdrawal_status_id, N'已完成', '3', 'success', 1, 3, GETDATE()),
        (@withdrawal_status_id, N'已取消', '4', 'info', 1, 4, GETDATE());
    PRINT N'  - withdrawal_status items inserted';
END

-- category_type items (八大分类类型)
IF @category_type_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @category_type_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@category_type_id, N'八大智能', '1', 'primary', 1, 1, GETDATE()),
        (@category_type_id, N'个人特质', '2', 'success', 1, 2, GETDATE()),
        (@category_type_id, N'细分能力', '3', 'warning', 1, 3, GETDATE()),
        (@category_type_id, N'先天学习', '4', 'info', 1, 4, GETDATE()),
        (@category_type_id, N'学习能力', '5', 'primary', 1, 5, GETDATE()),
        (@category_type_id, N'大脑类型', '6', 'success', 1, 6, GETDATE()),
        (@category_type_id, N'性格类型', '7', 'warning', 1, 7, GETDATE()),
        (@category_type_id, N'未来能力', '8', 'danger', 1, 8, GETDATE());
    PRINT N'  - category_type items inserted';
END

-- score_rule items
IF @score_rule_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @score_rule_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@score_rule_id, N'累加计分(1-10)', '1', 'primary', 1, 1, GETDATE()),
        (@score_rule_id, N'二值计分(0/1)', '2', 'success', 1, 2, GETDATE());
    PRINT N'  - score_rule items inserted';
END

-- conclusion_type items
IF @conclusion_type_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @conclusion_type_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@conclusion_type_id, N'最强', '1', 'success', 1, 1, GETDATE()),
        (@conclusion_type_id, N'较强', '2', 'primary', 1, 2, GETDATE()),
        (@conclusion_type_id, N'较弱', '3', 'warning', 1, 3, GETDATE()),
        (@conclusion_type_id, N'最弱', '4', 'danger', 1, 4, GETDATE());
    PRINT N'  - conclusion_type items inserted';
END

-- student_grade items
IF @student_grade_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM [dbo].[dict_items] WHERE [type_id] = @student_grade_id)
BEGIN
    INSERT INTO [dbo].[dict_items] ([type_id], [label], [value], [css_class], [status], [sort], [created_at])
    VALUES 
        (@student_grade_id, N'小学', '1', 'primary', 1, 1, GETDATE()),
        (@student_grade_id, N'初中', '2', 'success', 1, 2, GETDATE()),
        (@student_grade_id, N'高中', '3', 'warning', 1, 3, GETDATE());
    PRINT N'  - student_grade items inserted';
END

GO

-- =============================================
-- Verification
-- =============================================
PRINT N'';
PRINT N'==============================================';
PRINT N'Business Dictionary Initialization Complete!';
PRINT N'==============================================';
PRINT N'';

-- Display summary
SELECT 
    dt.[code] AS [字典类型编码],
    dt.[name] AS [字典类型名称],
    COUNT(di.[Id]) AS [字典项数量]
FROM [dbo].[dict_types] dt
LEFT JOIN [dbo].[dict_items] di ON dt.[Id] = di.[type_id]
WHERE dt.[code] IN (
    'banner_link_type', 'common_status', 'promotion_position', 'assessment_status',
    'user_level', 'order_type', 'order_status', 'pay_type', 'booking_status',
    'invite_code_status', 'commission_level', 'commission_status', 'withdrawal_status',
    'category_type', 'score_rule', 'conclusion_type', 'student_grade'
)
GROUP BY dt.[code], dt.[name]
ORDER BY dt.[code];

PRINT N'';
PRINT N'Script execution completed successfully.';
GO
