78 lines
4.0 KiB
PHP
78 lines
4.0 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use App\Support\UnshippedSourceSynchronizer;
|
||
use App\Support\UnshippedWaybills;
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Throwable;
|
||
|
||
final class SyncUnshippedWaybills extends Command
|
||
{
|
||
protected $signature = 'courier:sync-unshipped-waybills {--source= : 只刷新指定来源} {--skip-returned : 退回件已刷新时复用该完整结果} {--max-batches=0 : 仓内工单每轮最大批次,0表示至完成}';
|
||
protected $description = '从当前业务副本和发货换单记录刷新未发出明细';
|
||
|
||
public function handle(UnshippedWaybills $waybills, UnshippedSourceSynchronizer $sources): int
|
||
{
|
||
if (app()->environment('production') && !env('FINANCE_BATCH_CONTAINER')) {
|
||
$this->error('请通过 run-finance-batch.sh 执行。');
|
||
return self::FAILURE;
|
||
}
|
||
$selected = (string) $this->option('source');
|
||
if ($selected !== '' && !array_key_exists($selected, UnshippedWaybills::SYNC_SOURCES)) {
|
||
$this->error('未知来源');
|
||
return self::FAILURE;
|
||
}
|
||
$lock = Cache::lock('courier-unshipped-sync', 2100);
|
||
if (!$lock->get()) {
|
||
$this->warn('未发出明细正在同步,本次跳过。');
|
||
return self::SUCCESS;
|
||
}
|
||
$failed = false;
|
||
try {
|
||
$returnedOk = true;
|
||
if (!$this->option('skip-returned') && ($selected === '' || str_starts_with($selected, 'intercept_'))) {
|
||
$returnedOk = $this->call('business-mirror:sync-returned-parcels') === self::SUCCESS;
|
||
}
|
||
foreach (UnshippedWaybills::SYNC_SOURCES as $source => $label) {
|
||
if ($selected !== '' && $selected !== $source) continue;
|
||
try {
|
||
if ($source === 'warehouse') {
|
||
$result = $sources->warehouse(max(0, (int) $this->option('max-batches')),
|
||
fn ($n, $last, $high) => $this->line("仓内未发:已扫描 {$n} 行,游标 {$last}/{$high}"));
|
||
if ($result['status'] === 'running') {
|
||
$this->line('仓内未发:分批同步进行中,保留上次完整结果。');
|
||
continue;
|
||
}
|
||
$ok = true;
|
||
} elseif (str_starts_with($source, 'intercept_')) {
|
||
if (!$returnedOk) throw new \RuntimeException('退回件刷新失败,保留上次未发出结果。');
|
||
$sources->intercept($source);
|
||
$ok = true;
|
||
} else {
|
||
$ok = $waybills->sync($source);
|
||
}
|
||
} catch (Throwable $error) {
|
||
report($error);
|
||
$ok = false;
|
||
$safeMessages = ['退回件明细尚未完成刷新,保留上次结果。', '退回件来源覆盖不足,保留上次结果。', '退回件刷新失败,保留上次未发出结果。'];
|
||
$warning = in_array($error->getMessage(), $safeMessages, true) ? $error->getMessage() : '来源读取失败,保留上次完整结果。';
|
||
$waybills->connection()->table('courier_unshipped_sync')->updateOrInsert(['source' => $source], ['warning' => $warning]);
|
||
}
|
||
if (!$ok) {
|
||
$warning = $waybills->connection()->table('courier_unshipped_sync')->where('source', $source)->value('warning');
|
||
$this->warn($label.':'.$warning);
|
||
$failed = true;
|
||
} else {
|
||
$state = $waybills->connection()->table('courier_unshipped_sync')->where('source', $source)->first();
|
||
$this->info($label.':'.$state->row_count.' 行,缺单号 '.$state->missing_count.' 行。');
|
||
}
|
||
}
|
||
return $failed ? self::FAILURE : self::SUCCESS;
|
||
} finally {
|
||
$lock->release();
|
||
}
|
||
}
|
||
}
|