HrServies.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\zskk\servies;
  4. use app\admin\model\hospital\Hrpatient;
  5. use app\admin\servies\HrpatientServies;
  6. use app\common\library\Gm;
  7. use app\zskk\model\CallRecord;
  8. use app\zskk\model\HospitalHrpatient;
  9. use app\zskk\model\HrRecord;
  10. use app\zskk\model\Patient;
  11. use think\Exception;
  12. use think\facade\Cache;
  13. use think\facade\Config;
  14. class HrServies
  15. {
  16. public $patientModel = null;
  17. public $hrpatientModel = null;
  18. private $patientServies = null;
  19. private $hrpatientServies = null;
  20. private $hrRecordModel = null;
  21. private $callRecordModel = null;
  22. public function __construct(
  23. PatientServies $patientServies,
  24. Patient $patientModel,
  25. Hrpatient $hrpatientModel,
  26. HrRecord $hrRecordModel,
  27. HrpatientServies $hrpatientServies,
  28. CallRecord $callRecordModel
  29. )
  30. {
  31. // parent::__construct();
  32. $this->patientServies = $patientServies;
  33. $this->patientModel = $patientModel;
  34. $this->hrpatientModel = $hrpatientModel;
  35. $this->hrpatientServies = $hrpatientServies;
  36. $this->hrRecordModel = $hrRecordModel;
  37. $this->callRecordModel = $callRecordModel;
  38. }
  39. public function getShareCheck($ID_CARDNUM, $ORGCODE,$DOCTORCODE,$DEPART,$result): bool|array
  40. {
  41. $ID_CARDNUM = Gm::encrypt(Config::get('gm.key'),$ID_CARDNUM);
  42. $data = array();
  43. $patients = $this->patientModel->getPatientsByID_CARDNUM($ID_CARDNUM);
  44. $r = false;
  45. foreach($patients as $patient) {
  46. $return = $this->getRecogViewDetails($patient['PATIENT_CODE'], $ORGCODE,$DOCTORCODE,$DEPART,$result);
  47. $data[] = $return;
  48. if($return)
  49. {
  50. $r = true;
  51. }
  52. }
  53. if($result == 1)
  54. {
  55. return $r;
  56. }
  57. return $data;
  58. }
  59. public function getRecogData($ID_CARDNUM, $ORGCODE): array {
  60. $ID_CARDNUM = Gm::encrypt(Config::get('gm.key'),$ID_CARDNUM);
  61. $data = array();
  62. $patients = $this->patientModel->getPatientsByID_CARDNUM($ID_CARDNUM);
  63. foreach($patients as $patient) {
  64. $data[] = $this->getRecogViewDetailByPATIENT_CODE($patient['PATIENT_CODE'], $ORGCODE);
  65. }
  66. return ['DATA'=>$data];
  67. }
  68. public function getRecogViewDetailByToken($token): array {
  69. $params = Cache::get($token);
  70. if(empty($params))
  71. {
  72. // todo 错误返回 先登录
  73. return [];
  74. }
  75. $ID_CARDNUM = $params['ID_CARDNUM'];
  76. $ID_CARDNUM = Gm::encrypt(Config::get('gm.key'),$ID_CARDNUM);
  77. $HRORGCODE = $params['ORGCODE'];
  78. $_data = $this->getRecogViewDetail($ID_CARDNUM, $HRORGCODE);
  79. $patient = $_data[0]['PATIENT'];
  80. $data = [
  81. 'patient' => $patient,
  82. 'data' => $_data,
  83. ];
  84. // todo 容错处理
  85. $this->recordTokenRequestTime($token);
  86. $this->recordCallGetRecogViewDetail($params, $patient);
  87. return $data;
  88. }
  89. public function recordTokenRequestTime($token) {
  90. // TODO 记录token请求时间
  91. }
  92. public function recordCallGetRecogViewDetail($params, $patient) {
  93. $data = [
  94. 'MPI'=>$patient['MPI'],
  95. 'MPI_KEY'=>$patient['MPI_KEY'],
  96. 'ID_CARDNUM' => $params['ID_CARDNUM'],
  97. 'ORGCODE'=>$params['ORGCODE'],
  98. 'ORGNAME'=>$params['ORGNAME'],
  99. 'DOCTORNAME'=>$params['DOCTORNAME'],
  100. 'DOCTORCODE'=>$params['DOCTORCODE']
  101. ];
  102. $this->callRecordModel->save($data);
  103. }
  104. public function getRecogViewDetail($ID_CARDNUM, $HRORGCODE): array {
  105. $datas = array();
  106. $patients = $this->patientModel->getPatientsByID_CARDNUM($ID_CARDNUM);
  107. foreach($patients as $patient) {
  108. $data = $this->getRecogViewDetailByPATIENT_CODE($patient['PATIENT_CODE'], $HRORGCODE);
  109. $data['MEDICAL_INFORMATION']['PATIENT_CODE'] = $patient['PATIENT_CODE'];
  110. $HR_RECORDS = $this->hrpatientServies->_getHrpatientDetailByPATIENT_CODE($patient['PATIENT_CODE'], $HRORGCODE);
  111. $data['HR_RECORDS'] = $HR_RECORDS;
  112. $datas[] = $data;
  113. }
  114. return $datas;
  115. }
  116. public function getRecogViewDetailByPATIENT_CODE($PATIENT_CODE): array {
  117. $data = null;
  118. $data = $this->patientServies->getPatient(['PATIENT_CODE'=> $PATIENT_CODE]);
  119. return $data;
  120. }
  121. public function getRecogViewDetails($PATIENT_CODE,$ORGCODE,$DOCTORCODE,$DEPART,$RESULT): bool|array {
  122. $data = null;
  123. $WHERE = [];
  124. if($ORGCODE)
  125. {
  126. $WHERE['ORGCODE'] = $ORGCODE;
  127. }
  128. if($DOCTORCODE)
  129. {
  130. $WHERE['DOCTORCODE'] = $DOCTORCODE;
  131. }
  132. if($DEPART)
  133. {
  134. $WHERE['DEPTNAME'] = $DEPART;
  135. }
  136. $data = $this->patientServies->getPatientSign(['PATIENT_CODE'=> $PATIENT_CODE],$WHERE,$RESULT);
  137. return $data;
  138. }
  139. public function hanldeHR_RECORDS(&$HR_RECORDS) {
  140. foreach($HR_RECORDS as &$HR_RECORD) {
  141. $HR_RECORD['PATIENT_CODE'] = $this->getPATIENT_CODE($HR_RECORD['ID_CARDNUM'], $HR_RECORD['SERIESNUM'], $HR_RECORD['ORGCODE']);
  142. }
  143. }
  144. public function hanldeHR_RECORDSAndToken(&$HR_RECORDS, $TOKEN) {
  145. $data = Cache::get($TOKEN);
  146. $DOCTORCODE = $data['DOCTORCODE'];
  147. $DOCTORNAME = $data['DOCTORNAME'];
  148. $HRORGNAME = $data['ORGNAME'];
  149. $HRORGCODE = $data['ORGCODE'];
  150. foreach($HR_RECORDS as &$HR_RECORD) {
  151. $HR_RECORD['DOCTORCODE'] = $DOCTORCODE;
  152. $HR_RECORD['DOCTORNAME'] = $DOCTORNAME;
  153. $HR_RECORD['HRORGNAME'] = $HRORGNAME;
  154. $HR_RECORD['HRORGCODE'] = $HRORGCODE;
  155. }
  156. }
  157. public function getPATIENT_CODE($ID_CARDNUM, $SERIESNUM, $HRORGCODE) {
  158. return md5(md5($ID_CARDNUM).$HRORGCODE.$SERIESNUM);
  159. }
  160. public function saveRecogResult($HR_RECORDS): void {
  161. $this->hanldeHR_RECORDS($HR_RECORDS);
  162. $where = $this->_saveRecogViewDetail($HR_RECORDS);
  163. $this->saveHospitalHrPatient($where);
  164. }
  165. public function saveHospitalHrPatient($where)
  166. {
  167. unset($where['REPORTID']);
  168. $data = $this->makeHrPatient($where);
  169. // $hrPatient = $this->hrpatientModel->where($where)->find();
  170. // if(!empty($hrPatient))
  171. // {
  172. // $this->hrpatientModel->where($where)->update($data);
  173. // }else{
  174. // $this->hrpatientModel->insert($data);
  175. // }
  176. // todo
  177. $model= new HospitalHrpatient();
  178. $info = $model->where($where)->find();
  179. if(empty($info))
  180. {
  181. $model->save($data);
  182. }else{
  183. $info->save($data);
  184. }
  185. }
  186. public function makeHrPatient($where) {
  187. // $where
  188. // 'PATIENT_CODE' => $PATIENT_CODE,
  189. // 'ORGCODE' => $ORGCODE,
  190. // 'HRORGCODE' => $HRORGCODE,
  191. // 'REPORTID' => $REPORTID
  192. $arr = [];
  193. $patient = $this->patientModel->getPatient(['PATIENT_CODE'=>$where['PATIENT_CODE']]);
  194. $record = $this->hrRecordModel->where($where)->where('HR_STATUS','<>','0')->select();
  195. $medical = $this->patientModel->getMedical($where['PATIENT_CODE']);
  196. $data = [];
  197. $data['MPI_KEY'] = $patient['MPI_KEY'];
  198. $data['PATIENT_CODE'] = $patient['PATIENT_CODE'];
  199. $data['NAME'] = $patient['NAME'];
  200. $data['GENDER'] = $patient['GENDER'];
  201. $data['AGE'] = $medical['AGE'];
  202. $age_last = substr( $data['AGE'], -1);
  203. $age_num = substr( $data['AGE'], 0,-1);
  204. $last = match ($age_last) {
  205. 'Y' => '365',
  206. 'M' => '30',
  207. 'W' => '7',
  208. 'D' => '1',
  209. default => $age_last,
  210. };
  211. $data['AGE_DAY'] = $last*$age_num;
  212. $data['ID_CARDNUM'] = $patient['ID_CARDNUM'];
  213. $data['MPI'] = $patient['MPI'];
  214. $data['ENCOUNTER_DATE'] = $medical['ENCOUNTER_DATE']; // 就诊时间
  215. $data['CHIEFCOMPLAINT'] = $medical['CHIEFCOMPLAINT']; //主诉
  216. $data['DIAGNOSENAME'] = $medical['DIAGNOSENAME']; //诊断
  217. $data['INSUR_CARD_NO'] = $patient['INSUR_CARD_NO'];
  218. $data['ID_CARD_CODE'] = $patient['ID_CARD_CODE'];
  219. $data['ID_CARD'] = $patient['ID_CARD'];
  220. $data['GENDER_CODE'] = $patient['GENDER_CODE'];
  221. $data['BIRTH_DATE'] = $patient['BIRTH_DATE'];
  222. $data['MARITAL_STATUS_CODE'] = $patient['MARITAL_STATUS_CODE'];
  223. $data['MARITAL_STATUS'] = $patient['MARITAL_STATUS'];
  224. $data['ADDRESS'] = $patient['ADDRESS'];
  225. $data['ECARD_NO'] = $patient['ECARD_NO'];
  226. $data['SERIESNUM'] = $medical['SERIESNUM'];
  227. $data['CLASS_CODE'] = $medical['CLASS_CODE'];
  228. $data['CLASS'] = $medical['CLASS'];
  229. $data['ENCOUNTER_CARD_NO'] = $medical['ENCOUNTER_CARD_NO'];
  230. $data['RECORD_CODE'] = $medical['RECORD_CODE'];
  231. $data['HOS_EMPI'] = $medical['HOS_EMPI'];
  232. $data['DEPTNAME'] = $medical['DEPTNAME'];
  233. $data['DIAGNOSECODE'] = $medical['DIAGNOSECODE'];
  234. $data['ORGCODE'] = $record[0]['ORGCODE'];
  235. $data['ORGNAME'] = $record[0]['ORGNAME'];
  236. $data['HRORGNAME'] = $record[0]['HRORGNAME'];
  237. $data['HRORGCODE'] = $record[0]['HRORGCODE'];
  238. $data['create_time'] = $record[0]['HRTIME'];
  239. // $exam = $this->patientModel->getExam($where['PATIENT_CODE']);
  240. // $examCode = [];
  241. // $examItem = [];
  242. // $recordCode = array_column($record,'ITEMNAMECODE');
  243. // if(!empty($exam))
  244. // {
  245. // $examCode = array_column($exam,'HR_ITEMNAME_CODE');
  246. // $examItem = array_column($exam,'EXAM_ITEMNAME');
  247. // $hrExamCode = array_column($exam,'HR_ITEMNAME_CODE');
  248. // $hrExam = array_column($exam,'HR_ITEMNAME');
  249. // }
  250. // $medical = $this->patientModel->getMedical($where['PATIENT_CODE']);
  251. // $medicalCode = [];
  252. // $medicalItem = [];
  253. // if(!empty($medical))
  254. // {
  255. // $medicalCode = array_column($medical,'LAB_HR_ITEMNAME_CODE');
  256. // $medicalItem = array_column($medical,'LAB_ITEMNAME');
  257. // $hrInspectCode = array_column($exam,'LAB_HR_ITEMNAME_CODE');
  258. // $hrInspectExam = array_column($exam,'LAB_HR_ITEMNAME');
  259. // }
  260. $exam = [];
  261. $examCode = [];
  262. $lab = [];
  263. $labCode = [];
  264. $hrExam = [];
  265. $hrExamCode = [];
  266. $bhrExam = [];
  267. $bhrExamCode = [];
  268. $hrInspect = [];
  269. $hrInspectCode = [];
  270. $bhrInspect = [];
  271. $bhrInspectCode = [];
  272. foreach ($record as $v)
  273. {
  274. $data['HR_CODE'] = md5($v['ORGCODE'].$v['HRORGCODE'].$patient['PATIENT_CODE']);
  275. if($v['HR_STATUS'] == 1)
  276. {
  277. $status = '是';
  278. }elseif ($v['HR_STATUS'] == 2)
  279. {
  280. $status = '否';
  281. }else{
  282. continue;
  283. }
  284. if($v['ITEMNAMETYPE'] == 1)
  285. {
  286. //检查
  287. $exam[] = $v['ITEMNAME'].'('.$status.')'; //检查项目
  288. $examCode[] = $v['ITEMNAMECODE'].'('.$v['HR_STATUS'].')';
  289. if($v['HR_STATUS'] == 1)
  290. {
  291. $hrExam[] = $v['ITEMNAME'];
  292. $hrExamCode[] = $v['ITEMNAMECODE'];
  293. }elseif ($v['HR_STATUS'] == 2)
  294. {
  295. $bhrExam[] = $v['ITEMNAME'];
  296. $bhrExamCode[] = $v['ITEMNAMECODE'];
  297. }
  298. }else{
  299. //检验
  300. $lab[] = $v['ITEMNAME'].'('.$status.')';; //检验项目
  301. $labCode[] = $v['ITEMNAMECODE'].'('.$v['HR_STATUS'].')';
  302. if($v['HR_STATUS'] == 1)
  303. {
  304. $hrInspect[] = $v['ITEMNAME'];
  305. $hrInspectCode[] = $v['ITEMNAMECODE'];
  306. }elseif ($v['HR_STATUS'] == 2)
  307. {
  308. $bhrInspect[] = $v['ITEMNAME'];
  309. $bhrInspectCode[] = $v['ITEMNAMECODE'];
  310. }
  311. }
  312. $data['HRORGCODE'] = $v['HRORGCODE'];
  313. }
  314. if(!empty($exam))
  315. {
  316. $data['EXAM_ITEMNAME'] = implode(',',$exam);
  317. $data['EXAM_ITEMNAME_CODE'] = implode(',',$examCode);
  318. }
  319. if(!empty($lab))
  320. {
  321. $data['LAB_ITEMNAME'] = implode(',',$lab);
  322. $data['LAB_ITEMNAME_CODE'] = implode(',',$labCode);
  323. }
  324. if(!empty($hrExam))
  325. {
  326. $data['HR_EXAM_ITEMNAME'] = implode(',',$hrExam);
  327. $data['HR_EXAM_ITEMNAME_CODE'] = implode(',',$hrExamCode);
  328. }
  329. if(!empty($bhrExam))
  330. {
  331. $data['BHR_EXAM_ITEMNAME'] = implode(',',$bhrExam);
  332. $data['BHR_EXAM_ITEMNAME_CODE'] = implode(',',$bhrExamCode);
  333. }
  334. if(!empty($hrInspect))
  335. {
  336. $data['HR_LAB_ITEMNAME'] = implode(',',$hrInspect);
  337. $data['HR_LAB_ITEMNAME_CODE'] = implode(',',$hrInspectCode);
  338. }
  339. if(!empty($bhrInspect))
  340. {
  341. $data['BHR_LAB_ITEMNAME'] = implode(',',$bhrInspect);
  342. $data['BHR_LAB_ITEMNAME_CODE'] = implode(',',$bhrInspectCode);
  343. }
  344. return $data;
  345. }
  346. public function saveRecogViewDetail($HR_RECORDS, $TOKEN) {
  347. $this->hanldeHR_RECORDSAndToken($HR_RECORDS, $TOKEN);
  348. // 获取token请求时间和PATIENT_CODE删除时间和内容做对比
  349. $this->_saveRecogViewDetail($HR_RECORDS);
  350. }
  351. public function _saveRecogViewDetail($HR_RECORDS)
  352. {
  353. $data = $this->checkHR_RECORDS($HR_RECORDS);
  354. $where = $data['where'];
  355. // 删除 hr_record表
  356. $this->hrRecordModel->where($where)->delete();
  357. $news = $data['news'];
  358. // 存储hr_record表
  359. $this->hrRecordModel->saveAll($news);
  360. return $where;
  361. }
  362. /**
  363. * @throws Exception
  364. */
  365. public function checkHR_RECORDS($HR_RECORDS): array {
  366. if(count($HR_RECORDS) === 0) {
  367. //抛出异常 未传入互认数据
  368. throw new Exception('未传入互认数据');
  369. }
  370. $PATIENT_CODE = $HR_RECORDS[0]['PATIENT_CODE'];
  371. $ORGCODE = $HR_RECORDS[0]['ORGCODE'];
  372. $HRORGCODE = $HR_RECORDS[0]['HRORGCODE'];
  373. $REPORTID = array();
  374. $news = array();
  375. foreach($HR_RECORDS as $HR_RECORD) {
  376. $this->checkPATIENT_CODE($PATIENT_CODE, $HR_RECORD['PATIENT_CODE']);
  377. $this->checkORGCODE($ORGCODE, $HR_RECORD['ORGCODE'], $HR_RECORD['ORGNAME']);
  378. $this->checkHRORGCODE($HRORGCODE, $HR_RECORD['HRORGCODE'], $HR_RECORD['HRORGNAME']);
  379. $this->checkITEMNAME($HR_RECORD['REPORTID'], $HR_RECORD['ITEMNAME'], $HR_RECORD['ITEMNAMECODE'], $HR_RECORD['ITEMNAMETYPE']);
  380. $this->checkBHR_CODE($HR_RECORD['HR_STATUS'], $HR_RECORD['BHR_CODE'], $HR_RECORD['BHR_REASON']);
  381. $new = [
  382. 'DOCTORNAME' => $HR_RECORD['DOCTORNAME'],
  383. 'DOCTORCODE' => $HR_RECORD['DOCTORCODE'],
  384. 'REPORTID' => $HR_RECORD['REPORTID'],
  385. 'PATIENT_CODE' => $HR_RECORD['PATIENT_CODE'],
  386. 'ORGNAME' => $HR_RECORD['ORGNAME'],
  387. 'HRORGNAME' => $HR_RECORD['HRORGNAME'],
  388. 'ORGCODE' => $HR_RECORD['ORGCODE'],
  389. 'HRORGCODE' => $HR_RECORD['HRORGCODE'],
  390. 'ITEMNAME' => $HR_RECORD['ITEMNAME'],
  391. 'ITEMNAMECODE' => $HR_RECORD['ITEMNAMECODE'],
  392. 'HR_STATUS' => $HR_RECORD['HR_STATUS'],
  393. 'BHR_CODE' => $HR_RECORD['BHR_CODE'],
  394. 'ITEMNAMETYPE' => $HR_RECORD['ITEMNAMETYPE'],
  395. 'BHR_REASON' => $HR_RECORD['BHR_REASON'],
  396. 'HRTIME' => $HR_RECORD['HRTIME'] ?? date('Y-m-d H:i:s'),
  397. ];
  398. $news[] = $new;
  399. $REPORTID[] = $HR_RECORD['REPORTID'];
  400. }
  401. $where = [
  402. 'PATIENT_CODE' => $PATIENT_CODE,
  403. 'ORGCODE' => $ORGCODE,
  404. 'HRORGCODE' => $HRORGCODE,
  405. 'REPORTID' => $REPORTID
  406. ];
  407. // PATIENT_CODE
  408. // ORGNAME
  409. // HRORGNAME
  410. // ORGCODE
  411. // HRORGCODE
  412. // ITEMNAME
  413. // ITEMNAMECODE
  414. // HR_STATUS
  415. // BHR_CODE
  416. // ITEMNAMETYPE
  417. // BHR_REASON
  418. return [
  419. 'where' => $where,
  420. 'news' => $news,
  421. ];
  422. }
  423. /**
  424. * @throws Exception
  425. */
  426. public function checkPATIENT_CODE($_PATIENT_CODE, $PATIENT_CODE): void {
  427. if($_PATIENT_CODE !== $PATIENT_CODE) {
  428. //抛出患者标识不一致异常
  429. throw new Exception('患者标识不一致');
  430. }
  431. }
  432. /**
  433. * @throws Exception
  434. */
  435. public function checkORGCODE($_ORGCODE, $ORGCODE, $ORGNAME): void {
  436. if($_ORGCODE !== $ORGCODE) {
  437. // 抛出互认机构不一致异常
  438. throw new Exception('互认机构不一致');
  439. }
  440. // todo 校验医院编码和医院名称是否一致
  441. }
  442. /**
  443. * @throws Exception
  444. */
  445. public function checkHRORGCODE($_HRORGCODE, $HRORGCODE, $HRORGNAME): void {
  446. if($_HRORGCODE !== $HRORGCODE) {
  447. // 抛出就诊机构不一致异常
  448. throw new Exception('就诊机构不一致');
  449. }
  450. // todo 校验医院编码和医院名称是否一致 去机构名称校验
  451. }
  452. /**
  453. * @throws Exception
  454. */
  455. public function checkITEMNAME($REPORT_CODE, $ITEMNAME, $ITEMNAMECODE, $ITEMNAMETYPE): void {
  456. // todo 校验项目编码 项目名称 项目类型是否一致
  457. if(true)
  458. {
  459. // todo 类型-》检验、检查
  460. }else{
  461. throw new Exception('校验项目编码 项目名称 项目类型是否一致');
  462. }
  463. }
  464. /**
  465. * @throws Exception
  466. */
  467. public function checkBHR_CODE($HR_STATUS, $BHR_CODE, $BHR_REASON): void {
  468. // todo 校验互认状态 不互认编码 不互认原因是否一致和合法
  469. }
  470. }