FundModel.php 1.9 KB

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