-- ============================================================
-- 商品系统表结构验证脚本
-- Feature: database-migration, Property 1: 表结构完整性
-- Validates: Requirements 2.1, 2.2, 2.3, 2.4, 2.5
-- ============================================================

SET NOCOUNT ON;

PRINT '========================================';
PRINT '商品系统表结构验证报告';
PRINT '执行时间: ' + CONVERT(VARCHAR, GETDATE(), 120);
PRINT '========================================';
PRINT '';

-- ============================================================
-- 1. 验证表是否存在
-- ============================================================
PRINT '1. 验证表是否存在';
PRINT '----------------------------------------';

DECLARE @goods_exists BIT = 0;
DECLARE @goods_items_exists BIT = 0;
DECLARE @goods_types_exists BIT = 0;
DECLARE @goods_extensions_exists BIT = 0;
DECLARE @prize_levels_exists BIT = 0;

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'goods')
BEGIN
    SET @goods_exists = 1;
    PRINT '✓ goods 表存在';
END
ELSE
    PRINT '✗ goods 表不存在';

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'goods_items')
BEGIN
    SET @goods_items_exists = 1;
    PRINT '✓ goods_items 表存在';
END
ELSE
    PRINT '✗ goods_items 表不存在';

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'goods_types')
BEGIN
    SET @goods_types_exists = 1;
    PRINT '✓ goods_types 表存在';
END
ELSE
    PRINT '✗ goods_types 表不存在';

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'goods_extensions')
BEGIN
    SET @goods_extensions_exists = 1;
    PRINT '✓ goods_extensions 表存在';
END
ELSE
    PRINT '✗ goods_extensions 表不存在';

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'prize_levels')
BEGIN
    SET @prize_levels_exists = 1;
    PRINT '✓ prize_levels 表存在';
END
ELSE
    PRINT '✗ prize_levels 表不存在';

PRINT '';

-- ============================================================
-- 2. 验证字段数量
-- ============================================================
PRINT '2. 验证字段数量';
PRINT '----------------------------------------';

SELECT 
    TABLE_NAME AS '表名',
    COUNT(*) AS '字段数量',
    CASE TABLE_NAME
        WHEN 'goods' THEN '>=54'
        WHEN 'goods_items' THEN '>=30'
        WHEN 'goods_types' THEN '>=15'
        WHEN 'goods_extensions' THEN '>=8'
        WHEN 'prize_levels' THEN '>=9'
    END AS '预期字段数',
    CASE 
        WHEN TABLE_NAME = 'goods' AND COUNT(*) >= 54 THEN '✓ 通过'
        WHEN TABLE_NAME = 'goods_items' AND COUNT(*) >= 30 THEN '✓ 通过'
        WHEN TABLE_NAME = 'goods_types' AND COUNT(*) >= 15 THEN '✓ 通过'
        WHEN TABLE_NAME = 'goods_extensions' AND COUNT(*) >= 8 THEN '✓ 通过'
        WHEN TABLE_NAME = 'prize_levels' AND COUNT(*) >= 9 THEN '✓ 通过'
        ELSE '✗ 失败'
    END AS '验证结果'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME IN ('goods', 'goods_items', 'goods_types', 'goods_extensions', 'prize_levels')
GROUP BY TABLE_NAME
ORDER BY TABLE_NAME;

PRINT '';

-- ============================================================
-- 3. 验证主键约束
-- ============================================================
PRINT '3. 验证主键约束';
PRINT '----------------------------------------';

SELECT 
    t.name AS '表名',
    c.name AS '主键列',
    i.name AS '主键名称',
    '✓ 存在' AS '验证结果'
FROM sys.tables t
INNER JOIN sys.indexes i ON t.object_id = i.object_id AND i.is_primary_key = 1
INNER JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
INNER JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE t.name IN ('goods', 'goods_items', 'goods_types', 'goods_extensions', 'prize_levels')
ORDER BY t.name;

PRINT '';

-- ============================================================
-- 4. 验证索引
-- ============================================================
PRINT '4. 验证索引';
PRINT '----------------------------------------';

SELECT 
    t.name AS '表名',
    i.name AS '索引名称',
    i.type_desc AS '索引类型'
FROM sys.indexes i
INNER JOIN sys.tables t ON i.object_id = t.object_id
WHERE t.name IN ('goods', 'goods_items', 'goods_types', 'goods_extensions', 'prize_levels')
AND i.name IS NOT NULL
ORDER BY t.name, i.name;

PRINT '';

-- ============================================================
-- 5. 验证表注释
-- ============================================================
PRINT '5. 验证表注释';
PRINT '----------------------------------------';

