TokenModel.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. FROM "token" t
  47. INNER JOIN "wallet" w ON w.wallet_id = t.wallet_id
  48. WHERE w.company_id = :company_id
  49. ORDER BY t.token_id';
  50. $stmt = $this->pdo->prepare($sql);
  51. $stmt->execute(['company_id' => $companyId]);
  52. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  53. }
  54. }