123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package com.yihu.ehr.service.patient;
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.yihu.ehr.common.config.SysConfig;
- import com.yihu.ehr.model.Patient;
- import com.yihu.ehr.util.compress.Zipper;
- import com.yihu.ehr.util.encrypt.MD5;
- import com.yihu.ehr.util.encrypt.RSA;
- import com.yihu.ehr.util.file.FileUtil;
- import com.yihu.ehr.util.http.HOPClient;
- import com.yihu.ehr.util.http.Response;
- import com.yihu.ehr.util.log.LogUtil;
- import com.yihu.ehr.util.operator.ConfigureUtil;
- import java.io.File;
- import java.security.Key;
- import java.util.UUID;
- /**
- * 档案上传
- *
- * @author Air
- * @version 1.0
- * @created 2015.07.06 15:58
- */
- public class PatientCDAUpload {
- public static String uploadMethod;
- /**
- * @param patient
- * @return
- * @modify 2015.09.15 airhead 修订删除目录
- * @modify 2015.09.19 airhead 修复无文档问题及错误信息
- */
- public Boolean upload(Patient patient) {
- ZipFile zipFile = zip(patient);
- try {
- if (zipFile == null || zipFile.file == null) {
- LogUtil.fatal("压缩病人档案失败,病人文档未生成,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return false;
- }
- boolean result = upload(patient, zipFile);
- if (!result) {
- LogUtil.fatal("上传病人档案失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return result;
- }
- LogUtil.trace(zipFile.directory);
- result = FileUtil.deleteDirectory(new File(zipFile.directory));
- if (!result) {
- LogUtil.fatal("删除临时文件失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return result;
- }
- } catch (Exception e) {
- FileUtil.deleteDirectory(new File(zipFile.directory));
- return false;
- }
- return true;
- }
- /**
- * @param patient
- * @return
- * @modify 从data目录生成zip数据
- */
- public ZipFile zip(Patient patient) {
- try {
- PatientCDAIndex patientCDAIndex = new PatientCDAIndex(patient);
- String dataDirectory = patientCDAIndex.getDataDirectory();
- String filePath = patientCDAIndex.createIndex(PatientCDAIndex.IndexType.ZIP, PatientCDAIndex.FileType.ZIP);
- UUID uuidPwd = UUID.randomUUID();
- String pwd = uuidPwd.toString();
- Key key = RSA.genPublicKey(SysConfig.getInstance().getPublicKey());
- if (key == null) {
- LogUtil.fatal("压缩文件错误,获取公钥错误.");
- return null;
- }
- ZipFile zipFile = new ZipFile();
- zipFile.encryptPwd = RSA.encrypt(pwd, key);
- Zipper zipper = new Zipper();
- zipFile.file = zipper.zipFile(new File(dataDirectory), filePath, pwd);
- zipFile.dataDirectory = dataDirectory;
- zipFile.directory = patientCDAIndex.getDirectory();
- return zipFile;
- } catch (Exception e) {
- LogUtil.error("从data目录生成zip数据时,压缩文件异常");
- LogUtil.error(e);
- }
- return null;
- }
- private boolean upload(Patient patient, ZipFile zipFile) {
- try {
- if (uploadMethod == null) {
- uploadMethod = ConfigureUtil.getProValue(ConfigureUtil.CRAWLER_PROPERTIES, "ha.url.patient.upload");
- }
- String fileMd5= MD5.getMd5ByFile(zipFile.file);
- HOPClient hopClient = new HOPClient(HOPClient.HTTPS);
- hopClient.setAppId(SysConfig.getInstance().getPlatformAppId());
- hopClient.setUrl(SysConfig.getInstance().getPlatformUrl());
- hopClient.setMethod(uploadMethod);
- hopClient.setParam("md5", fileMd5);
- hopClient.setParam("package_crypto", zipFile.encryptPwd);
- hopClient.setParam("user_name", SysConfig.getInstance().getPlatformUserName());
- Response response = hopClient.postFile(zipFile.file.getAbsolutePath());
- if (response == null) {
- LogUtil.fatal("上传病人档案请求失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return false;
- }
- if (response.statusCode != 200) {
- LogUtil.fatal("上传病人档案请求失败,错误代码:" + response.statusCode + ",patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return false;
- }
- ObjectMapper mapper = new ObjectMapper();
- JsonNode rootNode = mapper.readValue(response.body, JsonNode.class);
- JsonNode codeNode = rootNode.get("code");
- String result = codeNode.asText();
- if (!result.equals("0")) {
- LogUtil.fatal("上传病人档案失败,错误代码:" + result + ",patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return false;
- } else {
- LogUtil.info("上传病人档案成功,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return true;
- }
- } catch (Exception e) {
- LogUtil.fatal("上传病人档案异常,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- LogUtil.error(e);
- return false;
- }
- }
- private class ZipFile {
- public File file;
- public String encryptPwd;
- public String directory;
- public String dataDirectory;
- }
- }
|