2022-10-02 13:26:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Models\Transaction;
|
2022-11-06 11:28:22 +00:00
|
|
|
use App\Models\User;
|
2022-10-02 13:26:59 +00:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
protected $description = '减少用户的余额(发生退款时使用)';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
|
|
|
|
|
|
|
|
$user = User::find($user_id);
|
|
|
|
|
2022-10-02 13:29:59 +00:00
|
|
|
$this->warn('扣除金额: ' . $user->balance . ' 元');
|
2022-10-02 13:26:59 +00:00
|
|
|
|
2022-10-02 13:29:59 +00:00
|
|
|
$this->warn('用户当前余额:' . $user->balance . ' 元');
|
2022-10-02 13:26:59 +00:00
|
|
|
|
2022-10-02 13:29:59 +00:00
|
|
|
$this->warn('剩余余额:' . $user->balance -$amount . ' 元');
|
2022-10-02 13:26:59 +00:00
|
|
|
|
|
|
|
$confirm = $this->confirm('确认扣除?');
|
|
|
|
|
|
|
|
$transaction = new Transaction();
|
|
|
|
if ($confirm) {
|
|
|
|
$transaction->reduceAmount($user_id, $amount, '控制台扣除。');
|
|
|
|
|
|
|
|
$this->info('扣除成功。');
|
|
|
|
} else {
|
2022-10-02 13:29:59 +00:00
|
|
|
$this->info('取消扣除。');
|
2022-10-02 13:26:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|