-- 礼物系统完整诊断脚本

-- 1. 检查礼物记录表
SELECT '=== 礼物记录表检查 ===' as '';
SELECT COUNT(*) as '礼物记录总数' FROM eb_gift_record;
SELECT * FROM eb_gift_record ORDER BY create_time DESC LIMIT 5;

-- 2. 检查礼物配置表
SELECT '=== 礼物配置表检查 ===' as '';
SELECT COUNT(*) as '礼物配置总数' FROM eb_gift_config;
SELECT * FROM eb_gift_config ORDER BY sort_order LIMIT 5;

-- 3. 检查用户虚拟货币
SELECT '=== 用户虚拟货币检查 ===' as '';
SELECT u.uid, u.nickname, u.phone, u.virtual_balance
FROM eb_user u
WHERE u.uid IN (43, 44, 45)
ORDER BY u.uid;

-- 4. 检查充值记录
SELECT '=== 充值记录检查 ===' as '';
SELECT COUNT(*) as '充值记录总数' FROM eb_virtual_currency_recharge;
SELECT * FROM eb_virtual_currency_recharge ORDER BY create_time DESC LIMIT 5;

-- 5. 检查直播间
SELECT '=== 直播间检查 ===' as '';
SELECT id, title, uid as streamer_id, is_live as status FROM eb_live_room WHERE id IN (8, 9, 10) ORDER BY id;

-- 6. 如果没有测试数据，插入一些
SELECT '=== 开始插入测试数据 ===' as '';

-- 确保用户有虚拟货币余额
UPDATE eb_user 
SET virtual_balance = 10000 
WHERE uid IN (43, 44, 45) AND (virtual_balance IS NULL OR virtual_balance = 0);

-- 插入测试礼物记录
INSERT INTO eb_gift_record (sender_id, sender_name, receiver_id, receiver_name, room_id, gift_id, gift_name, gift_price, quantity, total_price, create_time)
SELECT 
    43 as sender_id,
    '测试用户43' as sender_name,
    44 as receiver_id,
    '测试用户44' as receiver_name,
    8 as room_id,
    1 as gift_id,
    '玫瑰花' as gift_name,
    10 as gift_price,
    5 as quantity,
    50 as total_price,
    NOW() as create_time
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM eb_gift_record WHERE sender_id = 43 AND receiver_id = 44 LIMIT 1);

INSERT INTO eb_gift_record (sender_id, sender_name, receiver_id, receiver_name, room_id, gift_id, gift_name, gift_price, quantity, total_price, create_time)
SELECT 
    45 as sender_id,
    '测试用户45' as sender_name,
    44 as receiver_id,
    '测试用户44' as receiver_name,
    8 as room_id,
    5 as gift_id,
    '跑车' as gift_name,
    500 as gift_price,
    1 as quantity,
    500 as total_price,
    NOW() - INTERVAL 1 HOUR as create_time
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM eb_gift_record WHERE sender_id = 45 AND receiver_id = 44 LIMIT 1);

INSERT INTO eb_gift_record (sender_id, sender_name, receiver_id, receiver_name, room_id, gift_id, gift_name, gift_price, quantity, total_price, create_time)
SELECT 
    43 as sender_id,
    '测试用户43' as sender_name,
    45 as receiver_id,
    '测试用户45' as receiver_name,
    9 as room_id,
    1 as gift_id,
    '爱心' as gift_name,
    1 as gift_price,
    100 as quantity,
    100 as total_price,
    NOW() - INTERVAL 2 HOUR as create_time
FROM DUAL
WHERE (SELECT COUNT(*) FROM eb_gift_record) < 3;

-- 7. 验证插入结果
SELECT '=== 验证测试数据 ===' as '';
SELECT COUNT(*) as '当前礼物记录总数' FROM eb_gift_record;

SELECT 
    g.id,
    g.gift_name,
    g.gift_price,
    g.quantity,
    g.total_price,
    sender.nickname as sender_name,
    receiver.nickname as receiver_name,
    g.room_id,
    g.create_time
FROM eb_gift_record g
LEFT JOIN eb_user sender ON g.sender_id = sender.uid
LEFT JOIN eb_user receiver ON g.receiver_id = receiver.uid
ORDER BY g.create_time DESC
LIMIT 10;

SELECT '=== 诊断完成 ===' as '';
