option('from'); $to = (string) $this->option('to'); $validDate = static fn (string $date): bool => preg_match('/^\d{4}-\d{2}-\d{2}$/', $date) && checkdate((int) substr($date, 5, 2), (int) substr($date, 8, 2), (int) substr($date, 0, 4)); if (! $validDate($from) || ! $validDate($to) || $from >= $to) { $this->error('--from / --to 需要 YYYY-MM-DD,且 from 早于 to'); return self::FAILURE; } $failed = 0; $cur = Carbon::parse($from); $end = Carbon::parse($to); while ($cur->lt($end)) { $next = $cur->copy()->addMonthNoOverflow()->startOfMonth(); if ($next->gt($end)) { $next = $end->copy(); } [$a, $b] = [$cur->format('Y-m-d H:i:s'), $next->format('Y-m-d H:i:s')]; $label = $cur->format('Y-m-d') . ' ~ ' . $next->format('Y-m-d'); $t0 = microtime(true); try { $al = $sync->alignWithOutbound($a, $b); $this->info(sprintf('%s 按销售出库单校正:发货时间 %d 个包裹,线上/实称重量 %d 个包裹(%d 秒)', $label, $al['ship_time'], $al['weight'], (int) (microtime(true) - $t0))); if (! $this->option('skip-freight')) { $t1 = microtime(true); $fr = $freight->recalculate($cur->format('Y-m-d'), $next->format('Y-m-d')); $this->info(sprintf('%s 重算运费:包裹 %d 个,核算成功 %d 个(%d 秒)', $label, $fr['packages'], $fr['ok'], (int) (microtime(true) - $t1))); } } catch (Throwable $e) { $failed++; $this->error($label . ' 失败:' . mb_substr($e->getMessage(), 0, 500)); } $cur = $next; } return $failed === 0 ? self::SUCCESS : self::FAILURE; } }