initialized(); $full = (bool) $this->option('full') || $initial; $timeType = ($full || (bool) $this->option('fee-date')) ? 'fee_time' : 'modified'; [$from, $to] = $this->dateRange($full); $maxPages = (int) $this->option('max-pages'); $result = $synchronizer->sync( $from, $to, $full ? 'full' : 'incremental', $timeType, $maxPages > 0 ? $maxPages : null, ); } catch (Throwable $exception) { Log::error('聚水潭店铺级推广费镜像同步失败。', [ 'exception' => $exception::class, 'code' => $exception->getMessage(), ]); $this->error($this->messageForException($exception)); return self::FAILURE; } if ($result['source_rows'] === 0) { $this->warn($full ? '聚水潭店铺级推广费暂无数据,首次全量尚未完成。' : '本次修改时间窗口内暂无店铺级推广费变更。'); } $this->info(sprintf( '聚水潭店铺级推广费同步完成:%s,%s 至 %s,%d 页,源记录 %d 条,镜像写入 %d 条。', $result['mode'], $result['from'], $result['to'], $result['pages'], $result['source_rows'], $result['stored_rows'], )); return self::SUCCESS; } /** @return array{0:Carbon,1:Carbon} */ private function dateRange(bool $full): array { $fromInput = trim((string) $this->option('from')); $toInput = trim((string) $this->option('to')); $to = $toInput === '' ? now('Asia/Shanghai')->startOfDay() : Carbon::createFromFormat('Y-m-d', $toInput, 'Asia/Shanghai')->startOfDay(); $from = $fromInput !== '' ? Carbon::createFromFormat('Y-m-d', $fromInput, 'Asia/Shanghai')->startOfDay() : ($full ? Carbon::parse( (string) config( 'services.jushuitan.shop_promotion_fee_history_start', '2026-01-01', ), 'Asia/Shanghai', )->startOfDay() : $to->copy()->subDays(max( (int) config('services.jushuitan.shop_promotion_fee_overlap_days', 7), 1, ) - 1)); if ($from->gt($to)) { throw new \InvalidArgumentException('开始日期不能晚于结束日期。'); } return [$from, $to]; } private function messageForException(Throwable $exception): string { return match ($exception->getMessage()) { 'jushuitan_open_api_credentials_missing' => '聚水潭新开放平台凭据未配置,请设置 JUSHUITAN_APP_KEY、JUSHUITAN_APP_SECRET、JUSHUITAN_ACCESS_TOKEN。', 'jushuitan_open_api_app_key_invalid' => '聚水潭开放平台拒绝当前 App Key,请更换为已开通收费接口权限的有效凭据。', default => '聚水潭店铺级推广费同步失败,页面继续显示上一版数据。', }; } }