ButtService.php 65 KB

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