Lae/app/Models/Transaction.php

268 lines
6.6 KiB
PHP
Raw Normal View History

2022-09-14 14:19:28 +00:00
<?php
namespace App\Models;
2022-10-07 05:15:08 +00:00
use App\Exceptions\ChargeException;
2022-10-29 03:44:52 +00:00
use App\Exceptions\User\BalanceNotEnoughException;
2022-11-18 09:16:30 +00:00
use Carbon\Carbon;
2022-10-07 05:15:08 +00:00
use Illuminate\Contracts\Cache\LockTimeoutException;
2022-11-06 11:28:22 +00:00
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Jenssegers\Mongodb\Eloquent\Model;
2022-09-14 14:19:28 +00:00
class Transaction extends Model
{
const UPDATED_AT = null;
2022-09-14 14:19:28 +00:00
protected $connection = 'mongodb';
2022-09-15 04:13:37 +00:00
// 停用 updated_at
protected $collection = 'transactions';
2022-09-14 14:19:28 +00:00
protected $dates = [
'created_at',
'updated_at',
2022-09-15 04:13:37 +00:00
'paid_at',
2022-09-14 14:19:28 +00:00
];
protected $fillable = [
2022-09-15 04:13:37 +00:00
// 交易类型
'type',
// 交易渠道
'payment',
// 描述
'description',
// 入账
'income',
// 出账
'outcome',
// 可用余额
2022-11-16 04:48:39 +00:00
'balances',
2022-11-16 13:02:20 +00:00
'balance',
2022-09-15 04:13:37 +00:00
// 赠送金额
'gift',
'user_id',
2022-09-15 05:38:50 +00:00
'host_id',
'module_id',
2022-09-14 14:19:28 +00:00
];
2022-09-15 04:13:37 +00:00
2022-11-20 14:35:53 +00:00
public function scopeThisUser($query)
{
return $query->where('user_id', auth()->id());
}
2022-09-15 04:13:37 +00:00
private function addLog($user_id, $data)
2022-09-15 04:13:37 +00:00
{
$user = User::find($user_id);
$current = [
2022-11-18 08:15:30 +00:00
'balance' => (float)$user->balance,
'user_id' => intval($user_id),
2022-09-15 04:13:37 +00:00
];
// merge
$data = array_merge($data, $current);
2022-09-15 04:13:37 +00:00
// add expired at
$data['expired_at'] = now()->addSeconds(7);
2022-09-15 04:13:37 +00:00
return $this->create($data);
2022-09-15 04:13:37 +00:00
}
public function reduceAmount($user_id, $amount = 0, $description = '扣除费用请求。')
{
$lock = Cache::lock("user_balance_lock_" . $user_id, 10);
2022-10-07 05:15:08 +00:00
try {
$lock->block(5);
$user = User::findOrFail($user_id);
$user->balance -= $amount;
$user->save();
2022-10-07 05:15:08 +00:00
$this->addPayoutBalance($user_id, $amount, $description);
2022-10-29 03:44:52 +00:00
} finally {
optional($lock)->release();
}
2022-11-16 13:02:20 +00:00
return $user->balance;
2022-10-29 03:44:52 +00:00
}
public function addPayoutBalance($user_id, $amount, $description, $module_id = null)
{
$data = [
'type' => 'payout',
'payment' => 'balance',
'description' => $description,
'income' => 0,
'outcome' => (float)$amount,
];
if ($module_id) {
$data['module_id'] = $module_id;
}
return $this->addLog($user_id, $data);
}
2022-10-29 03:44:52 +00:00
public function reduceAmountModuleFail($user_id, $module_id, $amount = 0, $description = '扣除费用请求。')
{
$lock = Cache::lock("user_balance_lock_" . $user_id, 10);
2022-10-29 03:44:52 +00:00
try {
$lock->block(5);
$user = User::findOrFail($user_id);
$user->balance -= $amount;
2022-10-29 03:44:52 +00:00
// if balance < 0
if ($user->balance < 0) {
2022-10-29 03:44:52 +00:00
throw new BalanceNotEnoughException('余额不足。');
}
$user->save();
$this->addPayoutBalance($user_id, $amount, $description, $module_id);
2022-10-07 05:15:08 +00:00
} finally {
optional($lock)->release();
}
2022-11-16 13:02:20 +00:00
return $user->balance;
2022-10-07 05:15:08 +00:00
}
2022-10-09 07:01:21 +00:00
public function reduceHostAmount($user_id, $host_id, $module_id, $amount = 0, $description = '扣除费用请求。')
{
$lock = Cache::lock("user_balance_lock_" . $user_id, 10);
2022-10-09 07:01:21 +00:00
try {
$lock->block(5);
$user = User::findOrFail($user_id);
$user->balance -= $amount;
2022-10-09 07:01:21 +00:00
$user->save();
$this->addHostPayoutBalance($user_id, $host_id, $module_id, $amount, $description);
2022-10-09 07:01:21 +00:00
} finally {
optional($lock)->release();
}
2022-11-16 13:02:20 +00:00
return $user->balance;
2022-10-09 07:01:21 +00:00
}
public function addHostPayoutBalance($user_id, $host_id, $module_id, $amount, $description)
{
$data = [
'type' => 'payout',
2022-11-16 12:31:15 +00:00
'payment' => 'balance',
'description' => $description,
'income' => 0,
'outcome' => (float)$amount,
'host_id' => $host_id,
'module_id' => $module_id,
];
return $this->addLog($user_id, $data);
}
2022-11-16 04:52:51 +00:00
/**
* @throws ChargeException
*/
public function addAmount($user_id, $payment = 'console', $amount = 0, $description = null, $add_charge_log = false)
2022-10-07 05:15:08 +00:00
{
$lock = Cache::lock("user_balance_lock_" . $user_id, 10);
2022-10-07 05:15:08 +00:00
try {
$lock->block(5);
$user = User::findOrFail($user_id);
$left_balance = $user->balance + $amount;
2022-10-07 05:15:08 +00:00
$user->increment('balance', $amount);
2022-10-07 05:15:08 +00:00
if (!$description) {
$description = '充值 ' . $amount . ' 元';
}
if ($add_charge_log) {
2022-11-18 08:15:30 +00:00
$data = [
'user_id' => $user_id,
'amount' => $amount,
'payment' => $payment,
2022-11-18 09:16:30 +00:00
'paid_at' => Carbon::now(),
2022-11-18 08:15:30 +00:00
];
Balance::create($data);
}
$this->addIncomeBalance($user_id, $payment, $amount, $description);
2022-10-07 05:15:08 +00:00
} catch (LockTimeoutException $e) {
Log::error($e);
throw new ChargeException('充值失败,请稍后再试。');
} finally {
optional($lock)->release();
}
2022-11-16 13:02:20 +00:00
return $left_balance;
}
public function addIncomeBalance($user_id, $payment, $amount, $description)
2022-09-15 04:13:37 +00:00
{
$data = [
'type' => 'income',
'payment' => $payment,
'description' => $description,
'income' => (float)$amount,
'outcome' => 0,
2022-09-15 04:13:37 +00:00
];
return $this->addLog($user_id, $data);
2022-09-15 04:13:37 +00:00
}
2022-11-16 13:02:20 +00:00
public function transfer(User $user, User $to, float $amount, string|null $description): float
{
$lock = Cache::lock("user_balance_lock_" . $user->id, 10);
$lock_to = Cache::lock("user_balance_lock_" . $to->id, 10);
try {
$lock->block(5);
$lock_to->block(5);
$user->balance -= $amount;
$user->save();
$to->balance += $amount;
$to->save();
2022-11-16 13:02:20 +00:00
if (!$description) {
$description = '完成。';
}
$description_new = "转账给 {$to->name}({$to->email}) {$amount} 元,{$description}";
$this->addPayoutBalance($user->id, $amount, $description_new);
$description_new = "收到来自 {$user->name}($user->email) 转来的 {$amount} 元, $description";
$this->addIncomeBalance($to->id, 'transfer', $amount, $description_new);
} finally {
optional($lock)->release();
optional($lock_to)->release();
}
2022-11-16 13:02:20 +00:00
return $user->balance;
}
2022-09-14 14:19:28 +00:00
}