FundModel.php 2.3 KB

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