<?php
/**
 * Created by PhpStorm.
 * User: zilongs
 * Date: 20-10-1
 * Time: 上午12:10
 */

namespace App\Models;
use App\Models\User;

class Patient extends BaseModel
{
    protected $appends = ['age'];
    protected $table="patients";

    public function getAgeAttribute()
    {
        return birthday_to_age($this->birthday);
    }

    public function getOrderNumAttribute()
    {
        return Order::where('patient_id', $this->id)->where('payment_status', 2)->where('product_type', '<', 6)->count();
    }

    public function getPackDocterNumAttribute()
    {
        $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();
        return TeamDocter::whereIn('team_id', $team_ids)->count();
    }

    public function getPerfectdDegreeAttribute()
    {
        $field = ['name', 'sex', 'avatar', 'birthday', 'relationship_type', 'card_number', 'email', 'phone', 'social_card_number', 'born_hospital', 'guardian_name', 'address'];
        $total = count($field);
        $fill = 0;
        foreach ($field as $k => $v) {
            if (!empty($this->$v)) {
                $fill++;
            }
        }
        $text = round($fill/$total*100) . '%';
        return $text;
    }
    public function users(){
        return $this->belongsTo(User::class,'id','user_id');
    }
    public function order(){
        return $this->hasMany(Order::class);
    }
}
