-- 提现数据验证脚本
-- 用于手动验证用户的佣金和提现数据是否准确

-- ============================================
-- 1. 查看用户的完整佣金情况
-- ============================================

-- 替换 'YOUR_USER_ID' 为实际的用户ID
SET @user_id = 'YOUR_USER_ID';

SELECT 
  '=== 用户基本信息 ===' as info;

SELECT 
  id as 用户ID,
  nickname as 昵称,
  referral_code as 推荐码,
  earnings as 已结算收益,
  pending_earnings as 待结算收益,
  withdrawn_earnings as 已提现金额
FROM users 
WHERE id = @user_id;

-- ============================================
-- 2. 计算累计佣金（从订单表）
-- ============================================

SELECT 
  '=== 累计佣金计算 ===' as info;

SELECT 
  COUNT(*) as 订单数量,
  SUM(amount) as 订单总金额,
  ROUND(SUM(amount) * 0.9, 2) as 累计佣金_90分成
FROM orders 
WHERE referrer_id = @user_id 
  AND status = 'paid';

-- ============================================
-- 3. 查看提现记录
-- ============================================

SELECT 
  '=== 提现记录 ===' as info;

SELECT 
  id as 提现ID,
  amount as 金额,
  status as 状态,
  created_at as 申请时间,
  processed_at as 处理时间
FROM withdrawals 
WHERE user_id = @user_id
ORDER BY created_at DESC;

-- ============================================
-- 4. 统计提现金额
-- ============================================

SELECT 
  '=== 提现统计 ===' as info;

SELECT 
  SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) as 待审核金额,
  SUM(CASE WHEN status = 'success' THEN amount ELSE 0 END) as 已完成金额,
  SUM(CASE WHEN status = 'failed' THEN amount ELSE 0 END) as 已拒绝金额,
  SUM(amount) as 总申请金额
FROM withdrawals 
WHERE user_id = @user_id;

-- ============================================
-- 5. 综合计算可提现金额
-- ============================================

SELECT 
  '=== 可提现金额计算 ===' as info;

SELECT 
  -- 累计佣金
  ROUND((SELECT COALESCE(SUM(amount), 0) FROM orders WHERE referrer_id = @user_id AND status = 'paid') * 0.9, 2) as 累计佣金,
  
  -- 已提现金额
  (SELECT withdrawn_earnings FROM users WHERE id = @user_id) as 已提现金额,
  
  -- 待审核金额
  (SELECT COALESCE(SUM(amount), 0) FROM withdrawals WHERE user_id = @user_id AND status = 'pending') as 待审核金额,
  
  -- 可提现金额
  ROUND(
    (SELECT COALESCE(SUM(amount), 0) FROM orders WHERE referrer_id = @user_id AND status = 'paid') * 0.9
    - (SELECT withdrawn_earnings FROM users WHERE id = @user_id)
    - (SELECT COALESCE(SUM(amount), 0) FROM withdrawals WHERE user_id = @user_id AND status = 'pending'),
    2
  ) as 可提现金额;

-- ============================================
-- 6. 查看所有用户的提现概览
-- ============================================

SELECT 
  '=== 所有用户提现概览 ===' as info;

SELECT 
  u.id as 用户ID,
  u.nickname as 昵称,
  ROUND(COALESCE(orders_sum.total_amount, 0) * 0.9, 2) as 累计佣金,
  u.withdrawn_earnings as 已提现,
  COALESCE(pending_sum.pending_amount, 0) as 待审核,
  ROUND(
    COALESCE(orders_sum.total_amount, 0) * 0.9 
    - u.withdrawn_earnings 
    - COALESCE(pending_sum.pending_amount, 0),
    2
  ) as 可提现金额
FROM users u
LEFT JOIN (
  SELECT referrer_id, SUM(amount) as total_amount
  FROM orders
  WHERE status = 'paid'
  GROUP BY referrer_id
) orders_sum ON u.id = orders_sum.referrer_id
LEFT JOIN (
  SELECT user_id, SUM(amount) as pending_amount
  FROM withdrawals
  WHERE status = 'pending'
  GROUP BY user_id
) pending_sum ON u.id = pending_sum.user_id
WHERE u.referral_code IS NOT NULL
  AND (orders_sum.total_amount > 0 OR u.withdrawn_earnings > 0)
ORDER BY 累计佣金 DESC
LIMIT 20;

-- ============================================
-- 7. 查找异常数据
-- ============================================

SELECT 
  '=== 异常数据检查 ===' as info;

-- 检查：待审核金额 > 累计佣金
SELECT 
  u.id as 用户ID,
  u.nickname as 昵称,
  ROUND(COALESCE(orders_sum.total_amount, 0) * 0.9, 2) as 累计佣金,
  COALESCE(pending_sum.pending_amount, 0) as 待审核金额,
  '超额提现风险' as 异常类型
FROM users u
LEFT JOIN (
  SELECT referrer_id, SUM(amount) as total_amount
  FROM orders
  WHERE status = 'paid'
  GROUP BY referrer_id
) orders_sum ON u.id = orders_sum.referrer_id
LEFT JOIN (
  SELECT user_id, SUM(amount) as pending_amount
  FROM withdrawals
  WHERE status = 'pending'
  GROUP BY user_id
) pending_sum ON u.id = pending_sum.user_id
WHERE COALESCE(pending_sum.pending_amount, 0) > ROUND(COALESCE(orders_sum.total_amount, 0) * 0.9, 2)
LIMIT 10;

-- 检查：已提现 > 累计佣金
SELECT 
  u.id as 用户ID,
  u.nickname as 昵称,
  ROUND(COALESCE(orders_sum.total_amount, 0) * 0.9, 2) as 累计佣金,
  u.withdrawn_earnings as 已提现金额,
  '已提现超过累计' as 异常类型
FROM users u
LEFT JOIN (
  SELECT referrer_id, SUM(amount) as total_amount
  FROM orders
  WHERE status = 'paid'
  GROUP BY referrer_id
) orders_sum ON u.id = orders_sum.referrer_id
WHERE u.withdrawn_earnings > ROUND(COALESCE(orders_sum.total_amount, 0) * 0.9, 2)
  AND u.withdrawn_earnings > 0
LIMIT 10;

-- ============================================
-- 8. 提现数据统计
-- ============================================

SELECT 
  '=== 提现数据统计 ===' as info;

SELECT 
  status as 状态,
  COUNT(*) as 数量,
  SUM(amount) as 总金额,
  AVG(amount) as 平均金额,
  MIN(amount) as 最小金额,
  MAX(amount) as 最大金额
FROM withdrawals
GROUP BY status
ORDER BY 
  CASE status 
    WHEN 'pending' THEN 1 
    WHEN 'processing' THEN 2 
    WHEN 'success' THEN 3 
    WHEN 'failed' THEN 4 
  END;
