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.

90 lines
2.3 KiB

3 months ago
<?php
namespace App\Console\Commands;
use App\Models\Company;
use App\Models\User;
use Illuminate\Console\Command;
class SyncCompany extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync:company';
/**
* 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()
{
// 全量同步所有有公司名称的用户
$this->syncAllCompanies();
return $this->info('全量同步完成');
}
/**
* 全量同步公司信息
*/
public function syncAllCompanies()
{
// 获取所有有公司名称的用户全量同步不限制company_id
$users = User::whereNotNull('company_name')
->orderBy('id', 'desc')
->get();
$total = $users->count();
if ($total == 0) {
return $this->info('没有需要同步的用户');
}
$this->info("开始全量同步公司信息,共 {$total} 个用户");
$bar = $this->output->createProgressBar($total);
$bar->start();
$successCount = 0;
$failCount = 0;
foreach ($users as $user) {
// 调用模型方法同步公司信息(不包含经纬度和地址)
$result = Company::syncCompanyFromUser($user);
if ($result['success']) {
$successCount++;
$bar->setMessage($result['company']->company_name . ' 同步成功', 'status');
} else {
$failCount++;
$bar->setMessage($user->company_name . ' ' . $result['message'], 'status');
}
$bar->advance();
}
$bar->finish();
$this->newLine();
$this->info("同步完成:成功 {$successCount} 个,失败 {$failCount} 个");
return $this->info('公司信息-全量同步完成');
}
}