Patient.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Repositories\Models\TCM;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Enums\SexEnum;
  5. use App\Repositories\Models\Model;
  6. use Carbon\Carbon;
  7. use Prettus\Repository\Contracts\Transformable;
  8. use Prettus\Repository\Traits\TransformableTrait;
  9. /**
  10. * Class Patient.
  11. *
  12. * @package namespace App\Repositories\Models\TCM;
  13. */
  14. class Patient extends Model implements Transformable
  15. {
  16. use TransformableTrait;
  17. protected $appends = ['age'];
  18. protected $table = 'tcm_patients';
  19. /**
  20. * The attributes that are mass assignable.
  21. *
  22. * @var array
  23. */
  24. protected $guarded = [];
  25. protected static function booted()
  26. {
  27. self::language();
  28. }
  29. public function setAllergyAttribute($val)
  30. {
  31. $this->attributes['allergy'] = '-' . arr2str($val, '-') . '-';
  32. }
  33. public function getAllergyAttribute($val)
  34. {
  35. return str2arr(trim($val, '-'), '-');
  36. }
  37. public function setPastMedicalAttribute($val)
  38. {
  39. if (count($val) == 0) return '';
  40. $this->attributes['past_medical'] = '-' . arr2str($val, '-') . '-';
  41. }
  42. public function getPastMedicalAttribute($val)
  43. {
  44. return str2arr(trim($val, '-'), '-');
  45. }
  46. // public function records()
  47. // {
  48. // return $this->hasMany(Record::class)->where('status', Record::STATUS_OVER);
  49. // }
  50. //
  51. public function prescriptions()
  52. {
  53. return $this->hasMany(Prescription::class);
  54. }
  55. public function medicals()
  56. {
  57. return $this->hasMany(MedicalRecord::class);
  58. }
  59. public function getAgeAttribute()
  60. {
  61. if (is_null($this->attributes['birth_date'])) return '--';
  62. return Carbon::parse($this->attributes['birth_date'])->age;
  63. }
  64. public function getSexTextAttribute()
  65. {
  66. return SexEnum::getDescription($this->attributes['sex']);
  67. }
  68. public function getSourceTextAttribute()
  69. {
  70. return Category::byIdGetCategoryValue($this->attributes['source']);
  71. }
  72. public function getAllergyTextAttribute()
  73. {
  74. if (!$this->attributes['allergy']) return '';
  75. $ids = str2arr(trim($this->attributes['allergy'], '-'), '-');
  76. return Category::byIdsGetCategoryValue($ids);
  77. }
  78. public function getPastMedicalTextAttribute()
  79. {
  80. if (!$this->attributes['past_medical']) return '';
  81. $ids = str2arr(trim($this->attributes['past_medical'], '-'), '-');
  82. return Category::byIdsGetCategoryValue($ids);
  83. }
  84. public function getLatelyRecordAttribute()
  85. {
  86. $record = self::medicals()->orderByDesc('id')->first();
  87. if ($record) {
  88. return [
  89. 'doctor' => $record->doctor,
  90. 'date' => $record->created_at->format('Y-m-d')
  91. ];
  92. } else {
  93. return [
  94. 'doctor' => str_is_null(),
  95. 'date' => str_is_null()
  96. ];
  97. }
  98. }
  99. public function getVisitCountAttribute()
  100. {
  101. return $this->medicals()->where('status', ModelStatusEnum::OK)->count();
  102. }
  103. }