You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.3 KiB
63 lines
1.3 KiB
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Console\Command;
|
|
use Overtrue\Pinyin\Pinyin;
|
|
|
|
|
|
class UpdateLetter extends Command
|
|
{
|
|
protected $tryTimes = 3;
|
|
protected $scheduleNumber = 10;
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'update_letter';
|
|
|
|
/**
|
|
* 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()
|
|
{
|
|
// 更新用户首字母
|
|
$users = User::whereNull('letter')
|
|
->where(function ($query) {
|
|
$query->whereNotNull('name')->orWhereNotNull('username');
|
|
})->limit(100)->get();
|
|
foreach ($users as $user) {
|
|
$name = $user->username;
|
|
if ($user->name) {
|
|
$name = $user->name;
|
|
}
|
|
$user->letter = strtoupper(Pinyin::abbr(mb_substr($name, 0, 1))[0]);
|
|
$user->save();
|
|
}
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
|
|
}
|