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.

89 lines
2.2 KiB

<?php
namespace App\Console\Commands;
use App\Models\Company;
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()
{
// 获取所有公司(同步存量数据)
$companies = Company::whereNotNull('company_name')
->orderBy('id', 'desc')
->get();
$total = $companies->count();
if ($total == 0) {
return $this->info('没有需要同步的公司');
}
$this->info("开始全量同步公司信息,共 {$total} 家公司");
$bar = $this->output->createProgressBar($total);
$bar->start();
$successCount = 0;
$failCount = 0;
foreach ($companies as $company) {
// 调用模型方法同步公司信息(不包含经纬度和地址)
$result = Company::syncCompanyInfo($company);
if ($result['success']) {
$successCount++;
$bar->setMessage($result['company']->company_name . ' 同步成功', 'status');
} else {
$failCount++;
$bar->setMessage($company->company_name . ' ' . $result['message'], 'status');
}
$bar->advance();
}
$bar->finish();
$this->newLine();
$this->info("同步完成:成功 {$successCount} 个,失败 {$failCount}");
return $this->info('公司信息-全量同步完成');
}
}