ExamModel.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. namespace app\api\model\exam;
  3. use app\api\model\aireport\AireportModel;
  4. use app\api\model\annex\AnnexModel;
  5. use app\api\model\dcmdel\DcmdelModel;
  6. use app\api\model\dcmreport\DcmreportModel;
  7. use app\api\model\department\DepartmentModel;
  8. use app\api\model\doctorclass\DoctorClassModel;
  9. use app\api\model\application\ApplicationModel;
  10. use app\api\model\inspect\InspectModel;
  11. use app\api\model\institution\InstitutionModel;
  12. use app\api\model\register\RegisterModel;
  13. use app\api\model\remote\RemoteModel;
  14. use app\api\model\report\ReportModel;
  15. use app\api\model\studies\StudiesModel;
  16. use app\api\model\ZskkDefaultModel;
  17. use app\api\response\ServerResponse;
  18. use think\Exception;
  19. use think\facade\Log;
  20. use app\api\model\patient\PatientModel;
  21. class ExamModel extends ZskkDefaultModel {
  22. protected $table= 'exams';
  23. protected $logName = "ExamModel";
  24. protected $model = null;
  25. public function registerList($institutionId, $fuzzyWhere, $moreWhere,$class_where, $params,$whereStatus)
  26. {
  27. try{
  28. $nameWhere = [];
  29. if(isset($moreWhere['name']) && !empty($moreWhere['name'])){
  30. $nameWhere[] = ['e.name','like','%'.$moreWhere['name'].'%'];
  31. unset($moreWhere['name']);
  32. }
  33. $timeWhere = [];
  34. if(isset($moreWhere[0]) && !empty($moreWhere[0])){
  35. $timeWhere[] = $moreWhere[0];
  36. unset($moreWhere[0]);
  37. }
  38. $field = [
  39. 'e.name', 'e.sex', 'e.age','e.institution_id','e.study_id','e.id as exam_id',
  40. 'e.id as exam_id', 'e.patient_num', 'e.exam_class', 'e.exam_datetime', 'e.exam_status', 'e.accession_num', 'e.exam_project','e.accession_num',
  41. 're.clin_doctors_name','re.register_name'
  42. ];
  43. $total = $this
  44. ->alias('e')
  45. ->join(['register'=>'re'],'re.exam_id = e.id','LEFT')
  46. ->where('e.status','1')
  47. ->where($fuzzyWhere)
  48. ->where($moreWhere)
  49. ->where($whereStatus)
  50. ->where($timeWhere)
  51. ->where($nameWhere)
  52. ->where($class_where)
  53. ->where('e.institution_id', $institutionId)
  54. ->count();
  55. $num = $total - ($params['page'] - 1) * $params['num'];
  56. if($num > $params['num']) {
  57. $num = $params['num'];
  58. }
  59. $list = $this
  60. ->alias('e')
  61. ->join(['register'=>'re'],'re.exam_id = e.id','LEFT')
  62. ->where('e.status','1')
  63. ->where($fuzzyWhere)
  64. ->where($moreWhere)
  65. ->where($whereStatus)
  66. ->where($timeWhere)
  67. ->where($nameWhere)
  68. ->where($class_where)
  69. ->where('e.institution_id', $institutionId)
  70. ->limit(($params['page'] - 1) * $params['num'], $num)
  71. // ->page($params['page'],$params['num'])
  72. ->order('e.exam_datetime desc,e.createdAt desc')
  73. ->field($field)
  74. // ->fetchSql()
  75. ->select();
  76. // $total = 10;
  77. // $total = $this
  78. // ->alias('e')
  79. // ->join(['register'=>'re'],'re.exam_id = e.id','LEFT')
  80. // ->where('e.status','1')
  81. // ->where($fuzzyWhere)
  82. // ->where($moreWhere)
  83. // ->where($whereStatus)
  84. // ->where($timeWhere)
  85. // ->where($nameWhere)
  86. // ->where($class_where)
  87. // ->where('e.institution_id', $institutionId)
  88. // ->count();
  89. return ['total'=>$total, 'list'=>$list];
  90. } catch (\Exception $exception){
  91. $this->throwError($exception->getMessage(),0001);
  92. }
  93. }
  94. public function getPatientId($id)
  95. {
  96. $patientId = $this
  97. ->where('id',$id)
  98. ->value('patient_id');
  99. return $patientId;
  100. }
  101. public function updatePatient($patientData,$patientId)
  102. {
  103. $patient = PatientModel::where('id',$patientId)->update($patientData);
  104. return $patient;
  105. }
  106. public function insertPatient($patientData)
  107. {
  108. $patient = PatientModel::insert($patientData);
  109. return $patient;
  110. }
  111. public function updateExam($data,$id)
  112. {
  113. $info = $this->where('id',$id)->update($data);
  114. return $info;
  115. }
  116. public function getRegister($params)
  117. {
  118. // 获取详情数据
  119. $field = [
  120. 'e.patient_num','r.exam_sub_class','e.exam_project','r.illness_desc','r.phys_sign','e.exam_datetime','e.bed_no','e.hopitalized_no','e.patient_area','e.application_department','e.application_doctor','e.id','e.exam_class',
  121. 'e.clin_symp','e.clin_diag','r.anamnesis','r.family_ill','r.ext as remark','r.device_name','e.out_patient',
  122. 'e.name','p.card_type','e.card_num','e.sex','e.age','e.birthday','e.phone','e.institution_id',"'' as institution",
  123. 'e.his_patient_id','e.body_part_text'
  124. ];
  125. $data = $this
  126. ->alias('e')
  127. ->join(['register'=>'r'],'r.exam_id=e.id','left')
  128. ->join(['patient_infos'=>'p'], 'e.patient_id = p.id','left')
  129. ->where('e.id', $params['examId'])
  130. ->field($field)
  131. ->find();
  132. if(!empty($data)){
  133. $institution = InstitutionModel::where('id',$data['institution_id'])->value('name');
  134. $data['institution'] = $institution;
  135. }
  136. return $data;
  137. }
  138. public function insertRegister($data)
  139. {
  140. RegisterModel::insert($data);
  141. }
  142. public function updateRegister($data,$examId)
  143. {
  144. RegisterModel::where('exam_id',$examId)->update($data);
  145. }
  146. public function deleteRegister($params,$user)
  147. {
  148. $info = $this->where('id',$params['examId'])->update(['status'=>'0','del_doctor_id'=>$user['id'],'del_doctor_name'=>$user['realname'],'del_datetime'=>date('Y-m-d H:i:s',time())]);
  149. return $info;
  150. }
  151. public function insertExam($data)
  152. {
  153. $info = $this->insert($data);
  154. return $info;
  155. }
  156. public function getExamList($institutionId, $fuzzyWhere, $moreWhere,$class_where, $params,$report_where,$gradeWhere)
  157. {
  158. // try{
  159. $nameWhere = [];
  160. if(isset($moreWhere['name_fuzzy']) && !empty($moreWhere['name_fuzzy'])){
  161. if(isset($moreWhere['name']) && !empty($moreWhere['name']))
  162. {
  163. // todo
  164. }else{
  165. $nameWhere[] = ['e.name','like','%'.$moreWhere['name_fuzzy'].'%'];
  166. }
  167. unset($moreWhere['name_fuzzy']);
  168. }
  169. if(isset($moreWhere['exam_class']) && !empty($moreWhere['exam_class'])){
  170. $moreWhere['e.exam_class'] = $moreWhere['exam_class'];
  171. unset($moreWhere['exam_class']);
  172. }
  173. $whereStatus = [];
  174. if (!(isset($moreWhere['exam_status']) && !empty($moreWhere['exam_status']))){
  175. $whereStatus = "e.exam_status not in ('0','1','2','-99')";
  176. }
  177. if(empty($report_where))
  178. {
  179. $order = "e.exam_datetime desc,e.createdAt desc";
  180. // $order = "e.id desc";
  181. }else{
  182. // $order = "e.doctor_sign desc,e.id desc";
  183. $order = "e.doctor_sign desc,e.exam_datetime desc,e.createdAt desc";
  184. }
  185. $field = [
  186. 'e.name', 'e.sex', 'e.age','e.patient_id as pid','e.hopitalized_no','e.bed_no','e.patient_area','e.body_part','e.application_department','e.application_doctor','e.out_patient','e.delivery_doctor','e.his_patient_id',
  187. 'e.id as exam_id', 'e.patient_num', 'e.exam_class', 'e.exam_datetime', "CONCAT(study_id,'&node_type=',node_type) as study_id", 'e.exam_status', 'e.accession_num', 'e.exam_project','e.medical_code','e.body_part_code','e.device_code','e.exam_datetime_code',
  188. 'r.report_result', 'r.report_datetime','r.id as rid','e.doctor_sign','e.patient_source','e.node_type',
  189. 'r.report_doctor_name','r.report_datetime',
  190. 'r.review_doctor_name','r.review_datetime',
  191. 'r.confirm_doctor_name','r.confirm_datetime',
  192. 'e.report_status','e.write_report','e.institution_id','r.impression','r.description'
  193. ];
  194. $whereDate = [];
  195. if(($moreWhere[0] ?? null))
  196. {
  197. $whereDate[] = $moreWhere[0];
  198. unset($moreWhere[0]);
  199. };
  200. $depart_where = [];
  201. if($moreWhere['application_department'] ?? null)
  202. {
  203. $depart_where[] = ['application_department','in',$moreWhere['application_department'][1]];
  204. unset($moreWhere['application_department']);
  205. }
  206. // $total = $this
  207. // ->alias('e')
  208. //// ->join(['patient_infos'=>'p'],'p.id=e.patient_id')
  209. //// ->join(['report'=>'r'], 'e.id = r.exam_id and r.type=1','LEFT')
  210. // ->where($fuzzyWhere)
  211. // ->where($whereDate)
  212. // ->where($gradeWhere)
  213. // ->where($moreWhere)
  214. // ->where($depart_where)
  215. // ->where($whereStatus)
  216. // ->where($report_where)
  217. // ->where($nameWhere)
  218. // ->where($class_where)
  219. // ->where('e.institution_id', $institutionId)
  220. // ->where('e.status', 1)
  221. // ->count();
  222. // $limit = ($params['page']-1) * $params['num'];
  223. // $num = $params['num'];
  224. // // if($limit > $total)
  225. // // {
  226. // // $limit = $limit-$total;
  227. // // }
  228. // if($limit + $num > $total)
  229. // {
  230. // $num = $total-$limit;
  231. // if($num < 0) {
  232. // $num = 0;
  233. // }
  234. // }
  235. $list = $this
  236. ->alias('e')
  237. ->join(['report'=>'r'], 'e.id = r.exam_id and r.type=1','LEFT')
  238. ->where($fuzzyWhere)
  239. ->where($whereDate)
  240. ->where($gradeWhere)
  241. ->where($moreWhere)
  242. ->where($depart_where)
  243. ->where($whereStatus)
  244. ->where($report_where)
  245. ->where($nameWhere)
  246. ->where($class_where)
  247. ->where('e.institution_id', $institutionId)
  248. ->where('e.status', 1)
  249. ->limit(10)
  250. ->order($order)
  251. ->field($field)
  252. ->select();
  253. var_dump($list);die;
  254. if($institutionId == '73000003')
  255. {
  256. foreach ($list as $k=>$v)
  257. {
  258. $aiReport = ReportModel::where('exam_id',$v['exam_id'])->where('type',3)->value('id');
  259. if(empty($aiReport))
  260. {
  261. $haveAi = 2;
  262. }else{
  263. $haveAi = 1;
  264. }
  265. $list[$k]['have_ai'] = $haveAi;
  266. }
  267. }
  268. $institution = InstitutionModel::where('id',$institutionId)->value('custom_field');
  269. $unit = InstitutionModel::where('id',$institutionId)->value('image_unit_code');
  270. foreach ($list as $k=>$v)
  271. {
  272. $list[$k]['studytime'] = '';
  273. $studyTime = StudiesModel::where('id',$v['study_id'])->value('studytime');
  274. if(empty($studyTime))
  275. {
  276. $list[$k]['studytime'] = $v['exam_datetime'];
  277. }else{
  278. $hour = substr($studyTime,0,2);
  279. $minute = substr($studyTime,2,2);
  280. $second = substr($studyTime,4,2);
  281. $year = substr($v['exam_datetime'],0,4);
  282. $month = substr($v['exam_datetime'],4,2);
  283. $day = substr($v['exam_datetime'],6,2);
  284. $list[$k]['studytime'] = $year.'.'.$month.'.'.$day.' '.$hour.':'.$minute.':'.$second;
  285. }
  286. $list[$k]['medical_index'] = $v['medical_code'].'-'.$v['exam_datetime_code'].'-'.$v['body_part_code'].'-'.$v['device_code'].'-'.$unit;
  287. }
  288. return ['total'=>$total, 'list'=>$list,'custom_field'=>$institution];
  289. // } catch (Exception $exception){
  290. // $this->throwError($exception->getMessage(),0001);
  291. // }
  292. }
  293. public function getHistoryList($institutionId,$institutionIds, $fuzzyWhere, $params,$id)
  294. {
  295. try{
  296. $order = "e.doctor_sign desc,e.exam_datetime desc,e.createdAt desc";
  297. $field = [
  298. 'e.name', 'e.sex', 'e.age','e.patient_id as pid','e.hopitalized_no','e.bed_no','e.patient_area','e.body_part','e.application_department','e.application_doctor','e.out_patient','e.delivery_doctor','e.his_patient_id',
  299. 'e.id as exam_id', 'e.patient_num', 'e.exam_class', 'e.exam_datetime', "CONCAT(study_id,'&node_type=',node_type) as study_id", 'e.exam_status', 'e.accession_num', 'e.exam_project','e.medical_code','e.body_part_code','e.device_code','e.exam_datetime_code',
  300. 'r.report_result', 'r.report_datetime','r.id as rid','e.doctor_sign','e.patient_source','e.node_type',
  301. 'r.report_doctor_name','r.report_datetime',
  302. 'r.review_doctor_name','r.review_datetime',
  303. 'r.confirm_doctor_name','r.confirm_datetime',
  304. 'e.report_status','e.write_report','e.institution_id','r.impression','r.description'
  305. ];
  306. $total = $this
  307. ->alias('e')
  308. ->whereOr($fuzzyWhere)
  309. ->whereIn('e.institution_id', $institutionIds)
  310. ->where('e.status', 1)
  311. ->count();
  312. $limit = ($params['page']-1) * $params['num'];
  313. $num = $params['num'];
  314. if($limit + $num > $total)
  315. {
  316. $num = $total-$limit;
  317. if($num < 0) {
  318. $num = 0;
  319. }
  320. }
  321. $list = $this
  322. ->alias('e')
  323. ->join(['report'=>'r'], 'e.id = r.exam_id and r.type=1','LEFT')
  324. ->whereOr($fuzzyWhere)
  325. ->whereIn('e.institution_id', $institutionIds)
  326. ->where('e.status', 1)
  327. ->limit($limit,$num)
  328. ->order($order)
  329. ->field($field)
  330. ->select();
  331. $institution = InstitutionModel::where('id',$institutionId)->value('custom_field');
  332. $allIns = InstitutionModel::whereIn('id',$institutionIds)->column('id,name');
  333. $unit = InstitutionModel::where('id',$institutionId)->value('image_unit_code');
  334. foreach ($list as $k=>$v)
  335. {
  336. $list[$k]['studytime'] = '';
  337. $studyTime = StudiesModel::where('id',$v['study_id'])->value('studytime');
  338. if(empty($studyTime))
  339. {
  340. $list[$k]['studytime'] = $v['exam_datetime'];
  341. }else{
  342. $hour = substr($studyTime,0,2);
  343. $minute = substr($studyTime,2,2);
  344. $second = substr($studyTime,4,2);
  345. $year = substr($v['exam_datetime'],0,4);
  346. $month = substr($v['exam_datetime'],4,2);
  347. $day = substr($v['exam_datetime'],6,2);
  348. $list[$k]['studytime'] = $year.'.'.$month.'.'.$day.' '.$hour.':'.$minute.':'.$second;
  349. }
  350. if($v['exam_id'] == $id)
  351. {
  352. $list[$k]['local'] = 1;
  353. }else{
  354. if($v['institution_id'] == $institutionId)
  355. {
  356. $list[$k]['local'] = 2;
  357. }else{
  358. $list[$k]['local'] = 3;
  359. }
  360. }
  361. $list[$k]['institution_name'] = $allIns[$v['institution_id']] ?? '';
  362. $list[$k]['medical_index'] = $v['medical_code'].'-'.$v['exam_datetime_code'].'-'.$v['body_part_code'].'-'.$v['device_code'].'-'.$unit;
  363. }
  364. return ['total'=>$total, 'list'=>$list,'custom_field'=>$institution];
  365. } catch (Exception $exception){
  366. $this->throwError($exception->getMessage(),0001);
  367. }
  368. }
  369. public function getExamData($id)
  370. {
  371. return ExamModel::where('id',$id)->find();
  372. }
  373. public function getRemoteExamList($institutionId, $fuzzyWhere, $moreWhere,$class_where, $params,$report_where)
  374. {
  375. try{
  376. $field = [
  377. 'a.name', 'a.sex', 'a.age',
  378. 'a.exam_id', 'a.patient_num', 'a.exam_class', 'a.exam_datetime', 'a.study_id', 'a.accession_num', 'a.exam_project','a.remote_institution_name',
  379. 'r.report_result', 'r.report_datetime','r.id as rid',
  380. 'r.report_doctor_name',
  381. 'r.review_doctor_name',
  382. 'a.report_status',
  383. 'r.remote_application_id',
  384. 'a.id as ra_id',
  385. 'ro.status','ro.id as order_id',
  386. 'a.remote_doctor_name'
  387. ];
  388. $dateWhere = [];
  389. if(isset($moreWhere[0]) && !empty($moreWhere[0])){
  390. $dateWhere[] = $moreWhere[0];
  391. unset($moreWhere[0]);
  392. }
  393. $clWhere = [];
  394. if(isset($moreWhere['exam_class']) && !empty($moreWhere['exam_class'])){
  395. $clWhere['a.exam_class'] = $moreWhere['exam_class'];
  396. unset($moreWhere['exam_class']);
  397. }
  398. $list = ApplicationModel::alias('a')
  399. ->join(['remote_order'=>'ro'],'a.id=ro.application_id','left')
  400. ->join(['report'=>'r'],'r.remote_application_id=a.id','LEFT')
  401. ->where($fuzzyWhere)
  402. ->where($moreWhere)
  403. ->where($clWhere)
  404. ->where($report_where)
  405. ->where($dateWhere)
  406. ->where($class_where)
  407. ->where('a.status',0)
  408. ->where('a.local_institution_id', $institutionId)
  409. ->field($field)
  410. ->page($params['page'],$params['num'])
  411. ->order('a.req_date_time desc')
  412. ->select();
  413. $total = ApplicationModel::alias('a')
  414. ->join(['report'=>'r'],'r.remote_application_id=a.id','LEFT')
  415. ->where($fuzzyWhere)
  416. ->where($moreWhere)
  417. ->where($clWhere)
  418. ->where($report_where)
  419. ->where($dateWhere)
  420. ->where($class_where)
  421. ->where('a.status',0)
  422. ->where('a.local_institution_id', $institutionId)
  423. ->count();
  424. return ['total'=>$total, 'list'=>$list];
  425. } catch (Exception $exception){
  426. $this->throwError($exception->getMessage(),0001);
  427. }
  428. }
  429. public function getRemoteList($id,$institutionId)
  430. {
  431. $field = [
  432. // 'p.name', 'p.sex', 'p.age','p.id as pid',
  433. // 'e.id as exam_id', 'e.patient_num', 'e.exam_class', 'e.exam_datetime', 'e.is_remote', 'e.study_id', 'e.exam_status', 'e.accession_num', 'e.exam_project',
  434. 'r.report_datetime','r.id as rid',
  435. 'a.*','a.id as ra_id',
  436. ];
  437. $list = RemoteModel::alias('a')
  438. ->join(['report'=>'r'], 'a.id = r.remote_application_id','LEFT')
  439. ->where('a.local_institution_id', $institutionId)
  440. ->where('a.exam_id',$id)
  441. ->field($field)
  442. ->order('a.exam_datetime desc')
  443. ->select();
  444. $total = RemoteModel::alias('a')
  445. ->join(['report'=>'r'], 'a.id = r.remote_application_id','LEFT')
  446. ->where('a.local_institution_id', $institutionId)
  447. ->where('a.exam_id',$id)
  448. ->count();
  449. return ['total'=>$total, 'list'=>$list];
  450. }
  451. public function check($studyId)
  452. {
  453. $id = $this->where('study_id',$studyId)->value('id');
  454. return $id;
  455. }
  456. public function getReport($where,$institution)
  457. {
  458. $data = ReportModel::alias('r')
  459. ->join(['exams'=>'e'],'e.id=r.exam_id and r.type=1')
  460. ->where('e.institution_id',$institution)
  461. ->where($where)
  462. ->field(['e.name','r.impression','r.description','e.id as exam_id','e.study_id','r.id as report_id','e.exam_class','e.exam_status'])
  463. ->limit(0,10)
  464. ->select();
  465. return $data;
  466. }
  467. public function getDoctorClass($doctor)
  468. {
  469. $class = DoctorClassModel::where('doctor_id',$doctor)->value('doctor_class');
  470. return $class;
  471. }
  472. public function changeDoctor($doctor,$id)
  473. {
  474. $info = $this->where('id',$id)->update(['doctor_sign'=>$doctor]);
  475. return $info;
  476. }
  477. public function geInsInfo($id)
  478. {
  479. $info = InstitutionModel::where('id',$id)->find();
  480. return $info;
  481. }
  482. public function getDoctorDepart($ids)
  483. {
  484. return DepartmentModel::where('id','in',$ids)->column('department_name');
  485. }
  486. public function getFilmAnnex($id,$exam_datetime,$code)
  487. {
  488. $info = AnnexModel::where(function ($query) use ($id,$exam_datetime,$code){
  489. $query->where('exam_id',$id)
  490. ->whereOr("code = '$code' and exam_datetime='$exam_datetime'");
  491. })
  492. ->distinct(true)
  493. ->where('annex_class_code',4)
  494. ->where('status',1)
  495. ->field('name')
  496. ->select();
  497. return $info;
  498. }
  499. public function del_exam($id)
  500. {
  501. $info = $this->where('id',$id)->delete();
  502. return $info;
  503. }
  504. public function insertDcmDel($data)
  505. {
  506. $info = DcmdelModel::insert($data);
  507. return $info;
  508. }
  509. public function getSyncAi($where)
  510. {
  511. return DcmreportModel::where($where)->where('type',6)->column('exam_id');
  512. }
  513. public function getAiNode($where)
  514. {
  515. $examId = ExamModel::where($where)->value('id');
  516. return AireportModel::where('exam_id',$examId)->value('detections');
  517. }
  518. }