package com.yihu.ehr.service.crawler; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import com.yihu.ehr.common.config.SysConfig; import com.yihu.ehr.common.constants.LogicValues; import com.yihu.ehr.model.DataSource; import com.yihu.ehr.model.Patient; import com.yihu.ehr.model.PatientIdentity; import com.yihu.ehr.model.entity.adapter.AdapterDataSet; import com.yihu.ehr.model.entity.adapter.AdapterMetaData; import com.yihu.ehr.model.entity.standard.StdMetaDataT; import com.yihu.ehr.service.standard.StandardManager; import com.yihu.ehr.util.db.BeanProcessorEx; import com.yihu.ehr.util.db.DBSessionFactory; import com.yihu.ehr.util.log.LogUtil; import com.yihu.ehr.util.operator.CollectionUtil; import com.yihu.ehr.util.operator.DateUtil; import com.yihu.ehr.util.operator.SqlCreate; import com.yihu.ehr.util.operator.StringUtil; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import java.sql.SQLException; import java.util.*; public class DBOrigin { public DBOrigin() { } public void finalize() throws Throwable { } public ObjectNode fecthData(Patient patient, DBSessionFactory dbSessionFactory, AdapterDataSet adapterDataSet) { try { List metaDataList = adapterDataSet.getAdapterMetaDataList(); if (CollectionUtil.isEmpty(metaDataList)) { LogUtil.error("标准适配错误,请确认!平台数据集编码:"+ adapterDataSet.getAdapterDataSetT().getStdDatasetCode()); return null; } SqlCreate sqlCreate = new SqlCreate(); List itemList = new ArrayList<>(); Map itemTagMap = new HashMap<>(); List conditionList = new ArrayList<>(); ArrayList tagList = new ArrayList<>(); StandardManager standardManager = StandardManager.getInstance(); String innerVersion = standardManager.getCurrentVersion(); if (StringUtil.isEmpty(innerVersion)){ LogUtil.error("本地标准版本信息有误,请检查 std_inner_version表数据是否正常:"); return null; } for (AdapterMetaData metaData : metaDataList) { String stdCode = metaData.getAdapterMetaDataT().getStdMetadataCode(); String orgCode = metaData.getAdapterMetaDataT().getOrgMetadataCode(); if (stdCode != null && orgCode !=null) { itemList.add(orgCode); tagList.add(stdCode); itemTagMap.put(orgCode, stdCode); } } sqlCreate.setTableName(adapterDataSet.getAdapterDataSetT().getOrgDatasetCode()); sqlCreate.setItemList(itemList); ArrayList arrayList = new ArrayList<>(); boolean patientId = true; if (adapterDataSet.isHavePatientID()) { StdMetaDataT stdMetaDataT = adapterDataSet.getStdMetaDataMap().get(PatientIdentity.getPatientIDCode()); conditionList.add(sqlCreate.equalCondition(stdMetaDataT.getCode())); arrayList.add(patient.getPatientId()); } else { patientId = false; } boolean eventNo = true; if (adapterDataSet.isHaveEventNo()) { StdMetaDataT stdMetaDataT = adapterDataSet.getStdMetaDataMap().get(adapterDataSet.getEventNoCode()); conditionList.add(sqlCreate.equalCondition(stdMetaDataT.getCode())); arrayList.add(patient.getEventNo()); } else { eventNo = false; } if (!patientId && !eventNo) { LogUtil.error("采集病人数据集至少需要一项病人标识.数据集名:" + adapterDataSet.getAdapterDataSetT().getStdDatasetCode()); throw new Exception("采集病人数据集至少需要一项病人标识.数据集名:" + adapterDataSet.getAdapterDataSetT().getStdDatasetCode()); } sqlCreate.setConditionList(conditionList); ObjectNode jsonObject = null; try { jsonObject = dbSessionFactory.queryForJson("data", sqlCreate.selectAllData(), arrayList.toArray()); JsonNode data = jsonObject.get("data"); if (data == null || ((ArrayNode) data).size() == 0) { return null; } JSONObject json = new JSONObject(); if (data != null && data.get(0) != null) { Iterator fileNames = data.get(0).fieldNames(); while (fileNames.hasNext()) { String fieldName = (String)fileNames.next(); if (!StringUtil.isEmpty(itemTagMap.get(fieldName))) { json.put(itemTagMap.get(fieldName), data.get(0).get(fieldName).asText()); } } } if (json.keySet().size() != itemTagMap.keySet().size()) { LogUtil.info("适配数据元数量与机构提供数据元不一致,请确认!"); LogUtil.info("视图提供数据元:" + json.keySet().toString() + "适配数据元:" + itemList.toString()); } JSONArray jsonArray = new JSONArray(); jsonArray.add(0, json); ObjectMapper mapper = new ObjectMapper(); //JSON ----> JsonNode JsonNode jsonNode = mapper.readTree(jsonArray.toString()); jsonObject.put("data", jsonNode); } catch (Exception e) { LogUtil.error("数据库执行异常,无对应表视图:" + sqlCreate.selectAllData()); LogUtil.error(e); return null; } jsonObject.put("code", adapterDataSet.getAdapterDataSetT().getStdDatasetCode()); jsonObject.put("patient_id", patient.getPatientId()); jsonObject.put("event_no", patient.getEventNo()); String agencyCode = patient.getOrgCode(); jsonObject.put("org_code", agencyCode); jsonObject.put("inner_version", innerVersion); jsonObject.put("create_date", DateUtil.toString(new Date(), DateUtil.DEFAULT_YMDHMSDATE_FORMAT)); jsonObject.put("event_time", patient.getReferenceTime());//DateUtil.toString(patient.getReferenceTime(), DateUtil.DEFAULT_YMDHMSDATE_FORMAT) if (StringUtil.isEmpty(patient.getReUploadFlg())) { jsonObject.put("reUploadFlg", LogicValues.LOGIC_FALSE); } else { jsonObject.put("reUploadFlg", patient.getReUploadFlg()); } return jsonObject; } catch (Exception e) { LogUtil.error(e); } return null; } public List getPatientList(DBSessionFactory dbSessionFactory, String datasourceId, AdapterDataSet adapterDataSet, Map condition) { try { String orgCode = StringUtil.toString(condition.get("orgCode")); SqlCreate sqlCreate = new SqlCreate(); List itemList = new ArrayList<>(); List conditionList = new ArrayList<>(); PatientIdentity patientIdentity = SysConfig.getInstance().getPatientIdentity(adapterDataSet.getAdapterDataSetT().getStdDatasetCode()); Map propertyMap = new HashMap<>(); Object[] param = new Object[condition.size() - 1]; Integer count = 0; Date beginDate = (Date) condition.get("beginDate"); beginDate = OriginDateTimeManager.getInstance().getRealTime(this, dbSessionFactory, datasourceId, beginDate); Date endDate = (Date) condition.get("endDate"); endDate = OriginDateTimeManager.getInstance().getRealTime(this, dbSessionFactory, datasourceId, endDate); String patientId = (String) condition.get("patientId"); String eventNo = (String) condition.get("eventNo"); if (adapterDataSet.isHavePatientID()) { StdMetaDataT stdMetaDataT = adapterDataSet.getStdMetaData(PatientIdentity.getPatientIDCode()); itemList.add(stdMetaDataT.getCode()); propertyMap.put(stdMetaDataT.getCode().toUpperCase(), "patientId"); if (!StringUtil.isEmpty(patientId)) { param[count++] = patientId; conditionList.add(sqlCreate.likeCondition(stdMetaDataT.getCode())); } } else { throw new Exception("采集病人列表数据集必须有patient_id."); } if (adapterDataSet.isHaveEventNo()) { StdMetaDataT stdMetaDataT = adapterDataSet.getStdMetaData(patientIdentity.getEventNoCode()); itemList.add(stdMetaDataT.getCode()); propertyMap.put(stdMetaDataT.getCode().toUpperCase(), "eventNo"); if (!StringUtil.isEmpty(eventNo)) { param[count++] = eventNo; conditionList.add(sqlCreate.likeCondition(stdMetaDataT.getCode())); } } else { throw new Exception("采集病人列表数据集必须有event_no."); } StdMetaDataT stdRefMetaData = adapterDataSet.getStdMetaData(patientIdentity.getRefTimeCode()); if (stdRefMetaData == null) { throw new Exception("采集病人列表数据集必须有采集时间."); } itemList.add(stdRefMetaData.getCode()); propertyMap.put(stdRefMetaData.getCode().toUpperCase(), "referenceTime"); itemList.add("'" + orgCode + "' orgCode"); propertyMap.put(orgCode.toUpperCase(), "orgCode"); sqlCreate.setItemList(itemList); sqlCreate.setTableName(adapterDataSet.getAdapterDataSetT().getOrgDatasetCode()); conditionList.add(sqlCreate.greaterAndEqualCondition(stdRefMetaData.getCode())); conditionList.add(sqlCreate.lessCondition(stdRefMetaData.getCode())); sqlCreate.setConditionList(conditionList); sqlCreate.selectData(); BeanProcessorEx beanProcessor = new BeanProcessorEx(propertyMap); List patientList = null; try { param[count++] = beginDate; param[count++] = endDate; patientList = dbSessionFactory.queryForBeanList(Patient.class, beanProcessor, sqlCreate.selectData(), param); } catch (Exception e) { LogUtil.fatal("采集病人列表异常,等待下一次采集:"+ sqlCreate.selectData() + param); LogUtil.error(e); return null; } if (patientList == null) { return new ArrayList<>(); } return patientList; } catch (Exception e) { LogUtil.fatal("采集病人列表异常,等待下一次采集"); LogUtil.error(e); return null; } } public boolean clearData(Patient patient, DataSource dataSource, AdapterDataSet adapterDataSet) { return false; } public Date getServerDateTime(DBSessionFactory dbSessionFactory) { try { try { return dbSessionFactory.getSystemDateTime(); } catch (SQLException e) { LogUtil.error(e); } } catch (Exception e) { LogUtil.error(e); } return null; } }//end DBOrigin