-- 检查提现数据是否存在

-- 1. 检查 withdrawals 表是否存在
SHOW TABLES LIKE 'withdrawals';

-- 2. 查看表结构
DESC withdrawals;

-- 3. 查看所有提现记录
SELECT * FROM withdrawals ORDER BY created_at DESC LIMIT 10;

-- 4. 统计提现记录数量
SELECT 
  COUNT(*) as 总数,
  SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as 待处理,
  SUM(CASE WHEN status = 'success' THEN 1 ELSE 0 END) as 已完成,
  SUM(CASE WHEN status = 'failed' THEN 1 ELSE 0 END) as 已拒绝
FROM withdrawals;

-- 5. 如果没有数据，插入测试数据
-- 注意：需要先确认用户ID存在

-- 查看有推荐码的用户
SELECT id, nickname, referral_code, withdrawn_earnings 
FROM users 
WHERE referral_code IS NOT NULL 
LIMIT 5;

-- 查看有推荐订单的用户
SELECT 
  o.referrer_id,
  u.nickname,
  u.referral_code,
  COUNT(*) as 订单数,
  SUM(o.amount) as 订单总额,
  ROUND(SUM(o.amount) * 0.9, 2) as 累计佣金
FROM orders o
LEFT JOIN users u ON o.referrer_id = u.id
WHERE o.referrer_id IS NOT NULL 
  AND o.status = 'paid'
GROUP BY o.referrer_id, u.nickname, u.referral_code
LIMIT 5;

-- 插入测试提现记录（使用上面查到的用户ID）
-- 示例：
-- INSERT INTO withdrawals (id, user_id, amount, status, wechat_openid, created_at) 
-- VALUES ('W_TEST_' || UNIX_TIMESTAMP(), '替换为实际用户ID', 50, 'pending', NULL, NOW());
