ProfileInspectionService.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package com.yihu.ehr.profile.service;
  2. import com.yihu.ehr.profile.family.ResourceCells;
  3. import com.yihu.ehr.profile.model.InspectionInfo;
  4. import com.yihu.ehr.profile.util.NumberValidationUtils;
  5. import com.yihu.ehr.profile.util.SimpleSolrQueryUtil;
  6. import com.yihu.ehr.util.rest.Envelop;
  7. import org.springframework.stereotype.Service;
  8. import org.springframework.util.StringUtils;
  9. import java.util.*;
  10. /**
  11. * @author progr1mmer
  12. * @date Created on 2018/3/13.
  13. */
  14. @Service
  15. public class ProfileInspectionService extends ProfileBasicService {
  16. public List inspectionRecords(String demographicId, String filter, String date, String searchParam) throws Exception {
  17. List<Map<String, Object>> resultList = new ArrayList<>();
  18. Map<String, String> typeMap = new HashMap<>();
  19. typeMap.put("HDSD00_79", "inspect");
  20. typeMap.put("HDSD00_77", "examine");
  21. Map<String, String> nameMap = new HashMap<>();
  22. //检查名称
  23. nameMap.put("HDSD00_79", "EHR_002883");
  24. //检验项目
  25. nameMap.put("HDSD00_77", "EHR_000352");
  26. Map<String, String> numMap = new HashMap<>();
  27. //检查报告单号
  28. numMap.put("HDSD00_79", "EHR_000316");
  29. //检验报告单号
  30. numMap.put("HDSD00_77", "EHR_000363");
  31. for (String dataset : typeMap.keySet()) {
  32. String query = "{\"q\":\"demographic_id:" + demographicId + "\"}";
  33. query = SimpleSolrQueryUtil.getQuery(filter, date, query);
  34. Envelop masterEnvelop = resource.getMasterData(query, 1, 1000, null);
  35. if (masterEnvelop.isSuccessFlg()) {
  36. List<Map<String, Object>> masterList = masterEnvelop.getDetailModelList();
  37. //循环获取结果集
  38. for (Map<String, Object> temp : masterList) {
  39. String subQ = "{\"q\":\"rowkey:" + temp.get("rowkey") + "$" + dataset + "$*\"}";
  40. Envelop subEnvelop = resource.getSubData(subQ, 1, 1000, null);
  41. List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
  42. subList.forEach(item -> {
  43. Map<String, Object> resultMap = simpleEvent(temp, searchParam);
  44. if (resultMap != null) {
  45. String healthProblemName = (String) resultMap.get("healthProblemName");
  46. String itemName = (String) item.get(nameMap.get(dataset));
  47. if (itemName != null) {
  48. resultMap.put("projectName", itemName);
  49. } else {
  50. if (dataset.equals("HDSD00_79")) {
  51. resultMap.put("projectName", "检查报告-" + healthProblemName);
  52. } else {
  53. resultMap.put("projectName", "检验报告-" + healthProblemName);
  54. }
  55. }
  56. resultMap.put("type", typeMap.get(dataset));
  57. resultMap.put("mark", item.get(numMap.get(dataset)));
  58. resultList.add(resultMap);
  59. }
  60. });
  61. }
  62. }
  63. }
  64. return resultList;
  65. }
  66. public List<InspectionInfo> inspectionStatistics(String demographicId, String table) throws Exception {
  67. String query = "{\"q\":\"demographic_id:" + demographicId + "\"}";
  68. query = SimpleSolrQueryUtil.getQuery(null, null, query);
  69. Envelop masterEnvelop = resource.getMasterData(query, 1, 1000, null);
  70. Map<String, Integer> dataMap = new HashMap<>();
  71. Map<String, String> firstMap = new HashMap<>();
  72. Map<String, String> lastMap = new HashMap<>();
  73. Map<String, Integer> exMap = new HashMap<>();
  74. if (masterEnvelop.isSuccessFlg()) {
  75. List<Map<String, Object>> masterList = masterEnvelop.getDetailModelList();
  76. //循环获取结果集
  77. for (Map<String, Object> temp : masterList) {
  78. String subQ = "{\"q\":\"rowkey:" + temp.get("rowkey") + "$" + table + "$*\"}";
  79. Envelop subEnvelop = resource.getSubData(subQ, 1, 1000, null);
  80. List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
  81. for (int i = 0; i < subList.size(); i ++) {
  82. Map<String, Object> item = subList.get(i);
  83. //检查名称
  84. if (!StringUtils.isEmpty(item.get("EHR_002883"))) {
  85. String name = item.get("EHR_002883").toString();
  86. //影像结论
  87. String result = item.get("EHR_000347") == null ? "-" : item.get("EHR_000347").toString();
  88. if (dataMap.containsKey(name)) {
  89. Integer count = dataMap.get(name);
  90. dataMap.put(name, count + 1);
  91. lastMap.put(name, temp.get(ResourceCells.EVENT_DATE) + "&" + temp.get(ResourceCells.ORG_NAME) + "&" + item.get(ResourceCells.ROWKEY) + "&" + result);
  92. } else {
  93. dataMap.put(name, 1);
  94. firstMap.put(name, temp.get(ResourceCells.EVENT_DATE) + "&" + temp.get(ResourceCells.ORG_NAME) + "&" + item.get(ResourceCells.ROWKEY) + "&" + result);
  95. }
  96. if (!StringUtils.isEmpty(item.get("EHR_000344"))) {
  97. String ex = item.get("EHR_000344").toString();
  98. if ("1".equals(ex)) {
  99. if (exMap.containsKey(name)) {
  100. Integer count = exMap.get(name);
  101. exMap.put(name, count + 1);
  102. } else {
  103. exMap.put(name, 1);
  104. }
  105. }
  106. }
  107. //检验项目
  108. } else if (!StringUtils.isEmpty(item.get("EHR_000352"))) {
  109. String name = item.get("EHR_000352").toString();
  110. if (dataMap.containsKey(name)) {
  111. Integer count = dataMap.get(name);
  112. dataMap.put(name, count + 1);
  113. lastMap.put(name, temp.get(ResourceCells.EVENT_DATE) + "&" + temp.get(ResourceCells.ORG_NAME) + "&" + item.get(ResourceCells.ROWKEY) + "&" + "-");
  114. } else {
  115. dataMap.put(name, 1);
  116. firstMap.put(name, temp.get(ResourceCells.EVENT_DATE) + "&" + temp.get(ResourceCells.ORG_NAME) + "&" + item.get(ResourceCells.ROWKEY) + "&" + "-");
  117. }
  118. }
  119. }
  120. }
  121. }
  122. Map<String, Integer> newDataMap = sortByValue(dataMap);
  123. List<InspectionInfo> inspectionInfos = new ArrayList<>();
  124. newDataMap.forEach((key, val) -> {
  125. InspectionInfo inspectionInfo = new InspectionInfo();
  126. inspectionInfo.setName(key);
  127. inspectionInfo.setCount(val);
  128. inspectionInfo.setFirstTime(firstMap.get(key).split("&")[0]);
  129. if (exMap.containsKey(key)) {
  130. inspectionInfo.setExCount(exMap.get(key));
  131. }
  132. if (lastMap.get(key) != null) {
  133. inspectionInfo.setLastTime(lastMap.get(key).split("&")[0]);
  134. inspectionInfo.setLastOrg(lastMap.get(key).split("&")[1]);
  135. inspectionInfo.setLastRecord(lastMap.get(key).split("&")[2]);
  136. inspectionInfo.setResult(lastMap.get(key).split("&")[3]);
  137. } else {
  138. inspectionInfo.setLastTime(firstMap.get(key).split("&")[0]);
  139. inspectionInfo.setLastOrg(firstMap.get(key).split("&")[1]);
  140. inspectionInfo.setLastRecord(firstMap.get(key).split("&")[2]);
  141. inspectionInfo.setResult(firstMap.get(key).split("&")[3]);
  142. }
  143. inspectionInfos.add(inspectionInfo);
  144. });
  145. return inspectionInfos;
  146. }
  147. public Map<String, String> inspectionStatisticsOneSub(String profileId) throws Exception {
  148. Map<String, String> dataMap = new HashMap<>();
  149. String subQ = "{\"q\":\"rowkey:" + profileId + "\"}";
  150. Envelop subData = resource.getSubData(subQ, 1, 1, null);
  151. List<Map<String, Object>> subList = subData.getDetailModelList();
  152. if (subList.size() > 0) {
  153. Map<String, Object> temp = subList.get(0);
  154. //检验报告单号
  155. String reportNo = temp.get("EHR_000363").toString();
  156. subQ = "{\"q\":\"rowkey:" + profileId.split("\\$")[0] + "$HDSD00_75$*" + "\"}";
  157. subData = resource.getSubData(subQ, 1, 1000, null);
  158. List<Map<String, Object>> subItems = subData.getDetailModelList();
  159. subItems.forEach(item -> {
  160. //子项所属的报告单号
  161. if (reportNo.equals(item.get("EHR_006339"))) {
  162. //结果值
  163. if (item.get("EHR_000387") != null) {
  164. //结果值
  165. String result = item.get("EHR_000387").toString();
  166. if (item.get("EHR_000391") != null && item.get("EHR_000390")!= null) {
  167. //下限
  168. String low = item.get("EHR_000391").toString();
  169. //上限
  170. String high = item.get("EHR_000390").toString();
  171. if (NumberValidationUtils.isRealNumber(result) && NumberValidationUtils.isRealNumber(low) && NumberValidationUtils.isRealNumber(high)) {
  172. Double _result = Double.parseDouble(result);
  173. Double _row = Double.parseDouble(low);
  174. Double _high = Double.parseDouble(high);
  175. if (_result < _row) {
  176. dataMap.put(item.get("EHR_000394").toString(), "偏低");
  177. } else if (_result > _high) {
  178. dataMap.put(item.get("EHR_000394").toString(), "偏高");
  179. } else {
  180. dataMap.put(item.get("EHR_000394").toString(), "正常");
  181. }
  182. } else {
  183. dataMap.put(item.get("EHR_000394").toString(), item.get("EHR_000387").toString());
  184. }
  185. } else {
  186. dataMap.put(item.get("EHR_000394").toString(), item.get("EHR_000387").toString());
  187. }
  188. } else {
  189. dataMap.put(item.get("EHR_000394").toString(), "");
  190. }
  191. }
  192. });
  193. }
  194. return dataMap;
  195. }
  196. public Map<String, Map<String, Integer>> inspectionStatisticsAllSub(String demographicId, String name) throws Exception {
  197. String query = "{\"q\":\"demographic_id:" + demographicId + "\"}";
  198. query = SimpleSolrQueryUtil.getQuery(null, null, query);
  199. Envelop masterEnvelop = resource.getMasterData(query, 1, 1000, null);
  200. List<Map<String, Object>> masterList = masterEnvelop.getDetailModelList();
  201. //循环获取结果集
  202. Set<String> data = new HashSet<>();
  203. Map<String, Integer> lowMap = new HashMap<>();
  204. Map<String, Integer> highMap = new HashMap<>();
  205. Map<String, Integer> normalMap = new HashMap<>();
  206. Map<String, Integer> unknownMap = new HashMap<>();
  207. Map<String, Integer> positiveMap = new HashMap<>();
  208. Map<String, Integer> negativeMap = new HashMap<>();
  209. for (Map<String, Object> masterItem : masterList) {
  210. String subQ = "{\"q\":\"rowkey:" + masterItem.get("rowkey") + "$HDSD00_77$*\"}";
  211. Envelop subEnvelop = resource.getSubData(subQ, 1, 1000, null);
  212. List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
  213. for (Map<String, Object> subItem : subList) {
  214. //检验项目
  215. if (name.equals(subItem.get("EHR_000352"))) {
  216. //检验报告单号
  217. String reportNo = subItem.get("EHR_000363").toString();
  218. subQ = "{\"q\":\"rowkey:" + masterItem.get(ResourceCells.ROWKEY) + "$HDSD00_75$*" + "\"}";
  219. subEnvelop = resource.getSubData(subQ, 1, 1000, null);
  220. List<Map<String, Object>> subItems = subEnvelop.getDetailModelList();
  221. subItems.forEach(item -> {
  222. //子项所属的报告单号
  223. if (reportNo.equals(item.get("EHR_006339"))) {
  224. //子项名称
  225. String itemName = item.get("EHR_000394").toString();
  226. data.add(itemName);
  227. //结果值
  228. if (item.get("EHR_000387") != null) {
  229. String result = item.get("EHR_000387").toString();
  230. if (item.get("EHR_000391") != null && item.get("EHR_000390")!= null) {
  231. //上限
  232. String low = item.get("EHR_000391").toString();
  233. //下限
  234. String high = item.get("EHR_000390").toString();
  235. if (NumberValidationUtils.isRealNumber(result) && NumberValidationUtils.isRealNumber(low) && NumberValidationUtils.isRealNumber(high)) {
  236. Double _result = Double.parseDouble(result);
  237. Double _row = Double.parseDouble(low);
  238. Double _high = Double.parseDouble(high);
  239. if (_result < _row) {
  240. if (lowMap.containsKey(itemName)) {
  241. lowMap.put(itemName, lowMap.get(itemName) + 1);
  242. } else {
  243. lowMap.put(itemName, 1);
  244. }
  245. } else if (_result > _high) {
  246. if (highMap.containsKey(itemName)) {
  247. highMap.put(itemName, highMap.get(itemName) + 1);
  248. } else {
  249. highMap.put(itemName, 1);
  250. }
  251. } else {
  252. if (normalMap.containsKey(itemName)) {
  253. normalMap.put(itemName, normalMap.get(itemName) + 1);
  254. } else {
  255. normalMap.put(itemName, 1);
  256. }
  257. }
  258. } else {
  259. if (unknownMap.containsKey(itemName)) {
  260. unknownMap.put(itemName, unknownMap.get(itemName) + 1);
  261. } else {
  262. unknownMap.put(itemName, 1);
  263. }
  264. }
  265. //参考值备注
  266. } else if (item.get("EHR_000382") != null) {
  267. String range = item.get("EHR_000382").toString();
  268. //上限
  269. if (range.split("-").length == 2) {
  270. String low = range.split("-")[0];
  271. //下限
  272. String high = range.split("-")[1];
  273. if (NumberValidationUtils.isRealNumber(result) && NumberValidationUtils.isRealNumber(low) && NumberValidationUtils.isRealNumber(high)) {
  274. Double _result = Double.parseDouble(result);
  275. Double _row = Double.parseDouble(low);
  276. Double _high = Double.parseDouble(high);
  277. if (_result < _row) {
  278. if (lowMap.containsKey(itemName)) {
  279. lowMap.put(itemName, lowMap.get(itemName) + 1);
  280. } else {
  281. lowMap.put(itemName, 1);
  282. }
  283. } else if (_result > _high) {
  284. if (highMap.containsKey(itemName)) {
  285. highMap.put(itemName, highMap.get(itemName) + 1);
  286. } else {
  287. highMap.put(itemName, 1);
  288. }
  289. } else {
  290. if (normalMap.containsKey(itemName)) {
  291. normalMap.put(itemName, normalMap.get(itemName) + 1);
  292. } else {
  293. normalMap.put(itemName, 1);
  294. }
  295. }
  296. } else {
  297. if (unknownMap.containsKey(itemName)) {
  298. unknownMap.put(itemName, unknownMap.get(itemName) + 1);
  299. } else {
  300. unknownMap.put(itemName, 1);
  301. }
  302. }
  303. } else {
  304. if (unknownMap.containsKey(itemName)) {
  305. unknownMap.put(itemName, unknownMap.get(itemName) + 1);
  306. } else {
  307. unknownMap.put(itemName, 1);
  308. }
  309. }
  310. } else {
  311. if (result.equals("阳性")) {
  312. if (positiveMap.containsKey(itemName)) {
  313. positiveMap.put(itemName, positiveMap.get(itemName) + 1);
  314. } else {
  315. positiveMap.put(itemName, 1);
  316. }
  317. } else if (result.equals("阴性")) {
  318. if (negativeMap.containsKey(itemName)) {
  319. negativeMap.put(itemName, negativeMap.get(itemName) + 1);
  320. } else {
  321. negativeMap.put(itemName, 1);
  322. }
  323. } else {
  324. if (unknownMap.containsKey(itemName)) {
  325. unknownMap.put(itemName, unknownMap.get(itemName) + 1);
  326. } else {
  327. unknownMap.put(itemName, 1);
  328. }
  329. }
  330. }
  331. } else {
  332. if (unknownMap.containsKey(itemName)) {
  333. unknownMap.put(itemName, unknownMap.get(itemName) + 1);
  334. } else {
  335. unknownMap.put(itemName, 1);
  336. }
  337. }
  338. }
  339. });
  340. }
  341. }
  342. }
  343. Map<String, Map<String, Integer>> result = new HashMap<>();
  344. data.forEach(item -> {
  345. Map<String, Integer> details = new HashMap<>();
  346. if (lowMap.containsKey(item)) {
  347. details.put("偏低", lowMap.get(item));
  348. }
  349. if (highMap.containsKey(item)) {
  350. details.put("偏高", highMap.get(item));
  351. }
  352. if (normalMap.containsKey(item)) {
  353. details.put("正常", normalMap.get(item));
  354. }
  355. if (unknownMap.containsKey(item)) {
  356. details.put("未知", unknownMap.get(item));
  357. }
  358. if (positiveMap.containsKey(item)) {
  359. details.put("阳性", positiveMap.get(item));
  360. }
  361. if (negativeMap.containsKey(item)) {
  362. details.put("阴性", negativeMap.get(item));
  363. }
  364. result.put(item, details);
  365. });
  366. return result;
  367. }
  368. private Map<String, Integer> sortByValue(Map<String, Integer> sourceMap) {
  369. if (sourceMap == null || sourceMap.isEmpty()) {
  370. return sourceMap;
  371. }
  372. Map<String, Integer> sortedMap = new LinkedHashMap<>();
  373. List<Map.Entry<String, Integer>> entryList = new ArrayList<>(sourceMap.entrySet());
  374. Collections.sort(entryList, new MapValueComparator());
  375. Iterator<Map.Entry<String, Integer>> iterator = entryList.iterator();
  376. while (iterator.hasNext()) {
  377. Map.Entry<String, Integer> tmpEntry = iterator.next();
  378. sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
  379. }
  380. return sortedMap;
  381. }
  382. class MapValueComparator implements Comparator<Map.Entry<String, Integer>> {
  383. @Override
  384. public int compare(Map.Entry<String, Integer> me1, Map.Entry<String, Integer> me2) {
  385. return - me1.getValue().compareTo(me2.getValue());
  386. }
  387. }
  388. }