PatientAuxiliary.php 17 KB

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