QuestionAnswer.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace app\controller;
  3. use app\model\diseasequestionanswer\Question;
  4. use app\model\CdssXyDisease;
  5. use app\model\diseasequestionanswer\QuestionDetail;
  6. use app\model\diseasequestionanswer\QuestionHistory;
  7. use think\facade\Db;
  8. use think\facade\Request;
  9. use Fukuball\Jieba\Jieba;
  10. use Fukuball\Jieba\Finalseg;
  11. use think\response\Json;
  12. require_once root_path().'vendor/fukuball/jieba-php/src/vendor/multi-array/MultiArray.php';
  13. require_once root_path().'vendor/fukuball/jieba-php/src/vendor/multi-array/Factory/MultiArrayFactory.php';
  14. require_once root_path().'vendor/fukuball/jieba-php/src/class/Jieba.php';
  15. require_once root_path().'vendor/fukuball/jieba-php/src/class/Finalseg.php';
  16. Jieba::init();
  17. Finalseg::init();
  18. /**
  19. * 智能问答
  20. */
  21. class QuestionAnswer extends CommonTwoController
  22. {
  23. /**
  24. * @param $question string 问题
  25. *
  26. * @return Json
  27. */
  28. public function index()
  29. {
  30. ini_set('memry_limit','1500M');
  31. $question = Request::param('question') ?? '';
  32. /**
  33. * 分词
  34. */
  35. $stringArr = Jieba::cut($question);
  36. /**
  37. * 疾病
  38. */
  39. $disease = '';
  40. foreach ($stringArr as $vv) {
  41. $result = CdssXyDisease::where('name' , $vv)->find();
  42. if($result) {
  43. $disease = $vv;
  44. }
  45. }
  46. /**
  47. * 检测不到疾病时
  48. */
  49. if( ! $disease) {
  50. $id = Request::param('id') ?? '';
  51. $val = Request::param('value') ?? '';
  52. $current_field = Request::param('current_field') ?? 'pathogenesis';
  53. $selectField = Request::param('select_field') ?? 'pathogenesis';
  54. return $this->_json_succ(
  55. $this->questionDetail($id , $question , $val , $current_field , $selectField)
  56. );
  57. }
  58. if($disease) {
  59. $field = Request::param('field') ?? '';
  60. $comment = Request::param('comment') ?? '';
  61. $table = "jm_cdss_xy_disease";
  62. $sql = "show full columns from {$table}";
  63. $res = Db::query($sql);
  64. /**
  65. * 字段
  66. */
  67. foreach ($res as $key => $value) {
  68. if('id' === $value['Field']) {
  69. unset($res[$key]);
  70. }
  71. if('department_1' === $value['Field']) {
  72. unset($res[$key]);
  73. }
  74. if('department_2' === $value['Field']) {
  75. unset($res[$key]);
  76. }
  77. if('alias' === $value['Field']) {
  78. unset($res[$key]);
  79. }
  80. foreach ($stringArr as $v) {
  81. if($value['Comment'] === $v) {
  82. $field = $value['Field'];
  83. }
  84. }
  85. }
  86. if($disease && ! $field) {
  87. $newArray = array_combine(array_column($res , 'Field') , array_column($res , 'Comment'));
  88. foreach ($newArray as $kkk => $vvv) {
  89. $new[] = [
  90. 'field_name' => $kkk ,
  91. 'filed_comment' => $vvv
  92. ];
  93. }
  94. if($new) {
  95. QuestionHistory::questionAnswerRecord($question , array_column($new , 'filed_comment'));
  96. }
  97. return $this->_json_succ(
  98. [
  99. 'question' => $question,
  100. 'answer' => '请选择提问属性!' ,
  101. 'button_list' => $new ,
  102. 'type' => 'disease'
  103. ]
  104. );
  105. }
  106. if($disease && $field) {
  107. $answer = CdssXyDisease::where('name' , $disease)->value($field) ?? '';
  108. if($answer) {
  109. return $this->_json_succ(
  110. [
  111. 'question' => $question,
  112. 'answer' => $answer ,
  113. 'button_list' => [],
  114. 'type' => 'disease'
  115. ]
  116. );
  117. }
  118. QuestionHistory::questionAnswerRecord($question , $answer);
  119. return $this->_json_succ(
  120. [
  121. 'question' => $question.$comment,
  122. 'type' => ''
  123. ]
  124. );
  125. }
  126. }
  127. }
  128. protected function getAnswerByChatGpt($question)
  129. {
  130. $url = "http://18.221.12.198:5001/chatgpt?content=".$question;
  131. $result = file_get_contents($url);
  132. return [
  133. 'type' => 'chatgpt' ,
  134. 'result' => $result
  135. ];
  136. }
  137. /**
  138. * 问题
  139. */
  140. public function questionList()
  141. {
  142. ini_set('memry_limit','1500M');
  143. $page = Request::param('page') ?? 1;
  144. $pagesize = 30;
  145. $limit = ($page - 1) * $pagesize;
  146. $questionList = Question::field('id , question')->where('status' , 1)->limit($limit , $pagesize)->select()->toArray();
  147. return $this->_json_succ(
  148. [
  149. 'question_list' => $questionList ,
  150. ]
  151. );
  152. }
  153. /**
  154. * 问题详情
  155. */
  156. protected function questionDetail($id , $question = '' , $val = '' , $current_field = '' , $selectField = '')
  157. {
  158. if( ! $val) {
  159. $result = QuestionDetail::field($selectField)->where(
  160. [
  161. 'question_id' => $id ,
  162. 'status' => 1
  163. ]
  164. )->select()->toArray();
  165. $result = array_values(array_filter(array_column($result , $selectField)));
  166. } else {
  167. $result = QuestionDetail::field($selectField)->where(
  168. [
  169. 'question_id' => $id ,
  170. 'status' => 1,
  171. $current_field => $val
  172. ]
  173. )->find();
  174. }
  175. $fields = $this->getTabelFieldsAndComments('jm_question_detail');
  176. $key = $this->getNextFieldKey($selectField);
  177. /**
  178. * 查询不到的情况前端调用chatGpt
  179. */
  180. if( ! $selectField || ! $result || 'product_name' === $current_field) {
  181. return [
  182. 'question' => $question,
  183. 'type' => 'chatgpt'
  184. ];
  185. }
  186. QuestionHistory::questionAnswerRecord($question , $result);
  187. return [
  188. 'current_field' => $selectField,
  189. 'comment' => $this->getNextFieldKey($selectField , 'comment'),
  190. 'result' => $result,
  191. 'next_field_info' => $fields[$key+1] ?? '',
  192. 'type' => 'stage',
  193. 'id' => $id
  194. ];
  195. }
  196. protected function getNextFieldKey($selectField , $type = 'key')
  197. {
  198. $fields = $this->getTabelFieldsAndComments('jm_question_detail');
  199. foreach ($fields as $key => $value) {
  200. if($selectField === $value['field_name']) {
  201. if('key' === $type) {
  202. return $key ?? '';
  203. }
  204. if('comment' === $type) {
  205. return $value['filed_comment'] ?? '';
  206. }
  207. }
  208. }
  209. }
  210. /**
  211. * 数据表字段结构
  212. */
  213. protected function getTabelFieldsAndComments($table)
  214. {
  215. $sql = "show full columns from {$table}";
  216. $res = Db::query($sql);
  217. $newArray = array_combine(array_column($res , 'Field') , array_column($res , 'Comment'));
  218. foreach ($newArray as $kkk => $vvv) {
  219. if(in_array($kkk , ['id' , 'question_id' , 'status' , 'createtime' , 'updatetime'])) {
  220. unset($newArray[$kkk]);
  221. }
  222. }
  223. foreach ($newArray as $k => $v) {
  224. $new[] = [
  225. 'field_name' => $k ,
  226. 'filed_comment' => $v
  227. ];
  228. }
  229. return $new;
  230. }
  231. }