<?php

namespace app\model;

use think\Model;


class CdssXyDisease extends Model
{
    protected $table = "jm_cdss_xy_disease";


    /**
     * 搜索器 disease(name)
     */
    public function searchNameAttr($query , $value , $data)
    {
        $query->where('name' , 'like' , '%'.$value.'%');
    }


    /**
     * 获取名称别名
     */
    public function getNameAndAlias($value)
    {
        $diseaseName = CdssXyDisease::field('name as disease_name, alias')->where('name' , $value)->find();
        if($diseaseName) {
            if($diseaseName->alias) {
                return $this->dealAlias($diseaseName->disease_name , $diseaseName->alias);
            } else {
                return [];
            }
        } else {
            $disease = CdssXyDisease::field('name as disease_name , alias')->where('alias' , 'like' , "%$value%")->find();
            if(empty($disease)) {
                return [];
            }

            $aliasArr = $disease->getAliasArr($disease->alias);
            if(is_array($aliasArr)) {
                if(in_array($value, $aliasArr)) {
                    return $this->dealAlias($disease->disease_name , $disease->alias);
                }
            } else {
                if($value == $aliasArr) {
                    return $this->dealAlias($disease->disease_name , $disease->alias);
                }
            }
            return [];
        }
    }


    protected function getAliasArr($alias)
    {
        if(stripos($alias , ';') !== false ) {
            return explode(';' , $alias);
        }
        if(stripos($alias , '；') !== false ) {
            return explode('；' , $alias);
        }
        return $alias;
    }



    protected function dealAlias($name , $alias)
    {
        if(stripos($alias , ';') !== false) {
            $newArray[] = $name;
            $aliasArr   = explode(';' , $alias);
            $newArray   = array_merge($newArray, $aliasArr);
        } elseif(stripos($alias , '；') !== false) {
            $newArray[] = $name;
            $aliasArr   = explode('；' , $alias);
            $newArray   = array_merge($newArray, $aliasArr);
        } else {
            $newArray[] = $name;
            $newArray[] = $alias;
        }
        return array_values(array_filter($newArray));
    }

}