ButtService.php 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  1. <?php
  2. namespace app\api\servies\butt;
  3. use app\api\dao\applets\AppletsDao;
  4. use app\api\dao\report\ReportDao;
  5. use app\api\dao\register\RegisterDao;
  6. use app\api\model\debug\DebugModel;
  7. use app\api\servies\application\ApplicationService;
  8. use app\api\servies\report\ReportService;
  9. use app\api\servies\ZskkDefaultService;
  10. use app\api\dao\butt\ButtDao;
  11. use app\api\utils\UUIDUtils;
  12. use app\common\library\Gm;
  13. use app\common\library\PublicPhone;
  14. use app\common\library\send_message;
  15. use think\Db;
  16. use think\Exception;
  17. use think\facade\Config;
  18. use think\facade\Log;
  19. use think\exception\DbException;
  20. /**
  21. * 后台控制器基类
  22. * 接口方法权限 必传参数 接口返回 错误抛出 通用参数处理
  23. */
  24. class ButtService extends ZskkDefaultService {
  25. protected $logName = "ButtService";
  26. private $butt = null;
  27. private $applets = null;
  28. private $application = null;
  29. private $reportDao = null;
  30. private $registerDao = null;
  31. public function __construct(ButtDao $buttDao,AppletsDao $appletsDao,ApplicationService $application,ReportDao $reportDao,RegisterDao $registerDao) {
  32. parent::__construct();
  33. $this->butt = $buttDao;
  34. $this->applets = $appletsDao;
  35. $this->application = $application;
  36. $this->reportDao = $reportDao;
  37. $this->registerDao = $registerDao;
  38. }
  39. public function save_report($params)
  40. {
  41. // try{
  42. $time1 = time();
  43. Log::record('当前传参为'.json_encode($params));
  44. // Db::startTrans();
  45. if(isset($params['type']) && !empty($params['type']))
  46. {
  47. if(empty(Config::get('report_save_type')[$params['type']]))
  48. {
  49. $this->throwError('非规则内的type值',0002);
  50. }
  51. if(empty($params['code']) || empty($params['institution_id']))
  52. {
  53. $this->throwError('参数错误',0003);
  54. }
  55. $search = [Config::get('report_save_type')[$params['type']]=>$params['code']];
  56. $exams = $this->butt->getExamBySearch($search,$params['institution_id']);
  57. // if(count($exams) !== 1)
  58. // {
  59. // $this->throwError('存在复数的检查,请联系管理员',0004);
  60. // }
  61. // $exam = $exams[0];
  62. }else{
  63. $exam = $this->butt->getExamById($params['exam_id']);
  64. $exams[] = $exam;
  65. }
  66. if(empty($exams))
  67. {
  68. $this->throwError('找不到检查',0004);
  69. }
  70. $id = '';
  71. foreach ($exams as $exam)
  72. {
  73. // if($exam['exam_status'] != '3'){
  74. // $this->throwError('报告重复','0999');
  75. // }
  76. $reportDoctor = $this->getReportDoctor($params['report_doctor_name'],$exam['institution_id']);
  77. $reviewDoctor = $this->getReportDoctor($params['review_doctor_name'],$exam['institution_id']);
  78. $confirmDoctor = $this->getReportDoctor($params['confirm_doctor_name'],$exam['institution_id']);
  79. $report = [
  80. 'id'=>UUIDUtils::uuid(),
  81. 'exam_id'=>$exam['id'],
  82. 'report_doctor_id'=>$reportDoctor,
  83. 'report_doctor_name'=>$params['report_doctor_name'],
  84. 'report_datetime'=>$params['report_datetime'],
  85. 'review_doctor_id'=>$reviewDoctor,
  86. 'review_doctor_name'=>$params['review_doctor_name'],
  87. 'review_datetime'=>$params['review_datetime'],
  88. 'confirm_doctor_id'=>$confirmDoctor,
  89. 'confirm_doctor_name'=>$params['confirm_doctor_name'],
  90. 'confirm_datetime'=>$params['confirm_datetime'],
  91. 'description'=>$params['description'],
  92. 'impression'=>$params['impression'],
  93. 'report_result'=>$params['report_result'],
  94. 'type'=>1
  95. ];
  96. $id = $report['id'];
  97. $report_id = $this->butt->checkReport($exam['id']);
  98. if(!empty($report_id)){
  99. unset($report['id']);
  100. $id = $report_id;
  101. $this->butt->saveReport($report,$report_id);
  102. $report['id'] = $report_id;
  103. }else{
  104. $this->butt->saveReport($report);
  105. }
  106. $url = 'https://wechat.pacsonline.cn/wx_patient/api/unifyGetWxQrcode?reportId='.$id.'&version=3';
  107. // exec("nohup /bin/curl '$url' > /dev/null &");
  108. // $this->curl_get($url);
  109. $examData = [
  110. 'exam_status'=>9,
  111. 'application_department'=>$params['application_department'] ?? null,
  112. 'application_doctor'=>$params['application_doctor'] ?? null,
  113. 'clin_diag'=>$params['clin_diag'] ?? null,
  114. 'clin_symp'=>$params['clin_symp'] ?? null,
  115. 'name'=>$params['name'],
  116. 'report_result'=>$params['report_result'] ?? ''
  117. ];
  118. if(isset($params['patient_source']) && !empty($params['patient_source']))
  119. {
  120. $examData['patient_source'] = $params['patient_source'];
  121. }
  122. if(isset($params['hopitalized_no']) && !empty($params['hopitalized_no']))
  123. {
  124. $examData['hopitalized_no'] = $params['hopitalized_no'];
  125. }
  126. if(isset($params['out_patient']) && !empty($params['out_patient']))
  127. {
  128. $examData['out_patient'] = $params['out_patient'];
  129. }
  130. if(isset($params['his_patient_id']) && !empty($params['his_patient_id']))
  131. {
  132. $examData['his_patient_id'] = $params['his_patient_id'];
  133. }
  134. $patient = [];
  135. if(isset($params['exam_class']) && !empty($params['exam_class']))
  136. {
  137. $examData['exam_class'] = $params['exam_class'];
  138. if($params['institution_id'] == '16100014')
  139. {
  140. if($examData['exam_class' == 'RF'])
  141. {
  142. $examData['exam_class'] = 'DX';
  143. }
  144. }
  145. }
  146. if(isset($params['phone']) && !empty($params['phone']))
  147. {
  148. $examData['phone'] = $params['phone'];
  149. $patient['phone'] = $params['phone'];
  150. }
  151. if(isset($params['card_num']) && !empty($params['card_num']))
  152. {
  153. $examData['card_num'] = $params['card_num'];
  154. $patient['card_num'] = $params['card_num'];
  155. }
  156. if(isset($params['exam_project']) && !empty($params['exam_project']))
  157. {
  158. $examData['exam_project'] = $params['exam_project'];
  159. $examData['body_part_text'] = $this->butt->getBodyByProject($params['exam_project']);
  160. }
  161. if(isset($params['exam_method']) && !empty($params['exam_method']))
  162. {
  163. $examData['exam_method'] = $params['exam_method'];
  164. }
  165. //medical_code 国家医保信息平台人员管理码
  166. //body_part_code 检查部位代码
  167. //device_code 医保医用设备代码
  168. $examData['medical_code'] = $params['medical_code'] ?? str_pad(0, 26, "0", STR_PAD_LEFT);
  169. $examData['body_part_code'] = $params['body_part_code'] ?? str_pad(0, 9, "0", STR_PAD_LEFT);
  170. $examData['device_code'] = $params['device_code'] ?? str_pad(0, 13, "0", STR_PAD_LEFT);
  171. $study = $this->butt->getStudyByExamId($exam['id']);
  172. if(!empty($study['studytime'] ?? ''))
  173. {
  174. $studyDate = $study['studytime'];
  175. $examData['exam_datetime_code'] = $exam['exam_datetime'].substr($studyDate, 0, 4);
  176. }
  177. $this->butt->updateExam($examData,$exam['id']);
  178. PublicPhone::savePublicSearch($params['phone'] ?? '',$exam['id'] ?? '');
  179. $patientId = $exam['patient_id'];
  180. $patient['name'] = $params['name'];
  181. $this->butt->updatePatient($patient,$patientId);
  182. $institution = $this->butt->getIns($exam['institution_id']);
  183. Log::record('当前医院是否发送短信'.$institution['patient_sms']);
  184. $url = "http://111.53.165.106:9605/zskk/patient/syncExams";
  185. $data = [
  186. 'studyUid'=>$exam['STUDYUID'],
  187. 'study_id'=>$exam['STUDY_ID']
  188. ];
  189. $this->curlPost($url,$data);
  190. // if($institution['patient_sms'] == 1)
  191. // {
  192. // log::record('可以发送短信,查看检查id'.$exam['id'].'查看study是'.$exam['study_id']);
  193. // $telSend = $this->butt->getHistorySend($exam['id'],$exam['study_id']);
  194. // if(empty($telSend))
  195. // {
  196. // log::record('手机号'.($params['phone'] ?? $exam['phone']),'没有发送过短信');
  197. // // 为空 没有发送过短信
  198. // $this->sendSms(($params['phone'] ?? $exam['phone']),$exam['id'],$exam['study_id'],$exam['institution_id'],3,($params['exam_class'] ?? $exam['exam_class']));
  199. // }
  200. // }
  201. // if(in_array($exam['institution_id'],['13000003','04500002']))
  202. // {
  203. // $newExam = $this->butt->getExamById($exam['id']);
  204. // $this->saveHrData($newExam,$report,$institution);
  205. // }
  206. }
  207. // DB::commit();
  208. $time2 = time();
  209. $time = $time2-$time1;
  210. //$this->butt->insertSpecialLog($time,'butt/saveReport');
  211. return $id;
  212. // } catch (\Exception $exception){
  213. // DB::rollback();
  214. // $this->throwError($exception->getMessage(),0001);
  215. // }
  216. }
  217. public function saveHrData($exam, $report,$institution)
  218. {
  219. $hrItems = $this->butt->getHrItems($exam['exam_project']);
  220. if(empty($hrItems))
  221. {
  222. return '';
  223. }
  224. if($this->getCache('hrToken'))
  225. {
  226. // 已获取过登陆 todo
  227. $return = $this->getCache('hrToken');
  228. }else{
  229. $return = $this->getHrToken();
  230. if($return == 'error')
  231. {
  232. // 错误 todo
  233. return 'error';
  234. }
  235. }
  236. $header = array ("Content-Type: application/json;charset=utf-8","Authorization: Bearer $return");
  237. $arr = $this->makeHrData($exam,$report,$institution,$hrItems);
  238. $hrInfo = $this->request_by_curl("https://8001.pacsonline.cn/zskk/api/postExamReport",json_encode($arr),$header);
  239. }
  240. public function makeHrData($exam, $report,$institution,$hrItems)
  241. {
  242. switch ($exam['sex'])
  243. {
  244. case 'M':
  245. $sex = '1';
  246. break;
  247. case 'F':
  248. $sex = '2';
  249. break;
  250. case 'O':
  251. $sex = '9';
  252. break;
  253. default:
  254. $sex = '0';
  255. break;
  256. }
  257. $arrA = [
  258. 'AA'=>$exam['name'], // 姓名
  259. 'AB'=>'01', // 证件类型编码
  260. 'AC'=>$exam['card_num'], // 证件号码
  261. 'AD'=>$sex, // 性别编码
  262. 'AE'=>$exam['birthday'], // 出生日期
  263. ];
  264. switch ($exam['patient_source'])
  265. {
  266. case '1':
  267. $source = '2';
  268. break;
  269. case '2':
  270. $source = '3';
  271. break;
  272. case '3':
  273. $source = '1';
  274. break;
  275. case '4':
  276. $source = '4';
  277. break;
  278. default:
  279. $source = '';
  280. break;
  281. }
  282. $arrB = [
  283. 'BA'=>$source, //就诊类型编码 1 门诊 2 急诊 3 住院 4 体检 9 其他
  284. 'BB'=>$exam['patient_num'], //就诊卡号
  285. 'BC'=>$exam['patient_num'], //就诊号
  286. 'BD'=>$exam['institution_id'], //医院主索引
  287. 'BE'=>$exam['exam_datetime'], //就诊时间 todo
  288. 'BF'=>$exam['application_department'], //就诊科室名称
  289. 'BG'=>$exam['clin_symp'], //主诉 使用症状
  290. 'BH'=>$exam['age'], //年龄
  291. 'BI'=>$exam['institution_id'], //机构编码
  292. 'BJ'=>$institution['name'], //机构名称
  293. 'BK'=>'', //诊断编码
  294. 'BL'=>'', //DIAGNOSENAME
  295. 'BM'=>$exam['application_doctor'], //DOCTORNAME
  296. 'BN'=>$exam['application_doctor'], //DOCTORCODE
  297. ];
  298. switch ($report['report_result'])
  299. {
  300. case '1':
  301. $result = '1';
  302. break;
  303. case '2':
  304. $result = '2';
  305. break;
  306. default:
  307. $result = '9';
  308. break;
  309. }
  310. $arrC = [
  311. 'CA'=>$exam['study_id'], //申请单编号/医嘱编号
  312. 'CB'=>$report['id'], //报告单编号
  313. 'CC'=>$exam['exam_class'], //检查类型
  314. 'CD'=>$exam['exam_project'], //检查项目名称 todo
  315. 'CE'=>$hrItems['code'], //检查项目编码
  316. 'CF'=>$exam['body_part_text'], //检查部位
  317. 'CG'=>$report['description'], //检查所见/影像所见
  318. 'CH'=>$report['impression'], //影像诊断/意见
  319. 'CI'=>$result, //检查结果类别
  320. 'CJ'=>'', //互认范围
  321. 'CK'=>$hrItems['code'], //互认项目编码
  322. 'CL'=>$institution['name'], //报告机构
  323. 'CM'=>$exam['institution_id'], //报告机构编码
  324. 'CN'=>'影像科', //报告科室名称
  325. 'CO'=>$report['report_doctor_name'], //报告医生姓名
  326. 'CP'=>$report['review_doctor_name'], //审核医师姓名
  327. 'CQ'=>$report['report_datetime'], //报告时间
  328. 'CR'=>$exam['exam_datetime'], //检查时间
  329. 'CS'=>'', //报告单内容
  330. 'CT'=>$hrItems['name'], //互认项目
  331. ];
  332. $arr = [
  333. 'A'=>$arrA,
  334. 'B'=>$arrB,
  335. 'C'=>[$arrC]
  336. ];
  337. $key = 'zLxapoeqWYpoeqWY';
  338. $json = base64_encode(json_encode($arr));
  339. $jm = Gm::encrypt($key,$json);
  340. return ['data'=>$jm];
  341. }
  342. public function getHrToken()
  343. {
  344. $clientid = '12371600494581158K';
  345. $clientsecret = '837ffb03803b72cd99a941770fb6b2be';
  346. $arr = ['request_time'=>time(),'request_no'=>time(),'data'=>['clientid'=>$clientid,'clientsecret'=>$clientsecret]];
  347. $return = $this->request_by_curl("https://8001.pacsonline.cn/zskk/api/getToken",json_encode($arr));
  348. $return = json_decode($return,true);
  349. if($return['code'] == 1)
  350. {
  351. // 请求成功
  352. $this->setCache('hrToken',$return['data']['token'],3500);
  353. return $return['data']['token'];
  354. }else{
  355. return 'error';
  356. }
  357. }
  358. public function sendSms($phone,$examId,$studyId,$institution,$source,$exam_class='')
  359. {
  360. $data = $this->butt->getInsById($institution);
  361. $name = $data['name'];
  362. if(!empty($data['filter']))
  363. {
  364. $filter = json_decode($data['filter'],true);
  365. if(!empty($filter) && !empty($filter['exam_class'] ?? ''))
  366. {
  367. $class = explode(',',$filter['exam_class']);
  368. if(in_array($exam_class,$class))
  369. {
  370. return '';
  371. }
  372. }
  373. }
  374. $code = rand(0,9).rand(0,9).rand(0,9).rand(0,9);
  375. $id = UUIDUtils::uuid();
  376. if(empty($phone)){
  377. $send_message = 0;
  378. }else{
  379. //发送短信
  380. // $send = send_message::sendExam2Patient($phone,$code,$name,$id);
  381. $note = [
  382. 'long_url'=>'https://wc.pacsonline.cn/#/jump?id='.$id
  383. ];
  384. $noteId = $this->reportDao->insertNote($note);
  385. $nid = base64_encode(str_pad(base_convert($noteId,10,36),6,'0',STR_PAD_LEFT));
  386. if($institution == '43600001')
  387. {
  388. if($exam_class == 'US'){
  389. return '';
  390. }
  391. $send = send_message::sendEZShortMessage($phone,$code,$nid);
  392. }elseif($institution == '03060002'){
  393. $send = send_message::sendReportShortMessage($phone,$name);
  394. }else{
  395. $send = send_message::sendExam2ShortPatient($phone,$code,$name,$nid);
  396. }
  397. $data = json_decode(json_encode($send),true);
  398. if($data['Code'] == 'OK'){
  399. $send_message = 1;
  400. }else{
  401. $send_message = 0;
  402. }
  403. $share = [
  404. 'id'=>$id,
  405. 'examId'=>$examId,
  406. 'status'=>1,
  407. 'overdueTime'=>5475,
  408. 'code'=>$code,
  409. 'short_url'=>'',
  410. 'phone'=>$phone,
  411. 'report_isset'=>1,
  412. 'study_id'=>$studyId,
  413. 'source'=>$source,
  414. 'send_message'=>$send_message,
  415. ];
  416. $this->reportDao->insertShare($share);
  417. }
  418. }
  419. public function save_exam($params)
  420. {
  421. try{
  422. $exam = $this->butt->getExamById($params['exam_id']);
  423. if($exam['exam_status'] != '3'){
  424. $this->throwError('报告重复','0999');
  425. }
  426. // $reportDoctor = $this->getReportDoctor($params['report_doctor_name'],$exam['institution_id']);
  427. // $reviewDoctor = $this->getReportDoctor($params['review_doctor_name'],$exam['institution_id']);
  428. // $confirmDoctor = $this->getReportDoctor($params['confirm_doctor_name'],$exam['institution_id']);
  429. // $report = [
  430. // 'id'=>UUIDUtils::uuid(),
  431. // 'exam_id'=>$params['exam_id'],
  432. // 'report_doctor_id'=>$this->butt->getDcotorId($params['report_doctor_name'],$exam['institution_id']),
  433. // 'report_doctor_name'=>$params['report_doctor_name'],
  434. // 'report_datetime'=>$params['report_datetime'],
  435. // 'review_doctor_id'=>$this->butt->getDcotorId($params['review_doctor_name'],$exam['institution_id']),
  436. // 'review_doctor_name'=>$params['review_doctor_name'],
  437. // 'review_datetime'=>$params['review_datetime'],
  438. // 'confirm_doctor_id'=>$this->butt->getDcotorId($params['confirm_doctor_name'],$exam['institution_id']),
  439. // 'confirm_doctor_name'=>$params['confirm_doctor_name'],
  440. // 'confirm_datetime'=>$params['confirm_datetime'],
  441. // 'description'=>$params['description'],
  442. // 'impression'=>$params['impression'],
  443. // 'report_result'=>$params['report_result'],
  444. // 'type'=>1
  445. // ];
  446. // $id = $report['id'];
  447. // $report_id = $this->butt->checkReport($params['exam_id']);
  448. // if(!empty($report_id)){
  449. // unset($report['id']);
  450. // $this->butt->saveReport($report,$report_id);
  451. // }else{
  452. // $this->butt->saveReport($report);
  453. // }
  454. // $url = 'http://wechat.pacsonline.cn/wx_patient/api/unifyGetWxQrcode?reportId='.$id.'&version=3';
  455. // $this->curl_get($url);
  456. $params['phone'] = trim($params['phone']);
  457. $examData = [
  458. 'application_department'=>$params['application_department'] ?? null,
  459. 'application_doctor'=>$params['application_doctor'] ?? null,
  460. 'clin_diag'=>$params['clin_diag'] ?? null,
  461. 'clin_symp'=>$params['clin_symp'] ?? null,
  462. 'name'=>$params['name'],
  463. 'phone'=>$params['phone'],
  464. 'card_num'=>$params['card_num'] ?? null,
  465. 'patient_source'=>$params['patient_source'] ?? null
  466. ];
  467. if(isset($params['exam_project']) && !empty($params['exam_project'])){
  468. $examData['exam_project'] = $params['exam_project'];
  469. $examData['body_part_text'] = $this->butt->getBodyByProject($params['exam_project']);
  470. }
  471. if(isset($params['device_name']) && !empty($params['device_name'])){
  472. $examData['device_name'] = $params['device_name'];
  473. }
  474. if(isset($params['exam_class']) && !empty($params['exam_class']))
  475. {
  476. $examData['exam_class'] = $params['exam_class'];
  477. }
  478. if(isset($params['hopitalized_no']) && !empty($params['hopitalized_no']))
  479. {
  480. $examData['hopitalized_no'] = $params['hopitalized_no'];
  481. }
  482. if(isset($params['out_patient']) && !empty($params['out_patient']))
  483. {
  484. $examData['out_patient'] = $params['out_patient'];
  485. }
  486. if(isset($params['his_patient_id']) && !empty($params['his_patient_id']))
  487. {
  488. $examData['his_patient_id'] = $params['his_patient_id'];
  489. }
  490. if(isset($params['exam_method']) && !empty($params['exam_method']))
  491. {
  492. $examData['exam_method'] = $params['exam_method'];
  493. }
  494. $examData['sync_exam'] = 1;
  495. $this->butt->updateExam($examData,$params['exam_id']);
  496. $institution = $this->butt->getIns($exam['institution_id']);
  497. Log::record('当前医院是否发送短信'.$institution['patient_sms']);
  498. if($institution['patient_sms'] == 1)
  499. {
  500. // log::record('可以发送短信,查看检查id'.$exam['id'].'查看study是'.$exam['study_id']);
  501. // $telSend = $this->butt->getHistorySend($exam['id'],$exam['study_id']);
  502. // if(empty($telSend))
  503. // {
  504. // log::record('手机号'.$params['phone'],'没有发送过短信');
  505. // // 为空 没有发送过短信
  506. // $this->sendSms($params['phone'],$exam['id'],$exam['study_id'],$exam['institution_id'],2);
  507. // }
  508. }
  509. $patientId = $exam['patient_id'];
  510. $patient = [
  511. 'name'=>$params['name'],
  512. 'phone'=>$params['phone'],
  513. 'card_num'=>$params['card_num'] ?? null
  514. ];
  515. $this->butt->updatePatient($patient,$patientId);
  516. return $params['exam_id'];
  517. } catch (DbException $exception){
  518. $this->throwError($exception->getMessage(),0001);
  519. }
  520. }
  521. public function save_register($params)
  522. {
  523. try{
  524. $field = [
  525. 'application_department',
  526. 'application_doctor',
  527. 'clin_diag',
  528. 'clin_symp',
  529. 'name',
  530. 'phone',
  531. 'card_num',
  532. 'patient_source',
  533. 'exam_project',
  534. 'exam_class',
  535. 'hopitalized_no',
  536. 'out_patient',
  537. 'his_patient_id',
  538. 'exam_method',
  539. 'studyuid',
  540. 'accession_num',
  541. 'patient_num',
  542. 'institution_id',
  543. 'age',
  544. 'exam_datetime'
  545. ];
  546. $examData = $this->makeRegisterExamData($params,$field);
  547. $examData['id'] = UUIDUtils::uuid();
  548. $examData['patient_id'] = UUIDUtils::uuid();
  549. if(isset($params['is_dcm']) && $params['is_dcm'] == 2)
  550. {
  551. $examData['is_dcm'] = 2;
  552. }
  553. $this->butt->insertExam($examData);
  554. $patient = [
  555. 'id'=>$examData['patient_id'],
  556. 'name'=>$params['name'],
  557. 'phone'=>$params['phone'] ?? null,
  558. 'card_num'=>$params['card_num'] ?? null,
  559. 'temp_patient_id'=>$params['patient_num'] ?? null,
  560. 'institution_id'=>$params['institution_id'],
  561. 'age'=>$params['age'] ?? null
  562. ];
  563. $this->butt->insertPatient($patient);
  564. return $examData['id'];
  565. } catch (DbException $exception){
  566. $this->throwError($exception->getMessage(),0001);
  567. }
  568. }
  569. public function saveFiles($params)
  570. {
  571. $fileData = [];
  572. if(isset($params['data']) && !empty($params['data']))
  573. {
  574. $data = [
  575. 'exam_id'=>$params['id']
  576. ];
  577. $url = json_decode($params['data'],true);
  578. foreach ($url as $v)
  579. {
  580. $data['type']=$v['type'];
  581. $data['url']=$v['url'];
  582. $fileData[] = $data;
  583. }
  584. }
  585. if(empty($fileData))
  586. {
  587. return '';
  588. }
  589. $return = $this->butt->saveFileData($fileData);
  590. $this->butt->updateExam(['exam_status'=>3],$params['id']);
  591. return $return;
  592. }
  593. public function makeRegisterExamData($data,$field)
  594. {
  595. $examData = [];
  596. foreach ($field as $v)
  597. {
  598. if(isset($data[$v]) && !empty($data[$v]))
  599. {
  600. $examData[$v] = $data[$v];
  601. }
  602. }
  603. return $examData;
  604. }
  605. public function getReportDoctor($name,$institution)
  606. {
  607. if($name == ''){
  608. return '';
  609. }
  610. $id = $this->butt->getDcotorId($name,$institution);
  611. if(empty($id)){
  612. $doctor = [
  613. 'id'=>UUIDUtils::uuid(),
  614. 'realname'=>$name,
  615. 'institution_id'=>$institution,
  616. 'password'=>md5('zskk1234'),
  617. 'username'=>'none'
  618. ];
  619. $this->butt->makeDcotor($doctor);
  620. return $doctor['id'];
  621. }
  622. return $id;
  623. }
  624. public function getExam($params)
  625. {
  626. $timeWhere = '';
  627. if(isset($params['createdAt']) && !empty($params['createdAt']))
  628. {
  629. $time = explode(',',$params['createdAt']);
  630. $timeWhere = "createdAt between '$time[0]' and '$time[1]'";
  631. }
  632. if(isset($params['exam_status']) && !empty($params['exam_status']))
  633. {
  634. $status = explode(',',$params['exam_status']);
  635. }else{
  636. $status = [3];
  637. }
  638. $where = [];
  639. if(isset($params['is_sync']) && $params['is_sync'] == '0')
  640. {
  641. $where = ['sync_exam'=>'0'];
  642. }
  643. $data = $this->butt->getExams($params['institution_id'],$params['num'],$where,$timeWhere,$status);
  644. return $data;
  645. }
  646. public function getReports($params)
  647. {
  648. $timeWhere = '';
  649. if(isset($params['date']) && !empty($params['date']))
  650. {
  651. $time = explode(',',$params['date']);
  652. $timeWhere = "confirm_datetime between '$time[0]' and '$time[1]'";
  653. }
  654. $data = $this->butt->getReports($params['institution_id'],$params['page'],$params['num'],$timeWhere);
  655. foreach ($data as $k=>$v)
  656. {
  657. $data[$k]['impression'] = strip_tags($v['impression']);
  658. $data[$k]['description'] = strip_tags($v['description']);
  659. }
  660. return $data;
  661. }
  662. public function getConfirmExams($params)
  663. {
  664. $data = $this->butt->getConfirmExams($params['institution_id'],$params['num']);
  665. return $data;
  666. }
  667. public function sendDingDing()
  668. {
  669. $time = time()* 1000;
  670. $secret = 'SECc2b5ad43721741fe16f6ac39ed423b047668fd691c01f9a051037bf4733a09e8';
  671. $m = $time."\n".$secret;
  672. $s = hash_hmac('sha256', $m, $secret, true);
  673. $a = base64_encode($s);
  674. $b = urlencode($a);
  675. $dingurl = Config::get('dingding_url');
  676. $url =$dingurl."/robot/send?access_token=2030ac71bf6811a7caad9a34899e686df9bef4e45d1c990b14339ba65c333776&timestamp=$time&sign=$b";
  677. $message = '';
  678. $user_wechat = $this->butt->getUserWechat();
  679. $message .= "累计公众号关注人数:".$user_wechat['all'];
  680. $yestWechat = $this->butt->getYestNumber(); //获取昨日的已关注数量
  681. if((int)$user_wechat['now'] >= (int)$yestWechat){
  682. $user_different = "增加".((int)$user_wechat['now']-(int)$yestWechat).'人';
  683. }else{
  684. $user_different = "减少".((int)$yestWechat-(int)$user_wechat['now']).'人';
  685. }
  686. $message .= "\n公众号实时关注总人数为".$user_wechat['now'].'人';
  687. $message .= "\n公众号实时关注人数较昨日".$user_different;
  688. $exams = $this->butt->getExamData();
  689. $message .= "\n昨日新增数字胶片量:".$exams['yest']."张";
  690. // $message .= "\n昨日财务流水:".($exams['yest']*20);
  691. $message .= "\n累计数字胶片量:".$exams['all']."张";
  692. $message .= "\n胶片增长率:".$exams['percentage']."%";
  693. $picNumber = $this->butt->getPicNumber();
  694. $message .= "\n昨日查阅数字胶片量:".$picNumber['yestAll']."次(昨日患者共计查阅胶片次数)";
  695. $message .= "\n昨日新增查阅数字胶片量:".$picNumber['yest']."次(去重查阅)";
  696. $institutionInfo = $this->butt->getInsInfo();
  697. $message .= "\n新增医院数:".$institutionInfo['yest']."家 ";
  698. // 新增医院名称
  699. if($institutionInfo['yest'] > 0){
  700. foreach ($institutionInfo['name'] as $k=>$v){
  701. $message .= $v['name'].' ';
  702. }
  703. }
  704. $message .= "\n累计医院数:".$institutionInfo['all']."家";
  705. $ins_all_info = $this->butt->getAllInsInfo();
  706. $b = $ins_all_info['patient_num'];
  707. $d = $ins_all_info['accession_num'];
  708. $e = $ins_all_info['phone'];
  709. $f= $ins_all_info['card_num'];
  710. $g = $ins_all_info['hopitalized_no'];
  711. $c = [];
  712. foreach ($b as $k=>$v)
  713. {
  714. if(isset($c[$v['institution_id']]['num']) && !empty($c[$v['institution_id']]['num']))
  715. {
  716. $c[$v['institution_id']]['num']++;
  717. }else{
  718. $c[$v['institution_id']]['num'] = 1;
  719. $c[$v['institution_id']]['name'] = $v['name'];
  720. $c[$v['institution_id']]['code'] = $v['patient_num'];
  721. }
  722. }
  723. foreach ($d as $k=>$v)
  724. {
  725. if(isset($c[$v['institution_id']]['code']) && $c[$v['institution_id']]['code'] == $v['accession_num']){
  726. continue;
  727. }
  728. if(isset($c[$v['institution_id']]['num']) && !empty($c[$v['institution_id']]['num']))
  729. {
  730. $c[$v['institution_id']]['num']++;
  731. }else{
  732. $c[$v['institution_id']]['num'] = 1;
  733. $c[$v['institution_id']]['name'] = $v['name'];
  734. $c[$v['institution_id']]['code'] = $v['accession_num'];
  735. }
  736. }
  737. foreach ($e as $k=>$v)
  738. {
  739. if(isset($c[$v['institution_id']]['code']) && $c[$v['institution_id']]['code'] == $v['phone']){
  740. continue;
  741. }
  742. if(isset($c[$v['institution_id']]['num']) && !empty($c[$v['institution_id']]['num']))
  743. {
  744. $c[$v['institution_id']]['num']++;
  745. }else{
  746. $c[$v['institution_id']]['num'] = 1;
  747. $c[$v['institution_id']]['name'] = $v['name'];
  748. $c[$v['institution_id']]['code'] = $v['phone'];
  749. }
  750. }
  751. foreach ($f as $k=>$v)
  752. {
  753. if(isset($c[$v['institution_id']]['code']) && $c[$v['institution_id']]['code'] == $v['card_num']){
  754. continue;
  755. }
  756. if(isset($c[$v['institution_id']]['num']) && !empty($c[$v['institution_id']]['num']))
  757. {
  758. $c[$v['institution_id']]['num']++;
  759. }else{
  760. $c[$v['institution_id']]['num'] = 1;
  761. $c[$v['institution_id']]['name'] = $v['name'];
  762. $c[$v['institution_id']]['code'] = $v['card_num'];
  763. }
  764. }
  765. foreach ($g as $k=>$v)
  766. {
  767. if(isset($c[$v['institution_id']]['code']) && $c[$v['institution_id']]['code'] == $v['hopitalized_no']){
  768. continue;
  769. }
  770. if(isset($c[$v['institution_id']]['num']) && !empty($c[$v['institution_id']]['num']))
  771. {
  772. $c[$v['institution_id']]['num']++;
  773. }else{
  774. $c[$v['institution_id']]['num'] = 1;
  775. $c[$v['institution_id']]['name'] = $v['name'];
  776. $c[$v['institution_id']]['code'] = $v['hopitalized_no'];
  777. }
  778. }
  779. $message .= "\n新增医院名称与每日每家新增量:";
  780. $i = 1;
  781. foreach ($c as $key => $row)
  782. {
  783. $volume[$key] = $row['num'];
  784. }
  785. array_multisort($volume, SORT_DESC, $c);
  786. foreach ($c as $k=>$v){
  787. if($i > 49)
  788. {
  789. continue;
  790. }
  791. $ins_name = $v['name'];
  792. $ins_num = $v['num'];
  793. if($i < 10)
  794. {
  795. $a = $i." ";
  796. }else{
  797. $a = $i;
  798. }
  799. $message .= "\n $a:$ins_name \n \t\t 新增胶片查阅量:$ins_num";
  800. $i++;
  801. }
  802. $data = array ('msgtype' => 'text','text' => array ('content' => $message));
  803. $data_string = json_encode($data);
  804. $result = $this->request_by_curl($url, $data_string);
  805. Log::record('钉钉返回'.$result);
  806. $message0 = '医院昨日检查/报告/报告率统计:';
  807. $allIns = $this->butt->getAllIns();
  808. $yestExams = $this->butt->getYestExams();
  809. $YestCompleteExams = $this->butt->getYestCompleteExams();
  810. foreach ($yestExams as $v)
  811. {
  812. $message0 .= "\n ".($allIns[$v['institution_id']] ?? '').':'.$v['c'].'/'.($YestCompleteExams[$v['institution_id']] ?? 0).'/'.(number_format(($YestCompleteExams[$v['institution_id']] ?? 0)/$v['c'],2)*100).'%';
  813. }
  814. $data0 = array ('msgtype' => 'text','text' => array ('content' => $message0));
  815. $data_string0 = json_encode($data0);
  816. $result = $this->request_by_curl($url, $data_string0);
  817. $ding = [
  818. 'wechat_bind_all'=>$user_wechat['all'],
  819. 'wechat_yestday_focus'=>$user_wechat['now'],
  820. 'exam_all'=>$exams['all'],
  821. 'exam_yestday_add'=>$exams['yest'],
  822. 'exam_add_percentage'=>$exams['percentage']."%",
  823. 'film_all'=>$exams['all'],
  824. 'film_yestday_add'=>$exams['yest'],
  825. 'film_add_percentage'=>$exams['percentage']."%",
  826. 'film_yest_look_all'=>$picNumber['yestAll'],
  827. 'film_yest_look_add'=>$picNumber['yest'],
  828. 'institution_all'=>$institutionInfo['all'],
  829. 'institution_add'=>$institutionInfo['yest'],
  830. ];
  831. $this->butt->insertDing($ding);
  832. return $result;
  833. }
  834. public function sendHosDing()
  835. {
  836. $time = time()* 1000;
  837. $secret = 'SECc2b5ad43721741fe16f6ac39ed423b047668fd691c01f9a051037bf4733a09e8';
  838. $m = $time."\n".$secret;
  839. $s = hash_hmac('sha256', $m, $secret, true);
  840. $a = base64_encode($s);
  841. $b = urlencode($a);
  842. $dingurl = Config::get('dingding_url');
  843. $url =$dingurl."/robot/send?access_token=2030ac71bf6811a7caad9a34899e686df9bef4e45d1c990b14339ba65c333776&timestamp=$time&sign=$b";
  844. $message = '';
  845. $institution_ids = $this->butt->getInsIds();
  846. $no_use = []; // 开通但是没有使用
  847. $use_his7 = []; //7天以上没有上传
  848. $use_his1 = []; //1天以上没有上传
  849. $exam7Out = []; //昨天上传了7天以外的数据
  850. foreach ($institution_ids as $k=>$v)
  851. {
  852. // $k 为医院id $v为名称
  853. // 获取检查最大时间
  854. $exam_time = $this->butt->getExamMaxTime($k);
  855. if(empty($exam_time[0]))
  856. {
  857. // 时间为空 代表医院未使用过数据
  858. $no_use[] = $v;
  859. }else {
  860. $diff = time() - strtotime($exam_time[0]);
  861. if ($diff > 86400*7){
  862. $array = [$v,$exam_time[0]];
  863. $use_his7[] = $array;
  864. }elseif($diff > 86400)
  865. {
  866. $array = [$v,$exam_time[0]];
  867. $use_his1[] = $array;
  868. }else{
  869. $examCount = $this->butt->get7OutExam($k);
  870. if($examCount > 0)
  871. {
  872. $exam7Out[] = [$v,$examCount];
  873. }
  874. }
  875. }
  876. }
  877. $count1 = count($no_use);
  878. $count2 = count($use_his7);
  879. $count3 = count($use_his1);
  880. $count4 = count($exam7Out);
  881. foreach ($no_use as $k=>$v)
  882. {
  883. if(empty($message))
  884. {
  885. $message = "已安装但是后续无使用记录医院共计".$count1."家,分别为:\n".($k+1).":".$v;
  886. }else{
  887. $message .= "\n".($k+1).":".$v;
  888. }
  889. }
  890. $data = array ('msgtype' => 'text','text' => array ('content' => $message));
  891. $data_string = json_encode($data);
  892. $result = $this->request_by_curl($url, $data_string);
  893. sleep(1);
  894. $message1 = '';
  895. foreach ($use_his7 as $k=>$v)
  896. {
  897. $create = explode(' ',$v[1]);
  898. if(empty($message1))
  899. {
  900. $message1 = "超过7天无使用记录的医院共计".$count2."家,分别为:\n".($k+1).":".$v[0].",最后上传时间为".$create[0];
  901. }else{
  902. $message1 .= "\n".($k+1).":".$v[0].",最后上传时间为".$create[0];
  903. }
  904. }
  905. $data1 = array ('msgtype' => 'text','text' => array ('content' => $message1));
  906. $data_string1 = json_encode($data1);
  907. $result1 = $this->request_by_curl($url, $data_string1);
  908. sleep(1);
  909. $message2 = '';
  910. foreach ($use_his1 as $k=>$v)
  911. {
  912. $create = explode(' ',$v[1]);
  913. if(empty($message2))
  914. {
  915. $message2 = "超过1天无使用记录的医院共计".$count3."家,分别为:\n".($k+1).":".$v[0].",最后上传时间为".$create[0];
  916. }else{
  917. $message2 .= "\n".($k+1).":".$v[0].",最后上传时间为".$create[0];
  918. }
  919. }
  920. $data2 = array ('msgtype' => 'text','text' => array ('content' => $message2));
  921. $data_string2 = json_encode($data2);
  922. $result2 = $this->request_by_curl($url, $data_string2);
  923. sleep(1);
  924. $all_message = "已安装但是后续无使用记录医院共计".$count1."家\n";
  925. $all_message .= "超过7天无使用记录的医院共计".$count2."家\n";
  926. $all_message .= "超过1天无使用记录的医院共计".$count3."家";
  927. $data3 = array ('msgtype' => 'text','text' => array ('content' => $all_message));
  928. $data_string3 = json_encode($data3);
  929. $result3 = $this->request_by_curl($url, $data_string3);
  930. sleep(1);
  931. $at = 1;
  932. if(!empty($exam7Out))
  933. {
  934. $message4 = '';
  935. foreach ($exam7Out as $k=>$v)
  936. {
  937. if($v[1] > 100)
  938. {
  939. $at = 2;
  940. }
  941. if(empty($message4))
  942. {
  943. $message4 = "昨日上传了7天以上的数据的医院共计".$count4."家,分别为:\n".($k+1).":".$v[0].",上传数量为".$v[1];
  944. }else{
  945. $message4 .= "\n".($k+1).":".$v[0].",上传数量为".$v[1];
  946. }
  947. }
  948. if($at == 1)
  949. {
  950. $data4 = array ('msgtype' => 'text','text' => array ('content' => $message4));
  951. }else{
  952. $data4 = array ('msgtype' => 'text','text' => array ('content' => $message4.' @18910065009'),'at'=>['atMobiles'=>['18910065009']]);
  953. }
  954. $data_string4 = json_encode($data4);
  955. $result4 = $this->request_by_curl($url, $data_string4);
  956. }
  957. return $result;
  958. }
  959. public function getInsNum()
  960. {
  961. return 441;
  962. $ins2 = $this->butt->get2Ins();
  963. $ins3 = $this->butt->get3Ins();
  964. $num = count($ins3);
  965. $a = 1;
  966. foreach ($ins2 as $k=>$v)
  967. {
  968. if(in_array($v['id'],$ins3)){
  969. continue;
  970. }else{
  971. $num++;
  972. }
  973. }
  974. return $num;
  975. }
  976. public function exam_search($params)
  977. {
  978. $study_id= $params['study_id'] ?? null;
  979. if(!$study_id)
  980. {
  981. return [];
  982. }
  983. $institution = $this->butt->getInstitutionId($study_id);
  984. $search = $this->getSearch($params['search'] ?? null);
  985. if(empty($search))
  986. {
  987. return [];
  988. }
  989. $exam = $this->butt->getExamBySearch($search,$institution);
  990. return $exam;
  991. }
  992. public function another_exam_search($params)
  993. {
  994. $study_id= $params['study_id'] ?? null;
  995. if(!$study_id)
  996. {
  997. return [];
  998. }
  999. $institutionId = $this->butt->getInstitutionId($study_id);
  1000. $where = [];
  1001. if($institutionId == '16100017') {
  1002. $name = $this->butt->getPatientNameByStudyId($study_id);
  1003. if(empty($name))
  1004. {
  1005. return [];
  1006. }
  1007. $where['institution_id'] = $institutionId;
  1008. $where['name'] = $name;
  1009. } else {
  1010. $patient = $this->butt->getPatientByStudyId($study_id);
  1011. if(empty($patient))
  1012. {
  1013. return [];
  1014. }
  1015. $where['patient_id'] = $patient;
  1016. }
  1017. $exam = $this->butt->getExamByPatient($where);
  1018. return $exam;
  1019. }
  1020. public function getSearch($search)
  1021. {
  1022. $search_array = '';
  1023. if(empty($search))
  1024. {
  1025. return [];
  1026. }
  1027. $search_array = "name='$search' or patient_num='$search' or accession_num='$search'";
  1028. return $search_array;
  1029. }
  1030. public function save_video_url($params)
  1031. {
  1032. if(empty($params['video_url']))
  1033. {
  1034. $this->throwError('无效的url地址',0045);
  1035. }
  1036. $data = [
  1037. 'video_url'=>$params['video_url'],
  1038. 'doctor_id'=>$params['user_id'],
  1039. 'image_url'=>$params['image_url'] ?? '',
  1040. 'video_type'=>$params['video_type'] ?? '0',
  1041. ];
  1042. if(isset($params['exam_id']) && !empty($params['exam_id']))
  1043. {
  1044. //存在exam_id 则为上传并保存接口
  1045. $data['exam_id'] = $params['exam_id'];
  1046. $data['status'] = 1;
  1047. }
  1048. $info = $this->butt->save_video_url($data);
  1049. return $info;
  1050. }
  1051. public function getWorkList($params)
  1052. {
  1053. $info = $this->butt->getWorkList();
  1054. foreach ($info as $k=>$v)
  1055. {
  1056. $sex = $this->handle_sex($v['Sex']);
  1057. $info[$k]['Sex'] = $sex;
  1058. $info[$k]['DateOfBirth'] = $this->handleTime($v['DateOfBirth']);
  1059. $info[$k]['ExamDateAndTime'] = $this->handleTime($v['ExamDateAndTime']);
  1060. }
  1061. return $info;
  1062. }
  1063. public function handleTime($time)
  1064. {
  1065. $t = strtotime($time);
  1066. $r = date('Y-m-d H:i:s',$t);
  1067. return $r;
  1068. }
  1069. public function handle_sex($sex)
  1070. {
  1071. switch ($sex){
  1072. case 'M':
  1073. case '男':
  1074. return 'M';
  1075. break;
  1076. case 'F':
  1077. case '女':
  1078. return 'F';
  1079. break;
  1080. default:
  1081. return 'O';
  1082. break;
  1083. }
  1084. }
  1085. public function getNotInsReport($param)
  1086. {
  1087. $where_status[] = ['exam_status','in',explode(',',$param['report_status'])];
  1088. $where_institution = ['institution_id'=>$param['institution_id']];
  1089. $time = explode(',',$param['time']);
  1090. $where_time = [$time[0],$time[1]];
  1091. $field = [
  1092. 'e.name',
  1093. 'e.sex',
  1094. 'phone',
  1095. 'card_num',
  1096. 'hopitalized_no',
  1097. 'out_patient',
  1098. 'accession_num',
  1099. 'patient_num',
  1100. 'studyuid',
  1101. 'his_patient_id',
  1102. 'impression',
  1103. 'description',
  1104. 'report_datetime',
  1105. 'report_doctor_name',
  1106. 'review_doctor_name',
  1107. 'review_datetime',
  1108. 'r.report_result'
  1109. ];
  1110. $data = $this->butt->getNotInsReport($where_status,$where_institution,$where_time,$field);
  1111. return $data;
  1112. }
  1113. public function setNameCache($params)
  1114. {
  1115. foreach ($params as $k=>$v)
  1116. {
  1117. // $cache = $this->butt->saveCache($v['INPATIENTNO'],$v['NAME']);
  1118. $key = "worklist_".$v['INPATIENTNO'];
  1119. $cache = $this->butt->saveCache($key, json_encode($v));
  1120. Log::record('存储结果'.$cache.';存储key为'.$key.';存储内容为'.$this->butt->getCache($key));
  1121. }
  1122. return true;
  1123. }
  1124. public function getRisVersion()
  1125. {
  1126. $data = $this->butt->getRisVersion();
  1127. $return = [];
  1128. foreach ($data as $k=>$v)
  1129. {
  1130. $new = 1;
  1131. $time = time()-strtotime($v['time']);
  1132. if($time < 86400*30)
  1133. {
  1134. $new = 2;
  1135. }
  1136. $name = $this->butt->getProjectName($v['project']);
  1137. $info = ['project'=>$v['project'],'new'=>$new,'name'=>$name];
  1138. $return[] = $info;
  1139. }
  1140. return $return;
  1141. }
  1142. public function getVersionInfo($project)
  1143. {
  1144. $info = $this->butt->getVersionInfo($project);
  1145. return $info;
  1146. }
  1147. public function getBindInfo($param)
  1148. {
  1149. if(!($param['institution_id'] ?? null) || !($param['start'] ?? null) || !($param['finish'] ?? null) )
  1150. {
  1151. $this->throwError('医院id不能为空','0081');
  1152. }
  1153. $ins = ['institution_id'=>$param['institution_id']];
  1154. $start = $param['start'].' 00:00:00';
  1155. $finish = $param['finish'].' 23:59:59';
  1156. $visit = $this->butt->getBindInfo($ins,$start,$finish);
  1157. $ids = [];
  1158. $cTime = [];
  1159. $type = [];
  1160. foreach ($visit as $k=>$v)
  1161. {
  1162. $ids[] = $v['exam_id'];
  1163. $cTime[$v['exam_id']] = $v['createdAt'];
  1164. if($type[$v['exam_id']] ?? null)
  1165. {
  1166. $type[$v['exam_id']] = $type[$v['exam_id']].','.$v['source'];
  1167. }else{
  1168. $type[$v['exam_id']] = $v['source'];
  1169. }
  1170. }
  1171. $list= $this->butt->getExamByIds($ids);
  1172. $data = [];
  1173. foreach ($list as $k=>$v)
  1174. {
  1175. $list[$k]['createdAt'] = $cTime[$v['id']];
  1176. $source = '';
  1177. if($type[$v['id']] ?? null)
  1178. {
  1179. if(strpos($type[$v['id']],'1') !== false)
  1180. {
  1181. if(empty($source))
  1182. {
  1183. $source = '1';
  1184. }else{
  1185. $source .= ',1';
  1186. }
  1187. }
  1188. if(strpos($type[$v['id']],'2') !== false)
  1189. {
  1190. if(empty($source))
  1191. {
  1192. $source = '2';
  1193. }else{
  1194. $source .= ',2';
  1195. }
  1196. }
  1197. if(strpos($type[$v['id']],'3') !== false)
  1198. {
  1199. if(empty($source))
  1200. {
  1201. $source = '3';
  1202. }else{
  1203. $source .= ',3';
  1204. }
  1205. }
  1206. $list[$k]['source'] = $source;
  1207. }
  1208. if(!empty($source))
  1209. {
  1210. $data[] = $v;
  1211. }
  1212. }
  1213. return $list;
  1214. }
  1215. public function saveAnnex($params)
  1216. {
  1217. if(empty($params['url']))
  1218. {
  1219. $this->throwError('无效的url地址','9001');
  1220. }
  1221. $value = $params['code'];
  1222. $key = Config::get('report_save_type')[$params['type']] ?? null;
  1223. if(empty($key))
  1224. {
  1225. $this->throwError('无效的type类型','9002');
  1226. }
  1227. $field = ['e.id','r.id as report_id'];
  1228. if($key == 'id')
  1229. {
  1230. $key= 'e.id';
  1231. }
  1232. $where = [$key=>$value,'institution_id'=>$params['institution_id']];
  1233. $whereTime = [];
  1234. if($params['institution_id'] == '06300006')
  1235. {
  1236. if($params['type'] == 2)
  1237. {
  1238. $whereTime['exam_datetime'] = date('Ymd',time());
  1239. }
  1240. }
  1241. $exam = $this->butt->getExamReport($where,$field,$whereTime);
  1242. if(empty($exam))
  1243. {
  1244. $data = [
  1245. 'annex_class_code'=>$params['annex_class_code'] ?? '',
  1246. 'url'=>$params['url'],
  1247. 'name'=>$params['name'] ?? '',
  1248. 'report_id'=>'',
  1249. 'exam_id'=>'',
  1250. 'type'=>$params['type'],
  1251. 'code'=>$value,
  1252. 'exam_datetime'=>$params['exam_datetime'] ?? '',
  1253. 'institution_id'=>$params['institution_id']
  1254. ];
  1255. $whereAnnex = ['type'=>$params['type'],'code'=>$value,'url'=>$params['url']];
  1256. $info = $this->butt->checkAnnes($whereAnnex);
  1257. if($info)
  1258. {
  1259. $return = $this->butt->updateAnnex($data,$whereAnnex);
  1260. }else{
  1261. $return = $this->butt->saveAnnex($data);
  1262. }
  1263. return $return;
  1264. // $this->throwError('不存在的检查','9003');
  1265. }else{
  1266. $return = '';
  1267. foreach ($exam as $k=>$v)
  1268. {
  1269. $data = [
  1270. 'annex_class_code'=>$params['annex_class_code'] ?? '',
  1271. 'url'=>$params['url'],
  1272. 'name'=>$params['name'] ?? '',
  1273. 'report_id'=>$v['report_id'] ? $v['report_id'] : '',
  1274. 'exam_id'=>$v['id'],
  1275. 'type'=>$params['type'],
  1276. 'code'=>$value,
  1277. 'exam_datetime'=>$params['exam_datetime'] ?? '',
  1278. 'institution_id'=>$params['institution_id']
  1279. ];
  1280. $whereAnnex = ['exam_id'=>$v['id'],'url'=>$params['url']];
  1281. $info = $this->butt->checkAnnes($whereAnnex);
  1282. if($info)
  1283. {
  1284. $return = $this->butt->updateAnnex($data,$whereAnnex);
  1285. }else{
  1286. $return = $this->butt->saveAnnex($data);
  1287. }
  1288. }
  1289. return $return;
  1290. }
  1291. }
  1292. public function saveRegister($params)
  1293. {
  1294. if(empty($params['code']))
  1295. {
  1296. $this->throwError('无效的code码',0025);
  1297. }
  1298. $key = Config::get('ut')[$params['code']] ?? null;
  1299. if(empty($key))
  1300. {
  1301. $this->throwError('无效的code码',0025);
  1302. }
  1303. $where = [$key=>$params[$key]];
  1304. $examId = $this->butt->getRegisterExam($where);
  1305. // 添加 patient_infos 表数据
  1306. $patient_data = $this->makePatientData($params);
  1307. if(!is_null($examId)){
  1308. // 修改操作
  1309. unset($patient_data['id']);
  1310. $pat_id = $this->registerDao->getPatientId($examId);
  1311. $patient = $this->registerDao->updatePatient($patient_data,$pat_id);
  1312. } else {
  1313. // 添加操作
  1314. $pat_id = $patient_data['id'];
  1315. $patient = $this->registerDao->insertParent($patient_data);
  1316. }
  1317. // 添加 exam 表数据
  1318. $exam_data = $this->makeExamData($params);
  1319. $exam_data['patient_id'] = $pat_id;
  1320. if(!is_null($examId)){
  1321. // 修改操作
  1322. unset($exam_data['id']);
  1323. unset($exam_data['exam_status']);
  1324. $exam_res = $this->registerDao->updateExam($exam_data,$examId);
  1325. $exam_data['id'] = $examId;
  1326. } else {
  1327. $examId = $exam_data['id'];
  1328. $exam_res = $this->registerDao->insertExam($exam_data);
  1329. }
  1330. $register_data = $this->makeRegister($params);
  1331. $register_data['exam_id'] = $exam_data['id'];
  1332. $registerId = $this->registerDao->getRegisterId($exam_data['id']);
  1333. if(!is_null($registerId)){
  1334. // xiugai
  1335. $this->registerDao->updateRegister($register_data,$examId);
  1336. }else{
  1337. // xinzeng
  1338. $register_data['id'] = UUIDUtils::uuid();
  1339. $this->registerDao->insertRegister($register_data);
  1340. }
  1341. return $examId;
  1342. }
  1343. public function makePatientData($params)
  1344. {
  1345. try{
  1346. $sex_choose = $params['sex'] ?? '';
  1347. $sex = $this->getSex($sex_choose);
  1348. $data = [
  1349. 'id' => UUIDUtils::uuid(),
  1350. 'sex' => $sex,
  1351. 'name' => $params['name'] ?? '',
  1352. 'card_num' => $params['card_num'] ?? '',
  1353. 'phone' => $params['phone'] ?? '',
  1354. 'ctime' => date('Y-m-d H:i:s'),
  1355. 'birthday' => $params['birthday'] ?? '',
  1356. 'age' => $params['age'] ?? '',
  1357. 'card_type' => $params['card_type'] ?? '',
  1358. 'nationality' => $params['nationality'] ?? '',
  1359. 'nation' => $params['nation'] ?? '',
  1360. 'marry' => $params['marry'] ?? '',
  1361. 'professional' => $params['professional'] ?? '',
  1362. 'address' => $params['address'] ?? '',
  1363. 'status' => 1,
  1364. 'anamnesis' => $params['anamnesis'] ?? '',
  1365. 'family_ill' => $params['family_ill'] ?? '',
  1366. 'createdAt' => date('Y-m-d H:i:s'),
  1367. 'institution_id' => $params['institution_id'],
  1368. 'temp_patient_id' => $params['patient_num']
  1369. ];
  1370. return $data;
  1371. }catch (Exception $exception){
  1372. $this->throwError($exception->getMessage(),0001);
  1373. }
  1374. }
  1375. public function makeExamData($params)
  1376. {
  1377. try{
  1378. $sex_choose = $params['sex'] ?? '';
  1379. $sex = $this->getSex($sex_choose);
  1380. $data = [
  1381. 'id' => UUIDUtils::uuid(),
  1382. 'institution_id' => $params['institution_id'],
  1383. 'name' => $params['name'] ?? '',
  1384. 'phone' => $params['phone'] ?? '',
  1385. 'card_num' => $params['card_num'] ?? '',
  1386. 'patient_num' => $params['patient_num'] ?? '',
  1387. 'accession_num' => $params['accession_num'] ?? '', // 暂无
  1388. 'exam_project' => $params['exam_project'] ?? '',
  1389. 'exam_datetime' => $params['exam_datetime'] ?? '',
  1390. 'exam_status' => 1,
  1391. 'exam_sub_class' => $params['exam_sub_class'] ?? '',
  1392. 'body_part' =>$params['body_part'] ?? '',
  1393. 'device_name' => $params['device_name'] ?? '',
  1394. 'sex' => $sex,
  1395. 'age' =>$params['age'] ?? '',
  1396. 'birthday' => $params['birthday'] ?? '',
  1397. 'hopitalized_no' => $params['hopitalized_no'] ?? '',
  1398. 'bed_no' => $params['bed_no'] ?? '',
  1399. 'patient_area' => $params['patient_area'] ?? '',
  1400. 'application_department' => $params['application_department'] ?? '',
  1401. 'application_doctor' => $params['application_doctor'] ?? '',
  1402. 'out_patient' => $params['out_patient'] ?? '',
  1403. 'clin_symp' => $params['clin_symp'] ?? '',
  1404. 'clin_diag' => $params['clin_diag'] ?? '',
  1405. 'his_patient_id' => $params['his_patient_id'] ?? '',
  1406. 'exam_class' =>$params['exam_class'],
  1407. 'write_report' =>1
  1408. ];
  1409. return $data;
  1410. }catch (Exception $exception){
  1411. $this->throwError($exception->getMessage(),0001);
  1412. }
  1413. }
  1414. public function makeRegister($params)
  1415. {
  1416. $data = [
  1417. 'exam_sub_class' => $params['exam_sub_class'] ?? '',
  1418. 'body_part' =>$params['body_part'] ?? '',
  1419. 'device_name' => $params['device_name'] ?? null,
  1420. 'register_datetime' => date('Y-m-d H:i:s'),
  1421. 'illness_desc' => $params['illness_desc'] ?? '',
  1422. 'phys_sign' => $params['phys_sign'] ?? '',
  1423. 'clin_symp' => $params['clin_symp'] ?? '',
  1424. 'anamnesis' => $params['anamnesis'] ?? '',
  1425. 'family_ill' => $params['family_ill'] ?? '',
  1426. 'clin_diag' => $params['clin_diag'] ?? '',
  1427. 'application_department' => $params['application_department'] ?? '',
  1428. 'ext' => $params['remark'] ?? ''
  1429. ];
  1430. return $data;
  1431. }
  1432. public function getField($params)
  1433. {
  1434. $ins = $params['institution_id'];
  1435. $type = $this->butt->getField($ins);
  1436. return Config::get('report_save_type')[$type] ?? 'patient_num';
  1437. }
  1438. public function savePrintSync($params)
  1439. {
  1440. if(empty($params['type']))
  1441. {
  1442. $this->throwError('无效的type码',0025);
  1443. }
  1444. if(empty($params['code']))
  1445. {
  1446. $this->throwError('无效的code码',0025);
  1447. }
  1448. $key = Config::get('report_save_type')[$params['type']] ?? null;
  1449. if(empty($key))
  1450. {
  1451. $this->throwError('无效的type码',0025);
  1452. }
  1453. $where = [$key=>$params['code'],'institution_id'=>$params['institution_id']];
  1454. $exam = $this->butt->getExam($where,['name']);
  1455. if(empty($exam))
  1456. {
  1457. $this->throwError('无效的code,查不到匹配的检查',0027);
  1458. }
  1459. $where = ['code'=>$params['code'],'institution_id'=>$params['institution_id']];
  1460. // $today_print = $this->butt->checkPrint($where,"to_days(createdAt) = to_days(now())");
  1461. // if($today_print)
  1462. // {
  1463. // //检查次数+1
  1464. // $info = $this->butt->printZz($where);
  1465. // return '同步成功';
  1466. // }
  1467. $where['print_time'] = $params['print_time'] ?? null;
  1468. $data = [
  1469. 'institution_id'=>$params['institution_id'] ?? '',
  1470. 'type'=>$params['type'],
  1471. 'code'=>$params['code'],
  1472. 'print_time'=>$params['print_time'] ?? null,
  1473. 'name'=>$exam['name']
  1474. ];
  1475. $print = $this->butt->getPrint($where);
  1476. if($print)
  1477. {
  1478. $info = $this->butt->updatePrintSync($data,$where);
  1479. }else{
  1480. $info = $this->butt->savePrintSync($data);
  1481. }
  1482. return $info;
  1483. }
  1484. public function saveReportAnnex($url,$reportId)
  1485. {
  1486. $examId = $this->butt->getExamFromReport($reportId);
  1487. $data = [
  1488. 'annex_class_code'=>2,
  1489. 'url'=>$url,
  1490. 'name'=>'',
  1491. 'report_id'=>$reportId,
  1492. 'exam_id'=>$examId
  1493. ];
  1494. log::record('附件'.json_encode($data));
  1495. $this->butt->saveAnnex($data);
  1496. }
  1497. public function getPayExam($params)
  1498. {
  1499. $start = $params['start'];
  1500. $finish = $params['finish'];
  1501. $institution = $params['institution'];
  1502. $where = ['m.pay_type'=>1,'m.pay_way'=>1,'m.institution_id'=>$institution,'m.status'=>2,'m.type'=>2,'m.order_type'=>1,'m.is_sync'=>0];
  1503. $time = "m.createdAt between '$start' and '$finish'";
  1504. $water = $this->butt->getPayExam($where,$time);
  1505. // $examWhere[] = ['id','in',$water];
  1506. // $info = $this->butt->getPayExams($examWhere,['patient_num','accession_num','studyuid']);
  1507. return $water;
  1508. }
  1509. public function changeSyncStatus($params)
  1510. {
  1511. $id = $params['id'];
  1512. $info = $this->butt->changeSyncStatus($id);
  1513. return $info;
  1514. }
  1515. public function changePayStatus($params)
  1516. {
  1517. $patient_num = $params['patient_num'] ?? '';
  1518. if(empty($patient_num))
  1519. {
  1520. return '';
  1521. }
  1522. $ins = '03060002';
  1523. $info = $this->butt->changePayStatus($patient_num,$ins);
  1524. return $info;
  1525. }
  1526. public function getMiddle($params)
  1527. {
  1528. $num = $params['minute'];
  1529. $ins = $params['institution_id'];
  1530. //获取多少分钟内数据
  1531. $where = "m.createdAt > DATE_SUB(NOW(), INTERVAL $num MINUTE) and m.institution_id='$ins'";
  1532. $info = $this->butt->getMiddleInfo($where);
  1533. return $info;
  1534. }
  1535. public function changeMiddleStatus($params)
  1536. {
  1537. $where[] = ['id','in',explode(',',$params['ids'])];
  1538. $update = ['is_sync'=>1];
  1539. $info = $this->butt->changeMiddleStatus($where,$update);
  1540. return $info;
  1541. }
  1542. public function syncMiddle($params)
  1543. {
  1544. $where = ['studyuid'=>$params['studyuid']];
  1545. $field = ['id','institution_id'];
  1546. $exam = $this->butt->getExam($where,$field);
  1547. $examId = $exam['id'] ?? null;
  1548. if(empty($examId))
  1549. {
  1550. return '无效的studyuid';
  1551. }
  1552. $middleInfo = [
  1553. 'institution_id'=>$exam['institution_id'],
  1554. 'status'=>$params['status'],
  1555. 'doctor_id'=>$params['doctor_id'],
  1556. 'doctor_name'=>$params['doctor_name'],
  1557. 'exam_id'=>$examId,
  1558. 'is_sync'=>1
  1559. ];
  1560. $r = $this->butt->saveMiddle($middleInfo);
  1561. return $r;
  1562. }
  1563. public function goNoteUrl($param)
  1564. {
  1565. try{
  1566. if($param['id'] == 1)
  1567. {
  1568. return DebugModel::value('url');
  1569. }
  1570. $num = base64_decode($param['id']);
  1571. $id = base_convert(ltrim($num,0),36,10);
  1572. $url = $this->butt->getNoteUrl($id);
  1573. return $url;
  1574. }catch (Exception $e)
  1575. {
  1576. return '';
  1577. }
  1578. }
  1579. public function saveAiReport($param)
  1580. {
  1581. $this->reportDao->saveDcmReportData(json_encode($param),$param['exam_id'],5,$this->ts_time());
  1582. if($param['describe'] == 'gpu不可用,请1小时后再试')
  1583. {
  1584. return '';
  1585. }
  1586. $data = [
  1587. 'id'=>UUIDUtils::uuid(),
  1588. 'impression'=>$param['diagnosis'],
  1589. 'description'=>$param['describe'],
  1590. 'type'=>3,
  1591. 'exam_id'=>$param['exam_id'],
  1592. 'report_doctor_name'=>'ai报告医生',
  1593. 'review_doctor_name'=>'ai报告医生',
  1594. 'confirm_doctor_name'=>'ai报告医生',
  1595. 'report_datetime'=>date('Y-m-d H:i:s'),
  1596. 'review_datetime'=>date('Y-m-d H:i:s'),
  1597. 'confirm_datetime'=>date('Y-m-d H:i:s'),
  1598. 'detections'=>empty($param['detections']) ? '' : json_encode($param['detections'])
  1599. ];
  1600. $aiData = $this->reportDao->getReport($param['exam_id'],3);
  1601. if($aiData)
  1602. {
  1603. //修改ai报告
  1604. unset($data['id']);
  1605. $info = $this->reportDao->updateReport($param['exam_id'],3,$data);
  1606. }else{
  1607. $info = $this->butt->saveReport($data);
  1608. }
  1609. $arr = [
  1610. 'report_id'=>$data['exam_id'],
  1611. 'exam_id'=>$param['exam_id'],
  1612. 'diagnosis'=>$param['diagnosis'],
  1613. 'describe'=>$param['describe'],
  1614. 'detections'=>empty($param['detections']) ? '' : json_encode($param['detections'])
  1615. ];
  1616. $this->butt->saveAiReport($arr);
  1617. $this->reportDao->saveDcmReportData('',$param['exam_id'],6,$this->ts_time());
  1618. return $info;
  1619. }
  1620. public function getDmReport($param)
  1621. {
  1622. if($param['datetime'] ?? '')
  1623. {
  1624. $date = explode(',',$param['datetime']);
  1625. if(empty($date[0] ?? '') || empty($date['1'] ?? ''))
  1626. {
  1627. $this->throwError('参数传参错误','0058');
  1628. }
  1629. }else{
  1630. $this->throwError('缺少必要参数','0057');
  1631. }
  1632. $data = $this->reportDao->getDmReport($date[0],$date[1]);
  1633. return $data;
  1634. }
  1635. public function getInsReport($param)
  1636. {
  1637. if($param['datetime'] ?? '')
  1638. {
  1639. $date = explode(',',$param['datetime']);
  1640. if(empty($date[0] ?? '') || empty($date['1'] ?? ''))
  1641. {
  1642. $this->throwError('参数传参错误','0058');
  1643. }
  1644. }else{
  1645. $this->throwError('缺少必要参数','0057');
  1646. }
  1647. if(!($param['institution_id'] ?? ''))
  1648. {
  1649. $this->throwError('缺少必要参数','0057');
  1650. }
  1651. $data = $this->reportDao->getInsReport($date[0],$date[1],$param['institution_id'],$param['remote'] ?? '',$param['page'] ?? 1,$param['num'] ?? 10);
  1652. return $data;
  1653. }
  1654. public function getWechatData($openid)
  1655. {
  1656. if(empty($openid))
  1657. {
  1658. $this->throwError('错误的参数','0059');
  1659. }
  1660. $data = $this->reportDao->getWechatData($openid);
  1661. if(empty($data))
  1662. {
  1663. $this->throwError('未找到绑定信息','0060');
  1664. }
  1665. $wx_id = $data['wx_id'];
  1666. $patient = $data['patient'];
  1667. $exam = $data['exams'];
  1668. $arr = [
  1669. 'name'=>empty($patient['patient_name'] ?? '') ? ($exam['name'] ?? '') : $patient['patient_name'],
  1670. 'idCard' => empty($patient['idCard'] ?? '') ? ($exam['card_num'] ?? '') : $patient['idCard'],
  1671. 'phone'=>$patient['phone'] ?? '',
  1672. 'birthday'=>$exam['birthday'] ?? '',
  1673. 'sex'=>$exam['sex'] ?? '',
  1674. 'id'=>$wx_id
  1675. ];
  1676. $key = 'e386c7039aa49826664d576eb798da21';
  1677. $arr = openssl_encrypt((json_encode($arr)),'AES-128-ECB',$key,OPENSSL_RAW_DATA);
  1678. return base64_encode($arr);
  1679. }
  1680. }