option('courier')); if ($courier === '' || (int) $this->option('company') < 1) { $this->error('请指定快递完整名称及有效公司。'); return self::FAILURE; } foreach (['from','to'] as $option) { $date = $this->option($option); if ($date && (! preg_match('/^(\d{4})-(\d{2})-(\d{2})$/D', $date, $parts) || ! checkdate((int) $parts[2], (int) $parts[3], (int) $parts[1]))) { $this->error('日期需要 YYYY-MM-DD。'); return self::FAILURE; } } if (app()->environment('production') && ! env('FINANCE_BATCH_CONTAINER')) { $this->error('请使用服务器 run-finance-batch.sh 执行。'); return self::FAILURE; } $lock = Cache::lock(CustomerServiceSurchargeSynchronizer::dataset((int) $this->option('company'), $courier), 2100); if (! $lock->get()) { $this->error('该快递附加费同步正在执行。'); return self::FAILURE; } try { $result = $sync->sync((int) $this->option('company'), $courier, $this->option('from'), $this->option('to'), fn ($done, $total) => $this->line("已扫描 {$done} / {$total} 张查件工单")); $this->info(sprintf('%s 附加费同步完成:%s 至 %s,%d 条,金额 %s 元,耗时 %.2f 秒,PHP 峰值 %.2f MiB。', $courier, $result['from'], $result['to'], $result['rows'], $result['amount'], $result['seconds'], $result['peak_mb'])); return self::SUCCESS; } catch (Throwable $e) { report($e); $this->error('附加费同步失败,保留上次完整结果:' . $e->getMessage()); return self::FAILURE; } finally { $lock->release(); } } }