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 groupMap = solrUtil.groupCountLte(ResourceCore.MasterTable, null, fq, demographicId, 0, -1, 3); // 遍历 查询用户信息并存到ES for (String key : groupMap.keySet()) { Map 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 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); } }