Lae/app/Console/Commands/UserAddBalance.php

73 lines
1.5 KiB
PHP
Raw Normal View History

2022-09-19 13:03:55 +00:00
<?php
namespace App\Console\Commands;
use App\Models\Transaction;
2022-11-06 11:28:22 +00:00
use App\Models\User;
2022-09-19 13:03:55 +00:00
use Illuminate\Console\Command;
class UserAddBalance extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
2022-11-18 10:29:17 +00:00
protected $signature = 'user:add-balance {user_id} {amount}';
2022-09-19 13:03:55 +00:00
/**
* 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.
*
2022-12-11 11:47:30 +00:00
* @return int
2022-09-19 13:03:55 +00:00
*/
2023-01-10 13:42:27 +00:00
public function handle(): int
2022-09-19 13:03:55 +00:00
{
//
$user_id = $this->argument('user_id');
$amount = $this->argument('amount');
// find user
2023-01-10 13:42:27 +00:00
$user = (new User)->findOrFail($user_id);
2022-09-19 13:03:55 +00:00
2023-02-07 09:04:11 +00:00
$this->info($user->name.', 当前余额: '.$user->balance.' 元');
2022-09-19 13:03:55 +00:00
2023-02-07 09:04:11 +00:00
$this->info('充值后余额: '.($user->balance + $amount).' 元');
if (! $this->confirm('确认充值 '.$amount.' 元?')) {
2022-09-19 13:03:55 +00:00
$this->info('已取消。');
2023-01-30 16:14:07 +00:00
2022-11-18 08:15:30 +00:00
return 0;
2022-09-19 13:03:55 +00:00
}
$transaction = new Transaction();
2023-02-07 09:04:11 +00:00
$description = '控制台充值 '.$amount.' 元';
2022-09-19 13:03:55 +00:00
2023-01-10 13:42:27 +00:00
$transaction->addAmount($user->id, 'console', $amount, $description, true);
2022-09-19 13:03:55 +00:00
2023-01-10 13:42:27 +00:00
$this->info('充值成功。');
2022-09-19 13:03:55 +00:00
2023-01-10 13:42:27 +00:00
$user->refresh();
2023-02-07 09:04:11 +00:00
$this->info($user->name.', 当前余额: '.$user->balance);
2022-11-18 08:15:30 +00:00
2022-11-18 08:30:16 +00:00
return 0;
2022-09-19 13:03:55 +00:00
}
}