diff --git a/app/Console/Commands/UserAddBalance.php b/app/Console/Commands/UserAddBalance.php new file mode 100644 index 0000000..f72e239 --- /dev/null +++ b/app/Console/Commands/UserAddBalance.php @@ -0,0 +1,99 @@ +argument('user_id'); + $amount = $this->argument('amount'); + + + // find user + $user = User::findOrFail($user_id); + + + $balance = new Balance(); + + $this->info($user->name . ', 当前余额: ' . $user->balance); + + if (!$this->confirm('确认充值 ' . $amount . ' 元?')) { + $this->info('已取消。'); + return; + } + + $data = [ + 'user_id' => $user->id, + 'amount' => $amount, + 'payment' => 'console', + ]; + + $balance = $balance->create($data); + + $transaction = new Transaction(); + + DB::beginTransaction(); + try { + $balance->user->increment('balance', $amount); + + $description = '控制台充值 ' . $amount . ' 元'; + $transaction->addIncomeBalance($balance->user_id, 'console', $amount, $description); + + $balance->update([ + 'paid_at' => now(), + ]); + + DB::commit(); + + $this->info('充值成功。'); + + $user->refresh(); + $this->info($user->name . ', 当前余额: ' . $user->balance); + + } catch (\Exception $e) { + DB::rollBack(); + + $this->error('充值失败。' . $e->getMessage()); + + return; + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 737030b..1ac910a 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -6,6 +6,7 @@ use App\Console\Commands\CalcModule; use App\Console\Commands\SuspendUserAllHosts; use App\Console\Commands\UnbanUser; +use App\Console\Commands\UserAddBalance; use App\Jobs\CheckAndChargeBalance; use App\Jobs\HostCost; use App\Jobs\ClearTasks; @@ -27,6 +28,7 @@ class Kernel extends ConsoleKernel UnbanUser::class, SuspendUserAllHosts::class, CalcModule::class, + UserAddBalance::class, ]; /**