修复(today_bonds,online_curve): 债券看板消费数据源错误+在线曲线不记录快照
今日债券: - 消费改为从 web_transactions.amount<0 取绝对值 - 收入从 web_transactions.amount>0 取 - 移除 orders.db.cashier_orders 消费查询(导致消费总是0) 在线曲线: - 新增 ensureRecentSnapshot() 自动记录机制 - 每次 action=data 时检查上次快照是否>55秒前,是则自动记录 - 避免后台只调 action=data 导致永不记录快照的 bug
This commit is contained in:
@@ -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,取平均值
|
||||
|
||||
+11
-15
@@ -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 = [];
|
||||
|
||||
Reference in New Issue
Block a user