85 lines
2.8 KiB
PHP
85 lines
2.8 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use App\Business\JstOrderLabelDailySynchronizer;
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Carbon;
|
||
use Throwable;
|
||
|
||
class SyncJstOrderLabelsDaily extends Command
|
||
{
|
||
protected $signature = 'jst-order-labels:sync-daily
|
||
{--from=2026-10-01 : 覆盖核查起始日期 YYYY-MM-DD}
|
||
{--to= : 截止日期 YYYY-MM-DD,默认昨日}
|
||
{--company=1 : 公司 ID}
|
||
{--refresh-days=7 : 每日替换刷新最近天数}
|
||
{--dry-run : 只检查并输出计划,不写库}
|
||
{--json : 输出 JSON}';
|
||
|
||
protected $description = '先核对聚水潭订单标签历史覆盖,补齐缺口后刷新最近数据';
|
||
|
||
public function handle(JstOrderLabelDailySynchronizer $synchronizer): int
|
||
{
|
||
$from = trim((string) $this->option('from'));
|
||
$to = trim((string) $this->option('to'));
|
||
$to = $to !== '' ? $to : Carbon::now('Asia/Shanghai')->subDay()->toDateString();
|
||
|
||
try {
|
||
$result = $synchronizer->sync(
|
||
$from,
|
||
$to,
|
||
(int) $this->option('company'),
|
||
(int) $this->option('refresh-days'),
|
||
(bool) $this->option('dry-run'),
|
||
);
|
||
} catch (Throwable $exception) {
|
||
if ($this->option('json')) {
|
||
$this->line(json_encode([
|
||
'status' => 'failed',
|
||
'from' => $from,
|
||
'to' => $to,
|
||
'error' => $exception->getMessage(),
|
||
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||
} else {
|
||
$this->error($exception->getMessage());
|
||
}
|
||
|
||
return self::FAILURE;
|
||
}
|
||
|
||
if ($this->option('json')) {
|
||
$this->line(json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
$this->info(sprintf(
|
||
'聚水潭订单标签明细同步完成:%s ~ %s,覆盖 %d 天。',
|
||
$result['from'],
|
||
$result['to'],
|
||
(int) ($result['checked_days'] ?? 0),
|
||
));
|
||
foreach ($result['backfill_results'] ?? [] as $backfill) {
|
||
$this->line(sprintf(
|
||
' 补导 %s ~ %s:%d 单,%d 行。',
|
||
$backfill['from'],
|
||
$backfill['to'],
|
||
(int) $backfill['orders'],
|
||
(int) $backfill['rows'],
|
||
));
|
||
}
|
||
$refresh = $result['refresh'] ?? null;
|
||
if (is_array($refresh)) {
|
||
$this->line(sprintf(
|
||
' 刷新 %s ~ %s:%d 单,%d 行。',
|
||
$refresh['from'],
|
||
$refresh['to'],
|
||
(int) $refresh['orders'],
|
||
(int) $refresh['rows'],
|
||
));
|
||
}
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
}
|