ExamService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. namespace app\api\servies\exam;
  3. use app\api\actions\ZskkCache;
  4. use app\api\response\ZskkErrorResponse;
  5. use app\api\servies\report\ReportService;
  6. use app\api\servies\ZskkDefaultService;
  7. use app\api\utils\UUIDUtils;
  8. use app\api\validate\exam\ExamValidate;
  9. use app\api\dao\exam\ExamDao;
  10. use app\api\servies\common\CommonService;
  11. use think\facade\Config;
  12. use think\facade\Log;
  13. /**
  14. * 后台控制器基类
  15. * 接口方法权限 必传参数 接口返回 错误抛出 通用参数处理
  16. */
  17. class ExamService extends ZskkDefaultService {
  18. protected $logName = "ExamService";
  19. private $examDao = null;
  20. private $reportService = null;
  21. public function __construct(ExamDao $examDao,ReportService $reportService) {
  22. parent::__construct();
  23. $this->examDao = $examDao;
  24. $this->reportService = $reportService;
  25. }
  26. public function changeInsLogin($params,$token)
  27. {
  28. $user = $this->examDao->getCache($token);
  29. $user['institution_id'] = $params['institution_id'];
  30. $institution = $this->examDao->getInsInfo($user['institution_id']);
  31. $user['institution'] = $institution['name'];
  32. $rule = $this->examDao->getOtherRule(['doctor_id'=>$user['id'],'institution_id'=>$user['institution_id']],'rule_ids');
  33. if(empty($rule)){
  34. $rule = $user['doctor_role'];
  35. }else{
  36. $rule = explode(',',$rule);
  37. }
  38. $token = (time()+86400).rand(0,1000);
  39. $data = $this->setCache($token,$user);
  40. if(!$data){
  41. $this->throwError('token信息存储失败','0009');
  42. }
  43. $data = [
  44. 'token' => $token,
  45. 'realname' => $user['realname'],
  46. 'is_admin' => $user['is_admin'],
  47. 'username' => $user['username'],
  48. 'institution' => $user['institution'],
  49. 'institution_id'=> $user['institution_id'],
  50. 'report_full'=> $user['report_full'],
  51. 'is_new_browser'=> $institution['is_new_browser'],
  52. 'department_name' => $user['department'],
  53. 'role'=> $rule,
  54. 'message_push' => $user['message_push'],
  55. 'need_code'=> false,
  56. 'user_id' =>$user['id'],
  57. 'is_auto_mode' =>$institution['is_auto_mode'],
  58. 'remote' =>empty($institution['parent_institution']) ? '0' : '1'
  59. ];
  60. return $data;
  61. }
  62. public function getExamLIst($params,$token)
  63. {
  64. $user = $this->examDao->getCache($token);
  65. $arr = ['doctor_id'=>$user['id'],'doctor_name'=>$user['realname'],'institution_id'=>$user['institution_id']];
  66. if(($params['type'] ?? '') == 1)
  67. {
  68. // ris
  69. $arr['type'] = 2;
  70. }else{
  71. // 小程序
  72. $arr['type'] = 1;
  73. $this->examDao->saveDoctorVisit($arr);
  74. }
  75. $report_where = '';
  76. if(empty($user))
  77. {
  78. $this->throwError('登陆信息失效,请重新进行登陆','0099');
  79. }
  80. $institutionId = $user['institution_id'];
  81. $fuzzyField = ['name','patient_num','accession_num'];
  82. // $fuzzyWhere = $this->getListFuzzyWhere($params,$fuzzyField);
  83. $fuzzyWhere = '';
  84. if(!empty($params['fuzzy_search']))
  85. {
  86. $fuzzy = $params['fuzzy_search'];
  87. $fuzzyWhere = "name = '$fuzzy' or patient_num = '$fuzzy' or accession_num = '$fuzzy' ";
  88. }
  89. // 获取具体筛选 where 条件
  90. $moreField = ['exam_datetime','name','accession_num','patient_num','exam_class','exam_status','report_status','exam_project','application_department','name_fuzzy'];
  91. $moreWhere = $this->getListSpecificWhere($params, $moreField);
  92. if($params['status'] == 3){
  93. $class_where = $this->examDao->getRemoteExamClass($user['exam_class']);
  94. // 发起了远程
  95. // $report_where = " report_status>3 and report_status != 11";
  96. $data = $this->examDao->getRemoteExamList($institutionId, $fuzzyWhere, $moreWhere,$class_where, $params,$report_where);
  97. }else{
  98. // $ins = $this->examDao->getInsInfo($institutionId);
  99. $class_where = $this->examDao->getExamClass($user['exam_class']);
  100. $gradeWhere = [];
  101. if($user['doctor_grade'] == 1)
  102. {
  103. //管理级查看所有
  104. }elseif($user['doctor_grade'] == 2)
  105. {
  106. //科室级查看当前科室
  107. $depart = $this->examDao->getDoctorDepart($user['department_id']);
  108. $gradeWhere[] = ['application_department','in',$depart];
  109. }elseif($user['doctor_grade'] == 3)
  110. {
  111. //医生级查看当前医生的
  112. $name = $user['realname'];
  113. $gradeWhere = "application_doctor='$name'";
  114. }
  115. // if($ins['is_auto_mode'] == '0')
  116. // {
  117. // $data = $this->examDao->getExamList($institutionId, $fuzzyWhere, $moreWhere,$class_where, $params,$report_where,$gradeWhere);
  118. //
  119. // }else{
  120. if($user['is_admin'] == 1)
  121. {
  122. //医院管理员 可以查看当前类型的所有
  123. $data = $this->examDao->getExamList($institutionId, $fuzzyWhere, $moreWhere,$class_where, $params,$report_where,$gradeWhere);
  124. }else{
  125. //获取医生权限
  126. $userId = $user['id'];
  127. $doctor_class = $this->examDao->getDoctorClass($userId);
  128. $data = [];
  129. // 写审确认 自己的+所有审、确认
  130. // 写审 自己的+所有审、确认
  131. // 写确认 自己的+所有审、确认
  132. // 审确认 所有已写、审、确认
  133. // 审 所有已写、审、确认
  134. // 确认 所有审、确认
  135. // 写 自己的+确认
  136. if(strpos($doctor_class, '2') !== false && (strpos($doctor_class, '3') !== false || strpos($doctor_class, '4') !== false)){
  137. //既是写 又是审或者确认 获取自己的 + 所有已写、已审核、已完成的
  138. $report_where = "doctor_sign='$userId' or doctor_sign='' or exam_status=7 or exam_status=8 or exam_status=12 or exam_status=9";
  139. $data = $this->examDao->getExamList($institutionId, $fuzzyWhere, $moreWhere,$class_where, $params,$report_where,$gradeWhere);
  140. }
  141. if(strpos($doctor_class, '2') === false && (strpos($doctor_class, '3') !== false || strpos($doctor_class, '4') !== false)){
  142. //没有写权限但是 有审核权限或者确认权限 获取所有审、已完成的
  143. $report_where = "exam_status=8 or exam_status=9";
  144. if(strpos($doctor_class, '3') !== false)
  145. {
  146. //有审核权限 添加所有已写的
  147. $report_where .= " or exam_status=7 or exam_status=12";
  148. }
  149. $data = $this->examDao->getExamList($institutionId, $fuzzyWhere, $moreWhere,$class_where, $params,$report_where,$gradeWhere);
  150. }
  151. if(strpos($doctor_class, '2') !== false && strpos($doctor_class, '3') === false && strpos($doctor_class, '4') === false){
  152. //只有写权限
  153. $report_where = "doctor_sign='$userId' or doctor_sign='' or exam_status=9";
  154. $data = $this->examDao->getExamList($institutionId, $fuzzyWhere, $moreWhere,$class_where, $params,$report_where,$gradeWhere);
  155. }
  156. if(strpos($doctor_class, '1') !== false && strpos($doctor_class, '2') === false && strpos($doctor_class, '3') === false && strpos($doctor_class, '4') === false){
  157. //只有临床权限
  158. $report_where = '';
  159. $data = $this->examDao->getExamList($institutionId, $fuzzyWhere, $moreWhere,$class_where, $params,$report_where,$gradeWhere);
  160. }
  161. }
  162. // }
  163. $custom_field = $data['custom_field'];
  164. $custom = [];
  165. if($custom_field !== '')
  166. {
  167. $field = explode(',',$custom_field);
  168. foreach ($field as $k=>$v)
  169. {
  170. if(!(Config::get('ins_custom_field')[$v] ?? null))
  171. {
  172. continue;
  173. }
  174. $custom[$k]['props'] = $v;
  175. $custom[$k]['name'] = Config::get('ins_custom_field')[$v];
  176. }
  177. }
  178. $data['custom_field'] = $custom;
  179. }
  180. foreach ($data['list'] as $k=>$v)
  181. {
  182. $data['list'][$k]['insConfig'] = '';
  183. if(!empty($v['patient_source']))
  184. {
  185. $sourceArr = Config::get('patient_source');
  186. $sourceInfo = $sourceArr[$v['patient_source']] ?? [];
  187. if(!empty($sourceInfo))
  188. {
  189. $data['list'][$k]['insConfig'] = $sourceInfo['name'];
  190. }
  191. }
  192. }
  193. return $data;
  194. }
  195. public function getRemoteLIst($params,$token)
  196. {
  197. $institutionId = $this->examDao->getInstitution($token);
  198. $data = $this->examDao->getRemoteList($params['examId'],$institutionId);
  199. return $data;
  200. }
  201. public function saveExam($params)
  202. {
  203. $id = $this->examDao->checkIsSet($params['study_id']);
  204. if($id){
  205. //存在则修改
  206. $update = ['exam_status'=>'3','updatedAt'=>date('Y-m-d H:i:s',time())];
  207. $this->examDao->updateExam($id,$update);
  208. } else {
  209. //不存在则创建
  210. $data = [
  211. 'id'=>UUIDUtils::uuid(),
  212. 'patient_id'=>$params['patient_id'],
  213. 'study_id'=>$params['study_id'],
  214. 'accession_num'=>$params['accession_num'],
  215. 'studyuid'=>$params['studyuid'],
  216. 'birthday'=>$params['birthday'] ?? '',
  217. 'exam_datetime'=>$params['exam_datetime'],
  218. 'exam_class'=>$params['exam_class'],
  219. 'body_part'=>$params['body_part'],
  220. 'institution_id'=>$params['institution_id'],
  221. 'device_name'=>$params['device_name'],
  222. 'patient_num'=>$params['patient_num'],
  223. 'createdAt'=>date('Y-m-d H:i:s',time()),
  224. 'exam_status'=>3
  225. ];
  226. $this->examDao->insertExam($data);
  227. $id = $data['id'];
  228. }
  229. return $id;
  230. }
  231. public function getReport($params,$doctor)
  232. {
  233. $search = $params['fuzzy_search'] ?? null;
  234. if(empty($search))
  235. {
  236. $this->throwError('无效的请求参数,请求不能为空','0012');
  237. }
  238. $field = ['impression','description'];
  239. $where = $this->getListFuzzyWhere($params,$field);
  240. $report = $this->examDao->getReport($where,$doctor['institution_id']);
  241. foreach ($report as $k=>$v)
  242. {
  243. $replace = "<span style='color:red'>$search</span>";
  244. $report[$k]['impression'] = str_replace($search,$replace,$v['impression']);
  245. $report[$k]['description'] = str_replace($search,$replace,$v['description']);
  246. }
  247. return $report;
  248. }
  249. public function changeDoctor($params,$doctor)
  250. {
  251. if($doctor['is_admin'] !== '1')
  252. {
  253. $this->throwError('非管理员无法分配');
  254. }
  255. $info = $this->examDao->changeDoctor($params['doctor'],$params['exam_id']);
  256. return $info;
  257. }
  258. public function getFilmAnnex($id,$exam_datetime,$code)
  259. {
  260. $data = $this->examDao->getFilmAnnex($id,$exam_datetime,$code);
  261. return $data;
  262. }
  263. public function del_exam($params,$token)
  264. {
  265. $user = $this->getCache($token);
  266. $result = $this->reportService->reportAuthentication($user['id'],'del',$user['institution_id'],2);
  267. if(!$result){
  268. $this->throwError('权限未分配,请联系管理员',0005);
  269. }
  270. $data = [
  271. 'doctor_id'=>$user['id'],
  272. 'exam_id'=>$params['id'],
  273. 'study_id'=>$params['study_id']
  274. ];
  275. $this->examDao->insertDcmDel($data);
  276. $data = $this->examDao->del_exam($params['id']);
  277. $study = $params['study_id'];
  278. $institution = $params['institution_id'];
  279. $query = Config::get('query_url');
  280. $url = $query."/delete/study?key=ZSKK_DELETE&address=&study_id=$study&institution_id=$institution";
  281. $this->curl_get_time1($url);
  282. return $data;
  283. }
  284. function curl_get_time1($url){
  285. $header = array(
  286. 'Accept: application/json',
  287. );
  288. $curl = curl_init();
  289. //设置抓取的url
  290. curl_setopt($curl, CURLOPT_URL, $url);
  291. //设置头文件的信息作为数据流输出
  292. curl_setopt($curl, CURLOPT_HEADER, 0);
  293. // 超时设置,以秒为单位
  294. curl_setopt($curl, CURLOPT_TIMEOUT, 1);
  295. // 设置请求头
  296. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  297. //设置获取的信息以文件流的形式返回,而不是直接输出。
  298. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  299. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  300. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  301. //执行命令
  302. $data = curl_exec($curl);
  303. return $data;
  304. }
  305. public function getSyncAi($params)
  306. {
  307. $where[] = ['exam_id','in',explode(',',$params['exam_id'])];
  308. $ids = $this->examDao->getSyncAi($where);
  309. return $ids;
  310. }
  311. public function getAiNode($params)
  312. {
  313. if(empty($params['study_id'] ?? ''))
  314. {
  315. return [];
  316. }
  317. $where = ['study_id'=>$params['study_id']];
  318. $node = $this->examDao->getAiNode($where);
  319. if(empty($node))
  320. {
  321. return [];
  322. }
  323. return $node;
  324. }
  325. public function getHistory($params)
  326. {
  327. $id = $params['id'] ?? '';
  328. if(empty($id))
  329. {
  330. $this->throwError('参数错误,请联系管理员','0013');
  331. }
  332. $exam = $this->examDao->getExamData($id);
  333. if(empty($exam))
  334. {
  335. $this->throwError('参数错误,请联系管理员','0014');
  336. }
  337. $ins = $this->examDao->getInsInfo($exam['institution_id']);
  338. if(empty($ins['interconnected_exam_ins']))
  339. {
  340. $insIds = $exam['institution_id'];
  341. }else{
  342. $insIds = explode(',',$ins['interconnected_exam_ins']);
  343. $insIds[] = $exam['institution_id'];
  344. }
  345. if(empty($ins['interconnected_ins_field']))
  346. {
  347. $interconnected_ins_field = ['phone','card_num'];
  348. }else{
  349. $interconnected_ins_field = explode(',',$ins['interconnected_ins_field']);
  350. }
  351. $where = [];
  352. foreach ($interconnected_ins_field as $v)
  353. {
  354. if(!empty($exam[$v]))
  355. {
  356. $where[$v] = $exam[$v];
  357. }
  358. }
  359. $data = $this->examDao->getHistoryList($exam['institution_id'],$insIds,$where,$params,$exam['id']);
  360. $custom_field = $data['custom_field'];
  361. $custom = [];
  362. if($custom_field !== '')
  363. {
  364. $custom[] = ['props'=>'local','name'=>'本院标识'];
  365. $custom[] = ['props'=>'institution_name','name'=>'医院名称'];
  366. $field = explode(',',$custom_field);
  367. foreach ($field as $k=>$v)
  368. {
  369. if(!(Config::get('ins_custom_field')[$v] ?? null))
  370. {
  371. continue;
  372. }
  373. $custom[] = ['props'=>$v,'name' => Config::get('ins_custom_field')[$v]];
  374. }
  375. }
  376. $data['custom_field'] = $custom;
  377. return $data;
  378. }
  379. }