-- ========================================
-- 验证方案A数据库完整性
-- 创建日期：2026-01-23
-- 数据库：peixue
-- ========================================

USE peixue;

SELECT '========================================' AS '';
SELECT '方案A数据库完整性验证' AS '';
SELECT '========================================' AS '';

-- ========================================
-- 1. 检查新表是否存在
-- ========================================

SELECT '1. 检查新表是否存在' AS '';
SELECT '----------------------------------------' AS '';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ coupon表存在' ELSE '❌ coupon表不存在' END AS result
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'coupon';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ user_coupon表存在' ELSE '❌ user_coupon表不存在' END AS result
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'user_coupon';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ user_address表存在' ELSE '❌ user_address表不存在' END AS result
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'user_address';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ user_package表存在' ELSE '❌ user_package表不存在' END AS result
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'user_package';

-- ========================================
-- 2. 检查订单表新字段
-- ========================================

SELECT '' AS '';
SELECT '2. 检查订单表新字段' AS '';
SELECT '----------------------------------------' AS '';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ payment_method字段存在' ELSE '❌ payment_method字段不存在' END AS result
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND COLUMN_NAME = 'payment_method';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ wallet_amount字段存在' ELSE '❌ wallet_amount字段不存在' END AS result
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND COLUMN_NAME = 'wallet_amount';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ timecard_id字段存在' ELSE '❌ timecard_id字段不存在' END AS result
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND COLUMN_NAME = 'timecard_id';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ package_id字段存在' ELSE '❌ package_id字段不存在' END AS result
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND COLUMN_NAME = 'package_id';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ coupon_id字段存在' ELSE '❌ coupon_id字段不存在' END AS result
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND COLUMN_NAME = 'coupon_id';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ coupon_discount字段存在' ELSE '❌ coupon_discount字段不存在' END AS result
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND COLUMN_NAME = 'coupon_discount';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ service_address_id字段存在' ELSE '❌ service_address_id字段不存在' END AS result
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND COLUMN_NAME = 'service_address_id';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ original_amount字段存在' ELSE '❌ original_amount字段不存在' END AS result
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND COLUMN_NAME = 'original_amount';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ discount_amount字段存在' ELSE '❌ discount_amount字段不存在' END AS result
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND COLUMN_NAME = 'discount_amount';

-- ========================================
-- 3. 检查索引
-- ========================================

SELECT '' AS '';
SELECT '3. 检查订单表索引' AS '';
SELECT '----------------------------------------' AS '';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ idx_timecard_id索引存在' ELSE '❌ idx_timecard_id索引不存在' END AS result
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND INDEX_NAME = 'idx_timecard_id';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ idx_package_id索引存在' ELSE '❌ idx_package_id索引不存在' END AS result
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND INDEX_NAME = 'idx_package_id';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ idx_coupon_id索引存在' ELSE '❌ idx_coupon_id索引不存在' END AS result
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND INDEX_NAME = 'idx_coupon_id';

SELECT 
    CASE WHEN COUNT(*) > 0 THEN '✅ idx_service_address_id索引存在' ELSE '❌ idx_service_address_id索引不存在' END AS result
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'peixue' AND TABLE_NAME = 'order' AND INDEX_NAME = 'idx_service_address_id';

-- ========================================
-- 4. 查看测试数据
-- ========================================

SELECT '' AS '';
SELECT '4. 查看测试数据' AS '';
SELECT '----------------------------------------' AS '';

SELECT '优惠券测试数据：' AS '';
SELECT id, name, type, discount_type, discount_value, min_amount, status 
FROM coupon 
LIMIT 5;

SELECT '' AS '';
SELECT '用户地址测试数据：' AS '';
SELECT id, user_id, contact_name, contact_phone, province, city, district, address 
FROM user_address 
LIMIT 5;

-- ========================================
-- 5. 统计信息
-- ========================================

SELECT '' AS '';
SELECT '5. 数据统计' AS '';
SELECT '----------------------------------------' AS '';

SELECT 
    (SELECT COUNT(*) FROM coupon) AS '优惠券数量',
    (SELECT COUNT(*) FROM user_coupon) AS '用户优惠券数量',
    (SELECT COUNT(*) FROM user_address) AS '用户地址数量',
    (SELECT COUNT(*) FROM user_package) AS '用户套餐数量';

-- ========================================
-- 完成
-- ========================================

SELECT '' AS '';
SELECT '========================================' AS '';
SELECT '✅ 方案A数据库验证完成！' AS '';
SELECT '========================================' AS '';
