-- ============================================================
-- 订单数据迁移验证脚本
-- Feature: database-migration, Property 2: 数据记录数一致性
-- Feature: database-migration, Property 4: 财务数据一致性
-- Validates: Requirements 3.5, 11.1, 11.3
-- ============================================================

SET NOCOUNT ON;

PRINT '========================================';
PRINT '订单数据迁移验证报告';
PRINT '执行时间: ' + CONVERT(VARCHAR, GETDATE(), 120);
PRINT '========================================';
PRINT '';

-- ============================================================
-- 1. 验证记录数一致性 (Property 2)
-- ============================================================
PRINT '1. 验证记录数一致性';
PRINT '----------------------------------------';

DECLARE @orders_count INT;
DECLARE @order_items_count INT;
DECLARE @order_items_recovery_count INT;
DECLARE @order_items_send_count INT;

-- 预期值 (来自 MySQL 源数据库)
DECLARE @expected_orders INT = 15;
DECLARE @expected_order_items INT = 67;
DECLARE @expected_order_items_recovery INT = 8;
DECLARE @expected_order_items_send INT = 0;

SELECT @orders_count = COUNT(*) FROM orders;
SELECT @order_items_count = COUNT(*) FROM order_items;
SELECT @order_items_recovery_count = COUNT(*) FROM order_items_recovery;
SELECT @order_items_send_count = COUNT(*) FROM order_items_send;

PRINT 'orders 表: 预期 ' + CAST(@expected_orders AS VARCHAR) + ' 条, 实际 ' + CAST(@orders_count AS VARCHAR) + ' 条 ' + 
      CASE WHEN @orders_count = @expected_orders THEN '✓ 通过' ELSE '✗ 失败' END;
PRINT 'order_items 表: 预期 ' + CAST(@expected_order_items AS VARCHAR) + ' 条, 实际 ' + CAST(@order_items_count AS VARCHAR) + ' 条 ' + 
      CASE WHEN @order_items_count = @expected_order_items THEN '✓ 通过' ELSE '✗ 失败' END;
PRINT 'order_items_recovery 表: 预期 ' + CAST(@expected_order_items_recovery AS VARCHAR) + ' 条, 实际 ' + CAST(@order_items_recovery_count AS VARCHAR) + ' 条 ' + 
      CASE WHEN @order_items_recovery_count = @expected_order_items_recovery THEN '✓ 通过' ELSE '✗ 失败' END;
PRINT 'order_items_send 表: 预期 ' + CAST(@expected_order_items_send AS VARCHAR) + ' 条, 实际 ' + CAST(@order_items_send_count AS VARCHAR) + ' 条 ' + 
      CASE WHEN @order_items_send_count = @expected_order_items_send THEN '✓ 通过' ELSE '✗ 失败' END;

PRINT '';

-- ============================================================
-- 2. 验证订单金额一致性 (Property 4)
-- ============================================================
PRINT '2. 验证订单金额一致性';
PRINT '----------------------------------------';

DECLARE @total_order_amount DECIMAL(18,2);
DECLARE @total_use_money DECIMAL(18,2);
DECLARE @total_use_integral DECIMAL(18,2);
DECLARE @total_use_money2 DECIMAL(18,2);

-- 预期值 (来自 MySQL 源数据库计算)
DECLARE @expected_total_order_amount DECIMAL(18,2) = 722.10;

SELECT @total_order_amount = SUM(order_total) FROM orders;
SELECT @total_use_money = SUM(use_money) FROM orders;
SELECT @total_use_integral = SUM(use_integral) FROM orders;
SELECT @total_use_money2 = SUM(use_money2) FROM orders;

PRINT '订单总金额 (order_total): ' + CAST(@total_order_amount AS VARCHAR) + ' 元 ' +
      CASE WHEN @total_order_amount = @expected_total_order_amount THEN '✓ 通过' ELSE '✗ 失败 (预期: ' + CAST(@expected_total_order_amount AS VARCHAR) + ')' END;
PRINT '使用余额总额 (use_money): ' + CAST(@total_use_money AS VARCHAR) + ' 元';
PRINT '使用积分总额 (use_integral): ' + CAST(@total_use_integral AS VARCHAR) + ' 元';
PRINT '使用积分2总额 (use_money2): ' + CAST(@total_use_money2 AS VARCHAR) + ' 元';

PRINT '';

-- ============================================================
-- 3. 验证回收金额一致性 (Property 4)
-- ============================================================
PRINT '3. 验证回收金额一致性';
PRINT '----------------------------------------';

DECLARE @total_recovery_money DECIMAL(18,2);
DECLARE @total_recovery_count INT;

