package com.yihu.ehr.service.crawler; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import com.yihu.ehr.common.config.SysConfig; import com.yihu.ehr.common.constants.Constants; import com.yihu.ehr.model.Patient; import com.yihu.ehr.model.PatientIdentity; import com.yihu.ehr.model.entity.adapter.AdapterDataSet; import com.yihu.ehr.service.patient.PatientCDAIndex; import com.yihu.ehr.service.patient.PatientCDAUpload; import com.yihu.ehr.util.db.DBSessionFactory; import com.yihu.ehr.util.file.FileUtil; import com.yihu.ehr.util.httpclient.EsbHttp; import com.yihu.ehr.util.log.LogUtil; import com.yihu.ehr.util.operator.CollectionUtil; import com.yihu.ehr.util.operator.StringUtil; import java.io.IOException; import java.util.*; public class DataCollectDispatcher { public static String registerMethod; private static DataCollectDispatcher ourInstance = new DataCollectDispatcher(); private DataCollectDispatcher() { } public static DataCollectDispatcher getInstance() { return ourInstance; } public void finalize() throws Throwable { } /** * @param patient 病人 * @return true-采集成功,false-采集失败 */ public Boolean collectData(Patient patient, List adapterDataSetList, Map dataSourceMap, String token) { Boolean result = true; Map dataSetMap = new HashMap<>(); List dataList = new ArrayList<>(); try { for (String key : dataSourceMap.keySet()) { DBOrigin dataCollector = new DBOrigin(); if (dataCollector == null) { LogUtil.fatal("采集数据:无法获取采集数据源."); continue; } for (AdapterDataSet adapterDataSet : adapterDataSetList) { //采集病人信息 ObjectNode jsonObject = dataCollector.fecthData(patient, dataSourceMap.get(key), adapterDataSet); if (jsonObject == null) { continue; } if (jsonObject != null && !StringUtil.isEmpty(jsonObject)) { dataSetMap.put(adapterDataSet.getAdapterDataSetT().getStdDatasetCode(), adapterDataSet); dataList.add(jsonObject); } //注册病人 if (SysConfig.getInstance().getRegisterDataSet().equals(adapterDataSet.getAdapterDataSetT().getStdDatasetCode())) { if (!StringUtil.isEmpty(jsonObject.get("data")) && !StringUtil.isEmpty(jsonObject.get("data").get(0))) { if (!StringUtil.isEmpty(jsonObject.get("data").get(0).get(SysConfig.getInstance().getRegisterIdCardNo()))) { register(patient, StringUtil.toString(jsonObject), token); } } } } } LogUtil.info("采集病人成功,patient_id:" + patient.getPatientId() + ", event_no:" + patient.getEventNo()); } catch (Exception e) { LogUtil.error("采集病人失败,patient_id:" + patient.getPatientId() + ", event_no:" + patient.getEventNo()); LogUtil.error(e); result = false; } //上传档案 try { if (!CollectionUtil.isEmpty(dataList)) { if (!upload(dataList, patient, dataSetMap, token)) { result = false; } } } catch (Exception e) { result = false; LogUtil.error("档案上传失败,patient_id:" + patient.getPatientId() + ", event_no:" + patient.getEventNo()); } return result; } public Boolean upload(List dataList, Patient patient, Map dataSetMap, String token) { Boolean result = true; try { DataSetTransformer dataTransformer = new DataSetTransformer(); for (ObjectNode data : dataList) { dataTransformer.setData(data); if (!toFile(dataTransformer, patient, "origin")) { LogUtil.fatal("存储原始文件失败:patient_id=" + patient.getPatientId() +"event_no=" + patient.getEventNo()); result = false; break; } dataTransformer.transfer(dataSetMap); if (!toFile(dataTransformer, patient, "standard")) { LogUtil.fatal("存储标准文件失败:patient_id=" + patient.getPatientId() + "event_no=" + patient.getEventNo()); result = false; break; } } PatientCDAUpload patientCDAUpload = new PatientCDAUpload(); if (!patientCDAUpload.upload(patient, token)) { result = false; } } catch (Exception e) { result = false; } return result; } public boolean toFile(DataSetTransformer dataTransformer, Patient patient, String fileName) { ObjectNode jsonObject = dataTransformer.getJsonObject(); PatientCDAIndex patientCDAIndex = new PatientCDAIndex(patient); String filePath = patientCDAIndex.createDataIndex(fileName, PatientCDAIndex.FileType.JSON); boolean writeFile = false; try { writeFile = FileUtil.writeFile(filePath, jsonObject.toString(), "UTF-8"); } catch (IOException e) { LogUtil.fatal("存储临时文件失败."); LogUtil.error(e); } return writeFile; } /** * @param patient * @return boolean * 注册不是否成功 统一返回true */ public Boolean register(Patient patient, String data, String token) { return EsbHttp.register(patient, data, token); } /** * @param condition 查询条件 * @return 病人列表 */ public List getPatientQueue(Map condition, List adapterDataSetList, Map dataSourceMap) { List patientList = new ArrayList<>(); DBOrigin dbOrigin = new DBOrigin(); for(String key : dataSourceMap.keySet()) { DBSessionFactory dbSessionFactory = dataSourceMap.get(key); for (AdapterDataSet adapterDataSet : adapterDataSetList) { PatientIdentity patientIdentity = SysConfig.getInstance().getPatientIdentity(adapterDataSet.getAdapterDataSetT().getStdDatasetCode()); if (patientIdentity == null) { continue; } List patients = dbOrigin.getPatientList(dbSessionFactory, key, adapterDataSet, condition); if (patients == null) { return null; } if (patients.size() > 0) { patientList.addAll(patients); } } } return patientList; } /** * 解析病人索引信息 * * @param patientInfo 病人索引信息 * @return */ public Patient parsePatient(String patientInfo) throws IOException { Patient patient = null; ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = null; patient = new Patient(); patient = mapper.readValue(patientInfo, Patient.class); return patient; } /** * 解析token内容 * * @param responToken * @return */ public Map parseToken(String responToken) { ObjectMapper mapper = new ObjectMapper(); Map tokenMap = null; try { Map map = mapper.readValue(responToken, Map.class); String code = (String) map.get("code"); if (Constants.OK.equals(code)) { tokenMap = (Map) map.get("result"); } } catch (IOException e) { e.printStackTrace(); } return tokenMap; } }//end DataCollectDispatcher