feat: 重构代码架构 + 新增报表/跟单/输赢统计功能
- 拆分 HomeController → TransactionController, ReportWebController, FollowPlanController - 新增 Service 层: TransactionService, ReportService, FollowPlanService - pk10.php JS 抽离为 4 个独立文件 (sound/race/bet/poll) - 前台新增报表查询页面 (/report) + 跟单计划页面 (/follow-plan) - 后台新增跟单计划管理 + 用户输赢明细统计 - 封盘状态显示倒计时 (x:xx) - 音效仅在开奖弹窗打开时播放 - 路由按模块分组整理 - autoload 支持 App\Services 命名空间
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
namespace App\Services;
|
||||
|
||||
use Db\Database;
|
||||
|
||||
class TransactionService {
|
||||
private $db;
|
||||
|
||||
public function __construct(Database $db) {
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户间转账
|
||||
*
|
||||
* @param int $fromUserId 转出用户ID
|
||||
* @param string $toUsername 收款用户名
|
||||
* @param float $amount 转账金额
|
||||
* @return array ['success' => bool, 'message' => string]
|
||||
*/
|
||||
public function transfer(int $fromUserId, string $toUsername, float $amount): array
|
||||
{
|
||||
$toUsername = trim($toUsername);
|
||||
|
||||
if ($toUsername === '') {
|
||||
return ['success' => false, 'message' => '请输入对方用户名'];
|
||||
}
|
||||
if ($amount <= 0) {
|
||||
return ['success' => false, 'message' => '金额无效'];
|
||||
}
|
||||
|
||||
$fromUser = $this->db->get('users', '*', ['id' => $fromUserId]);
|
||||
if (!$fromUser) {
|
||||
return ['success' => false, 'message' => '用户不存在'];
|
||||
}
|
||||
if ($fromUser['balance'] < $amount) {
|
||||
return ['success' => false, 'message' => '余额不足'];
|
||||
}
|
||||
|
||||
// 查找收款用户
|
||||
$toUser = $this->db->get('users', '*', ['username' => $toUsername]);
|
||||
if (!$toUser) {
|
||||
return ['success' => false, 'message' => '收款用户不存在'];
|
||||
}
|
||||
if ($toUser['id'] === $fromUser['id']) {
|
||||
return ['success' => false, 'message' => '不能转给自己'];
|
||||
}
|
||||
|
||||
$this->db->medoo->pdo->beginTransaction();
|
||||
try {
|
||||
$now = date('Y-m-d H:i:s');
|
||||
$fromBalanceBefore = floatval($fromUser['balance']);
|
||||
$toBalanceBefore = floatval($toUser['balance']);
|
||||
$fromBalanceAfter = $fromBalanceBefore - $amount;
|
||||
$toBalanceAfter = $toBalanceBefore + $amount;
|
||||
|
||||
// 扣款
|
||||
$this->db->update('users', ['balance' => $fromBalanceAfter, 'updated_at' => $now], ['id' => $fromUser['id']]);
|
||||
// 加款
|
||||
$this->db->update('users', ['balance' => $toBalanceAfter, 'updated_at' => $now], ['id' => $toUser['id']]);
|
||||
|
||||
// 转出流水
|
||||
$this->db->insert('transactions', [
|
||||
'user_id' => $fromUser['id'],
|
||||
'type' => 'transfer_out',
|
||||
'amount' => -$amount,
|
||||
'balance_before' => $fromBalanceBefore,
|
||||
'balance_after' => $fromBalanceAfter,
|
||||
'description' => '转账给 ' . $toUser['username'],
|
||||
'created_at' => $now,
|
||||
]);
|
||||
// 转入流水
|
||||
$this->db->insert('transactions', [
|
||||
'user_id' => $toUser['id'],
|
||||
'type' => 'transfer_in',
|
||||
'amount' => $amount,
|
||||
'balance_before' => $toBalanceBefore,
|
||||
'balance_after' => $toBalanceAfter,
|
||||
'description' => '收到 ' . $fromUser['username'] . ' 的转账',
|
||||
'created_at' => $now,
|
||||
]);
|
||||
|
||||
$this->db->medoo->pdo->commit();
|
||||
return ['success' => true, 'message' => '转账成功'];
|
||||
} catch (\Exception $e) {
|
||||
$this->db->medoo->pdo->rollBack();
|
||||
return ['success' => false, 'message' => '系统错误'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 充提申请
|
||||
*
|
||||
* @param int $userId 用户ID
|
||||
* @param string $type 类型: deposit|withdraw
|
||||
* @param float $amount 金额
|
||||
* @param string $remark 备注
|
||||
* @return array ['success' => bool, 'message' => string]
|
||||
*/
|
||||
public function fundRequest(int $userId, string $type, float $amount, string $remark = ''): array
|
||||
{
|
||||
if (!in_array($type, ['deposit', 'withdraw'])) {
|
||||
return ['success' => false, 'message' => '无效类型'];
|
||||
}
|
||||
if ($amount <= 0) {
|
||||
return ['success' => false, 'message' => '金额无效'];
|
||||
}
|
||||
|
||||
$user = $this->db->get('users', '*', ['id' => $userId]);
|
||||
if (!$user) {
|
||||
return ['success' => false, 'message' => '用户不存在'];
|
||||
}
|
||||
|
||||
if ($type === 'withdraw') {
|
||||
if ($user['balance'] < $amount) {
|
||||
return ['success' => false, 'message' => '余额不足'];
|
||||
}
|
||||
$this->db->medoo->pdo->beginTransaction();
|
||||
try {
|
||||
$this->db->update('users', ['balance[-]' => $amount], ['id' => $user['id']]);
|
||||
$this->db->insert('fund_requests', [
|
||||
'user_id' => $user['id'],
|
||||
'type' => 'withdraw',
|
||||
'amount' => $amount,
|
||||
'status' => 'pending',
|
||||
'remark' => $remark,
|
||||
]);
|
||||
$this->db->medoo->pdo->commit();
|
||||
return ['success' => true, 'message' => '提现申请已提交'];
|
||||
} catch (\Exception $e) {
|
||||
$this->db->medoo->pdo->rollBack();
|
||||
return ['success' => false, 'message' => '系统错误'];
|
||||
}
|
||||
}
|
||||
|
||||
// deposit - 记录申请
|
||||
$this->db->insert('fund_requests', [
|
||||
'user_id' => $user['id'],
|
||||
'type' => 'deposit',
|
||||
'amount' => $amount,
|
||||
'status' => 'pending',
|
||||
'remark' => $remark,
|
||||
]);
|
||||
return ['success' => true, 'message' => '充值申请已提交'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user