|
|
|
|
|
<?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('公司信息-全部更新完成');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|