PatientAuxiliary.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\controller;
  4. use think\App;
  5. use think\facade\Request;
  6. use think\response\Json;
  7. use app\model\PatientAuxiliaryDiagnosis;
  8. use app\controller\Cdss;
  9. use think\facade\Db;
  10. /**
  11. * 患者辅助诊疗控制器
  12. * Class PatientAuxiliary
  13. * @package app\controller
  14. */
  15. class PatientAuxiliary extends CommonTwoController
  16. {
  17. /**
  18. * CDSS控制器实例
  19. * @var Cdss
  20. */
  21. protected $cdss;
  22. /**
  23. * 构造函数
  24. * @param App $app 应用实例
  25. */
  26. public function __construct(App $app)
  27. {
  28. parent::__construct($app);
  29. $this->cdss = new Cdss($app);
  30. }
  31. /**
  32. * 保存患者辅助诊疗信息
  33. * @return Json
  34. */
  35. public function savePatientAuxiliaryInfo(): Json
  36. {
  37. // 获取请求参数
  38. $params = $this->request->param();
  39. // 设置默认值
  40. $params = array_merge([
  41. 'ZYH' => '',
  42. 'ZSZZ' => '',
  43. 'name' => '',
  44. 'tag' => '',
  45. 'XB' => '',
  46. 'NL' => 0,
  47. 'document' => '',
  48. 'docter' => '',
  49. 'YGDM' => '',
  50. 'KSDM' => ''
  51. ], $params);
  52. // 参数验证
  53. $validate = validate([
  54. 'ZYH' => 'require|number', // 只验证住院号必填
  55. ]);
  56. if (!$validate->check($params)) {
  57. return $this->_json_error($validate->getError());
  58. }
  59. // 准备基础数据
  60. $baseData = [
  61. 'name' => $params['name'],
  62. 'tag' => $params['tag'],
  63. 'zszz' => $params['ZSZZ'],
  64. 'xb' => $params['XB'],
  65. 'nl' => intval($params['NL']),
  66. 'document' => $params['document'],
  67. 'docter' => $params['docter'],
  68. 'ygdm' => $params['YGDM'],
  69. 'ksdm' => $params['KSDM']
  70. ];
  71. try {
  72. // 1. 辅助诊疗 - 只要有zszz和性别年龄就调用
  73. if (!empty($params['ZSZZ']) && !empty($params['XB']) && !empty($params['NL'])) {
  74. try {
  75. // 从主诉症状中提取关键词
  76. $mainSymptoms = '';
  77. $symptomKeywords = Db::table('jm_cdss_symptom_keywords')
  78. ->column('keyword');
  79. foreach ($symptomKeywords as $keyword) {
  80. // 确保keyword是字符串类型
  81. $keyword = (string)$keyword;
  82. // 确保ZSZZ是字符串类型
  83. $zszz = (string)$params['ZSZZ'];
  84. if (stripos($zszz, $keyword) !== false) {
  85. if ($mainSymptoms === '') {
  86. $mainSymptoms = $keyword;
  87. } else {
  88. $mainSymptoms .= ',' . $keyword;
  89. }
  90. }
  91. }
  92. // 如果提取到了症状关键词,才调用辅助诊疗
  93. if (!empty($mainSymptoms)) {
  94. $auxiliaryResult = $this->cdss->getSymptomAboutDisease(
  95. $mainSymptoms, // 使用提取的症状关键词
  96. $params['NL'],
  97. $params['XB'] == '男' ? 2 : 1
  98. );
  99. if (!empty($auxiliaryResult)) {
  100. $data = array_merge($baseData, [
  101. 'content' => json_encode($auxiliaryResult, JSON_UNESCAPED_UNICODE)
  102. ]);
  103. PatientAuxiliaryDiagnosis::updateOrInsert(
  104. (string)$params['ZYH'],
  105. PatientAuxiliaryDiagnosis::TAG_AUXILIARY,
  106. $data
  107. );
  108. }
  109. }
  110. } catch (\Exception $e) {
  111. return $this->_json_error('辅助诊疗调用失败: ' . $e->getMessage());
  112. }
  113. }
  114. // 2. 知识库 - 需要name和tag
  115. if ($params['name'] && $params['tag']) {
  116. try {
  117. // 调用detail方法
  118. $response = $this->cdss->detail([
  119. 'name' => $params['name'],
  120. 'tag' => $params['tag']
  121. ]);
  122. // 从Json响应中获取数据
  123. $knowledgeResult = $response->getData();
  124. if (!empty($knowledgeResult)) {
  125. $data = array_merge($baseData, [
  126. 'content' => json_encode($knowledgeResult, JSON_UNESCAPED_UNICODE)
  127. ]);
  128. PatientAuxiliaryDiagnosis::updateOrInsert(
  129. (string)$params['ZYH'],
  130. PatientAuxiliaryDiagnosis::TAG_KNOWLEDGE,
  131. $data
  132. );
  133. }
  134. } catch (\Exception $e) {
  135. return $this->_json_error('知识库调用失败: ' . $e->getMessage());
  136. }
  137. }
  138. // 3. 医嘱审核 - 需要name和性别
  139. $hasReviewResult = false;
  140. if ($params['name'] && $params['XB']) {
  141. $gender = stripos($params['XB'], '男') !== false ? 1 : 2;
  142. try {
  143. $reviewResult = $this->cdss->yzEamine([
  144. 'name' => $params['name'],
  145. 'gender' => $gender
  146. ]);
  147. if (!empty($reviewResult)) {
  148. $data = array_merge($baseData, [
  149. 'content' => json_encode($reviewResult, JSON_UNESCAPED_UNICODE)
  150. ]);
  151. PatientAuxiliaryDiagnosis::updateOrInsert(
  152. (string)$params['ZYH'],
  153. PatientAuxiliaryDiagnosis::TAG_REVIEW,
  154. $data
  155. );
  156. $hasReviewResult = true;
  157. }
  158. } catch (\Exception $e) {
  159. return $this->_json_error('医嘱审核调用失败: ' . $e->getMessage());
  160. }
  161. }
  162. // 检查辅助诊疗结果
  163. $hasAuxiliaryResult = !empty($auxiliaryResult) && count($auxiliaryResult) > 0;
  164. // 检查知识库结果 - detail()返回的是Json对象
  165. $hasKnowledgeResult = !empty($knowledgeResult) && count($knowledgeResult) > 0;
  166. // 返回结果 - 任何一个匹配就设置req=1
  167. return $this->_json_succ(
  168. ['req' => ($hasAuxiliaryResult || $hasKnowledgeResult || $hasReviewResult) ? 1 : 0],
  169. 0,
  170. '保存成功'
  171. );
  172. } catch (\Exception $e) {
  173. return $this->_json_error('保存失败: ' . $e->getMessage());
  174. }
  175. }
  176. /**
  177. * 获取患者辅助诊疗信息
  178. * @return Json
  179. */
  180. public function getPatientAuxiliaryInfo(): Json
  181. {
  182. // 获取住院号
  183. $zyh = $this->request->param('zyh');
  184. if (!$zyh) {
  185. return $this->_json_error('住院号不能为空');
  186. }
  187. try {
  188. // 获取该住院号的所有记录
  189. $records = PatientAuxiliaryDiagnosis::where('zyh', $zyh)->select()->toArray();
  190. // 整理返回数据
  191. $result = [];
  192. foreach ($records as $record) {
  193. $result[] = [
  194. 'tag' => $record['tag'], // auxiliary/knowledge/review
  195. 'name' => $record['name'],
  196. 'content' => json_decode($record['content'], true), // 解码JSON字符串为数组
  197. 'xb' => $record['xb'],
  198. 'nl' => $record['nl'],
  199. 'document' => $record['document'],
  200. 'docter' => $record['docter'],
  201. 'ygdm' => $record['ygdm'],
  202. 'ksdm' => $record['ksdm'],
  203. 'create_time' => $record['create_time'],
  204. 'update_time' => $record['update_time']
  205. ];
  206. }
  207. return $this->_json_succ($result);
  208. } catch (\Exception $e) {
  209. return $this->_json_error('获取失败: ' . $e->getMessage());
  210. }
  211. }
  212. }