TokenModel.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Models;
  3. class TokenModel
  4. {
  5. private \PDO $pdo;
  6. public function __construct()
  7. {
  8. if (isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof \PDO) {
  9. $this->pdo = $GLOBALS['pdo'];
  10. return;
  11. }
  12. throw new \RuntimeException('Global PDO connection not initialized');
  13. }
  14. public function getByUf(string $uf): array
  15. {
  16. $stmt = $this->pdo->prepare('SELECT token_id, token_external_id, token_commodities_amount, token_commodities_value, token_uf, token_city, token_content, token_flag, wallet_id, chain_id, commodities_id, cpr_id, user_id FROM "token" WHERE token_uf = :uf ORDER BY token_id');
  17. $stmt->execute(['uf' => $uf]);
  18. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  19. }
  20. public function getByUfAndCommodityName(string $uf, string $commoditiesName): array
  21. {
  22. $sql = 'SELECT t.token_id, t.token_external_id, t.token_commodities_amount, t.token_commodities_value, t.token_uf, t.token_city, t.token_content, t.token_flag, t.wallet_id, t.chain_id, t.commodities_id, t.cpr_id, t.user_id
  23. FROM "token" t
  24. JOIN "commodities" c ON c.commodities_id = t.commodities_id
  25. WHERE t.token_uf = :uf AND LOWER(TRIM(c.commodities_name)) = LOWER(TRIM(:name))
  26. ORDER BY t.token_id';
  27. $stmt = $this->pdo->prepare($sql);
  28. $stmt->execute(['uf' => $uf, 'name' => $commoditiesName]);
  29. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  30. }
  31. public function getByCompanyId(int $companyId): array
  32. {
  33. $sql = 'SELECT t.token_id,
  34. t.token_external_id,
  35. t.token_commodities_amount,
  36. t.token_commodities_value,
  37. t.token_uf,
  38. t.token_city,
  39. t.token_content,
  40. t.token_flag,
  41. t.wallet_id,
  42. t.chain_id,
  43. t.commodities_id,
  44. t.cpr_id,
  45. t.user_id,
  46. c.cpr_product_name,
  47. c."cpr_deliveryPlace_state_acronym",
  48. c."cpr_deliveryPlace_city_name",
  49. c.cpr_issue_value,
  50. c.cpr_packaging_way_name,
  51. c.cpr_product_quantity,
  52. c.cpr_measure_unit_name,
  53. c.cpr_production_place_name,
  54. c.cpr_maturity_date
  55. FROM "token" t
  56. INNER JOIN "wallet" w ON w.wallet_id = t.wallet_id
  57. LEFT JOIN "cpr" c ON c.cpr_id = t.cpr_id
  58. LEFT JOIN "orderbook" o ON o.token_id = t.token_id AND o.status_id = 0
  59. WHERE w.company_id = :company_id
  60. AND o.orderbook_id IS NULL
  61. ORDER BY t.token_id';
  62. $stmt = $this->pdo->prepare($sql);
  63. $stmt->execute(['company_id' => $companyId]);
  64. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  65. }
  66. public function findByCprId(int $cprId, bool $forUpdate = false): ?array
  67. {
  68. $sql = 'SELECT token_id,
  69. token_external_id,
  70. token_commodities_amount,
  71. token_commodities_value,
  72. token_uf,
  73. token_city,
  74. token_content,
  75. token_flag,
  76. wallet_id,
  77. chain_id,
  78. commodities_id,
  79. cpr_id,
  80. user_id
  81. FROM "token"
  82. WHERE cpr_id = :cpr_id
  83. ORDER BY token_id DESC
  84. LIMIT 1';
  85. if ($forUpdate) {
  86. $sql .= ' FOR UPDATE';
  87. }
  88. $stmt = $this->pdo->prepare($sql);
  89. $stmt->execute(['cpr_id' => $cprId]);
  90. $record = $stmt->fetch(\PDO::FETCH_ASSOC);
  91. return $record ?: null;
  92. }
  93. public function findById(int $tokenId): ?array
  94. {
  95. $stmt = $this->pdo->prepare(
  96. 'SELECT token_id,
  97. token_external_id,
  98. token_commodities_amount,
  99. token_commodities_value,
  100. token_uf,
  101. token_city,
  102. token_content,
  103. token_flag,
  104. wallet_id,
  105. chain_id,
  106. commodities_id,
  107. cpr_id,
  108. user_id
  109. FROM "token"
  110. WHERE token_id = :token_id
  111. LIMIT 1'
  112. );
  113. $stmt->execute(['token_id' => $tokenId]);
  114. $record = $stmt->fetch(\PDO::FETCH_ASSOC);
  115. return $record ?: null;
  116. }
  117. public function updateCommoditiesValue(int $tokenId, float $value): void
  118. {
  119. $stmt = $this->pdo->prepare(
  120. 'UPDATE "token"
  121. SET token_commodities_value = :value
  122. WHERE token_id = :token_id'
  123. );
  124. $stmt->execute([
  125. 'value' => $value,
  126. 'token_id' => $tokenId,
  127. ]);
  128. }
  129. public function updateWalletId(int $tokenId, int $walletId): void
  130. {
  131. if ($tokenId <= 0) {
  132. throw new \InvalidArgumentException('Invalid token id provided');
  133. }
  134. if ($walletId <= 0) {
  135. throw new \InvalidArgumentException('Invalid wallet id provided');
  136. }
  137. $stmt = $this->pdo->prepare(
  138. 'UPDATE "token"
  139. SET wallet_id = :wallet_id
  140. WHERE token_id = :token_id'
  141. );
  142. $stmt->execute([
  143. 'wallet_id' => $walletId,
  144. 'token_id' => $tokenId,
  145. ]);
  146. if ($stmt->rowCount() === 0) {
  147. throw new \RuntimeException('Token record not found for wallet update');
  148. }
  149. }
  150. }