-- 预期值 (来自 MySQL 源数据库计算)
DECLARE @expected_recovery_money DECIMAL(18,2) = 112.00;
DECLARE @expected_recovery_count INT = 9;

SELECT @total_recovery_money = SUM(money) FROM order_items_recovery;
SELECT @total_recovery_count = SUM(count) FROM order_items_recovery;

PRINT '回收总金额: ' + CAST(ISNULL(@total_recovery_money, 0) AS VARCHAR) + ' 元 ' +
      CASE WHEN ISNULL(@total_recovery_money, 0) = @expected_recovery_money THEN '✓ 通过' ELSE '✗ 失败 (预期: ' + CAST(@expected_recovery_money AS VARCHAR) + ')' END;
PRINT '回收总数量: ' + CAST(ISNULL(@total_recovery_count, 0) AS VARCHAR) + ' 件 ' +
      CASE WHEN ISNULL(@total_recovery_count, 0) = @expected_recovery_count THEN '✓ 通过' ELSE '✗ 失败 (预期: ' + CAST(@expected_recovery_count AS VARCHAR) + ')' END;

PRINT '';

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

DECLARE @payment_fields_count INT;

SELECT @payment_fields_count = 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_count >= 7
    PRINT '✓ 支付方式组合字段完整 (' + CAST(@payment_fields_count AS VARCHAR) + '/7)';
ELSE
    PRINT '✗ 支付方式组合字段不完整 (' + CAST(@payment_fields_count AS VARCHAR) + '/7)';

PRINT '';

-- ============================================================
-- 5. 验证订单与订单详情关联
-- ============================================================
PRINT '5. 验证订单与订单详情关联';
PRINT '----------------------------------------';

DECLARE @orphan_items INT;

SELECT @orphan_items = COUNT(*)
FROM order_items oi
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.id = oi.order_id);

IF @orphan_items = 0
    PRINT '✓ 所有订单详情都有对应的订单记录';
ELSE
    PRINT '✗ 存在 ' + CAST(@orphan_items AS VARCHAR) + ' 条孤立的订单详情记录';

PRINT '';

-- ============================================================
-- 6. 验证时间戳转换正确性 (Property 3)
-- ============================================================
PRINT '6. 验证时间戳转换正确性';
PRINT '----------------------------------------';

-- 抽样验证: 检查 created_at 是否与 addtime 转换一致
DECLARE @timestamp_mismatch INT;

SELECT @timestamp_mismatch = COUNT(*)
FROM orders
WHERE ABS(DATEDIFF(SECOND, created_at, DATEADD(SECOND, addtime, '1970-01-01'))) > 1;

IF @timestamp_mismatch = 0
    PRINT '✓ 所有订单的时间戳转换正确';
ELSE
    PRINT '✗ 存在 ' + CAST(@timestamp_mismatch AS VARCHAR) + ' 条时间戳转换不一致的记录';

PRINT '';

-- ============================================================
-- 7. 验证订单状态分布
-- ============================================================
PRINT '7. 验证订单状态分布';
PRINT '----------------------------------------';

SELECT 
    status AS '订单状态',
    COUNT(*) AS '数量',
    CASE status
        WHEN 0 THEN '待支付'
        WHEN 1 THEN '已支付'
        WHEN 2 THEN '已取消'
        ELSE '其他'
    END AS '状态说明'
FROM orders
GROUP BY status
ORDER BY status;

PRINT '';

-- ============================================================
-- 8. 验证订单详情状态分布
-- ============================================================
PRINT '8. 验证订单详情状态分布';
PRINT '----------------------------------------';

SELECT 
    status AS '详情状态',
    COUNT(*) AS '数量',
    CASE status
        WHEN 0 THEN '待处理'
        WHEN 1 THEN '已回收'
        WHEN 2 THEN '已发货'
        ELSE '其他'
    END AS '状态说明'
FROM order_items
GROUP BY status
ORDER BY status;

PRINT '';

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

DECLARE @all_passed BIT = 1;

IF @orders_count <> @expected_orders SET @all_passed = 0;
IF @order_items_count <> @expected_order_items SET @all_passed = 0;
IF @order_items_recovery_count <> @expected_order_items_recovery SET @all_passed = 0;
IF @order_items_send_count <> @expected_order_items_send SET @all_passed = 0;
IF @total_order_amount <> @expected_total_order_amount SET @all_passed = 0;
IF @orphan_items > 0 SET @all_passed = 0;
IF @timestamp_mismatch > 0 SET @all_passed = 0;

IF @all_passed = 1
    PRINT '✓ 所有验证项目通过!';
ELSE
    PRINT '✗ 部分验证项目失败，请检查上述详细信息';

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