XzService.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. <?php
  2. namespace app\api\servies\xz;
  3. use app\api\model\studies\StudiesModel;
  4. use app\api\servies\ZskkDefaultService;
  5. // use app\api\validate\xz\XzValidate;
  6. use app\api\dao\xz\XzDao;
  7. use app\api\servies\common\CommonService;
  8. use app\api\utils\UUIDUtils;
  9. use app\common\library\send_message;
  10. use think\Db;
  11. use think\Exception;
  12. use think\facade\Config;
  13. use think\facade\Log;
  14. use app\api\change\PinyinChange;
  15. /**
  16. * 后台控制器基类
  17. * 接口方法权限 必传参数 接口返回 错误抛出 通用参数处理
  18. */
  19. class XzService extends ZskkDefaultService {
  20. protected $logName = "XzService";
  21. private $xz = null;
  22. public function __construct(XzDao $xzDao) {
  23. parent::__construct();
  24. $this->xz = $xzDao;
  25. }
  26. public function savePacs($params)
  27. {
  28. try{
  29. $xz = $params;
  30. // 数据存储
  31. $xz['id'] = UUIDUtils::uuid();
  32. $xz['phonetic'] = PinyinChange::utf8_to($xz['patientName'],1);
  33. $xz['descriptionPhonetic'] = PinyinChange::utf8_to($xz['contentDescription'],1);
  34. $where = [
  35. 'applicationFormNo'=>$params['applicationFormNo'],
  36. 'clinicId'=>$params['clinicId']
  37. ];
  38. $xzInfo = $this->xz->checkXz($where);
  39. if(empty($xzInfo)){
  40. $data = $this->xz->insertXz($xz);
  41. }else{
  42. return json_encode(['code'=>'0']);
  43. }
  44. // 存储患者信息
  45. $patientData = [
  46. 'id'=>UUIDUtils::uuid(),
  47. 'name'=>$params['patientName'],
  48. 'sex'=>$params['patientSex'],
  49. 'birthday'=>$params['patientBirthDate'] ?? '',
  50. 'age'=>$params['patientAge'],
  51. 'institution_id'=>$params['clinicId'],
  52. 'createdAt'=>date('Y-m-d H:i:s',time()),
  53. 'temp_patient_id'=>$params['patientId']
  54. ];
  55. $patient = $this->xz->insertPatient($patientData);
  56. // 存储studies
  57. $studiesData = [
  58. 'id'=>UUIDUtils::uuid(),
  59. 'studyuid'=>'1.3.6.1.4.1.30071.8.'.time().$params['clinicId'].$params['patientId'].$params['applicationFormNo'],
  60. 'patient_id'=>$patientData['id'],
  61. 'studydate'=>$params['startDate'],
  62. 'modality'=> $params['modality'] == 1? 'US' : 'OP',
  63. 'accession_num'=>$params['applicationFormNo'],
  64. 'status'=>1,
  65. 'patient_age'=>$params['patientAge'],
  66. 'studyDescribe'=>$params['contentDescription'] ?? '',
  67. 'institution_id'=>$params['clinicId'],
  68. 'createdAt' => date('Y-m-d H:i:s',time()),
  69. 'weight'=>$params['patientWeight'] ?? ''
  70. ];
  71. $studies = $this->xz->insertStudies($studiesData);
  72. // 存储exams 信息
  73. $examData = [
  74. 'id'=>UUIDUtils::uuid(),
  75. 'patient_num'=>$params['patientId'],
  76. 'accession_num'=>$params['applicationFormNo'],
  77. 'patient_id'=>$patientData['id'],
  78. 'exam_datetime'=>$params['startDate'],
  79. 'exam_class' => $params['modality'] == 1? 'US' : 'OP',
  80. 'clin_dept_id'=>$params['department'] ?? '',
  81. 'institution_id'=>$params['clinicId'],
  82. 'illness_desc'=>$params['symptom'] ?? '',
  83. 'clin_doctors'=>$params['clinicalDoctor'] ?? '',
  84. 'study_uid'=>$studiesData['studyuid'],
  85. 'exam_status'=>'1',
  86. 'study_id'=>$studiesData['id']
  87. ];
  88. if($params['clinicalDoctor'] ?? ''){
  89. $doctorId = $this->xz->checkDoctor($params['clinicalDoctor']);
  90. if(!empty($doctorId)){
  91. $examData['register'] = $doctorId;
  92. }else{
  93. $doctorData = [
  94. 'id'=>UUIDUtils::uuid(),
  95. 'realname'=>$params['clinicalDoctor'],
  96. 'password'=>md5('123456'),
  97. 'username'=>UUIDUtils::uuid(),
  98. 'institution_id'=>$params['clinicId']
  99. ];
  100. $examData['register'] = $doctorData['id'];
  101. }
  102. }
  103. $exam = $this->xz->insertExam($examData);
  104. $this->xz->updateStudies($studiesData['id'],$examData['id']);
  105. $updateXz = ['exam_id'=>$examData['id'],'studyUid'=>$studiesData['studyuid']];
  106. $this->xz->updateXz($where,$updateXz);
  107. $status = $this->xz->updateStatus($where,1);
  108. return json_encode(['code'=>'0']);
  109. }catch(\Exception $e){
  110. return json_encode(['code'=>'1','message'=>$e->getMessage()]);
  111. }
  112. }
  113. public function cancel($params)
  114. {
  115. try{
  116. $where = [
  117. 'applicationFormNo'=>$params['applicationFormNo'],
  118. 'clinicId'=>$params['clinicId']
  119. ];
  120. $xzInfo = $this->xz->checkXz($where);
  121. if(empty($xzInfo)){
  122. return json_encode(['code'=>'1','message'=>'该检查不存在']);
  123. }
  124. if($xzInfo['exam_status'] == '1' || $xzInfo['exam_status'] == '0'){
  125. $status = $this->xz->updateStatus($where,0);
  126. $update = ['exam_status'=>-99];
  127. $a = $this->xz->updateExam($xzInfo['exam_id'],$update);
  128. return json_encode(['code'=>'0']);
  129. }else{
  130. return json_encode(['code'=>'1','message'=>'非检查完成状态的检查不可撤回']);
  131. }
  132. }catch(\Exception $e){
  133. return json_encode(['code'=>'1','message'=>$e->getMessage()]);
  134. }
  135. }
  136. // 3 推送检查消息
  137. public function pushExam($params,$url)
  138. {
  139. try{
  140. $data = $params;
  141. $info = $this->curlJsonPost($url,$data);
  142. $xz = json_decode($info,true);
  143. $return = [];
  144. if($xz['code'] == '0'){
  145. return '1';
  146. }else{
  147. return $xz;
  148. }
  149. // 是否要修改xz表中的状态
  150. // return $info;
  151. }catch(\Exception $e){
  152. return json_encode(['code'=>'0001','message'=>$e->getMessage()]);
  153. }
  154. }
  155. public function getData($params)
  156. {
  157. $info = $this->xz->getXzData($params['id']);
  158. if($info['exam_status'] != 1){
  159. $this->throwError('非正常的流程操作,无法开始检查','0001');
  160. }
  161. $data = [
  162. 'applicationFormNo'=>$info['applicationFormNo'],
  163. 'clinicId'=>$info['clinicId']
  164. ];
  165. return $data;
  166. }
  167. public function change($params,$status)
  168. {
  169. $where = [
  170. 'exam_id'=>$params['id']
  171. ];
  172. $this->xz->updateStatus($where,$status);
  173. $this->xz->updateExam($params['id'],['exam_status'=>$status]);
  174. }
  175. public function getInfo($params)
  176. {
  177. $info = $this->xz->getXzData($params['id']);
  178. $data = [
  179. 'applicationFormNo'=>$info['applicationFormNo'],
  180. 'clinicId'=>$info['clinicId'],
  181. 'urlParm'=>$params['id']
  182. ];
  183. return $data;
  184. }
  185. public function getExamData($id)
  186. {
  187. $xzData = $this->xz->getExamData($id);
  188. return $xzData;
  189. }
  190. public function getExamPush($id)
  191. {
  192. try{
  193. $xzData = $this->xz->getXzData($id);
  194. $report = $this->xz->getReport($id);
  195. $reportName = $this->xz->getDoctorName($report['report_doctor_id']);
  196. $reviewName = $this->xz->getDoctorName($report['review_doctor_id']);
  197. $studyUid = $this->xz->getUid($id);
  198. $accessionNum = $this->xz->getAccession($id);
  199. $info = [
  200. 'report'=>[
  201. 'department'=>$xzData['department'],
  202. 'clinicalDoctor'=>$xzData['clinicalDoctor'],
  203. 'symptom'=>$xzData['symptom'],
  204. 'reportDate'=>$report['report_datetime'],
  205. 'description'=>$report['description'],
  206. 'impression'=>$report['impression'],
  207. 'reportDoctor'=>$reportName,
  208. 'reviewDoctor'=>$reviewName,
  209. 'studyInstanceUId'=>$studyUid,
  210. 'modality'=>$xzData['modality'],
  211. 'accessionNumber'=>$accessionNum
  212. ],
  213. 'applicationFormNo'=>$xzData['applicationFormNo'],
  214. 'clinicId'=>$xzData['clinicId'],
  215. ];
  216. $data = json_encode($info);
  217. $url = 'http://wjw.yidab.cn:801/pacs/pushExamReport.do';
  218. // $url = 'http://wjw.yidab.cn:801/wjw-test/pacs/pushExamReport.do';
  219. $return = $this->curlJsonPost($url,$data);
  220. return $return;
  221. }catch(\Exception $e){
  222. return json_encode(['code'=>'1','message'=>$e->getMessage()]);
  223. }
  224. }
  225. public function saveExam($params)
  226. {
  227. /*if(isset($params['institution_id']) && $params['institution_id'] == '730900006')
  228. {
  229. log::record('存在730900006');
  230. // $db = Db::connect('db2');
  231. $e = Db::connect('db2')->table('exams')->where('study_id',$params['study_id'])->value('id');
  232. if(!empty($e)){
  233. log::record('存在检查');
  234. log::record('检查id%'.$e.'%');
  235. return true;
  236. }
  237. log::record('不存在检查');
  238. $exam2 = [
  239. 'id'=>UUIDUtils::uuid(),
  240. 'patient_id'=>$params['patient_id'] ?? null,
  241. 'patient_num'=>$params['patient_num'] ?? null,
  242. 'accession_num'=>$params['accession_num'] ?? null,
  243. 'exam_datetime'=>$params['exam_datetime'] ?? null,
  244. 'exam_class'=>$params['exam_class'] ?? null,
  245. 'institution_id'=>$params['institution_id'],
  246. 'study_id'=>$params['study_id'] ?? null,
  247. 'id_card' => $params['card_num'] ?? null,
  248. 'study_uid'=>StudiesModel::where('id',$params['study_id'])->value('studyuid'),
  249. 'exam_status'=>3,
  250. 'createdAt'=>date('Y-m-d H:i:s',time()),
  251. 'phone'=>$params['phone'] ?? null,
  252. ];
  253. log::record('存储的检查数据'.json_encode($exam2));
  254. $e2 = Db::connect('db2')->table('exams')->insert($exam2);
  255. log::record('存储是否成功'.$e2);
  256. log::record('当前患者id为'.$params['patient_id'].'患者id存在');
  257. if(!empty($params['patient_id'])){
  258. log::record('存在患者id');
  259. $patient = [
  260. 'id'=>$params['patient_id'],
  261. 'name'=>$params['name'] ?? null,
  262. 'sex'=>$params['sex'] ?? null,
  263. 'card_num'=>$params['card_num'] ?? null,
  264. 'phone'=>$params['phone'] ?? null,
  265. 'age'=>$params['age'] ?? null,
  266. 'temp_patient_id'=>$params['patient_num'] ?? null,
  267. 'institution_id'=>$params['institution_id']
  268. ];
  269. log::record('存储的患者数据'.json_encode($patient));
  270. $p = Db::connect('db2')->table('patient_infos')->insert($patient);
  271. log::record('患者存储成功'.$p);
  272. }
  273. return true;
  274. }*/
  275. $time1 = time();
  276. $exam = $this->xz->checkIsSet($params['study_id']);
  277. if(empty($exam))
  278. {
  279. $exam = $this->xz->checkIsSet($params['study_id'],$params['studyuid'],$params['institution_id']);
  280. if(empty($exam))
  281. {
  282. if(!empty($params['accession_num']))
  283. {
  284. $exam = $this->xz->checkExam(['accession_num'=>$params['accession_num'],'institution_id'=>$params['institution_id']]);
  285. }
  286. }
  287. }
  288. if($params['institution_id'] == '71900004')
  289. {
  290. $params['name'] = str_replace('=','',$params['name']);
  291. }
  292. if($params['institution_id'] == '16100014')
  293. {
  294. if($params['exam_class'] == 'RF')
  295. {
  296. $params['exam_class'] = 'DX';
  297. }
  298. }
  299. if($params['institution_id'] == '71900007' || $params['institution_id'] == '71900008')
  300. {
  301. if($params['exam_class'] == 'CR')
  302. {
  303. $params['exam_class'] = 'DR';
  304. }
  305. }
  306. if($exam){
  307. //存在则修改
  308. $id = $exam['id'];
  309. $water_log = $this->xz->getWaterLog($id,$params['institution_id']);
  310. $update = [];
  311. if(empty($water_log)){
  312. $film = $this->xz->getInsFilmInfo($params['institution_id']);
  313. if(!empty($film))
  314. {
  315. if($film['charge_mode'] == Config::get('order_type')['hospitalPay']){
  316. //创建检查 必须为医院托管模式 才允许创建胶片记录
  317. $this->save_ins_film_water($id,$film['charge_mode'],$film['film_price'],$params['institution_id']);
  318. $update['pay_status'] = 1;
  319. }
  320. }
  321. }
  322. $update['updatedAt'] = date('Y-m-d H:i:s',time());
  323. if(isset($params['node_type']) && !empty($params['node_type']))
  324. {
  325. $update['node_type'] = $params['node_type'];
  326. }
  327. if(isset($params['exam_class']) && !empty($params['exam_class']))
  328. {
  329. if($exam['exam_class'] !== $params['exam_class'])
  330. {
  331. $update['exam_class'] = $params['exam_class'];
  332. }
  333. }
  334. $update['status'] = 1;
  335. if($params['institution_id'] == '27400005') {
  336. $cache = $this->xz->getCache("worklist_".$params['patient_num']);
  337. if(!empty($cache))
  338. {
  339. $key = "worklist_".$params['patient_num'];
  340. $d = json_decode($cache, true);
  341. Log::record('东民二院;存储key为'.$key.';存储内容为'.$cache);
  342. $update["name"] = $d['NAME'];
  343. $update["hopitalized_no"] = $d['zyh'];
  344. $update["bed_no"] = $d['cwh'];
  345. $update["exam_project"] = $d['PARTOFCHECK'];
  346. }
  347. }
  348. // foreach ($params as $k=>$v){
  349. // if(!empty($v) && $k !== 'exam_class'){
  350. // $update[$k] = $v;
  351. // if($k == 'name' && $params['institution_id'] == '27400005'){
  352. // //东明二院
  353. // $cache = $this->xz->getCache("worklist_".$params['patient_num']);
  354. // if(!empty($cache))
  355. // {
  356. // // $d = json_decode($cache, true);
  357. // $update[$k] = $cache;
  358. // }
  359. // }
  360. // }
  361. // }
  362. $update['study_id'] = $params['study_id'] ?? null;
  363. if($exam['exam_status'] == -1){
  364. $update['exam_status'] = '9';
  365. $this->xz->updateExam($id,$update);
  366. }elseif($exam['exam_status']<3){
  367. $update['exam_status'] = '3';
  368. //进行分派
  369. $did = $this->handle_queue($params['institution_id']);
  370. $update['doctor_sign'] = $did;
  371. $this->xz->updateExam($id,$update);
  372. }else{
  373. $this->xz->updateExam($id,$update);
  374. }
  375. }
  376. else{
  377. $name = $params['name'] ?? null;
  378. $hopitalized_no = null;
  379. $bed_no = null;
  380. $exam_project = null;
  381. if($params['institution_id'] == '27400005') {
  382. $cache = $this->xz->getCache("worklist_".$params['patient_num']);
  383. if(!empty($cache))
  384. {
  385. $key = "worklist_".$params['patient_num'];
  386. $d = json_decode($cache, true);
  387. Log::record('东民二院;存储key为'.$key.';存储内容为'.$cache);
  388. $name = $d['NAME'];
  389. $hopitalized_no = $d['zyh'];
  390. $bed_no = $d['cwh'];
  391. $exam_project = $d['PARTOFCHECK'];
  392. }
  393. }
  394. $uid = $this->xz->getStudyUid($params['study_id']);
  395. if(empty($uid))
  396. {
  397. $uid = $params['studyuid'] ?? null;
  398. }
  399. $body_part_text = '';
  400. if(!empty($params['body_part'] ?? null))
  401. {
  402. $body_part_text = $this->xz->getBodyText($params['body_part']);
  403. }
  404. $data = [
  405. 'id'=>UUIDUtils::uuid(),
  406. 'patient_id'=>$params['patient_id'],
  407. 'study_id'=>$params['study_id'],
  408. 'patient_num'=>$params['patient_num'],
  409. 'accession_num'=>$params['accession_num'] ?? null,
  410. 'exam_datetime'=>$params['exam_datetime'] ?? null,
  411. 'exam_class'=>$params['exam_class'] ?? null,
  412. 'institution_id'=>$params['institution_id'] ?? null,
  413. 'device_name'=>$params['device_name'] ?? null,
  414. 'body_part'=>$params['body_part'] ?? null,
  415. 'body_part_text'=>$body_part_text,
  416. 'createdAt'=>date('Y-m-d H:i:s',time()),
  417. 'exam_status'=>'3',
  418. 'name'=>$name,
  419. 'phone'=>$params['phone'] ?? null,
  420. 'card_num' => $params['card_num'] ?? null,
  421. 'studyid'=>$params['studyid'] ?? null,
  422. 'studyuid'=>$uid,
  423. 'sex' =>$params['sex'] ?? null,
  424. 'age' => $params['age'] ?? null,
  425. 'hopitalized_no' => $hopitalized_no,
  426. 'bed_no' => $bed_no,
  427. 'exam_project' => $exam_project,
  428. 'node_type'=>$params['node_type'] ?? 1
  429. ];
  430. // 队列处理并分配
  431. $did = $this->handle_queue($params['institution_id']);
  432. $exam['doctor_sign'] = $did;
  433. $this->xz->insertExam($data);
  434. $id = $data['id'];
  435. $exam = $data;
  436. unset($exam['id']);
  437. $exam['exam_id'] = $data['id'];
  438. $film = $this->xz->getInsFilmInfo($params['institution_id']);
  439. if(!empty($film))
  440. {
  441. if($film['charge_mode'] == Config::get('order_type')['hospitalPay']){
  442. //创建检查 必须为医院托管模式 才允许创建胶片记录
  443. $this->save_ins_film_water($id,$film['charge_mode'],$film['film_price'],$params['institution_id']);
  444. $this->xz->updateExam($id,['pay_status'=>1]);
  445. }
  446. }
  447. log::record('当前医院id'.$params['institution_id']);
  448. $phone = $params['phone'] ?? '';
  449. $value = 'patient_sms';
  450. $sms = $this->xz->getInsValue($params['institution_id'],$value);
  451. log::record('当前医院发送短信'.$sms);
  452. if($sms == 1)
  453. {
  454. // log::record('手机号为'.$phone);
  455. // if(!empty($phone))
  456. // {
  457. // $institution = $this->xz->getInstitutionName($params['institution_id']);
  458. // $code = rand(1000,9999);
  459. // $share = [
  460. // 'id'=>UUIDUtils::uuid(),
  461. // 'examId'=>$id,
  462. // 'code'=>$code,
  463. // 'overdueTime'=>'5475', //有效期15年
  464. // 'phone'=>$phone,
  465. // 'study_id'=>$params['study_id'],
  466. // 'send_message'=>1,
  467. // 'source'=>1 //1 为影像同步
  468. // ];
  469. // $send = send_message::sendExam2Patient($phone,$code,$institution,$share['id']);
  470. // $data = json_decode(json_encode($send),true);
  471. // if($data['Code'] == 'OK'){
  472. // $share['send_message'] = 1;
  473. // }else{
  474. // $share['send_message'] = 0;
  475. // }
  476. // $this->xz->saveSms($share);
  477. // }
  478. }
  479. if($params['institution_id'] ?? null){
  480. log::record('开始回调');
  481. $method = $this->xz->getApiAction($params['institution_id'],Config::get('api_action')['create']);
  482. $action = $method.'Action';
  483. log::record($action);
  484. if(method_exists($this,$action)){
  485. $r = $this->$action($params,$exam,Config::get('api_action')['create']);
  486. // return $r;
  487. }else{
  488. log::record('没有找到对应的action');
  489. }
  490. }else{
  491. log::record('没有这个医院id'.$params['institution_id'] ?? null);
  492. }
  493. }
  494. $time2 = time();
  495. $time = $time2-$time1;
  496. $this->xz->insertSpecialLog($time,'xz/create');
  497. $this->saveMonitorExam($params);
  498. return $id;
  499. }
  500. public function saveMonitorExam($params)
  501. {
  502. $arr = [];
  503. $arr['institution_id'] = $params['institution_id'];
  504. $arr['institution_name'] = $this->xz->getInsValue($params['institution_id'],'name');;
  505. $arr['name'] = $params['name'];
  506. $arr['patient_num'] = $params['patient_num'];
  507. $arr['accession_num'] = $params['patient_num'] ?? '';
  508. $arr['body_part'] = $params['body_part'] ?? '';
  509. $arr['sex'] = $params['sex'] ?? '';
  510. $arr['age'] = $params['age'] ?? '';
  511. $this->xz->saveMonitorExam($arr);
  512. }
  513. //医院胶片流水
  514. public function save_ins_film_water($exam_id,$order_type,$money,$institution)
  515. {
  516. $money_water = [
  517. 'money'=>$money,
  518. 'order_type'=>$order_type,
  519. 'type'=>'2', //电子胶片
  520. 'exam_id'=>$exam_id,
  521. 'institution_id'=>$institution,
  522. 'pay_way'=>'2', //医院支付
  523. 'status'=>'0', //未支付
  524. 'pay_type'=>'1' //支入
  525. ];
  526. $this->xz->saveMoneyWater($money_water);
  527. return true;
  528. }
  529. public function test($params,$exam)
  530. {
  531. $method = $this->xz->getApiAction($params['institution_id'],Config::get('api_action')['create']);
  532. $action = $method.'Action';
  533. if(method_exists($this,$action)){
  534. $r = $this->$action($params,$exam,Config::get('api_action')['create']);
  535. return $r;
  536. }
  537. }
  538. public function jsdAction($params,$data,$action,$times = 1)
  539. {
  540. try{
  541. $this->jsdLog('进入金盛达区间');
  542. log::record('进入金盛达区间');
  543. $this->jsdLog('当前次数----'.$times);
  544. log::record('当前次数----'.$times);
  545. $jsd_token = $this->xz->getCache('jsd_token');
  546. $this->jsdLog('第一次请求的token----'.$jsd_token);
  547. $this->jsdLog('请求的examid----'.$data['exam_id']);
  548. log::record('第一次请求的token----'.$jsd_token);
  549. if(!$jsd_token){
  550. $jsd_token = $this->getJsdToken($params['institution_id'],$data['exam_id']);
  551. }
  552. $this->jsdLog("token为:".$jsd_token) ;
  553. log::record("token为:".$jsd_token) ;
  554. $this->jsdLog('医院id'.$params['institution_id']);
  555. log::record('医院id'.$params['institution_id']);
  556. $this->jsdLog('动作'.$action);
  557. log::record('动作'.$action);
  558. $apis = $this->xz->getApis($params['institution_id'],$action);
  559. $method = $apis['action'].'_'.$apis['method'];
  560. $this->jsdLog('调用方法'.$method);
  561. log::record('调用方法'.$method);
  562. // $apis['key'] = 'a093e7998f0f4c80a5a7f66f31bdb483';
  563. // $apis['url'] = 'http://pacs.hnjsd.com.cn:8080/pacs/api/pacs/pacsOrganRegister.json';
  564. // $method = 'jsdcreate_jsd';
  565. $this->jsdLog("密钥为 :".$apis['key']) ;
  566. log::record("密钥为 :".$apis['key']) ;
  567. $jsdInfo = $this->makeJsdInfo($params,$data,$jsd_token,$apis['key']);
  568. if(method_exists($this,$method)){
  569. $this->jsdLog('存在方法,进入区间');
  570. log::record('存在方法,进入区间');
  571. $return = $this->$method($apis['url'],$jsdInfo);
  572. $this->jsdLog('金盛达回调返回数据'.$return);
  573. log::record('金盛达回调返回数据'.$return);
  574. $r = json_decode($return,true);
  575. $this->jsdLog('返回码是:'.$r['resultCode']);
  576. log::record('返回码是:'.$r['resultCode']);
  577. $update = ['push_api'=>$r['resultCode'],'api_message'=>$r['message'] ?? null];
  578. $this->xz->updateExam($jsdInfo['exam_id'],$update);
  579. if($r['resultCode'] != 1){
  580. $this->sendDing('金盛达请求失败,失败原因:'.($r['message'] ?? '无').'失败返回码:'.$r['resultCode'].',登陆请求token为'.$jsd_token,$data['exam_id']);
  581. $this->jsdLog('当前次数----'.$times);
  582. log::record('当前次数----'.$times);
  583. $del = $this->xz->delCache('jsd_token');
  584. $this->jsdLog('缓存删除'.($del ? '成功' : '失败'));
  585. log::record('缓存删除'.($del ? '成功' : '失败'));
  586. if($times == 3){
  587. $this->throwError($r['message'],$r['resultCode']);
  588. }
  589. $time = $times+1;
  590. return $this->jsdAction($params,$data,$action,$time);
  591. }
  592. $success = '金盛达请求成功,唯一检查id:'.$data['exam_id'].',登陆token为'.$jsd_token;
  593. $this->sendDing($success);
  594. return $r;
  595. }else{
  596. $this->jsdLog('方法不存在');
  597. log::record('方法不存在');
  598. $this->throwError('不存在方法'.$method,'3030');
  599. }
  600. }catch (\Exception $e)
  601. {
  602. $this->sendDing('金盛达异常,原因:'.$e->getMessage(),$data['exam_id']);
  603. $this->jsdLog('金盛达异常,原因:'.$e->getMessage().' 检查id:'.$data['exam_id']);
  604. log::record('金盛达异常,原因:'.$e->getMessage());
  605. $this->throwError('金盛达异常,原因:'.$e->getMessage(),'3031');
  606. }
  607. }
  608. public function sendDing($message,$examId=null)
  609. {
  610. $time = time()* 1000;
  611. $secret = 'SEC7eb73155768e08feeb0cb1bb84b2effe7ce6a75b69634a339a900dcc2cf5dd4e';
  612. $m = $time."\n".$secret;
  613. $s = hash_hmac('sha256', $m, $secret, true);
  614. $a = base64_encode($s);
  615. $b = urlencode($a);
  616. if(!empty($examId)){
  617. $message .= " 继续请求地址为:https://risdevserver3.pacsonline.cn/xz/test?id=".$examId;
  618. }
  619. $dingding = Config::get('dingding_url');
  620. $url =$dingding."/robot/send?access_token=a7994ade98986725955c4a6f583ba4b5d6e500ad15f16173c96cc76518b67ae4&timestamp=$time&sign=$b";
  621. $data = array ('msgtype' => 'text','text' => array ('content' => $message));
  622. $data_string = json_encode($data);
  623. $result = $this->request_by_curl($url, $data_string);
  624. }
  625. public function makeJsdInfo($params,$data,$token,$key)
  626. {
  627. $info = [
  628. 'sysCode'=>'0015',
  629. 'patient_id'=>$data['patient_id'],
  630. 'patient_name'=>$data['name'],
  631. 'patient_sex'=>$data['sex'],
  632. 'card_num'=>$data['card_num'],
  633. 'phone'=>$data['phone'],
  634. 'birthday'=>$data['birthday'] ?? null,
  635. 'patient_age'=>$data['age'],
  636. 'hospital_id'=>$params['institution_id'],
  637. 'hospital_name'=>$this->xz->getInstitutionName($params['institution_id']),
  638. 'dcm_patient_id'=>$data['patient_num'],
  639. 'study_id'=>$data['study_id'],
  640. 'studyuid'=>$data['studyuid'],
  641. 'studyid'=>$data['studyid'],
  642. 'accession_num'=>$data['accession_num'],
  643. 'studydate'=>$data['exam_datetime'],
  644. 'modality'=>$data['exam_class'],
  645. 'status'=>'1',
  646. 'exam_id'=>$data['exam_id'],
  647. 'exam_status'=>(string)$data['exam_status'],
  648. 'timestamp'=>(string)$this->msectime(),
  649. ];
  650. ksort($info);
  651. $string = '';
  652. foreach ($info as $k=>$v){
  653. if(!empty($v)){
  654. if($string == ''){
  655. $string = $k.'='.$v;
  656. }else{
  657. $string .= "&".$k.'='.$v;
  658. }
  659. }
  660. }
  661. $string .= '&key='.$key;
  662. $this->jsdLog( "加密前字符串 :".$string);
  663. $this->jsdLog( "加密后sign ".md5($string));;
  664. $info['sign'] = strtoupper(md5($string));
  665. $info['token'] = $token;
  666. // var_dump(json_encode($info));
  667. $this->jsdLog( "转换为json的数据:".json_encode($info));
  668. return $info;
  669. }
  670. public function getJsdToken($institution,$exam_id)
  671. {
  672. $username = 'pacsonline';
  673. $password = 'zskk_200408';
  674. $sysCode = '0015';
  675. $data = [
  676. 'userName'=>$username,
  677. 'password'=>$password,
  678. 'sysCode'=>$sysCode
  679. ];
  680. $url = 'http://pacs.hnjsd.com.cn:9080/pacs/api/pacs/getToken.json';
  681. $return = $this->curlPost($url,$data);
  682. $this->jsdLog($exam_id.'登陆请求的返回值'.$return);
  683. log::record($exam_id.'登陆请求的返回值'.$return);
  684. $info = json_decode($return,true);
  685. if($info['resultCode'] == '1')
  686. {
  687. $token = $info['uniqueCode'];
  688. $this->xz->setCache('jsd_token',$token,6000);
  689. return $token;
  690. }else{
  691. $this->sendDing('金盛达登陆异常,原因:'.$info['message'] ?? '',$exam_id);
  692. $this->throwError($info['message'] ?? '金盛达登陆异常',$info['resultCode'] ?? 10000);
  693. }
  694. }
  695. public function create_jsd($url,$info)
  696. {
  697. $data = $this->curlPost($url,$info);
  698. $this->jsdLog('金盛达创建请求数据为'.json_encode($info));
  699. $this->jsdLog('金盛达创建请求结果为'.$data);
  700. log::record('金盛达创建请求数据为'.json_encode($info));
  701. log::record('金盛达创建请求结果为'.$data);
  702. return $data;
  703. }
  704. public function yjkAction($params,$data,$action)
  705. {
  706. $apis = $this->xz->getApis($params['institution_id'],$action);
  707. $data = json_encode($data);
  708. $method = $apis['action'].'_'.$apis['method'];
  709. $number = rand(10000000,99999999);
  710. $time = time();
  711. $key = $apis['key'];
  712. $str = $data.$number.$time;
  713. $info = [
  714. 'timestamp'=>$time,
  715. 'random'=>$number,
  716. 'signature'=>base64_encode(hash_hmac("sha1", $str, $key, true)),
  717. 'data'=>$data
  718. ];
  719. if(method_exists($this,$method)){
  720. $this->$method($apis['url'],$info);
  721. }
  722. }
  723. public function yjkcreate_jyk($url,$info)
  724. {
  725. $data = $this->curlPost($url,$info);
  726. Log::record('医健康创建请求数据为'.$info);
  727. Log::record('医健康创建请求结果为'.$data);
  728. }
  729. public function filmCallback($params)
  730. {
  731. $key = Config::get('ins_type')[$params['ins_type']];
  732. $exam = $this->xz->getExam($key,$params[$key],$params['institution_id']);
  733. $examId = $exam['id'];
  734. if(!empty($examId)){
  735. $film = $this->xz->getInsFilmInfo($params['institution_id']);
  736. if($film['charge_mode'] == Config::get('order_type')['patientToHospital']){
  737. $water_log = $this->xz->getWaterLog($examId,$params['institution_id']);
  738. //胶片回调 必须为医院代收模式才允许创建胶片流水
  739. if(empty($water_log)){
  740. $this->save_ins_film_water($examId,$film['charge_mode'],$film['film_price'],$params['institution_id']);
  741. }
  742. }
  743. log::record('zskkccs存在回调');
  744. if(!empty($exam['id'])){
  745. if($exam['pay_status'] !== '1')
  746. {
  747. log::record('支付状态为'.$exam['pay_status']);
  748. Log::record('zskkccs胶片回调key值'.$params[$key]);
  749. $data = $this->xz->getPushInfo($exam['id']);
  750. $pushType = 5;
  751. $cont = [
  752. 'name'=>$exam['name'],
  753. 'modality'=>$exam['exam_class'],
  754. 'examDate'=>$exam['exam_datetime'],
  755. 'study_id'=>$exam['study_id']
  756. ];
  757. $content = json_encode($cont,true);
  758. $openids = $this->xz->getOpenId($data);
  759. foreach ($openids as $k=>$v){
  760. $this->new_wechat_push($v,$pushType,$content);
  761. }
  762. }
  763. }
  764. $this->xz->updateExam($examId,['pay_status'=>1]);
  765. return true;
  766. }else{
  767. $this->throwError('检查不存在','0231');
  768. }
  769. }
  770. public function jsdLog($info)
  771. {
  772. $file = ROOT_PATH . 'public' . DS . 'runtime/jsd';
  773. if(!file_exists($file)){
  774. mkdir($file,0777,true);
  775. }
  776. $date = date('Ymd',time());
  777. $value = date('Y-m-d H:i:s',time()).$info;
  778. //创建ini文件
  779. $gao_file = fopen($file.'/'.$date.".txt","a+");
  780. //替换
  781. $string = iconv("utf-8", "GB2312", $value);
  782. //附加分号和换行专
  783. $string = $string . ";" . "\r\n";
  784. //将内容写入文件
  785. fwrite($gao_file, $string);
  786. //关闭文件
  787. fclose($gao_file);
  788. }
  789. public function getDcmUrlInfo($id)
  790. {
  791. $exam = $this->xz->getExamDataById($id);
  792. $study = $exam['study_id'];
  793. $image= $this->xz->getImageInfo($study);
  794. $data = [];
  795. $i = 0;
  796. foreach ($image as $k=>$v)
  797. {
  798. $info = [];
  799. $adress = ''; //前端传值
  800. $url = $this->makeUrl($v['url'],$adress);
  801. $info = ['id'=>$v['id'],'url'=>str_replace('dicomweb','http',$url),'exam_id'=>$id,'study_id'=>$v['study_id'],'series_id'=>$v['series_id'],'sop_uid'=>$v['sopUid']];
  802. $data[] = $info;
  803. }
  804. // var_dump($data);die;
  805. // var_dump(json_encode($data,JSON_UNESCAPED_SLASHES));
  806. $this->curlJsonYbPost('http://192.168.1.51:5000/get_dcm_batch_flask/',json_encode($data,JSON_UNESCAPED_SLASHES));
  807. return true;
  808. }
  809. public function makeUrl($url,$adress)
  810. {
  811. if(!$url)
  812. {
  813. return '';
  814. }
  815. if(preg_match('/^dicomweb:\/\//',$url)){
  816. return $this->generateAliossURL($url,$adress);
  817. }
  818. if(preg_match('/^group/',$url)){
  819. return $this->generateFastdfsURL($url,$adress);
  820. }
  821. return $this->generateLcossURL($url,$adress);
  822. }
  823. public function generateAliossURL($url,$address)
  824. {
  825. if ($address) {
  826. return str_replace('zskk-dcm.oss-cn-beijing.aliyuncs.com',$address.'/oss',$url);
  827. }
  828. return $url;
  829. }
  830. public function generateFastdfsURL($url,$adress)
  831. {
  832. if($adress)
  833. {
  834. return "dicomweb://$adress/$url";
  835. }
  836. return "dicomweb://dcm.pacsonline.cn/$url";
  837. }
  838. public function wechatFix($str)
  839. {
  840. if(!$str) {
  841. return $str;
  842. }
  843. return str_replace('=','%3D',$str);
  844. }
  845. public function generateLcossURL($key,$adress)
  846. {
  847. $uCloudPublicKey = 'TOKEN_109bb131-db96-46cf-b693-745f37129bee';
  848. $uCloudPrivateKey = '515bec45-1709-41e9-862e-f05705ba47b3';
  849. $hTTPVerb = "GET";
  850. $expires = '';
  851. $canonicalizedUCloudHeaders = '';
  852. $bucket = 'zskkdcm';
  853. $canonicalizedResource = '/'.$bucket.'/'.$key;
  854. $stringToSign = $hTTPVerb."\n" .
  855. "\n" .
  856. "\n" .
  857. $expires . "\n" .
  858. $canonicalizedUCloudHeaders .
  859. $canonicalizedResource;
  860. $signature = base64_encode(hash_hmac("sha1", $stringToSign, $uCloudPrivateKey, true));
  861. $domain = 'dicomweb://'.$bucket.'.infile.inspurcloud.cn';
  862. if($adress)
  863. {
  864. $domain = 'dicomweb://'.$adress.'/lcoss';
  865. }
  866. $uCloudPublicKey = $this->wechatFix($uCloudPublicKey);
  867. $signature = $this->wechatFix($signature);
  868. $url = $domain.'/'.$key.'?UCloudPublicKey='.$uCloudPublicKey.'&Signature='.$signature.'&Expires='.$expires;
  869. return $url;
  870. // $uCloudRootPublicKey = '0cCMBBXqiuFdJrb3jy0csUwXHDEAzjPT4I8ZDvcnKlpd6RGdQ7vStHO0z1M=';
  871. // $uCloudRootPrivateKey = '8JYvmvzBIaM7W15bVqmdriR9pFohACpBMDz/w/c/XSqTjX+aL0YN8yFb9EQ2SUP1';
  872. }
  873. // public function getlsoss()
  874. //{
  875. // $data['UCloudPublicKey'] = 'TOKEN_109bb131-db96-46cf-b693-745f37129bee';
  876. // $data['UCloudPrivateKey'] = '515bec45-1709-41e9-862e-f05705ba47b3';
  877. // $data['bucket'] = 'zskkdcm';
  878. // $data['UCloudRootPublicKey'] = '0cCMBBXqiuFdJrb3jy0csUwXHDEAzjPT4I8ZDvcnKlpd6RGdQ7vStHO0z1M=';
  879. // $data['UCloudRootPrivateKey'] = '8JYvmvzBIaM7W15bVqmdriR9pFohACpBMDz/w/c/XSqTjX+aL0YN8yFb9EQ2SUP1';
  880. // return $data;
  881. //}
  882. public function saveImageNode($params)
  883. {
  884. if(empty($params))
  885. {
  886. return false;
  887. }
  888. $data = json_decode($params,true);
  889. $info = [];
  890. $num = 0;
  891. foreach ($data as $k=>$v)
  892. {
  893. if($k == ($num+1)*500 && $k != 0)
  894. {
  895. $num++;
  896. }
  897. if($num*500 <= $k && $k < ($num+1) * 500)
  898. {
  899. //500一组 防止一次性过多存储
  900. $info[$num][] = ['image_id'=>$v['id'],'image_node'=>json_encode($v['node']),'exam_id'=>$v['exam_id'],'study_id'=>$v['study_id'],'series_id'=>$v['series_id'],'sop_uid'=>$v['sop_uid']];
  901. }
  902. }
  903. foreach ($info as $v)
  904. {
  905. $this->xz->saveImageNode($v);
  906. }
  907. return true;
  908. }
  909. public function getImageNode($code,$type)
  910. {
  911. $where = [Config::get('dcm_type')[$type]=>$code];
  912. $nodeInfo = $this->xz->getImageNode($where);
  913. $array = [];
  914. foreach ($nodeInfo as $k=>$v)
  915. {
  916. $array[$k]['instanceUID'] = $v['sop_uid'];
  917. $nodeArray = json_decode($v['image_node']);
  918. $coords = [];
  919. foreach ($nodeArray as $key=>$value)
  920. {
  921. $coords['coords'][$key]['x'] = $value[0];
  922. $coords['coords'][$key]['y'] = $value[1];
  923. $coords['color'] = 'red';
  924. }
  925. $array[$k]['data'] = [$coords];
  926. }
  927. return json_encode($array);
  928. }
  929. public function getWjwData()
  930. {
  931. $data = $this->xz->getWjwData();
  932. return $data;
  933. }
  934. public function getRemoteCount()
  935. {
  936. $data = $this->xz->getRemoteCount();
  937. return $data;
  938. }
  939. public function getYjRemote($page,$num)
  940. {
  941. $data = $this->xz->getYjRemote($page,$num);
  942. return $data;
  943. }
  944. public function getYjDoctor()
  945. {
  946. $data = $this->xz->getYjDoctor();
  947. return $data;
  948. }
  949. }