PatientAuxiliary.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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_cdss_symptom_keywords')->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. 'is_web' => 1
  127. ]);
  128. // 从Json响应中获取数据
  129. $knowledgeResult = $response->getData();
  130. if (!empty($knowledgeResult)) {
  131. $data = array_merge($baseData, [
  132. 'content' => json_encode($knowledgeResult, JSON_UNESCAPED_UNICODE)
  133. ]);
  134. PatientAuxiliaryDiagnosis::updateOrInsert(
  135. (string)$params['ZYH'],
  136. PatientAuxiliaryDiagnosis::TAG_KNOWLEDGE,
  137. $data
  138. );
  139. }
  140. } catch (\Exception $e) {
  141. return $this->_json_error('知识库调用失败: ' . $e->getMessage());
  142. }
  143. }
  144. if((isset($medicineProduction) && isset($inspection)) && $medicineProduction || $inspection){
  145. try {
  146. if($medicineProduction){
  147. // 调用detail方法
  148. $medicineResult = $this->cdss->detail([
  149. 'name' => $medicineProduction,
  150. 'tag' => 'MedicineProduction',
  151. 'is_web' => 1
  152. ]);
  153. // 从Json响应中获取数据
  154. $medicine = $medicineResult->getData();
  155. $knowledgeResult = $medicine;
  156. }
  157. if($inspection){
  158. // 调用detail方法
  159. $inspectionResult = $this->cdss->detail([
  160. 'name' => $inspection,
  161. 'tag' => 'Inspection',
  162. ]);
  163. // 从Json响应中获取数据
  164. $inspections = $inspectionResult->getData();
  165. if(isset($knowledgeResult)){
  166. $knowledgeResult['data'] = array_merge($knowledgeResult['data'],$inspections['data']);
  167. }else {
  168. $knowledgeResult = $inspections;
  169. }
  170. }
  171. if (!empty($knowledgeResult)) {
  172. $data = array_merge($baseData, [
  173. 'content' => json_encode($knowledgeResult, JSON_UNESCAPED_UNICODE)
  174. ]);
  175. PatientAuxiliaryDiagnosis::updateOrInsert(
  176. (string)$params['ZYH'],
  177. PatientAuxiliaryDiagnosis::TAG_KNOWLEDGE,
  178. $data
  179. );
  180. }
  181. } catch (\Exception $e) {
  182. return $this->_json_error('知识库调用失败: ' . $e->getMessage());
  183. }
  184. }
  185. // 3. 医嘱审核 - 需要name和性别
  186. $hasReviewResult = false;
  187. if (((isset($inspection) && $inspection) || $params['name']) && $params['XB']) {
  188. $gender = stripos($params['XB'], '男') !== false ? 1 : 2;
  189. try {
  190. $reviewResult = $this->cdss->yzEamine([
  191. 'name' => $params['name'] ?: $inspection,
  192. 'gender' => $gender
  193. ]);
  194. if (!empty($reviewResult)) {
  195. $data = array_merge($baseData, [
  196. 'content' => json_encode($reviewResult, JSON_UNESCAPED_UNICODE)
  197. ]);
  198. PatientAuxiliaryDiagnosis::updateOrInsert(
  199. (string)$params['ZYH'],
  200. PatientAuxiliaryDiagnosis::TAG_REVIEW,
  201. $data
  202. );
  203. $hasReviewResult = true;
  204. }
  205. } catch (\Exception $e) {
  206. return $this->_json_error('医嘱审核调用失败: ' . $e->getMessage());
  207. }
  208. }
  209. // 检查辅助诊疗结果
  210. $hasAuxiliaryResult = !empty($auxiliaryResult) && count($auxiliaryResult) > 0;
  211. // 检查知识库结果 - detail()返回的是Json对象
  212. $hasKnowledgeResult = !empty($knowledgeResult) && count($knowledgeResult) > 0;
  213. // 返回结果 - 任何一个匹配就设置req=1
  214. return $this->_json_succ(
  215. [
  216. // 'req' => ($hasAuxiliaryResult || $hasKnowledgeResult || $hasReviewResult) ? 1 : 0
  217. 'req' => $hasReviewResult ? 1 : 0
  218. ],
  219. 0,
  220. '保存成功'
  221. );
  222. } catch (\Exception $e) {
  223. return $this->_json_error('保存失败: ' . $e->getMessage());
  224. }
  225. }
  226. /**
  227. * 获取患者辅助诊疗信息
  228. * @return Json
  229. */
  230. public function getPatientAuxiliaryInfo(): Json
  231. {
  232. // 获取住院号
  233. $zyh = $this->request->param('zyh');
  234. \think\facade\Log::info(__FUNCTION__.': zyh='.$zyh);
  235. if (!$zyh) {
  236. return $this->_json_error('住院号不能为空');
  237. }
  238. try {
  239. // 获取该住院号的所有记录
  240. $records = PatientAuxiliaryDiagnosis::where('zyh', $zyh)->select()->toArray();
  241. // 整理返回数据
  242. $result = [];
  243. foreach ($records as $record) {
  244. $result[] = [
  245. 'tag' => $record['tag'], // auxiliary/knowledge/review
  246. 'name' => $record['name'],
  247. 'ZSZZ' => $record['ZSZZ'],
  248. 'content' => json_decode($record['content'], true), // 解码JSON字符串为数组
  249. 'xb' => $record['xb'],
  250. 'nl' => $record['nl'],
  251. 'document' => $record['document'],
  252. 'docter' => $record['docter'],
  253. 'ygdm' => $record['ygdm'],
  254. 'ksdm' => $record['ksdm'],
  255. 'create_time' => $record['create_time'],
  256. 'update_time' => $record['update_time']
  257. ];
  258. }
  259. return $this->_json_succ($result);
  260. } catch (\Exception $e) {
  261. return $this->_json_error('获取失败: ' . $e->getMessage());
  262. }
  263. }
  264. /**
  265. * 保存患者辅助诊疗信息
  266. * @return Json
  267. */
  268. public function savePatientAuxiliaryInfoInterfece($params): Json
  269. {
  270. // 设置默认值
  271. $params = array_merge([
  272. 'ZYH' => '',
  273. 'ZSZZ' => '',
  274. 'name' => '',
  275. 'tag' => '',
  276. 'XB' => '',
  277. 'NL' => 0,
  278. 'document' => '',
  279. 'docter' => '',
  280. 'YGDM' => '',
  281. 'KSDM' => ''
  282. ], $params);
  283. // 参数验证
  284. $validate = validate([
  285. 'ZYH' => 'require|number', // 只验证住院号必填
  286. ]);
  287. if (!$validate->check($params)) {
  288. return $this->_json_error($validate->getError());
  289. }
  290. // 准备基础数据
  291. $baseData = [
  292. 'name' => $params['name'],
  293. 'tag' => $params['tag'],
  294. 'zszz' => $params['ZSZZ'],
  295. 'xb' => $params['XB'],
  296. 'nl' => intval($params['NL']),
  297. 'document' => $params['document'],
  298. 'docter' => $params['docter'],
  299. 'ygdm' => $params['YGDM'],
  300. 'ksdm' => $params['KSDM']
  301. ];
  302. try {
  303. // 1. 辅助诊疗 - 只要有zszz和性别年龄就调用
  304. if (!empty($params['ZSZZ']) && !empty($params['XB']) && !empty($params['NL'])) {
  305. try {
  306. // 从主诉症状中提取关键词
  307. $dictionary = SymptomDictionary::field('symptom')->whereRaw("'".$params['ZSZZ']."'".' LIKE CONCAT("%", symptom, "%")')->select()->toArray();
  308. // $dictionary = SymptomDictionary::field('keyword')->whereRaw("'".$params['ZSZZ']."'".' LIKE CONCAT("%", keyword, "%")')->select()->toArray();
  309. $mainSymptoms = $dictionary ? implode(',',array_column($dictionary,'symptom')) : '';
  310. // 如果提取到了症状关键词,才调用辅助诊疗
  311. if (!empty($mainSymptoms)) {
  312. $auxiliaryResult = $this->cdss->getSymptomAboutDisease(
  313. $mainSymptoms, // 使用提取的症状关键词
  314. $params['NL'],
  315. stripos($params['XB'], '男') !== false ? 1 : 2
  316. );
  317. if (!empty($auxiliaryResult)) {
  318. $data = array_merge($baseData, [
  319. 'content' => json_encode($auxiliaryResult, JSON_UNESCAPED_UNICODE)
  320. ]);
  321. PatientAuxiliaryDiagnosis::updateOrInsert(
  322. (string)$params['ZYH'],
  323. PatientAuxiliaryDiagnosis::TAG_AUXILIARY,
  324. $data
  325. );
  326. }
  327. }
  328. } catch (\Exception $e) {
  329. return $this->_json_error('辅助诊疗调用失败: ' . $e->getMessage());
  330. }
  331. }
  332. // 2. 知识库 - 需要name和tag
  333. if ($params['name'] && $params['tag']) {
  334. try {
  335. // 调用detail方法
  336. $response = $this->cdss->detail([
  337. 'name' => $params['name'],
  338. 'tag' => $params['tag']
  339. ]);
  340. // 从Json响应中获取数据
  341. $knowledgeResult = $response->getData();
  342. if (!empty($knowledgeResult)) {
  343. $data = array_merge($baseData, [
  344. 'content' => json_encode($knowledgeResult, JSON_UNESCAPED_UNICODE)
  345. ]);
  346. PatientAuxiliaryDiagnosis::updateOrInsert(
  347. (string)$params['ZYH'],
  348. PatientAuxiliaryDiagnosis::TAG_KNOWLEDGE,
  349. $data
  350. );
  351. }
  352. } catch (\Exception $e) {
  353. return $this->_json_error('知识库调用失败: ' . $e->getMessage());
  354. }
  355. }
  356. // 3. 医嘱审核 - 需要name和性别
  357. $hasReviewResult = false;
  358. if ($params['name'] && $params['XB']) {
  359. $gender = stripos($params['XB'], '男') !== false ? 1 : 2;
  360. try {
  361. $reviewResult = $this->cdss->yzEamine([
  362. 'name' => $params['name'],
  363. 'gender' => $gender
  364. ]);
  365. if (!empty($reviewResult)) {
  366. $data = array_merge($baseData, [
  367. 'content' => json_encode($reviewResult, JSON_UNESCAPED_UNICODE)
  368. ]);
  369. PatientAuxiliaryDiagnosis::updateOrInsert(
  370. (string)$params['ZYH'],
  371. PatientAuxiliaryDiagnosis::TAG_REVIEW,
  372. $data
  373. );
  374. $hasReviewResult = true;
  375. }
  376. } catch (\Exception $e) {
  377. return $this->_json_error('医嘱审核调用失败: ' . $e->getMessage());
  378. }
  379. }
  380. // 检查辅助诊疗结果
  381. $hasAuxiliaryResult = !empty($auxiliaryResult) && count($auxiliaryResult) > 0;
  382. // 检查知识库结果 - detail()返回的是Json对象
  383. $hasKnowledgeResult = !empty($knowledgeResult) && count($knowledgeResult) > 0;
  384. // 返回结果 - 任何一个匹配就设置req=1
  385. return $this->_json_succ(
  386. ['req' => ($hasAuxiliaryResult || $hasKnowledgeResult || $hasReviewResult) ? 1 : 0],
  387. 0,
  388. '保存成功'
  389. );
  390. } catch (\Exception $e) {
  391. return $this->_json_error('保存失败: ' . $e->getMessage());
  392. }
  393. }
  394. }