58 lines
1.7 KiB
PHP
58 lines
1.7 KiB
PHP
<?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);
|
||
}
|
||
}
|