FundModel.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * field start
  11. * @property int $id 自增
  12. * @property int $user_id 用户ID
  13. * @property int $fund_id 资金ID
  14. * @property int $balance 余额
  15. * @property int $update_time 更新时间
  16. * @property int $create_time 创建时间
  17. * field end
  18. *
  19. */
  20. class FundModel extends ModelCore
  21. {
  22. protected $table = 'fund';
  23. public $timestamps = false;
  24. // attrlist start
  25. protected $fillable = [
  26. 'id',
  27. 'user_id',
  28. 'fund_id',
  29. 'balance',
  30. 'update_time',
  31. 'create_time',
  32. ];
  33. // attrlist end
  34. protected $casts = [
  35. 'fund_id' => FUND_TYPE::class
  36. ];
  37. public static function userAccount($user_id)
  38. {
  39. $q = self::query()->where([
  40. 'user_id' => $user_id,
  41. ])->lockForUpdate()->get();
  42. return $q;
  43. }
  44. /**
  45. *
  46. * @param $user_id
  47. * @param $fund_id
  48. * @return false|FundModel
  49. */
  50. static public function getAccount($user_id, $fund_id)
  51. {
  52. $q = self::query()->where([
  53. 'user_id' => $user_id,
  54. 'fund_id' => $fund_id
  55. ])->lockForUpdate()->first();
  56. return $q;
  57. }
  58. /**
  59. * @param $userId
  60. * @param $fundIds
  61. * @return \Illuminate\Database\Eloquent\Collection
  62. * 获取用户全部资金账户
  63. */
  64. public static function getAllAccount($userId, $fundIds)
  65. {
  66. $query = self::query();
  67. $query->where('user_id', $userId);
  68. $query->whereIn('fund_id', $fundIds);
  69. return $query->get();
  70. }
  71. public function user()
  72. {
  73. return $this->hasOne(User::class, 'id', 'user_id');
  74. }
  75. //
  76. public function userInfo()
  77. {
  78. return $this->hasOne(UserInfo::class, 'user_id', 'user_id');
  79. }
  80. /**
  81. * @param $userId
  82. * @param $fundId
  83. * @param $amount
  84. * @return int
  85. * 减少资金
  86. */
  87. public static function dec($userId, $fundId, $amount)
  88. {
  89. $query = self::query();
  90. $query->where('user_id', $userId);
  91. $query->where('fund_id', $fundId);
  92. return $query->decrement('balance', $amount);
  93. }
  94. /**
  95. * @param $userId
  96. * @param $fundId
  97. * @param $amount
  98. * @return int
  99. * 增加资金
  100. */
  101. public static function inc($userId, $fundId, $amount)
  102. {
  103. $query = self::query();
  104. $query->where('user_id', $userId);
  105. $query->where('fund_id', $fundId);
  106. return $query->increment('balance', $amount);
  107. }
  108. }