PatientAuxiliary.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\controller;
  4. use app\model\SymptomDictionary;
  5. use think\App;
  6. use think\facade\Request;
  7. use think\helper\Arr;
  8. use think\response\Json;
  9. use app\model\PatientAuxiliaryDiagnosis;
  10. use app\controller\Cdss;
  11. use think\facade\Db;
  12. /**
  13. * 患者辅助诊疗控制器
  14. * Class PatientAuxiliary
  15. * @package app\controller
  16. */
  17. class PatientAuxiliary extends CommonTwoController
  18. {
  19. /**
  20. * CDSS控制器实例
  21. * @var Cdss
  22. */
  23. protected $cdss;
  24. /**
  25. * 构造函数
  26. * @param App $app 应用实例
  27. */
  28. public function __construct(App $app)
  29. {
  30. parent::__construct($app);
  31. $this->cdss = new Cdss($app);
  32. }
  33. /**
  34. * 保存患者辅助诊疗信息
  35. * @return Json
  36. */
  37. public function savePatientAuxiliaryInfo(): Json
  38. {
  39. // 获取请求参数
  40. $params = $this->request->param();
  41. // 设置默认值
  42. $params = array_merge([
  43. 'ZYH' => '',
  44. 'ZSZZ' => '',
  45. 'name' => '',
  46. 'tag' => '',
  47. 'XB' => '',
  48. 'NL' => 0,
  49. 'document' => '',
  50. 'docter' => '',
  51. 'YGDM' => '',
  52. 'KSDM' => ''
  53. ], $params);
  54. // 参数验证
  55. $validate = validate([
  56. 'ZYH' => 'require|number', // 只验证住院号必填
  57. ]);
  58. if (!$validate->check($params)) {
  59. return $this->_json_error($validate->getError());
  60. }
  61. // 准备基础数据
  62. $baseData = [
  63. 'name' => $params['name'],
  64. 'tag' => $params['tag'],
  65. 'zszz' => $params['ZSZZ'],
  66. 'xb' => $params['XB'],
  67. 'nl' => intval($params['NL']),
  68. 'document' => $params['document'],
  69. 'docter' => $params['docter'],
  70. 'ygdm' => $params['YGDM'],
  71. 'ksdm' => $params['KSDM']
  72. ];
  73. try {
  74. // 1. 辅助诊疗 - 只要有zszz和性别年龄就调用
  75. if (!empty($params['ZSZZ']) && !empty($params['XB']) && !empty($params['NL'])) {
  76. try {
  77. // 从主诉症状中提取关键词
  78. $dictionary = SymptomDictionary::field('symptom')->whereRaw("'".$params['ZSZZ']."'".' LIKE CONCAT("%", symptom, "%")')->select()->toArray();
  79. $mainSymptoms = $dictionary ? implode(',',array_column($dictionary,'symptom')) : '';
  80. // 如果提取到了症状关键词,才调用辅助诊疗
  81. if (!empty($mainSymptoms)) {
  82. $auxiliaryResult = $this->cdss->getSymptomAboutDisease(
  83. $mainSymptoms, // 使用提取的症状关键词
  84. $params['NL'],
  85. $params['XB'] == '男' ? 2 : 1
  86. );
  87. if (!empty($auxiliaryResult)) {
  88. $data = array_merge($baseData, [
  89. 'content' => json_encode($auxiliaryResult, JSON_UNESCAPED_UNICODE)
  90. ]);
  91. PatientAuxiliaryDiagnosis::updateOrInsert(
  92. (string)$params['ZYH'],
  93. PatientAuxiliaryDiagnosis::TAG_AUXILIARY,
  94. $data
  95. );
  96. }
  97. }
  98. } catch (\Exception $e) {
  99. return $this->_json_error('辅助诊疗调用失败: ' . $e->getMessage());
  100. }
  101. }
  102. // 2. 知识库 - 需要name和tag
  103. if ($params['name'] && $params['tag']) {
  104. try {
  105. // 调用detail方法
  106. $response = $this->cdss->detail([
  107. 'name' => $params['name'],
  108. 'tag' => $params['tag']
  109. ]);
  110. // 从Json响应中获取数据
  111. $knowledgeResult = $response->getData();
  112. if (!empty($knowledgeResult)) {
  113. $data = array_merge($baseData, [
  114. 'content' => json_encode($knowledgeResult, JSON_UNESCAPED_UNICODE)
  115. ]);
  116. PatientAuxiliaryDiagnosis::updateOrInsert(
  117. (string)$params['ZYH'],
  118. PatientAuxiliaryDiagnosis::TAG_KNOWLEDGE,
  119. $data
  120. );
  121. }
  122. } catch (\Exception $e) {
  123. return $this->_json_error('知识库调用失败: ' . $e->getMessage());
  124. }
  125. }
  126. // 3. 医嘱审核 - 需要name和性别
  127. $hasReviewResult = false;
  128. if ($params['name'] && $params['XB']) {
  129. $gender = stripos($params['XB'], '男') !== false ? 1 : 2;
  130. try {
  131. $reviewResult = $this->cdss->yzEamine([
  132. 'name' => $params['name'],
  133. 'gender' => $gender
  134. ]);
  135. if (!empty($reviewResult)) {
  136. $data = array_merge($baseData, [
  137. 'content' => json_encode($reviewResult, JSON_UNESCAPED_UNICODE)
  138. ]);
  139. PatientAuxiliaryDiagnosis::updateOrInsert(
  140. (string)$params['ZYH'],
  141. PatientAuxiliaryDiagnosis::TAG_REVIEW,
  142. $data
  143. );
  144. $hasReviewResult = true;
  145. }
  146. } catch (\Exception $e) {
  147. return $this->_json_error('医嘱审核调用失败: ' . $e->getMessage());
  148. }
  149. }
  150. // 检查辅助诊疗结果
  151. $hasAuxiliaryResult = !empty($auxiliaryResult) && count($auxiliaryResult) > 0;
  152. // 检查知识库结果 - detail()返回的是Json对象
  153. $hasKnowledgeResult = !empty($knowledgeResult) && count($knowledgeResult) > 0;
  154. // 返回结果 - 任何一个匹配就设置req=1
  155. return $this->_json_succ(
  156. ['req' => ($hasAuxiliaryResult || $hasKnowledgeResult || $hasReviewResult) ? 1 : 0],
  157. 0,
  158. '保存成功'
  159. );
  160. } catch (\Exception $e) {
  161. return $this->_json_error('保存失败: ' . $e->getMessage());
  162. }
  163. }
  164. /**
  165. * 获取患者辅助诊疗信息
  166. * @return Json
  167. */
  168. public function getPatientAuxiliaryInfo(): Json
  169. {
  170. // 获取住院号
  171. $zyh = $this->request->param('zyh');
  172. \think\facade\Log::info(__FUNCTION__.': zyh='.$zyh);
  173. if (!$zyh) {
  174. return $this->_json_error('住院号不能为空');
  175. }
  176. try {
  177. // 获取该住院号的所有记录
  178. $records = PatientAuxiliaryDiagnosis::where('zyh', $zyh)->select()->toArray();
  179. // 整理返回数据
  180. $result = [];
  181. foreach ($records as $record) {
  182. $result[] = [
  183. 'tag' => $record['tag'], // auxiliary/knowledge/review
  184. 'name' => $record['name'],
  185. 'content' => json_decode($record['content'], true), // 解码JSON字符串为数组
  186. 'xb' => $record['xb'],
  187. 'nl' => $record['nl'],
  188. 'document' => $record['document'],
  189. 'docter' => $record['docter'],
  190. 'ygdm' => $record['ygdm'],
  191. 'ksdm' => $record['ksdm'],
  192. 'create_time' => $record['create_time'],
  193. 'update_time' => $record['update_time']
  194. ];
  195. }
  196. return $this->_json_succ($result);
  197. } catch (\Exception $e) {
  198. return $this->_json_error('获取失败: ' . $e->getMessage());
  199. }
  200. }
  201. /**
  202. * 保存患者辅助诊疗信息
  203. * @return Json
  204. */
  205. public function savePatientAuxiliaryInfoInterfece($params): Json
  206. {
  207. // 设置默认值
  208. $params = array_merge([
  209. 'ZYH' => '',
  210. 'ZSZZ' => '',
  211. 'name' => '',
  212. 'tag' => '',
  213. 'XB' => '',
  214. 'NL' => 0,
  215. 'document' => '',
  216. 'docter' => '',
  217. 'YGDM' => '',
  218. 'KSDM' => ''
  219. ], $params);
  220. // 参数验证
  221. $validate = validate([
  222. 'ZYH' => 'require|number', // 只验证住院号必填
  223. ]);
  224. if (!$validate->check($params)) {
  225. return $this->_json_error($validate->getError());
  226. }
  227. // 准备基础数据
  228. $baseData = [
  229. 'name' => $params['name'],
  230. 'tag' => $params['tag'],
  231. 'zszz' => $params['ZSZZ'],
  232. 'xb' => $params['XB'],
  233. 'nl' => intval($params['NL']),
  234. 'document' => $params['document'],
  235. 'docter' => $params['docter'],
  236. 'ygdm' => $params['YGDM'],
  237. 'ksdm' => $params['KSDM']
  238. ];
  239. try {
  240. // 1. 辅助诊疗 - 只要有zszz和性别年龄就调用
  241. if (!empty($params['ZSZZ']) && !empty($params['XB']) && !empty($params['NL'])) {
  242. try {
  243. // 从主诉症状中提取关键词
  244. $dictionary = SymptomDictionary::field('symptom')->whereRaw("'".$params['ZSZZ']."'".' LIKE CONCAT("%", symptom, "%")')->select()->toArray();
  245. $mainSymptoms = $dictionary ? implode(',',array_column($dictionary,'symptom')) : '';
  246. // 如果提取到了症状关键词,才调用辅助诊疗
  247. if (!empty($mainSymptoms)) {
  248. $auxiliaryResult = $this->cdss->getSymptomAboutDisease(
  249. $mainSymptoms, // 使用提取的症状关键词
  250. $params['NL'],
  251. stripos($params['XB'], '男') !== false ? 1 : 2
  252. );
  253. if (!empty($auxiliaryResult)) {
  254. $data = array_merge($baseData, [
  255. 'content' => json_encode($auxiliaryResult, JSON_UNESCAPED_UNICODE)
  256. ]);
  257. PatientAuxiliaryDiagnosis::updateOrInsert(
  258. (string)$params['ZYH'],
  259. PatientAuxiliaryDiagnosis::TAG_AUXILIARY,
  260. $data
  261. );
  262. }
  263. }
  264. } catch (\Exception $e) {
  265. return $this->_json_error('辅助诊疗调用失败: ' . $e->getMessage());
  266. }
  267. }
  268. // 2. 知识库 - 需要name和tag
  269. if ($params['name'] && $params['tag']) {
  270. try {
  271. // 调用detail方法
  272. $response = $this->cdss->detail([
  273. 'name' => $params['name'],
  274. 'tag' => $params['tag']
  275. ]);
  276. // 从Json响应中获取数据
  277. $knowledgeResult = $response->getData();
  278. if (!empty($knowledgeResult)) {
  279. $data = array_merge($baseData, [
  280. 'content' => json_encode($knowledgeResult, JSON_UNESCAPED_UNICODE)
  281. ]);
  282. PatientAuxiliaryDiagnosis::updateOrInsert(
  283. (string)$params['ZYH'],
  284. PatientAuxiliaryDiagnosis::TAG_KNOWLEDGE,
  285. $data
  286. );
  287. }
  288. } catch (\Exception $e) {
  289. return $this->_json_error('知识库调用失败: ' . $e->getMessage());
  290. }
  291. }
  292. // 3. 医嘱审核 - 需要name和性别
  293. $hasReviewResult = false;
  294. if ($params['name'] && $params['XB']) {
  295. $gender = stripos($params['XB'], '男') !== false ? 1 : 2;
  296. try {
  297. $reviewResult = $this->cdss->yzEamine([
  298. 'name' => $params['name'],
  299. 'gender' => $gender
  300. ]);
  301. if (!empty($reviewResult)) {
  302. $data = array_merge($baseData, [
  303. 'content' => json_encode($reviewResult, JSON_UNESCAPED_UNICODE)
  304. ]);
  305. PatientAuxiliaryDiagnosis::updateOrInsert(
  306. (string)$params['ZYH'],
  307. PatientAuxiliaryDiagnosis::TAG_REVIEW,
  308. $data
  309. );
  310. $hasReviewResult = true;
  311. }
  312. } catch (\Exception $e) {
  313. return $this->_json_error('医嘱审核调用失败: ' . $e->getMessage());
  314. }
  315. }
  316. // 检查辅助诊疗结果
  317. $hasAuxiliaryResult = !empty($auxiliaryResult) && count($auxiliaryResult) > 0;
  318. // 检查知识库结果 - detail()返回的是Json对象
  319. $hasKnowledgeResult = !empty($knowledgeResult) && count($knowledgeResult) > 0;
  320. // 返回结果 - 任何一个匹配就设置req=1
  321. return $this->_json_succ(
  322. ['req' => ($hasAuxiliaryResult || $hasKnowledgeResult || $hasReviewResult) ? 1 : 0],
  323. 0,
  324. '保存成功'
  325. );
  326. } catch (\Exception $e) {
  327. return $this->_json_error('保存失败: ' . $e->getMessage());
  328. }
  329. }
  330. }