QuotaController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package com.yihu.quota.controller;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.yihu.ehr.constants.ApiVersion;
  4. import com.yihu.ehr.constants.ServiceApi;
  5. import com.yihu.ehr.solr.SolrUtil;
  6. import com.yihu.ehr.util.datetime.DateUtil;
  7. import com.yihu.ehr.util.rest.Envelop;
  8. import com.yihu.quota.etl.model.EsConfig;
  9. import com.yihu.quota.model.jpa.TjQuota;
  10. import com.yihu.quota.model.jpa.save.TjQuotaDataSave;
  11. import com.yihu.quota.model.jpa.source.TjQuotaDataSource;
  12. import com.yihu.quota.model.rest.HospitalComposeModel;
  13. import com.yihu.quota.model.rest.QuotaReport;
  14. import com.yihu.quota.model.rest.ResultModel;
  15. import com.yihu.quota.service.orgHealthCategory.OrgHealthCategoryStatisticsService;
  16. import com.yihu.quota.service.quota.BaseStatistsService;
  17. import com.yihu.quota.service.quota.QuotaService;
  18. import com.yihu.quota.service.save.TjDataSaveService;
  19. import com.yihu.quota.service.source.TjDataSourceService;
  20. import com.yihu.quota.vo.OrgHealthCategoryShowModel;
  21. import com.yihu.quota.vo.SaveModel;
  22. import io.swagger.annotations.Api;
  23. import io.swagger.annotations.ApiOperation;
  24. import io.swagger.annotations.ApiParam;
  25. import net.sf.json.JSONObject;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.apache.hadoop.hdfs.server.namenode.Quota;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.web.bind.annotation.*;
  32. import java.net.URLDecoder;
  33. import java.text.SimpleDateFormat;
  34. import java.util.*;
  35. /**
  36. *
  37. * @author janseny
  38. */
  39. @RestController
  40. @RequestMapping(ApiVersion.Version1_0)
  41. @Api(description = "指标统计 -指标控制入口")
  42. public class QuotaController extends BaseController {
  43. @Autowired
  44. private ObjectMapper objectMapper;
  45. @Autowired
  46. private QuotaService quotaService;
  47. @Autowired
  48. private TjDataSourceService dataSourceService;
  49. @Autowired
  50. private BaseStatistsService baseStatistsService;
  51. @Autowired
  52. private TjDataSaveService dataSaveService;
  53. @Autowired
  54. private SolrUtil solrUtil;
  55. private static final Logger log = LoggerFactory.getLogger(QuotaController.class);
  56. /**
  57. * 查询结果
  58. * @param id
  59. * @return
  60. */
  61. @ApiOperation(value = "获取指标执行结果分页")
  62. @RequestMapping(value = ServiceApi.TJ.TjGetQuotaResult, method = RequestMethod.GET)
  63. public Envelop getQuotaResult(
  64. @ApiParam(name = "id", value = "指标任务ID", required = true)
  65. @RequestParam(value = "id" , required = true) int id,
  66. @ApiParam(name = "filters", value = "检索条件", defaultValue = "")
  67. @RequestParam(value = "filters", required = false) String filters,
  68. @ApiParam(name = "pageNo", value = "页码", defaultValue = "0")
  69. @RequestParam(value = "pageNo" , required = false ,defaultValue = "0") int pageNo,
  70. @ApiParam(name = "pageSize", value = "分页大小", defaultValue = "15")
  71. @RequestParam(value = "pageSize" , required = false ,defaultValue ="15") int pageSize
  72. ) {
  73. Envelop envelop = new Envelop();
  74. try {
  75. if(filters!=null){
  76. filters = URLDecoder.decode(filters, "UTF-8");
  77. }
  78. System.out.println(filters);
  79. List<SaveModel> saveModelList = new ArrayList<SaveModel>();
  80. List<Map<String, Object>> resultList = quotaService.queryResultPage(id, filters, pageNo, pageSize);
  81. if(resultList != null && resultList.size() > 0){
  82. for(Map<String, Object> map : resultList){
  83. SaveModel saveModel = objectMapper.convertValue(map, SaveModel.class);
  84. if(saveModel != null){
  85. saveModelList.add(saveModel);
  86. }
  87. }
  88. }
  89. long totalCount = quotaService.getQuotaTotalCount(id,filters);
  90. envelop.setSuccessFlg(true);
  91. envelop.setDetailModelList(saveModelList);
  92. envelop.setCurrPage(pageNo);
  93. envelop.setPageSize(pageSize);
  94. envelop.setTotalCount((int) totalCount);
  95. return envelop;
  96. } catch (Exception e) {
  97. error(e);
  98. invalidUserException(e, -1, "查询失败:" + e.getMessage());
  99. }
  100. envelop.setSuccessFlg(false);
  101. return envelop;
  102. }
  103. /**
  104. * 获取指标统计不同维度结果总量
  105. * @param id
  106. * @return
  107. */
  108. @ApiOperation(value = "获取指标统计不同维度结果数据")
  109. @RequestMapping(value = ServiceApi.TJ.GetQuotaTotalCount, method = RequestMethod.GET)
  110. public Envelop getQuotaTotalCount(
  111. @ApiParam(name = "id", value = "指标任务ID", required = true)
  112. @RequestParam(value = "id" , required = true) int id,
  113. @ApiParam(name = "filters", value = "检索条件", defaultValue = "")
  114. @RequestParam(value = "filters", required = false) String filters,
  115. @ApiParam(name = "dimension", value = "需要统计不同维度字段多个维度用;隔开", defaultValue = "quotaDate")
  116. @RequestParam(value = "dimension", required = false) String dimension,
  117. @ApiParam(name = "top", value = "获取前几条数据")
  118. @RequestParam(value = "top", required = false) String top
  119. ) {
  120. Envelop envelop = new Envelop();
  121. TjQuota tjQuota = quotaService.findOne(id);
  122. String code = tjQuota.getCode();
  123. String dateType = null;
  124. try {
  125. if(filters!=null){
  126. filters = URLDecoder.decode(filters, "UTF-8");
  127. if(filters.equals("{}")){
  128. filters = null;
  129. }
  130. }
  131. TjQuotaDataSource quotaDataSource = dataSourceService.findSourceByQuotaCode(code);
  132. JSONObject obj = new JSONObject().fromObject(quotaDataSource.getConfigJson());
  133. EsConfig esConfig= (EsConfig) JSONObject.toBean(obj,EsConfig.class);
  134. List<Map<String, Object>> resultList = new ArrayList<>();
  135. String configFilter = esConfig.getFilter();
  136. if(StringUtils.isNotEmpty(configFilter) && quotaDataSource.getSourceCode().equals("1")){//数据源为ES库
  137. TjQuotaDataSave quotaDataSave = dataSaveService.findByQuota(code);
  138. if(quotaDataSave != null && StringUtils.isNotEmpty(quotaDataSave.getConfigJson())){
  139. JSONObject objSave = new JSONObject().fromObject(quotaDataSave.getConfigJson());
  140. EsConfig esConfigSave = (EsConfig) JSONObject.toBean(objSave,EsConfig.class);
  141. if(StringUtils.isEmpty(esConfig.getIndex()) || esConfig.getIndex().equals(esConfigSave.getIndex()) ){
  142. if(StringUtils.isNotEmpty(filters)){
  143. filters += " and " + configFilter;
  144. }else {
  145. filters = configFilter;
  146. }
  147. }
  148. }else {
  149. if(StringUtils.isNotEmpty(filters)){
  150. filters += " and " + configFilter;
  151. }else {
  152. filters = configFilter;
  153. }
  154. }
  155. }
  156. // 判断该指标是否需要同比, 需要的话拼接时间条件
  157. if (StringUtils.isNotEmpty(esConfig.getIncrementFlag())) {
  158. filters = baseStatistsService.filtersExchangeHandle(filters, esConfig);
  159. log.info("filters = {}", filters);
  160. }
  161. String molecularFilter = filters;
  162. String denominatorFilter = filters;
  163. if (StringUtils.isNotEmpty(esConfig.getGrowthFlag())) {
  164. resultList = baseStatistsService.getGrowthByQuota(dimension, filters, esConfig, dateType, top);
  165. } else {
  166. if(tjQuota.getResultGetType().equals("1")){
  167. //普通指标直接查询
  168. resultList = baseStatistsService.getQuotaResultList(code, dimension, filters, dateType, top);
  169. }else {
  170. if( (StringUtils.isNotEmpty(esConfig.getMolecular())) && StringUtils.isNotEmpty(esConfig.getDenominator())) {//除法
  171. //除法指标查询输出结果
  172. molecularFilter = baseStatistsService.handleFilter(esConfig.getMolecularFilter(), molecularFilter);
  173. denominatorFilter = baseStatistsService.handleFilter(esConfig.getDenominatorFilter(), denominatorFilter);
  174. if (StringUtils.isNotEmpty(esConfig.getDivisionType()) && esConfig.getDivisionType().equals("2")) {
  175. resultList = baseStatistsService.divisionQuotaDenoConstant(esConfig.getMolecular(), dimension, molecularFilter, denominatorFilter,esConfig.getPercentOperation(), esConfig.getPercentOperationValue(), dateType, top);
  176. } else {
  177. resultList = baseStatistsService.divisionQuota(esConfig.getMolecular(), esConfig.getDenominator(), dimension, molecularFilter, denominatorFilter, esConfig.getPercentOperation(), esConfig.getPercentOperationValue(), dateType, top);
  178. }
  179. } else if (StringUtils.isNotEmpty(esConfig.getAddOperation())) {
  180. String firstFilter = baseStatistsService.handleFilter(esConfig.getAddFirstFilter(), filters);
  181. String secondFilter = baseStatistsService.handleFilter(esConfig.getAddSecondFilter(), filters);
  182. resultList = baseStatistsService.addQuota(esConfig.getAddFirstQuotaCode(), firstFilter, esConfig.getAddSecondQuotaCode(), secondFilter, esConfig.getAddOperation(), dimension, dateType, top);
  183. } else {
  184. if(StringUtils.isNotEmpty(esConfig.getSuperiorBaseQuotaCode())){
  185. //通过基础指标 抽取查询
  186. resultList = baseStatistsService.getQuotaResultList(esConfig.getSuperiorBaseQuotaCode(), dimension,filters,dateType, top);
  187. }
  188. }
  189. }
  190. }
  191. List<ResultModel> resultModelList = new ArrayList<>();
  192. String [] dimens = dimension.split(";");
  193. if(resultList != null && resultList.size() > 0){
  194. for(Map<String, Object> map :resultList){
  195. if(map.size() > 0){
  196. List<String> cloumns = new ArrayList<>();
  197. ResultModel resultModel = new ResultModel();
  198. resultModel.setValue(map.get("result"));
  199. for(int i = 0;i < dimens.length;i++){
  200. if(map.get(dimens[i]) != null){
  201. cloumns.add(map.get(dimens[i]).toString());
  202. }
  203. }
  204. resultModel.setCloumns(cloumns);
  205. resultModelList.add(resultModel);
  206. }
  207. }
  208. }
  209. envelop.setDetailModelList(resultModelList);
  210. envelop.setSuccessFlg(true);
  211. return envelop;
  212. } catch (Exception e) {
  213. error(e);
  214. invalidUserException(e, -1, "查询失败:" + e.getMessage());
  215. }
  216. envelop.setSuccessFlg(false);
  217. return envelop;
  218. }
  219. /**
  220. * 根据指标code获取指标统计结果 swagger- 测试接口
  221. * @param
  222. * @return
  223. */
  224. @ApiOperation(value = "根据指标code获取指标统计结果")
  225. @RequestMapping(value = ServiceApi.TJ.TjGetReportQuotaResult, method = RequestMethod.GET)
  226. public Envelop tjGetReportQuotaResult(
  227. @ApiParam(name = "code", value = "指标code", required = true)
  228. @RequestParam(value = "code" , required = true) String code,
  229. @ApiParam(name = "filters", value = "检索条件 多个条件用 and 拼接 如:town=361002 and org=10000001 ", defaultValue = "")
  230. @RequestParam(value = "filters", required = false) String filters,
  231. @ApiParam(name = "dimension", value = "需要统计不同维度字段", defaultValue = "")
  232. @RequestParam(value = "dimension", required = true) String dimension,
  233. @ApiParam(name = "top", value = "获取前几条数据")
  234. @RequestParam(value = "top", required = false) String top
  235. ) {
  236. Envelop envelop = new Envelop();
  237. try {
  238. if(filters!=null){
  239. filters = URLDecoder.decode(filters, "UTF-8");
  240. }
  241. List<Map<String, Object>> result = baseStatistsService.getSimpleQuotaReport(code,filters,dimension,true, top);
  242. envelop.setObj(result);
  243. envelop.setSuccessFlg(true);
  244. return envelop;
  245. } catch (Exception e) {
  246. error(e);
  247. invalidUserException(e, -1, "查询失败:" + e.getMessage());
  248. }
  249. envelop.setSuccessFlg(false);
  250. return envelop;
  251. }
  252. @ApiOperation("根据条件到solr中获取记录数")
  253. @RequestMapping(value = "/report/searchSolrByParam", method = RequestMethod.POST)
  254. public Envelop searchSolrByParam(
  255. @ApiParam(name = "core", value = "solr core名称")
  256. @RequestParam(value = "core") String core,
  257. @ApiParam(name = "eventType", value = "就诊类型")
  258. @RequestParam(value = "eventType") String eventType,
  259. @ApiParam(name = "time", value = "过滤时间")
  260. @RequestParam(value = "time", required = false) String time,
  261. @ApiParam(name = "month", value = "获取几个月数据", defaultValue = "0")
  262. @RequestParam(value = "month", defaultValue = "0", required = false) Integer month) throws Exception {
  263. Envelop envelop = new Envelop();
  264. List<String> xDate = new ArrayList<>(); // x轴坐标
  265. List<String> yDate = new ArrayList<>(); // x轴坐标对应的值
  266. Map<String, String> map = new HashMap();
  267. String q = "";
  268. SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd'T'23:59:59'Z'");
  269. if (StringUtils.isEmpty(eventType)) {
  270. q = "*:*";
  271. } else {
  272. q = "event_type:" + eventType;
  273. }
  274. if (StringUtils.isNotEmpty(time)) {
  275. Date dateTime = DateUtil.strToDate(time);
  276. Calendar calendar = new GregorianCalendar();
  277. calendar.setTime(dateTime);
  278. for (int i = 0; i < month; i++) {
  279. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
  280. String startDate = DateUtil.utcToDate(calendar.getTime());
  281. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
  282. String endDate = dfs.format(calendar.getTime());
  283. StringBuffer sb = new StringBuffer();
  284. if ("1".equals(eventType)) { // 就诊类型为住院
  285. sb.append("EHR_005130:[").append(startDate).append(" TO ").append(endDate).append("] OR event_date:[")
  286. .append(startDate).append(" TO ").append(endDate).append("]");
  287. } else if ("0".equals(eventType) || "2".equals(eventType)) { // 就诊类型为门急诊、体检
  288. sb.append("event_date:[").append(startDate).append(" TO ").append(endDate).append("]");
  289. }
  290. log.info(q);
  291. log.info(sb.toString());
  292. long count = solrUtil.count(core, q, sb.toString());
  293. xDate.add(startDate.substring(0,7));
  294. yDate.add(count == 0 ? "0" : count + "");
  295. calendar.add(Calendar.MONTH, -1);
  296. }
  297. Collections.reverse(xDate);
  298. Collections.reverse(yDate);
  299. envelop.setSuccessFlg(true);
  300. envelop.setObj(xDate);
  301. envelop.setDetailModelList(yDate);
  302. }
  303. return envelop;
  304. }
  305. }