ProfileEventService.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package com.yihu.ehr.profile.service;
  2. import com.yihu.ehr.profile.family.ResourceCells;
  3. import com.yihu.ehr.profile.util.SimpleSolrQueryUtil;
  4. import com.yihu.ehr.util.rest.Envelop;
  5. import org.apache.commons.lang.time.DateUtils;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.util.StringUtils;
  8. import java.text.DateFormat;
  9. import java.text.SimpleDateFormat;
  10. import java.util.*;
  11. /**
  12. * @author hzp 2016-06-27
  13. */
  14. @Service
  15. public class ProfileEventService extends ProfileBasicService {
  16. /**
  17. *
  18. * @param demographicId
  19. * @param filter
  20. * 1. key=value的形式,多个条件用;分隔
  21. * @param blurryType
  22. * 1. 针对需要对特殊档案类型进行查询的参数(0-门诊 1-住院 2-体检 3-影像 4-检查 5-检验 6-妇幼 7-免疫)
  23. * 2. 此处有值的话filter参数中不能再包含event_type
  24. * @param date
  25. * 1. {"start":"2018-01-01T00:00:00Z","end":"2018-02-01T00:00:00Z","month":"2018-03"}
  26. * @param searchParam
  27. * 1. 此参数只针对机构和诊断
  28. * @return
  29. * @throws Exception
  30. */
  31. public List<Map<String,Object>> visits (String demographicId, String filter, String blurryType, String date, String searchParam) throws Exception {
  32. List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
  33. String query = "{\"q\":\"demographic_id:" + demographicId + "\"}";
  34. Envelop envelop;
  35. if (!StringUtils.isEmpty(blurryType)) {
  36. if ("0".equals(blurryType)) { //门诊 clinic
  37. query = "{\"q\":\"demographic_id:" + demographicId + " AND event_type:0\"}";
  38. } else if ("1".equals(blurryType)) { //住院 resident
  39. query = "{\"q\":\"demographic_id:" + demographicId + " AND event_type:1\"}";
  40. } else if ("2".equals(blurryType)) { //体检 medicalExam
  41. query = "{\"q\":\"demographic_id:" + demographicId + " AND event_type:2\"}";
  42. } else if ("3".equals(blurryType)) { //影像 imagery
  43. Envelop envelop1 = resource.healthFile("profile_type=2;id_card_no=" + demographicId, "-event_date", 1, 1000);
  44. List<Map<String, Object>> list1 = envelop1.getDetailModelList();
  45. if (list1 != null && list1.size() > 0) {
  46. for (Map<String, Object> temp : list1) {
  47. Map<String, Object> resultMap = simpleEvent(temp, searchParam);
  48. if (resultMap != null) {
  49. resultMap.put("eventType", blurryType);
  50. resultMap.put("mark", "mark"); //临时处理
  51. resultList.add(resultMap);
  52. }
  53. }
  54. }
  55. return resultList;
  56. } else if ("4".equals(blurryType)) { //检查 inspect
  57. query = SimpleSolrQueryUtil.getQuery(filter, date, query);
  58. envelop = resource.getMasterData(query, 1, 1000, null);
  59. List<Map<String, Object>> eventList = envelop.getDetailModelList();
  60. if (eventList != null && eventList.size() > 0) {
  61. for (Map<String, Object> temp : eventList) {
  62. String subQ = "{\"q\":\"rowkey:" + temp.get("rowkey") + "$HDSD00_79$*\"}";
  63. Envelop subEnvelop = resource.getSubData(subQ, 1, 1, null);
  64. List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
  65. if (subList != null && subList.size() > 0) {
  66. Map<String, Object> resultMap = simpleEvent(temp, searchParam);
  67. if (resultMap != null) {
  68. resultMap.put("eventType", blurryType);
  69. resultMap.put("mark", subList.get(0).get("EHR_000316")); //检查报告单号
  70. resultList.add(resultMap);
  71. }
  72. }
  73. }
  74. }
  75. return resultList;
  76. } else if ("5".equals(blurryType)) { //检验 examine
  77. query = SimpleSolrQueryUtil.getQuery(filter, date, query);
  78. envelop = resource.getMasterData(query, 1, 1000, null);
  79. List<Map<String, Object>> eventList = envelop.getDetailModelList();
  80. if (eventList != null && eventList.size() > 0) {
  81. for (Map<String, Object> temp : eventList) {
  82. String subQ = "{\"q\":\"rowkey:" + temp.get("rowkey") + "$HDSD00_77$*\"}";
  83. Envelop subEnvelop = resource.getSubData(subQ, 1, 1, null);
  84. List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
  85. if (subList != null && subList.size() > 0) {
  86. Map<String, Object> resultMap = simpleEvent(temp, searchParam);
  87. if (resultMap != null) {
  88. resultMap.put("eventType", blurryType);
  89. resultMap.put("mark", subList.get(0).get("EHR_000363")); //检验报告单号
  90. resultList.add(resultMap);
  91. }
  92. }
  93. }
  94. }
  95. return resultList;
  96. } else if ("6".equals(blurryType)) { //妇幼 immunity
  97. //query = "{\"q\":\"demographic_id:" + demographicId + " AND EHR_002443:*\"}";
  98. return resultList;
  99. } else if ("7".equals(blurryType)){ //免疫 immunity
  100. query = SimpleSolrQueryUtil.getQuery(filter, date, query);
  101. envelop = resource.getMasterData(query, 1, 1000, null);
  102. List<Map<String, Object>> eventList = envelop.getDetailModelList();
  103. if (eventList != null && eventList.size() > 0) {
  104. for (Map<String, Object> temp : eventList) {
  105. String subQ = "{\"q\":\"rowkey:" + temp.get("rowkey") + "$HDSB03_12$*\"}";
  106. Envelop subEnvelop = resource.getSubData(subQ, 1, 1, null);
  107. List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
  108. if (subList != null && subList.size() > 0) {
  109. Map<String, Object> resultMap = simpleEvent(temp, searchParam);
  110. if (resultMap != null) {
  111. resultMap.put("eventType", blurryType);
  112. resultMap.put("mark", subList.get(0).get("EHR_002438")); //预防接种卡编号
  113. resultList.add(resultMap);
  114. }
  115. }
  116. }
  117. }
  118. return resultList;
  119. } else {
  120. return resultList;
  121. }
  122. query = SimpleSolrQueryUtil.getQuery(filter, date, query);
  123. envelop = resource.getMasterData(query, 1, 1000, null);
  124. List<Map<String, Object>> eventList = envelop.getDetailModelList();
  125. if (eventList != null && eventList.size() > 0) {
  126. for (Map<String ,Object> temp : eventList) {
  127. Map<String, Object> resultMap = simpleEvent(temp, searchParam);
  128. if (resultMap != null) {
  129. resultMap.put("eventType", blurryType);
  130. resultList.add(resultMap);
  131. }
  132. }
  133. }
  134. return resultList;
  135. } else {
  136. query = SimpleSolrQueryUtil.getQuery(filter, date, query);
  137. envelop = resource.getMasterData(query, 1, 1000, null);
  138. List<Map<String, Object>> eventList = envelop.getDetailModelList();
  139. if (eventList != null && eventList.size() > 0) {
  140. for (Map<String ,Object> temp : eventList) {
  141. Map<String, Object> resultMap = simpleEvent(temp, searchParam);
  142. if (resultMap != null) {
  143. resultList.add(resultMap);
  144. }
  145. }
  146. }
  147. }
  148. return resultList;
  149. }
  150. /**
  151. * 获取最近的一条就诊信息
  152. * @param demographicId
  153. * @return
  154. * @throws Exception
  155. */
  156. public Map<String, Object> recentVisit (String demographicId, Integer days) throws Exception {
  157. String q = "{\"q\":\"demographic_id:" + demographicId + "\"}";
  158. Calendar calendar = Calendar.getInstance();
  159. calendar.set(Calendar.HOUR_OF_DAY, 0);
  160. calendar.set(Calendar.MINUTE, 0);
  161. calendar.set(Calendar.SECOND, 0);
  162. calendar.set(Calendar.MILLISECOND, 0);
  163. Date now = calendar.getTime();
  164. Date before = DateUtils.addDays(now, days);
  165. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  166. String start = dateFormat.format(before);
  167. String end = dateFormat.format(now);
  168. String date = "{\"start\":\"" + start + "\",\"end\":\"" + end + "\"}";
  169. q = SimpleSolrQueryUtil.getQuery(null, date, q);
  170. Envelop envelop = resource.getMasterData(q, 1, 500, null);
  171. List<Map<String, Object>> eventList = envelop.getDetailModelList();
  172. for (Map<String, Object> temp : eventList) {
  173. if (temp.get(ResourceCells.EVENT_TYPE) != null && temp.get(ResourceCells.EVENT_TYPE).equals("2")) {
  174. continue;
  175. }
  176. return simpleEvent(temp, null);
  177. }
  178. return new HashMap<>();
  179. }
  180. /**
  181. * 获取半年内的就诊记录
  182. * @param demographicId
  183. * @return
  184. * @throws Exception
  185. */
  186. public List<Map<String, Object>> recentVisits (String demographicId, Integer days) throws Exception {
  187. List<Map<String, Object>> resultList = new ArrayList<>();
  188. String q = "{\"q\":\"demographic_id:" + demographicId + "\"}";
  189. Calendar calendar = Calendar.getInstance();
  190. calendar.set(Calendar.HOUR_OF_DAY, 0);
  191. calendar.set(Calendar.MINUTE, 0);
  192. calendar.set(Calendar.SECOND, 0);
  193. calendar.set(Calendar.MILLISECOND, 0);
  194. Date now = calendar.getTime();
  195. Date before = DateUtils.addDays(now, days);
  196. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  197. String start = dateFormat.format(before);
  198. String end = dateFormat.format(DateUtils.addDays(now, 1));
  199. String date = "{\"start\":\"" + start + "\",\"end\":\"" + end + "\"}";
  200. q = SimpleSolrQueryUtil.getQuery(null, date, q);
  201. Envelop envelop = resource.getMasterData(q, 1, 500, null);
  202. List<Map<String, Object>> eventList = envelop.getDetailModelList();
  203. for (Map<String, Object> temp : eventList) {
  204. if (temp.get(ResourceCells.EVENT_TYPE) != null && temp.get(ResourceCells.EVENT_TYPE).equals("2")) {
  205. continue;
  206. }
  207. resultList.add(simpleEvent(temp, null));
  208. }
  209. return resultList;
  210. }
  211. public Map<String, Object> recentVisitsSub (String profileId) throws Exception {
  212. String q = "{\"q\":\"rowkey:" + profileId + "\"}";
  213. Envelop envelop = resource.getMasterData(q, 1, 1, null);
  214. List<Map<String, Object>> eventList = envelop.getDetailModelList();
  215. if (eventList.size() > 0) {
  216. Map<String ,Object> temp = eventList.get(0);
  217. Map<String, Object> resultMap = simpleEvent(temp, null);
  218. List<Map<String, Object>> diagnosisList = new ArrayList<>();
  219. if (!StringUtils.isEmpty(temp.get(ResourceCells.DIAGNOSIS))) {
  220. String [] diagnosisCode = ((String) temp.get(ResourceCells.DIAGNOSIS)).split(";");
  221. Calendar calendar = Calendar.getInstance();
  222. calendar.set(Calendar.HOUR_OF_DAY, 0);
  223. calendar.set(Calendar.MINUTE, 0);
  224. calendar.set(Calendar.SECOND, 0);
  225. calendar.set(Calendar.MILLISECOND, 0);
  226. Date now = calendar.getTime();
  227. Date before = DateUtils.addDays(now, -180);
  228. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  229. String start = dateFormat.format(before);
  230. String end = dateFormat.format(DateUtils.addDays(now, 1));
  231. String date = "{\"start\":\"" + start + "\",\"end\":\"" + end + "\"}";
  232. for (String code : diagnosisCode) {
  233. q = SimpleSolrQueryUtil.getQuery("diagnosis?" + code, date, q);
  234. envelop = resource.getMasterData(q, 1, 500, null);
  235. String name = redisService.getIcd10Name(code);
  236. Map<String, Object> diagnosisMap = new HashMap<>();
  237. diagnosisMap.put("healthProblemName", name == null ? "自定义" : name);
  238. diagnosisMap.put("count", envelop.getDetailModelList().size());
  239. diagnosisList.add(diagnosisMap);
  240. }
  241. }
  242. resultMap.put("diagnosis", diagnosisList);
  243. String subQ = "{\"q\":\"profile_id:" + profileId + "\"}";
  244. Envelop subEnvelop = resource.getSubData(subQ, 1, 1000, null);
  245. List<Map<String, Object>> subList = subEnvelop.getDetailModelList(); //细表数据
  246. if (temp.get(ResourceCells.EVENT_TYPE).equals("0")) { //门诊信息
  247. resultMap.put("department", temp.get("EHR_000082") == null ? "" : temp.get("EHR_000082") );
  248. resultMap.put("doctor", temp.get("EHR_000079") == null ? "" : temp.get("EHR_000079"));
  249. resultMap.put("diagnosticResult", resultMap.get("healthProblemName")); //诊断结果
  250. //检查
  251. List<Map<String, Object>> inspectResult = new ArrayList<>();
  252. for (Map<String, Object> temp1 : subList) {
  253. if (temp1.get(ResourceCells.ROWKEY).toString().contains("HDSD00_79")) {
  254. if (temp1.get("EHR_002883") != null) {
  255. Map<String, Object> data = new HashMap<>();
  256. data.put("name", temp1.get("EHR_002883"));
  257. data.put("mark", temp1.get("EHR_000316")); //检查报告单号
  258. inspectResult.add(data);
  259. }
  260. }
  261. }
  262. resultMap.put("inspectResult", inspectResult);
  263. //检验
  264. List<Map<String, Object>> examineResult = new ArrayList<>();
  265. for (Map<String, Object> temp1 : subList) {
  266. if (temp1.get(ResourceCells.ROWKEY).toString().contains("HDSD00_77")) {
  267. if (temp1.get("EHR_000352") != null) {
  268. Map<String, Object> data = new HashMap<>();
  269. data.put("name", temp1.get("EHR_000352"));
  270. data.put("mark", temp1.get("EHR_000363")); //检验报告单号
  271. examineResult.add(data);
  272. }
  273. }
  274. }
  275. resultMap.put("examineResult", examineResult);
  276. } else if (temp.get(ResourceCells.EVENT_TYPE).equals("1")) { //住院信息
  277. String department = "";
  278. if (!StringUtils.isEmpty(temp.get("EHR_006209"))) {
  279. department = (String) temp.get("EHR_006209");
  280. }
  281. if (StringUtils.isEmpty(department) && !StringUtils.isEmpty("EHR_000228_VALUE")) {
  282. department = (String) temp.get("EHR_000228_VALUE");
  283. }
  284. if (StringUtils.isEmpty(department) && !StringUtils.isEmpty("EHR_000228")) {
  285. department = (String) temp.get("EHR_000228");
  286. }
  287. resultMap.put("department", department);
  288. String doctor = "";
  289. for (Map<String, Object> temp1 : subList) {
  290. if (temp1.get(ResourceCells.ROWKEY).toString().contains("HDSD00_11")) {
  291. if (!StringUtils.isEmpty(temp1.get("EHR_005072"))) {
  292. doctor = (String) temp1.get("EHR_005072");
  293. break;
  294. }
  295. }
  296. }
  297. resultMap.put("doctor", doctor);
  298. resultMap.put("inSituation", temp.get("EHR_005203") == null ? "" : temp.get("EHR_005203")); //入院情况
  299. resultMap.put("outSituation", temp.get("EHR_000154") == null ? "" : temp.get("EHR_000154")); //出院情况
  300. String inResult = "";
  301. String outResult = "";
  302. for (Map<String, Object> temp1 : subList) {
  303. if (temp1.get(ResourceCells.ROWKEY).toString().contains("HDSD00_69")) {
  304. if (temp1.get("EHR_006081") != null) {
  305. if (temp1.get("EHR_006081").equals("入院诊断")) {
  306. inResult = temp1.get("EHR_000293_VALUE") != null ? (String) temp1.get("EHR_000293_VALUE") : (String) temp1.get("EHR_000293");
  307. }
  308. if (temp1.get("EHR_006081").equals("出院诊断")) {
  309. outResult = temp1.get("EHR_000293_VALUE") != null ? (String) temp1.get("EHR_000293_VALUE") : (String) temp1.get("EHR_000293");
  310. }
  311. }
  312. }
  313. }
  314. resultMap.put("inResult", inResult); //入院诊断
  315. resultMap.put("outResult", outResult); //出院诊断
  316. resultMap.put("treatmentResults", temp.get("EHR_000299") == null ? "" : temp.get("EHR_000299")); //治疗结果
  317. resultMap.put("dischargeInstructions", temp.get("EHR_000157") == null ? "" : temp.get("EHR_000157")); //出院医嘱
  318. }
  319. return resultMap;
  320. }
  321. return new HashMap<>();
  322. }
  323. }