358 lines
11 KiB
PHP
Executable File
358 lines
11 KiB
PHP
Executable File
<?php
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Core\AdminBaseController;
|
|
use Db\Database;
|
|
|
|
class FinanceController extends AdminBaseController {
|
|
public function __construct() {
|
|
$this->checkLogin();
|
|
$this->checkAdmin();
|
|
}
|
|
|
|
/**
|
|
* 财务管理页面
|
|
*/
|
|
public function index() {
|
|
$db = new Database();
|
|
try {
|
|
// 获取资金流水列表
|
|
$transactions = $db->select('transactions', [
|
|
'[>]users' => ['user_id' => 'id']
|
|
], [
|
|
'transactions.id',
|
|
'transactions.user_id',
|
|
'users.username',
|
|
'transactions.type',
|
|
'transactions.amount',
|
|
'transactions.balance_before',
|
|
'transactions.balance_after',
|
|
'transactions.related_id',
|
|
'transactions.description',
|
|
'transactions.created_at'
|
|
], [
|
|
'ORDER' => ['transactions.id' => 'DESC'],
|
|
'LIMIT' => 100
|
|
]);
|
|
|
|
if (!is_array($transactions)) {
|
|
$transactions = [];
|
|
}
|
|
|
|
// 计算统计信息
|
|
$stats = $this->calculateStats($db);
|
|
|
|
} catch (\Throwable $e) {
|
|
$transactions = [];
|
|
$stats = [
|
|
'total_deposit' => 0,
|
|
'total_withdraw' => 0,
|
|
'total_bet' => 0,
|
|
'total_win' => 0,
|
|
'today_deposit' => 0,
|
|
'today_withdraw' => 0
|
|
];
|
|
}
|
|
|
|
$this->render('Admin/finance.php', [
|
|
'transactions' => $transactions,
|
|
'stats' => $stats,
|
|
'title' => '财务管理'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取资金流水详情
|
|
*/
|
|
public function get($id) {
|
|
header('Content-Type: application/json');
|
|
|
|
if (empty($id) || !is_numeric($id)) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '无效的流水ID'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$db = new Database();
|
|
try {
|
|
$transaction = $db->get('transactions', [
|
|
'[>]users' => ['user_id' => 'id']
|
|
], [
|
|
'transactions.id',
|
|
'transactions.user_id',
|
|
'users.username',
|
|
'transactions.type',
|
|
'transactions.amount',
|
|
'transactions.balance_before',
|
|
'transactions.balance_after',
|
|
'transactions.related_id',
|
|
'transactions.description',
|
|
'transactions.created_at'
|
|
], [
|
|
'transactions.id' => $id
|
|
]);
|
|
|
|
if ($transaction) {
|
|
echo json_encode([
|
|
'success' => true,
|
|
'data' => $transaction
|
|
]);
|
|
} else {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '流水记录不存在'
|
|
]);
|
|
}
|
|
} catch (\Throwable $e) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '获取数据失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 处理充值
|
|
*/
|
|
public function deposit() {
|
|
header('Content-Type: application/json');
|
|
|
|
$rawData = file_get_contents('php://input');
|
|
$data = json_decode($rawData, true);
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '数据格式错误'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$userId = isset($data['user_id']) ? (int)$data['user_id'] : 0;
|
|
$amount = isset($data['amount']) ? floatval($data['amount']) : 0;
|
|
$description = trim($data['description'] ?? '');
|
|
|
|
if ($userId <= 0) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '无效的用户ID'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
if ($amount <= 0) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '充值金额必须大于0'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$db = new Database();
|
|
|
|
try {
|
|
// 获取用户信息
|
|
$user = $db->get('users', ['id', 'balance', 'username'], ['id' => $userId]);
|
|
if (!$user) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '用户不存在'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$balanceBefore = floatval($user['balance']);
|
|
$balanceAfter = $balanceBefore + $amount;
|
|
|
|
// 更新用户余额
|
|
$db->update('users', [
|
|
'balance' => $balanceAfter,
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
], ['id' => $userId]);
|
|
|
|
// 记录资金流水
|
|
$transactionId = $db->insert('transactions', [
|
|
'user_id' => $userId,
|
|
'type' => 'deposit',
|
|
'amount' => $amount,
|
|
'balance_before' => $balanceBefore,
|
|
'balance_after' => $balanceAfter,
|
|
'description' => $description ?: '管理员充值',
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => '充值成功',
|
|
'data' => [
|
|
'transaction_id' => $transactionId,
|
|
'balance_before' => $balanceBefore,
|
|
'balance_after' => $balanceAfter
|
|
]
|
|
]);
|
|
} catch (\Exception $e) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '充值失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 处理提现
|
|
*/
|
|
public function withdraw() {
|
|
header('Content-Type: application/json');
|
|
|
|
$rawData = file_get_contents('php://input');
|
|
$data = json_decode($rawData, true);
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '数据格式错误'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$userId = isset($data['user_id']) ? (int)$data['user_id'] : 0;
|
|
$amount = isset($data['amount']) ? floatval($data['amount']) : 0;
|
|
$description = trim($data['description'] ?? '');
|
|
|
|
if ($userId <= 0) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '无效的用户ID'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
if ($amount <= 0) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '提现金额必须大于0'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$db = new Database();
|
|
|
|
try {
|
|
// 获取用户信息
|
|
$user = $db->get('users', ['id', 'balance', 'username'], ['id' => $userId]);
|
|
if (!$user) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '用户不存在'
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$balanceBefore = floatval($user['balance']);
|
|
|
|
// 检查余额是否充足
|
|
if ($balanceBefore < $amount) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '用户余额不足,当前余额:' . number_format($balanceBefore, 2)
|
|
]);
|
|
return;
|
|
}
|
|
|
|
$balanceAfter = $balanceBefore - $amount;
|
|
|
|
// 更新用户余额
|
|
$db->update('users', [
|
|
'balance' => $balanceAfter,
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
], ['id' => $userId]);
|
|
|
|
// 记录资金流水
|
|
$transactionId = $db->insert('transactions', [
|
|
'user_id' => $userId,
|
|
'type' => 'withdraw',
|
|
'amount' => -$amount, // 提现为负数
|
|
'balance_before' => $balanceBefore,
|
|
'balance_after' => $balanceAfter,
|
|
'description' => $description ?: '管理员提现',
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => '提现成功',
|
|
'data' => [
|
|
'transaction_id' => $transactionId,
|
|
'balance_before' => $balanceBefore,
|
|
'balance_after' => $balanceAfter
|
|
]
|
|
]);
|
|
} catch (\Exception $e) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'message' => '提现失败:' . $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 计算财务统计
|
|
*/
|
|
private function calculateStats($db) {
|
|
try {
|
|
// 总充值 (包含普通充值和人工加款)
|
|
$totalDeposit = $db->sum('transactions', 'amount', [
|
|
'type' => ['deposit', 'manual_deposit']
|
|
]) ?: 0;
|
|
|
|
// 总提现(取绝对值,包含普通提现和人工扣款)
|
|
$totalWithdraw = abs($db->sum('transactions', 'amount', [
|
|
'type' => ['withdraw', 'manual_withdraw']
|
|
]) ?: 0);
|
|
|
|
// 总投注(取绝对值)
|
|
$totalBet = abs($db->sum('transactions', 'amount', [
|
|
'type' => 'bet'
|
|
]) ?: 0);
|
|
|
|
// 总中奖
|
|
$totalWin = $db->sum('transactions', 'amount', [
|
|
'type' => 'win'
|
|
]) ?: 0;
|
|
|
|
// 今日充值 (包含普通充值和人工加款)
|
|
$todayDeposit = $db->sum('transactions', 'amount', [
|
|
'type' => ['deposit', 'manual_deposit'],
|
|
'created_at[>=]' => date('Y-m-d 00:00:00')
|
|
]) ?: 0;
|
|
|
|
// 今日提现(取绝对值,包含普通提现和人工扣款)
|
|
$todayWithdraw = abs($db->sum('transactions', 'amount', [
|
|
'type' => ['withdraw', 'manual_withdraw'],
|
|
'created_at[>=]' => date('Y-m-d 00:00:00')
|
|
]) ?: 0);
|
|
|
|
return [
|
|
'total_deposit' => floatval($totalDeposit),
|
|
'total_withdraw' => floatval($totalWithdraw),
|
|
'total_bet' => floatval($totalBet),
|
|
'total_win' => floatval($totalWin),
|
|
'today_deposit' => floatval($todayDeposit),
|
|
'today_withdraw' => floatval($todayWithdraw)
|
|
];
|
|
} catch (\Throwable $e) {
|
|
return [
|
|
'total_deposit' => 0,
|
|
'total_withdraw' => 0,
|
|
'total_bet' => 0,
|
|
'total_win' => 0,
|
|
'today_deposit' => 0,
|
|
'today_withdraw' => 0
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|