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.

92 lines
2.3 KiB

9 months ago
<?php
namespace App\Console\Commands;
8 months ago
use App\Models\Company;
9 months ago
use App\Models\User;
use Illuminate\Console\Command;
class UpdateCompany extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
3 months ago
protected $signature = 'update_company {--user_id=}';
9 months ago
/**
* The console command description.
*
* @var string
*/
5 months ago
protected $description = '批量更新公司信息';
9 months ago
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
8 months ago
$user_id = $this->option('user_id');
8 months ago
// 更新公司信息
3 months ago
$this->compnay($user_id);
8 months ago
return $this->info('全部更新完成');
}
/**
* 更新公司信息
*/
3 months ago
public function compnay($user_id = null)
8 months ago
{
3 months ago
// 扫描待更新的用户company_id = -1待更新或 company_id = null兼容旧数据
3 months ago
$users = User::whereNotNull('company_name')
->where(function ($query) use ($user_id) {
if ($user_id) {
$query->where('id', $user_id);
3 months ago
}
3 months ago
})->where(function ($query) {
$query->where('company_id', -1)->orWhereNull('company_id');
})->orderBy('id', 'desc')
3 months ago
->get();
5 months ago
$total = $users->count();
if ($total == 0) {
return $this->info('没有需要更新的用户');
}
$this->info("开始更新公司信息,共 {$total} 个用户");
$bar = $this->output->createProgressBar($total);
$bar->start();
8 months ago
foreach ($users as $user) {
3 months ago
// 调用模型方法更新公司信息
$result = Company::updateCompanyFromUser($user);
9 months ago
3 months ago
if ($result['success']) {
$bar->setMessage($result['company']->company_name . ' 更新成功', 'status');
} else {
$bar->setMessage($user->company_name . ' ' . $result['message'], 'status');
8 months ago
}
4 months ago
$bar->advance();
}
$bar->finish();
$this->newLine();
3 months ago
return $this->info('公司信息-全部更新完成');
4 months ago
}
9 months ago
}