123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- package com.yihu.ehr.profile.service;
- import com.yihu.ehr.profile.family.ResourceCells;
- import com.yihu.ehr.profile.model.DrugInfo;
- import com.yihu.ehr.profile.util.SimpleSolrQueryUtil;
- import com.yihu.ehr.util.rest.Envelop;
- import org.springframework.stereotype.Service;
- import org.springframework.util.StringUtils;
- import java.util.*;
- /**
- * Created by progr1mmer on 2018/3/13.
- */
- @Service
- public class ProfileMedicationService extends ProfileBasicService {
- public Map<String, Integer> medicationRanking(String demographicId, String filter, String hpCode, String date) throws Exception {
- String masterQ;
- if (!StringUtils.isEmpty(hpCode)) {
- masterQ = "{\"q\":\"demographic_id:" + demographicId + " AND health_problem:*" + hpCode + "*\"}";
- } else {
- masterQ = "{\"q\":\"demographic_id:" + demographicId + "\"}";
- }
- masterQ = SimpleSolrQueryUtil.getQuery(filter, date, masterQ);
- Map<String, Integer> dataMap = new HashMap<>();
- Envelop masterEnvelop = resource.getMasterData(masterQ, 1, 500, null);
- if (masterEnvelop.isSuccessFlg()) {
- List<Map<String, Object>> masterList = masterEnvelop.getDetailModelList();
- //循环获取结果集
- for (Map<String, Object> masterMap : masterList) {
- String rowKey = (String) masterMap.get(ResourceCells.ROWKEY);
- String subQ = "{\"q\":\"rowkey:" + rowKey + "$HDSD00_83$* OR rowkey:" + rowKey + "$HDSD00_84$*\"}";
- Envelop subEnvelop = resource.getSubData(subQ, 1, 1000, null);
- if (subEnvelop.isSuccessFlg()) {
- List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
- if (subList.size() > 0) {
- for (Map<String, Object> subMap : subList) {
- if (!StringUtils.isEmpty(subMap.get("EHR_000131"))) {
- String drugName = (String) subMap.get("EHR_000131");
- if (dataMap.containsKey(drugName)) {
- Integer count = dataMap.get(drugName);
- dataMap.put(drugName, count + 1);
- } else {
- dataMap.put(drugName, 1);
- }
- continue;
- }
- if (!StringUtils.isEmpty(subMap.get("EHR_000100"))) {
- String drugName = (String) subMap.get("EHR_000100");
- if (dataMap.containsKey(drugName)) {
- Integer count = dataMap.get(drugName);
- dataMap.put(drugName, count + 1);
- } else {
- dataMap.put(drugName, 1);
- }
- continue;
- }
- }
- }
- }
- }
- }
- return sortByValue(dataMap);
- }
- public List<DrugInfo> medicationRankingWithTable(String demographicId, String filter, String hpCode, String date, String table) throws Exception {
- String masterQ;
- if (!StringUtils.isEmpty(hpCode)) {
- masterQ = "{\"q\":\"demographic_id:" + demographicId + " AND health_problem:*" + hpCode + "*\"}";
- } else {
- masterQ = "{\"q\":\"demographic_id:" + demographicId + "\"}";
- }
- masterQ = SimpleSolrQueryUtil.getQuery(filter, date, masterQ);
- Map<String, Integer> dataMap = new HashMap<>();
- Envelop masterEnvelop = resource.getMasterData(masterQ, 1, 500, null);
- if (masterEnvelop.isSuccessFlg()) {
- List<Map<String, Object>> masterList = masterEnvelop.getDetailModelList();
- //循环获取结果集
- for (Map<String, Object> masterMap : masterList) {
- String rowKey = (String) masterMap.get(ResourceCells.ROWKEY);
- String subQ;
- if (StringUtils.isEmpty(table)) {
- subQ = "{\"q\":\"rowkey:" + rowKey + "$HDSD00_83$* OR rowkey:" + rowKey + "$HDSD00_84$*\"}";
- } else {
- subQ = "{\"q\":\"rowkey:" + rowKey + "$" + table + "$*\"}";
- }
- Envelop subEnvelop = resource.getSubData(subQ, 1, 1000, null);
- if (subEnvelop.isSuccessFlg()) {
- List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
- if (subList.size() > 0) {
- for (Map<String, Object> subMap : subList) {
- if (!StringUtils.isEmpty(subMap.get("EHR_000131"))) {
- String drugName = subMap.get("EHR_000131") + "_01"; //中药名称
- if (dataMap.containsKey(drugName)) {
- Integer count = dataMap.get(drugName);
- dataMap.put(drugName, count + 1);
- } else {
- dataMap.put(drugName, 1);
- }
- continue;
- }
- if (!StringUtils.isEmpty(subMap.get("EHR_000100"))) {
- String drugName = subMap.get("EHR_000100") + "_02"; //西药名称
- if (dataMap.containsKey(drugName)) {
- Integer count = dataMap.get(drugName);
- dataMap.put(drugName, count + 1);
- } else {
- dataMap.put(drugName, 1);
- }
- continue;
- }
- }
- }
- }
- }
- }
- List<DrugInfo> drugInfos = new ArrayList<>();
- dataMap = sortByValue(dataMap);
- dataMap.forEach((key, val) -> {
- DrugInfo drugInfo = new DrugInfo();
- String [] info = key.split("_");
- drugInfo.setName(info[0]);
- drugInfo.setCount(val);
- if (info[1].endsWith("01")) {
- drugInfo.setLabel("中药");
- } else {
- drugInfo.setLabel("西药");
- }
- drugInfos.add(drugInfo);
- });
- return drugInfos;
- }
- public List<Map<String, Object>> medicationRecords(String demographicId, String filter, String date, String keyWord) throws Exception {
- List<Map<String, Object>> resultList = new ArrayList<>();
- String masterQ = "{\"q\":\"demographic_id:" + demographicId + "\"}";
- masterQ = SimpleSolrQueryUtil.getQuery(filter, date, masterQ);
- Envelop masterEnvelop = resource.getMasterData(masterQ, 1, 500, null);
- if (masterEnvelop.isSuccessFlg()) {
- List<Map<String, Object>> masterList = masterEnvelop.getDetailModelList();
- //循环获取结果集
- for (Map<String, Object> masterMap : masterList) {
- String rowKey = (String) masterMap.get(ResourceCells.ROWKEY);
- String subQ = "{\"q\":\"rowkey:" + rowKey + "$HDSD00_83$* OR rowkey:" + rowKey + "$HDSD00_84$*\"}";
- Envelop subEnvelop = resource.getSubData(subQ, 1, 1000, null);
- if (subEnvelop.isSuccessFlg()) {
- List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
- if (subList.size() > 0) {
- boolean match = false;
- String typeMark = "00"; //中西药标识
- for (Map<String, Object> subMap : subList) {
- if (subMap.get("EHR_000131") != null) {
- if (subMap.get(ResourceCells.ROWKEY).toString().contains("HDSD00_83")) {
- typeMark = "01"; //中药
- } else {
- typeMark = "02"; //西药
- }
- if (keyWord != null) {
- if (subMap.get("EHR_000131").toString().contains(keyWord)) {
- match = true;
- break;
- }
- } else {
- match = true;
- break;
- }
- }
- if (subMap.get("EHR_000100") != null) {
- if (subMap.get(ResourceCells.ROWKEY).toString().contains("HDSD00_83")) {
- typeMark = "01"; //中药
- } else {
- typeMark = "02"; //西药
- }
- if (keyWord != null && subMap.get("EHR_000100") != null) {
- if (subMap.get("EHR_000100").toString().contains(keyWord)) {
- match = true;
- break;
- }
- } else {
- match = true;
- break;
- }
- }
- }
- if (match) {
- //时间轴基本字段
- Map<String, Object> resultMap = simpleEvent(masterMap, null);
- resultMap.put("mark", typeMark);
- if (masterMap.get(ResourceCells.EVENT_TYPE).equals("0")) { //门诊信息
- resultMap.put("department", masterMap.get("EHR_000082"));
- resultMap.put("doctor", masterMap.get("EHR_000079"));
- } else if (masterMap.get(ResourceCells.EVENT_TYPE).equals("1")) { //住院信息
- resultMap.put("department", masterMap.get("EHR_000229"));
- resultMap.put("doctor", masterMap.get("EHR_005072"));
- }
- resultList.add(resultMap);
- }
- }
- }
- }
- }
- return resultList;
- }
- public Envelop recentMedicationSub(String demographicId, String date, Integer page, Integer size) throws Exception {
- String masterQ = "{\"q\":\"demographic_id:" + demographicId + "\"}";
- masterQ = SimpleSolrQueryUtil.getQuery(null, date, masterQ);
- Envelop masterEnvelop = resource.getMasterData(masterQ, 1, 500, null);
- List<Map<String, Object>> masterList = masterEnvelop.getDetailModelList();
- List<Map<String, Object>> dataList = new ArrayList<>();
- Object eventDate = "";
- for (Map<String, Object> event : masterList) {
- //详情
- String rowKey = (String) event.get(ResourceCells.ROWKEY);
- String subQ = "{\"q\":\"rowkey:" + rowKey + "$HDSD00_83$* OR rowkey:" + rowKey + "$HDSD00_84$*\"}";
- Envelop subEnvelop = resource.getSubData(subQ, 1, 1000, null);
- List<Map<String, Object>> subList = subEnvelop.getDetailModelList();
- if (subList.size() > 0) {
- for (Map<String, Object> subMap : subList) {
- Map<String, Object> dataMap = new HashMap<>();
- dataMap.put("prescriptionNumber", subMap.get("EHR_000086") == null ? "" : subMap.get("EHR_000086") ); //处方编号
- dataMap.put("substancesForDrugUse", subMap.get("EHR_000101") == null ? "" : subMap.get("EHR_000101")); //药物使用次剂量
- dataMap.put("prescriptionDrugGroupNumber", subMap.get("EHR_000127") == null ? "" : subMap.get("EHR_000127")); //处方药品组号
- dataMap.put("drugSpecifications", subMap.get("EHR_000129") == null ? "" : subMap.get("EHR_000129")); //药物规格
- dataMap.put("drugFormulationCode", subMap.get("EHR_000130") == null ? "" : subMap.get("EHR_000130")); //药物剂型代码
- dataMap.put("drugFormulationValue", subMap.get("EHR_000130_VALUE") == null ? "" : subMap.get("EHR_000130_VALUE")); //药物剂型值
- String drugName = "";
- if (!StringUtils.isEmpty(subMap.get("EHR_000100"))) {
- drugName = (String) subMap.get("EHR_000100");
- } else if (!StringUtils.isEmpty(subMap.get("EHR_000131"))) {
- drugName = (String) subMap.get("EHR_000131");
- }
- dataMap.put("drugName", drugName); //药物名称
- dataMap.put("drugsUseDosageUnits", subMap.get("EHR_000133") == null ? "" : subMap.get("EHR_000133")); //药物使用剂量单位
- dataMap.put("drugUsageFrequencyCode", subMap.get("EHR_000134") == null ? "" : subMap.get("EHR_000134")); //药物使用频次代码
- dataMap.put("drugUsageFrequencyValue", subMap.get("EHR_000134_VALUE") == null ? "" : subMap.get("EHR_000134_VALUE")); //药物使用频次值
- dataMap.put("totalDoseOfDrugUsed", subMap.get("EHR_000135") == null ? "" : subMap.get("EHR_000135")); //药物使用总剂量
- dataMap.put("medicationRouteCode", subMap.get("EHR_000136") == null ? "" : subMap.get("EHR_000136")); //用药途径代码
- dataMap.put("medicationRouteValue", subMap.get("EHR_000136_VALUE") == null ? "" : subMap.get("EHR_000136_VALUE")); //用药途径值
- String dataSetCode = String.valueOf(subMap.get("rowkey")).split("\\$")[1];
- if ("HDSD00_84".equals(dataSetCode)) { //西药
- dataMap.put("drugUseTotalDoseUnit", subMap.get("EHR_001249") == null ? "" : subMap.get("EHR_001249")); //药物使用总剂量单位
- } else { //中药
- dataMap.put("drugUseTotalDoseUnit", subMap.get("EHR_001250") == null ? "" : subMap.get("EHR_001250")); //药物使用总剂量单位
- }
- dataList.add(dataMap);
- }
- }
- if (dataList.size() > 0) {
- eventDate = event.get(ResourceCells.EVENT_DATE);
- break;
- }
- }
- Envelop envelop = new Envelop();
- envelop.setSuccessFlg(true);
- envelop.setCurrPage(page);
- envelop.setPageSize(size);
- envelop.setTotalPage(dataList.size() % size > 0 ? dataList.size() / size + 1 : dataList.size() / size);
- envelop.setTotalCount(dataList.size());
- List result = new ArrayList();
- for (int i = (page - 1) * size; i < page * size; i ++) {
- if (i > dataList.size() - 1) {
- break;
- }
- result.add(dataList.get(i));
- }
- envelop.setDetailModelList(result);
- envelop.setObj(eventDate);
- return envelop;
- }
- private Map<String, Integer> sortByValue(Map<String, Integer> sourceMap) {
- if (sourceMap == null || sourceMap.isEmpty()) {
- return sourceMap;
- }
- Map<String, Integer> sortedMap = new LinkedHashMap<>();
- List<Map.Entry<String, Integer>> entryList = new ArrayList<>(sourceMap.entrySet());
- Collections.sort(entryList, new MapValueComparator());
- Iterator<Map.Entry<String, Integer>> iterator = entryList.iterator();
- while (iterator.hasNext()) {
- Map.Entry<String, Integer> tmpEntry = iterator.next();
- sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
- }
- return sortedMap;
- }
- class MapValueComparator implements Comparator<Map.Entry<String, Integer>> {
- @Override
- public int compare(Map.Entry<String, Integer> me1, Map.Entry<String, Integer> me2) {
- return - me1.getValue().compareTo(me2.getValue());
- }
- }
- }
|