73 lines
1.9 KiB
PHP
73 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
use DateTimeImmutable;
|
|
use InvalidArgumentException;
|
|
use Throwable;
|
|
|
|
/**
|
|
* 原始账单导入成功后,把覆盖月份加入业绩明细重算队列。
|
|
*/
|
|
class BillPerformanceResyncDispatcher
|
|
{
|
|
public const PLATFORM_LABELS = [
|
|
'taobao-alipay' => '淘系支付宝',
|
|
'taobao-wechat' => '淘系微信',
|
|
'pinduoduo' => '拼多多',
|
|
'dewu' => '得物',
|
|
];
|
|
|
|
public function __construct(
|
|
private readonly AccountMappingResyncDispatcher $queue,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @return array{jobs:int,months:list<string>}
|
|
*/
|
|
public function dispatch(string $platform, string $periodStart, string $periodEnd): array
|
|
{
|
|
if (! isset(self::PLATFORM_LABELS[$platform])) {
|
|
throw new InvalidArgumentException("不支持自动重算业绩明细的平台:{$platform}");
|
|
}
|
|
|
|
$months = $this->monthsBetween($periodStart, $periodEnd);
|
|
if ($months === []) {
|
|
throw new InvalidArgumentException('账单账期格式无效,无法排队重算业绩明细。');
|
|
}
|
|
|
|
$result = $this->queue->dispatchPlatformMonths([
|
|
$platform => $months,
|
|
]);
|
|
|
|
return [
|
|
'jobs' => (int) $result['jobs'],
|
|
'months' => $months,
|
|
];
|
|
}
|
|
|
|
/** @return list<string> */
|
|
private function monthsBetween(string $periodStart, string $periodEnd): array
|
|
{
|
|
try {
|
|
$cursor = (new DateTimeImmutable($periodStart))->modify('first day of this month');
|
|
$end = (new DateTimeImmutable($periodEnd))->modify('first day of this month');
|
|
} catch (Throwable) {
|
|
return [];
|
|
}
|
|
|
|
if ($cursor > $end) {
|
|
return [];
|
|
}
|
|
|
|
$months = [];
|
|
while ($cursor <= $end) {
|
|
$months[] = $cursor->format('Y-m');
|
|
$cursor = $cursor->modify('+1 month');
|
|
}
|
|
|
|
return $months;
|
|
}
|
|
}
|