PatientCDAUpload.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.yihu.ehr.service.patient;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.yihu.ehr.common.config.SysConfig;
  5. import com.yihu.ehr.model.Patient;
  6. import com.yihu.ehr.util.compress.Zipper;
  7. import com.yihu.ehr.util.encrypt.MD5;
  8. import com.yihu.ehr.util.encrypt.RSA;
  9. import com.yihu.ehr.util.file.FileUtil;
  10. import com.yihu.ehr.util.http.HOPClient;
  11. import com.yihu.ehr.util.http.Response;
  12. import com.yihu.ehr.util.log.LogUtil;
  13. import com.yihu.ehr.util.operator.ConfigureUtil;
  14. import java.io.File;
  15. import java.security.Key;
  16. import java.util.UUID;
  17. /**
  18. * 档案上传
  19. *
  20. * @author Air
  21. * @version 1.0
  22. * @created 2015.07.06 15:58
  23. */
  24. public class PatientCDAUpload {
  25. public static String uploadMethod;
  26. /**
  27. * @param patient
  28. * @return
  29. * @modify 2015.09.15 airhead 修订删除目录
  30. * @modify 2015.09.19 airhead 修复无文档问题及错误信息
  31. */
  32. public Boolean upload(Patient patient) {
  33. ZipFile zipFile = zip(patient);
  34. try {
  35. if (zipFile == null || zipFile.file == null) {
  36. LogUtil.fatal("压缩病人档案失败,病人文档未生成,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  37. return false;
  38. }
  39. boolean result = upload(patient, zipFile);
  40. if (!result) {
  41. LogUtil.fatal("上传病人档案失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  42. return result;
  43. }
  44. LogUtil.trace(zipFile.directory);
  45. result = FileUtil.deleteDirectory(new File(zipFile.directory));
  46. if (!result) {
  47. LogUtil.fatal("删除临时文件失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  48. return result;
  49. }
  50. } catch (Exception e) {
  51. FileUtil.deleteDirectory(new File(zipFile.directory));
  52. return false;
  53. }
  54. return true;
  55. }
  56. /**
  57. * @param patient
  58. * @return
  59. * @modify 从data目录生成zip数据
  60. */
  61. public ZipFile zip(Patient patient) {
  62. try {
  63. PatientCDAIndex patientCDAIndex = new PatientCDAIndex(patient);
  64. String dataDirectory = patientCDAIndex.getDataDirectory();
  65. String filePath = patientCDAIndex.createIndex(PatientCDAIndex.IndexType.ZIP, PatientCDAIndex.FileType.ZIP);
  66. UUID uuidPwd = UUID.randomUUID();
  67. String pwd = uuidPwd.toString();
  68. Key key = RSA.genPublicKey(SysConfig.getInstance().getPublicKey());
  69. if (key == null) {
  70. LogUtil.fatal("压缩文件错误,获取公钥错误.");
  71. return null;
  72. }
  73. ZipFile zipFile = new ZipFile();
  74. zipFile.encryptPwd = RSA.encrypt(pwd, key);
  75. Zipper zipper = new Zipper();
  76. zipFile.file = zipper.zipFile(new File(dataDirectory), filePath, pwd);
  77. zipFile.dataDirectory = dataDirectory;
  78. zipFile.directory = patientCDAIndex.getDirectory();
  79. return zipFile;
  80. } catch (Exception e) {
  81. LogUtil.error("从data目录生成zip数据时,压缩文件异常");
  82. LogUtil.error(e);
  83. }
  84. return null;
  85. }
  86. private boolean upload(Patient patient, ZipFile zipFile) {
  87. try {
  88. if (uploadMethod == null) {
  89. uploadMethod = ConfigureUtil.getProValue(ConfigureUtil.CRAWLER_PROPERTIES, "ha.url.patient.upload");
  90. }
  91. String fileMd5= MD5.getMd5ByFile(zipFile.file);
  92. HOPClient hopClient = new HOPClient(HOPClient.HTTPS);
  93. hopClient.setAppId(SysConfig.getInstance().getPlatformAppId());
  94. hopClient.setUrl(SysConfig.getInstance().getPlatformUrl());
  95. hopClient.setMethod(uploadMethod);
  96. hopClient.setParam("md5", fileMd5);
  97. hopClient.setParam("package_crypto", zipFile.encryptPwd);
  98. hopClient.setParam("user_name", SysConfig.getInstance().getPlatformUserName());
  99. Response response = hopClient.postFile(zipFile.file.getAbsolutePath());
  100. if (response == null) {
  101. LogUtil.fatal("上传病人档案请求失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  102. return false;
  103. }
  104. if (response.statusCode != 200) {
  105. LogUtil.fatal("上传病人档案请求失败,错误代码:" + response.statusCode + ",patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  106. return false;
  107. }
  108. ObjectMapper mapper = new ObjectMapper();
  109. JsonNode rootNode = mapper.readValue(response.body, JsonNode.class);
  110. JsonNode codeNode = rootNode.get("code");
  111. String result = codeNode.asText();
  112. if (!result.equals("0")) {
  113. LogUtil.fatal("上传病人档案失败,错误代码:" + result + ",patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  114. return false;
  115. } else {
  116. LogUtil.info("上传病人档案成功,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  117. return true;
  118. }
  119. } catch (Exception e) {
  120. LogUtil.fatal("上传病人档案异常,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  121. LogUtil.error(e);
  122. return false;
  123. }
  124. }
  125. private class ZipFile {
  126. public File file;
  127. public String encryptPwd;
  128. public String directory;
  129. public String dataDirectory;
  130. }
  131. }