修复(today_bonds): web_transactions用type区分方向, game_transactions用balance差值
【问题】两表的 amount 始终为正,不能用金额符号判断收支方向。 【修复】 - web_transactions: amount<0 →消费, amount>0中用type区分(shop_buy/shop_cart=消费,其他=收入) - game_transactions: balance_after > balance_before →收入, 反之为消费 - 覆盖 admin_add/deduct/refund 和 Java同步的所有交易类型
This commit is contained in:
+37
-20
@@ -19,39 +19,56 @@ function getTodayBondData() {
|
||||
$incomeData = [];
|
||||
try {
|
||||
$db = getDB();
|
||||
// ===== web_transactions(Web端交易) =====
|
||||
// 收入(amount > 0)
|
||||
$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");
|
||||
$stmt->bindValue(':start', $todayStart, SQLITE3_INTEGER);
|
||||
$stmt->bindValue(':end', $todayEnd, SQLITE3_INTEGER);
|
||||
// ==========================================
|
||||
// web_transactions(Web端交易)
|
||||
// amount < 0 必定为消费(如 admin_deduct)
|
||||
// amount > 0 则根据类型判断方向
|
||||
// ==========================================
|
||||
$spendWebTypes = ['shop_buy', 'shop_cart']; // 金额为正但属于消费的类型
|
||||
|
||||
// 消费:amount < 0 取绝对值
|
||||
$stmt = $db->prepare("SELECT player_name, SUM(ABS(amount)) AS total_spend FROM web_transactions WHERE amount < 0 AND created_at >= :s1 AND created_at < :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'];
|
||||
$spendData[$row['player_name']] = ($spendData[$row['player_name']] ?? 0) + (int)$row['total_spend'];
|
||||
}
|
||||
// 消费(amount < 0 取绝对值)
|
||||
$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");
|
||||
$stmt2->bindValue(':start', $todayStart, SQLITE3_INTEGER);
|
||||
$stmt2->bindValue(':end', $todayEnd, SQLITE3_INTEGER);
|
||||
// 消费:amount > 0 类型为消费类(shop_buy, shop_cart)
|
||||
$stmt2 = $db->prepare("SELECT player_name, SUM(amount) AS total_spend FROM web_transactions WHERE amount > 0 AND type IN ('shop_buy','shop_cart') AND created_at >= :s2 AND created_at < :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'];
|
||||
}
|
||||
|
||||
// ===== game_transactions(游戏内交易) =====
|
||||
// 收入(amount > 0)
|
||||
$stmt3 = $db->prepare("SELECT player_name, SUM(amount) AS total_income FROM game_transactions WHERE amount > 0 AND tx_time >= :start2 AND tx_time < :end2 GROUP BY player_name");
|
||||
$stmt3->bindValue(':start2', $todayStart, SQLITE3_INTEGER);
|
||||
$stmt3->bindValue(':end2', $todayEnd, SQLITE3_INTEGER);
|
||||
// 收入:amount > 0 且非消费类
|
||||
$stmt3 = $db->prepare("SELECT player_name, SUM(amount) AS total_income FROM web_transactions WHERE amount > 0 AND type NOT IN ('shop_buy','shop_cart') AND created_at >= :s3 AND created_at < :e3 GROUP BY player_name");
|
||||
$stmt3->bindValue(':s3', $todayStart, SQLITE3_INTEGER);
|
||||
$stmt3->bindValue(':e3', $todayEnd, SQLITE3_INTEGER);
|
||||
$res3 = $stmt3->execute();
|
||||
while ($row = $res3->fetchArray(SQLITE3_ASSOC)) {
|
||||
$incomeData[$row['player_name']] = ($incomeData[$row['player_name']] ?? 0) + (int)$row['total_income'];
|
||||
}
|
||||
// 消费(amount < 0 取绝对值)
|
||||
$stmt4 = $db->prepare("SELECT player_name, SUM(ABS(amount)) AS total_spend FROM game_transactions WHERE amount < 0 AND tx_time >= :start3 AND tx_time < :end3 GROUP BY player_name");
|
||||
$stmt4->bindValue(':start3', $todayStart, SQLITE3_INTEGER);
|
||||
$stmt4->bindValue(':end3', $todayEnd, SQLITE3_INTEGER);
|
||||
|
||||
// ==========================================
|
||||
// game_transactions(游戏内交易)
|
||||
// 金额始终为正,用 balance_after-before 判断方向
|
||||
// ==========================================
|
||||
// 收入:余额增加
|
||||
$stmt4 = $db->prepare("SELECT player_name, SUM(balance_after - balance_before) AS total_income FROM game_transactions WHERE balance_after > balance_before AND tx_time >= :s4 AND tx_time < :e4 GROUP BY player_name");
|
||||
$stmt4->bindValue(':s4', $todayStart, SQLITE3_INTEGER);
|
||||
$stmt4->bindValue(':e4', $todayEnd, SQLITE3_INTEGER);
|
||||
$res4 = $stmt4->execute();
|
||||
while ($row = $res4->fetchArray(SQLITE3_ASSOC)) {
|
||||
$incomeData[$row['player_name']] = ($incomeData[$row['player_name']] ?? 0) + (int)$row['total_income'];
|
||||
}
|
||||
// 消费:余额减少
|
||||
$stmt5 = $db->prepare("SELECT player_name, SUM(balance_before - balance_after) AS total_spend FROM game_transactions WHERE balance_after < balance_before AND tx_time >= :s5 AND tx_time < :e5 GROUP BY player_name");
|
||||
$stmt5->bindValue(':s5', $todayStart, SQLITE3_INTEGER);
|
||||
$stmt5->bindValue(':e5', $todayEnd, SQLITE3_INTEGER);
|
||||
$res5 = $stmt5->execute();
|
||||
while ($row = $res5->fetchArray(SQLITE3_ASSOC)) {
|
||||
$spendData[$row['player_name']] = ($spendData[$row['player_name']] ?? 0) + (int)$row['total_spend'];
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
|
||||
Reference in New Issue
Block a user