update
This commit is contained in:
@@ -987,4 +987,60 @@ final class OrdersRepository
|
||||
return $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{id:int, order_number:string, buyer_name:string, buyer_email:string, buyer_phone:string}>
|
||||
*/
|
||||
public function quickSearch(string $query, int $limit = 10): array
|
||||
{
|
||||
$query = trim($query);
|
||||
if ($query === '' || mb_strlen($query) < 2) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$limit = max(1, min($limit, 20));
|
||||
$searchVal = '%' . $query . '%';
|
||||
|
||||
$sql = 'SELECT o.id, o.source_order_id, o.external_order_id, '
|
||||
. 'a.name AS buyer_name, a.email AS buyer_email, a.phone AS buyer_phone '
|
||||
. 'FROM orders o '
|
||||
. 'LEFT JOIN order_addresses a ON a.order_id = o.id AND a.address_type = "customer" '
|
||||
. 'WHERE (o.source_order_id LIKE :s1 OR o.external_order_id LIKE :s2 '
|
||||
. 'OR a.name LIKE :s3 OR a.email LIKE :s4 OR a.phone LIKE :s5 '
|
||||
. 'OR EXISTS (SELECT 1 FROM order_items oi WHERE oi.order_id = o.id AND oi.original_name LIKE :s6)) '
|
||||
. 'ORDER BY o.ordered_at DESC LIMIT :lim';
|
||||
|
||||
try {
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->bindValue(':s1', $searchVal);
|
||||
$stmt->bindValue(':s2', $searchVal);
|
||||
$stmt->bindValue(':s3', $searchVal);
|
||||
$stmt->bindValue(':s4', $searchVal);
|
||||
$stmt->bindValue(':s5', $searchVal);
|
||||
$stmt->bindValue(':s6', $searchVal);
|
||||
$stmt->bindValue(':lim', $limit, PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
|
||||
$rows = $stmt->fetchAll();
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(static function (array $row): array {
|
||||
$orderNumber = ((string) ($row['source_order_id'] ?? '')) !== ''
|
||||
? (string) $row['source_order_id']
|
||||
: (string) ($row['external_order_id'] ?? '');
|
||||
|
||||
return [
|
||||
'id' => (int) ($row['id'] ?? 0),
|
||||
'order_number' => $orderNumber,
|
||||
'buyer_name' => (string) ($row['buyer_name'] ?? ''),
|
||||
'buyer_email' => (string) ($row['buyer_email'] ?? ''),
|
||||
'buyer_phone' => (string) ($row['buyer_phone'] ?? ''),
|
||||
];
|
||||
}, $rows);
|
||||
} catch (Throwable) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user