Patient.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-10-1
  6. * Time: 上午12:10
  7. */
  8. namespace App\Models;
  9. class Patient extends BaseModel
  10. {
  11. protected $appends = ['age'];
  12. protected $table = "patients";
  13. public function getAgeAttribute()
  14. {
  15. return birthday_to_age($this->birthday);
  16. }
  17. public function getOrderNumAttribute()
  18. {
  19. return Order::where('patient_id', $this->id)->where('payment_status', 2)->where('product_type', '<', 6)->count();
  20. }
  21. public function getPackDocterNumAttribute()
  22. {
  23. $team_ids = OrderPack::join('orders', 'orders.id', '=', 'order_packs.order_id')->where('orders.patient_id', $this->id)->where('orders.payment_status', 2)->where('orders.product_type', 7)->pluck('order_packs.team_id')->toArray();
  24. return TeamDocter::whereIn('team_id', $team_ids)->count();
  25. }
  26. public function getPerfectdDegreeAttribute()
  27. {
  28. $field = ['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'card_number', 'email', 'phone', 'social_card_number', 'born_hospital', 'guardian_name', 'address'];
  29. $total = count($field);
  30. $fill = 0;
  31. foreach ($field as $k => $v) {
  32. if (!empty($this->$v)) {
  33. $fill++;
  34. }
  35. }
  36. $text = round($fill/$total*100) . '%';
  37. return $text;
  38. }
  39. public function users()
  40. {
  41. return $this->belongsTo(User::class,'user_id','id');
  42. }
  43. public function userspatient()
  44. {
  45. return $this->belongsTo(User::class,'user_id','id');
  46. }
  47. public function order()
  48. {
  49. return $this->hasMany(Order::class);
  50. }
  51. public function user()
  52. {
  53. return $this->belongsTo(User::class);
  54. }
  55. public function orderPatient()
  56. {
  57. return $this->hasOne(OrderPatient::class,'patient_id','id');
  58. }
  59. public function getRelationshipTypeTextAttribute()
  60. {
  61. return config('config.relationship_type_map')[$this->relationship_type] ?? '';
  62. }
  63. }