FundModel.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Module\Fund\Models;
  3. use App\Module\Fund\Enums\FUND_TYPE;
  4. use App\Module\User\Models\User;
  5. use App\Module\User\Models\UserInfo;
  6. use UCore\ModelCore;
  7. /**
  8. * 用户账户表
  9. *
  10. * 存储用户的各种账户信息及余额。每个用户可以拥有多个不同种类的账户,
  11. * 每个账户对应一种账户种类(FundConfigModel)。
  12. * 同一币种可以有多个不同用途的账户,如可用账户、冻结账户等。
  13. *
  14. * field start
  15. * @property int $id 自增
  16. * @property int $user_id 用户ID
  17. * @property \App\Module\Fund\Enums\FUND_TYPE $fund_id 资金ID
  18. * @property int $balance 余额
  19. * @property int $update_time 更新时间
  20. * @property int $create_time 创建时间
  21. * field end
  22. */
  23. class FundModel extends ModelCore
  24. {
  25. protected $table = 'fund';
  26. public $timestamps = false;
  27. // attrlist start
  28. protected $fillable = [
  29. 'id',
  30. 'user_id',
  31. 'fund_id',
  32. 'balance',
  33. 'update_time',
  34. 'create_time',
  35. ];
  36. // attrlist end
  37. protected $casts = [
  38. 'fund_id' => FUND_TYPE::class
  39. ];
  40. public static function userAccount($user_id)
  41. {
  42. $q = self::query()->where([
  43. 'user_id' => $user_id,
  44. ])->lockForUpdate()->get();
  45. return $q;
  46. }
  47. /**
  48. *
  49. * @param $user_id
  50. * @param $fund_id
  51. * @return false|FundModel
  52. */
  53. static public function getAccount($user_id, $fund_id)
  54. {
  55. $q = self::query()->where([
  56. 'user_id' => $user_id,
  57. 'fund_id' => $fund_id
  58. ])->lockForUpdate()->first();
  59. return $q;
  60. }
  61. /**
  62. * @param $userId
  63. * @param $fundIds
  64. * @return \Illuminate\Database\Eloquent\Collection
  65. * 获取用户全部资金账户
  66. */
  67. public static function getAllAccount($userId, $fundIds)
  68. {
  69. $query = self::query();
  70. $query->where('user_id', $userId);
  71. $query->whereIn('fund_id', $fundIds);
  72. return $query->get();
  73. }
  74. public function user()
  75. {
  76. return $this->hasOne(User::class, 'id', 'user_id');
  77. }
  78. //
  79. public function userInfo()
  80. {
  81. return $this->hasOne(UserInfo::class, 'user_id', 'user_id');
  82. }
  83. /**
  84. * @param $userId
  85. * @param $fundId
  86. * @param $amount
  87. * @return int
  88. * 减少资金
  89. */
  90. public static function dec($userId, $fundId, $amount)
  91. {
  92. $query = self::query();
  93. $query->where('user_id', $userId);
  94. $query->where('fund_id', $fundId);
  95. return $query->decrement('balance', $amount);
  96. }
  97. /**
  98. * @param $userId
  99. * @param $fundId
  100. * @param $amount
  101. * @return int
  102. * 增加资金
  103. */
  104. public static function inc($userId, $fundId, $amount)
  105. {
  106. $query = self::query();
  107. $query->where('user_id', $userId);
  108. $query->where('fund_id', $fundId);
  109. return $query->increment('balance', $amount);
  110. }
  111. }