SELECT 
    t.name AS '表名',
    CAST(ep.value AS NVARCHAR(500)) AS '表注释',
    CASE WHEN ep.value IS NOT NULL THEN '✓ 存在' ELSE '✗ 缺失' END AS '验证结果'
FROM sys.tables t
LEFT JOIN sys.extended_properties ep ON ep.major_id = t.object_id AND ep.minor_id = 0 AND ep.name = 'MS_Description'
WHERE t.name IN ('goods', 'goods_items', 'goods_types', 'goods_extensions', 'prize_levels')
ORDER BY t.name;

PRINT '';

-- ============================================================
-- 6. 验证关键字段注释
-- ============================================================
PRINT '6. 验证关键字段注释数量';
PRINT '----------------------------------------';

SELECT 
    t.name AS '表名',
    COUNT(ep.value) AS '有注释的字段数',
    (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = t.name) AS '总字段数'
FROM sys.tables t
LEFT JOIN sys.columns c ON t.object_id = c.object_id
LEFT JOIN sys.extended_properties ep ON ep.major_id = c.object_id AND ep.minor_id = c.column_id AND ep.name = 'MS_Description'
WHERE t.name IN ('goods', 'goods_items', 'goods_types', 'goods_extensions', 'prize_levels')
GROUP BY t.name
ORDER BY t.name;

PRINT '';

-- ============================================================
-- 7. 验证福利屋和限购字段 (Requirements 2.6, 2.7)
-- ============================================================
PRINT '7. 验证福利屋和限购字段';
PRINT '----------------------------------------';

DECLARE @flw_fields INT = 0;
DECLARE @xiangou_fields INT = 0;

SELECT @flw_fields = COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'goods'
AND COLUMN_NAME IN ('is_flw', 'flw_start_time', 'flw_end_time');

SELECT @xiangou_fields = COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'goods'
AND COLUMN_NAME IN ('quanju_xiangou', 'daily_xiangou', 'choujiang_xianzhi');

IF @flw_fields = 3
    PRINT '✓ 福利屋字段完整 (is_flw, flw_start_time, flw_end_time)';
ELSE
    PRINT '✗ 福利屋字段不完整';

IF @xiangou_fields = 3
    PRINT '✓ 限购字段完整 (quanju_xiangou, daily_xiangou, choujiang_xianzhi)';
ELSE
    PRINT '✗ 限购字段不完整';

PRINT '';

-- ============================================================
-- 8. 验证特殊功能字段 (灵珠、连击、暴怒)
-- ============================================================
PRINT '8. 验证特殊功能字段';
PRINT '----------------------------------------';

DECLARE @lingzhu_fields INT = 0;
DECLARE @lianji_fields INT = 0;
DECLARE @rage_fields INT = 0;

SELECT @lingzhu_fields = COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'goods'
AND COLUMN_NAME IN ('lingzhu_is', 'lingzhu_fan', 'lingzhu_shang_id');

SELECT @lianji_fields = COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'goods'
AND COLUMN_NAME IN ('lian_ji_num', 'lian_ji_shang_id');

SELECT @rage_fields = COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'goods'
AND COLUMN_NAME IN ('rage_is', 'rage');

IF @lingzhu_fields = 3
    PRINT '✓ 灵珠功能字段完整 (lingzhu_is, lingzhu_fan, lingzhu_shang_id)';
ELSE
    PRINT '✗ 灵珠功能字段不完整';

IF @lianji_fields = 2
    PRINT '✓ 连击功能字段完整 (lian_ji_num, lian_ji_shang_id)';
ELSE
    PRINT '✗ 连击功能字段不完整';

IF @rage_fields = 2
    PRINT '✓ 暴怒功能字段完整 (rage_is, rage)';
ELSE
    PRINT '✗ 暴怒功能字段不完整';

PRINT '';

-- ============================================================
-- 验证总结
-- ============================================================
PRINT '========================================';
PRINT '验证总结';
PRINT '========================================';

DECLARE @total_tables INT = @goods_exists + @goods_items_exists + @goods_types_exists + @goods_extensions_exists + @prize_levels_exists;

IF @total_tables = 5
    PRINT '✓ 所有商品系统表创建成功 (5/5)';
ELSE
    PRINT '✗ 部分表创建失败 (' + CAST(@total_tables AS VARCHAR) + '/5)';

IF @flw_fields = 3 AND @xiangou_fields = 3
    PRINT '✓ 福利屋和限购配置字段完整';
ELSE
    PRINT '✗ 福利屋或限购配置字段不完整';

IF @lingzhu_fields = 3 AND @lianji_fields = 2 AND @rage_fields = 2
    PRINT '✓ 特殊功能字段完整 (灵珠、连击、暴怒)';
ELSE
    PRINT '✗ 特殊功能字段不完整';

PRINT '';
PRINT '验证完成!';
