-- ============================================================
-- 财务系统表结构验证脚本
-- Feature: database-migration, Property 1: 表结构完整性
-- Validates: Requirements 4.1, 4.2, 4.3, 4.4, 4.5, 4.6
-- ============================================================

USE honey_box;
GO

PRINT '========================================';
PRINT '财务系统表结构验证报告';
PRINT '========================================';
PRINT '';

-- 1. 验证所有财务系统表是否存在
PRINT '1. 表存在性验证';
PRINT '----------------------------------------';

DECLARE @expected_tables TABLE (table_name NVARCHAR(50));
INSERT INTO @expected_tables VALUES 
    ('profit_money'), 
    ('profit_integral'), 
    ('profit_money2'), 
    ('profit_pay'), 
    ('profit_score'), 
    ('profit_ou_qi');

SELECT 
    e.table_name AS [预期表名],
    CASE WHEN t.TABLE_NAME IS NOT NULL THEN '✓ 存在' ELSE '✗ 不存在' END AS [状态]
FROM @expected_tables e
LEFT JOIN INFORMATION_SCHEMA.TABLES t 
    ON t.TABLE_NAME = e.table_name 
    AND t.TABLE_TYPE = 'BASE TABLE';

-- 2. 验证每个表的字段数量
PRINT '';
PRINT '2. 字段数量验证';
PRINT '----------------------------------------';

SELECT 
    TABLE_NAME AS [表名],
    COUNT(*) AS [字段数量]
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME IN ('profit_money', 'profit_integral', 'profit_money2', 'profit_pay', 'profit_score', 'profit_ou_qi')
GROUP BY TABLE_NAME
ORDER BY TABLE_NAME;

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

SELECT 
    tc.TABLE_NAME AS [表名],
    tc.CONSTRAINT_NAME AS [约束名称],
    '✓ 已创建' AS [状态]
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
WHERE tc.TABLE_NAME IN ('profit_money', 'profit_integral', 'profit_money2', 'profit_pay', 'profit_score', 'profit_ou_qi')
    AND tc.CONSTRAINT_TYPE = 'PRIMARY KEY';

-- 4. 验证索引
PRINT '';
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 ('profit_money', 'profit_integral', 'profit_money2', 'profit_pay', 'profit_score', 'profit_ou_qi')
    AND i.name IS NOT NULL
ORDER BY t.name, i.name;

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

SELECT 
    t.name AS [表名],
    CAST(ep.value AS NVARCHAR(200)) 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 ('profit_money', 'profit_integral', 'profit_money2', 'profit_pay', 'profit_score', 'profit_ou_qi')
ORDER BY t.name;

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

SELECT 
    t.name AS [表名],
    COUNT(c.column_id) AS [总字段数],
    COUNT(ep.value) AS [已注释字段数],
    CASE WHEN COUNT(c.column_id) = COUNT(ep.value) THEN '✓ 完整' ELSE '⚠ 部分缺失' END AS [状态]
FROM sys.tables t
INNER 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 ('profit_money', 'profit_integral', 'profit_money2', 'profit_pay', 'profit_score', 'profit_ou_qi')
GROUP BY t.name
ORDER BY t.name;

-- 7. 验证字段详情
PRINT '';
PRINT '7. 字段详情验证';
PRINT '----------------------------------------';

SELECT 
    c.TABLE_NAME AS [表名],
    c.COLUMN_NAME AS [字段名],
    c.DATA_TYPE AS [数据类型],
    c.IS_NULLABLE AS [允许空值],
    c.COLUMN_DEFAULT AS [默认值]
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE c.TABLE_NAME IN ('profit_money', 'profit_integral', 'profit_money2', 'profit_pay', 'profit_score', 'profit_ou_qi')
ORDER BY c.TABLE_NAME, c.ORDINAL_POSITION;

PRINT '';
PRINT '========================================';
PRINT '验证完成';
PRINT '========================================';
GO
