123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace App\Repositories\Models\TCM;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Enums\SexEnum;
- use App\Repositories\Models\Model;
- use Carbon\Carbon;
- use Prettus\Repository\Contracts\Transformable;
- use Prettus\Repository\Traits\TransformableTrait;
- /**
- * Class Patient.
- *
- * @package namespace App\Repositories\Models\TCM;
- */
- class Patient extends Model implements Transformable
- {
- use TransformableTrait;
- protected $appends = ['age'];
- protected $table = 'tcm_patients';
- /**
- * The attributes that are mass assignable.
- *
- * @var array
- */
- protected $guarded = [];
- protected static function booted()
- {
- self::language();
- }
- public function setAllergyAttribute($val)
- {
- $this->attributes['allergy'] = '-' . arr2str($val, '-') . '-';
- }
- public function getAllergyAttribute($val)
- {
- return str2arr(trim($val, '-'), '-');
- }
- public function setPastMedicalAttribute($val)
- {
- if (count($val) == 0) return '';
- $this->attributes['past_medical'] = '-' . arr2str($val, '-') . '-';
- }
- public function getPastMedicalAttribute($val)
- {
- return str2arr(trim($val, '-'), '-');
- }
- // public function records()
- // {
- // return $this->hasMany(Record::class)->where('status', Record::STATUS_OVER);
- // }
- //
- public function prescriptions()
- {
- return $this->hasMany(Prescription::class);
- }
- public function medicals()
- {
- return $this->hasMany(MedicalRecord::class);
- }
- public function getAgeAttribute()
- {
- if (is_null($this->attributes['birth_date'])) return '--';
- return Carbon::parse($this->attributes['birth_date'])->age;
- }
- public function getSexTextAttribute()
- {
- return SexEnum::getDescription($this->attributes['sex']);
- }
- public function getSourceTextAttribute()
- {
- return Category::byIdGetCategoryValue($this->attributes['source']);
- }
- public function getAllergyTextAttribute()
- {
- if (!$this->attributes['allergy']) return '';
- $ids = str2arr(trim($this->attributes['allergy'], '-'), '-');
- return Category::byIdsGetCategoryValue($ids);
- }
- public function getPastMedicalTextAttribute()
- {
- if (!$this->attributes['past_medical']) return '';
- $ids = str2arr(trim($this->attributes['past_medical'], '-'), '-');
- return Category::byIdsGetCategoryValue($ids);
- }
- public function getLatelyRecordAttribute()
- {
- $record = self::medicals()->orderByDesc('id')->first();
- if ($record) {
- return [
- 'doctor' => $record->doctor,
- 'date' => $record->created_at->format('Y-m-d')
- ];
- } else {
- return [
- 'doctor' => str_is_null(),
- 'date' => str_is_null()
- ];
- }
- }
- public function getVisitCountAttribute()
- {
- return $this->medicals()->where('status', ModelStatusEnum::OK)->count();
- }
- }
|