DBOrigin.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. JSONArray jsonArray = new JSONArray();
  90. for(int i=0;i<data.size();i++)
  91. {
  92. JsonNode o = data.get(i);
  93. JSONObject json = new JSONObject();
  94. Iterator fileNames = o.fieldNames();
  95. while (fileNames.hasNext()) {
  96. String fieldName = (String)fileNames.next();
  97. if (!StringUtil.isEmpty(itemTagMap.get(fieldName))) {
  98. json.put(itemTagMap.get(fieldName), o.get(fieldName).asText());
  99. }
  100. }
  101. if (json.keySet().size() != itemTagMap.keySet().size()) {
  102. LogUtil.info("适配数据元数量与机构提供数据元不一致,请确认!");
  103. LogUtil.info("视图提供数据元:" + json.keySet().toString() + "适配数据元:" + itemList.toString());
  104. }
  105. jsonArray.add(json);
  106. }
  107. ObjectMapper mapper = new ObjectMapper();
  108. //JSON ----> JsonNode
  109. JsonNode jsonNode = mapper.readTree(jsonArray.toString());
  110. jsonObject.put("data", jsonNode);
  111. } catch (Exception e) {
  112. LogUtil.error("数据库执行异常,无对应表视图:" + sqlCreate.selectAllData());
  113. LogUtil.error(e);
  114. return null;
  115. }
  116. jsonObject.put("code", adapterDataSet.getAdapterDataSetT().getStdDatasetCode());
  117. jsonObject.put("patient_id", patient.getPatientId());
  118. jsonObject.put("event_no", patient.getEventNo());
  119. String agencyCode = patient.getOrgCode();
  120. jsonObject.put("org_code", agencyCode);
  121. jsonObject.put("inner_version", innerVersion);
  122. jsonObject.put("create_date", DateUtil.toString(new Date(), DateUtil.DEFAULT_YMDHMSDATE_FORMAT));
  123. jsonObject.put("event_time", patient.getReferenceTime());//DateUtil.toString(patient.getReferenceTime(), DateUtil.DEFAULT_YMDHMSDATE_FORMAT)
  124. if (StringUtil.isEmpty(patient.getReUploadFlg())) {
  125. jsonObject.put("reUploadFlg", LogicValues.LOGIC_FALSE);
  126. } else {
  127. jsonObject.put("reUploadFlg", patient.getReUploadFlg());
  128. }
  129. return jsonObject;
  130. } catch (Exception e) {
  131. LogUtil.error(e);
  132. }
  133. return null;
  134. }
  135. public List<Patient> getPatientList(DBSessionFactory dbSessionFactory, String datasourceId, AdapterDataSet adapterDataSet, Map<String, Object> condition) {
  136. try {
  137. String orgCode = StringUtil.toString(condition.get("orgCode"));
  138. SqlCreate sqlCreate = new SqlCreate();
  139. List<String> itemList = new ArrayList<>();
  140. List<String> conditionList = new ArrayList<>();
  141. PatientIdentity patientIdentity = SysConfig.getInstance().getPatientIdentity(adapterDataSet.getAdapterDataSetT().getStdDatasetCode());
  142. Map<String, String> propertyMap = new HashMap<>();
  143. Object[] param = new Object[condition.size() - 1];
  144. Integer count = 0;
  145. Date beginDate = (Date) condition.get("beginDate");
  146. beginDate = OriginDateTimeManager.getInstance().getRealTime(this, dbSessionFactory, datasourceId, beginDate);
  147. Date endDate = (Date) condition.get("endDate");
  148. endDate = OriginDateTimeManager.getInstance().getRealTime(this, dbSessionFactory, datasourceId, endDate);
  149. String patientId = (String) condition.get("patientId");
  150. String eventNo = (String) condition.get("eventNo");
  151. if (adapterDataSet.isHavePatientID()) {
  152. StdMetaDataT stdMetaDataT = adapterDataSet.getStdMetaData(PatientIdentity.getPatientIDCode());
  153. itemList.add(stdMetaDataT.getCode());
  154. propertyMap.put(stdMetaDataT.getCode().toUpperCase(), "patientId");
  155. if (!StringUtil.isEmpty(patientId)) {
  156. param[count++] = patientId;
  157. conditionList.add(sqlCreate.likeCondition(stdMetaDataT.getCode()));
  158. }
  159. } else {
  160. throw new Exception("采集病人列表数据集必须有patient_id.");
  161. }
  162. if (adapterDataSet.isHaveEventNo()) {
  163. StdMetaDataT stdMetaDataT = adapterDataSet.getStdMetaData(patientIdentity.getEventNoCode());
  164. itemList.add(stdMetaDataT.getCode());
  165. propertyMap.put(stdMetaDataT.getCode().toUpperCase(), "eventNo");
  166. if (!StringUtil.isEmpty(eventNo)) {
  167. param[count++] = eventNo;
  168. conditionList.add(sqlCreate.likeCondition(stdMetaDataT.getCode()));
  169. }
  170. } else {
  171. throw new Exception("采集病人列表数据集必须有event_no.");
  172. }
  173. StdMetaDataT stdRefMetaData = adapterDataSet.getStdMetaData(patientIdentity.getRefTimeCode());
  174. if (stdRefMetaData == null) {
  175. throw new Exception("采集病人列表数据集必须有采集时间.");
  176. }
  177. itemList.add(stdRefMetaData.getCode());
  178. propertyMap.put(stdRefMetaData.getCode().toUpperCase(), "referenceTime");
  179. itemList.add("'" + orgCode + "' orgCode");
  180. propertyMap.put(orgCode.toUpperCase(), "orgCode");
  181. sqlCreate.setItemList(itemList);
  182. sqlCreate.setTableName(adapterDataSet.getAdapterDataSetT().getOrgDatasetCode());
  183. conditionList.add(sqlCreate.greaterAndEqualCondition(stdRefMetaData.getCode()));
  184. conditionList.add(sqlCreate.lessCondition(stdRefMetaData.getCode()));
  185. sqlCreate.setConditionList(conditionList);
  186. sqlCreate.selectData();
  187. BeanProcessorEx beanProcessor = new BeanProcessorEx(propertyMap);
  188. List<Patient> patientList = null;
  189. try {
  190. param[count++] = beginDate;
  191. param[count++] = endDate;
  192. patientList = dbSessionFactory.queryForBeanList(Patient.class, beanProcessor, sqlCreate.selectData(), param);
  193. } catch (Exception e) {
  194. LogUtil.fatal("采集病人列表异常,等待下一次采集:"+ sqlCreate.selectData() + param);
  195. LogUtil.error(e);
  196. return null;
  197. }
  198. if (patientList == null) {
  199. return new ArrayList<>();
  200. }
  201. return patientList;
  202. } catch (Exception e) {
  203. LogUtil.fatal("采集病人列表异常,等待下一次采集");
  204. LogUtil.error(e);
  205. return null;
  206. }
  207. }
  208. public boolean clearData(Patient patient, DataSource dataSource, AdapterDataSet adapterDataSet) {
  209. return false;
  210. }
  211. public Date getServerDateTime(DBSessionFactory dbSessionFactory) {
  212. try {
  213. try {
  214. return dbSessionFactory.getSystemDateTime();
  215. } catch (SQLException e) {
  216. LogUtil.error(e);
  217. }
  218. } catch (Exception e) {
  219. LogUtil.error(e);
  220. }
  221. return null;
  222. }
  223. }//end DBOrigin