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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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