<?php


namespace app\controller;

use app\model\LySymptom;
use app\model\LySymptomLog;
use app\model\RDepartment;
use think\facade\Db;
use think\facade\Request;

class Guidance extends CommonController
{
    /**
     * 获取年龄性别配置
     * @return \think\response\Json
     */
    public function getConfig()
    {
        $data = self::initConfig();
        return json($data);
    }
    public static function initConfig(){
        return [
            'gender'=>['男','女'],
            'age_range' =>[
                0=>'0～28天',
                1=>'29天～6岁',
                2=>'7岁～14岁',
                3=>'15岁～35岁',
                4=>'36岁～60岁',
                5=>'大于60岁',
            ]
        ];
    }

    /**
     * 获取症状
     * @return \think\response\Json
     */
    public function getSymptom(){
        $text = Request::post('text');
        $age = Request::post('age');
        $search = ['症状','异常','的','所有','经常','偶尔','症','不正常','感觉'];
        $replace = ['','','','','','','','','',''];
        $text = str_replace($search,$replace,$text);
        if (empty($text)){
            $data['type'] = 0;
            $data['symptom'] = [];
        }else{
            if ($age <= 1){
                $data['type'] = 0;
                $data['symptom'] = [];
            }else{
                $data['type'] = 1;
                $gender = Request::post('gender',0) + 1;
                $leng = mb_strlen($text);
                if ($leng >= 2){
                    $arr = [];
                    for ($i = 0; $i < $leng; $i+=2){
                        $a = mb_substr($text,$i,2);
                        if (mb_strlen($a) > 1){
                            $arr[] = $a;
                        }
                    }
                    $arr[] = $text;
                    $text = implode('|',$arr);
                }
                $symptom = LySymptom::where('gender','<=',$gender)
                    ->where('name|relevant','regexp',$text)
                    ->column('name');
                $data['symptom'] = [];
                foreach ($symptom as $item){
                    if (strpos($item,'儿') !== false){
                        continue;
                    }
                    if ($gender == 1 && strpos($item,'妇') !== false){
                        continue;
                    }
                    $data['symptom'][] = $item;
                }
            }
        }
        return json($data);
    }
    /**
     * 获取导诊科室
     * @return \think\response\Json
     */
    public function getGuidance(){
        $name = Request::post('name','');
        $text = Request::post('text');
        $gender = Request::post('gender');
        $age = Request::post('age');
        if ($age <= 1){
            $data['department'] = ['儿科'];
        }else{
            $result = LySymptom::where('name', 'like', "%$name%")->value('department');
            $result = array_unique(explode(' ', $result));
            $Department = ['内科', '外科', '儿科','妇产科'];
            if ($age != 5){
                $Department[] = '老年科';
            }
            $data['department'] = array_values(array_diff($result, $Department));
        }
        $config = self::initConfig();
        $department = RDepartment::whereIn('department_type',$data['department'])->field('department,parameter')->select()->toArray();
        $creat = [
            'text' => $text,
            'name' => $name,
            'age' => $config['age_range'][$age],
            'gender' => $config['gender'][$gender],
            'department' => implode(' ',$data['department'])
        ];
        $data['department'] = $department;
        $data['insert_id'] = Db::name('ly_symptom_log')->insertGetId($creat);
        return json($data);
    }

    /**
     * 评价
     * @return \think\response\Json
     */
    public function evaluate(){
        $id = Request::post('insert_id');
        $evaluate = Request::post('evaluate');
        if (LySymptomLog::update(['evaluate'=>$evaluate],['id'=>$id])){
            return json(['result'=>true]);
        }else{
            return json(['result'=>false]);
        }
    }
}
