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.5 KiB

2 weeks ago
<?php
namespace App\Console\Commands;
use App\Models\Company;
use Illuminate\Console\Command;
class RefreshCompanyMarketStatus extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'company:refresh-market-status {--company_id=} {--chunk=500}';
/**
* The console command description.
*
* @var string
*/
protected $description = '全量重算公司是否上市字段 company_market';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$companyId = $this->option('company_id');
$chunk = (int) $this->option('chunk');
$chunk = $chunk > 0 ? $chunk : 500;
$query = Company::query()->orderBy('id');
if (!empty($companyId)) {
$query->where('id', $companyId);
}
$total = (clone $query)->count();
if ($total == 0) {
return $this->info('没有需要处理的公司');
}
$this->info("开始重算 company_market共 {$total} 家公司");
$bar = $this->output->createProgressBar($total);
$bar->start();
$updatedCount = 0;
$unchangedCount = 0;
$failCount = 0;
$query->chunkById($chunk, function ($companies) use (&$updatedCount, &$unchangedCount, &$failCount, $bar) {
foreach ($companies as $company) {
try {
$updated = Company::updateMarketStatus($company->id);
if ($updated) {
$updatedCount++;
$bar->setMessage($company->company_name . ' 已更新', 'status');
} else {
$unchangedCount++;
$bar->setMessage($company->company_name . ' 无变化', 'status');
}
} catch (\Throwable $e) {
$failCount++;
$bar->setMessage($company->company_name . ' 失败: ' . $e->getMessage(), 'status');
}
$bar->advance();
}
});
$bar->finish();
$this->newLine();
$this->info("处理完成:更新 {$updatedCount} 家,未变化 {$unchangedCount} 家,失败 {$failCount} 家");
return $this->info('company_market 全量重算完成');
}
}