OutPatientCostScheduler.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package com.yihu.quota.scheduler.special;
  2. import com.fasterxml.jackson.annotation.JsonInclude;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.yihu.ehr.constants.ApiVersion;
  5. import com.yihu.ehr.elasticsearch.ElasticSearchUtil;
  6. import com.yihu.ehr.profile.core.ResourceCore;
  7. import com.yihu.ehr.solr.SolrUtil;
  8. import com.yihu.ehr.util.datetime.DateUtil;
  9. import com.yihu.ehr.util.rest.Envelop;
  10. import com.yihu.quota.service.special.scheduler.HealthArchiveSchedulerService;
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiOperation;
  13. import io.swagger.annotations.ApiParam;
  14. import org.apache.commons.lang.StringUtils;
  15. import org.apache.commons.lang.time.DateUtils;
  16. import org.apache.solr.common.SolrDocument;
  17. import org.apache.solr.common.SolrDocumentList;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.scheduling.annotation.Scheduled;
  22. import org.springframework.web.bind.annotation.RequestMapping;
  23. import org.springframework.web.bind.annotation.RequestMethod;
  24. import org.springframework.web.bind.annotation.RequestParam;
  25. import org.springframework.web.bind.annotation.RestController;
  26. import java.text.DecimalFormat;
  27. import java.util.*;
  28. /**
  29. * Created by wxw on 2018/3/24.
  30. */
  31. @RestController
  32. @RequestMapping(value = ApiVersion.Version1_0)
  33. @Api(description = "统计疾病费用", tags = {"solr跨表数据抽取--统计疾病费用"})
  34. public class OutPatientCostScheduler {
  35. private static final Logger log = LoggerFactory.getLogger(OutPatientCostScheduler.class);
  36. DecimalFormat df = new DecimalFormat("#.00");
  37. @Autowired
  38. private SolrUtil solrUtil;
  39. @Autowired
  40. private ObjectMapper objectMapper;
  41. @Autowired
  42. private HealthArchiveSchedulerService healthArchiveSchedulerService;
  43. @Autowired
  44. private ElasticSearchUtil elasticSearchUtil;
  45. /**
  46. * 门急诊疾病费用统计
  47. */
  48. @Scheduled(cron = "0 36 07 * * ?")
  49. public void statisticOutPatientDiseaseCostScheduler() {
  50. try {
  51. Date now = new Date();
  52. Date yesterdayDate = DateUtils.addDays(now, -1);
  53. String yesterday = DateUtil.formatDate(yesterdayDate, DateUtil.DEFAULT_DATE_YMD_FORMAT);
  54. String fq = "event_date:[" + yesterday + "T00:00:00Z TO " + yesterday + "T23:59:59Z]";
  55. String q = "EHR_000109:*"; // 查询条件 诊断代码(门诊)
  56. String sQuery = "EHR_000045:* AND profile_id:";
  57. String sQuery2 = "EHR_000109:* AND profile_id:";
  58. String keyEventDate = "event_date";
  59. String keyArea = "org_area"; // 行政区划代码 EHR_001225
  60. String keyDisease = "EHR_000109"; // 诊断代码(门诊)
  61. String keyDiseaseName = "EHR_000112"; // 诊断名称(门诊)
  62. String keyCost = "EHR_000045"; // 门诊费用金额(元)
  63. // 抽取并保存到ES
  64. saveDiseaseExpenseData(fq, q, sQuery, sQuery2, keyEventDate, keyArea, keyDisease, keyDiseaseName, keyCost, 1);
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. /**
  70. * 住院疾病费用统计
  71. */
  72. @Scheduled(cron = "0 56 07 * * ?")
  73. public void statisticInPatientDiseaseCostScheduler() {
  74. try {
  75. Date now = new Date();
  76. Date yesterdayDate = DateUtils.addDays(now, -1);
  77. String yesterday = DateUtil.formatDate(yesterdayDate, DateUtil.DEFAULT_DATE_YMD_FORMAT);
  78. String fq = "event_date:[" + yesterday + "T00:00:00Z TO " + yesterday + "T23:59:59Z]";
  79. String q = "EHR_000293:*"; // 查询条件 诊断代码(住院)
  80. String sQuery = "EHR_000175:* AND profile_id:";
  81. String sQuery2 = "EHR_000293:* AND profile_id:";
  82. String keyEventDate = "event_date";
  83. String keyArea = "org_area"; // 行政区划代码
  84. String keyDisease = "EHR_000293"; // 诊断代码(住院)
  85. String keyDiseaseName = "EHR_000295"; // 诊断名称(住院)
  86. String keyCost = "EHR_000175"; // 门诊费用金额(元)
  87. // 抽取并保存到ES
  88. saveDiseaseExpenseData(fq, q, sQuery, sQuery2, keyEventDate, keyArea, keyDisease, keyDiseaseName, keyCost, 3);
  89. } catch (Exception e) {
  90. e.printStackTrace();
  91. }
  92. }
  93. @ApiOperation("抽取指定时间段住院疾病费用")
  94. @RequestMapping(value = "/extractInPatientDiseaseExpense", method = RequestMethod.GET)
  95. public Envelop extractInPatientDiseaseExpense(
  96. @ApiParam(name = "startDate", value = "开始日期,格式 YYYY-MM-DD,接口里自动拼接 T00:00:00Z,", required = true)
  97. @RequestParam(value = "startDate") String startDate,
  98. @ApiParam(name = "endDate", value = "截止日期,格式 YYYY-MM-DD,接口里自动拼接 T23:59:59Z", required = true)
  99. @RequestParam(value = "endDate") String endDate) {
  100. Envelop envelop = new Envelop();
  101. envelop.setSuccessFlg(true);
  102. try {
  103. String fq = "event_date:[" + startDate + "T00:00:00Z TO " + endDate + "T23:59:59Z]";
  104. String q = "EHR_000293:*"; // 查询条件 诊断代码(住院)
  105. String sQuery = "EHR_000175:* AND profile_id:";
  106. String sQuery2 = "EHR_000293:* AND profile_id:";
  107. String keyEventDate = "event_date";
  108. String keyArea = "org_area"; // 行政区划代码
  109. String keyDisease = "EHR_000293"; // 诊断代码(住院)
  110. String keyDiseaseName = "EHR_000295"; // 诊断名称(住院)
  111. String keyCost = "EHR_000175"; // 门诊费用金额(元)
  112. // 抽取并保存到ES
  113. saveDiseaseExpenseData(fq, q, sQuery, sQuery2, keyEventDate, keyArea, keyDisease, keyDiseaseName, keyCost, 3);
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. envelop.setSuccessFlg(false);
  117. envelop.setErrorMsg(e.getMessage());
  118. }
  119. return envelop;
  120. }
  121. @ApiOperation("抽取指定时间段门急诊疾病费用")
  122. @RequestMapping(value = "/extractOutPatientDiseaseExpense", method = RequestMethod.GET)
  123. public Envelop extractOutPatientDiseaseExpense(
  124. @ApiParam(name = "startDate", value = "开始日期,格式 YYYY-MM-DD,接口里自动拼接 T00:00:00Z,", required = true)
  125. @RequestParam(value = "startDate") String startDate,
  126. @ApiParam(name = "endDate", value = "截止日期,格式 YYYY-MM-DD,接口里自动拼接 T23:59:59Z", required = true)
  127. @RequestParam(value = "endDate") String endDate) {
  128. Envelop envelop = new Envelop();
  129. envelop.setSuccessFlg(true);
  130. try {
  131. String fq = "event_date:[" + startDate + "T00:00:00Z TO " + endDate + "T23:59:59Z]";
  132. String q = "EHR_000109:*"; // 查询条件 诊断代码(门诊)
  133. String sQuery = "EHR_000045:* AND profile_id:";
  134. String sQuery2 = "EHR_000109:* AND profile_id:";
  135. String keyEventDate = "event_date";
  136. String keyArea = "org_area"; // 行政区划代码 EHR_001225
  137. String keyDisease = "EHR_000109"; // 诊断代码(门诊)
  138. String keyDiseaseName = "EHR_000112"; // 诊断名称(门诊)
  139. String keyCost = "EHR_000045"; // 门诊费用金额(元)
  140. // 抽取并保存到ES
  141. saveDiseaseExpenseData(fq, q, sQuery, sQuery2, keyEventDate, keyArea, keyDisease, keyDiseaseName, keyCost, 1);
  142. } catch (Exception e) {
  143. e.printStackTrace();
  144. envelop.setSuccessFlg(false);
  145. envelop.setErrorMsg(e.getMessage());
  146. }
  147. return envelop;
  148. }
  149. /**
  150. * 存储到es, index:hospitalCost(别名)
  151. *
  152. * @param costInfo
  153. * @throws Exception
  154. */
  155. public void saveToEs(Map<String, Object> costInfo) throws Exception {
  156. String index = "hospitalCost";
  157. String type = "cost_info";
  158. long start = System.currentTimeMillis();
  159. log.info("开始保存时间 = {}", costInfo, start);
  160. elasticSearchUtil.index(index, type, costInfo);
  161. long end = System.currentTimeMillis();
  162. log.info("用时 = {}毫秒", end - start);
  163. }
  164. /**
  165. * 抽取疾病费用并保存到ES
  166. *
  167. * @param fq
  168. * @throws Exception
  169. */
  170. private void saveDiseaseExpenseData(String fq, String q, String sQuery, String sQuery2, String keyEventDate, String keyArea,
  171. String keyDisease, String keyDiseaseName, String keyCost, Integer type) throws Exception {
  172. String sq = "";
  173. String sq2 = "";
  174. objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
  175. List<String> profileIdList = new ArrayList<>();
  176. profileIdList.clear();
  177. // 疾病的处理
  178. // 找出就诊档案数
  179. long count = solrUtil.count(ResourceCore.SubTable, q, fq);
  180. // 细表中查询EHR_000109:* 的记录, 返回细表的profile_id
  181. profileIdList = selectSubProfileId(ResourceCore.SubTable, q, fq, count < 1000 ? 1000 : count);
  182. if (profileIdList != null && profileIdList.size() > 0) {
  183. for (String profileId : profileIdList) {
  184. sq = sQuery + profileId; // EHR_000045:* AND profile_id:xx
  185. sq2 = sQuery2 + profileId; // 查询疾病编码
  186. List<String> keyList = healthArchiveSchedulerService.selectSubRowKey(ResourceCore.SubTable, sq2, fq, count < 1000 ? 1000 : count);
  187. if (keyList != null && keyList.size() > 0) {
  188. Double value = 0.0;
  189. Map<String, String> diseaseName = new HashMap<>();
  190. Map<String, String> rowKeyName = new HashMap<>();
  191. List<Map<String, Object>> dataList = healthArchiveSchedulerService.selectHbaseData(ResourceCore.SubTable, keyList);
  192. if (dataList != null && dataList.size() > 0) {
  193. for (Map<String, Object> map : dataList) {
  194. diseaseName.put(map.get(keyDisease) + "", map.get(keyDiseaseName) + "");
  195. rowKeyName.put(map.get(keyDisease) + "", map.get("rowkey") + "");
  196. }
  197. List<String> rowKeyList = healthArchiveSchedulerService.selectSubRowKey(ResourceCore.SubTable, sq, fq, count < 1000 ? 1000 : count);
  198. if (rowKeyList != null && rowKeyList.size() > 0) {
  199. String town = "";
  200. String org = "";
  201. String dept = "";
  202. String eventDate = "";
  203. List<Map<String, Object>> hbaseDataList = healthArchiveSchedulerService.selectHbaseData(ResourceCore.SubTable, rowKeyList);
  204. if (hbaseDataList != null && hbaseDataList.size() > 0) {
  205. for (Map<String, Object> map : hbaseDataList) {
  206. if (StringUtils.isEmpty(town) && null != map.get(keyArea)) {
  207. town = map.get(keyArea).toString();
  208. }
  209. if (StringUtils.isEmpty(org) && null != map.get("org_code")) {
  210. org = map.get("org_code").toString();
  211. }
  212. if (StringUtils.isEmpty(dept) && null != map.get("dept_code")) {
  213. dept = map.get("dept_code").toString();
  214. }
  215. if (StringUtils.isEmpty(eventDate) && null != map.get(keyEventDate)) {
  216. eventDate = map.get(keyEventDate) + "";
  217. }
  218. value += Double.parseDouble(null != map.get(keyCost) ? map.get(keyCost) + "" : "0");
  219. }
  220. }
  221. String valueStr = value == 0 ? value.toString() : df.format(value);
  222. for (Map.Entry<String, String> m : diseaseName.entrySet()) {
  223. Map<String, Object> costInfo = new HashMap<>();
  224. costInfo.put("_id", rowKeyName.get(m.getKey()) + type.toString());
  225. costInfo.put("type", type);
  226. costInfo.put("code", m.getKey());
  227. costInfo.put("name", m.getValue());
  228. costInfo.put("result", valueStr);
  229. costInfo.put("town", town);
  230. costInfo.put("org", org);
  231. costInfo.put("dept", dept);
  232. costInfo.put("eventDate", DateUtil.formatCharDate(eventDate, DateUtil.DATE_WORLD_FORMAT));
  233. costInfo.put("createTime", new Date());
  234. // 将疾病费用保存到ES
  235. saveToEs(costInfo);
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }
  243. // 获取查询结果中的profile_id
  244. private List<String> selectSubProfileId(String core, String q, String fq, long count) throws Exception {
  245. List<String> data = new ArrayList<>();
  246. /***** Solr查询 ********/
  247. SolrDocumentList solrList = solrUtil.query(core, q, fq, null, 0, count);
  248. if (solrList != null && solrList.getNumFound() > 0) {
  249. for (SolrDocument doc : solrList) {
  250. String rowkey = String.valueOf(doc.getFieldValue("profile_id"));
  251. data.add(rowkey);
  252. }
  253. // 去掉重复
  254. Set<String> hashSet = new HashSet<>(data);
  255. data.clear();
  256. data.addAll(hashSet);
  257. }
  258. return data;
  259. }
  260. }