FundModel.php 2.5 KB

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