ExamModel.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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', "e.study_id",/*"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($limit,$num)
  250. ->order($order)
  251. ->field($field)
  252. ->select();
  253. if($institutionId == '73000003')
  254. {
  255. foreach ($list AS $k=>$v)
  256. {
  257. $aiReport = ReportModel::where('exam_id',$v['exam_id'])->where('type',3)->value('id');
  258. if(empty($aiReport))
  259. {
  260. $haveAi = 2;
  261. }else{
  262. $haveAi = 1;
  263. }
  264. $list[$k]['have_ai'] = $haveAi;
  265. }
  266. }
  267. $institution = InstitutionModel::where('id',$institutionId)->value('custom_field');
  268. $unit = InstitutionModel::where('id',$institutionId)->value('image_unit_code');
  269. foreach ($list AS $k=>$v)
  270. {
  271. $list[$k]['studytime'] = '';
  272. $studyTime = StudiesModel::where('id',$v['study_id'])->value('studytime');
  273. if(empty($studyTime))
  274. {
  275. $list[$k]['studytime'] = $v['exam_datetime'];
  276. }else{
  277. $hour = substr($studyTime,0,2);
  278. $minute = substr($studyTime,2,2);
  279. $second = substr($studyTime,4,2);
  280. $year = substr($v['exam_datetime'],0,4);
  281. $month = substr($v['exam_datetime'],4,2);
  282. $day = substr($v['exam_datetime'],6,2);
  283. $list[$k]['studytime'] = $year.'.'.$month.'.'.$day.' '.$hour.':'.$minute.':'.$second;
  284. }
  285. $list[$k]['medical_index'] = $v['medical_code'].'-'.$v['exam_datetime_code'].'-'.$v['body_part_code'].'-'.$v['device_code'].'-'.$unit;
  286. }
  287. return ['total'=>$total, 'list'=>$list,'custom_field'=>$institution];
  288. // } catch (Exception $exception){
  289. // $this->throwError($exception->getMessage(),0001);
  290. // }
  291. }
  292. public function getHistoryList($institutionId,$institutionIds, $fuzzyWhere, $params,$id)
  293. {
  294. try{
  295. $order = "e.doctor_sign desc,e.exam_datetime desc,e.createdAt desc";
  296. $field = [
  297. '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',
  298. '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',
  299. 'r.report_result', 'r.report_datetime','r.id AS rid','e.doctor_sign','e.patient_source','e.node_type',
  300. 'r.report_doctor_name','r.report_datetime',
  301. 'r.review_doctor_name','r.review_datetime',
  302. 'r.confirm_doctor_name','r.confirm_datetime',
  303. 'e.report_status','e.write_report','e.institution_id','r.impression','r.description'
  304. ];
  305. $total = $this
  306. ->alias('e')
  307. ->whereOr($fuzzyWhere)
  308. ->whereIn('e.institution_id', $institutionIds)
  309. ->where('e.status', 1)
  310. ->count();
  311. $limit = ($params['page']-1) * $params['num'];
  312. $num = $params['num'];
  313. if($limit + $num > $total)
  314. {
  315. $num = $total-$limit;
  316. if($num < 0) {
  317. $num = 0;
  318. }
  319. }
  320. $list = $this
  321. ->alias('e')
  322. ->join(['report'=>'r'], 'e.id = r.exam_id and r.type=1','LEFT')
  323. ->whereOr($fuzzyWhere)
  324. ->whereIn('e.institution_id', $institutionIds)
  325. ->where('e.status', 1)
  326. ->limit($limit,$num)
  327. ->order($order)
  328. ->field($field)
  329. ->select();
  330. $institution = InstitutionModel::where('id',$institutionId)->value('custom_field');
  331. $allIns = InstitutionModel::whereIn('id',$institutionIds)->column('id,name');
  332. $unit = InstitutionModel::where('id',$institutionId)->value('image_unit_code');
  333. foreach ($list AS $k=>$v)
  334. {
  335. $list[$k]['studytime'] = '';
  336. $studyTime = StudiesModel::where('id',$v['study_id'])->value('studytime');
  337. if(empty($studyTime))
  338. {
  339. $list[$k]['studytime'] = $v['exam_datetime'];
  340. }else{
  341. $hour = substr($studyTime,0,2);
  342. $minute = substr($studyTime,2,2);
  343. $second = substr($studyTime,4,2);
  344. $year = substr($v['exam_datetime'],0,4);
  345. $month = substr($v['exam_datetime'],4,2);
  346. $day = substr($v['exam_datetime'],6,2);
  347. $list[$k]['studytime'] = $year.'.'.$month.'.'.$day.' '.$hour.':'.$minute.':'.$second;
  348. }
  349. if($v['exam_id'] == $id)
  350. {
  351. $list[$k]['local'] = 1;
  352. }else{
  353. if($v['institution_id'] == $institutionId)
  354. {
  355. $list[$k]['local'] = 2;
  356. }else{
  357. $list[$k]['local'] = 3;
  358. }
  359. }
  360. $list[$k]['institution_name'] = $allIns[$v['institution_id']] ?? '';
  361. $list[$k]['medical_index'] = $v['medical_code'].'-'.$v['exam_datetime_code'].'-'.$v['body_part_code'].'-'.$v['device_code'].'-'.$unit;
  362. }
  363. return ['total'=>$total, 'list'=>$list,'custom_field'=>$institution];
  364. } catch (Exception $exception){
  365. $this->throwError($exception->getMessage(),0001);
  366. }
  367. }
  368. public function getExamData($id)
  369. {
  370. return ExamModel::where('id',$id)->find();
  371. }
  372. public function getRemoteExamList($institutionId, $fuzzyWhere, $moreWhere,$class_where, $params,$report_where)
  373. {
  374. try{
  375. $field = [
  376. 'a.name', 'a.sex', 'a.age',
  377. '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',
  378. 'r.report_result', 'r.report_datetime','r.id AS rid',
  379. 'r.report_doctor_name',
  380. 'r.review_doctor_name',
  381. 'a.report_status',
  382. 'r.remote_application_id',
  383. 'a.id AS ra_id',
  384. 'ro.status','ro.id AS order_id',
  385. 'a.remote_doctor_name'
  386. ];
  387. $dateWhere = [];
  388. if(isset($moreWhere[0]) && !empty($moreWhere[0])){
  389. $dateWhere[] = $moreWhere[0];
  390. unset($moreWhere[0]);
  391. }
  392. $clWhere = [];
  393. if(isset($moreWhere['exam_class']) && !empty($moreWhere['exam_class'])){
  394. $clWhere['a.exam_class'] = $moreWhere['exam_class'];
  395. unset($moreWhere['exam_class']);
  396. }
  397. $list = ApplicationModel::alias('a')
  398. ->join(['remote_order'=>'ro'],'a.id=ro.application_id','left')
  399. ->join(['report'=>'r'],'r.remote_application_id=a.id','LEFT')
  400. ->where($fuzzyWhere)
  401. ->where($moreWhere)
  402. ->where($clWhere)
  403. ->where($report_where)
  404. ->where($dateWhere)
  405. ->where($class_where)
  406. ->where('a.status',0)
  407. ->where('a.local_institution_id', $institutionId)
  408. ->field($field)
  409. ->page($params['page'],$params['num'])
  410. ->order('a.req_date_time desc')
  411. ->select();
  412. $total = ApplicationModel::alias('a')
  413. ->join(['report'=>'r'],'r.remote_application_id=a.id','LEFT')
  414. ->where($fuzzyWhere)
  415. ->where($moreWhere)
  416. ->where($clWhere)
  417. ->where($report_where)
  418. ->where($dateWhere)
  419. ->where($class_where)
  420. ->where('a.status',0)
  421. ->where('a.local_institution_id', $institutionId)
  422. ->count();
  423. return ['total'=>$total, 'list'=>$list];
  424. } catch (Exception $exception){
  425. $this->throwError($exception->getMessage(),0001);
  426. }
  427. }
  428. public function getRemoteList($id,$institutionId)
  429. {
  430. $field = [
  431. // 'p.name', 'p.sex', 'p.age','p.id AS pid',
  432. // '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',
  433. 'r.report_datetime','r.id AS rid',
  434. 'a.*','a.id AS ra_id',
  435. ];
  436. $list = RemoteModel::alias('a')
  437. ->join(['report'=>'r'], 'a.id = r.remote_application_id','LEFT')
  438. ->where('a.local_institution_id', $institutionId)
  439. ->where('a.exam_id',$id)
  440. ->field($field)
  441. ->order('a.exam_datetime desc')
  442. ->select();
  443. $total = RemoteModel::alias('a')
  444. ->join(['report'=>'r'], 'a.id = r.remote_application_id','LEFT')
  445. ->where('a.local_institution_id', $institutionId)
  446. ->where('a.exam_id',$id)
  447. ->count();
  448. return ['total'=>$total, 'list'=>$list];
  449. }
  450. public function check($studyId)
  451. {
  452. $id = $this->where('study_id',$studyId)->value('id');
  453. return $id;
  454. }
  455. public function getReport($where,$institution)
  456. {
  457. $data = ReportModel::alias('r')
  458. ->join(['exams'=>'e'],'e.id=r.exam_id and r.type=1')
  459. ->where('e.institution_id',$institution)
  460. ->where($where)
  461. ->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'])
  462. ->limit(0,10)
  463. ->select();
  464. return $data;
  465. }
  466. public function getDoctorClass($doctor)
  467. {
  468. $class = DoctorClassModel::where('doctor_id',$doctor)->value('doctor_class');
  469. return $class;
  470. }
  471. public function changeDoctor($doctor,$id)
  472. {
  473. $info = $this->where('id',$id)->update(['doctor_sign'=>$doctor]);
  474. return $info;
  475. }
  476. public function geInsInfo($id)
  477. {
  478. $info = InstitutionModel::where('id',$id)->find();
  479. return $info;
  480. }
  481. public function getDoctorDepart($ids)
  482. {
  483. return DepartmentModel::where('id','in',$ids)->column('department_name');
  484. }
  485. public function getFilmAnnex($id,$exam_datetime,$code)
  486. {
  487. $info = AnnexModel::where(function ($query) use ($id,$exam_datetime,$code){
  488. $query->where('exam_id',$id)
  489. ->whereOr("code = '$code' and exam_datetime='$exam_datetime'");
  490. })
  491. ->distinct(true)
  492. ->where('annex_class_code',4)
  493. ->where('status',1)
  494. ->field('name')
  495. ->select();
  496. return $info;
  497. }
  498. public function del_exam($id)
  499. {
  500. $info = $this->where('id',$id)->delete();
  501. return $info;
  502. }
  503. public function insertDcmDel($data)
  504. {
  505. $info = DcmdelModel::insert($data);
  506. return $info;
  507. }
  508. public function getSyncAi($where)
  509. {
  510. return DcmreportModel::where($where)->where('type',6)->column('exam_id');
  511. }
  512. public function getAiNode($where)
  513. {
  514. $examId = ExamModel::where($where)->value('id');
  515. return AireportModel::where('exam_id',$examId)->value('detections');
  516. }
  517. }