Browse Source

修改bug 7246及占比、环比合计的计算

wangxingwang 6 years ago
parent
commit
b216e113a1

+ 213 - 0
src/main/java/com/yihu/quota/scheduler/MedicalInsuranceScheduler.java

@ -0,0 +1,213 @@
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);
    }
}

+ 13 - 70
src/main/java/com/yihu/quota/service/medicalInsurance/MedicalInsuranceService.java

@ -1,19 +1,20 @@
package com.yihu.quota.service.medicalInsurance;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.ehr.elasticsearch.ElasticSearchUtil;
import com.yihu.ehr.hbase.HBaseDao;
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.controller.QuotaReportController;
import com.yihu.quota.service.quota.BaseStatistsService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.solr.client.solrj.response.PivotField;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@ -36,9 +37,9 @@ public class MedicalInsuranceService {
    @Autowired
    private QuotaReportController quotaReportController;
    @Autowired
    private BaseStatistsService baseStatistsService;
    @Autowired
    private HBaseDao hBaseDao;
    @Autowired
    private ElasticSearchUtil elasticSearchUtil;
    /**
@ -160,14 +161,10 @@ public class MedicalInsuranceService {
     */
    public Envelop oftenIll(String time, int size, int page) throws Exception {
        Envelop envelop = new Envelop();
        List<Map<String, Object>> list = new ArrayList<>();
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd'T'23:59:59'Z'");
        // idCardList用来存放最近7天就诊次数大于等于3的用户身份证号码
        HashSet<String> idCardList = new HashSet<>();
        // 时间
        Date dateTime = DateUtil.strToDate(time);
        Calendar calendar = new GregorianCalendar();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        // 设置成本月第一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
@ -175,74 +172,20 @@ public class MedicalInsuranceService {
        // 设置成本月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String endDate = dfs.format(calendar.getTime());
        String demographicId = "demographic_id";
        String[] fields = {"rowkey","patient_name","patient_age","patient_sex","demographic_id","EHR_001211","event_date"};
        // 设置成本月第一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        // 遍历一个月
        while (belongCalendar(calendar, startDate, endDate)) {
            Calendar innerCalendar = Calendar.getInstance();
            innerCalendar.setTime(calendar.getTime());
            // 获取该月份下每天前后3天的就诊记录, 共7天
            innerCalendar.add(Calendar.DATE, -3);
            String preDate = DateUtil.utcToDate(innerCalendar.getTime());
            innerCalendar.add(Calendar.DATE, 6);
            String sufDate = dfs.format(innerCalendar.getTime());
            String fq = new StringBuffer().append("event_date:[").append(preDate).append(" TO ").append(sufDate).append("]").toString();
            Map<String, Long> groupMap = solrUtil.groupCountLte(ResourceCore.MasterTable, null, fq, demographicId, 0, -1, 3);
            for (String key : groupMap.keySet()) {
                idCardList.add(key);
            }
            calendar.add(Calendar.DATE, 1);
        String filters = "type=1";
        if (!StringUtils.isEmpty(startDate)) {
            filters = "eventDate>=" + startDate;
        }
        // 设置查询的时间段
        calendar.add(Calendar.DATE, 2);
        String end = dfs.format(calendar.getTime());
        calendar.add(Calendar.DATE, -3);
        // 设置成本月第一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.DATE, -3);
        String start = DateUtil.utcToDate(calendar.getTime());
        String fqAll = new StringBuffer().append("event_date:[").append(start).append(" TO ").append(end).append("]").toString();
        Map<String, Long> groupMap = solrUtil.groupCountLte(ResourceCore.MasterTable, null, fqAll, demographicId, 0, -1, 3);
        for (String idCard : idCardList) {
            Map<String, Object> mapInfo = new HashMap();
            SolrDocumentList query = solrUtil.query(ResourceCore.MasterTable, "demographic_id:\"" + idCard + "\"", fqAll, null, 0, 1, fields);
            if (null != query && query.size() > 0) {
                String rowKey = null != query.get(0).get("rowkey") ? query.get(0).get("rowkey") + "" : "";
                String sex = null != query.get(0).get("patient_sex") ? query.get(0).get("patient_sex") + "" : "";
                mapInfo.put("rowkey", rowKey);
                mapInfo.put("demographic_id", idCard);
                mapInfo.put("patient_name", null != query.get(0).get("patient_name") ? query.get(0).get("patient_name") : "");
                mapInfo.put("patient_age", null != query.get(0).get("patient_age") ? query.get(0).get("patient_age") : "");
                mapInfo.put("patient_sex", sexExchangeByCode(sex));
                mapInfo.put("result", null != groupMap.get(idCard) ? groupMap.get(idCard) : 3);
                mapInfo.put("address", null != query.get(0).get("EHR_001211") ? query.get(0).get("EHR_001211") : "");
                list.add(mapInfo);
            }
        if (!StringUtils.isEmpty(endDate)) {
            filters += ";eventDate<=" + endDate;
        }
        // 获取总记录数
        int totalCount = list.size();
        // 根据就诊次数排序
        list = baseStatistsService.sortResultList(list, null);
        list = list.subList((page - 1) * size, ((page - 1) * size + size) > list.size() ? list.size() : (page - 1) * size + size);
        Page<Map<String, Object>> pages = elasticSearchUtil.pageBySort("medical_insurance_index", "medical_insurance", filters, "-result", page, size);
        envelop.setDetailModelList(pages.getContent());
        envelop.setTotalCount((int) pages.getTotalElements());
        envelop.setCurrPage(page);
        envelop.setPageSize(size);
        envelop.setTotalCount(totalCount);
        envelop.setDetailModelList(list);
        // 设置总页码数
        if (envelop.getTotalCount() % envelop.getPageSize() > 0) {
            envelop.setTotalPage(envelop.getTotalCount() / envelop.getPageSize() + 1);
        } else {
            envelop.setTotalPage(envelop.getTotalCount() / envelop.getPageSize());
        }
        envelop.setSuccessFlg(true);
        return envelop;
    }