-- ========================================
-- 团播机构 v1.0.1 数据库升级验证脚本
-- 创建时间: 2025-01-10
-- 说明: 验证 v1.0.1 升级是否成功完成
-- ========================================

PRINT '开始验证 v1.0.1 数据库升级结果...'
PRINT '验证时间: ' + CONVERT(VARCHAR, GETDATE(), 120)
PRINT ''

DECLARE @ErrorCount INT = 0

-- ========================================
-- 1. 验证 T_HomePopups 表结构
-- ========================================
PRINT '1. 验证 T_HomePopups 表结构...'

-- 检查表是否存在
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'T_HomePopups')
BEGIN
    PRINT '   ✓ T_HomePopups 表存在'
    
    -- 检查关键字段
    DECLARE @ColumnCount INT
    SELECT @ColumnCount = COUNT(*) 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_NAME = 'T_HomePopups' 
    AND COLUMN_NAME IN ('Id', 'Title', 'ImageUrl', 'LinkType', 'LinkUrl', 'TargetId', 'IsActive', 'SortOrder', 'ViewCount', 'ClickCount', 'CreatedAt', 'UpdatedAt')
    
    IF @ColumnCount >= 12
        PRINT '   ✓ 表字段完整 (' + CAST(@ColumnCount AS VARCHAR) + '/12)'
    ELSE
    BEGIN
        PRINT '   ✗ 表字段不完整 (' + CAST(@ColumnCount AS VARCHAR) + '/12)'
        SET @ErrorCount = @ErrorCount + 1
    END
END
ELSE
BEGIN
    PRINT '   ✗ T_HomePopups 表不存在'
    SET @ErrorCount = @ErrorCount + 1
END

-- ========================================
-- 2. 验证索引
-- ========================================
PRINT '2. 验证索引...'

IF EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_T_HomePopups_IsActive_StartTime_EndTime')
    PRINT '   ✓ 索引 IX_T_HomePopups_IsActive_StartTime_EndTime 存在'
ELSE
BEGIN
    PRINT '   ✗ 索引 IX_T_HomePopups_IsActive_StartTime_EndTime 不存在'
    SET @ErrorCount = @ErrorCount + 1
END

IF EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_T_HomePopups_SortOrder')
    PRINT '   ✓ 索引 IX_T_HomePopups_SortOrder 存在'
ELSE
BEGIN
    PRINT '   ✗ 索引 IX_T_HomePopups_SortOrder 不存在'
    SET @ErrorCount = @ErrorCount + 1
END

-- ========================================
-- 3. 验证 T_Banners 表约束
-- ========================================
PRINT '3. 验证 T_Banners 表约束...'

IF EXISTS (SELECT * FROM sys.check_constraints WHERE name = 'CK_T_Banners_LinkType' AND definition LIKE '%[LinkType]=(4)%')
    PRINT '   ✓ T_Banners LinkType 约束支持值 1-4'
ELSE
BEGIN
    PRINT '   ✗ T_Banners LinkType 约束不支持值 4'
    SET @ErrorCount = @ErrorCount + 1
END

-- ========================================
-- 4. 验证字典配置
-- ========================================
PRINT '4. 验证字典配置...'

-- 验证弹窗字典类型
IF EXISTS (SELECT * FROM sys_dict_type WHERE DictType = 'liveforum_popup_linktype')
    PRINT '   ✓ 弹窗链接类型字典类型存在'
ELSE
BEGIN
    PRINT '   ✗ 弹窗链接类型字典类型不存在'
    SET @ErrorCount = @ErrorCount + 1
END

-- 验证弹窗字典数据
DECLARE @PopupDictCount INT
SELECT @PopupDictCount = COUNT(*) FROM sys_dict_data WHERE DictType = 'liveforum_popup_linktype'

IF @PopupDictCount >= 4
    PRINT '   ✓ 弹窗链接类型字典数据完整 (' + CAST(@PopupDictCount AS VARCHAR) + ' 项)'
