CaiWuXiTong/app/Console/Commands/RefreshShippingTracking.php

28 lines
1.5 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 App\Support\ShippingTrackingRefresh;
use Illuminate\Console\Command;
class RefreshShippingTracking extends Command
{
protected $signature='jushuitan:refresh-tracking {--source-id=* : 定向校准包裹源键,最多500个} {--max-batches=4}';
protected $description='按上游更新时间校准现有发货单号并保留换单前信息,不重算运费';
public function handle(ShippingTrackingRefresh $refresh): int
{
if(app()->environment('production')&&!env('FINANCE_BATCH_CONTAINER')){$this->error('请通过 run-finance-batch.sh 执行。');return self::FAILURE;}
ini_set('memory_limit', '512M');
set_time_limit(1800);
$ids=$this->option('source-id');
if(count($ids)>500||collect($ids)->contains(fn($id)=>!ctype_digit((string)$id)||(int)$id<1)){$this->error('包裹源键无效。');return self::FAILURE;}
$started=microtime(true);$queries=0;$sqlMs=0;
\Illuminate\Support\Facades\DB::listen(function($q)use(&$queries,&$sqlMs){$queries++;$sqlMs+=$q->time;});
$r=$refresh->sync($ids,min(4,max(1,(int)$this->option('max-batches'))));
$this->line('运单校准:扫描 '.$r['scanned'].' 个源键,更新 '.$r['changed'].' 个包裹,补回 '.$r['names'].' 条旧快递名称,状态 '.$r['status']);
$this->line(sprintf('耗时 %.3f 秒,PHP 峰值 %.1f MiB,数据库查询 %d 次 / %.1f ms',microtime(true)-$started,memory_get_peak_usage(true)/1048576,$queries,$sqlMs));
return self::SUCCESS;
}
}