add 手动充值

This commit is contained in:
iVampireSP.com 2022-09-19 21:03:55 +08:00
parent b895b18173
commit 619d233ae2
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
2 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,99 @@
<?php
namespace App\Console\Commands;
use App\Models\User;
use App\Models\Transaction;
use App\Models\User\Balance;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class UserAddBalance extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:charge {user_id} {amount}';
/**
* The console command description.
*
* @var string
*/
protected $description = '为用户充值,用法: 用户ID, 金额。';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
$user_id = $this->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;
}
}
}

View File

@ -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,
];
/**