FundModel.php 2.0 KB

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