User.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 (isset($arr['doctor_id'])){
  43. //说明是医生登陆!
  44. if (empty($arr['doctor_id'])) {
  45. exit_out(null, 401, '医生验证失效,请重新登录');
  46. }
  47. $user = DB::table('docters')->where('id', $arr['doctor_id'])->first();
  48. if (empty($user)){
  49. exit_out(null, 601, '该账号已被删除');
  50. }
  51. $user = object_array($user);
  52. return $user;
  53. }
  54. else{
  55. if (empty($arr['id'])) {
  56. exit_out(null, 401, '账号验证失效,请重新登录');
  57. }
  58. $user = User::where('id', $arr['id'])->first();
  59. if (empty($user)){
  60. exit_out(null, 601, '该账号已被删除');
  61. }
  62. $user = $user->toArray();
  63. if ($user['status'] == 0){
  64. exit_out(null, 602, '该账号已被冻结');
  65. }
  66. return $user;
  67. }
  68. }
  69. /**
  70. * 用户医生关注表
  71. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  72. * @author Liu-Yh
  73. * Create By 2020/11/18 11:06
  74. */
  75. public function userDorter(){
  76. return $this->hasOne(UserDocter::class,'user_id','id')->select(['id', 'remark']);;
  77. }
  78. //改变用户余额
  79. public static function changeBalance($user_id, $change_balance, $type, $relation_id, $remark, $admin_user_id = 0)
  80. {
  81. $user = User::select(['balance'])->where('id', $user_id)->first();
  82. if ($type == 2) {
  83. User::where('id', $user_id)->update([
  84. 'balance' => DB::raw('balance + '.$change_balance),
  85. 'topup_balance' => DB::raw('topup_balance + '.$change_balance),
  86. ]);
  87. }
  88. elseif ($type == 3) {
  89. User::where('id', $user_id)->update([
  90. 'balance' => DB::raw('balance + '.$change_balance),
  91. 'giving_balance' => DB::raw('giving_balance + '.$change_balance),
  92. ]);
  93. }
  94. else {
  95. User::where('id', $user_id)->increment('balance', $change_balance);
  96. }
  97. UserBalanceLog::create([
  98. 'user_id' => $user_id,
  99. 'admin_user_id' => $admin_user_id,
  100. 'type' => $type,
  101. 'relation_id' => $relation_id,
  102. 'before_balance' => $user['balance'],
  103. 'change_balance' => $change_balance,
  104. 'after_balance' => $user['balance'] + $change_balance,
  105. 'remark' => $remark,
  106. ]);
  107. return true;
  108. }
  109. public function UserBalanceLog(){
  110. return $this->hasMany(UserBalanceLog::class);
  111. }
  112. public function patient(){
  113. return $this->hasMany(Patient::class,'user_id','id');
  114. }
  115. }