<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 医生模型
 * Class MedicalCareDoctor
 * @package app\admin\model
 */
class MedicalCareDoctor extends Model
{
    const UPDATED_AT = 'doctor_update_time';//模型自动更新时间字段
    const TYPE_HOSPITAL  = 0;
    const TYPE_PHYSICIAN = 1;
    const TYPE_FAMILY    = 2;
    const DOCTOR_TYPE = [
        self::TYPE_HOSPITAL  => '医院医生',
        self::TYPE_PHYSICIAN => '健康管理师',
        self::TYPE_FAMILY    => '家庭医生'
    ];

    const ON_LINE_NO = 0;
    const ON_LINE_YES = 1;
    const DOCTOR_ON_LINE = [
        self::ON_LINE_NO  => '不支持',
        self::ON_LINE_YES => '支持线上'
    ];

    const STATUS_WORK = 1;
    const STATUS_REST = 0;
    const DOCTOR_STATUS = [
        self::STATUS_REST => '休班',
        self::STATUS_WORK => '上班'
    ];

    const DEL_NO  = 0;
    const DEL_YES = 1;
    const DOCTOR_DEL = [
        self::DEL_NO  => '未删除',
        self::DEL_YES => '已删除'
    ];
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'medical_care_doctor';

    protected $dateFormat = 'U';//操作数据库时间格式 时间戳

    /**
     * Notes:模型关联 科目
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * User: yym
     * Date: 2022/9/26
     */
    public function subject()
    {
        return $this->hasOne(MedicalCareSubject::class, 'subject_id', 'doctor_subject_id')->where(['subject_del' => MedicalCareSubject::DEL_NO]);
    }

    public function merchant_shop()
    {
        return $this->hasOne(MerchantShop::class, 'shop_id', 'doctor_shop_id')->where(['shop_del' => MerchantShop::DEL_NO]);
    }
    /**
     * Notes:获取医生列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/9/21
     */
    public static function  getDoctorList(int $page, int $limit, $keywords)
    {
        $list = static::select('*')
            ->where(['doctor_del' => static::DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('doctor_name', 'like', '%' . $keywords . '%')
                ->orWhere('doctor_mobile', 'like', '%' . $keywords . '%');
            })
            ->with(
                [
                    'subject' => function($query){
                        $query->select('subject_id', 'subject_name');
                    },
                    'merchant_shop' => function($query){
                        $query->select('shop_id', 'shop_name', 'shop_logo');
                    }
                ]
            )
            ->orderBy('doctor_create_time','DESC')
            ->forPage($page, $limit)
            ->get();
        $count = static::count();
        return [$list, $count];
    }

    /**
     * Notes:检测医生是否存在
     * @param $doctor_name
     * @param $doctor_mobile
     * @return bool
     * User: yym
     * Date: 2022/9/26
     * 验证条件：医生姓名+手机号同时存在的情况下
     */
    public static function checkDoctorName($doctor_name, $doctor_mobile)
    {
        return static::where(['doctor_name' => $doctor_name, 'doctor_mobile' => $doctor_mobile, 'doctor_del' => static::DEL_NO])->exists();
    }

    /**
     * Notes:检测不同账号姓名存在
     * @param $doctor_name
     * @param $doctor_mobile
     * @param $doctor_id
     * @return bool
     * User: yym
     * Date: 2022/9/26
     */
    public static function checkDoctorCount($doctor_name, $doctor_mobile, $doctor_id)
    {
        return static::where(['doctor_name' => $doctor_name, 'doctor_mobile' => $doctor_mobile, 'doctor_del' => static::DEL_NO])->where('doctor_id', '!=', $doctor_id)->exists();
    }

    /**
     * Notes:获取医生详情
     * @param int $doctor_id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: yym
     * Date: 2022/9/26
     */
    public static function getDoctorInfo(int $doctor_id)
    {
        return static::where(['doctor_id' => $doctor_id])
            ->with(
                [
                    'subject' => function($query){
                        $query->select('subject_id', 'subject_name');
                    },
                    'merchant_shop' => function($query){
                        $query->select('shop_id', 'shop_name', 'shop_logo');
                    }
                ]
            )
            ->first();
    }

    /**
     * Notes:更新医生数据
     * @param int $doctor_id
     * @param array $update
     * @return false|int
     * User: yym
     * Date: 2022/9/26
     */
    public static function updateDoctor(int $doctor_id, array $update)
    {
        if(empty($doctor_id) || empty($update))
        {
            return false;
        }
        return static::where(['doctor_id' => $doctor_id])->update($update);
    }
}
