OrderbookModel.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Models;
  3. class OrderbookModel
  4. {
  5. private \PDO $pdo;
  6. public const STATUS_OPEN = 0;
  7. public const STATUS_COMPLETED = 1;
  8. public function __construct()
  9. {
  10. if (isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof \PDO) {
  11. $this->pdo = $GLOBALS['pdo'];
  12. return;
  13. }
  14. throw new \RuntimeException('Global PDO connection not initialized');
  15. }
  16. /**
  17. * @param array{
  18. * orderbook_flag:string,
  19. * orderbook_ts:int,
  20. * orderbook_is_token:bool,
  21. * orderbook_amount:string,
  22. * orderbook_state:string,
  23. * orderbook_commodity_type:string,
  24. * token_external_id:string,
  25. * status_id:int,
  26. * user_id:int,
  27. * wallet_id:int,
  28. * token_id:int,
  29. * currency_id:?int,
  30. * chain_id:int
  31. * } $data
  32. */
  33. public function create(array $data): array
  34. {
  35. $stmt = $this->pdo->prepare(
  36. 'INSERT INTO "orderbook" (
  37. orderbook_flag,
  38. orderbook_ts,
  39. orderbook_is_token,
  40. orderbook_amount,
  41. orderbook_state,
  42. orderbook_commodity_type,
  43. token_external_id,
  44. status_id,
  45. user_id,
  46. wallet_id,
  47. token_id,
  48. currency_id,
  49. chain_id
  50. ) VALUES (
  51. :orderbook_flag,
  52. :orderbook_ts,
  53. :orderbook_is_token,
  54. :orderbook_amount,
  55. :orderbook_state,
  56. :orderbook_commodity_type,
  57. :token_external_id,
  58. :status_id,
  59. :user_id,
  60. :wallet_id,
  61. :token_id,
  62. :currency_id,
  63. :chain_id
  64. ) RETURNING orderbook_id'
  65. );
  66. $stmt->execute([
  67. 'orderbook_flag' => $data['orderbook_flag'],
  68. 'orderbook_ts' => $data['orderbook_ts'],
  69. 'orderbook_is_token' => $data['orderbook_is_token'],
  70. 'orderbook_amount' => $data['orderbook_amount'],
  71. 'orderbook_state' => $data['orderbook_state'],
  72. 'orderbook_commodity_type' => $data['orderbook_commodity_type'],
  73. 'token_external_id' => $data['token_external_id'],
  74. 'status_id' => $data['status_id'],
  75. 'user_id' => $data['user_id'],
  76. 'wallet_id' => $data['wallet_id'],
  77. 'token_id' => $data['token_id'],
  78. 'currency_id' => $data['currency_id'],
  79. 'chain_id' => $data['chain_id'],
  80. ]);
  81. $orderbookId = (int)$stmt->fetchColumn();
  82. return [
  83. 'orderbook_id' => $orderbookId,
  84. 'orderbook_ts' => $data['orderbook_ts'],
  85. ];
  86. }
  87. public function findByStateAndCommodity(string $state, string $commodityType, int $statusId = self::STATUS_OPEN): array
  88. {
  89. $stmt = $this->pdo->prepare(
  90. 'SELECT
  91. o.orderbook_id,
  92. o.orderbook_flag,
  93. o.orderbook_ts,
  94. o.orderbook_is_token,
  95. o.orderbook_amount,
  96. o.orderbook_state,
  97. o.orderbook_commodity_type,
  98. o.token_external_id,
  99. o.status_id,
  100. o.user_id,
  101. o.wallet_id,
  102. o.token_id,
  103. o.currency_id,
  104. o.chain_id,
  105. t.token_commodities_value,
  106. t.token_commodities_amount,
  107. c."cpr_deliveryPlace_city_name" AS cityName,
  108. c.cpr_product_quantity AS cprProductQuantity,
  109. c.cpr_measure_unit_name AS measureUnitName
  110. FROM "orderbook" o
  111. LEFT JOIN "token" t ON t.token_id = o.token_id
  112. LEFT JOIN "cpr" c ON c.cpr_id = t.cpr_id
  113. WHERE UPPER(o.orderbook_state) = UPPER(:state)
  114. AND LOWER(o.orderbook_commodity_type) = LOWER(:commodity_type)
  115. AND o.status_id = :status_id
  116. ORDER BY o.orderbook_ts DESC'
  117. );
  118. $stmt->execute([
  119. 'state' => $state,
  120. 'commodity_type' => $commodityType,
  121. 'status_id' => $statusId,
  122. ]);
  123. return $stmt->fetchAll(\PDO::FETCH_ASSOC) ?: [];
  124. }
  125. public function findByIdWithToken(int $orderbookId): ?array
  126. {
  127. $stmt = $this->pdo->prepare(
  128. 'SELECT
  129. o.*,
  130. t.token_commodities_value,
  131. t.token_commodities_amount,
  132. t.token_external_id AS token_table_external_id,
  133. w.company_id,
  134. w.wallet_address
  135. FROM "orderbook" o
  136. LEFT JOIN "token" t ON t.token_id = o.token_id
  137. LEFT JOIN "wallet" w ON w.wallet_id = o.wallet_id
  138. WHERE o.orderbook_id = :orderbook_id
  139. LIMIT 1'
  140. );
  141. $stmt->execute(['orderbook_id' => $orderbookId]);
  142. $record = $stmt->fetch(\PDO::FETCH_ASSOC);
  143. return $record ?: null;
  144. }
  145. public function findByTokenExternalId(string $tokenExternalId): ?array
  146. {
  147. $stmt = $this->pdo->prepare(
  148. 'SELECT
  149. o.*,
  150. t.token_commodities_value,
  151. t.token_commodities_amount,
  152. w.company_id,
  153. w.wallet_address
  154. FROM "orderbook" o
  155. LEFT JOIN "token" t ON t.token_id = o.token_id
  156. LEFT JOIN "wallet" w ON w.wallet_id = o.wallet_id
  157. WHERE o.token_external_id = :token_external_id
  158. ORDER BY o.orderbook_id DESC
  159. LIMIT 1'
  160. );
  161. $stmt->execute(['token_external_id' => $tokenExternalId]);
  162. $record = $stmt->fetch(\PDO::FETCH_ASSOC);
  163. return $record ?: null;
  164. }
  165. public function updateStatus(int $orderbookId, int $statusId): void
  166. {
  167. $stmt = $this->pdo->prepare(
  168. 'UPDATE "orderbook"
  169. SET status_id = :status_id
  170. WHERE orderbook_id = :orderbook_id'
  171. );
  172. $stmt->execute([
  173. 'status_id' => $statusId,
  174. 'orderbook_id' => $orderbookId,
  175. ]);
  176. }
  177. }