OrderbookModel.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. w.company_id,
  106. t.token_commodities_value,
  107. t.token_commodities_amount,
  108. c."cpr_deliveryPlace_city_name" AS cityName,
  109. c.cpr_product_quantity AS cprProductQuantity,
  110. c.cpr_measure_unit_name AS measureUnitName,
  111. c.cpr_packaging_way_name AS packagingName,
  112. c.cpr_maturity_date AS maturityDate
  113. FROM "orderbook" o
  114. LEFT JOIN "token" t ON t.token_id = o.token_id
  115. LEFT JOIN "cpr" c ON c.cpr_id = t.cpr_id
  116. LEFT JOIN "wallet" w ON w.wallet_id = o.wallet_id
  117. WHERE UPPER(o.orderbook_state) = UPPER(:state)
  118. AND LOWER(o.orderbook_commodity_type) = LOWER(:commodity_type)
  119. AND o.status_id = :status_id
  120. ORDER BY o.orderbook_ts DESC'
  121. );
  122. $stmt->execute([
  123. 'state' => $state,
  124. 'commodity_type' => $commodityType,
  125. 'status_id' => $statusId,
  126. ]);
  127. return $stmt->fetchAll(\PDO::FETCH_ASSOC) ?: [];
  128. }
  129. public function findByIdWithToken(int $orderbookId): ?array
  130. {
  131. $stmt = $this->pdo->prepare(
  132. 'SELECT
  133. o.*,
  134. t.token_commodities_value,
  135. t.token_commodities_amount,
  136. t.token_external_id AS token_table_external_id,
  137. w.company_id,
  138. w.wallet_address
  139. FROM "orderbook" o
  140. LEFT JOIN "token" t ON t.token_id = o.token_id
  141. LEFT JOIN "wallet" w ON w.wallet_id = o.wallet_id
  142. WHERE o.orderbook_id = :orderbook_id
  143. LIMIT 1'
  144. );
  145. $stmt->execute(['orderbook_id' => $orderbookId]);
  146. $record = $stmt->fetch(\PDO::FETCH_ASSOC);
  147. return $record ?: null;
  148. }
  149. public function findByTokenExternalId(string $tokenExternalId): ?array
  150. {
  151. $stmt = $this->pdo->prepare(
  152. 'SELECT
  153. o.*,
  154. t.token_commodities_value,
  155. t.token_commodities_amount,
  156. w.company_id,
  157. w.wallet_address
  158. FROM "orderbook" o
  159. LEFT JOIN "token" t ON t.token_id = o.token_id
  160. LEFT JOIN "wallet" w ON w.wallet_id = o.wallet_id
  161. WHERE o.token_external_id = :token_external_id
  162. ORDER BY o.orderbook_id DESC
  163. LIMIT 1'
  164. );
  165. $stmt->execute(['token_external_id' => $tokenExternalId]);
  166. $record = $stmt->fetch(\PDO::FETCH_ASSOC);
  167. return $record ?: null;
  168. }
  169. public function updateStatus(int $orderbookId, int $statusId): void
  170. {
  171. $stmt = $this->pdo->prepare(
  172. 'UPDATE "orderbook"
  173. SET status_id = :status_id
  174. WHERE orderbook_id = :orderbook_id'
  175. );
  176. $stmt->execute([
  177. 'status_id' => $statusId,
  178. 'orderbook_id' => $orderbookId,
  179. ]);
  180. }
  181. public function updateWalletId(int $orderbookId, int $walletId): void
  182. {
  183. if ($orderbookId <= 0) {
  184. throw new \InvalidArgumentException('Invalid orderbook id provided');
  185. }
  186. if ($walletId <= 0) {
  187. throw new \InvalidArgumentException('Invalid wallet id provided');
  188. }
  189. $stmt = $this->pdo->prepare(
  190. 'UPDATE "orderbook"
  191. SET wallet_id = :wallet_id
  192. WHERE orderbook_id = :orderbook_id'
  193. );
  194. $stmt->execute([
  195. 'wallet_id' => $walletId,
  196. 'orderbook_id' => $orderbookId,
  197. ]);
  198. if ($stmt->rowCount() === 0) {
  199. throw new \RuntimeException('Orderbook record not found for wallet update');
  200. }
  201. }
  202. }