-- ========================================
-- 团播机构 v1.2.0 数据库升级脚本
-- 创建时间: 2025-01-25
-- 说明: CDK 激活功能数据库修改
-- ========================================

-- 检查脚本执行环境
PRINT '开始执行 v1.2.0 数据库升级脚本...'
PRINT '执行时间: ' + CONVERT(VARCHAR, GETDATE(), 120)

-- ========================================
-- 1. 创建 CDK 表 T_CDKs
-- ========================================
PRINT '正在创建 CDK 表 T_CDKs...'

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'T_CDKs')
BEGIN
    CREATE TABLE T_CDKs (
        Id BIGINT PRIMARY KEY IDENTITY(1,1),
        Code NVARCHAR(50) NOT NULL,
        BatchNo NVARCHAR(50) NULL,
        Status INT NOT NULL DEFAULT 0,
        UsedByUserId BIGINT NULL,
        UsedAt DATETIME2 NULL,
        CreatedAt DATETIME2 NOT NULL DEFAULT GETDATE(),
        Remark NVARCHAR(200) NULL
    );
    
    PRINT '✓ T_CDKs 表创建成功'
END
ELSE
BEGIN
    PRINT '⚠ T_CDKs 表已存在，跳过创建'
END

-- ========================================
-- 2. 为 T_CDKs 表添加唯一索引
-- ========================================
PRINT '正在为 T_CDKs 表添加索引...'

-- 添加 Code 字段唯一索引
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_T_CDKs_Code' AND object_id = OBJECT_ID('T_CDKs'))
BEGIN
    CREATE UNIQUE INDEX IX_T_CDKs_Code ON T_CDKs (Code);
    PRINT '✓ 唯一索引 IX_T_CDKs_Code 创建成功'
END
ELSE
BEGIN
    PRINT '⚠ 索引 IX_T_CDKs_Code 已存在，跳过创建'
END

-- 添加 BatchNo 索引用于批次查询
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_T_CDKs_BatchNo' AND object_id = OBJECT_ID('T_CDKs'))
BEGIN
    CREATE INDEX IX_T_CDKs_BatchNo ON T_CDKs (BatchNo);
    PRINT '✓ 索引 IX_T_CDKs_BatchNo 创建成功'
END

-- 添加 Status 索引用于状态筛选
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_T_CDKs_Status' AND object_id = OBJECT_ID('T_CDKs'))
BEGIN
    CREATE INDEX IX_T_CDKs_Status ON T_CDKs (Status);
    PRINT '✓ 索引 IX_T_CDKs_Status 创建成功'
END


-- ========================================
-- 3. 创建系统设置表 T_SystemSettings
-- ========================================
PRINT '正在创建系统设置表 T_SystemSettings...'

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'T_SystemSettings')
BEGIN
    CREATE TABLE T_SystemSettings (
        Id INT PRIMARY KEY IDENTITY(1,1),
        SettingKey NVARCHAR(100) NOT NULL,
        SettingValue NVARCHAR(500) NULL,
        Description NVARCHAR(200) NULL,
        UpdatedAt DATETIME2 NOT NULL DEFAULT GETDATE()
    );
    
    PRINT '✓ T_SystemSettings 表创建成功'
END
ELSE
BEGIN
    PRINT '⚠ T_SystemSettings 表已存在，跳过创建'
END

-- ========================================
-- 4. 为 T_SystemSettings 表添加唯一索引
-- ========================================
PRINT '正在为 T_SystemSettings 表添加索引...'

-- 添加 SettingKey 字段唯一索引
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_T_SystemSettings_SettingKey' AND object_id = OBJECT_ID('T_SystemSettings'))
BEGIN
    CREATE UNIQUE INDEX IX_T_SystemSettings_SettingKey ON T_SystemSettings (SettingKey);
    PRINT '✓ 唯一索引 IX_T_SystemSettings_SettingKey 创建成功'
END
ELSE
BEGIN
    PRINT '⚠ 索引 IX_T_SystemSettings_SettingKey 已存在，跳过创建'
END

-- ========================================
-- 5. 插入 CDK 功能开关默认配置
-- ========================================
PRINT '正在插入 CDK 功能开关默认配置...'

IF NOT EXISTS (SELECT * FROM T_SystemSettings WHERE SettingKey = 'cdk_enabled')
BEGIN
    INSERT INTO T_SystemSettings (SettingKey, SettingValue, Description, UpdatedAt)
    VALUES ('cdk_enabled', 'true', 'CDK功能开关', GETDATE());
    PRINT '✓ CDK 功能开关默认配置插入成功'
