CaiWuXiTong/modules/EcommerceFinance/View/PlatformPromotionAccountPageData.php

126 lines
4.1 KiB
PHP

<?php
namespace Modules\EcommerceFinance\View;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Modules\EcommerceFinance\Repositories\PlatformPromotionAccountRepository;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Throwable;
class PlatformPromotionAccountPageData
{
private const TAB_PLATFORMS = [
'jd-promotion' => 'jd',
'douyin-promotion' => 'douyin',
'dewu-promotion' => 'dewu',
'vip-promotion' => 'vip',
];
public function __construct(private readonly PlatformPromotionAccountRepository $repository)
{
}
public function content(?string $tab = null): array
{
$platform = self::platformForTab($tab);
abort_if($platform === null, 404);
$requestedMonth = $this->month(request('month'));
$month = $requestedMonth ?: now()->format('Y-m');
$filters = [
'month' => $month,
'shop_name' => $this->text(request('shop_name')),
'account' => $this->text(request('account')),
];
if ($platform === 'vip') {
$filters['advertiser_id'] = $this->text(request('advertiser_id'));
}
try {
if ($requestedMonth === '') {
$month = $this->repository->latestMonth() ?: $month;
$filters['month'] = $month;
}
$data = $this->repository->dashboard($filters, 5000, $platform);
return [
'view' => 'banking.marketing_platform_promotion_bills.partials.embedded-table',
'filters' => $filters,
'data' => $data,
'platform' => $platform,
'marketingTab' => $tab,
'rows' => $this->paginateRows($data, $platform),
'error' => null,
];
} catch (ServiceUnavailableHttpException $exception) {
return [
'view' => 'banking.marketing_platform_promotion_bills.partials.embedded-table',
'filters' => $filters,
'data' => [],
'platform' => $platform,
'marketingTab' => $tab,
'rows' => $this->emptyPaginator(),
'error' => $exception->getMessage(),
];
} catch (Throwable $exception) {
report($exception);
return [
'view' => 'banking.marketing_platform_promotion_bills.partials.embedded-table',
'filters' => $filters,
'data' => [],
'platform' => $platform,
'marketingTab' => $tab,
'rows' => $this->emptyPaginator(),
'error' => '平台推广数据暂时无法读取,请稍后重试。',
];
}
}
public static function platformForTab(?string $tab): ?string
{
return self::TAB_PLATFORMS[$tab] ?? null;
}
private function paginateRows(array $data, string $tab): LengthAwarePaginator
{
$key = ['jd' => 'jd', 'douyin' => 'dy', 'dewu' => 'dw', 'vip' => 'vipMarketingDaily'][$tab];
$rows = $data[$key] ?? [];
$page = max((int) request('page', 1), 1);
$perPage = 50;
return new LengthAwarePaginator(
Collection::make($rows)->forPage($page, $perPage)->values(),
count($rows),
$perPage,
$page,
['path' => request()->url(), 'query' => request()->except('page')],
);
}
private function emptyPaginator(): LengthAwarePaginator
{
return new LengthAwarePaginator([], 0, 50, 1, [
'path' => request()->url(),
'query' => request()->query(),
]);
}
private function text(mixed $value): string
{
return mb_substr(trim((string) $value), 0, 100);
}
private function month(mixed $value): string
{
$value = trim((string) $value);
if (preg_match('/^(\d{4})-(\d{2})$/', $value, $matches) !== 1
|| (int) $matches[2] < 1
|| (int) $matches[2] > 12) {
return '';
}
return $value;
}
}