DBOrigin.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package com.yihu.ehr.service.crawler;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.fasterxml.jackson.databind.node.ArrayNode;
  5. import com.fasterxml.jackson.databind.node.ObjectNode;
  6. import com.yihu.ehr.common.config.SysConfig;
  7. import com.yihu.ehr.common.constants.LogicValues;
  8. import com.yihu.ehr.model.DataSource;
  9. import com.yihu.ehr.model.Patient;
  10. import com.yihu.ehr.model.PatientIdentity;
  11. import com.yihu.ehr.model.entity.adapter.AdapterDataSet;
  12. import com.yihu.ehr.model.entity.adapter.AdapterMetaData;
  13. import com.yihu.ehr.model.entity.standard.StdMetaDataT;
  14. import com.yihu.ehr.service.standard.StandardManager;
  15. import com.yihu.ehr.util.db.BeanProcessorEx;
  16. import com.yihu.ehr.util.db.DBSessionFactory;
  17. import com.yihu.ehr.util.log.LogUtil;
  18. import com.yihu.ehr.util.operator.CollectionUtil;
  19. import com.yihu.ehr.util.operator.DateUtil;
  20. import com.yihu.ehr.util.operator.SqlCreate;
  21. import com.yihu.ehr.util.operator.StringUtil;
  22. import net.sf.json.JSONArray;
  23. import net.sf.json.JSONObject;
  24. import java.sql.SQLException;
  25. import java.util.*;
  26. public class DBOrigin {
  27. public DBOrigin() {
  28. }
  29. public void finalize() throws Throwable {
  30. }
  31. public ObjectNode fecthData(Patient patient, DBSessionFactory dbSessionFactory, AdapterDataSet adapterDataSet) {
  32. try {
  33. List<AdapterMetaData> metaDataList = adapterDataSet.getAdapterMetaDataList();
  34. if (CollectionUtil.isEmpty(metaDataList)) {
  35. LogUtil.error("标准适配错误,请确认!平台数据集编码:"+ adapterDataSet.getAdapterDataSetT().getStdDatasetCode());
  36. return null;
  37. }
  38. SqlCreate sqlCreate = new SqlCreate();
  39. List<String> itemList = new ArrayList<>();
  40. Map<String, String> itemTagMap = new HashMap<>();
  41. List<String> conditionList = new ArrayList<>();
  42. ArrayList<String> tagList = new ArrayList<>();
  43. StandardManager standardManager = StandardManager.getInstance();
  44. String innerVersion = standardManager.getCurrentVersion();
  45. if (StringUtil.isEmpty(innerVersion)){
  46. LogUtil.error("本地标准版本信息有误,请检查 std_inner_version表数据是否正常:");
  47. return null;
  48. }
  49. for (AdapterMetaData metaData : metaDataList) {
  50. String stdCode = metaData.getAdapterMetaDataT().getStdMetadataCode();
  51. String orgCode = metaData.getAdapterMetaDataT().getOrgMetadataCode();
  52. if (stdCode != null && orgCode !=null) {
  53. itemList.add(orgCode);
  54. tagList.add(stdCode);
  55. itemTagMap.put(orgCode, stdCode);
  56. }
  57. }
  58. sqlCreate.setTableName(adapterDataSet.getAdapterDataSetT().getOrgDatasetCode());
  59. sqlCreate.setItemList(itemList);
  60. ArrayList<Object> arrayList = new ArrayList<>();
  61. boolean patientId = true;
  62. if (adapterDataSet.isHavePatientID()) {
  63. StdMetaDataT stdMetaDataT = adapterDataSet.getStdMetaDataMap().get(PatientIdentity.getPatientIDCode());
  64. conditionList.add(sqlCreate.equalCondition(stdMetaDataT.getCode()));
  65. arrayList.add(patient.getPatientId());
  66. } else {
  67. patientId = false;
  68. }
  69. boolean eventNo = true;
  70. if (adapterDataSet.isHaveEventNo()) {
  71. StdMetaDataT stdMetaDataT = adapterDataSet.getStdMetaDataMap().get(adapterDataSet.getEventNoCode());
  72. conditionList.add(sqlCreate.equalCondition(stdMetaDataT.getCode()));
  73. arrayList.add(patient.getEventNo());
  74. } else {
  75. eventNo = false;
  76. }
  77. if (!patientId && !eventNo) {
  78. LogUtil.error("采集病人数据集至少需要一项病人标识.数据集名:" + adapterDataSet.getAdapterDataSetT().getStdDatasetCode());
  79. throw new Exception("采集病人数据集至少需要一项病人标识.数据集名:" + adapterDataSet.getAdapterDataSetT().getStdDatasetCode());
  80. }
  81. sqlCreate.setConditionList(conditionList);
  82. ObjectNode jsonObject = null;
  83. try {
  84. jsonObject = dbSessionFactory.queryForJson("data", sqlCreate.selectAllData(), arrayList.toArray());
  85. JsonNode data = jsonObject.get("data");
  86. if (data == null || ((ArrayNode) data).size() == 0) {
  87. return null;
  88. }
  89. JSONObject json = new JSONObject();
  90. if (data != null && data.get(0) != null) {
  91. Iterator fileNames = data.get(0).fieldNames();
  92. while (fileNames.hasNext()) {
  93. String fieldName = (String)fileNames.next();
  94. if (!StringUtil.isEmpty(itemTagMap.get(fieldName))) {
  95. json.put(itemTagMap.get(fieldName), data.get(0).get(fieldName).asText());
  96. }
  97. }
  98. }
  99. if (json.keySet().size() != itemTagMap.keySet().size()) {
  100. LogUtil.info("适配数据元数量与机构提供数据元不一致,请确认!");
  101. LogUtil.info("视图提供数据元:" + json.keySet().toString() + "适配数据元:" + itemList.toString());
  102. }
  103. JSONArray jsonArray = new JSONArray();
  104. jsonArray.add(0, json);
  105. ObjectMapper mapper = new ObjectMapper();
  106. //JSON ----> JsonNode
  107. JsonNode jsonNode = mapper.readTree(jsonArray.toString());
  108. jsonObject.put("data", jsonNode);
  109. } catch (Exception e) {
  110. LogUtil.error("数据库执行异常,无对应表视图:" + sqlCreate.selectAllData());
  111. LogUtil.error(e);
  112. return null;
  113. }
  114. jsonObject.put("code", adapterDataSet.getAdapterDataSetT().getStdDatasetCode());
  115. jsonObject.put("patient_id", patient.getPatientId());
  116. jsonObject.put("event_no", patient.getEventNo());
  117. String agencyCode = patient.getOrgCode();
  118. jsonObject.put("org_code", agencyCode);
  119. jsonObject.put("inner_version", innerVersion);
  120. jsonObject.put("create_date", DateUtil.toString(new Date(), DateUtil.DEFAULT_YMDHMSDATE_FORMAT));
  121. jsonObject.put("event_time", patient.getReferenceTime());//DateUtil.toString(patient.getReferenceTime(), DateUtil.DEFAULT_YMDHMSDATE_FORMAT)
  122. if (StringUtil.isEmpty(patient.getReUploadFlg())) {
  123. jsonObject.put("reUploadFlg", LogicValues.LOGIC_FALSE);
  124. } else {
  125. jsonObject.put("reUploadFlg", patient.getReUploadFlg());
  126. }
  127. return jsonObject;
  128. } catch (Exception e) {
  129. LogUtil.error(e);
  130. }
  131. return null;
  132. }
  133. public List<Patient> getPatientList(DBSessionFactory dbSessionFactory, String datasourceId, AdapterDataSet adapterDataSet, Map<String, Object> condition) {
  134. try {
  135. String orgCode = StringUtil.toString(condition.get("orgCode"));
  136. SqlCreate sqlCreate = new SqlCreate();
  137. List<String> itemList = new ArrayList<>();
  138. List<String> conditionList = new ArrayList<>();
  139. PatientIdentity patientIdentity = SysConfig.getInstance().getPatientIdentity(adapterDataSet.getAdapterDataSetT().getStdDatasetCode());
  140. Map<String, String> propertyMap = new HashMap<>();
  141. Object[] param = new Object[condition.size() - 1];
  142. Integer count = 0;
  143. Date beginDate = (Date) condition.get("beginDate");
  144. beginDate = OriginDateTimeManager.getInstance().getRealTime(this, dbSessionFactory, datasourceId, beginDate);
  145. Date endDate = (Date) condition.get("endDate");
  146. endDate = OriginDateTimeManager.getInstance().getRealTime(this, dbSessionFactory, datasourceId, endDate);
  147. String patientId = (String) condition.get("patientId");
  148. String eventNo = (String) condition.get("eventNo");
  149. if (adapterDataSet.isHavePatientID()) {
  150. StdMetaDataT stdMetaDataT = adapterDataSet.getStdMetaData(PatientIdentity.getPatientIDCode());
  151. itemList.add(stdMetaDataT.getCode());
  152. propertyMap.put(stdMetaDataT.getCode().toUpperCase(), "patientId");
  153. if (!StringUtil.isEmpty(patientId)) {
  154. param[count++] = patientId;
  155. conditionList.add(sqlCreate.likeCondition(stdMetaDataT.getCode()));
  156. }
  157. } else {
  158. throw new Exception("采集病人列表数据集必须有patient_id.");
  159. }
  160. if (adapterDataSet.isHaveEventNo()) {
  161. StdMetaDataT stdMetaDataT = adapterDataSet.getStdMetaData(patientIdentity.getEventNoCode());
  162. itemList.add(stdMetaDataT.getCode());
  163. propertyMap.put(stdMetaDataT.getCode().toUpperCase(), "eventNo");
  164. if (!StringUtil.isEmpty(eventNo)) {
  165. param[count++] = eventNo;
  166. conditionList.add(sqlCreate.likeCondition(stdMetaDataT.getCode()));
  167. }
  168. } else {
  169. throw new Exception("采集病人列表数据集必须有event_no.");
  170. }
  171. StdMetaDataT stdRefMetaData = adapterDataSet.getStdMetaData(patientIdentity.getRefTimeCode());
  172. if (stdRefMetaData == null) {
  173. throw new Exception("采集病人列表数据集必须有采集时间.");
  174. }
  175. itemList.add(stdRefMetaData.getCode());
  176. propertyMap.put(stdRefMetaData.getCode().toUpperCase(), "referenceTime");
  177. itemList.add("'" + orgCode + "' orgCode");
  178. propertyMap.put(orgCode.toUpperCase(), "orgCode");
  179. sqlCreate.setItemList(itemList);
  180. sqlCreate.setTableName(adapterDataSet.getAdapterDataSetT().getOrgDatasetCode());
  181. conditionList.add(sqlCreate.greaterAndEqualCondition(stdRefMetaData.getCode()));
  182. conditionList.add(sqlCreate.lessCondition(stdRefMetaData.getCode()));
  183. sqlCreate.setConditionList(conditionList);
  184. sqlCreate.selectData();
  185. BeanProcessorEx beanProcessor = new BeanProcessorEx(propertyMap);
  186. List<Patient> patientList = null;
  187. try {
  188. param[count++] = beginDate;
  189. param[count++] = endDate;
  190. patientList = dbSessionFactory.queryForBeanList(Patient.class, beanProcessor, sqlCreate.selectData(), param);
  191. } catch (Exception e) {
  192. LogUtil.fatal("采集病人列表异常,等待下一次采集:"+ sqlCreate.selectData() + param);
  193. LogUtil.error(e);
  194. return null;
  195. }
  196. if (patientList == null) {
  197. return new ArrayList<>();
  198. }
  199. return patientList;
  200. } catch (Exception e) {
  201. LogUtil.fatal("采集病人列表异常,等待下一次采集");
  202. LogUtil.error(e);
  203. return null;
  204. }
  205. }
  206. public boolean clearData(Patient patient, DataSource dataSource, AdapterDataSet adapterDataSet) {
  207. return false;
  208. }
  209. public Date getServerDateTime(DBSessionFactory dbSessionFactory) {
  210. try {
  211. try {
  212. return dbSessionFactory.getSystemDateTime();
  213. } catch (SQLException e) {
  214. LogUtil.error(e);
  215. }
  216. } catch (Exception e) {
  217. LogUtil.error(e);
  218. }
  219. return null;
  220. }
  221. }//end DBOrigin