ProfileDiseaseService.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package com.yihu.ehr.profile.service;
  2. import com.yihu.ehr.profile.family.ResourceCells;
  3. import com.yihu.ehr.util.rest.Envelop;
  4. import org.apache.commons.collections.map.HashedMap;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.apache.commons.lang.time.DateUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import java.text.DateFormat;
  10. import java.text.SimpleDateFormat;
  11. import java.util.*;
  12. /**
  13. * Created by progr1mmer on 2018/1/7.
  14. */
  15. @Service
  16. public class ProfileDiseaseService extends ProfileBasicService {
  17. @Autowired
  18. private ProfileMedicationService profileMedicationService;
  19. /**
  20. * 根据就诊记录,获取慢病记录
  21. * @param demographicId
  22. * @return
  23. */
  24. public List<Map<String, Object>> getHealthProblem (String demographicId) throws Exception {
  25. List<Map<String, Object>> result = new ArrayList<>();
  26. //获取门诊住院记录
  27. Envelop envelop = resource.getMasterData("{\"q\":\"demographic_id:" + demographicId + "\"}", 1, 500, null);
  28. List<Map<String, Object>> eventList = envelop.getDetailModelList();
  29. Map<String, List<Map<String, Object>>> hpMap = new HashedMap();
  30. if (eventList.size() > 0) {
  31. //进行降序
  32. Collections.sort(eventList, new ProfileDiseaseService.EventDateComparatorDesc());
  33. eventList.forEach(item -> {
  34. if (item.containsKey(ResourceCells.DIAGNOSIS)) {
  35. String diagnosis = (String) item.get(ResourceCells.DIAGNOSIS);
  36. String [] _diagnosis = diagnosis.split(";");
  37. for (String code : _diagnosis) {
  38. String chronicInfo = redisService.getChronicInfo(code);
  39. if (!StringUtils.isEmpty(chronicInfo)) {
  40. String [] _chronicInfo = chronicInfo.split("-");
  41. if (!"0".equals(_chronicInfo[1])) {
  42. String healthProblem = redisService.getHpCodeByIcd10(code); //祝金仙
  43. if (!StringUtils.isEmpty(healthProblem)) {
  44. for (String hpCode : healthProblem.split(";")) {
  45. List<Map<String, Object>> profileList = new ArrayList<>();
  46. if (hpMap.containsKey(hpCode)) {
  47. profileList = hpMap.get(hpCode);
  48. }
  49. profileList.add(item);
  50. hpMap.put(hpCode, profileList);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
  57. });
  58. for (String hpCode : hpMap.keySet()) {
  59. Map<String, Object> obj = new HashedMap();
  60. obj.put("healthProblemCode", hpCode);
  61. obj.put("healthProblemName", redisService.getHealthProblem(hpCode));
  62. int visitTimes = 0;
  63. int hospitalizationTimes = 0;
  64. List<Map<String, Object>> profileList = hpMap.get(hpCode);
  65. Map<String, List<Map<String, Object>>> complication = new HashMap<>();
  66. for (int i = 0; i < profileList.size(); i++) {
  67. Map<String, Object> profile = profileList.get(i);
  68. //事件类型
  69. String eventType = (String) profile.get(ResourceCells.EVENT_TYPE);
  70. String recentEvent = "";
  71. if ("0".equals(eventType)) {
  72. recentEvent = "门诊";
  73. visitTimes ++;
  74. } else if ("1".equals(eventType)) {
  75. recentEvent = "住院";
  76. hospitalizationTimes ++;
  77. } else if ("2".equals(eventType)) {
  78. recentEvent = "体检";
  79. }
  80. //第一条
  81. if (i == 0) {
  82. obj.put("lastVisitDate", profile.get(ResourceCells.EVENT_DATE));
  83. obj.put("lastVisitOrgCode", profile.get(ResourceCells.ORG_CODE));
  84. obj.put("lastVisitOrg", profile.get(ResourceCells.ORG_NAME));
  85. obj.put("lastVisitRecord", profile.get(ResourceCells.ROWKEY));
  86. obj.put("recentEvent", recentEvent);
  87. obj.put("eventType", eventType);
  88. }
  89. //最后一条
  90. if (i == profileList.size() - 1) {
  91. obj.put("ageOfDisease", getAgeOfDisease(profile.get(ResourceCells.EVENT_DATE)));
  92. obj.put("firstVisitDate", profile.get(ResourceCells.EVENT_DATE));
  93. obj.put("firstVisitOrgCode", profile.get(ResourceCells.ORG_CODE));
  94. obj.put("firstVisitOrg", profile.get(ResourceCells.ORG_NAME));
  95. obj.put("firstVisitRecord", profile.get(ResourceCells.ROWKEY));
  96. }
  97. //提取并发症
  98. if (profile.containsKey(ResourceCells.DIAGNOSIS)) {
  99. String diagnosis = (String) profile.get(ResourceCells.DIAGNOSIS);
  100. String [] _diagnosis = diagnosis.split(";");
  101. for (String diagnosisCode : _diagnosis) {
  102. String _hpCode = redisService.getHpCodeByIcd10(diagnosisCode);
  103. if (_hpCode != null && !_hpCode.contains(hpCode)) {
  104. if (complication.containsKey(diagnosisCode)) {
  105. complication.get(diagnosisCode).add(profile);
  106. } else {
  107. List<Map<String, Object>> diagnosisList = new ArrayList<>();
  108. diagnosisList.add(profile);
  109. complication.put(diagnosisCode, diagnosisList);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. //近3个月常用药物
  116. Calendar calendar = Calendar.getInstance();
  117. calendar.set(Calendar.HOUR_OF_DAY, 0);
  118. calendar.set(Calendar.MINUTE, 0);
  119. calendar.set(Calendar.SECOND, 0);
  120. calendar.set(Calendar.MILLISECOND, 0);
  121. Date now = calendar.getTime();
  122. Date before = DateUtils.addDays(now, -180);
  123. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  124. String start = dateFormat.format(before);
  125. String end = dateFormat.format(DateUtils.addDays(now, 1));
  126. String date = "{\"start\":\"" + start + "\",\"end\":\"" + end + "\"}";
  127. Map<String, Integer> medication = profileMedicationService.medicationRanking(demographicId, null, hpCode, date);
  128. obj.put("medical", medication);
  129. //并发症确诊
  130. List<Map<String, Object>> complicationList = new ArrayList<>();
  131. complication.forEach((key, value) -> {
  132. Map<String, Object> data = new HashMap<>();
  133. Collections.sort(value, new ProfileDiseaseService.EventDateComparatorDesc());
  134. Map<String, Object> profile = value.get(value.size() - 1);
  135. data.put("name", redisService.getIcd10Name(key));
  136. data.put("date", profile.get(ResourceCells.EVENT_DATE));
  137. data.put("org", profile.get(ResourceCells.ORG_NAME));
  138. data.put("record", profile.get(ResourceCells.ROWKEY));
  139. complicationList.add(data);
  140. });
  141. obj.put("complication", complicationList);
  142. obj.put("visitTimes", visitTimes);
  143. obj.put("hospitalizationTimes", hospitalizationTimes);
  144. obj.put("demographicId", demographicId);
  145. result.add(obj);
  146. }
  147. }
  148. return result;
  149. }
  150. /**
  151. * 根据就诊记录,分析历史健康情况
  152. * @param demographicId
  153. * @return
  154. */
  155. public List<Map<String, Object>> getHealthCondition (String demographicId) {
  156. List<Map<String, Object>> result = new ArrayList<>();
  157. //获取门诊住院记录
  158. Envelop envelop = resource.getMasterData("{\"q\":\"demographic_id:" + demographicId + "\"}", 1, 500, null);
  159. List<Map<String, Object>> eventList = envelop.getDetailModelList();
  160. Map<String, List<Map<String, Object>>> hpMap = new HashedMap();
  161. if (eventList.size() > 0) {
  162. //进行降序
  163. Collections.sort(eventList, new ProfileDiseaseService.EventDateComparatorDesc());
  164. for (Map<String, Object> item : eventList) {
  165. if (item.containsKey(ResourceCells.HEALTH_PROBLEM)) {
  166. String healthProblem = (String) item.get(ResourceCells.HEALTH_PROBLEM);
  167. String [] _healthProblem = healthProblem.split(";");
  168. for (String code : _healthProblem) {
  169. List<Map<String, Object>> profileList = new ArrayList<>();
  170. if (hpMap.containsKey(code)) {
  171. profileList = hpMap.get(code);
  172. }
  173. profileList.add(item);
  174. hpMap.put(code, profileList);
  175. }
  176. }
  177. }
  178. hpMap = sortByValue(hpMap);
  179. for (String hpCode : hpMap.keySet()) {
  180. Map<String, Object> obj = new HashedMap();
  181. obj.put("healthProblemCode", hpCode);
  182. obj.put("healthProblemName", redisService.getHealthProblem(hpCode));
  183. int visitTimes = 0;
  184. int hospitalizationTimes = 0;
  185. int operateTimes = 0;
  186. Map<String, Integer> commonSymptoms = new HashMap<>();
  187. List<Map<String, Object>> profileList = hpMap.get(hpCode);
  188. for (int i = 0; i < profileList.size(); i++) {
  189. Map<String, Object> profile = profileList.get(i);
  190. //事件类型
  191. String eventType = (String) profile.get(ResourceCells.EVENT_TYPE);
  192. String recentEvent = "";
  193. if ("0".equals(eventType)) {
  194. recentEvent = "门诊";
  195. visitTimes ++;
  196. } else if ("1".equals(eventType)) {
  197. recentEvent = "住院";
  198. hospitalizationTimes ++;
  199. } else if ("2".equals(eventType)) {
  200. recentEvent = "体检";
  201. }
  202. String subQ = "{\"q\":\"rowkey:" + profile.get(ResourceCells.ROWKEY) + "$HDSD00_06$*" + "\"}";
  203. Envelop subEnvelop = resource.getSubData(subQ, 1, 1, null);
  204. if (subEnvelop.getDetailModelList().size() > 0) {
  205. operateTimes ++;
  206. }
  207. //第一条
  208. if (i == 0) {
  209. obj.put("lastVisitDate", profile.get(ResourceCells.EVENT_DATE));
  210. obj.put("lastVisitOrgCode", profile.get(ResourceCells.ORG_CODE));
  211. obj.put("lastVisitOrg", profile.get(ResourceCells.ORG_NAME));
  212. obj.put("lastVisitRecord", profile.get(ResourceCells.ROWKEY));
  213. obj.put("recentEvent", recentEvent);
  214. obj.put("eventType", eventType);
  215. }
  216. //最后一条
  217. if (i == profileList.size() - 1) {
  218. obj.put("firstVisitDate", profile.get(ResourceCells.EVENT_DATE));
  219. obj.put("firstVisitOrgCode", profile.get(ResourceCells.ORG_CODE));
  220. obj.put("firstVisitOrg", profile.get(ResourceCells.ORG_NAME));
  221. obj.put("firstVisitRecord", profile.get(ResourceCells.ROWKEY));
  222. }
  223. //提取常见症状
  224. if (profile.containsKey(ResourceCells.DIAGNOSIS)) {
  225. String diagnosis = (String) profile.get(ResourceCells.DIAGNOSIS);
  226. String [] _diagnosis = diagnosis.split(";");
  227. for (String diagnosisCode : _diagnosis) {
  228. String healthProblem = redisService.getHpCodeByIcd10(diagnosisCode);//通过ICD10获取健康问题
  229. if (!StringUtils.isEmpty(healthProblem)) {
  230. if (healthProblem.contains(hpCode)) {
  231. if (commonSymptoms.containsKey(diagnosisCode)) {
  232. int count = commonSymptoms.get(diagnosisCode);
  233. commonSymptoms.put(diagnosisCode, count++) ;
  234. } else {
  235. commonSymptoms.put(diagnosisCode, 1) ;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }
  242. //常见症状
  243. List<Map<String, Object>> commonSymptomsList = new ArrayList<>();
  244. commonSymptoms.forEach((key, value) -> {
  245. Map<String, Object> data = new HashMap<>();
  246. data.put("name", redisService.getIcd10Name(key));
  247. data.put("count", value);
  248. commonSymptomsList.add(data);
  249. });
  250. obj.put("commonSymptoms", commonSymptomsList);
  251. obj.put("visitTimes", visitTimes);
  252. obj.put("hospitalizationTimes", hospitalizationTimes);
  253. obj.put("operateTimes", operateTimes);
  254. obj.put("demographicId", demographicId);
  255. result.add(obj);
  256. }
  257. }
  258. return result;
  259. }
  260. /**
  261. * 根据时间获取病龄
  262. * @param eventData
  263. * @return
  264. */
  265. private String getAgeOfDisease(Object eventData){
  266. SimpleDateFormat sd = new SimpleDateFormat("yyyyMM");
  267. String eventDataYear=eventData.toString().substring(0, 7).substring(0,4);
  268. String eventDataMonth=eventData.toString().substring(0, 7).substring(5,7);
  269. String ageOfDisease;
  270. if (Integer.parseInt(sd.format(new Date()).substring(4, 6)) - Integer.parseInt(eventDataMonth)<0){
  271. Integer year = Integer.parseInt(sd.format(new Date()).substring(0, 4)) - Integer.parseInt(eventDataYear)-1;
  272. Integer month = Integer.parseInt(sd.format(new Date()).substring(4, 6))+12- Integer.parseInt(eventDataMonth);
  273. if (year>0) {
  274. ageOfDisease = year + "年" + month + "个月";
  275. } else {
  276. ageOfDisease = month + "个月";
  277. }
  278. } else {
  279. Integer year = Integer.parseInt(sd.format(new Date()).substring(0, 4)) - Integer.parseInt(eventDataYear);
  280. Integer month = Integer.parseInt(sd.format(new Date()).substring(4, 6))- Integer.parseInt(eventDataMonth);
  281. if (year>0) {
  282. ageOfDisease = year + "年" + month + "个月";
  283. } else {
  284. ageOfDisease = month + "个月";
  285. }
  286. }
  287. return ageOfDisease;
  288. }
  289. static class EventDateComparatorDesc implements Comparator<Map<String, Object>> {
  290. @Override
  291. public int compare(Map<String, Object> m1, Map<String, Object> m2) {
  292. String eventDate1 = (String)m1.get(ResourceCells.EVENT_DATE);
  293. String eventDate2 = (String)m2.get(ResourceCells.EVENT_DATE);
  294. String str1 = eventDate1.substring(0, eventDate1.length()-1).replaceAll("[a-zA-Z]"," ");
  295. String str2 = eventDate2.substring(0, eventDate1.length()-1).replaceAll("[a-zA-Z]"," ");
  296. DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  297. Date v1 = null;
  298. Date v2 = null;
  299. try {
  300. v1 = dateFormat.parse(str1);
  301. v2 = dateFormat.parse(str2);
  302. } catch (Exception e) {
  303. e.printStackTrace();
  304. }
  305. if (v2 != null) {
  306. return v2.compareTo(v1);
  307. }
  308. return 0;
  309. }
  310. }
  311. private Map<String, List<Map<String, Object>>> sortByValue(Map<String, List<Map<String, Object>>> sourceMap) {
  312. if (sourceMap == null || sourceMap.isEmpty()) {
  313. return sourceMap;
  314. }
  315. Map<String, List<Map<String, Object>>> sortedMap = new LinkedHashMap<>();
  316. List<Map.Entry<String, List<Map<String, Object>>>> entryList = new ArrayList<>(sourceMap.entrySet());
  317. Collections.sort(entryList, new ProfileDiseaseService.MapValueComparator());
  318. Iterator<Map.Entry<String, List<Map<String, Object>>>> iterator = entryList.iterator();
  319. while (iterator.hasNext()) {
  320. Map.Entry<String, List<Map<String, Object>>> tmpEntry = iterator.next();
  321. sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
  322. }
  323. return sortedMap;
  324. }
  325. class MapValueComparator implements Comparator<Map.Entry<String, List<Map<String, Object>>>> {
  326. @Override
  327. public int compare(Map.Entry<String, List<Map<String, Object>>> me1, Map.Entry<String, List<Map<String, Object>>> me2) {
  328. return - new Integer(me1.getValue().size()).compareTo(new Integer(me2.getValue().size()));
  329. }
  330. }
  331. }