Browse Source

区域检验-统计接口2

huangzhiyong 6 năm trước cách đây
mục cha
commit
c3df308077

+ 48 - 0
src/main/java/com/yihu/ehr/svrinspection/commons/service/BaseService.java

@ -0,0 +1,48 @@
package com.yihu.ehr.svrinspection.commons.service;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
/**
 *  公共业务类
 *
 * @author HZY
 * @created 2018/11/16 9:58
 */
public class BaseService {
    /**
     *  降序排序
     * @param list          排序集合
     * @param orderField    排序字段
     */
    public void comparator(List<Map<String, Object>> list , String orderField){
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                return getIntegerValue(o2.get(orderField)).compareTo(getIntegerValue(o1.get(orderField)));
            }
        });
    }
    /**
     * 获取值
     * @param objValue
     * @return
     */
    public Integer getIntegerValue(Object objValue) {
        int value = 0;
        try {
            if (objValue != null) {
                value = Integer.parseInt(objValue.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
}

+ 14 - 10
src/main/java/com/yihu/ehr/svrinspection/controller/AssistanceApplyController.java

@ -5,7 +5,7 @@ import com.yihu.ehr.controller.EnvelopRestEndPoint;
import com.yihu.ehr.svrinspection.commons.exception.ManageException;
import com.yihu.ehr.svrinspection.model.AssistanceApplyModel;
import com.yihu.ehr.svrinspection.service.AssistanceApplyService;
import com.yihu.ehr.svrinspection.service.SolrStatisticsService;
import com.yihu.ehr.svrinspection.service.ExamStatisticsService;
import com.yihu.ehr.util.rest.Envelop;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -39,7 +39,7 @@ public class AssistanceApplyController extends EnvelopRestEndPoint {
    @Autowired
    private AssistanceApplyService assistanceApplyService;
    @Autowired
    private SolrStatisticsService solrStatisticsService;
    private ExamStatisticsService examStatisticsService;
    @GetMapping(value = "/area")
    @ApiOperation(value = "区域检验-【区域排行】")
@ -49,7 +49,7 @@ public class AssistanceApplyController extends EnvelopRestEndPoint {
            @ApiParam(name = "eventDate", value = "就诊时间(年月)",required = true, defaultValue = "")
            @RequestParam(value = "eventDate", required = false) String eventDate) throws Exception {
        List<Map<String,Object>> list = solrStatisticsService.areaGroupCount(pid,"org_area",eventDate);
        List<Map<String,Object>> list = examStatisticsService.areaGroupCount(pid,"org_area",eventDate);
        return success(list);
    }
@ -63,23 +63,27 @@ public class AssistanceApplyController extends EnvelopRestEndPoint {
        List<Map<String,Object>> list = null;
        try {
            list = solrStatisticsService.hospitalGroupCount(areaCode,"org_code",eventDate);
            list = examStatisticsService.hospitalGroupCount(areaCode,"org_code",eventDate);
        } catch (Exception e) {
            failed(e.getMessage());
           return failed(e.getMessage());
        }
        return success(list);
    }
    @GetMapping(value = "/statistics")
    @ApiOperation(value = "区域检验数-【统计信息】",notes = "包含今日新增量,检验次数业务分布【医生端,市政端共用】")
    @ApiOperation(value = "区域检验数-【统计信息】",notes = "包含今日新增量,检验次数业务分布")
    public Envelop statistics(
            @ApiParam(name = "orgCode", value = "机构编码(不传默认全市)", defaultValue = "")
            @RequestParam(value = "orgCode", required = false) String orgCode,
            @ApiParam(name = "areaCode", value = "区域编码(不传默认全市)", defaultValue = "")
            @RequestParam(value = "areaCode", required = false) String areaCode,
            @ApiParam(name = "date", value = "日期", defaultValue = "")
            @RequestParam(value = "date", required = false) String date) throws IOException {
        String demoData = "{\"total\":10000,\"increase\":200,\"applied\":100,\"solution\":100,\"eventPercentage\":{\"clinic\":20,\"resident\":80}}";
        Map<String,Object> map = objectMapper.readValue(demoData,Map.class);
        Map<String,Object> map = null;
        try {
            map = examStatisticsService.areaExamStatistics(areaCode,date);
        } catch (ManageException e) {
            return failed(e.getMessage());
        }
        return success(map);
    }

+ 1 - 0
src/main/java/com/yihu/ehr/svrinspection/dao/AssistanceApplyDao.java

@ -15,4 +15,5 @@ public interface AssistanceApplyDao extends PagingAndSortingRepository<Assistanc
    Page<AssistanceApplyModel> queryAllByApplyOrgAndExamTypeAndExamDeptAndPatientNameLike(String appOrg, String type,String dept,String patientName, Pageable pageable);
    long countAllBySolutionStatusAndCreateTimeBetween(int solutionStatus,String startDate,String endDate );
}

+ 13 - 0
src/main/java/com/yihu/ehr/svrinspection/service/AssistanceApplyService.java

@ -4,6 +4,7 @@ import com.yihu.ehr.query.BaseJpaService;
import com.yihu.ehr.svrinspection.commons.exception.ManageException;
import com.yihu.ehr.svrinspection.dao.AssistanceApplyDao;
import com.yihu.ehr.svrinspection.model.AssistanceApplyModel;
import com.yihu.ehr.util.datetime.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -39,4 +40,16 @@ public class AssistanceApplyService  extends BaseJpaService<AssistanceApplyModel
        assistanceApplyDao.save(applyModel);
        return applyModel;
    }
    /**
     * 根据申请状态-获取今日统计量
     * @param status
     * @return
     */
    public long toDayCount(int status) {
        String toDay = DateUtil.getNowDate(DateUtil.DEFAULT_DATE_YMD_FORMAT);
        long todayCount = assistanceApplyDao.countAllBySolutionStatusAndCreateTimeBetween(status, toDay + " 00:00:00", toDay + " 23:59:59");
        return todayCount;
    }
}

+ 253 - 0
src/main/java/com/yihu/ehr/svrinspection/service/ExamStatisticsService.java

@ -0,0 +1,253 @@
package com.yihu.ehr.svrinspection.service;
import com.yihu.ehr.model.geography.MGeographyDict;
import com.yihu.ehr.model.org.MOrganization;
import com.yihu.ehr.profile.core.ResourceCore;
import com.yihu.ehr.solr.SolrUtil;
import com.yihu.ehr.svrinspection.commons.exception.ManageException;
import com.yihu.ehr.svrinspection.commons.service.BaseService;
import com.yihu.ehr.svrinspection.feign.AddressClient;
import com.yihu.ehr.util.datetime.DateUtil;
import com.yihu.ehr.util.log.LogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
/**
 *  检验数据统计-solr
 * @author HZY
 * @created 2018/11/15 14:01
 */
@Component
public class ExamStatisticsService extends BaseService {
    private static String core = ResourceCore.SubTable;
    @Autowired
    private AddressClient addressClient;
    @Autowired
    private SolrUtil solrUtil;
    @Autowired
    private AssistanceApplyService assistanceApplyService;
    /**
     *  区域检验统计分布
     *
     * @param pid           上级区域编码
     * @param field         分组字段
     * @param eventDate
     * @return
     * @throws Exception
     */
    public List<Map<String,Object>> areaGroupCount(Integer pid, String field, String eventDate) throws Exception {
       List<Map<String,Object>> resultList = new ArrayList<>();
        StringBuilder fq = new StringBuilder();
        Map<String, Long> result = new TreeMap<>();
        String q = "(rowkey:*HDSD00_79* OR rowkey:*HDSD00_77*)";//solr细表中的检查检验
        if (StringUtils.isBlank(eventDate)) {
            LogService.getLogger().error("eventDate is null,check your params");
            throw new Exception("eventDate is null,check your params");
        }
        //事件日期
        fq.append("event_date:[" + eventDate + "T00:00:00Z TO " + eventDate + "T23:59:59Z]");
        //统计查询
        Map<String, Long> facetField = solrUtil.groupCount(core,q, fq.toString(),field, 0, -1);
        //获取区县列表
        List<MGeographyDict> addressList = addressClient.getAddressDictByPid(pid);
        addressList.stream().forEach(address ->{
            Map<String,Object> areaData = areaData = new HashMap<>();
            areaData.put("areaCode",address.getPid());
            areaData.put("areaName",address.getName());
            if (facetField.get(String.valueOf(address.getId())) == null) {
                areaData.put("count",0);
            }else {
                areaData.put("count",facetField.get(String.valueOf(address.getId())));
            }
            resultList.add(areaData);
        });
        comparator(resultList,"count");
        return resultList;
    }
    /**
     * 医院检验统计分布
     *
     * @param areaCode      区域编码
     * @param field         分组字段
     * @param eventDate     事件时间
     * @return
     * @throws Exception
     */
    public List<Map<String,Object>> hospitalGroupCount(String areaCode, String field, String eventDate) throws Exception {
        List<Map<String,Object>> resultList = new ArrayList<>();
        StringBuilder fq = new StringBuilder();
        String q = "(rowkey:*HDSD00_79* OR rowkey:*HDSD00_77*)";//solr细表中的检查检验
        if (StringUtils.isBlank(areaCode)) {
            throw new ManageException("areaCode is null,check your params");
        }
        if (StringUtils.isBlank(eventDate)) {
            throw new ManageException("eventDate is null,check your params");
        }
        //区域编码
        fq.append("org_area:"+areaCode);
        //事件日期
        fq.append(" && event_date:[" + eventDate + "T00:00:00Z TO " + eventDate + "T23:59:59Z]");
        //统计查询
        Map<String, Long> facetField = solrUtil.groupCount(core,q, fq.toString(),field, 0, -1);
        //获取区县列表
        List<MOrganization> addressList = addressClient.getOrganizationByAreaCode(areaCode);
        addressList.stream().forEach(organization ->{
           //只显示有检验数的医院
            if (facetField.get(organization.getOrgCode()) != null) {
                Map<String,Object> areaData = areaData = new HashMap<>();
                areaData.put("orgCode",organization.getOrgCode());
                areaData.put("orgName",organization.getFullName());
                areaData.put("count",facetField.get(organization.getOrgCode()));
                resultList.add(areaData);
            }
        });
        comparator(resultList,"count");
        return resultList;
    }
    /**
     *  区域检验数统计
     *
     * TODO 累计数和新增数先从solr获取,后期考虑是否同步到mysql中
     * @param areaCode
     * @param eventDate
     * @return
     */
    public Map<String,Object> areaExamStatistics(String areaCode, String eventDate) throws Exception {
        Map<String,Object> result = new HashMap<>();
        //累计检验数
        long total = totalCount(areaCode, eventDate);
        //今日新增数
        long today = todayIncreaseCount(areaCode);
        //已申请数
        long apply = assistanceApplyService.toDayCount(0);
        //已处理数
        long solution = assistanceApplyService.toDayCount(1);
        //业务分布
        List<Map<String, Object>> list = examEventRanking(areaCode, eventDate);
        result.put("total",total);
        result.put("increase",today);
        result.put("applied",apply);
        result.put("solution",solution);
        result.put("eventPercentage",solution);
        return result;
    }
    /**
     *  solr 获取检验累计总数
     *
     * @param areaCode      区域编码
     * @param eventDate     时间
     * @return
     * @throws ManageException
     */
    public long totalCount(String areaCode, String eventDate) throws ManageException {
        long total = 0;
        StringBuilder fq = new StringBuilder();
        String q = "(rowkey:*HDSD00_79* OR rowkey:*HDSD00_77*)";//solr细表中的检查检验
        if (!StringUtils.isBlank(areaCode)) {
            //区域编码
            fq.append("org_area:"+areaCode);
        }
        if (StringUtils.isBlank(eventDate)) {
            throw new ManageException("eventDate is null,check your params");
        }
        //事件日期
        fq.append(" && event_date:[" + eventDate + "T00:00:00Z TO " + eventDate + "T23:59:59Z]");
        //统计查询
        try {
             total = solrUtil.count(core,q, fq.toString());
        } catch (Exception e) {
            throw new ManageException("获取累计统计数异常",e);
        }
        return total;
    }
    /**
     *  今日新增检验数
     *
     * @param areaCode  区域编码
     * @return
     * @throws ManageException
     */
    public long todayIncreaseCount(String areaCode) throws ManageException {
        long total = 0;
        StringBuilder fq = new StringBuilder();
        String q = "(rowkey:*HDSD00_79* OR rowkey:*HDSD00_77*)";//solr细表中的检查检验
        if (!StringUtils.isBlank(areaCode)) {
            //区域编码
            fq.append("org_area:"+areaCode);
        }
        String createDate = DateUtil.getNowDate(DateUtil.DEFAULT_DATE_YMD_FORMAT);
        //创建日期
        fq.append(" && create_date:[" + createDate + "T00:00:00Z TO " + createDate + "T23:59:59Z]");
        //统计查询
        try {
            total = solrUtil.count(core,q, fq.toString());
        } catch (Exception e) {
            throw new ManageException("获取今日新增档案数异常",e);
        }
        return total;
    }
    /**
     * 检验次数业务分布
     *
     * @param areaCode      区域编码
     * @param eventDate     时间
     * @return
     * @throws Exception
     */
    public List<Map<String,Object>> examEventRanking(String areaCode, String eventDate) throws Exception {
        List<Map<String,Object>> resultList = new ArrayList<>();
        StringBuilder fq = new StringBuilder();
        String q = "(rowkey:*HDSD00_79* OR rowkey:*HDSD00_77*)";//solr细表中的检查检验
        if (StringUtils.isBlank(areaCode)) {
            throw new ManageException("areaCode is null,check your params");
        }
        if (StringUtils.isBlank(eventDate)) {
            throw new ManageException("eventDate is null,check your params");
        }
        //区域编码
        fq.append("org_area:"+areaCode);
        //事件日期
        fq.append(" && event_date:[" + eventDate + "T00:00:00Z TO " + eventDate + "T23:59:59Z]");
        //统计查询
        Map<String, Long> facetField = solrUtil.groupCount(core,q, fq.toString(),"event_type", 0, -1);
        comparator(resultList,"count");
        return resultList;
    }
}

+ 0 - 188
src/main/java/com/yihu/ehr/svrinspection/service/SolrStatisticsService.java

@ -1,188 +0,0 @@
package com.yihu.ehr.svrinspection.service;
import com.yihu.ehr.model.geography.MGeographyDict;
import com.yihu.ehr.model.org.MOrganization;
import com.yihu.ehr.profile.core.ResourceCore;
import com.yihu.ehr.solr.SolrUtil;
import com.yihu.ehr.svrinspection.commons.exception.ManageException;
import com.yihu.ehr.svrinspection.feign.AddressClient;
import com.yihu.ehr.util.log.LogService;
import org.apache.commons.lang.StringUtils;
import org.apache.solr.client.solrj.response.FacetField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
/**
 *  检验数据统计-solr
 * @author HZY
 * @created 2018/11/15 14:01
 */
@Component
public class SolrStatisticsService {
    private static String core = ResourceCore.SubTable;
    @Autowired
    private AddressClient addressClient;
    @Autowired
    private SolrUtil solrUtil;
    /**
     *  区域检验统计分布
     *
     * @param pid           上级区域编码
     * @param field         分组字段
     * @param eventDate
     * @return
     * @throws Exception
     */
    public List<Map<String,Object>> areaGroupCount(Integer pid, String field, String eventDate) throws Exception {
       List<Map<String,Object>> resultList = new ArrayList<>();
        StringBuilder fq = new StringBuilder();
        Map<String, Long> result = new TreeMap<>();
        String q = "(rowkey:*HDSD00_79* OR rowkey:*HDSD00_77*)";//solr细表中的检查检验
        if (StringUtils.isBlank(eventDate)) {
            LogService.getLogger().error("eventDate is null,check your params");
            throw new Exception("eventDate is null,check your params");
        }
        //事件日期
        fq.append("event_date:[" + eventDate + "T00:00:00Z TO " + eventDate + "T23:59:59Z]");
        //统计查询
        Map<String, Long> facetField = solrUtil.groupCount(core,q, fq.toString(),field, 0, -1);
        //获取区县列表
        List<MGeographyDict> addressList = addressClient.getAddressDictByPid(pid);
        addressList.stream().forEach(address ->{
            Map<String,Object> areaData = areaData = new HashMap<>();
            areaData.put("areaCode",address.getPid());
            areaData.put("areaName",address.getName());
            if (facetField.get(String.valueOf(address.getId())) == null) {
                areaData.put("count",0);
            }else {
                areaData.put("count",facetField.get(String.valueOf(address.getId())));
            }
            resultList.add(areaData);
        });
        comparator(resultList,"count");
        return resultList;
    }
    public List<Map<String,Object>> hospitalGroupCount(String areaCode, String field, String eventDate) throws Exception {
        List<Map<String,Object>> resultList = new ArrayList<>();
        StringBuilder fq = new StringBuilder();
        String q = "(rowkey:*HDSD00_79* OR rowkey:*HDSD00_77*)";//solr细表中的检查检验
        if (StringUtils.isBlank(areaCode)) {
            throw new ManageException("areaCode is null,check your params");
        }
        if (StringUtils.isBlank(eventDate)) {
            throw new ManageException("eventDate is null,check your params");
        }
        //区域编码
        fq.append("org_area:"+areaCode);
        //事件日期
        fq.append(" && event_date:[" + eventDate + "T00:00:00Z TO " + eventDate + "T23:59:59Z]");
        //统计查询
        Map<String, Long> facetField = solrUtil.groupCount(core,q, fq.toString(),field, 0, -1);
        //获取区县列表
        List<MOrganization> addressList = addressClient.getOrganizationByAreaCode(areaCode);
        addressList.stream().forEach(organization ->{
            Map<String,Object> areaData = areaData = new HashMap<>();
            areaData.put("orgCode",organization.getOrgCode());
            areaData.put("orgName",organization.getFullName());
            if (facetField.get(organization.getOrgCode()) == null) {
                areaData.put("count",0);
            }else {
                areaData.put("count",facetField.get(organization.getOrgCode()));
            }
            resultList.add(areaData);
        });
        comparator(resultList,"count");
        return resultList;
    }
    public Map<String, Long> examGroupRanking(String field,  String eventDate,  boolean missing) throws Exception {
        StringBuilder fq = new StringBuilder();
        Map<String, Long> result = new TreeMap<>();
        if (StringUtils.isBlank(eventDate)) {
            LogService.getLogger().error("eventDate is null,check your params");
            throw new Exception("eventDate is null,check your params");
        }
        fq.append("(rowkey:*HDSD00_79* OR rowkey:*HDSD00_77*)");
        //事件日期
        fq.append(" && event_date:[" + eventDate + "T00:00:00Z TO " + eventDate + "T23:59:59Z]");
        //统计查询
        FacetField facetField = solrUtil.getFacetField(core, field, fq.toString(), 1, 0, -1, missing);
        if (facetField != null) {
            List<FacetField.Count> values = facetField.getValues();
            long total = 0;
            //查询结果处理
            for (FacetField.Count value : values) {
                //key为null或空字符串的统一为""
                String key = StringUtils.isBlank(value.getName()) ? "" : value.getName();
                long val = value.getCount();
                if (key.length() == 0 && result.get(key) != null) {
                    val += result.get(key);
                }
                result.put(key, val);
                total += val;
            }
            if (result.containsKey("") && result.get("") < 1) {
                result.remove("");
            }
        }
        return result;
    }
    /**
     *  降序排序
     * @param list          排序集合
     * @param orderField    排序字段
     */
    public void comparator(List<Map<String, Object>> list ,String orderField){
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                return getIntegerValue(o2.get(orderField)).compareTo(getIntegerValue(o1.get(orderField)));
            }
        });
    }
    /**
     * 获取值
     * @param objValue
     * @return
     */
    public Integer getIntegerValue(Object objValue) {
        int value = 0;
        try {
            if (objValue != null)
                value = Integer.parseInt(objValue.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
}