Lae/app/Console/Commands/ReduceBalance.php

65 lines
1.3 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Console\Commands;
2022-11-06 11:28:22 +00:00
use App\Models\User;
use Illuminate\Console\Command;
2023-02-19 16:06:52 +00:00
use Symfony\Component\Console\Command\Command as CommandAlias;
class ReduceBalance extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:reduce {user_id} {amount}';
/**
* The console command description.
*
* @var string
*/
2023-02-19 16:06:52 +00:00
protected $description = '减少用户的余额。';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*/
2023-02-19 16:06:52 +00:00
public function handle(): int
{
$user_id = $this->argument('user_id');
$amount = $this->argument('amount');
2023-01-10 13:42:27 +00:00
$user = (new User)->find($user_id);
2023-02-07 09:04:11 +00:00
$this->warn('扣除金额: '.$amount.' 元');
2023-02-07 09:04:11 +00:00
$this->warn('用户当前余额:'.$user->balance.' 元');
2023-02-07 09:04:11 +00:00
$this->warn('剩余余额:'.$user->balance - $amount.' 元');
$confirm = $this->confirm('确认扣除?');
if ($confirm) {
2023-02-19 16:06:52 +00:00
$user->reduce($amount, '控制台扣除。');
$this->info('扣除成功。');
} else {
2022-10-02 13:29:59 +00:00
$this->info('取消扣除。');
}
2023-02-19 16:06:52 +00:00
return CommandAlias::SUCCESS;
}
}