ProfileMedicationService.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package com.yihu.ehr.profile.service;
  2. import com.yihu.ehr.profile.family.ResourceCells;
  3. import com.yihu.ehr.profile.model.DrugInfo;
  4. import com.yihu.ehr.profile.util.SimpleSolrQueryUtil;
  5. import com.yihu.ehr.util.rest.Envelop;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.util.StringUtils;
  8. import java.util.*;
  9. /**
  10. * Created by progr1mmer on 2018/3/13.
  11. */
  12. @Service
  13. public class ProfileMedicationService extends ProfileBasicService {
  14. public Map<String, Integer> medicationRanking(String demographicId, String filter, String hpCode, String date) throws Exception {
  15. String masterQ;
  16. if (!StringUtils.isEmpty(hpCode)) {
  17. masterQ = "{\"q\":\"demographic_id:" + demographicId + " AND health_problem:*" + hpCode + "*\"}";
  18. } else {
  19. masterQ = "{\"q\":\"demographic_id:" + demographicId + "\"}";
  20. }
  21. masterQ = SimpleSolrQueryUtil.getQuery(filter, date, masterQ);
  22. Map<String, Integer> dataMap = new HashMap<>();
  23. Envelop masterEnvelop = resource.getMasterData(masterQ, 1, 500, null);
  24. if (masterEnvelop.isSuccessFlg()) {
  25. List<Map<String, Object>> masterList = masterEnvelop.getDetailModelList();
  26. //循环获取结果集
  27. for (Map<String, Object> masterMap : masterList) {
  28. String rowKey = (String) masterMap.get(ResourceCells.ROWKEY);
  29. String subQ = "{\"q\":\"rowkey:" + rowKey + "$HDSD00_83$* OR rowkey:" + rowKey + "$HDSD00_84$*\"}";
  30. Envelop subEnvelop = resource.getSubData(subQ, 1, 1000, null);
  31. if (subEnvelop.isSuccessFlg()) {
  32. List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
  33. if (subList.size() > 0) {
  34. for (Map<String, Object> subMap : subList) {
  35. if (!StringUtils.isEmpty(subMap.get("EHR_000131"))) {
  36. String drugName = (String) subMap.get("EHR_000131");
  37. if (dataMap.containsKey(drugName)) {
  38. Integer count = dataMap.get(drugName);
  39. dataMap.put(drugName, count + 1);
  40. } else {
  41. dataMap.put(drugName, 1);
  42. }
  43. continue;
  44. }
  45. if (!StringUtils.isEmpty(subMap.get("EHR_000100"))) {
  46. String drugName = (String) subMap.get("EHR_000100");
  47. if (dataMap.containsKey(drugName)) {
  48. Integer count = dataMap.get(drugName);
  49. dataMap.put(drugName, count + 1);
  50. } else {
  51. dataMap.put(drugName, 1);
  52. }
  53. continue;
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. return sortByValue(dataMap);
  61. }
  62. public List<DrugInfo> medicationRankingWithTable(String demographicId, String filter, String hpCode, String date, String table) throws Exception {
  63. String masterQ;
  64. if (!StringUtils.isEmpty(hpCode)) {
  65. masterQ = "{\"q\":\"demographic_id:" + demographicId + " AND health_problem:*" + hpCode + "*\"}";
  66. } else {
  67. masterQ = "{\"q\":\"demographic_id:" + demographicId + "\"}";
  68. }
  69. masterQ = SimpleSolrQueryUtil.getQuery(filter, date, masterQ);
  70. Map<String, Integer> dataMap = new HashMap<>();
  71. Envelop masterEnvelop = resource.getMasterData(masterQ, 1, 500, null);
  72. if (masterEnvelop.isSuccessFlg()) {
  73. List<Map<String, Object>> masterList = masterEnvelop.getDetailModelList();
  74. //循环获取结果集
  75. for (Map<String, Object> masterMap : masterList) {
  76. String rowKey = (String) masterMap.get(ResourceCells.ROWKEY);
  77. String subQ;
  78. if (StringUtils.isEmpty(table)) {
  79. subQ = "{\"q\":\"rowkey:" + rowKey + "$HDSD00_83$* OR rowkey:" + rowKey + "$HDSD00_84$*\"}";
  80. } else {
  81. subQ = "{\"q\":\"rowkey:" + rowKey + "$" + table + "$*\"}";
  82. }
  83. Envelop subEnvelop = resource.getSubData(subQ, 1, 1000, null);
  84. if (subEnvelop.isSuccessFlg()) {
  85. List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
  86. if (subList.size() > 0) {
  87. for (Map<String, Object> subMap : subList) {
  88. if (!StringUtils.isEmpty(subMap.get("EHR_000131"))) {
  89. String drugName = subMap.get("EHR_000131") + "_01"; //中药名称
  90. if (dataMap.containsKey(drugName)) {
  91. Integer count = dataMap.get(drugName);
  92. dataMap.put(drugName, count + 1);
  93. } else {
  94. dataMap.put(drugName, 1);
  95. }
  96. continue;
  97. }
  98. if (!StringUtils.isEmpty(subMap.get("EHR_000100"))) {
  99. String drugName = subMap.get("EHR_000100") + "_02"; //西药名称
  100. if (dataMap.containsKey(drugName)) {
  101. Integer count = dataMap.get(drugName);
  102. dataMap.put(drugName, count + 1);
  103. } else {
  104. dataMap.put(drugName, 1);
  105. }
  106. continue;
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. List<DrugInfo> drugInfos = new ArrayList<>();
  114. dataMap = sortByValue(dataMap);
  115. dataMap.forEach((key, val) -> {
  116. DrugInfo drugInfo = new DrugInfo();
  117. String [] info = key.split("_");
  118. drugInfo.setName(info[0]);
  119. drugInfo.setCount(val);
  120. if (info[1].endsWith("01")) {
  121. drugInfo.setLabel("中药");
  122. } else {
  123. drugInfo.setLabel("西药");
  124. }
  125. drugInfos.add(drugInfo);
  126. });
  127. return drugInfos;
  128. }
  129. public List<Map<String, Object>> medicationRecords(String demographicId, String filter, String date, String keyWord) throws Exception {
  130. List<Map<String, Object>> resultList = new ArrayList<>();
  131. String masterQ = "{\"q\":\"demographic_id:" + demographicId + "\"}";
  132. masterQ = SimpleSolrQueryUtil.getQuery(filter, date, masterQ);
  133. Envelop masterEnvelop = resource.getMasterData(masterQ, 1, 500, null);
  134. if (masterEnvelop.isSuccessFlg()) {
  135. List<Map<String, Object>> masterList = masterEnvelop.getDetailModelList();
  136. //循环获取结果集
  137. for (Map<String, Object> masterMap : masterList) {
  138. String rowKey = (String) masterMap.get(ResourceCells.ROWKEY);
  139. String subQ = "{\"q\":\"rowkey:" + rowKey + "$HDSD00_83$* OR rowkey:" + rowKey + "$HDSD00_84$*\"}";
  140. Envelop subEnvelop = resource.getSubData(subQ, 1, 1000, null);
  141. if (subEnvelop.isSuccessFlg()) {
  142. List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
  143. if (subList.size() > 0) {
  144. boolean match = false;
  145. String typeMark = "00"; //中西药标识
  146. for (Map<String, Object> subMap : subList) {
  147. if (subMap.get("EHR_000131") != null) {
  148. if (subMap.get(ResourceCells.ROWKEY).toString().contains("HDSD00_83")) {
  149. typeMark = "01"; //中药
  150. } else {
  151. typeMark = "02"; //西药
  152. }
  153. if (keyWord != null) {
  154. if (subMap.get("EHR_000131").toString().contains(keyWord)) {
  155. match = true;
  156. break;
  157. }
  158. } else {
  159. match = true;
  160. break;
  161. }
  162. }
  163. if (subMap.get("EHR_000100") != null) {
  164. if (subMap.get(ResourceCells.ROWKEY).toString().contains("HDSD00_83")) {
  165. typeMark = "01"; //中药
  166. } else {
  167. typeMark = "02"; //西药
  168. }
  169. if (keyWord != null && subMap.get("EHR_000100") != null) {
  170. if (subMap.get("EHR_000100").toString().contains(keyWord)) {
  171. match = true;
  172. break;
  173. }
  174. } else {
  175. match = true;
  176. break;
  177. }
  178. }
  179. }
  180. if (match) {
  181. //时间轴基本字段
  182. Map<String, Object> resultMap = simpleEvent(masterMap, null);
  183. resultMap.put("mark", typeMark);
  184. if (masterMap.get(ResourceCells.EVENT_TYPE).equals("0")) { //门诊信息
  185. resultMap.put("department", masterMap.get("EHR_000082"));
  186. resultMap.put("doctor", masterMap.get("EHR_000079"));
  187. } else if (masterMap.get(ResourceCells.EVENT_TYPE).equals("1")) { //住院信息
  188. resultMap.put("department", masterMap.get("EHR_000229"));
  189. resultMap.put("doctor", masterMap.get("EHR_005072"));
  190. }
  191. resultList.add(resultMap);
  192. }
  193. }
  194. }
  195. }
  196. }
  197. return resultList;
  198. }
  199. public Envelop recentMedicationSub(String demographicId, String date, Integer page, Integer size) throws Exception {
  200. String masterQ = "{\"q\":\"demographic_id:" + demographicId + "\"}";
  201. masterQ = SimpleSolrQueryUtil.getQuery(null, date, masterQ);
  202. Envelop masterEnvelop = resource.getMasterData(masterQ, 1, 500, null);
  203. List<Map<String, Object>> masterList = masterEnvelop.getDetailModelList();
  204. List<Map<String, Object>> dataList = new ArrayList<>();
  205. Object eventDate = "";
  206. for (Map<String, Object> event : masterList) {
  207. //详情
  208. String rowKey = (String) event.get(ResourceCells.ROWKEY);
  209. String subQ = "{\"q\":\"rowkey:" + rowKey + "$HDSD00_83$* OR rowkey:" + rowKey + "$HDSD00_84$*\"}";
  210. Envelop subEnvelop = resource.getSubData(subQ, 1, 1000, null);
  211. List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
  212. if (subList.size() > 0) {
  213. for (Map<String, Object> subMap : subList) {
  214. Map<String, Object> dataMap = new HashMap<>();
  215. dataMap.put("prescriptionNumber", subMap.get("EHR_000086") == null ? "" : subMap.get("EHR_000086") ); //处方编号
  216. dataMap.put("substancesForDrugUse", subMap.get("EHR_000101") == null ? "" : subMap.get("EHR_000101")); //药物使用次剂量
  217. dataMap.put("prescriptionDrugGroupNumber", subMap.get("EHR_000127") == null ? "" : subMap.get("EHR_000127")); //处方药品组号
  218. dataMap.put("drugSpecifications", subMap.get("EHR_000129") == null ? "" : subMap.get("EHR_000129")); //药物规格
  219. dataMap.put("drugFormulationCode", subMap.get("EHR_000130") == null ? "" : subMap.get("EHR_000130")); //药物剂型代码
  220. dataMap.put("drugFormulationValue", subMap.get("EHR_000130_VALUE") == null ? "" : subMap.get("EHR_000130_VALUE")); //药物剂型值
  221. String drugName = "";
  222. if (!StringUtils.isEmpty(subMap.get("EHR_000100"))) {
  223. drugName = (String) subMap.get("EHR_000100");
  224. } else if (!StringUtils.isEmpty(subMap.get("EHR_000131"))) {
  225. drugName = (String) subMap.get("EHR_000131");
  226. }
  227. dataMap.put("drugName", drugName); //药物名称
  228. dataMap.put("drugsUseDosageUnits", subMap.get("EHR_000133") == null ? "" : subMap.get("EHR_000133")); //药物使用剂量单位
  229. dataMap.put("drugUsageFrequencyCode", subMap.get("EHR_000134") == null ? "" : subMap.get("EHR_000134")); //药物使用频次代码
  230. dataMap.put("drugUsageFrequencyValue", subMap.get("EHR_000134_VALUE") == null ? "" : subMap.get("EHR_000134_VALUE")); //药物使用频次值
  231. dataMap.put("totalDoseOfDrugUsed", subMap.get("EHR_000135") == null ? "" : subMap.get("EHR_000135")); //药物使用总剂量
  232. dataMap.put("medicationRouteCode", subMap.get("EHR_000136") == null ? "" : subMap.get("EHR_000136")); //用药途径代码
  233. dataMap.put("medicationRouteValue", subMap.get("EHR_000136_VALUE") == null ? "" : subMap.get("EHR_000136_VALUE")); //用药途径值
  234. String dataSetCode = String.valueOf(subMap.get("rowkey")).split("\\$")[1];
  235. if ("HDSD00_84".equals(dataSetCode)) { //西药
  236. dataMap.put("drugUseTotalDoseUnit", subMap.get("EHR_001249") == null ? "" : subMap.get("EHR_001249")); //药物使用总剂量单位
  237. } else { //中药
  238. dataMap.put("drugUseTotalDoseUnit", subMap.get("EHR_001250") == null ? "" : subMap.get("EHR_001250")); //药物使用总剂量单位
  239. }
  240. dataList.add(dataMap);
  241. }
  242. }
  243. if (dataList.size() > 0) {
  244. eventDate = event.get(ResourceCells.EVENT_DATE);
  245. break;
  246. }
  247. }
  248. Envelop envelop = new Envelop();
  249. envelop.setSuccessFlg(true);
  250. envelop.setCurrPage(page);
  251. envelop.setPageSize(size);
  252. envelop.setTotalPage(dataList.size() % size > 0 ? dataList.size() / size + 1 : dataList.size() / size);
  253. envelop.setTotalCount(dataList.size());
  254. List result = new ArrayList();
  255. for (int i = (page - 1) * size; i < page * size; i ++) {
  256. if (i > dataList.size() - 1) {
  257. break;
  258. }
  259. result.add(dataList.get(i));
  260. }
  261. envelop.setDetailModelList(result);
  262. envelop.setObj(eventDate);
  263. return envelop;
  264. }
  265. private Map<String, Integer> sortByValue(Map<String, Integer> sourceMap) {
  266. if (sourceMap == null || sourceMap.isEmpty()) {
  267. return sourceMap;
  268. }
  269. Map<String, Integer> sortedMap = new LinkedHashMap<>();
  270. List<Map.Entry<String, Integer>> entryList = new ArrayList<>(sourceMap.entrySet());
  271. Collections.sort(entryList, new MapValueComparator());
  272. Iterator<Map.Entry<String, Integer>> iterator = entryList.iterator();
  273. while (iterator.hasNext()) {
  274. Map.Entry<String, Integer> tmpEntry = iterator.next();
  275. sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
  276. }
  277. return sortedMap;
  278. }
  279. class MapValueComparator implements Comparator<Map.Entry<String, Integer>> {
  280. @Override
  281. public int compare(Map.Entry<String, Integer> me1, Map.Entry<String, Integer> me2) {
  282. return - me1.getValue().compareTo(me2.getValue());
  283. }
  284. }
  285. }