123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- package com.yihu.quota.controller;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.yihu.ehr.constants.ApiVersion;
- import com.yihu.ehr.constants.ServiceApi;
- import com.yihu.ehr.solr.SolrUtil;
- import com.yihu.ehr.util.datetime.DateUtil;
- import com.yihu.ehr.util.rest.Envelop;
- import com.yihu.quota.etl.model.EsConfig;
- import com.yihu.quota.model.jpa.TjQuota;
- import com.yihu.quota.model.jpa.save.TjQuotaDataSave;
- import com.yihu.quota.model.jpa.source.TjQuotaDataSource;
- import com.yihu.quota.model.rest.HospitalComposeModel;
- import com.yihu.quota.model.rest.QuotaReport;
- import com.yihu.quota.model.rest.ResultModel;
- import com.yihu.quota.service.orgHealthCategory.OrgHealthCategoryStatisticsService;
- import com.yihu.quota.service.quota.BaseStatistsService;
- import com.yihu.quota.service.quota.QuotaService;
- import com.yihu.quota.service.save.TjDataSaveService;
- import com.yihu.quota.service.source.TjDataSourceService;
- import com.yihu.quota.vo.OrgHealthCategoryShowModel;
- import com.yihu.quota.vo.SaveModel;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import io.swagger.annotations.ApiParam;
- import net.sf.json.JSONObject;
- import org.apache.commons.lang.StringUtils;
- import org.apache.hadoop.hdfs.server.namenode.Quota;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import java.net.URLDecoder;
- import java.text.SimpleDateFormat;
- import java.util.*;
- /**
- *
- * @author janseny
- */
- @RestController
- @RequestMapping(ApiVersion.Version1_0)
- @Api(description = "指标统计 -指标控制入口")
- public class QuotaController extends BaseController {
- @Autowired
- private ObjectMapper objectMapper;
- @Autowired
- private QuotaService quotaService;
- @Autowired
- private TjDataSourceService dataSourceService;
- @Autowired
- private BaseStatistsService baseStatistsService;
- @Autowired
- private TjDataSaveService dataSaveService;
- @Autowired
- private SolrUtil solrUtil;
- private static final Logger log = LoggerFactory.getLogger(QuotaController.class);
- /**
- * 查询结果
- * @param id
- * @return
- */
- @ApiOperation(value = "获取指标执行结果分页")
- @RequestMapping(value = ServiceApi.TJ.TjGetQuotaResult, method = RequestMethod.GET)
- public Envelop getQuotaResult(
- @ApiParam(name = "id", value = "指标任务ID", required = true)
- @RequestParam(value = "id" , required = true) int id,
- @ApiParam(name = "filters", value = "检索条件", defaultValue = "")
- @RequestParam(value = "filters", required = false) String filters,
- @ApiParam(name = "pageNo", value = "页码", defaultValue = "0")
- @RequestParam(value = "pageNo" , required = false ,defaultValue = "0") int pageNo,
- @ApiParam(name = "pageSize", value = "分页大小", defaultValue = "15")
- @RequestParam(value = "pageSize" , required = false ,defaultValue ="15") int pageSize
- ) {
- Envelop envelop = new Envelop();
- try {
- if(filters!=null){
- filters = URLDecoder.decode(filters, "UTF-8");
- }
- System.out.println(filters);
- List<SaveModel> saveModelList = new ArrayList<SaveModel>();
- List<Map<String, Object>> resultList = quotaService.queryResultPage(id, filters, pageNo, pageSize);
- if(resultList != null && resultList.size() > 0){
- for(Map<String, Object> map : resultList){
- SaveModel saveModel = objectMapper.convertValue(map, SaveModel.class);
- if(saveModel != null){
- saveModelList.add(saveModel);
- }
- }
- }
- long totalCount = quotaService.getQuotaTotalCount(id,filters);
- envelop.setSuccessFlg(true);
- envelop.setDetailModelList(saveModelList);
- envelop.setCurrPage(pageNo);
- envelop.setPageSize(pageSize);
- envelop.setTotalCount((int) totalCount);
- return envelop;
- } catch (Exception e) {
- error(e);
- invalidUserException(e, -1, "查询失败:" + e.getMessage());
- }
- envelop.setSuccessFlg(false);
- return envelop;
- }
- /**
- * 获取指标统计不同维度结果总量
- * @param id
- * @return
- */
- @ApiOperation(value = "获取指标统计不同维度结果数据")
- @RequestMapping(value = ServiceApi.TJ.GetQuotaTotalCount, method = RequestMethod.GET)
- public Envelop getQuotaTotalCount(
- @ApiParam(name = "id", value = "指标任务ID", required = true)
- @RequestParam(value = "id" , required = true) int id,
- @ApiParam(name = "filters", value = "检索条件", defaultValue = "")
- @RequestParam(value = "filters", required = false) String filters,
- @ApiParam(name = "dimension", value = "需要统计不同维度字段多个维度用;隔开", defaultValue = "quotaDate")
- @RequestParam(value = "dimension", required = false) String dimension,
- @ApiParam(name = "top", value = "获取前几条数据")
- @RequestParam(value = "top", required = false) String top
- ) {
- Envelop envelop = new Envelop();
- TjQuota tjQuota = quotaService.findOne(id);
- String code = tjQuota.getCode();
- String dateType = null;
- try {
- if(filters!=null){
- filters = URLDecoder.decode(filters, "UTF-8");
- if(filters.equals("{}")){
- filters = null;
- }
- }
- TjQuotaDataSource quotaDataSource = dataSourceService.findSourceByQuotaCode(code);
- JSONObject obj = new JSONObject().fromObject(quotaDataSource.getConfigJson());
- EsConfig esConfig= (EsConfig) JSONObject.toBean(obj,EsConfig.class);
- List<Map<String, Object>> resultList = new ArrayList<>();
- String configFilter = esConfig.getFilter();
- if(StringUtils.isNotEmpty(configFilter) && quotaDataSource.getSourceCode().equals("1")){//数据源为ES库
- TjQuotaDataSave quotaDataSave = dataSaveService.findByQuota(code);
- if(quotaDataSave != null && StringUtils.isNotEmpty(quotaDataSave.getConfigJson())){
- JSONObject objSave = new JSONObject().fromObject(quotaDataSave.getConfigJson());
- EsConfig esConfigSave = (EsConfig) JSONObject.toBean(objSave,EsConfig.class);
- if(StringUtils.isEmpty(esConfig.getIndex()) || esConfig.getIndex().equals(esConfigSave.getIndex()) ){
- if(StringUtils.isNotEmpty(filters)){
- filters += " and " + configFilter;
- }else {
- filters = configFilter;
- }
- }
- }else {
- if(StringUtils.isNotEmpty(filters)){
- filters += " and " + configFilter;
- }else {
- filters = configFilter;
- }
- }
- }
- // 判断该指标是否需要同比, 需要的话拼接时间条件
- if (StringUtils.isNotEmpty(esConfig.getIncrementFlag())) {
- filters = baseStatistsService.filtersExchangeHandle(filters, esConfig);
- log.info("filters = {}", filters);
- }
- String molecularFilter = filters;
- String denominatorFilter = filters;
- if (StringUtils.isNotEmpty(esConfig.getGrowthFlag())) {
- resultList = baseStatistsService.getGrowthByQuota(dimension, filters, esConfig, dateType, top);
- } else {
- if(tjQuota.getResultGetType().equals("1")){
- //普通指标直接查询
- resultList = baseStatistsService.getQuotaResultList(code, dimension, filters, dateType, top);
- }else {
- if( (StringUtils.isNotEmpty(esConfig.getMolecular())) && StringUtils.isNotEmpty(esConfig.getDenominator())) {//除法
- //除法指标查询输出结果
- molecularFilter = baseStatistsService.handleFilter(esConfig.getMolecularFilter(), molecularFilter);
- denominatorFilter = baseStatistsService.handleFilter(esConfig.getDenominatorFilter(), denominatorFilter);
- if (StringUtils.isNotEmpty(esConfig.getDivisionType()) && esConfig.getDivisionType().equals("2")) {
- resultList = baseStatistsService.divisionQuotaDenoConstant(esConfig.getMolecular(), dimension, molecularFilter, denominatorFilter,esConfig.getPercentOperation(), esConfig.getPercentOperationValue(), dateType, top);
- } else {
- resultList = baseStatistsService.divisionQuota(esConfig.getMolecular(), esConfig.getDenominator(), dimension, molecularFilter, denominatorFilter, esConfig.getPercentOperation(), esConfig.getPercentOperationValue(), dateType, top);
- }
- } else if (StringUtils.isNotEmpty(esConfig.getAddOperation())) {
- String firstFilter = baseStatistsService.handleFilter(esConfig.getAddFirstFilter(), filters);
- String secondFilter = baseStatistsService.handleFilter(esConfig.getAddSecondFilter(), filters);
- resultList = baseStatistsService.addQuota(esConfig.getAddFirstQuotaCode(), firstFilter, esConfig.getAddSecondQuotaCode(), secondFilter, esConfig.getAddOperation(), dimension, dateType, top);
- } else {
- if(StringUtils.isNotEmpty(esConfig.getSuperiorBaseQuotaCode())){
- //通过基础指标 抽取查询
- resultList = baseStatistsService.getQuotaResultList(esConfig.getSuperiorBaseQuotaCode(), dimension,filters,dateType, top);
- }
- }
- }
- }
- List<ResultModel> resultModelList = new ArrayList<>();
- String [] dimens = dimension.split(";");
- if(resultList != null && resultList.size() > 0){
- for(Map<String, Object> map :resultList){
- if(map.size() > 0){
- List<String> cloumns = new ArrayList<>();
- ResultModel resultModel = new ResultModel();
- resultModel.setValue(map.get("result"));
- for(int i = 0;i < dimens.length;i++){
- if(map.get(dimens[i]) != null){
- cloumns.add(map.get(dimens[i]).toString());
- }
- }
- resultModel.setCloumns(cloumns);
- resultModelList.add(resultModel);
- }
- }
- }
- envelop.setDetailModelList(resultModelList);
- envelop.setSuccessFlg(true);
- return envelop;
- } catch (Exception e) {
- error(e);
- invalidUserException(e, -1, "查询失败:" + e.getMessage());
- }
- envelop.setSuccessFlg(false);
- return envelop;
- }
- /**
- * 根据指标code获取指标统计结果 swagger- 测试接口
- * @param
- * @return
- */
- @ApiOperation(value = "根据指标code获取指标统计结果")
- @RequestMapping(value = ServiceApi.TJ.TjGetReportQuotaResult, method = RequestMethod.GET)
- public Envelop tjGetReportQuotaResult(
- @ApiParam(name = "code", value = "指标code", required = true)
- @RequestParam(value = "code" , required = true) String code,
- @ApiParam(name = "filters", value = "检索条件 多个条件用 and 拼接 如:town=361002 and org=10000001 ", defaultValue = "")
- @RequestParam(value = "filters", required = false) String filters,
- @ApiParam(name = "dimension", value = "需要统计不同维度字段", defaultValue = "")
- @RequestParam(value = "dimension", required = true) String dimension,
- @ApiParam(name = "top", value = "获取前几条数据")
- @RequestParam(value = "top", required = false) String top
- ) {
- Envelop envelop = new Envelop();
- try {
- if(filters!=null){
- filters = URLDecoder.decode(filters, "UTF-8");
- }
- List<Map<String, Object>> result = baseStatistsService.getSimpleQuotaReport(code,filters,dimension,true, top);
- envelop.setObj(result);
- envelop.setSuccessFlg(true);
- return envelop;
- } catch (Exception e) {
- error(e);
- invalidUserException(e, -1, "查询失败:" + e.getMessage());
- }
- envelop.setSuccessFlg(false);
- return envelop;
- }
- @ApiOperation("根据条件到solr中获取记录数")
- @RequestMapping(value = "/report/searchSolrByParam", method = RequestMethod.POST)
- public Envelop searchSolrByParam(
- @ApiParam(name = "core", value = "solr core名称")
- @RequestParam(value = "core") String core,
- @ApiParam(name = "eventType", value = "就诊类型")
- @RequestParam(value = "eventType") String eventType,
- @ApiParam(name = "time", value = "过滤时间")
- @RequestParam(value = "time", required = false) String time,
- @ApiParam(name = "month", value = "获取几个月数据", defaultValue = "0")
- @RequestParam(value = "month", defaultValue = "0", required = false) Integer month) throws Exception {
- Envelop envelop = new Envelop();
- List<String> xDate = new ArrayList<>(); // x轴坐标
- List<String> yDate = new ArrayList<>(); // x轴坐标对应的值
- Map<String, String> map = new HashMap();
- String q = "";
- SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd'T'23:59:59'Z'");
- if (StringUtils.isEmpty(eventType)) {
- q = "*:*";
- } else {
- q = "event_type:" + eventType;
- }
- if (StringUtils.isNotEmpty(time)) {
- Date dateTime = DateUtil.strToDate(time);
- Calendar calendar = new GregorianCalendar();
- calendar.setTime(dateTime);
- for (int i = 0; i < month; i++) {
- calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
- String startDate = DateUtil.utcToDate(calendar.getTime());
- calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
- String endDate = dfs.format(calendar.getTime());
- StringBuffer sb = new StringBuffer();
- if ("1".equals(eventType)) { // 就诊类型为住院
- sb.append("EHR_005130:[").append(startDate).append(" TO ").append(endDate).append("] OR event_date:[")
- .append(startDate).append(" TO ").append(endDate).append("]");
- } else if ("0".equals(eventType) || "2".equals(eventType)) { // 就诊类型为门急诊、体检
- sb.append("event_date:[").append(startDate).append(" TO ").append(endDate).append("]");
- }
- log.info(q);
- log.info(sb.toString());
- long count = solrUtil.count(core, q, sb.toString());
- xDate.add(startDate.substring(0,7));
- yDate.add(count == 0 ? "0" : count + "");
- calendar.add(Calendar.MONTH, -1);
- }
- Collections.reverse(xDate);
- Collections.reverse(yDate);
- envelop.setSuccessFlg(true);
- envelop.setObj(xDate);
- envelop.setDetailModelList(yDate);
- }
- return envelop;
- }
- }
|