PatientAuxiliary.php 17 KB

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