<?php

declare(strict_types=1);

namespace App\Model;



use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Database\Model\Collection;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Snowflake\Concern\Snowflake;

/**
 * @property int $order_prescription_id 主键id
 * @property int $order_inquiry_id 订单-问诊id
 * @property int $doctor_id 医生id
 * @property int $patient_id 患者id
 * @property int $family_id 家庭成员id（就诊用户）
 * @property int $pharmacist_id 药师id
 * @property int $prescription_status 处方状态（1:待审核 3:待使用 4:已失效 5:已使用）
 * @property int $pharmacist_audit_status 药师审核状态（0:审核中 1:审核成功 2:审核驳回）
 * @property string $pharmacist_verify_time 药师审核时间
 * @property string $pharmacist_fail_reason 药师审核驳回原因
 * @property int $platform_audit_status 处方平台审核状态（0:审核中 1:审核成功 2:审核驳回）
 * @property string $platform_fail_time 平台审核失败时间
 * @property string $platform_fail_reason 处方平台驳回原因
 * @property string $doctor_created_time 医生开具处方时间
 * @property int $is_delete 是否删除（0:否 1:是）
 * @property string $prescription_code 处方编号
 * @property string $doctor_name 医生名称
 * @property string $patient_name 患者姓名-就诊人
 * @property int $patient_sex 患者性别-就诊人（1:男 2:女）
 * @property int $patient_age 患者年龄-就诊人
 * @property string $prescription_img 处方图片
 * @property string $doctor_advice 医嘱
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 * @property-read \Hyperf\Database\Model\Collection|OrderPrescriptionIcd[] $OrderPrescriptionIcd 
 * @property-read \Hyperf\Database\Model\Collection|OrderPrescriptionProduct[] $OrderPrescriptionProduct 
 * @property-read UserDoctor $UserDoctor 
 */
class OrderPrescription extends Model
{
    use Snowflake;

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

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['order_prescription_id', 'order_inquiry_id', 'doctor_id', 'patient_id', 'family_id', 'pharmacist_id', 'prescription_status', 'pharmacist_audit_status', 'pharmacist_verify_time', 'pharmacist_fail_reason', 'platform_audit_status', 'platform_fail_time', 'platform_fail_reason', 'doctor_created_time', 'is_delete', 'prescription_code', 'doctor_name', 'patient_name', 'patient_sex', 'patient_age', 'prescription_img', 'doctor_advice', 'created_at', 'updated_at'];

    protected string $primaryKey = "order_prescription_id";

    /**
     * 关联处方疾病表
     */
    public function OrderPrescriptionIcd(): HasMany
    {
        return $this->HasMany(OrderPrescriptionIcd::class, 'order_prescription_id','order_prescription_id');
    }

    /**
     * 关联处方商品表
     */
    public function OrderPrescriptionProduct(): HasMany
    {
        return $this->HasMany(OrderPrescriptionProduct::class, 'order_prescription_id','order_prescription_id');
    }

    /**
     * 关联医生表
     */
    public function UserDoctor(): HasOne
    {
        return $this->hasOne(UserDoctor::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 Collection|array
     */
    public static function getList(array $params = [], array $fields = ['*']): Collection|array
    {
        return self::where($params)->get($fields);
    }

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

    /**
     * 获取处方列表-分页
     * @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
    {
        $raw = self::where($params)->paginate($per_page, $fields, "page", $page);
        $data = array();
        $data['current_page'] = $raw->currentPage();// 当前页码
        $data['total'] = $raw->total();//数据总数
        $data['data'] = $raw->items();//数据
        $data['per_page'] = $raw->perPage();//每页个数
        $data['last_page'] = $raw->lastPage();//最后一页

        return $data;
    }

    /**
     * 获取处方列表-分页
     * @param array $params 条件
     * @param array $fields 字段
     * @param int|null $page 页码
     * @param int|null $per_page 每页个数
     * @return array
     */
    public static function getWithIcdPage(array $params, array $fields = ["*"], int $page = null, ?int $per_page = 10): array
    {
        $raw = self::with([
            'OrderPrescriptionIcd'
        ])
            ->where($params)
            ->paginate($per_page, $fields, "page", $page);
        $data = array();
        $data['current_page'] = $raw->currentPage();// 当前页码
        $data['total'] = $raw->total();//数据总数
        $data['data'] = $raw->items();//数据
        $data['per_page'] = $raw->perPage();//每页个数
        $data['last_page'] = $raw->lastPage();//最后一页

        return $data;
    }

    /**
     * 修改
     * @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 $data
     * @return \Hyperf\Database\Model\Model|OrderPrescription
     */
    public static function addOrderPrescription(array $data): \Hyperf\Database\Model\Model|OrderPrescription
    {
        return self::create($data);
    }

    /**
     * 获取数量
     * @param array $params
     * @return int
     */
    public static function getCount(array $params): int
    {
        return self::where($params)->count();
    }

    /**
     * 分页
     * 处方疾病表
     * 处方商品表
     * @param array $params
     * @param array $fields
     * @param int|null $page
     * @param int|null $per_page
     * @return array
     */
    public static function getWithPage(array $params, array $fields = ["*"], int $page = null, ?int $per_page = 10): array
    {
        $raw = self::with([
            "UserDoctor:doctor_id,user_name,doctor_title",
            "OrderPrescriptionIcd:prescription_icd_id,order_prescription_id,icd_name",
            "OrderPrescriptionProduct:prescription_product_id,order_prescription_id,product_name,product_spec"
        ])
            ->where($params)->paginate($per_page, $fields, "page", $page);

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

        return $data;
    }

    /**
     * 单条
     * 处方疾病表
     * 处方商品表
     * @param array $params
     * @param array $fields
     * @param int|null $page
     * @param int|null $per_page
     * @return object|null
     */
    public static function getWithOne(array $params, array $fields = ["*"], int $page = null, ?int $per_page = 10): object|null
    {
        return self::with([
            "UserDoctor:doctor_id,user_name,doctor_title",
            "OrderPrescriptionIcd:prescription_icd_id,order_prescription_id,icd_name",
            "OrderPrescriptionProduct:prescription_product_id,order_prescription_id,product_name,product_spec"
        ])
            ->where($params)->first($fields);
    }
}
