CaiWuXiTong/app/Console/Commands/SyncJushuitanShopPromotionFees.php

109 lines
4.3 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 Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
use Modules\EcommerceFinance\Repositories\JushuitanShopPromotionFeeMirrorRepository;
use Modules\EcommerceFinance\Services\JushuitanShopPromotionFeeSynchronizer;
use Throwable;
class SyncJushuitanShopPromotionFees extends Command
{
protected $signature = 'business-mirror:sync-jushuitan-shop-promotion-fees
{--from= : 开始日期(含)}
{--to= : 结束日期(含)}
{--days=7 : 修改时间增量回看天数}
{--full : 按费用发生时间从历史起点完整校准}
{--fee-date : 显式按费用发生时间同步指定区间}
{--max-pages=0 : 每个日期窗口最多页数,0 使用配置上限}';
protected $description = '同步聚水潭官方店铺级推广费到财务共享镜像';
public function handle(
JushuitanShopPromotionFeeMirrorRepository $repository,
JushuitanShopPromotionFeeSynchronizer $synchronizer,
): int {
try {
$initial = ! $repository->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 => '聚水潭店铺级推广费同步失败,页面继续显示上一版数据。',
};
}
}