<?php

declare(strict_types=1);

namespace App\Model;



use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Database\Model\Relations\HasOneThrough;
use Hyperf\Snowflake\Concern\Snowflake;

/**
 * @property int $patient_follow_id 主键id
 * @property int $patient_id 患者id
 * @property int $doctor_id 医生id
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 */
class PatientFollow extends Model
{
    use Snowflake;

    /**
     * The table associated with the model.
     */
    protected ?string $table = 'patient_follow';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['patient_follow_id', 'patient_id', 'doctor_id', 'created_at', 'updated_at'];

    protected string $primaryKey = "patient_follow_id";

    /**
     * 关联医生专长表
     * @return HasMany
     */
    public function DoctorExpertise(): HasMany
    {
        return $this->hasMany(DoctorExpertise::class, "doctor_id", "doctor_id");
    }

    /**
     * 关联医生表
     */
    public function UserDoctor(): HasOne
    {
        return $this->hasOne(UserDoctor::class, 'doctor_id','doctor_id');
    }

    /**
     * 远程关联医生表-医院表
     * @return HasOneThrough
     */
    public function UserDoctorHospital(): HasOneThrough
    {
        return $this->hasOneThrough(
            Hospital::class,
            UserDoctor::class,
            "doctor_id",
            "hospital_id",
            "doctor_id",
            "hospital_id",
        );
    }

    /**
     * 获取是否存在
     * @param array $params
     * @return bool
     */
    public static function getExists(array $params): bool
    {
        return self::where($params)->exists();
    }

    /**
     * 新增
     * @param array $data
     * @return \Hyperf\Database\Model\Model|PatientFollow
     */
    public static function addPatientFollow(array $data): \Hyperf\Database\Model\Model|PatientFollow
    {
        return self::create($data);
    }

    /**
     * @param array $params
     * @return int|mixed
     */
    public static function deletePatientFollow(array $params = []): mixed
    {
        return self::where($params)->delete();
    }

    /**
     * 获取分页数据
     * @param array $params
     * @param array $fields
     * @param int|null $page
     * @param int|null $per_page
     * @return array
     */
    public static function getPage(array $params,array $fields = ["*"], int $page = null, ?int $per_page = 10): array
    {
        $query = self::with([
            'DoctorExpertise:doctor_expertise_id,doctor_id,expertise_id',
            'DoctorExpertise.DiseaseClassExpertise:expertise_id,expertise_name',
            "UserDoctor:doctor_id,user_name,avatar,doctor_title,hospital_id,multi_point_status,department_custom_name,be_good_at",
            "UserDoctor.Hospital:hospital_id,hospital_name"
        ])
            ->where($params)
            ->paginate($per_page, $fields, "page", $page);

        $data = array();
        $data['current_page'] = $query->currentPage();// 当前页码
        $data['total'] = $query->total();//数据总数
        $data['data'] = $query->items();//数据
        $data['per_page'] = $query->perPage();//每页个数
        $data['last_page'] = $query->lastPage();//最后一页

        return $data;
    }
}
