CaiWuXiTong/app/Console/Commands/SyncJstOrderLabelsDaily.php
qiuxiang 0c077ecdf2 fix: 每日补刷聚水潭订单标签历史变化 (#1656)
每日同步按源订单更新时间补刷标签内容变化。
2026-12-02 15:12:21 +08:00

85 lines
2.8 KiB
PHP
Raw Permalink 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\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;
}
}