CaiWuXiTong/app/Support/CourierBills/WaybillMatch.php

72 lines
3.0 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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\Support\CourierBills;
/** Only formatting and the established numeric T prefix may be treated as equivalent. */
final class WaybillMatch
{
/** Indexed exact lookup first; formatting fallback only receives unresolved numbers. */
public static function find($query, string $column, array $numbers, array $columns): array
{
$result = [];
$numbers = array_values(array_unique(array_map('strval', $numbers)));
foreach (array_chunk($numbers, 300) as $chunk) {
foreach ((clone $query)->whereIn($column, $chunk)->get($columns) as $row) {
$actual = (string) $row->{$column};
if (in_array($actual, $chunk, true)) {
$result[$actual]['exact'] = true;
$result[$actual]['rows'][] = $row;
}
}
}
$pending = [];
foreach ($numbers as $number) if (! isset($result[$number]) && ($key = self::key($number)) !== null) $pending[$key][] = $number;
$expression = self::expression($query, $column);
foreach (array_chunk(array_keys($pending), 300) as $keys) {
$variants = $keys;
foreach ($keys as $key) {
if (ctype_digit((string) $key)) {
$variants[] = 'T' . $key;
$variants[] = $key . '.0';
}
}
$found = [];
foreach ((clone $query)->whereIn($query->getConnection()->raw($expression), array_map('strval', $variants))->get($columns) as $row) {
$key = self::key((string) $row->{$column});
if ($key !== null && isset($pending[$key])) $found[$key][] = $row;
}
foreach ($found as $key => $rows) {
if (count(array_unique(array_map(fn ($r) => Waybill::normalize(mb_convert_kana((string) $r->{$column}, 'as')), $rows))) !== 1) continue;
foreach ($pending[$key] as $number) $result[$number] = ['exact' => false, 'rows' => $rows];
}
}
return $result;
}
public static function expression($query, string $column): string
{
$expression = $query->getGrammar()->wrap($column);
foreach ([" ", "\t", "\r", "\n", "'", '@', '`', ''', ' '] as $remove) {
$literal = str_replace("'", "''", $remove);
$expression = "REPLACE({$expression}, '{$literal}', '')";
}
return 'UPPER(' . $expression . ')';
}
public static function key(string $number): ?string
{
$number = Waybill::normalize(mb_convert_kana($number, 'as'));
return $number === null ? null : (preg_match('/^T\d{10,30}$/', $number) ? substr($number, 1) : $number);
}
public static function uniqueFuzzy(array $records): array
{
$keys = [];
foreach ($records as $record) {
$number = Waybill::normalize(mb_convert_kana((string) $record['waybill'], 'as'));
if ($number !== null) $keys[$number] = true;
}
return count($keys) === 1 ? $records : [];
}
}