diff --git a/app/Console/Commands/UserAddBalance.php b/app/Console/Commands/UserAddBalance.php index be6827c..4e8acea 100644 --- a/app/Console/Commands/UserAddBalance.php +++ b/app/Console/Commands/UserAddBalance.php @@ -3,7 +3,6 @@ namespace App\Console\Commands; use App\Exceptions\ChargeException; -use App\Models\Balance; use App\Models\Transaction; use App\Models\User; use Illuminate\Console\Command; @@ -50,31 +49,20 @@ public function handle() // find user $user = User::findOrFail($user_id); - - $balance = new Balance(); - $this->info($user->name . ', 当前余额: ' . $user->balance . ' 元'); $this->info('充值后余额: ' . ($user->balance + $amount) . ' 元'); if (!$this->confirm('确认充值 ' . $amount . ' 元?')) { $this->info('已取消。'); - return; + return 0; } - $data = [ - 'user_id' => $user->id, - 'amount' => $amount, - 'payment' => 'console', - ]; - - $balance = $balance->create($data); - $transaction = new Transaction(); $description = '控制台充值 ' . $amount . ' 元'; try { - $transaction->addAmount($user->id, 'console', $amount, $description); + $transaction->addAmount($user->id, 'console', $amount, $description, true); $this->info('充值成功。'); @@ -84,5 +72,8 @@ public function handle() return $this->error($e->getMessage()); } + + return 1; + } } diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index ee6b413..17ff40b 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -113,7 +113,7 @@ private function addLog($user_id, $data) $current = [ - 'balance' => (float) $user->balance, + 'balance' => (float)$user->balance, 'drops' => $this->getDrops($user_id), 'user_id' => intval($user_id), ]; @@ -278,7 +278,7 @@ public function addHostPayoutBalance($user_id, $host_id, $module_id, $amount, $d /** * @throws ChargeException */ - public function addAmount($user_id, $payment = 'console', $amount = 0, $description = null) + public function addAmount($user_id, $payment = 'console', $amount = 0, $description = null, $add_to_balances = false) { $lock = Cache::lock("user_balance_lock_" . $user_id, 10); try { @@ -297,6 +297,16 @@ public function addAmount($user_id, $payment = 'console', $amount = 0, $descript $description = '充值 ' . $amount . ' 元'; } + if ($add_to_balances) { + $data = [ + 'user_id' => $user_id, + 'amount' => $amount, + 'payment' => $payment, + ]; + + Balance::create($data); + } + $this->addIncomeBalance($user_id, $payment, $amount, $description); } catch (LockTimeoutException $e) { Log::error($e);