UserCoin.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace app\models\user;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. class UserCoin extends BaseModel
  6. {
  7. use ModelTrait;
  8. public static function upsertAddr($uid, $symbol, $addr)
  9. {
  10. $cond = ['uid' => $uid, 'symbol' => $symbol];
  11. $row = self::where($cond)->find();
  12. if ($row) {
  13. return self::where($cond)->update(['addr' => $addr]);
  14. } else {
  15. return self::create([
  16. 'uid' => $uid,
  17. 'symbol' => $symbol,
  18. 'addr' => $addr,
  19. ]);
  20. }
  21. }
  22. public static function upsertCoin($uid, $symbol, $amount)
  23. {
  24. $cond = ['uid' => $uid, 'symbol' => $symbol];
  25. $row = self::where($cond)->find();
  26. if ($row) {
  27. return self::where($cond)->inc('balance', $amount)->update();
  28. } else {
  29. return self::create([
  30. 'uid' => $uid,
  31. 'symbol' => $symbol,
  32. 'balance' => $amount,
  33. ]);
  34. }
  35. }
  36. public static function getUserCoin($uid, $page, $limit)
  37. {
  38. $rows = self::alias('c')->join('dict_coin d', 'c.symbol=d.symbol')
  39. ->where('c.uid', $uid)
  40. ->field('d.icon,c.symbol,c.balance,c.addr')
  41. ->order('c.symbol asc')
  42. ->page(intval($page), intval($limit))
  43. ->select();
  44. count($rows) && $rows = $rows->toArray();
  45. return $rows;
  46. }
  47. }