<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 医疗服务-预约服务业务员模型
 * Class Users
 * @package app\admin\model
 */
class MedicalCareSubject extends Model
{
    const STATUS_DEL_NO = '0';
    const STATUS_DEL_YES = '1';
    const STATUS_DEL = [
        self::STATUS_DEL_NO => '正常',
        self::STATUS_DEL_YES => '删除'
    ];

    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_subject';

    public $timestamps = false;

    /**
     * Notes:子集关联
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * User: yym
     * Date: 2022/10/26
     */
    public function children()
    {
        return $this->hasMany(MedicalCareSubject::class, 'subject_pid', 'subject_id');
    }

    /**
     * Notes:获取菜单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/14
     */
    public static function getSubjectList(int $page, int $limit, string $keywords)
    {
//        if (!empty($keywords)){
//            $list = static::select('*')->where(['subject_del'=>static::STATUS_DEL_NO])
//                ->when($keywords != '', function ($query) use ($keywords){
//                    $query->where('subject_name', 'like', '%' . $keywords . '%');
//                })
//                ->forPage($page,$limit)
//                ->orderBy('subject_sort','DESC')
//                ->orderBy('subject_create_time','DESC')
//                ->get()
//                ->toArray();
//            $count = static::where(['subject_del'=>static::STATUS_DEL_NO])
//                ->when($keywords != '', function ($query) use ($keywords){
//                    $query->where('subject_name', 'like', '%' . $keywords . '%');
//                })
//                ->count();
//        }else{
            $list = static::select('*')->where(['subject_del'=>static::STATUS_DEL_NO,'subject_pid'=>0])
                ->when($keywords != '', function ($query) use ($keywords){
                    $query->where('subject_name', 'like', '%' . $keywords . '%');
                })
                ->with(['children'=>
                function ($query) {
                    $query->where(['subject_del'=>static::STATUS_DEL_NO]);
                }
                ])
                ->forPage($page,$limit)
                ->orderBy('subject_sort','DESC')
                ->orderBy('subject_create_time','DESC')
                ->get()
                ->toArray();
            $count = static::where(['subject_del'=>static::STATUS_DEL_NO,'subject_pid'=>0])
                ->when($keywords != '', function ($query) use ($keywords){
                    $query->where('subject_name', 'like', '%' . $keywords . '%');
                })
                ->count();
//        }

        return [$list,$count];
    }

    /**
     * Notes:获取菜单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/14
     */
    public static function getChildrenList($pid)
    {
        $list = static::select('*')->where(['subject_del'=>static::STATUS_DEL_NO])
            ->whereIn('subject_pid',$pid)
            ->orderBy('subject_sort','DESC')
            ->orderBy('subject_create_time','DESC')
            ->get()
            ->toArray();
        return $list;
    }


    /**
     * Notes:查询指定条件下的某个字段内容
     * @param int $subject_id
     * @param string $field
     * User: ZQ
     * Date: 2022/9/27
     */
    public static function getValue(int $subject_id, string $field)
    {
        return static::where(['subject_id' => $subject_id])->value($field);
    }

    /**
     * Notes:获取列表
     * @return array
     * User: yym
     * Date: 2022/10/26
     */
    public static function getDoctorSubjectList()
    {
        return static::where(['subject_del' => static::DEL_NO, 'subject_pid' => 0])
            ->with(['children'=>
                function ($query) {
                    $query->where(['subject_del'=>static::STATUS_DEL_NO]);
                }
                ])
            ->get()
            ->toArray();
    }
}
