ButtService.php 66 KB

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