ExamModel.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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", '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', (string)($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', (string)($institutionId))
  248. ->where('e.status', '1')
  249. ->limit($limit,$num)
  250. ->order($order)
  251. ->field($field)
  252. ->select();
  253. $institution = InstitutionModel::where('id',$institutionId)->value('custom_field');
  254. $unit = InstitutionModel::where('id',$institutionId)->value('image_unit_code');
  255. foreach ($list as $k=>$v)
  256. {
  257. if(!empty($v['study_id']))
  258. {
  259. $list[$k]['study_id'] = $v['study_id'].'&node_type=';
  260. }
  261. $list[$k]['studytime'] = '';
  262. $studyTime = StudiesModel::where('id',$v['study_id'])->value('studytime');
  263. if(empty($studyTime))
  264. {
  265. $list[$k]['studytime'] = $v['exam_datetime'];
  266. }else{
  267. $hour = substr($studyTime,0,2);
  268. $minute = substr($studyTime,2,2);
  269. $second = substr($studyTime,4,2);
  270. $year = substr($v['exam_datetime'],0,4);
  271. $month = substr($v['exam_datetime'],4,2);
  272. $day = substr($v['exam_datetime'],6,2);
  273. $list[$k]['studytime'] = $year.'.'.$month.'.'.$day.' '.$hour.':'.$minute.':'.$second;
  274. }
  275. $list[$k]['medical_index'] = $v['medical_code'].'-'.$v['exam_datetime_code'].'-'.$v['body_part_code'].'-'.$v['device_code'].'-'.$unit;
  276. }
  277. return ['total'=>$total, 'list'=>$list,'custom_field'=>$institution];
  278. } catch (Exception $exception){
  279. $this->throwError($exception->getMessage(),0001);
  280. }
  281. }
  282. public function getRemoteExamList($institutionId, $fuzzyWhere, $moreWhere,$class_where, $params,$report_where)
  283. {
  284. try{
  285. $field = [
  286. 'a.name', 'a.sex', 'a.age',
  287. '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',
  288. 'r.report_result', 'r.report_datetime','r.id AS rid',
  289. 'r.report_doctor_name',
  290. 'r.review_doctor_name',
  291. 'a.report_status',
  292. 'r.remote_application_id',
  293. 'a.id AS ra_id',
  294. 'ro.status','ro.id AS order_id',
  295. 'a.remote_doctor_name'
  296. ];
  297. $dateWhere = [];
  298. if(isset($moreWhere[0]) && !empty($moreWhere[0])){
  299. $dateWhere[] = $moreWhere[0];
  300. unset($moreWhere[0]);
  301. }
  302. $clWhere = [];
  303. if(isset($moreWhere['exam_class']) && !empty($moreWhere['exam_class'])){
  304. $clWhere['a.exam_class'] = $moreWhere['exam_class'];
  305. unset($moreWhere['exam_class']);
  306. }
  307. $list = ApplicationModel::alias('a')
  308. ->join(['remote_order'=>'ro'],'a.id=ro.application_id','left')
  309. ->join(['report'=>'r'],'r.remote_application_id=a.id','LEFT')
  310. ->where($fuzzyWhere)
  311. ->where($moreWhere)
  312. ->where($clWhere)
  313. ->where($report_where)
  314. ->where($dateWhere)
  315. ->where($class_where)
  316. ->where('a.status',0)
  317. ->where('a.local_institution_id', $institutionId)
  318. ->field($field)
  319. ->page($params['page'],$params['num'])
  320. ->order('a.req_date_time desc')
  321. ->select();
  322. $total = ApplicationModel::alias('a')
  323. ->join(['report'=>'r'],'r.remote_application_id=a.id','LEFT')
  324. ->where($fuzzyWhere)
  325. ->where($moreWhere)
  326. ->where($clWhere)
  327. ->where($report_where)
  328. ->where($dateWhere)
  329. ->where($class_where)
  330. ->where('a.status',0)
  331. ->where('a.local_institution_id', $institutionId)
  332. ->count();
  333. return ['total'=>$total, 'list'=>$list];
  334. } catch (Exception $exception){
  335. $this->throwError($exception->getMessage(),0001);
  336. }
  337. }
  338. public function getRemoteList($id,$institutionId)
  339. {
  340. $field = [
  341. // 'p.name', 'p.sex', 'p.age','p.id AS pid',
  342. // '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',
  343. 'r.report_datetime','r.id AS rid',
  344. 'a.*','a.id AS ra_id',
  345. ];
  346. $list = RemoteModel::alias('a')
  347. ->join(['report'=>'r'], 'a.id = r.remote_application_id','LEFT')
  348. ->where('a.local_institution_id', $institutionId)
  349. ->where('a.exam_id',$id)
  350. ->field($field)
  351. ->order('a.exam_datetime desc')
  352. ->select();
  353. $total = RemoteModel::alias('a')
  354. ->join(['report'=>'r'], 'a.id = r.remote_application_id','LEFT')
  355. ->where('a.local_institution_id', $institutionId)
  356. ->where('a.exam_id',$id)
  357. ->count();
  358. return ['total'=>$total, 'list'=>$list];
  359. }
  360. public function check($studyId)
  361. {
  362. $id = $this->where('study_id',$studyId)->value('id');
  363. return $id;
  364. }
  365. public function getReport($where,$institution)
  366. {
  367. $data = ReportModel::alias('r')
  368. ->join(['exams'=>'e'],'e.id=r.exam_id and r.type=1')
  369. ->where('e.institution_id',$institution)
  370. ->where($where)
  371. ->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'])
  372. ->limit(0,10)
  373. ->select();
  374. return $data;
  375. }
  376. public function getDoctorClass($doctor)
  377. {
  378. $class = DoctorClassModel::where('doctor_id',$doctor)->value('doctor_class');
  379. return $class;
  380. }
  381. public function changeDoctor($doctor,$id)
  382. {
  383. $info = $this->where('id',$id)->update(['doctor_sign'=>$doctor]);
  384. return $info;
  385. }
  386. public function geInsInfo($id)
  387. {
  388. $info = InstitutionModel::where('id',$id)->find();
  389. return $info;
  390. }
  391. public function getDoctorDepart($ids)
  392. {
  393. return DepartmentModel::where('id','in',$ids)->column('department_name');
  394. }
  395. public function getFilmAnnex($id,$exam_datetime,$code)
  396. {
  397. $info = AnnexModel::where(function ($query) use ($id,$exam_datetime,$code){
  398. $query->where('exam_id',$id)
  399. ->whereOr("code = '$code' and exam_datetime='$exam_datetime'");
  400. })
  401. ->distinct(true)
  402. ->where('annex_class_code',4)
  403. ->where('status',1)
  404. ->field('name')
  405. ->select();
  406. return $info;
  407. }
  408. public function del_exam($id)
  409. {
  410. $info = $this->where('id',$id)->delete();
  411. return $info;
  412. }
  413. public function insertDcmDel($data)
  414. {
  415. $info = DcmdelModel::insert($data);
  416. return $info;
  417. }
  418. public function getSyncAi($where)
  419. {
  420. return DcmreportModel::where($where)->where('type',6)->column('exam_id');
  421. }
  422. public function getAiNode($where)
  423. {
  424. $examId = ExamModel::where($where)->value('id');
  425. return AireportModel::where('exam_id',$examId)->value('detections');
  426. }
  427. }