123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- namespace app\controller;
- use app\model\diseasequestionanswer\Question;
- use app\model\CdssXyDisease;
- use app\model\diseasequestionanswer\QuestionDetail;
- use app\model\diseasequestionanswer\QuestionHistory;
- use think\facade\Db;
- use think\facade\Request;
- use Fukuball\Jieba\Jieba;
- use Fukuball\Jieba\Finalseg;
- use think\response\Json;
- require_once root_path().'vendor/fukuball/jieba-php/src/vendor/multi-array/MultiArray.php';
- require_once root_path().'vendor/fukuball/jieba-php/src/vendor/multi-array/Factory/MultiArrayFactory.php';
- require_once root_path().'vendor/fukuball/jieba-php/src/class/Jieba.php';
- require_once root_path().'vendor/fukuball/jieba-php/src/class/Finalseg.php';
- Jieba::init();
- Finalseg::init();
- /**
- * 智能问答
- */
- class QuestionAnswer extends CommonTwoController
- {
- /**
- * @param $question string 问题
- *
- * @return Json
- */
- public function index()
- {
- ini_set('memry_limit','1500M');
- $question = Request::param('question') ?? '';
-
- /**
- * 分词
- */
- $stringArr = Jieba::cut($question);
-
- /**
- * 疾病
- */
- $disease = '';
- foreach ($stringArr as $vv) {
- $result = CdssXyDisease::where('name' , $vv)->find();
- if($result) {
- $disease = $vv;
- }
- }
- /**
- * 检测不到疾病时
- */
- if( ! $disease) {
- $id = Request::param('id') ?? '';
- $val = Request::param('value') ?? '';
- $current_field = Request::param('current_field') ?? 'pathogenesis';
- $selectField = Request::param('select_field') ?? 'pathogenesis';
- return $this->_json_succ(
- $this->questionDetail($id , $question , $val , $current_field , $selectField)
- );
- }
- if($disease) {
- $field = Request::param('field') ?? '';
- $comment = Request::param('comment') ?? '';
- $table = "jm_cdss_xy_disease";
- $sql = "show full columns from {$table}";
- $res = Db::query($sql);
- /**
- * 字段
- */
- foreach ($res as $key => $value) {
- if('id' === $value['Field']) {
- unset($res[$key]);
- }
- if('department_1' === $value['Field']) {
- unset($res[$key]);
- }
- if('department_2' === $value['Field']) {
- unset($res[$key]);
- }
- if('alias' === $value['Field']) {
- unset($res[$key]);
- }
- foreach ($stringArr as $v) {
- if($value['Comment'] === $v) {
- $field = $value['Field'];
- }
- }
- }
- if($disease && ! $field) {
- $newArray = array_combine(array_column($res , 'Field') , array_column($res , 'Comment'));
- foreach ($newArray as $kkk => $vvv) {
- $new[] = [
- 'field_name' => $kkk ,
- 'filed_comment' => $vvv
- ];
- }
- if($new) {
- QuestionHistory::questionAnswerRecord($question , array_column($new , 'filed_comment'));
- }
- return $this->_json_succ(
- [
- 'question' => $question,
- 'answer' => '请选择提问属性!' ,
- 'button_list' => $new ,
- 'type' => 'disease'
- ]
- );
- }
- if($disease && $field) {
- $answer = CdssXyDisease::where('name' , $disease)->value($field) ?? '';
- if($answer) {
- return $this->_json_succ(
- [
- 'question' => $question,
- 'answer' => $answer ,
- 'button_list' => [],
- 'type' => 'disease'
- ]
- );
- }
- QuestionHistory::questionAnswerRecord($question , $answer);
- return $this->_json_succ(
- [
- 'question' => $question.$comment,
- 'type' => ''
- ]
- );
- }
- }
- }
- protected function getAnswerByChatGpt($question)
- {
- $url = "http://18.221.12.198:5001/chatgpt?content=".$question;
- $result = file_get_contents($url);
- return [
- 'type' => 'chatgpt' ,
- 'result' => $result
- ];
- }
- /**
- * 问题
- */
- public function questionList()
- {
- ini_set('memry_limit','1500M');
- $page = Request::param('page') ?? 1;
- $pagesize = 30;
- $limit = ($page - 1) * $pagesize;
- $questionList = Question::field('id , question')->where('status' , 1)->limit($limit , $pagesize)->select()->toArray();
- return $this->_json_succ(
- [
- 'question_list' => $questionList ,
- ]
- );
- }
- /**
- * 问题详情
- */
- protected function questionDetail($id , $question = '' , $val = '' , $current_field = '' , $selectField = '')
- {
- if( ! $val) {
- $result = QuestionDetail::field($selectField)->where(
- [
- 'question_id' => $id ,
- 'status' => 1
- ]
- )->select()->toArray();
- $result = array_values(array_filter(array_column($result , $selectField)));
- } else {
- $result = QuestionDetail::field($selectField)->where(
- [
- 'question_id' => $id ,
- 'status' => 1,
- $current_field => $val
- ]
- )->find();
- }
- $fields = $this->getTabelFieldsAndComments('jm_question_detail');
- $key = $this->getNextFieldKey($selectField);
- /**
- * 查询不到的情况前端调用chatGpt
- */
- if( ! $selectField || ! $result || 'product_name' === $current_field) {
- return [
- 'question' => $question,
- 'type' => 'chatgpt'
- ];
- }
- QuestionHistory::questionAnswerRecord($question , $result);
- return [
- 'current_field' => $selectField,
- 'comment' => $this->getNextFieldKey($selectField , 'comment'),
- 'result' => $result,
- 'next_field_info' => $fields[$key+1] ?? '',
- 'type' => 'stage',
- 'id' => $id
- ];
- }
- protected function getNextFieldKey($selectField , $type = 'key')
- {
- $fields = $this->getTabelFieldsAndComments('jm_question_detail');
- foreach ($fields as $key => $value) {
- if($selectField === $value['field_name']) {
- if('key' === $type) {
- return $key ?? '';
- }
- if('comment' === $type) {
- return $value['filed_comment'] ?? '';
- }
- }
- }
- }
- /**
- * 数据表字段结构
- */
- protected function getTabelFieldsAndComments($table)
- {
- $sql = "show full columns from {$table}";
- $res = Db::query($sql);
- $newArray = array_combine(array_column($res , 'Field') , array_column($res , 'Comment'));
- foreach ($newArray as $kkk => $vvv) {
- if(in_array($kkk , ['id' , 'question_id' , 'status' , 'createtime' , 'updatetime'])) {
- unset($newArray[$kkk]);
- }
- }
- foreach ($newArray as $k => $v) {
- $new[] = [
- 'field_name' => $k ,
- 'filed_comment' => $v
- ];
- }
- return $new;
- }
- }
|