-- 礼物系统完整诊断报告

SELECT '========== 礼物系统诊断报告 ==========' as '';

-- 1. 礼物记录统计
SELECT '1. 礼物记录统计' as '';
SELECT 
    COUNT(*) as '总记录数',
    COUNT(DISTINCT sender_id) as '送礼用户数',
    COUNT(DISTINCT receiver_id) as '收礼用户数',
    SUM(total_price) as '总金额',
    AVG(total_price) as '平均金额'
FROM eb_gift_record;

-- 2. 最受欢迎的礼物
SELECT '2. 最受欢迎的礼物 TOP 5' as '';
SELECT 
    gift_name,
    COUNT(*) as '送出次数',
    SUM(quantity) as '总数量',
    SUM(total_price) as '总金额'
FROM eb_gift_record
GROUP BY gift_name
ORDER BY COUNT(*) DESC
LIMIT 5;

-- 3. 收礼排行榜
SELECT '3. 收礼排行榜 TOP 5' as '';
SELECT 
    receiver_id,
    receiver_name,
    COUNT(*) as '收礼次数',
    SUM(total_price) as '收礼总额'
FROM eb_gift_record
GROUP BY receiver_id, receiver_name
ORDER BY SUM(total_price) DESC
LIMIT 5;

-- 4. 送礼排行榜
SELECT '4. 送礼排行榜 TOP 5' as '';
SELECT 
    sender_id,
    sender_name,
    COUNT(*) as '送礼次数',
    SUM(total_price) as '送礼总额'
FROM eb_gift_record
GROUP BY sender_id, sender_name
ORDER BY SUM(total_price) DESC
LIMIT 5;

-- 5. 按直播间统计
SELECT '5. 直播间礼物统计' as '';
SELECT 
    r.room_id,
    lr.title as '直播间名称',
    COUNT(*) as '礼物数量',
    SUM(r.total_price) as '礼物总额'
FROM eb_gift_record r
LEFT JOIN eb_live_room lr ON r.room_id = lr.id
GROUP BY r.room_id, lr.title
ORDER BY SUM(r.total_price) DESC;

-- 6. 礼物配置检查
SELECT '6. 礼物配置列表' as '';
SELECT 
    id,
    name,
    price,
    sort_order,
    is_enabled,
    CASE is_enabled WHEN 1 THEN '启用' ELSE '禁用' END as '状态'
FROM eb_gift_config
ORDER BY sort_order;

-- 7. 用户虚拟货币余额
SELECT '7. 测试用户虚拟货币余额' as '';
SELECT 
    uid,
    nickname,
    virtual_balance as '余额',
    CASE 
        WHEN virtual_balance >= 10000 THEN '充足'
        WHEN virtual_balance >= 1000 THEN '正常'
        WHEN virtual_balance > 0 THEN '偏低'
        ELSE '不足'
    END as '状态'
FROM eb_user
WHERE uid IN (43, 44, 45)
ORDER BY uid;

-- 8. 最近礼物记录
SELECT '8. 最近 10 条礼物记录' as '';
SELECT 
    id,
    sender_name as '送礼者',
    receiver_name as '接收者',
    gift_name as '礼物',
    quantity as '数量',
    total_price as '总价',
    DATE_FORMAT(create_time, '%Y-%m-%d %H:%i:%s') as '时间'
FROM eb_gift_record
ORDER BY create_time DESC
LIMIT 10;

-- 9. 充值记录统计
SELECT '9. 充值记录统计' as '';
SELECT 
    COUNT(*) as '充值记录数',
    COALESCE(SUM(virtual_amount), 0) as '充值总额',
    COUNT(CASE WHEN payment_status = 1 THEN 1 END) as '成功支付数'
FROM eb_virtual_currency_recharge;

-- 10. 充值套餐配置
SELECT '10. 充值套餐配置' as '';
SELECT 
    id,
    title as '套餐名称',
    amount as '金额',
    virtual_amount as '虚拟币',
    bonus_amount as '赠送',
    CASE is_hot WHEN 1 THEN '是' ELSE '否' END as '热门',
    CASE is_enabled WHEN 1 THEN '启用' ELSE '禁用' END as '状态'
FROM eb_recharge_package
ORDER BY sort_order;

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