CaiWuXiTong/app/Console/Commands/SyncSupplierManagementMirror.php

50 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Modules\EcommerceFinance\Repositories\SupplierManagementMirrorRepository;
use Modules\EcommerceFinance\Services\BusinessMirrorDatasetRegistry;
use Modules\EcommerceFinance\Services\BusinessMirrorSynchronizer;
use Throwable;
class SyncSupplierManagementMirror extends Command
{
protected $signature = 'business-mirror:sync-supplier-management';
protected $description = '完整校准供应商管理的档案、合同、联系人和打款资料共享镜像';
public function handle(BusinessMirrorDatasetRegistry $datasets, BusinessMirrorSynchronizer $synchronizer): int
{
if (! (bool) config('upstream_mirrors.refresh_enabled', false)) {
return self::SUCCESS;
}
$failed = false;
foreach ([SupplierManagementMirrorRepository::DATASET, ...array_values(SupplierManagementMirrorRepository::RELATED)] as $key) {
if (! $datasets->scheduled($key)) {
$this->warn($key . ':未启用后台同步,已跳过。');
continue;
}
try {
// 四个已接入的小数据集顺序校准,每批沿用现有大小,直到完整结束。
$result = $synchronizer->sync($key, true, null, 0);
if ($result['status'] !== 'succeeded') {
$failed = true;
$this->error($key . ':同步未完成,请检查同步状态。');
continue;
}
$this->info(sprintf('%s:同步完成,本轮 %d 批,累计处理 %d 行。', $key, $result['batches'], $result['rows']));
} catch (Throwable $exception) {
$failed = true;
Log::error('供应商管理每日镜像同步失败。', ['dataset' => $key, 'exception' => $exception::class]);
$this->error($key . ':同步失败,请检查同步状态。');
}
}
return $failed ? self::FAILURE : self::SUCCESS;
}
}