-- 只读账务核查 SQL,不自动修正数据。 -- 用户余额与统一账务净额核对。 SELECT u.id AS user_id, u.traffic_balance AS snapshot_bytes, COALESCE(SUM(CASE WHEN l.direction = 'credit' THEN l.amount_bytes ELSE -l.amount_bytes END), 0) AS ledger_net_bytes, u.traffic_balance - COALESCE(SUM(CASE WHEN l.direction = 'credit' THEN l.amount_bytes ELSE -l.amount_bytes END), 0) AS difference_bytes FROM `user` u LEFT JOIN traffic_ledger l ON l.account_type = 'user' AND l.account_id = u.id GROUP BY u.id, u.traffic_balance HAVING difference_bytes <> 0; -- 平台资源池与采购、平台消费流水核对。 SELECT p.id AS pool_id, p.total_bytes, p.available_bytes, p.consumed_bytes, COALESCE((SELECT SUM(total_bytes) FROM platform_resource_purchase WHERE status <> 'cancelled'), 0) AS purchased_bytes, COALESCE((SELECT SUM(amount_bytes) FROM traffic_ledger WHERE account_type = 'platform' AND direction = 'debit'), 0) AS ledger_consumed_bytes, p.total_bytes - COALESCE((SELECT SUM(total_bytes) FROM platform_resource_purchase WHERE status <> 'cancelled'), 0) AS purchase_difference_bytes, p.consumed_bytes - COALESCE((SELECT SUM(amount_bytes) FROM traffic_ledger WHERE account_type = 'platform' AND direction = 'debit'), 0) AS consumption_difference_bytes FROM platform_resource_pool p; -- 充值订单与充值流水核对。 SELECT o.id AS order_id, o.user_id, o.pay_status, o.credit_status, o.amount_bytes, l.id AS ledger_id, l.amount_bytes AS ledger_amount_bytes, l.idempotency_key FROM traffic_order o LEFT JOIN traffic_ledger l ON l.id = o.credit_ledger_id WHERE o.pay_status = 'paid' AND (o.credit_status <> 'credited' OR l.id IS NULL OR l.direction <> 'credit' OR l.amount_bytes <> o.amount_bytes); -- 直播计费段中未完成结算的记录。 SELECT id, session_id, user_id, period_start, period_end, bytes, status, idempotency_key FROM live_billing_segment WHERE status <> 'settled' ORDER BY created_at ASC, id ASC; -- 下载记录与下载消费流水核对。 SELECT d.id AS download_id, d.user_id, d.video_id, d.bytes, l.id AS ledger_id, l.amount_bytes AS ledger_amount_bytes FROM download_log d LEFT JOIN traffic_ledger l ON l.idempotency_key = CONCAT('download:', d.idempotency_key, ':user') WHERE l.id IS NULL OR l.direction <> 'debit' OR l.amount_bytes <> d.bytes;