👤 玩家明细
| 玩家 | 消费 ↓ | 收入 ↓ | 净变动 |
|---|
setTime(0, 0, 0)->getTimestamp(); $todayEnd = $todayStart + 86400; $todayLabel = $now->format('Y-m-d'); // JSON API 模式:?action=data $isApi = (getParam('action') === 'data'); function getTodayBondData() { global $todayStart, $todayEnd; $spendData = []; $incomeData = []; try { $db = getDB(); // ========================================== // 单一数据源:game_transactions(Java同步的完整交易记录) // 每笔交易都有 balance_before / balance_after, // 用余额变化方向判断收入/支出,100% 准确无重复。 // // 不使用 web_transactions 的原因: // 同一笔 Web 商城购买会同时出现在 web_transactions(PHP端) // 和 game_transactions(Java同步端),导致消费重复计算。 // ========================================== // 收入:余额增加 $stmt = $db->prepare("SELECT player_name, SUM(balance_after - balance_before) AS total_income FROM game_transactions WHERE balance_after > balance_before AND tx_time >= :s1 AND tx_time < :e1 GROUP BY player_name"); $stmt->bindValue(':s1', $todayStart, SQLITE3_INTEGER); $stmt->bindValue(':e1', $todayEnd, SQLITE3_INTEGER); $res = $stmt->execute(); while ($row = $res->fetchArray(SQLITE3_ASSOC)) { $incomeData[$row['player_name']] = ($incomeData[$row['player_name']] ?? 0) + (int)$row['total_income']; } // 消费:余额减少 $stmt2 = $db->prepare("SELECT player_name, SUM(balance_before - balance_after) AS total_spend FROM game_transactions WHERE balance_after < balance_before AND tx_time >= :s2 AND tx_time < :e2 GROUP BY player_name"); $stmt2->bindValue(':s2', $todayStart, SQLITE3_INTEGER); $stmt2->bindValue(':e2', $todayEnd, SQLITE3_INTEGER); $res2 = $stmt2->execute(); while ($row = $res2->fetchArray(SQLITE3_ASSOC)) { $spendData[$row['player_name']] = ($spendData[$row['player_name']] ?? 0) + (int)$row['total_spend']; } } catch (\Throwable $e) { debugLog('[today_bonds] 查询异常: ' . $e->getMessage()); } $allPlayers = array_unique(array_merge(array_keys($spendData), array_keys($incomeData))); $rows = []; $totalSpend = 0; $totalIncome = 0; foreach ($allPlayers as $p) { $spend = $spendData[$p] ?? 0; $income = $incomeData[$p] ?? 0; $net = $income - $spend; $totalSpend += $spend; $totalIncome += $income; $rows[] = ['name' => (string)$p, 'spend' => $spend, 'income' => $income, 'net' => $net]; } usort($rows, fn($a,$b) => $b['net'] - $a['net']); return [ 'rows' => $rows, 'total_spend' => $totalSpend, 'total_income' => $totalIncome, 'net_total' => $totalIncome - $totalSpend, 'player_count' => count($rows), 'date_label' => $GLOBALS['todayLabel'], ]; } if ($isApi) { success(getTodayBondData()); exit; } $initialState = json_encode(getTodayBondData(), JSON_UNESCAPED_UNICODE); ?>