END
ELSE
BEGIN
    PRINT '⚠ CDK 功能开关配置已存在，跳过插入'
END

-- ========================================
-- 6. 修改 T_Users 表添加 CDK 激活字段
-- ========================================
PRINT '正在修改 T_Users 表添加 CDK 激活字段...'

-- 添加 IsCdkActivated 字段
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'T_Users' AND COLUMN_NAME = 'IsCdkActivated')
BEGIN
    ALTER TABLE T_Users ADD IsCdkActivated BIT NOT NULL DEFAULT 0;
    PRINT '✓ T_Users 表添加 IsCdkActivated 字段成功'
END
ELSE
BEGIN
    PRINT '⚠ IsCdkActivated 字段已存在，跳过添加'
END

-- 添加 CdkActivatedAt 字段
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'T_Users' AND COLUMN_NAME = 'CdkActivatedAt')
BEGIN
    ALTER TABLE T_Users ADD CdkActivatedAt DATETIME2 NULL;
    PRINT '✓ T_Users 表添加 CdkActivatedAt 字段成功'
END
ELSE
BEGIN
    PRINT '⚠ CdkActivatedAt 字段已存在，跳过添加'
END

-- ========================================
-- 7. 验证升级结果
-- ========================================
PRINT '正在验证升级结果...'

-- 验证 T_CDKs 表
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'T_CDKs')
    PRINT '✓ T_CDKs 表验证通过'
ELSE
    PRINT '✗ T_CDKs 表验证失败'

-- 验证 T_CDKs 唯一索引
IF EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_T_CDKs_Code' AND object_id = OBJECT_ID('T_CDKs'))
    PRINT '✓ T_CDKs Code 唯一索引验证通过'
ELSE
    PRINT '✗ T_CDKs Code 唯一索引验证失败'

-- 验证 T_SystemSettings 表
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'T_SystemSettings')
    PRINT '✓ T_SystemSettings 表验证通过'
ELSE
    PRINT '✗ T_SystemSettings 表验证失败'

-- 验证 T_SystemSettings 唯一索引
IF EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_T_SystemSettings_SettingKey' AND object_id = OBJECT_ID('T_SystemSettings'))
    PRINT '✓ T_SystemSettings SettingKey 唯一索引验证通过'
ELSE
    PRINT '✗ T_SystemSettings SettingKey 唯一索引验证失败'

-- 验证 CDK 功能开关配置
IF EXISTS (SELECT * FROM T_SystemSettings WHERE SettingKey = 'cdk_enabled')
    PRINT '✓ CDK 功能开关配置验证通过'
ELSE
    PRINT '✗ CDK 功能开关配置验证失败'

-- 验证 T_Users 表新字段
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'T_Users' AND COLUMN_NAME = 'IsCdkActivated')
    PRINT '✓ T_Users IsCdkActivated 字段验证通过'
ELSE
    PRINT '✗ T_Users IsCdkActivated 字段验证失败'

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'T_Users' AND COLUMN_NAME = 'CdkActivatedAt')
    PRINT '✓ T_Users CdkActivatedAt 字段验证通过'
ELSE
    PRINT '✗ T_Users CdkActivatedAt 字段验证失败'

-- ========================================
-- 升级完成
-- ========================================
PRINT ''
PRINT '========================================='
PRINT 'v1.2.0 数据库升级脚本执行完成！'
PRINT '完成时间: ' + CONVERT(VARCHAR, GETDATE(), 120)
PRINT '========================================='
PRINT ''
PRINT '升级内容总结:'
PRINT '1. ✓ 创建 CDK 表 T_CDKs'
PRINT '2. ✓ 添加 T_CDKs Code 唯一索引'
PRINT '3. ✓ 创建系统设置表 T_SystemSettings'
PRINT '4. ✓ 添加 T_SystemSettings SettingKey 唯一索引'
PRINT '5. ✓ 插入 CDK 功能开关默认配置'
PRINT '6. ✓ 修改 T_Users 表添加 CDK 激活字段'
PRINT ''
PRINT '注意事项:'
PRINT '- 请确保应用程序代码已同步更新'
PRINT '- 建议在低峰期执行此脚本'
PRINT '- 执行前请备份数据库'
PRINT '========================================='
