MedicalInsuranceScheduler.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package com.yihu.quota.scheduler;
  2. import com.yihu.ehr.constants.ApiVersion;
  3. import com.yihu.ehr.elasticsearch.ElasticSearchUtil;
  4. import com.yihu.ehr.profile.core.ResourceCore;
  5. import com.yihu.ehr.solr.SolrUtil;
  6. import com.yihu.ehr.util.datetime.DateUtil;
  7. import com.yihu.ehr.util.rest.Envelop;
  8. import com.yihu.quota.service.medicalInsurance.MedicalInsuranceService;
  9. import io.swagger.annotations.Api;
  10. import io.swagger.annotations.ApiOperation;
  11. import io.swagger.annotations.ApiParam;
  12. import org.apache.commons.lang.time.DateUtils;
  13. import org.apache.solr.common.SolrDocumentList;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.scheduling.annotation.Scheduled;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RequestMethod;
  20. import org.springframework.web.bind.annotation.RequestParam;
  21. import org.springframework.web.bind.annotation.RestController;
  22. import java.text.SimpleDateFormat;
  23. import java.util.*;
  24. /**
  25. * Created by wxw on 2018/10/08.
  26. * 医保费用-频繁就医
  27. * @author wxw.
  28. */
  29. @RestController
  30. @RequestMapping(value = ApiVersion.Version1_0)
  31. @Api(description = "医保费用", tags = {"医保费用"})
  32. public class MedicalInsuranceScheduler {
  33. @Autowired
  34. private SolrUtil solrUtil;
  35. @Autowired
  36. private ElasticSearchUtil elasticSearchUtil;
  37. @Autowired
  38. private MedicalInsuranceService medicalInsuranceService;
  39. private static final Logger log = LoggerFactory.getLogger(MedicalInsuranceScheduler.class);
  40. private SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd'T'23:59:59'Z'");
  41. @Scheduled(cron = "0 50 07 * * ?")
  42. public void statisticOftenIllScheduler() {
  43. try {
  44. Date now = new Date();
  45. Calendar innerCalendar = Calendar.getInstance();
  46. innerCalendar.setTime(now);
  47. // 构建前后3天,形成一周
  48. innerCalendar.add(Calendar.DATE, -3);
  49. String preDate = DateUtil.utcToDate(innerCalendar.getTime());
  50. innerCalendar.add(Calendar.DATE, 6);
  51. String sufDate = dfs.format(innerCalendar.getTime());
  52. Calendar calendar = Calendar.getInstance();
  53. calendar.setTime(now);
  54. // 设置成本月第一天
  55. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
  56. calendar.add(Calendar.DATE, -3);
  57. String start = DateUtil.utcToDate(calendar.getTime());
  58. calendar.add(Calendar.DATE, 3);
  59. // 设置成本月最后一天
  60. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
  61. calendar.add(Calendar.DATE, 3);
  62. String end = dfs.format(calendar.getTime());
  63. String fq = "event_date:[" + preDate + " TO " + sufDate + "]";
  64. String countFq = "event_date:[" + start + " TO " + end + "]";
  65. oftenIll(fq, countFq);
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. @ApiOperation("执行指定时间段频繁就医")
  71. @RequestMapping(value = "/extractOftenIll", method = RequestMethod.GET)
  72. public Envelop extractOftenIll(
  73. @ApiParam(name = "startDate", value = "开始日期,格式 YYYY-MM-DD,接口里自动拼接 T00:00:00Z,", required = true)
  74. @RequestParam(value = "startDate") String startDate,
  75. @ApiParam(name = "endDate", value = "截止日期,格式 YYYY-MM-DD,接口里自动拼接 T23:59:59Z", required = true)
  76. @RequestParam(value = "endDate") String endDate) {
  77. Envelop envelop = new Envelop();
  78. envelop.setSuccessFlg(true);
  79. try {
  80. Date now = DateUtil.strToDate(startDate);
  81. Calendar date = Calendar.getInstance();
  82. date.setTime(now);
  83. while (medicalInsuranceService.belongCalendar(date, startDate + "T00:00:00Z", endDate + "T23:59:59Z")) {
  84. Calendar innerCalendar = Calendar.getInstance();
  85. innerCalendar.setTime(date.getTime());
  86. // 构建前后3天,形成一周
  87. innerCalendar.add(Calendar.DATE, -3);
  88. String preDate = DateUtil.utcToDate(innerCalendar.getTime());
  89. innerCalendar.add(Calendar.DATE, 6);
  90. String sufDate = dfs.format(innerCalendar.getTime());
  91. Calendar calendar = Calendar.getInstance();
  92. calendar.setTime(date.getTime());
  93. // 设置成本月第一天
  94. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
  95. calendar.add(Calendar.DATE, -3);
  96. String start = DateUtil.utcToDate(calendar.getTime());
  97. calendar.add(Calendar.DATE, 3);
  98. // 设置成本月最后一天
  99. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
  100. calendar.add(Calendar.DATE, 3);
  101. String end = dfs.format(calendar.getTime());
  102. date.add(Calendar.DATE, 1);
  103. String fq = "event_date:[" + preDate + " TO " + sufDate + "]";
  104. String countFq = "event_date:[" + start + " TO " + end + "]";
  105. oftenIll(fq, countFq);
  106. }
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. envelop.setSuccessFlg(false);
  110. envelop.setErrorMsg(e.getMessage());
  111. }
  112. return envelop;
  113. }
  114. /**
  115. * 频繁就医逻辑
  116. * @param fq
  117. * @throws Exception
  118. */
  119. public void oftenIll(String fq, String countFq) throws Exception {
  120. String demographicId = "demographic_id";
  121. String[] fields = {"rowkey","patient_name","patient_age","patient_sex","demographic_id","EHR_001211","event_date"};
  122. // 一周内就诊次数大于3的用户
  123. Map<String, Long> groupMap = solrUtil.groupCountLte(ResourceCore.MasterTable, null, fq, demographicId, 0, -1, 3);
  124. // 遍历 查询用户信息并存到ES
  125. for (String key : groupMap.keySet()) {
  126. Map<String, Object> mapInfo = new HashMap();
  127. SolrDocumentList query = solrUtil.query(ResourceCore.MasterTable, "demographic_id:\"" + key + "\"", fq, null, 0, 1, fields);
  128. Date date = DateUtils.addHours((Date) query.get(0).get("event_date"), -8);
  129. String sex = null != query.get(0).get("patient_sex") ? query.get(0).get("patient_sex") + "" : "";
  130. String strDate = DateUtil.toString(date, DateUtil.DEFAULT_DATE_YM_FORMAT);
  131. long count = solrUtil.count(ResourceCore.MasterTable, "demographic_id:\"" + key + "\"", countFq);
  132. mapInfo.put("_id", key + strDate);
  133. mapInfo.put("idCardNo", key);
  134. mapInfo.put("name", null != query.get(0).get("patient_name") ? query.get(0).get("patient_name") : "");
  135. mapInfo.put("age", null != query.get(0).get("patient_age") ? query.get(0).get("patient_age") : "");
  136. mapInfo.put("sex", sexExchangeByCode(sex));
  137. mapInfo.put("address", null != query.get(0).get("EHR_001211") ? query.get(0).get("EHR_001211") : "");
  138. mapInfo.put("eventDate", DateUtil.formatCharDate(DateUtil.toString((Date) query.get(0).get("event_date"), DateUtil.DATE_WORLD_FORMAT), DateUtil.DATE_WORLD_FORMAT));
  139. mapInfo.put("createTime", new Date());
  140. mapInfo.put("result", count);
  141. // type = 1 频繁就医
  142. mapInfo.put("type", 1);
  143. // 保存到ES
  144. saveToEs(mapInfo);
  145. }
  146. }
  147. /**
  148. * 性别字典转换
  149. * @param code
  150. * @return
  151. */
  152. private String sexExchangeByCode(String code) {
  153. String sex = "";
  154. switch (code) {
  155. case "0" : {
  156. sex = "未知的性别";
  157. break;
  158. }
  159. case "1" : {
  160. sex = "男性";
  161. break;
  162. }
  163. case "2" : {
  164. sex = "女性";
  165. break;
  166. }
  167. case "9" : {
  168. sex = "未说明的性别";
  169. break;
  170. }
  171. default : {
  172. break;
  173. }
  174. }
  175. return sex;
  176. }
  177. /**
  178. * 存储到es
  179. *
  180. * @param medicalInsuranceInfo
  181. * @throws Exception
  182. */
  183. public void saveToEs(Map<String, Object> medicalInsuranceInfo) throws Exception {
  184. String index = "medical_insurance_index";
  185. String type = "medical_insurance";
  186. long start = System.currentTimeMillis();
  187. log.info("开始保存时间 = {}", medicalInsuranceInfo, start);
  188. elasticSearchUtil.index(index, type, medicalInsuranceInfo);
  189. long end = System.currentTimeMillis();
  190. log.info("用时 = {}毫秒", end - start);
  191. }
  192. }