<?php
namespace app\controller;


use app\model\CdssDisease;
use app\model\VisitLimit;
use think\facade\Request;


/**
 * 西医疾病外部接口
 */
class XyZskOther extends CommonEsController
{

    /**
     * 2022/12/26
     * 西医疾病
     */
    public function getZskDetail()
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        $checkIsCanVisit = $this->checkIsCanVisit($ip);
        if( ! $checkIsCanVisit) {
            return $this->_json_error('非法请求！');
        }

        $params = Request::only(
            [
                'tag' , 'name'
            ]
        );

        $tag  = $params['tag']  ?? '';
        $name = $params['name'] ?? '';
        if( ! $tag || ! $name) {
            return $this->_json_error('请求参数有误！');
        }

        $tags = ['medicine' , 'disease'];
        if( ! in_array($tag, $tags)) {
            return $this->_json_error('请求标识错误！');
        }

        /**
         * 药品
         */
        if('medicine' === $tag) {
            $model = '\app\model\CdssMedicine';
            $field = 'name';
        }

        /**
         * 疾病
         */
        if('disease' === $tag) {
            $model = '\app\model\CdssDisease';
            $field = 'name';
        }

        $detail = $model::where($field , $name)->find();
        unset($detail['id']);

        return $this->_json_succ(
            $detail
        );
    }


    /**
     * 检测访问
     */
    protected function checkIsCanVisit($ip)
    {
        $visit = VisitLimit::where('ip' , $ip)->find();
        if( ! $visit ) return false;

        if(0 >= $visit->already_visit_count) {
            return false;
        }
        return true;
    }

}