123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- package com.yihu.quota.scheduler;
- import com.yihu.ehr.constants.ApiVersion;
- import com.yihu.ehr.elasticsearch.ElasticSearchUtil;
- import com.yihu.ehr.profile.core.ResourceCore;
- import com.yihu.ehr.solr.SolrUtil;
- import com.yihu.ehr.util.datetime.DateUtil;
- import com.yihu.ehr.util.rest.Envelop;
- import com.yihu.quota.service.medicalInsurance.MedicalInsuranceService;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import io.swagger.annotations.ApiParam;
- import org.apache.commons.lang.time.DateUtils;
- import org.apache.solr.common.SolrDocumentList;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import java.text.SimpleDateFormat;
- import java.util.*;
- /**
- * Created by wxw on 2018/10/08.
- * 医保费用-频繁就医
- * @author wxw.
- */
- @RestController
- @RequestMapping(value = ApiVersion.Version1_0)
- @Api(description = "医保费用", tags = {"医保费用"})
- public class MedicalInsuranceScheduler {
- @Autowired
- private SolrUtil solrUtil;
- @Autowired
- private ElasticSearchUtil elasticSearchUtil;
- @Autowired
- private MedicalInsuranceService medicalInsuranceService;
- private static final Logger log = LoggerFactory.getLogger(MedicalInsuranceScheduler.class);
- private SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd'T'23:59:59'Z'");
- @Scheduled(cron = "0 50 07 * * ?")
- public void statisticOftenIllScheduler() {
- try {
- Date now = new Date();
- Calendar innerCalendar = Calendar.getInstance();
- innerCalendar.setTime(now);
- // 构建前后3天,形成一周
- innerCalendar.add(Calendar.DATE, -3);
- String preDate = DateUtil.utcToDate(innerCalendar.getTime());
- innerCalendar.add(Calendar.DATE, 6);
- String sufDate = dfs.format(innerCalendar.getTime());
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(now);
- // 设置成本月第一天
- calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
- calendar.add(Calendar.DATE, -3);
- String start = DateUtil.utcToDate(calendar.getTime());
- calendar.add(Calendar.DATE, 3);
- // 设置成本月最后一天
- calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
- calendar.add(Calendar.DATE, 3);
- String end = dfs.format(calendar.getTime());
- String fq = "event_date:[" + preDate + " TO " + sufDate + "]";
- String countFq = "event_date:[" + start + " TO " + end + "]";
- oftenIll(fq, countFq);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @ApiOperation("执行指定时间段频繁就医")
- @RequestMapping(value = "/extractOftenIll", method = RequestMethod.GET)
- public Envelop extractOftenIll(
- @ApiParam(name = "startDate", value = "开始日期,格式 YYYY-MM-DD,接口里自动拼接 T00:00:00Z,", required = true)
- @RequestParam(value = "startDate") String startDate,
- @ApiParam(name = "endDate", value = "截止日期,格式 YYYY-MM-DD,接口里自动拼接 T23:59:59Z", required = true)
- @RequestParam(value = "endDate") String endDate) {
- Envelop envelop = new Envelop();
- envelop.setSuccessFlg(true);
- try {
- Date now = DateUtil.strToDate(startDate);
- Calendar date = Calendar.getInstance();
- date.setTime(now);
- while (medicalInsuranceService.belongCalendar(date, startDate + "T00:00:00Z", endDate + "T23:59:59Z")) {
- Calendar innerCalendar = Calendar.getInstance();
- innerCalendar.setTime(date.getTime());
- // 构建前后3天,形成一周
- innerCalendar.add(Calendar.DATE, -3);
- String preDate = DateUtil.utcToDate(innerCalendar.getTime());
- innerCalendar.add(Calendar.DATE, 6);
- String sufDate = dfs.format(innerCalendar.getTime());
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date.getTime());
- // 设置成本月第一天
- calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
- calendar.add(Calendar.DATE, -3);
- String start = DateUtil.utcToDate(calendar.getTime());
- calendar.add(Calendar.DATE, 3);
- // 设置成本月最后一天
- calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
- calendar.add(Calendar.DATE, 3);
- String end = dfs.format(calendar.getTime());
- date.add(Calendar.DATE, 1);
- String fq = "event_date:[" + preDate + " TO " + sufDate + "]";
- String countFq = "event_date:[" + start + " TO " + end + "]";
- oftenIll(fq, countFq);
- }
- } catch (Exception e) {
- e.printStackTrace();
- envelop.setSuccessFlg(false);
- envelop.setErrorMsg(e.getMessage());
- }
- return envelop;
- }
- /**
- * 频繁就医逻辑
- * @param fq
- * @throws Exception
- */
- public void oftenIll(String fq, String countFq) throws Exception {
- String demographicId = "demographic_id";
- String[] fields = {"rowkey","patient_name","patient_age","patient_sex","demographic_id","EHR_001211","event_date"};
- // 一周内就诊次数大于3的用户
- Map<String, Long> groupMap = solrUtil.groupCountLte(ResourceCore.MasterTable, null, fq, demographicId, 0, -1, 3);
- // 遍历 查询用户信息并存到ES
- for (String key : groupMap.keySet()) {
- Map<String, Object> mapInfo = new HashMap();
- SolrDocumentList query = solrUtil.query(ResourceCore.MasterTable, "demographic_id:\"" + key + "\"", fq, null, 0, 1, fields);
- Date date = DateUtils.addHours((Date) query.get(0).get("event_date"), -8);
- String sex = null != query.get(0).get("patient_sex") ? query.get(0).get("patient_sex") + "" : "";
- String strDate = DateUtil.toString(date, DateUtil.DEFAULT_DATE_YM_FORMAT);
- long count = solrUtil.count(ResourceCore.MasterTable, "demographic_id:\"" + key + "\"", countFq);
- mapInfo.put("_id", key + strDate);
- mapInfo.put("idCardNo", key);
- mapInfo.put("name", null != query.get(0).get("patient_name") ? query.get(0).get("patient_name") : "");
- mapInfo.put("age", null != query.get(0).get("patient_age") ? query.get(0).get("patient_age") : "");
- mapInfo.put("sex", sexExchangeByCode(sex));
- mapInfo.put("address", null != query.get(0).get("EHR_001211") ? query.get(0).get("EHR_001211") : "");
- mapInfo.put("eventDate", DateUtil.formatCharDate(DateUtil.toString((Date) query.get(0).get("event_date"), DateUtil.DATE_WORLD_FORMAT), DateUtil.DATE_WORLD_FORMAT));
- mapInfo.put("createTime", new Date());
- mapInfo.put("result", count);
- // type = 1 频繁就医
- mapInfo.put("type", 1);
- // 保存到ES
- saveToEs(mapInfo);
- }
- }
- /**
- * 性别字典转换
- * @param code
- * @return
- */
- private String sexExchangeByCode(String code) {
- String sex = "";
- switch (code) {
- case "0" : {
- sex = "未知的性别";
- break;
- }
- case "1" : {
- sex = "男性";
- break;
- }
- case "2" : {
- sex = "女性";
- break;
- }
- case "9" : {
- sex = "未说明的性别";
- break;
- }
- default : {
- break;
- }
- }
- return sex;
- }
- /**
- * 存储到es
- *
- * @param medicalInsuranceInfo
- * @throws Exception
- */
- public void saveToEs(Map<String, Object> medicalInsuranceInfo) throws Exception {
- String index = "medical_insurance_index";
- String type = "medical_insurance";
- long start = System.currentTimeMillis();
- log.info("开始保存时间 = {}", medicalInsuranceInfo, start);
- elasticSearchUtil.index(index, type, medicalInsuranceInfo);
- long end = System.currentTimeMillis();
- log.info("用时 = {}毫秒", end - start);
- }
- }
|