42 lines
2.4 KiB
PHP
42 lines
2.4 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Modules\EcommerceFinance\Services\CustomerServiceSurchargeSynchronizer;
|
||
use Throwable;
|
||
|
||
class PullCustomerServiceSurcharges extends Command
|
||
{
|
||
protected $signature = 'courier-surcharge:pull-customer-service {--courier= : 报价中的完整快递名称} {--company=1} {--from= : 工单创建日期起(含),默认报价生效日} {--to= : 工单创建日期止(含),默认今天}';
|
||
protected $description = '同步聚工单客服逐行审核通过的附加费,完整校验后替换指定快递的派生结算明细';
|
||
|
||
public function handle(CustomerServiceSurchargeSynchronizer $sync): int
|
||
{
|
||
$courier = trim((string) $this->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(); }
|
||
}
|
||
}
|