You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 line
4.3KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repositories;
  4. use App\Models\Customer;
  5. use Core\Repository;
  6. class CustomerRepository extends Repository
  7. {
  8. protected string $table = 'customer';
  9. protected string $primaryKey = 'id';
  10. public function count(): int
  11. {
  12. $row = $this->database->first('SELECT COUNT(*) AS total FROM customer');
  13. return (int) ($row['total'] ?? 0);
  14. }
  15. /** @return list<array<string, mixed>> */
  16. public function recentWithType(int $limit = 5): array
  17. {
  18. return $this->database->query(
  19. "SELECT TOP ({$limit}) c.id, c.created_at, ct.name AS customer_type_name
  20. FROM customer c
  21. INNER JOIN customer_type ct ON c.customer_type_id = ct.id
  22. ORDER BY c.id DESC"
  23. );
  24. }
  25. /** @return list<array<string, mixed>> */
  26. public function countByType(): array
  27. {
  28. return $this->database->query(
  29. 'SELECT ct.name AS customer_type_name, COUNT(c.id) AS customer_count
  30. FROM customer_type ct
  31. LEFT JOIN customer c ON c.customer_type_id = ct.id
  32. GROUP BY ct.id, ct.name
  33. ORDER BY customer_count DESC, ct.name ASC'
  34. );
  35. }
  36. /** @return list<array<string, mixed>> */
  37. public function allWithType(): array
  38. {
  39. return $this->database->query(
  40. 'SELECT c.id, c.customer_type_id, c.attribute_values,
  41. c.created_at, c.updated_at,
  42. ct.name AS customer_type_name,
  43. ct.attributes AS customer_type_attributes,
  44. ct.api_match_field
  45. FROM customer c
  46. INNER JOIN customer_type ct ON c.customer_type_id = ct.id
  47. ORDER BY c.id DESC'
  48. );
  49. }
  50. /** @return list<array<string, mixed>> */
  51. public function allByTypeWithType(int $typeId): array
  52. {
  53. return $this->database->query(
  54. 'SELECT c.id, c.customer_type_id, c.attribute_values,
  55. c.created_at, c.updated_at,
  56. ct.name AS customer_type_name,
  57. ct.attributes AS customer_type_attributes,
  58. ct.api_match_field
  59. FROM customer c
  60. INNER JOIN customer_type ct ON c.customer_type_id = ct.id
  61. WHERE c.customer_type_id = :type_id
  62. ORDER BY c.id DESC',
  63. ['type_id' => $typeId]
  64. );
  65. }
  66. public function findWithType(int $id): ?array
  67. {
  68. return $this->database->first(
  69. 'SELECT c.id, c.customer_type_id, c.attribute_values,
  70. c.created_at, c.updated_at,
  71. ct.name AS customer_type_name,
  72. ct.attributes AS customer_type_attributes,
  73. ct.api_match_field
  74. FROM customer c
  75. INNER JOIN customer_type ct ON c.customer_type_id = ct.id
  76. WHERE c.id = :id',
  77. ['id' => $id]
  78. );
  79. }
  80. /** Used after INSERT to recover the generated id for audit logging. */
  81. public function findLatestByType(int $typeId): ?array
  82. {
  83. return $this->database->first(
  84. 'SELECT TOP (1) * FROM customer
  85. WHERE customer_type_id = :type_id
  86. ORDER BY id DESC',
  87. ['type_id' => $typeId]
  88. );
  89. }
  90. public function create(Customer $customer): bool
  91. {
  92. return $this->database->execute(
  93. 'INSERT INTO customer (customer_type_id, attribute_values)
  94. VALUES (:customer_type_id, :attribute_values)',
  95. [
  96. 'customer_type_id' => $customer->customerTypeId,
  97. 'attribute_values' => json_encode($customer->attributeValues, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
  98. ]
  99. );
  100. }
  101. public function update(Customer $customer): bool
  102. {
  103. return $this->database->execute(
  104. 'UPDATE customer
  105. SET customer_type_id = :customer_type_id,
  106. attribute_values = :attribute_values,
  107. updated_at = CURRENT_TIMESTAMP
  108. WHERE id = :id',
  109. [
  110. 'customer_type_id' => $customer->customerTypeId,
  111. 'attribute_values' => json_encode($customer->attributeValues, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
  112. 'id' => $customer->id,
  113. ]
  114. );
  115. }
  116. }

Powered by TurnKey Linux.