FundModel.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. *
  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. /**
  38. *
  39. * @param $user_id
  40. * @param $fund_id
  41. * @return false|FundModel
  42. */
  43. static public function getAccount($user_id, $fund_id)
  44. {
  45. $q = self::query()->where([
  46. 'user_id' => $user_id,
  47. 'fund_id' => $fund_id
  48. ])->lockForUpdate()->first();
  49. return $q;
  50. }
  51. /**
  52. * @param $userId
  53. * @param $fundIds
  54. * @return \Illuminate\Database\Eloquent\Collection
  55. * 获取用户全部资金账户
  56. */
  57. public static function getAllAccount($userId, $fundIds)
  58. {
  59. $query = self::query();
  60. $query->where('user_id', $userId);
  61. $query->whereIn('fund_id', $fundIds);
  62. return $query->get();
  63. }
  64. public function user()
  65. {
  66. return $this->hasOne(User::class, 'id', 'user_id');
  67. }
  68. //
  69. public function userInfo()
  70. {
  71. return $this->hasOne(UserInfo::class, 'user_id', 'user_id');
  72. }
  73. /**
  74. * @param $userId
  75. * @param $fundId
  76. * @param $amount
  77. * @return int
  78. * 减少资金
  79. */
  80. public static function dec($userId, $fundId, $amount)
  81. {
  82. $query = self::query();
  83. $query->where('user_id', $userId);
  84. $query->where('fund_id', $fundId);
  85. return $query->decrement('balance', $amount);
  86. }
  87. /**
  88. * @param $userId
  89. * @param $fundId
  90. * @param $amount
  91. * @return int
  92. * 增加资金
  93. */
  94. public static function inc($userId, $fundId, $amount)
  95. {
  96. $query = self::query();
  97. $query->where('user_id', $userId);
  98. $query->where('fund_id', $fundId);
  99. return $query->increment('balance', $amount);
  100. }
  101. }