123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- 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<AdapterMetaData> metaDataList = adapterDataSet.getAdapterMetaDataList();
- if (CollectionUtil.isEmpty(metaDataList)) {
- LogUtil.error("标准适配错误,请确认!平台数据集编码:"+ adapterDataSet.getAdapterDataSetT().getStdDatasetCode());
- return null;
- }
- SqlCreate sqlCreate = new SqlCreate();
- List<String> itemList = new ArrayList<>();
- Map<String, String> itemTagMap = new HashMap<>();
- List<String> conditionList = new ArrayList<>();
- ArrayList<String> 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<Object> 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<Patient> getPatientList(DBSessionFactory dbSessionFactory, String datasourceId, AdapterDataSet adapterDataSet, Map<String, Object> condition) {
- try {
- String orgCode = StringUtil.toString(condition.get("orgCode"));
- SqlCreate sqlCreate = new SqlCreate();
- List<String> itemList = new ArrayList<>();
- List<String> conditionList = new ArrayList<>();
- PatientIdentity patientIdentity = SysConfig.getInstance().getPatientIdentity(adapterDataSet.getAdapterDataSetT().getStdDatasetCode());
- Map<String, String> 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<Patient> 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
|