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.

125 line
4.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. FROM customer c
  45. INNER JOIN customer_type ct ON c.customer_type_id = ct.id
  46. ORDER BY c.id DESC'
  47. );
  48. }
  49. public function findWithType(int $id): ?array
  50. {
  51. return $this->database->first(
  52. 'SELECT c.id, c.customer_type_id, c.attribute_values,
  53. c.created_at, c.updated_at,
  54. ct.name AS customer_type_name,
  55. ct.attributes AS customer_type_attributes
  56. FROM customer c
  57. INNER JOIN customer_type ct ON c.customer_type_id = ct.id
  58. WHERE c.id = :id',
  59. ['id' => $id]
  60. );
  61. }
  62. /** Used after INSERT to recover the generated id for audit logging. */
  63. /** @return list<array<string, mixed>> */
  64. public function searchByType(int $typeId): array
  65. {
  66. return $this->database->query(
  67. 'SELECT c.id, c.attribute_values, ct.attributes AS type_attributes
  68. FROM customer c
  69. INNER JOIN customer_type ct ON c.customer_type_id = ct.id
  70. WHERE c.customer_type_id = :type_id
  71. ORDER BY c.id ASC',
  72. ['type_id' => $typeId]
  73. );
  74. }
  75. /** Used after INSERT to recover the generated id for audit logging. */
  76. public function findLatestByType(int $typeId): ?array
  77. {
  78. return $this->database->first(
  79. 'SELECT TOP (1) * FROM customer
  80. WHERE customer_type_id = :type_id
  81. ORDER BY id DESC',
  82. ['type_id' => $typeId]
  83. );
  84. }
  85. public function create(Customer $customer): bool
  86. {
  87. return $this->database->execute(
  88. 'INSERT INTO customer (customer_type_id, attribute_values)
  89. VALUES (:customer_type_id, :attribute_values)',
  90. [
  91. 'customer_type_id' => $customer->customerTypeId,
  92. 'attribute_values' => json_encode($customer->attributeValues, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
  93. ]
  94. );
  95. }
  96. public function update(Customer $customer): bool
  97. {
  98. return $this->database->execute(
  99. 'UPDATE customer
  100. SET customer_type_id = :customer_type_id,
  101. attribute_values = :attribute_values,
  102. updated_at = CURRENT_TIMESTAMP
  103. WHERE id = :id',
  104. [
  105. 'customer_type_id' => $customer->customerTypeId,
  106. 'attribute_values' => json_encode($customer->attributeValues, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE),
  107. 'id' => $customer->id,
  108. ]
  109. );
  110. }
  111. }

Powered by TurnKey Linux.