ELSE
BEGIN
    PRINT '   ✗ 弹窗链接类型字典数据不完整 (' + CAST(@PopupDictCount AS VARCHAR) + ' 项)'
    SET @ErrorCount = @ErrorCount + 1
END

-- 验证Banner字典更新
IF EXISTS (SELECT * FROM sys_dict_data WHERE DictType = 'liveforum_banners_linktype' AND DictValue = '4')
    PRINT '   ✓ Banner 字典包含通知详情选项'
ELSE
BEGIN
    PRINT '   ✗ Banner 字典缺少通知详情选项'
    SET @ErrorCount = @ErrorCount + 1
END

-- ========================================
-- 5. 数据完整性检查
-- ========================================
PRINT '5. 数据完整性检查...'

-- 检查是否有示例数据
DECLARE @PopupDataCount INT
SELECT @PopupDataCount = COUNT(*) FROM T_HomePopups

PRINT '   ℹ T_HomePopups 表中有 ' + CAST(@PopupDataCount AS VARCHAR) + ' 条记录'

-- 检查Banner数据
DECLARE @BannerDataCount INT
SELECT @BannerDataCount = COUNT(*) FROM T_Banners

PRINT '   ℹ T_Banners 表中有 ' + CAST(@BannerDataCount AS VARCHAR) + ' 条记录'

-- ========================================
-- 6. 功能性测试查询
-- ========================================
PRINT '6. 功能性测试查询...'

-- 测试弹窗查询
BEGIN TRY
    DECLARE @TestPopupCount INT
    SELECT @TestPopupCount = COUNT(*) 
    FROM T_HomePopups 
    WHERE IsActive = 1 
    AND (StartTime IS NULL OR StartTime <= GETDATE()) 
    AND (EndTime IS NULL OR EndTime >= GETDATE())
    
    PRINT '   ✓ 弹窗查询功能正常 (找到 ' + CAST(@TestPopupCount AS VARCHAR) + ' 个可显示的弹窗)'
END TRY
BEGIN CATCH
    PRINT '   ✗ 弹窗查询功能异常: ' + ERROR_MESSAGE()
    SET @ErrorCount = @ErrorCount + 1
END CATCH

-- 测试Banner查询
BEGIN TRY
    DECLARE @TestBannerCount INT
    SELECT @TestBannerCount = COUNT(*) 
    FROM T_Banners 
    WHERE IsActive = 1 
    AND LinkType IN (1, 2, 3, 4)
    
    PRINT '   ✓ Banner 查询功能正常 (找到 ' + CAST(@TestBannerCount AS VARCHAR) + ' 个可用的Banner)'
END TRY
BEGIN CATCH
    PRINT '   ✗ Banner 查询功能异常: ' + ERROR_MESSAGE()
    SET @ErrorCount = @ErrorCount + 1
END CATCH

-- ========================================
-- 验证结果汇总
-- ========================================
PRINT ''
PRINT '========================================='
PRINT '验证结果汇总:'
PRINT '========================================='

IF @ErrorCount = 0
BEGIN
    PRINT '🎉 验证通过！v1.0.1 升级成功完成'
    PRINT ''
    PRINT '✓ 所有表结构正确'
    PRINT '✓ 所有索引创建成功'
    PRINT '✓ 所有约束更新正确'
    PRINT '✓ 所有字典配置完整'
    PRINT '✓ 功能查询正常'
    PRINT ''
    PRINT '可以安全部署应用程序代码。'
END
ELSE
BEGIN
    PRINT '❌ 验证失败！发现 ' + CAST(@ErrorCount AS VARCHAR) + ' 个问题'
    PRINT ''
    PRINT '请检查上述错误信息，修复问题后重新验证。'
    PRINT '建议联系技术团队进行排查。'
END

PRINT ''
PRINT '验证完成时间: ' + CONVERT(VARCHAR, GETDATE(), 120)
PRINT '========================================='