User.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-23
  6. * Time: 上午11:04
  7. */
  8. namespace App\Models;
  9. use DB;
  10. class User extends BaseModel
  11. {
  12. protected $appends = ['age'];
  13. protected $table = 'users';
  14. //从生日转成年龄
  15. public function getAgeAttribute()
  16. {
  17. return birthday_to_age($this->birthday);
  18. }
  19. public function getCouponNumAttribute()
  20. {
  21. return UserCoupon::where('user_id', $this->id)->where('status', 1)->where('expire_time', '>', time())->count();
  22. }
  23. public function patients()
  24. {
  25. return $this->hasMany(Patient::class);
  26. }
  27. public function order()
  28. {
  29. return $this->hasMany(Order::class);
  30. }
  31. //通过token获取用户信息
  32. public static function getUserByToken($is_exit = true)
  33. {
  34. $auth = request()->header('token');
  35. if (empty($auth)) {
  36. if (!$is_exit) {
  37. return [];
  38. }
  39. exit_out(null, 401, '验证失效,请重新登录');
  40. }
  41. $arr = aes_decrypt($auth);
  42. // if (empty($arr['id'])) {
  43. // if (!$is_exit) {
  44. // return [];
  45. // }
  46. // exit_out(null, 401, '账号验证失效,请重新登录');
  47. // }
  48. if (isset($arr['doctor_id'])){
  49. // 说明是医生登陆!
  50. if (empty($arr['doctor_id'])) {
  51. exit_out(null, 401, '医生验证失效,请重新登录');
  52. }
  53. $user = Docter::where('id', $arr['doctor_id'])->first();
  54. if (empty($user)){
  55. exit_out(null, 601, '该账号已被删除');
  56. }
  57. $user = $user->toArray();
  58. // if ($user['status'] == 0){
  59. // exit_out(null, 602, '该账号已被冻结');
  60. // }
  61. return $user;
  62. }else{
  63. if (empty($arr['id'])) {
  64. exit_out(null, 401, '账号验证失效,请重新登录');
  65. }
  66. $user = User::where('id', $arr['id'])->first();
  67. if (empty($user)){
  68. exit_out(null, 601, '该账号已被删除');
  69. }
  70. $user = $user->toArray();
  71. if ($user['status'] == 0){
  72. exit_out(null, 602, '该账号已被冻结');
  73. }
  74. return $user;
  75. }
  76. }
  77. /**
  78. * 用户医生关注表
  79. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  80. * @author Liu-Yh
  81. * Create By 2020/11/18 11:06
  82. */
  83. public function userDorter(){
  84. return $this->hasOne(UserDocter::class,'user_id','id')->select(['id', 'remark']);;
  85. }
  86. //改变用户余额
  87. public static function changeBalance($user_id, $change_balance, $type, $relation_id, $remark, $admin_user_id = 0)
  88. {
  89. $user = User::select(['balance'])->where('id', $user_id)->first();
  90. if ($type == 2) {
  91. User::where('id', $user_id)->update([
  92. 'balance' => DB::raw('balance + '.$change_balance),
  93. 'topup_balance' => DB::raw('topup_balance + '.$change_balance),
  94. ]);
  95. }
  96. elseif ($type == 3) {
  97. User::where('id', $user_id)->update([
  98. 'balance' => DB::raw('balance + '.$change_balance),
  99. 'giving_balance' => DB::raw('giving_balance + '.$change_balance),
  100. ]);
  101. }
  102. else {
  103. User::where('id', $user_id)->increment('balance', $change_balance);
  104. }
  105. UserBalanceLog::create([
  106. 'user_id' => $user_id,
  107. 'admin_user_id' => $admin_user_id,
  108. 'type' => $type,
  109. 'relation_id' => $relation_id,
  110. 'before_balance' => $user['balance'],
  111. 'change_balance' => $change_balance,
  112. 'after_balance' => $user['balance'] + $change_balance,
  113. 'remark' => $remark,
  114. ]);
  115. return true;
  116. }
  117. public function UserBalanceLog(){
  118. return $this->hasMany(UserBalanceLog::class);
  119. }
  120. public function patient(){
  121. return $this->hasMany(Patient::class,'user_id','id');
  122. }
  123. }