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.

148 lines
5.1KB

  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. /** Returns an existing customer with the same type and attribute values, excluding $excludeId (use for update). */
  81. public function findDuplicate(int $typeId, string $attributeValuesJson, int $excludeId = 0): ?array
  82. {
  83. $sql = 'SELECT TOP (1) c.id, ct.name AS customer_type_name
  84. FROM customer c
  85. INNER JOIN customer_type ct ON c.customer_type_id = ct.id
  86. WHERE c.customer_type_id = :type_id
  87. AND c.attribute_values = :attribute_values';
  88. $params = ['type_id' => $typeId, 'attribute_values' => $attributeValuesJson];
  89. if ($excludeId > 0) {
  90. $sql .= ' AND c.id != :exclude_id';
  91. $params['exclude_id'] = $excludeId;
  92. }
  93. return $this->database->first($sql, $params);
  94. }
  95. /** Used after INSERT to recover the generated id for audit logging. */
  96. public function findLatestByType(int $typeId): ?array
  97. {
  98. return $this->database->first(
  99. 'SELECT TOP (1) * FROM customer
  100. WHERE customer_type_id = :type_id
  101. ORDER BY id DESC',
  102. ['type_id' => $typeId]
  103. );
  104. }
  105. public function create(Customer $customer): bool
  106. {
  107. return $this->database->execute(
  108. 'INSERT INTO customer (customer_type_id, attribute_values)
  109. VALUES (:customer_type_id, :attribute_values)',
  110. [
  111. 'customer_type_id' => $customer->customerTypeId,
  112. 'attribute_values' => json_encode($customer->attributeValues, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
  113. ]
  114. );
  115. }
  116. public function update(Customer $customer): bool
  117. {
  118. return $this->database->execute(
  119. 'UPDATE customer
  120. SET customer_type_id = :customer_type_id,
  121. attribute_values = :attribute_values,
  122. updated_at = CURRENT_TIMESTAMP
  123. WHERE id = :id',
  124. [
  125. 'customer_type_id' => $customer->customerTypeId,
  126. 'attribute_values' => json_encode($customer->attributeValues, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
  127. 'id' => $customer->id,
  128. ]
  129. );
  130. }
  131. }

Powered by TurnKey Linux.