-- ============================================================
-- 订单系统表结构验证脚本
-- Feature: database-migration, Property 1: 表结构完整性
-- Validates: Requirements 3.1, 3.2, 3.3, 3.4
-- ============================================================

SET NOCOUNT ON;

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

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

DECLARE @orders_exists BIT = 0;
DECLARE @order_items_exists BIT = 0;
DECLARE @order_items_recovery_exists BIT = 0;
DECLARE @order_items_send_exists BIT = 0;

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

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

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

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

PRINT '';

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

SELECT 
    TABLE_NAME AS '表名',
    COUNT(*) AS '字段数量',
    CASE TABLE_NAME
        WHEN 'orders' THEN '>=35'
        WHEN 'order_items' THEN '>=31'
        WHEN 'order_items_recovery' THEN '>=6'
        WHEN 'order_items_send' THEN '>=22'
    END AS '预期字段数',
    CASE 
        WHEN TABLE_NAME = 'orders' AND COUNT(*) >= 35 THEN '✓ 通过'
        WHEN TABLE_NAME = 'order_items' AND COUNT(*) >= 31 THEN '✓ 通过'
        WHEN TABLE_NAME = 'order_items_recovery' AND COUNT(*) >= 6 THEN '✓ 通过'
        WHEN TABLE_NAME = 'order_items_send' AND COUNT(*) >= 22 THEN '✓ 通过'
        ELSE '✗ 失败'
    END AS '验证结果'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME IN ('orders', 'order_items', 'order_items_recovery', 'order_items_send')
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 ('orders', 'order_items', 'order_items_recovery', 'order_items_send')
ORDER BY t.name;

PRINT '';

-- ============================================================
-- 4. 验证唯一约束
-- ============================================================
PRINT '4. 验证唯一约束';
PRINT '----------------------------------------';

SELECT 
    t.name AS '表名',
    i.name AS '约束名称',
    c.name AS '约束列',
    '✓ 存在' AS '验证结果'
FROM sys.tables t
INNER JOIN sys.indexes i ON t.object_id = i.object_id AND i.is_unique = 1 AND i.is_primary_key = 0
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 ('orders', 'order_items', 'order_items_recovery', 'order_items_send')
ORDER BY t.name, i.name;

PRINT '';

-- ============================================================
-- 5. 验证索引
-- ============================================================
PRINT '5. 验证索引';
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 ('orders', 'order_items', 'order_items_recovery', 'order_items_send')
AND i.name IS NOT NULL
ORDER BY t.name, i.name;

PRINT '';

-- ============================================================
-- 6. 验证表注释
-- ============================================================
PRINT '6. 验证表注释';
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 ('orders', 'order_items', 'order_items_recovery', 'order_items_send')
ORDER BY t.name;

PRINT '';

-- ============================================================
-- 7. 验证关键字段注释数量
-- ============================================================
PRINT '7. 验证关键字段注释数量';
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 ('orders', 'order_items', 'order_items_recovery', 'order_items_send')
GROUP BY t.name
ORDER BY t.name;

PRINT '';

-- ============================================================
-- 8. 验证支付方式组合字段 (Requirements 3.5)
-- ============================================================
PRINT '8. 验证支付方式组合字段';
PRINT '----------------------------------------';

DECLARE @payment_fields INT = 0;

SELECT @payment_fields = COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'orders'
AND COLUMN_NAME IN ('use_money', 'use_integral', 'use_score', 'use_coupon', 'use_money2', 'use_draw', 'use_item_card');

IF @payment_fields >= 7
    PRINT '✓ 支付方式组合字段完整 (use_money, use_integral, use_score, use_coupon, use_money2, use_draw, use_item_card)';
ELSE
    PRINT '✗ 支付方式组合字段不完整 (当前: ' + CAST(@payment_fields AS VARCHAR) + '/7)';

PRINT '';

-- ============================================================
-- 9. 验证订单状态和类型字段
-- ============================================================
PRINT '9. 验证订单状态和类型字段';
PRINT '----------------------------------------';

DECLARE @status_fields INT = 0;

SELECT @status_fields = COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'orders'
AND COLUMN_NAME IN ('status', 'pay_type', 'order_type', 'is_flw', 'is_mibao', 'is_shou_zhe');

IF @status_fields >= 6
    PRINT '✓ 订单状态和类型字段完整 (status, pay_type, order_type, is_flw, is_mibao, is_shou_zhe)';
ELSE
    PRINT '✗ 订单状态和类型字段不完整 (当前: ' + CAST(@status_fields AS VARCHAR) + '/6)';

PRINT '';

-- ============================================================
-- 10. 验证订单详情关键字段
-- ============================================================
PRINT '10. 验证订单详情关键字段';
PRINT '----------------------------------------';

DECLARE @order_items_key_fields INT = 0;

SELECT @order_items_key_fields = COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'order_items'
AND COLUMN_NAME IN ('order_id', 'user_id', 'goods_id', 'shang_id', 'goodslist_id', 'status', 'recovery_num', 'send_num');

IF @order_items_key_fields >= 8
    PRINT '✓ 订单详情关键字段完整 (order_id, user_id, goods_id, shang_id, goodslist_id, status, recovery_num, send_num)';
ELSE
    PRINT '✗ 订单详情关键字段不完整 (当前: ' + CAST(@order_items_key_fields AS VARCHAR) + '/8)';

PRINT '';

-- ============================================================
-- 11. 验证发货记录关键字段
-- ============================================================
PRINT '11. 验证发货记录关键字段';
PRINT '----------------------------------------';

DECLARE @send_key_fields INT = 0;

SELECT @send_key_fields = COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'order_items_send'
AND COLUMN_NAME IN ('user_id', 'send_num', 'status', 'name', 'mobile', 'address', 'courier_number', 'courier_name');

IF @send_key_fields >= 8
    PRINT '✓ 发货记录关键字段完整 (user_id, send_num, status, name, mobile, address, courier_number, courier_name)';
ELSE
    PRINT '✗ 发货记录关键字段不完整 (当前: ' + CAST(@send_key_fields AS VARCHAR) + '/8)';

PRINT '';

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

DECLARE @total_tables INT = @orders_exists + @order_items_exists + @order_items_recovery_exists + @order_items_send_exists;

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

IF @payment_fields >= 7
    PRINT '✓ 支付方式组合字段完整';
ELSE
    PRINT '✗ 支付方式组合字段不完整';

IF @status_fields >= 6
    PRINT '✓ 订单状态和类型字段完整';
ELSE
    PRINT '✗ 订单状态和类型字段不完整';

IF @order_items_key_fields >= 8
    PRINT '✓ 订单详情关键字段完整';
ELSE
    PRINT '✗ 订单详情关键字段不完整';

IF @send_key_fields >= 8
    PRINT '✓ 发货记录关键字段完整';
ELSE
    PRINT '✗ 发货记录关键字段不完整';

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