<?php

declare(strict_types=1);

namespace App\Model;



use Hyperf\Database\Model\Collection;
use Hyperf\Database\Model\Relations\BelongsTo;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Database\Model\Relations\HasOneThrough;
use Hyperf\DbConnection\Db;
use Hyperf\Snowflake\Concern\Snowflake;

/**
 * @property int $history_inquiry_id 主键id
 * @property int $patient_id 患者id
 * @property int $doctor_id 医生id
 * @property int $pharmacist_id 药师id
 * @property int $order_inquiry_id 订单-问诊id
 * @property int $history_status 删除状态（0:否 1:是）
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 * @property-read UserDoctor $UserDoctor 
 * @property-read Hospital $UserDoctorHospital 
 */
class PatientHistoryInquiry extends Model
{
    use Snowflake;

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

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

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['history_inquiry_id' => 'integer', 'patient_id' => 'integer', 'doctor_id' => 'integer', 'pharmacist_id' => 'integer', 'order_inquiry_id' => 'integer', 'history_status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    protected string $primaryKey = "history_inquiry_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",
        );
    }

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

    /**
     * 获取信息-单条
     * @param array $params
     * @param array $fields
     * @return object|null
     */
    public static function getOne(array $params, array $fields = ['*']): object|null
    {
        return self::where($params)->first($fields);
    }

    /**
     * 获取信息-多条
     * @param array $params
     * @param array $fields
     * @return object|null
     */
    public static function getList(array $params, array $fields = ['*']): object|null
    {
        return self::where($params)->get($fields);
    }

    /**
     * 获取信息-多条-限制条数
     * @param array $params
     * @param array $fields
     * @param int $limit
     */
    public static function getLimit(array $params, array $fields = ['*'],int $limit = 5): array
    {
        return self::where($params)->limit($limit)->get($fields)->toArray();
    }

    /**
     * 获取首页用户历史问诊数据
     * 限制数量
     * @param array $params
     * @param array $fields
     * @param int $limit
     * @return Collection|array|\Hyperf\Utils\Collection
     */
    public static function getIndexHistoryDoctorLimit(array $params, array $fields = ["*"],int $limit = 5): Collection|array|\Hyperf\Utils\Collection
    {
        return self::with([
            "UserDoctor:doctor_id,user_name,avatar,hospital_id",
            "UserDoctor.Hospital:hospital_id,hospital_name"
        ])
            ->where($params)
            ->limit($limit)
            ->get($fields);
    }

    /**
     * 获取首页用户历史问诊数据
     * @param array $params
     * @param array $fields
     * @return Collection|array|\Hyperf\Utils\Collection
     */
    public static function getIndexHistoryDoctorList(array $params, array $fields = ["*"]): Collection|array|\Hyperf\Utils\Collection
    {
        return self::with([
            "UserDoctor:doctor_id,user_name,avatar,hospital_id",
            "UserDoctor.Hospital:hospital_id,hospital_name"
        ])
            ->where($params)
            ->get($fields);

    }

    /**
     * 修改
     * @param array $params
     * @param array $data
     * @return int
     */
    public static function edit(array $params = [], array $data = []): int
    {
        return self::where($params)->update($data);
    }

    /**
     * 获取分页数据
     * @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)
            ->groupBy('doctor_id')
            ->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;
    }
}
