CaiWuXiTong/app/Business/Piaoju/PiaojuSellerQuotaRepository.php
linchong 69ffa43d43 feat: 发票额度读取票聚销方可用额度 (#1233)
发票额度改为读取票聚销方管理的最新可用额度,并每 15 分钟同步共享镜像。
2026-11-18 15:35:10 +08:00

58 lines
1.7 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\Business\Piaoju;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Throwable;
class PiaojuSellerQuotaRepository
{
/**
* @return Collection<string,object>
*/
public function byNormalizedName(): Collection
{
try {
$connectionName = (string) config('upstream_mirrors.connection', 'business_mirror');
if (! Schema::connection($connectionName)->hasTable(PiaojuSellerQuotaImporter::TABLE)) {
return collect();
}
$rows = DB::connection($connectionName)
->table(PiaojuSellerQuotaImporter::TABLE)
->select([
'seller_key',
'seller_name',
'tax_number',
'seller_status',
'auth_status',
'auth_status_desc',
'invoice_limit',
'invoice_limit_updated_at',
'source_fetched_at',
'synced_at',
])
->get();
return $rows
->groupBy(fn (object $row): string => self::normalizeName((string) $row->seller_name))
->filter(fn (Collection $group): bool => $group->count() === 1)
->map(static fn (Collection $group): object => $group->first());
} catch (Throwable $exception) {
report($exception);
return collect();
}
}
public static function normalizeName(string $name): string
{
$name = str_replace(['(', ')'], ['(', ')'], trim($name));
$name = preg_replace('/\s+/u', '', $name) ?? $name;
return mb_strtoupper($name);
}
}