PatientAuxiliary.php 17 KB

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