From ba484e957d87db840913e42cb3e28521ad71b860 Mon Sep 17 00:00:00 2001 From: ypshidifu Date: Mon, 13 Jul 2026 20:25:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D(today=5Fbonds,online=5Fcurve?= =?UTF-8?q?):=20=E5=80=BA=E5=88=B8=E7=9C=8B=E6=9D=BF=E6=B6=88=E8=B4=B9?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=BA=90=E9=94=99=E8=AF=AF+=E5=9C=A8?= =?UTF-8?q?=E7=BA=BF=E6=9B=B2=E7=BA=BF=E4=B8=8D=E8=AE=B0=E5=BD=95=E5=BF=AB?= =?UTF-8?q?=E7=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 今日债券: - 消费改为从 web_transactions.amount<0 取绝对值 - 收入从 web_transactions.amount>0 取 - 移除 orders.db.cashier_orders 消费查询(导致消费总是0) 在线曲线: - 新增 ensureRecentSnapshot() 自动记录机制 - 每次 action=data 时检查上次快照是否>55秒前,是则自动记录 - 避免后台只调 action=data 导致永不记录快照的 bug --- online_curve.php | 20 ++++++++++++++++++++ today_bonds.php | 26 +++++++++++--------------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/online_curve.php b/online_curve.php index 8b0d949..4f34fe3 100644 --- a/online_curve.php +++ b/online_curve.php @@ -40,9 +40,29 @@ function recordSnapshot() { return $count; } +// ===== 确保有最新快照(如果上次快照超过60秒则自动记录) ===== +function ensureRecentSnapshot() { + try { + $db = getDB(); + $res = $db->query("SELECT MAX(snapshot_time) AS last_time FROM online_snapshots"); + $row = $res->fetchArray(SQLITE3_ASSOC); + $lastTime = (int)($row['last_time'] ?? 0); + $now = time(); + // 没有快照,或上次快照超过 55 秒(留 5 秒余量防止频繁写) + if ($lastTime === 0 || ($now - $lastTime) > 55) { + return recordSnapshot(); + } + } catch (\Throwable $e) { + debugLog('[online_curve] ensureRecentSnapshot 异常: ' . $e->getMessage()); + } + return null; +} + // ===== 获取24小时聚合数据(按小时) ===== function getHourlyData() { ensureSnapshotTable(); + // 每次查询数据时自动确保有最新快照(如果超过60秒未记录) + ensureRecentSnapshot(); // 24小时前的时间戳 $since = time() - 86400; // 按自然小时聚合:分桶到 h0~h23,取平均值 diff --git a/today_bonds.php b/today_bonds.php index 6e51093..0d5d79b 100644 --- a/today_bonds.php +++ b/today_bonds.php @@ -17,22 +17,10 @@ function getTodayBondData() { global $todayStart, $todayEnd; $spendData = []; $incomeData = []; - // orders.db 今日消费 - try { - $odb = getOrdersDB(); - $stmt = $odb->prepare("SELECT player_name, SUM(total_price) AS total_spend FROM cashier_orders WHERE status='completed' AND created_at >= :start AND created_at < :end GROUP BY player_name ORDER BY total_spend DESC"); - $stmt->bindValue(':start', $todayStart, SQLITE3_INTEGER); - $stmt->bindValue(':end', $todayEnd, SQLITE3_INTEGER); - $res = $stmt->execute(); - while ($row = $res->fetchArray(SQLITE3_ASSOC)) { - $spendData[$row['player_name']] = (int)$row['total_spend']; - } - } catch (\Throwable $e) { - debugLog('[today_bonds] 查询消费异常: ' . $e->getMessage()); - } - // web.db 今日收入(amount>0) + // web.db 今日债券变动:amount > 0 为收入,amount < 0 为消费(取绝对值) try { $db = getDB(); + // 收入 $stmt = $db->prepare("SELECT player_name, SUM(amount) AS total_income FROM web_transactions WHERE amount > 0 AND created_at >= :start AND created_at < :end GROUP BY player_name ORDER BY total_income DESC"); $stmt->bindValue(':start', $todayStart, SQLITE3_INTEGER); $stmt->bindValue(':end', $todayEnd, SQLITE3_INTEGER); @@ -40,8 +28,16 @@ function getTodayBondData() { while ($row = $res->fetchArray(SQLITE3_ASSOC)) { $incomeData[$row['player_name']] = (int)$row['total_income']; } + // 消费(负金额绝对值) + $stmt2 = $db->prepare("SELECT player_name, SUM(ABS(amount)) AS total_spend FROM web_transactions WHERE amount < 0 AND created_at >= :start AND created_at < :end GROUP BY player_name ORDER BY total_spend DESC"); + $stmt2->bindValue(':start', $todayStart, SQLITE3_INTEGER); + $stmt2->bindValue(':end', $todayEnd, SQLITE3_INTEGER); + $res2 = $stmt2->execute(); + while ($row = $res2->fetchArray(SQLITE3_ASSOC)) { + $spendData[$row['player_name']] = (int)$row['total_spend']; + } } catch (\Throwable $e) { - debugLog('[today_bonds] 查询收入异常: ' . $e->getMessage()); + debugLog('[today_bonds] 查询 web_transactions 异常: ' . $e->getMessage()); } $allPlayers = array_unique(array_merge(array_keys($spendData), array_keys($incomeData))); $rows = [];