BaseStatistsService.java 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  1. package com.yihu.quota.service.quota;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.yihu.ehr.util.datetime.DateUtil;
  4. import com.yihu.ehr.util.rest.Envelop;
  5. import com.yihu.quota.dao.jpa.TjQuotaDao;
  6. import com.yihu.quota.dao.jpa.TjQuotaGovProvisionDao;
  7. import com.yihu.quota.etl.extract.es.EsResultExtract;
  8. import com.yihu.quota.etl.model.EsConfig;
  9. import com.yihu.quota.etl.util.ElasticsearchUtil;
  10. import com.yihu.quota.model.jpa.TjQuota;
  11. import com.yihu.quota.model.jpa.dimension.TjQuotaDimensionMain;
  12. import com.yihu.quota.model.jpa.dimension.TjQuotaDimensionSlave;
  13. import com.yihu.quota.model.jpa.save.TjQuotaDataSave;
  14. import com.yihu.quota.model.jpa.source.TjQuotaDataSource;
  15. import com.yihu.quota.service.dimension.TjDimensionMainService;
  16. import com.yihu.quota.service.dimension.TjDimensionSlaveService;
  17. import com.yihu.quota.service.orgHealthCategory.OrgHealthCategoryStatisticsService;
  18. import com.yihu.quota.service.save.TjDataSaveService;
  19. import com.yihu.quota.service.singledisease.SingleDiseaseService;
  20. import com.yihu.quota.service.source.TjDataSourceService;
  21. import com.yihu.quota.util.BasesicUtil;
  22. import com.yihu.quota.vo.DictModel;
  23. import com.yihu.quota.vo.SaveModel;
  24. import net.sf.json.JSONObject;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  30. import org.springframework.jdbc.core.JdbcTemplate;
  31. import org.springframework.stereotype.Service;
  32. import java.math.BigDecimal;
  33. import java.text.SimpleDateFormat;
  34. import java.util.*;
  35. /**
  36. * Created by janseny on 2018/01/15.
  37. */
  38. @Service
  39. public class BaseStatistsService {
  40. Logger log = LoggerFactory.getLogger(BaseStatistsService.class);
  41. @Autowired
  42. private TjQuotaDao quotaDao;
  43. @Autowired
  44. private EsResultExtract esResultExtract;
  45. @Autowired
  46. private TjDimensionMainService tjDimensionMainService;
  47. @Autowired
  48. private TjDimensionSlaveService tjDimensionSlaveService;
  49. @Autowired
  50. private JdbcTemplate jdbcTemplate;
  51. @Autowired
  52. private OrgHealthCategoryStatisticsService orgHealthCategoryStatisticsService;
  53. @Autowired
  54. private TjDataSourceService dataSourceService;
  55. @Autowired
  56. private ObjectMapper objectMapper;
  57. @Autowired
  58. private QuotaService quotaService;
  59. @Autowired
  60. private ElasticsearchUtil elasticsearchUtil;
  61. @Autowired
  62. private SingleDiseaseService singleDiseaseService;
  63. @Autowired
  64. private TjQuotaGovProvisionDao tjQuotaGovProvisionDao;
  65. @Autowired
  66. private TjDataSaveService dataSaveService;
  67. private BasesicUtil basesicUtil = new BasesicUtil();
  68. private static String orgHealthCategory = "orgHealthCategory";
  69. public static String orgHealthCategoryCode = "orgHealthCategoryCode";
  70. public static String resultField = "result";
  71. public static String quotaDateField = "quotaDate";
  72. public static String firstColumnField = "firstColumn";
  73. /**
  74. * 特殊机构类型 根据 上级基础指标code 获取基础数据集
  75. * @param code
  76. * @param filter
  77. * @param dimension
  78. * @return
  79. * @throws Exception
  80. */
  81. public List<Map<String, Object>> getOrgHealthCategoryQuotaResultList(String code,String dimension,String filter, String top) throws Exception {
  82. List<Map<String, Object>> dimenListResult = getOrgHealthCategoryAggregationResult(code, dimension, filter, top);
  83. return dimenListResult;
  84. }
  85. /**
  86. * 根据指标code 和维度及条件 分组获取指标查询结果集
  87. * @param code
  88. * @param filter
  89. * @param dimension
  90. * @return
  91. * @throws Exception
  92. */
  93. public List<Map<String, Object>> getQuotaResultList(String code,String dimension,String filter,String dateType, String top) throws Exception {
  94. code = getBasicQuota(code);
  95. List<Map<String, Object>> dimenListResult = new ArrayList<>();
  96. if(StringUtils.isNotEmpty(dateType)){
  97. dimenListResult = getTimeAggregationResult(code, dimension, filter, dateType);
  98. }else {
  99. dimenListResult = getAggregationResult(code, dimension, filter, top);
  100. }
  101. return dimenListResult;
  102. }
  103. /**
  104. * 查找指标的最底层指标code
  105. * @param quotaCode
  106. * @return
  107. */
  108. public String getBasicQuota(String quotaCode){
  109. String code = quotaCode;
  110. TjQuota tjQuota = quotaService.findByCode(quotaCode);
  111. if( tjQuota != null && StringUtils.isNotEmpty(tjQuota.getResultGetType())){
  112. if (tjQuota.getResultGetType().equals("2")){//二次统计
  113. TjQuotaDataSource quotaDataSorce = dataSourceService.findSourceByQuotaCode(quotaCode);
  114. if(quotaDataSorce != null){
  115. JSONObject obj = new JSONObject().fromObject(quotaDataSorce.getConfigJson());
  116. EsConfig esConfig= (EsConfig) JSONObject.toBean(obj,EsConfig.class);
  117. if(StringUtils.isNotEmpty(esConfig.getSuperiorBaseQuotaCode()) && StringUtils.isEmpty(esConfig.getEspecialType()) ){
  118. code = esConfig.getSuperiorBaseQuotaCode();
  119. }
  120. }
  121. }
  122. }
  123. return code;
  124. }
  125. public List<Map<String, Object>> addQuota(String addFirstQuotaCode,String firstFilter, String addSecondQuotaCode, String secondFilter,String operation ,String dimension,String dateType, String top) throws Exception {
  126. List<Map<String, Object>> firstList = getQuotaResultList(addFirstQuotaCode,dimension,firstFilter,dateType, top);
  127. List<Map<String, Object>> secondList = getQuotaResultList(addSecondQuotaCode,dimension,secondFilter,dateType, top);
  128. dimension = StringUtils.isNotEmpty(dateType)? (StringUtils.isNotEmpty(dimension)? dimension +";"+dateType : dateType):dimension;
  129. List<Map<String, Object>> addition = addition(dimension, firstList, secondList, Integer.valueOf(operation));
  130. if (StringUtils.isNotEmpty(top)) {
  131. addition = sortResultList(addition, top);
  132. }
  133. return addition;
  134. }
  135. public List<Map<String, Object>> addition(String dimension, List<Map<String, Object>> firstList, List<Map<String, Object>> secondList,int operation){
  136. List<Map<String, Object>> addResultList = new ArrayList<>();
  137. List<Map<String, Object>> otherList = new ArrayList<>();
  138. otherList = secondList;
  139. String [] moleDimensions = dimension.split(";");
  140. for(Map<String, Object> firstMap :firstList) {
  141. if (null != firstMap && firstMap.size() > 0 ) {
  142. Map<String, Object> map = new HashMap<>();
  143. double firstResultVal = Double.valueOf(firstMap.get("result") == null ? "0" : firstMap.get(resultField).toString());
  144. map.put(firstColumnField, firstMap.get(firstColumnField));
  145. String firstKeyVal = "";
  146. for(int i = 0 ;i < moleDimensions.length ; i++){
  147. if(i == 0){
  148. firstKeyVal = firstMap.get(moleDimensions[i]).toString();
  149. }else {
  150. firstKeyVal = firstKeyVal + "-" + firstMap.get(moleDimensions[i]).toString() ;
  151. }
  152. map.put(moleDimensions[i], firstMap.get(moleDimensions[i]).toString());
  153. if(moleDimensions[i].equals("town") || moleDimensions[i].equals("org") ){
  154. if(firstMap.get(moleDimensions[i] + "Code") != null){
  155. map.put(moleDimensions[i]+ "Code", firstMap.get(moleDimensions[i] + "Code").toString());
  156. }
  157. }
  158. }
  159. boolean pflag = true;
  160. for(Map<String, Object> secondMap :secondList) {
  161. String secondKeyVal = "";
  162. String [] dimeDimensions = dimension.split(";");
  163. for(int i = 0 ;i < dimeDimensions.length ; i++){
  164. if(i == 0){
  165. secondKeyVal = secondMap.get(dimeDimensions[i]).toString();
  166. }else {
  167. secondKeyVal = secondKeyVal + "-" + secondMap.get(dimeDimensions[i]).toString() ;
  168. }
  169. }
  170. if(firstKeyVal.equals(secondKeyVal) || "quotaName".equals(dimension)){ // 如果维度是quotaName,则进入逻辑
  171. double point = 0;
  172. double dimeResultVal = Double.valueOf(secondMap.get(resultField).toString());
  173. BigDecimal first = new BigDecimal(Double.toString(firstResultVal));
  174. BigDecimal second = new BigDecimal(Double.toString(dimeResultVal));
  175. if(operation == 1){ //1 加法 默认
  176. point = first.add(second).doubleValue();
  177. }else if(operation == 2){ //2 减法
  178. point = first.subtract(second).doubleValue();
  179. }
  180. map.put(resultField, basesicUtil.decimalPointHandle(point));
  181. addResultList.add(map);
  182. pflag = false;
  183. otherList.remove(secondMap);
  184. break;
  185. }
  186. }
  187. if(pflag){
  188. map.put(resultField, basesicUtil.decimalPointHandle(firstResultVal));
  189. addResultList.add(map);
  190. }
  191. }
  192. }
  193. if (null != otherList && otherList.size() > 0) {
  194. for(Map<String, Object> secondMap : otherList) {
  195. Map<String, Object> map = new HashMap<>();
  196. map.put(firstColumnField, secondMap.get(firstColumnField));
  197. for(int i = 0 ;i < moleDimensions.length ; i++){
  198. map.put(moleDimensions[i], secondMap.get(moleDimensions[i]).toString());
  199. if((moleDimensions[i].equals("town") || moleDimensions[i].equals("org")) && null != secondMap.get(moleDimensions[i] + "Code")) {
  200. map.put(moleDimensions[i] + "Code", secondMap.get(moleDimensions[i] + "Code").toString());
  201. }
  202. }
  203. double point = 0;
  204. double secondResultVal = Double.valueOf(secondMap.get("result") == null ? "0" : secondMap.get(resultField).toString());
  205. if (secondResultVal != 0) {
  206. if(operation == 1){ //1 加法 默认
  207. point = secondResultVal ;
  208. }else if(operation == 2){ //2 减法
  209. point = -secondResultVal;
  210. }
  211. }
  212. map.put(resultField, basesicUtil.decimalPointHandle(point));
  213. addResultList.add(map);
  214. }
  215. }
  216. //检查后面指标的维度是否全部有 累加进去
  217. /*Map<String, Object> addResuDimenMap = new HashMap<>();
  218. for(int k = 0;k < addResultList.size();k++) {
  219. Map<String, Object> addResultMap = addResultList.get(k);
  220. String addDimenStr = "";
  221. for (int i = 0; i < moleDimensions.length; i++) {
  222. addDimenStr += addResultMap.get(moleDimensions[i]).toString() + "-";
  223. }
  224. addResuDimenMap.put(addDimenStr,addDimenStr);
  225. }
  226. for(Map<String, Object> secondMap :secondList) {
  227. String secondDimenStr = "";
  228. String addDimenStr = "";
  229. for(int i = 0 ;i < moleDimensions.length ; i++){
  230. secondDimenStr += secondMap.get(moleDimensions[i]).toString() + "-";
  231. }
  232. if( !addResuDimenMap.containsKey(secondDimenStr)){
  233. if( !addDimenStr.equals(secondDimenStr)){
  234. Map<String, Object> map = new HashMap<>();
  235. float dimeResultVal = Float.valueOf(secondMap.get(resultField).toString());
  236. map.put(resultField,df.format(dimeResultVal));
  237. map.put(firstColumnField, secondMap.get(firstColumnField));
  238. for(int i = 0 ;i < moleDimensions.length ; i++){
  239. map.put(moleDimensions[i], secondMap.get(moleDimensions[i]).toString());
  240. }
  241. addResultList.add(map);
  242. }
  243. }
  244. }*/
  245. if (moleDimensions.length == 1 && "month".equalsIgnoreCase(moleDimensions[0])) {
  246. addResultList = sortResultListByMonth(addResultList);
  247. }
  248. return addResultList;
  249. }
  250. public List<Map<String, Object>> sortResultList(List<Map<String, Object>> listMap, String top) {
  251. if (null != listMap && listMap.size() > 0) {
  252. Collections.sort(listMap, new Comparator<Map<String, Object>>() {
  253. @Override
  254. public int compare(Map<String, Object> o1, Map<String, Object> o2) {
  255. // 根据result进行降序
  256. double result = Double.parseDouble(o1.get("result") + "");
  257. double result2 = Double.parseDouble(o2.get("result") + "");
  258. double v = result2 - result;
  259. return v > 0 ? 1 : v == 0 ? 0 : -1;
  260. }
  261. });
  262. if (StringUtils.isNotEmpty(top)) {
  263. int t = Integer.parseInt(top);
  264. listMap = listMap.subList(0, listMap.size() > t ? t :listMap.size());
  265. }
  266. }
  267. return listMap;
  268. }
  269. public static List<Map<String, Object>> sortResultListByMonth(List<Map<String, Object>> listMap) {
  270. if (null != listMap && listMap.size() > 0) {
  271. Collections.sort(listMap, new Comparator<Map<String, Object>>() {
  272. @Override
  273. public int compare(Map<String, Object> o1, Map<String, Object> o2) {
  274. // 根据month进行升序
  275. String month1 = o1.get("month") + "";
  276. String month2 = o2.get("month") + "";
  277. int v = month1.compareTo(month2);
  278. return v > 0 ? 1 : v == 0 ? 0 : -1;
  279. }
  280. });
  281. }
  282. return listMap;
  283. }
  284. /**
  285. * 两个维度相同指标除法运算
  286. * @param molecular
  287. * @param denominator
  288. * @param dimension
  289. * @param molecularFilter
  290. * @param denominatorFilters
  291. * @param operation
  292. * @param operationValue
  293. * @return
  294. * @throws Exception
  295. */
  296. public List<Map<String, Object>> divisionQuota(String molecular, String denominator, String dimension,
  297. String molecularFilter,String denominatorFilters,String operation,String operationValue,String dateType, String top) throws Exception {
  298. if(StringUtils.isEmpty(dimension) && StringUtils.isNotEmpty(dateType)){
  299. dimension = dateType;
  300. }
  301. List<Map<String, Object>> moleList = getSimpleQuotaReport(molecular, molecularFilter,dimension ,false , top);
  302. List<Map<String, Object>> denoList = getSimpleQuotaReport(denominator,denominatorFilters,dimension,false, null);
  303. dimension = StringUtils.isNotEmpty(dateType)? (StringUtils.isNotEmpty(dimension)? dimension +";"+dateType : dateType):dimension;
  304. return division(dimension,moleList,denoList,Integer.valueOf(operation),Integer.valueOf(operationValue));
  305. }
  306. /**
  307. * 指标除法运算 分母按年份获取总数
  308. * @param molecular 分子
  309. * @param dimension 分母
  310. * @param molecularFilter 分子条件
  311. * @param denominatorFilter 分母条件
  312. * @param operation
  313. * @param operationValue
  314. * @return
  315. * @throws Exception
  316. */
  317. public List<Map<String, Object>> divisionQuotaDenoConstant(String molecular, String dimension,String molecularFilter,String denominatorFilter,
  318. String operation,String operationValue,String dateType , String top) throws Exception {
  319. Double denominatorVal = 0.0;
  320. List<Map<String, Object>> moleList = getQuotaResultList(molecular,dimension,molecularFilter,dateType, top);
  321. String year = DateUtil.getYearFromYMD(new Date());
  322. String town = "";
  323. if(StringUtils.isNotEmpty(denominatorFilter)){
  324. String [] filter = denominatorFilter.split("and");
  325. if(filter.length > 0 ){
  326. for(String key : filter){
  327. if(key.contains(quotaDateField)){
  328. year = key.substring(key.indexOf("'")+1,key.indexOf("'")+5);//quotaDate >= '2018-03-01'
  329. }
  330. if(key.contains("town")){
  331. town = key.substring(key.indexOf("=")+1);
  332. }
  333. }
  334. }
  335. }
  336. Long sumValue = null;
  337. if(StringUtils.isNotEmpty(town)){
  338. sumValue = tjQuotaGovProvisionDao.getSumByDistrict(Long.parseLong(town),year);
  339. }else {
  340. sumValue = tjQuotaGovProvisionDao.getSumByDistrict(year);
  341. }
  342. // 获取分母的数值
  343. if(sumValue != null && sumValue.doubleValue() != 0){
  344. denominatorVal = sumValue.doubleValue();
  345. return divisionDenoConstant(dimension, moleList, denominatorVal, Integer.valueOf(operation), Integer.valueOf(operationValue));
  346. }else {
  347. return null;
  348. }
  349. }
  350. /**
  351. * 指标结果相除
  352. * 除法运算 分母按年份获取总数
  353. * @param dimension 维度
  354. * @param moleList 分子
  355. * @param denominatorVal 分母 数值
  356. * @param operation 运算方式 1 乘法 2 除法
  357. * @param operationValue 运算参数值
  358. *
  359. */
  360. public List<Map<String, Object>> divisionDenoConstant(String dimension, List<Map<String, Object>> moleList, Double denominatorVal,int operation,int operationValue) {
  361. List<Map<String, Object>> divisionResultList = new ArrayList<>();
  362. for (Map<String, Object> moleMap : moleList) {
  363. Map<String, Object> map = new HashMap<>();
  364. double moleResultVal = Double.valueOf(moleMap.get(resultField).toString());
  365. String moleKeyVal = "";
  366. String [] moleDimensions = dimension.split(";");
  367. for(int i = 0 ;i < moleDimensions.length ; i++){
  368. if(i == 0){
  369. moleKeyVal = moleMap.get(moleDimensions[i]).toString();
  370. }else {
  371. moleKeyVal = moleKeyVal + "-" + moleMap.get(moleDimensions[i]).toString() ;
  372. }
  373. map.put(moleDimensions[i], moleMap.get(moleDimensions[i]).toString());
  374. map.put(moleDimensions[i]+"Name", moleMap.get(moleDimensions[i]).toString());
  375. }
  376. if (moleResultVal == 0) {
  377. map.put(resultField,0);
  378. divisionResultList.add(map);
  379. } else {
  380. double point = 0;
  381. if (operation == 1) {
  382. point = (moleResultVal / denominatorVal) * operationValue;
  383. } else if (operation == 2) {
  384. point = (moleResultVal / denominatorVal) / operationValue;
  385. }
  386. map.put(resultField, basesicUtil.decimalPointHandle(point));
  387. divisionResultList.add(map);
  388. }
  389. }
  390. return divisionResultList;
  391. }
  392. /**
  393. * 指标结果相除
  394. * @param dimension 维度
  395. * @param moleList 分子
  396. * @param denoList 分母
  397. * @param operation 运算方式 1 乘法 2 除法
  398. * @param operationValue 运算参数值
  399. *
  400. */
  401. public List<Map<String, Object>> division(String dimension, List<Map<String, Object>> moleList, List<Map<String, Object>> denoList,int operation,int operationValue){
  402. List<Map<String, Object>> divisionResultList = new ArrayList<>();
  403. for(Map<String, Object> moleMap :moleList) {
  404. if (null != moleMap && moleMap.size() > 0 ) {
  405. Map<String, Object> map = new HashMap<>();
  406. double moleResultVal = Double.valueOf(moleMap.get(resultField) == null ? "0" : moleMap.get(resultField).toString());
  407. String moleKeyVal = "";
  408. String [] moleDimensions = dimension.split(";");
  409. for(int i = 0 ;i < moleDimensions.length ; i++){
  410. if(i == 0){
  411. moleKeyVal = moleMap.get(moleDimensions[i]).toString();
  412. }else {
  413. moleKeyVal = moleKeyVal + "-" + moleMap.get(moleDimensions[i]).toString() ;
  414. }
  415. map.put(firstColumnField, moleMap.get(firstColumnField));
  416. map.put(moleDimensions[i], moleMap.get(moleDimensions[i]).toString());
  417. map.put(moleDimensions[i]+"Name", moleMap.get(moleDimensions[i]).toString());
  418. if("town".equals(moleDimensions[i]) || "org".equals(moleDimensions[i])) {
  419. if(moleMap.get(moleDimensions[i] + "Code") != null) {
  420. map.put(moleDimensions[i]+ "Code", moleMap.get(moleDimensions[i] + "Code").toString());
  421. }
  422. }
  423. }
  424. if (moleResultVal == 0) {
  425. map.put(resultField,0);
  426. divisionResultList.add(map);
  427. } else {
  428. for(Map<String, Object> denoMap :denoList) {
  429. String dimenKeyVal = "";
  430. String [] dimeDimensions = dimension.split(";");
  431. for(int i = 0 ;i < dimeDimensions.length ; i++){
  432. if(i == 0){
  433. dimenKeyVal = denoMap.get(dimeDimensions[i]).toString();
  434. }else {
  435. dimenKeyVal = dimenKeyVal + "-" + denoMap.get(dimeDimensions[i]).toString() ;
  436. }
  437. }
  438. if(moleKeyVal.equals(dimenKeyVal)){
  439. double point = 0;
  440. float dimeResultVal = Float.valueOf(denoMap.get(resultField).toString());
  441. if(dimeResultVal != 0){
  442. if(operation == 1){
  443. point = (moleResultVal/dimeResultVal) * operationValue;
  444. }else if(operation == 2){
  445. point = (moleResultVal/dimeResultVal) / operationValue;
  446. }
  447. }
  448. map.put(resultField, basesicUtil.decimalPointHandle(point));
  449. divisionResultList.add(map);
  450. break;
  451. }
  452. }
  453. }
  454. }
  455. }
  456. Double moleVal = caculateResult(moleList);
  457. Double denoVal = caculateResult(denoList);
  458. if (0 != denoVal) {
  459. Map<String, Object> map = new HashMap<>();
  460. double result = 0;
  461. if(operation == 1) {
  462. result = (moleVal / denoVal) * operationValue;
  463. } else if(operation == 2) {
  464. result = (moleVal / denoVal) / operationValue;
  465. }
  466. map.put("result", basesicUtil.decimalPointHandle(result));
  467. map.put("firstColumn", "合计");
  468. map.put(dimension, "合计");
  469. divisionResultList.add(map);
  470. }
  471. return divisionResultList;
  472. }
  473. /**
  474. * 特殊机构类别 根据条件查询结果
  475. * @param code
  476. * @param filters
  477. * @param dateType 日期类型
  478. * @param isTrunTree 是否转为机构类型树状机构
  479. * @throws Exception
  480. */
  481. public List<Map<String, Object>> getOrgHealthCategory(String code,String filters,String dateType,boolean isTrunTree, String top) throws Exception {
  482. List<Map<String, Object>> dimenListResult = new ArrayList<>();
  483. if(dateType != null && (dateType.contains("year") || dateType.contains("quarter")|| dateType.contains("month") || dateType.contains("day"))){
  484. dimenListResult = getTimeAggregationResult(code,orgHealthCategoryCode,filters,dateType);//dimension 维度为 year,month,day
  485. }else {
  486. dimenListResult = getAggregationResult(code, orgHealthCategoryCode, filters, top);
  487. }
  488. if(isTrunTree){
  489. List<Map<String, Object>> orgHealthCategoryList = orgHealthCategoryStatisticsService.getOrgHealthCategoryTreeByPid(-1);
  490. List<Map<String, Object>> resultList = setResult(code,orgHealthCategoryList,dimenListResult,dateType);
  491. return resultList;
  492. }else {
  493. return dimenListResult;
  494. }
  495. }
  496. /**
  497. * 递归循环 查询机构类型对应的名称和父节点
  498. * @param orgHealthCategoryList
  499. * @param dimenListResult
  500. * @param
  501. * @return
  502. */
  503. public List<Map<String,Object>> setResult(String quotaCode,List<Map<String,Object>> orgHealthCategoryList,List<Map<String, Object>> dimenListResult,String dateType){
  504. List<Map<String,Object>> result = new ArrayList<>();
  505. for(int i=0 ; i < orgHealthCategoryList.size() ; i++ ){
  506. Map<String,Object> mapCategory = orgHealthCategoryList.get(i);
  507. String code = mapCategory.get("code").toString();
  508. boolean notExitFalg = true;
  509. for(Map<String, Object> dimenMap : dimenListResult){
  510. boolean flag = false;
  511. if(dimenMap.get(orgHealthCategoryCode) != null){
  512. flag = dimenMap.get(orgHealthCategoryCode).equals(code);
  513. }
  514. if(dimenMap.get(code) != null || flag ){
  515. // mapCategory.putAll(dimenMap);
  516. if(dimenMap.containsKey(code)){
  517. mapCategory.put(code,dimenMap.get(code));
  518. mapCategory.put(resultField,dimenMap.get(resultField)!=null ? dimenMap.get(resultField):dimenMap.get(code));
  519. }
  520. if(StringUtils.isNotEmpty(dateType)){
  521. mapCategory.put(dimenMap.get(dateType).toString(),dimenMap.get(resultField));
  522. }
  523. mapCategory.put(quotaCode,dimenMap.get(resultField));
  524. break;
  525. }
  526. }
  527. if(notExitFalg){
  528. mapCategory.put(resultField,0);
  529. mapCategory.put(quotaCode,0);
  530. }
  531. mapCategory.put(firstColumnField,mapCategory.get("text"));
  532. result.add(mapCategory);
  533. if(mapCategory.get("children") != null){
  534. List<Map<String,Object>> childrenOrgHealthCategoryList = (List<Map<String, Object>>) mapCategory.get("children");
  535. mapCategory.put("children",setResult(quotaCode,childrenOrgHealthCategoryList,dimenListResult,dateType));
  536. }
  537. }
  538. return result;
  539. }
  540. // /**
  541. // * 递归循环 查询机构类型对应的名称和父节点
  542. // * @param orgHealthCategoryList
  543. // * @param dimenListResult
  544. // * @param
  545. // * @return
  546. // */
  547. // public List<Map<String,Object>> setResultAllDimenMap(String quotaCode,List<Map<String,Object>> orgHealthCategoryList,List<Map<String, Object>> dimenListResult,String dateType){
  548. // List<Map<String,Object>> result = new ArrayList<>();
  549. // for(int i=0 ; i < orgHealthCategoryList.size() ; i++ ){
  550. // Map<String,Object> mapCategory = orgHealthCategoryList.get(i);
  551. // String code = mapCategory.get("code").toString();
  552. // boolean notExitFalg = true;
  553. // for(Map<String, Object> dimenMap : dimenListResult){
  554. // boolean flag = false;
  555. // if(dimenMap.get(orgHealthCategoryCode) != null){
  556. // flag = dimenMap.get(orgHealthCategoryCode).equals(code);
  557. // }
  558. // if(dimenMap.get(code) != null || flag ){
  559. // //补充所有信息
  560. // mapCategory.putAll(dimenMap);
  561. // if(dimenMap.containsKey(code)){
  562. // mapCategory.put(code,dimenMap.get(code));
  563. // mapCategory.put(resultField,dimenMap.get(resultField)!=null ? dimenMap.get(resultField):dimenMap.get(code));
  564. // }
  565. // if(StringUtils.isNotEmpty(dateType)){
  566. // mapCategory.put(dimenMap.get(dateType).toString(),dimenMap.get(resultField));
  567. // }
  568. // mapCategory.put(quotaCode,dimenMap.get(resultField));
  569. // notExitFalg = false;
  570. // break;
  571. // }
  572. // }
  573. // if(notExitFalg){
  574. // mapCategory.put(resultField,0);
  575. // mapCategory.put(quotaCode,0);
  576. // }
  577. // mapCategory.put(firstColumnField,mapCategory.get("text"));
  578. // result.add(mapCategory);
  579. // if(mapCategory.get("children") != null){
  580. // List<Map<String,Object>> childrenOrgHealthCategoryList = (List<Map<String, Object>>) mapCategory.get("children");
  581. // mapCategory.put("children",setResultAllDimenMap(quotaCode, childrenOrgHealthCategoryList, dimenListResult, dateType));
  582. // }
  583. // }
  584. // return result;
  585. // }
  586. /**
  587. * 递归循环 计算各目录结构的值
  588. * @param orgHealthCategoryList
  589. * @param dimenListResult
  590. * @param
  591. * @return
  592. */
  593. public List<Map<String,Object>> allCategoryResultMap(List<String> quotaCodes,List<Map<String,Object>> orgHealthCategoryList,List<Map<String, Object>> dimenListResult ){
  594. List<Map<String,Object>> resultMap = new ArrayList<>();
  595. for(int i=0 ; i < orgHealthCategoryList.size() ; i++ ){
  596. Map<String,Object> mapCategory = orgHealthCategoryList.get(i);
  597. Map<String,Object> map = new HashMap<>();
  598. double parentResult = 0;
  599. mapCategory.put(firstColumnField,mapCategory.get("text"));
  600. map = getParentAllChildren(quotaCodes,mapCategory,map, dimenListResult,parentResult);
  601. if(map == null || map.size() == 0 ){
  602. for(String quotaCode : quotaCodes){
  603. map.put(quotaCode,0);
  604. }
  605. map.put(resultField,0);
  606. }
  607. mapCategory.putAll(map);
  608. resultMap.add(mapCategory);
  609. if(mapCategory.get("children") != null){
  610. List<Map<String,Object>> childrenOrgHealthCategoryList = (List<Map<String, Object>>) mapCategory.get("children");
  611. mapCategory.put("children",allCategoryResultMap(quotaCodes,childrenOrgHealthCategoryList,dimenListResult));
  612. }
  613. }
  614. return resultMap;
  615. }
  616. //获取该节点下所有末节点的结果和
  617. public Map<String,Object> getParentAllChildren(List<String> quotaCodes, Map<String,Object> mapCategory,Map<String,Object> returnMap, List<Map<String, Object>> dimenListResult, double parentResult ){
  618. try {
  619. boolean childrenFlag = false;
  620. if(mapCategory.get("children") != null){
  621. List<Map<String,Object>> childrenOrgHealthCategoryList = (List<Map<String, Object>>) mapCategory.get("children");
  622. if(childrenOrgHealthCategoryList != null && childrenOrgHealthCategoryList.size() > 0){
  623. childrenFlag = true;
  624. }
  625. }
  626. if(childrenFlag){
  627. List<Map<String,Object>> childrenOrgHealthCategoryList = (List<Map<String, Object>>) mapCategory.get("children");
  628. for(int j=0 ; j < childrenOrgHealthCategoryList.size() ; j++ ){
  629. Map<String,Object> childrenMapCategory = childrenOrgHealthCategoryList.get(j);
  630. returnMap = getParentAllChildren(quotaCodes ,childrenMapCategory,returnMap, dimenListResult,parentResult);
  631. }
  632. }else{
  633. for(Map<String, Object> dimenMap :dimenListResult){
  634. if(dimenMap.get(orgHealthCategoryCode) != null && dimenMap.get(resultField) != null){
  635. if(dimenMap.get(orgHealthCategoryCode).equals(mapCategory.get("code"))){
  636. double result = Double.parseDouble(dimenMap.get(resultField).toString());
  637. double oldResult = 0;
  638. if(returnMap.get(resultField) != null){
  639. oldResult = Double.parseDouble(returnMap.get(resultField).toString());
  640. }
  641. returnMap.put(resultField, basesicUtil.decimalPointHandle(result + oldResult));
  642. for(String quotaCode : quotaCodes){
  643. double quotaResult = Double.parseDouble(dimenMap.get(quotaCode).toString());
  644. double oldQuotaResult = 0;
  645. if( returnMap.get(quotaCode) != null ){
  646. oldQuotaResult = Double.parseDouble(returnMap.get(quotaCode).toString());
  647. }
  648. returnMap.put(quotaCode, basesicUtil.decimalPointHandle(quotaResult + oldQuotaResult));
  649. }
  650. break;
  651. }
  652. }
  653. }
  654. }
  655. }catch (Exception e){
  656. throw new NumberFormatException("统计数据转换异常");
  657. }
  658. return returnMap;
  659. }
  660. /**
  661. * 时间聚合查询指标结果
  662. * @param code
  663. * @param dimension 多维度 ; 分开
  664. * @param filter
  665. * @throws Exception
  666. */
  667. public List<Map<String, Object>> getTimeAggregationResult(String code,String dimension, String filter,String dateDime) throws Exception {
  668. TjQuota tjQuota= quotaDao.findByCode(code);
  669. Map<String,String> dimensionDicMap = getDimensionDicMap(code,dimension);
  670. List<String> dimenList = getDimenList(dimension);
  671. String groupDimension = joinDimen(dimension);
  672. List<Map<String, Object>> dimenListResult = esResultExtract.searcherSumByGroupByTime(tjQuota, groupDimension, filter, dateDime);
  673. List<Map<String, Object>> resultList = new ArrayList<>();
  674. String dateHist = "date_histogram(field=quotaDate,interval="+ dateDime +")";
  675. for(Map<String, Object> map : dimenListResult){
  676. Map<String,Object> dataMap = new HashMap<>();
  677. for(String key :map.keySet()){
  678. if(key.equals(dateHist)) {
  679. String value = "";
  680. if (dateDime.equals("year")) {
  681. value = map.get(key).toString().substring(0, 4);
  682. } else if (dateDime.contains("quarter")) {
  683. String y = map.get(key).toString().substring(0, 4);
  684. String q = map.get(key).toString().substring(5, 7);
  685. if(q.contains("01")){
  686. value = y + "年1季度";
  687. }else if(q.contains("04")){
  688. value = y + "年2季度";
  689. }else if(q.contains("07")){
  690. value = y + "年3季度";
  691. }else if(q.contains("10")){
  692. value = y + "年4季度";
  693. }
  694. } else if (dateDime.contains("month")) {
  695. value = map.get(key).toString().substring(0, 7);
  696. } else if (dateDime.contains("week")) {
  697. value = map.get(key).toString().substring(0, 7);
  698. } else if (dateDime.contains("day")) {
  699. value = map.get(key).toString().substring(0, 10);
  700. }
  701. dataMap.put(dateDime, value);
  702. }
  703. if(dimenList.contains(key)){
  704. if(dimensionDicMap.get(map.get(key).toString().toLowerCase()) != null){
  705. String dictVal = dimensionDicMap.get(map.get(key).toString().toLowerCase());
  706. dataMap.put(key+"Name",dictVal);
  707. dataMap.put(key,map.get(key).toString());
  708. dataMap.put(firstColumnField, dictVal);
  709. }else {
  710. if(key.equals(quotaDateField)){
  711. String dateFormat = "yyyy-MM-dd";
  712. if (dateDime.equals("year")) {
  713. dateFormat = "yyyy";
  714. }else if(dateDime.equals("month")){
  715. dateFormat = "yyyy-MM";
  716. }
  717. SimpleDateFormat format = new SimpleDateFormat(dateFormat);
  718. Long time = new Long(Long.valueOf(map.get(key).toString()));
  719. String quotaDate = format.format(time);
  720. dataMap.put(key, quotaDate);
  721. }else {
  722. dataMap.put(key,map.get(key));
  723. }
  724. }
  725. }
  726. //维度为特殊机构类型时
  727. if(key.equals(orgHealthCategoryCode)){
  728. dataMap.put(map.get(orgHealthCategoryCode).toString(),map.get(orgHealthCategoryCode));
  729. dataMap.put(firstColumnField,map.get("text"));
  730. }
  731. if(key.equals("SUM(result)")){
  732. dataMap.put(resultField, basesicUtil.decimalPointHandle(map.get(key)));
  733. }
  734. }
  735. resultList.add(dataMap);
  736. }
  737. return resultList;
  738. }
  739. /**
  740. * 获取聚合查询指标结果
  741. * @param code
  742. * @param dimension 多维度 ; 分开
  743. * @param filter
  744. * @throws Exception
  745. */
  746. public List<Map<String, Object>> getOrgHealthCategoryAggregationResult(String code,String dimension, String filter, String top) throws Exception {
  747. TjQuota tjQuota= quotaDao.findByCode(code);
  748. String groupDimension = "";
  749. if(dimension.contains(";")){
  750. String[] dimens = dimension.split(";");
  751. for(int i =0 ;i<dimens.length ;i++){
  752. groupDimension += dimens[i] + ",";
  753. }
  754. groupDimension = groupDimension.substring(0,groupDimension.length()-1);
  755. }else {
  756. groupDimension = dimension;
  757. }
  758. List<Map<String, Object>> dimenListResult = null;
  759. if (StringUtils.isNotEmpty(top)) {
  760. dimenListResult = esResultExtract.searcherSumGroup(tjQuota, groupDimension, filter, resultField, groupDimension, "asc", "1000");
  761. if (dimenListResult != null && dimenListResult.size() > 0) {
  762. dimenListResult = dimenListResult.subList(0, dimenListResult.size() > Integer.parseInt(top) ? Integer.parseInt(top) : dimenListResult.size());
  763. }
  764. }else {
  765. dimenListResult = esResultExtract.searcherSumGroup(tjQuota, groupDimension, filter, resultField, groupDimension, "asc", top);
  766. }
  767. List<Map<String, Object>> resultList = new ArrayList<>();
  768. for(Map<String, Object> map : dimenListResult){
  769. Map<String,Object> dataMap = new HashMap<>();
  770. boolean quotaFlag = false;
  771. for(String key :map.keySet()){
  772. //维度为特殊机构类型时
  773. if(key.equals(orgHealthCategoryCode)){
  774. dataMap.put(map.get(orgHealthCategoryCode).toString(),map.get(orgHealthCategoryCode));
  775. }
  776. if(key.equals("SUM(result)")){
  777. dataMap.put(resultField, basesicUtil.decimalPointHandle(map.get(key)));
  778. }
  779. if(key.equals(quotaDateField)){
  780. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  781. Long time = new Long(Long.valueOf(map.get(key).toString()));
  782. String quotaDate = format.format(time);
  783. dataMap.put(quotaDateField, quotaDate);
  784. quotaFlag = true;
  785. }
  786. }
  787. if(quotaFlag){
  788. map.remove(quotaDateField);
  789. }
  790. dataMap.putAll(map);
  791. resultList.add(dataMap);
  792. }
  793. return resultList;
  794. }
  795. /**
  796. * 获取 特殊机构类别 聚合查询指标结果
  797. * @param code
  798. * @param dimension 多维度 ; 分开
  799. * @param filter
  800. * @throws Exception
  801. */
  802. public List<Map<String, Object>> getAggregationResult(String code,String dimension, String filter, String top) throws Exception {
  803. TjQuota tjQuota= quotaDao.findByCode(code);
  804. Map<String,String> dimensionDicMap = getDimensionDicMap(code,dimension);
  805. List<String> dimenList = getDimenList(dimension);
  806. String groupDimension = joinDimen(dimension);
  807. List<TjQuotaDimensionSlave> slaves = tjDimensionSlaveService.findTjQuotaDimensionSlaveByQuotaCode(code);
  808. for(TjQuotaDimensionSlave slave :slaves) {
  809. if (slave.getSlaveCode().equals("dept")) {
  810. String dimenName = dimension + "Name not in('其他') ";
  811. if(StringUtils.isEmpty(filter)){
  812. filter = dimenName;
  813. }else {
  814. filter += " and " + dimenName;
  815. }
  816. }
  817. }
  818. List<Map<String, Object>> dimenListResult = null;
  819. if (StringUtils.isNotEmpty(top)) {
  820. dimenListResult = esResultExtract.searcherSumGroup(tjQuota, groupDimension, filter, resultField, groupDimension, "asc", "1000");
  821. if (dimenListResult != null && dimenListResult.size() > 0) {
  822. dimenListResult = dimenListResult.subList(0, dimenListResult.size() > Integer.parseInt(top) ? Integer.parseInt(top) : dimenListResult.size());
  823. }
  824. }else {
  825. dimenListResult = esResultExtract.searcherSumGroup(tjQuota, groupDimension, filter, resultField, groupDimension, "asc", top);
  826. }
  827. List<Map<String, Object>> resultList = new ArrayList<>();
  828. for(Map<String, Object> map : dimenListResult){
  829. Map<String,Object> dataMap = new HashMap<>();
  830. for(String key :map.keySet()){
  831. if(dimenList.contains(key)){
  832. if(dimensionDicMap.get(map.get(key).toString().toLowerCase()) != null){
  833. dataMap.put(key,dimensionDicMap.get(map.get(key).toString().toLowerCase()));
  834. dataMap.put(key+"Name",dimensionDicMap.get(map.get(key).toString().toLowerCase()));
  835. dataMap.put(key+"Code",map.get(key).toString());
  836. dataMap.put(firstColumnField,dimensionDicMap.get(map.get(key).toString().toLowerCase()));
  837. }else {
  838. dataMap.put(key,map.get(key));
  839. }
  840. }
  841. //维度为特殊机构类型时
  842. if(key.equals(orgHealthCategoryCode)){
  843. dataMap.put(map.get(orgHealthCategoryCode).toString(),map.get(orgHealthCategoryCode));
  844. dataMap.put(firstColumnField,map.get("text"));
  845. }
  846. if(key.equals("SUM(result)")){
  847. dataMap.put(resultField, basesicUtil.decimalPointHandle(map.get(key)));
  848. }
  849. }
  850. resultList.add(dataMap);
  851. }
  852. if (StringUtils.isEmpty(top) && "town".equals(dimension)) {
  853. resultList = noDataDimenDictionary(resultList,dimension,filter);
  854. }
  855. for(TjQuotaDimensionSlave slave :slaves){
  856. if(slave.getSlaveCode().equals("sex") ){
  857. resultList = filteUnKnowSex(resultList, dimension);
  858. }
  859. }
  860. if(dimension.equals("level")){
  861. resultList = filteUnKnowLeve(resultList, dimension);
  862. }
  863. return resultList;
  864. }
  865. /**
  866. * 过滤未说明的性别,和其他为0的数据
  867. * @param dataList
  868. * @return
  869. */
  870. public List<Map<String, Object>> filteUnKnowSex(List<Map<String, Object>> dataList,String dimension){
  871. List<Map<String, Object>> resultList = new ArrayList<>();
  872. for(Map<String,Object> map : dataList){
  873. if(map.get(dimension) !=null){
  874. if( !map.get(dimension).toString().contains("未说明")){
  875. if(map.get(resultField) !=null){
  876. if(!map.get(dimension).toString().contains("未知") || ! map.get(resultField).toString().equals("0") ){
  877. resultList.add(map);
  878. }
  879. }
  880. }
  881. }
  882. }
  883. return resultList;
  884. }
  885. /**
  886. * 过滤未知的等级
  887. * @param dataList
  888. * @return
  889. */
  890. public List<Map<String, Object>> filteUnKnowLeve(List<Map<String, Object>> dataList,String dimension){
  891. List<Map<String, Object>> resultList = new ArrayList<>();
  892. for(Map<String,Object> map : dataList){
  893. if(map.get(dimension) !=null){
  894. if( !(map.get(dimension).toString().contains("未知") || map.get(dimension).toString().contains("9")) ){
  895. resultList.add(map);
  896. }
  897. }
  898. }
  899. return resultList;
  900. }
  901. /**
  902. * 查询结果 对无数据的字典项补0
  903. * @param dataList 数据集合
  904. * @param dimen 维度
  905. * @return
  906. */
  907. public List<Map<String, Object>> noDataDimenDictionary(List<Map<String, Object>> dataList,String dimen,String filter){
  908. Map<String, Object> dictMap = new HashMap<>();
  909. if(dimen.equals("town")){
  910. String sql = "";
  911. if(dimen.equals("town") ){
  912. sql = "SELECT id as code,name as name from address_dict where pid = '361100'";
  913. List<Map<String, Object>> dictDataList = jdbcTemplate.queryForList(sql);
  914. if(null != dictDataList) {
  915. for(int i = 0 ; i < dictDataList.size();i++){
  916. if(null != dictDataList.get(i).get("code") && null != dictDataList.get(i).get("name")){
  917. dictMap.put(dictDataList.get(i).get("code").toString(),dictDataList.get(i).get("name").toString());
  918. }
  919. }
  920. }
  921. }
  922. List<Map<String, Object>> resultList = new ArrayList<>();
  923. for(String code : dictMap.keySet()){
  924. Map<String,Object> oneMap = new HashMap<>();
  925. String result = "0";
  926. for(Map<String,Object> map : dataList){
  927. if(map.get(dimen) !=null && map.get(dimen).equals(dictMap.get(code))){
  928. result = map.get(resultField).toString();
  929. break;
  930. }
  931. }
  932. oneMap.put(firstColumnField,dictMap.get(code));
  933. oneMap.put(dimen,dictMap.get(code));
  934. oneMap.put(dimen+"Name",dictMap.get(code));
  935. oneMap.put(dimen+"Code",code);
  936. oneMap.put(resultField,result);
  937. resultList.add(oneMap);
  938. }
  939. return resultList;
  940. }
  941. return dataList;
  942. }
  943. /**
  944. * 拼接维度分组
  945. * @param dimension
  946. * @return
  947. */
  948. public String joinDimen(String dimension){
  949. String groupDimension = "";
  950. if(dimension.contains(";")){
  951. String[] dimens = dimension.split(";");
  952. for(int i =0 ;i<dimens.length ;i++){
  953. groupDimension += dimens[i] + ",";
  954. }
  955. groupDimension = groupDimension.substring(0,groupDimension.length()-1);
  956. }else {
  957. groupDimension = dimension;
  958. }
  959. return groupDimension;
  960. }
  961. /**
  962. * 获取维度List
  963. * @param dimension
  964. * @return 多维度 ;隔开
  965. */
  966. public List<String> getDimenList(String dimension){
  967. List<String> dimenList = new ArrayList<>();
  968. if(dimension.contains(";")){
  969. String[] dimens = dimension.split(";");
  970. for(int i =0 ;i<dimens.length ;i++){
  971. dimenList.add(dimens[i]);
  972. }
  973. }else {
  974. dimenList.add(dimension);
  975. }
  976. return dimenList;
  977. }
  978. /**
  979. * 获取指标维度字典项
  980. * @param quotaCode
  981. * @param dimension 多维度 ;隔开
  982. * @return
  983. */
  984. public Map<String,String> getDimensionDicMap(String quotaCode ,String dimension){
  985. Map<String,String> dimensionDicMap = new HashMap<>();
  986. String[] dimens = dimension.split(";");
  987. for(int i =0 ;i<dimens.length ;i++){
  988. String dictSql = getQuotaDimensionDictSql(quotaCode, dimens[i]);
  989. if(StringUtils.isNotEmpty(dictSql)){
  990. Map<String,String> dicMap = getDimensionMap(dictSql, dimens[i]);
  991. for(String key :dicMap.keySet()){
  992. dimensionDicMap.put(key.toLowerCase(),dicMap.get(key));
  993. }
  994. }
  995. }
  996. return dimensionDicMap;
  997. }
  998. /**
  999. * 获取维度的字典 sql -- 针对于查询结果 从ES库查询结果
  1000. * @param quotaCode
  1001. * @param dimension
  1002. * @return
  1003. */
  1004. private String getQuotaDimensionDictSql(String quotaCode, String dimension) {
  1005. boolean mainFlag = dimension.contains("province") || dimension.contains("city") ||dimension.contains("town")
  1006. ||dimension.contains("org") ||dimension.contains("dept") ||dimension.contains("year") ||dimension.contains("quarter")
  1007. ||dimension.contains("month") ||dimension.contains("day") || dimension.contains(quotaDateField) ;
  1008. String dictSql = "";
  1009. //查询维度 sql
  1010. if( mainFlag){
  1011. List<TjQuotaDimensionMain> dimensionMains = tjDimensionMainService.findTjQuotaDimensionMainByQuotaCode(quotaCode);
  1012. if(dimensionMains != null && dimensionMains.size() > 0){
  1013. for(TjQuotaDimensionMain main:dimensionMains){
  1014. if(main.getMainCode().equals(dimension)){
  1015. dictSql = main.getDictSql();
  1016. }
  1017. }
  1018. }
  1019. }else {
  1020. if(StringUtils.isEmpty(dictSql)) {
  1021. List<TjQuotaDimensionSlave> dimensionSlaves = tjDimensionSlaveService.findTjQuotaDimensionSlaveByQuotaCode(quotaCode);
  1022. if (dimensionSlaves != null && dimensionSlaves.size() > 0) {
  1023. if(StringUtils.isNotEmpty(dimension)){
  1024. String n = dimension.substring(dimension.length() - 1, dimension.length());
  1025. if(StringUtils.isNotEmpty(n) && (n.equals("1") || n.equals("2") || n.equals("3") || n.equals("4")) ){
  1026. int slave = Integer.valueOf(n);
  1027. if(dimensionSlaves.size() >= slave){
  1028. dictSql = dimensionSlaves.get(slave-1).getDictSql();
  1029. }
  1030. }
  1031. }
  1032. }
  1033. }
  1034. }
  1035. return dictSql;
  1036. }
  1037. /**
  1038. *获取字典项数据集
  1039. * @param dictSql
  1040. * @param dimension
  1041. * @return
  1042. */
  1043. public Map<String,String> getDimensionMap(String dictSql, String dimension) {
  1044. Map<String,String> dimensionDicMap = new HashMap<>();
  1045. if(StringUtils.isNotEmpty(dictSql)) {
  1046. BasesicUtil baseUtil = new BasesicUtil();
  1047. boolean main = dimension.contains("province") || dimension.contains("city") ||dimension.contains("town")
  1048. ||dimension.contains("org") ||dimension.contains("dept") ||dimension.contains("year") ||dimension.contains("month")
  1049. ||dimension.contains("quarter") ||dimension.contains("day");
  1050. if( main){
  1051. //主纬度字典项
  1052. List<SaveModel> dictDatas = jdbcTemplate.query(dictSql, new BeanPropertyRowMapper(SaveModel.class));
  1053. if(dictDatas != null ) {
  1054. for (SaveModel saveModel : dictDatas) {
  1055. String name = baseUtil.getFieldValueByName(dimension + "Name", saveModel);
  1056. String val = baseUtil.getFieldValueByName(dimension, saveModel);
  1057. if(StringUtils.isNotEmpty(val) && StringUtils.isNotEmpty(name)){
  1058. dimensionDicMap.put(val.toLowerCase(),name);
  1059. }
  1060. }
  1061. }
  1062. } else{
  1063. //查询细维度字典数据
  1064. List<DictModel> dictDatas = jdbcTemplate.query(dictSql, new BeanPropertyRowMapper(DictModel.class));
  1065. for (DictModel dictModel : dictDatas) {
  1066. String name = baseUtil.getFieldValueByName("name", dictModel);
  1067. String val = baseUtil.getFieldValueByName("code", dictModel).toLowerCase();
  1068. dimensionDicMap.put(val,name);
  1069. }
  1070. }
  1071. }
  1072. return dimensionDicMap;
  1073. }
  1074. /**
  1075. * 获取单个指标结果
  1076. * @param code
  1077. * @param filters 外部传入条件
  1078. * @param dimension
  1079. * @return
  1080. * @throws Exception
  1081. */
  1082. public List<Map<String, Object>> getSimpleQuotaReport(String code,String filters,String dimension,boolean isTrunTree, String top) throws Exception {
  1083. String dateType = "";
  1084. //指标的展示维度,由视图中决定
  1085. if(dimension.trim().equals("year")){
  1086. dateType = "year";
  1087. dimension = "";
  1088. }else if(dimension.trim().equals("quarter")){
  1089. dateType = "quarter";
  1090. dimension = "";
  1091. }else if(dimension.trim().equals("month")){
  1092. dateType = "month";
  1093. dimension = "";
  1094. }else if(dimension.trim().equals("day")){
  1095. dateType = "day";
  1096. dimension = "";
  1097. }
  1098. List<Map<String, Object>> result = new ArrayList<>();
  1099. TjQuotaDataSource quotaDataSource = dataSourceService.findSourceByQuotaCode(code);
  1100. JSONObject obj = new JSONObject().fromObject(quotaDataSource.getConfigJson());
  1101. EsConfig esConfig= (EsConfig) JSONObject.toBean(obj,EsConfig.class);
  1102. String configFilter = esConfig.getFilter();
  1103. if(StringUtils.isNotEmpty(configFilter) && quotaDataSource.getSourceCode().equals("1")){//数据源为ES库
  1104. TjQuotaDataSave quotaDataSave = dataSaveService.findByQuota(code);
  1105. if(quotaDataSave != null && StringUtils.isNotEmpty(quotaDataSave.getConfigJson())){
  1106. JSONObject objSave = new JSONObject().fromObject(quotaDataSave.getConfigJson());
  1107. EsConfig esConfigSave = (EsConfig) JSONObject.toBean(objSave,EsConfig.class);
  1108. if(StringUtils.isEmpty(esConfig.getIndex()) || esConfig.getIndex().equals(esConfigSave.getIndex()) ){
  1109. if(StringUtils.isNotEmpty(filters)){
  1110. filters += " and " + configFilter;
  1111. }else {
  1112. filters = configFilter;
  1113. }
  1114. }
  1115. }else {
  1116. if(StringUtils.isNotEmpty(filters)){
  1117. filters += " and " + configFilter;
  1118. }else {
  1119. filters = configFilter;
  1120. }
  1121. }
  1122. }
  1123. // 判断该指标是否需要同比, 需要的话拼接时间条件
  1124. if (StringUtils.isNotEmpty(esConfig.getIncrementFlag())) {
  1125. filters = filtersExchangeHandle(filters, esConfig);
  1126. log.info("filters = {}", filters);
  1127. }
  1128. String molecularFilter = filters;
  1129. String denominatorFilter = filters;
  1130. if (StringUtils.isNotEmpty(esConfig.getGrowthFlag())) {
  1131. //增幅运算(环比和同比)
  1132. result = getGrowthByQuota(dimension, filters, esConfig, dateType, top);
  1133. } else {
  1134. if (StringUtils.isNotEmpty(esConfig.getDateComparisonType())) {
  1135. //时间条件处理
  1136. filters = getdateComparisonTypeFilter(esConfig,filters);
  1137. }
  1138. if( (StringUtils.isNotEmpty(esConfig.getEspecialType())) && esConfig.getEspecialType().equals(orgHealthCategory)){
  1139. // //特殊机构类型查询输出结果 只有查询条件没有维度 默认是 机构类型维度
  1140. // result = getOrgHealthCategory(code,filters,dateType,isTrunTree, top);
  1141. }else if( (StringUtils.isNotEmpty(esConfig.getMolecular())) && StringUtils.isNotEmpty(esConfig.getDenominator())){//除法
  1142. //除法指标查询输出结果
  1143. molecularFilter = handleFilter(esConfig.getMolecularFilter(), molecularFilter);
  1144. denominatorFilter = handleFilter(esConfig.getDenominatorFilter(), denominatorFilter);
  1145. if (StringUtils.isNotEmpty(esConfig.getDivisionType()) && esConfig.getDivisionType().equals("2")) {
  1146. result = divisionQuotaDenoConstant(esConfig.getMolecular(), dimension, molecularFilter, denominatorFilter,esConfig.getPercentOperation(), esConfig.getPercentOperationValue(), dateType, top);
  1147. } else {
  1148. result = divisionQuota(esConfig.getMolecular(), esConfig.getDenominator(), dimension, molecularFilter, denominatorFilter, esConfig.getPercentOperation(), esConfig.getPercentOperationValue(),dateType, top);
  1149. }
  1150. //计算除法 合计
  1151. String moleQuotaCode = getBasicQuota(esConfig.getMolecular());
  1152. moleQuotaCode = " quotaCode='" + moleQuotaCode.replaceAll("_", "") + "' ";
  1153. String denoQuotaCode = getBasicQuota(esConfig.getDenominator());
  1154. denoQuotaCode = " quotaCode='" + denoQuotaCode.replaceAll("_", "") + "' ";
  1155. if(StringUtils.isEmpty(molecularFilter)){
  1156. molecularFilter = moleQuotaCode;
  1157. }else {
  1158. molecularFilter += " and " + moleQuotaCode;
  1159. }
  1160. if(StringUtils.isEmpty(denominatorFilter)){
  1161. denominatorFilter = denoQuotaCode;
  1162. }else {
  1163. denominatorFilter += " and " + denoQuotaCode;
  1164. }
  1165. String moleTotalSql = "select sum(result) result from medical_service_index where " + molecularFilter;
  1166. String denoTotalSql = "select sum(result) result from medical_service_index where " + denominatorFilter;
  1167. double moleTotal = 0;
  1168. double denoTotal = 0;
  1169. List<Map<String, Object>> moleListMap = elasticsearchUtil.excuteDataModel(moleTotalSql.toString());
  1170. if(moleListMap != null && moleListMap.size() > 0){
  1171. if(moleListMap.get(0).get("result") != null){
  1172. moleTotal = Double.valueOf(moleListMap.get(0).get("result").toString());
  1173. }
  1174. }
  1175. List<Map<String, Object>> denoListMap = elasticsearchUtil.excuteDataModel(denoTotalSql.toString());
  1176. if(denoListMap != null && denoListMap.size() > 0){
  1177. if(denoListMap.get(0).get("result") != null){
  1178. denoTotal = Double.valueOf(denoListMap.get(0).get("result").toString());
  1179. }
  1180. }
  1181. double point = 0;
  1182. int operation = Integer.valueOf(esConfig.getPercentOperation());
  1183. int operationValue = Integer.valueOf(esConfig.getPercentOperationValue());
  1184. if(denoTotal - 0 != 0){
  1185. if(moleTotal != 0){
  1186. if(operation == 1){
  1187. point = (moleTotal/denoTotal) * operationValue;
  1188. }else if(operation == 2){
  1189. point = (moleTotal/denoTotal) / operationValue;
  1190. }
  1191. }
  1192. }
  1193. Map<String, Object> totalMap = new HashMap<>();
  1194. totalMap.put(resultField, basesicUtil.decimalPointHandle(point));
  1195. totalMap.put(dimension,"合计");
  1196. totalMap.put(firstColumnField,"合计");
  1197. result.add(totalMap);
  1198. }else if(StringUtils.isNotEmpty(esConfig.getAddOperation())){
  1199. String firstFilter = handleFilter(esConfig.getAddFirstFilter(), filters);
  1200. String secondFilter = handleFilter(esConfig.getAddSecondFilter(), filters);
  1201. result = addQuota(esConfig.getAddFirstQuotaCode(), firstFilter, esConfig.getAddSecondQuotaCode(), secondFilter, esConfig.getAddOperation(),dimension,dateType, top);
  1202. }else if(StringUtils.isNotEmpty(esConfig.getSuperiorBaseQuotaCode())) {
  1203. //二次统计 指标查询
  1204. result = getQuotaResultList(esConfig.getSuperiorBaseQuotaCode(), dimension,filters,dateType, top);
  1205. }else {
  1206. //普通基础指标查询
  1207. result = getQuotaResultList(code, dimension,filters,dateType, top);
  1208. }
  1209. }
  1210. return result;
  1211. }
  1212. //时间对比类型 时的 查询条件处理
  1213. public String getdateComparisonTypeFilter(EsConfig esConfig,String filters){
  1214. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  1215. Calendar c = Calendar.getInstance();
  1216. Map<String,String> filterMap = new HashMap<>();
  1217. if(StringUtils.isNotEmpty(filters)){
  1218. String [] filter = filters.split("and");
  1219. String year = "";
  1220. String month = "";
  1221. String day = "";
  1222. if(filter.length > 0 ){
  1223. for(String key : filter){
  1224. filterMap.put(key,key);
  1225. Date date = new Date();
  1226. if(key.contains(quotaDateField)){
  1227. //quotaDate >= '2018-03-01'
  1228. String dateStr = key.substring(key.indexOf("'")+1,key.lastIndexOf("'"));
  1229. date = DateUtil.parseDate(dateStr,DateUtil.DEFAULT_DATE_YMD_FORMAT);
  1230. }
  1231. if(esConfig.getDateComparisonType().equals("lastYear")){
  1232. c.setTime(date);
  1233. c.add(Calendar.YEAR, -1);
  1234. Date lastYear = c.getTime();
  1235. filterMap.put(key,format.format(lastYear));
  1236. }else if(esConfig.getDateComparisonType().equals("lastMonth")) {
  1237. c.setTime(date);
  1238. c.add(Calendar.MONTH, -1);
  1239. Date lastYear = c.getTime();
  1240. filterMap.put(key,format.format(lastYear));
  1241. }else if(esConfig.getDateComparisonType().equals("lastDay")) {
  1242. c.setTime(date);
  1243. c.add(Calendar.DATE, -1);
  1244. Date lastYear = c.getTime();
  1245. filterMap.put(key,format.format(lastYear));
  1246. }
  1247. }
  1248. }
  1249. filters = "";
  1250. if(filterMap != null && filterMap.size() > 0){
  1251. for(String key : filterMap.keySet()){
  1252. if(StringUtils.isEmpty(filters)){
  1253. filters = filterMap.get(key);
  1254. }else {
  1255. filters = " and " + filterMap.get(key);
  1256. }
  1257. }
  1258. }
  1259. }
  1260. return filters;
  1261. }
  1262. public String handleFilter(String secondFilter, String resultFilter) {
  1263. if (StringUtils.isNotEmpty(secondFilter)) {
  1264. if (StringUtils.isEmpty(resultFilter)) {
  1265. resultFilter = secondFilter;
  1266. } else {
  1267. resultFilter += " and " + secondFilter;
  1268. }
  1269. }
  1270. return resultFilter;
  1271. }
  1272. /**
  1273. * 门急诊费用
  1274. * @return
  1275. */
  1276. public String getCostOfOutPatient() {
  1277. String sum = "0";
  1278. String sql = "select sum(result) from medical_service_index where quotaCode='HC041047'";
  1279. List<Map<String, Object>> listData = elasticsearchUtil.excuteDataModel(sql);
  1280. if (null != listData && listData.size() > 0 && listData.get(0).size() > 0) {
  1281. for (Map<String, Object> map : listData) {
  1282. sum = basesicUtil.decimalPointHandle(map.get("SUM(result)"));
  1283. }
  1284. }
  1285. return sum;
  1286. }
  1287. /**
  1288. * 门急诊人次
  1289. * @return
  1290. */
  1291. public String getNumOfOutPatient() {
  1292. int sum = 0;
  1293. String sql = "select sum(result) from medical_service_index where quotaCode='HC041008'";
  1294. List<Map<String, Object>> listData = singleDiseaseService.parseIntegerValue(sql);
  1295. if (null != listData && listData.size() > 0 && listData.get(0).size() > 0) {
  1296. for (Map<String, Object> map : listData) {
  1297. String value = basesicUtil.decimalPointHandle(map.get("SUM(result)"));
  1298. sum += Integer.parseInt(value);
  1299. }
  1300. }
  1301. return sum + "";
  1302. }
  1303. /**
  1304. * 入院费用
  1305. * @return
  1306. */
  1307. public String getCostOfInPatient() {
  1308. String sum = "0";
  1309. String sql = "select sum(result) from medical_service_index where quotaCode='HC041068'";
  1310. List<Map<String, Object>> listData = elasticsearchUtil.excuteDataModel(sql);
  1311. if (null != listData && listData.size() > 0 && listData.get(0).size() > 0) {
  1312. for (Map<String, Object> map : listData) {
  1313. sum = basesicUtil.decimalPointHandle(map.get("SUM(result)"));
  1314. }
  1315. }
  1316. return sum;
  1317. }
  1318. /**
  1319. * 入院人次
  1320. * @return
  1321. */
  1322. public String getNumOfInPatient() {
  1323. int sum = 0;
  1324. String sql = "select sum(result) from medical_service_index where quotaCode='HC041000'";
  1325. List<Map<String, Object>> listData = singleDiseaseService.parseIntegerValue(sql);
  1326. if (null != listData && listData.size() > 0 && listData.get(0).size() > 0) {
  1327. for (Map<String, Object> map : listData) {
  1328. String value = basesicUtil.decimalPointHandle(map.get("SUM(result)"));
  1329. sum += Integer.parseInt(value);
  1330. }
  1331. }
  1332. return sum + "";
  1333. }
  1334. public String getCostOfMedicalMonitor() {
  1335. // 获取门急诊费用
  1336. Double costOfOutPatient = Double.parseDouble(getCostOfOutPatient());
  1337. // 获取入院费用
  1338. Double costOfInPatient = Double.parseDouble(getCostOfInPatient());
  1339. // 医疗费用监测 = 获取门急诊费用 + 获取入院费用
  1340. Double costOfMedicalMonitor = costOfInPatient + costOfOutPatient;
  1341. return basesicUtil.decimalPointHandle(costOfMedicalMonitor);
  1342. }
  1343. /**
  1344. * 数据查询
  1345. * @return
  1346. */
  1347. public Map<String, List<String>> getDataInfo(String sql ,String xdataName) {
  1348. List<Map<String, Object>> listData = parseIntegerValue(sql);
  1349. Map<String, List<String>> map = new HashMap<>();
  1350. List<String> xData = new ArrayList<>();
  1351. List<String> valueData = new ArrayList<>();
  1352. if (null != listData && listData.size() >0 && listData.get(0) !=null ) {
  1353. listData.forEach(one -> {
  1354. if(xdataName.contains("date_histogram")){
  1355. if(xdataName.contains("year")){
  1356. xData.add(one.get(xdataName).toString().substring(0,4) + "");
  1357. }else if(xdataName.contains("quarter")){
  1358. String quarter = "";
  1359. if(one.get(xdataName) != null){
  1360. String value = one.get(xdataName).toString();
  1361. String y = value.substring(0, 4);
  1362. String q = value.substring(5, 7);
  1363. if(q.contains("01")){
  1364. quarter = y + "年1季度";
  1365. }else if(q.contains("04")) {
  1366. value = y + "年2季度";
  1367. }else if(q.contains("07")){
  1368. quarter = y + "年3季度";
  1369. }else if(q.contains("10")){
  1370. quarter = y + "年4季度";
  1371. }
  1372. xData.add(quarter);
  1373. }
  1374. }else if(xdataName.contains("month")){
  1375. xData.add(one.get(xdataName).toString().substring(0,7) + "");
  1376. }
  1377. }else {
  1378. xData.add(one.get(xdataName) + "");
  1379. }
  1380. valueData.add(one.get("count") + "");
  1381. });
  1382. map.put("xData", xData);
  1383. map.put("valueData", valueData);
  1384. }
  1385. return map;
  1386. }
  1387. /**
  1388. * 对查询结果key包含count、sum的value去掉小数点
  1389. * @param sql
  1390. * @return
  1391. */
  1392. public List<Map<String, Object>> parseIntegerValue(String sql) {
  1393. List<Map<String, Object>> listData = elasticsearchUtil.excuteDataModel(sql);
  1394. List<Map<String, Object>> handleData = new ArrayList<>();
  1395. listData.forEach(item -> {
  1396. Map<String, Object> myMap = new HashMap<>();
  1397. item.forEach((k,v) -> {
  1398. if (k.contains("COUNT") || k.contains("SUM") || k.contains("count")) {
  1399. v = (int) Double.parseDouble(v + "");
  1400. }
  1401. myMap.put(k,v);
  1402. });
  1403. handleData.add(myMap);
  1404. });
  1405. return handleData;
  1406. }
  1407. /**
  1408. * 百分比 增幅运算 维度不是时间维度
  1409. * @param dimension
  1410. * @param moleList
  1411. * @param denoList
  1412. * @param operation
  1413. * @param operationValue
  1414. * @return
  1415. */
  1416. public List<Map<String, Object>> divisionPercent(String dimension, List<Map<String, Object>> moleList, List<Map<String, Object>> denoList,int operation,int operationValue){
  1417. List<Map<String, Object>> divisionResultList = new ArrayList<>();
  1418. for(Map<String, Object> denoMap : denoList) {
  1419. if (null != denoMap && denoMap.size() > 0 ) {
  1420. Map<String, Object> map = new HashMap<>();
  1421. double denoResultVal = Double.valueOf(denoMap.get(resultField) == null ? "0" : denoMap.get(resultField).toString());
  1422. String denoKeyVal = "";
  1423. String [] denoDimensions = dimension.split(";");
  1424. for(int i = 0 ;i < denoDimensions.length ; i++){
  1425. if(i == 0){
  1426. denoKeyVal = denoMap.get(denoDimensions[i]).toString();
  1427. }else {
  1428. denoKeyVal = denoKeyVal + "-" + denoMap.get(denoDimensions[i]).toString() ;
  1429. }
  1430. map.put(firstColumnField, denoMap.get(firstColumnField));
  1431. map.put(denoDimensions[i], denoMap.get(denoDimensions[i]).toString());
  1432. }
  1433. if (denoResultVal == 0) {
  1434. map.put(resultField, "--");
  1435. divisionResultList.add(map);
  1436. } else {
  1437. if(moleList != null && moleList.size() > 0){
  1438. for(Map<String, Object> moleMap :moleList) {
  1439. String moleKeyVal = "";
  1440. String [] moleDimensions = dimension.split(";");
  1441. for(int i = 0 ;i < moleDimensions.length ; i++){
  1442. if(i == 0){
  1443. moleKeyVal = moleMap.get(moleDimensions[i]).toString();
  1444. }else {
  1445. moleKeyVal = moleKeyVal + "-" + moleMap.get(moleDimensions[i]).toString() ;
  1446. }
  1447. }
  1448. if(denoKeyVal.equals(moleKeyVal)){
  1449. double point = 0;
  1450. float moleResultVal = Float.valueOf(moleMap.get(resultField).toString());
  1451. point = ((moleResultVal - denoResultVal)/denoResultVal) * operationValue;
  1452. map.put(resultField, basesicUtil.decimalPointHandle(point));
  1453. divisionResultList.add(map);
  1454. break;
  1455. }
  1456. }
  1457. }else {
  1458. map.put(resultField, 0);
  1459. divisionResultList.add(map);
  1460. }
  1461. }
  1462. }
  1463. }
  1464. Double moleVal = caculateResult(moleList);
  1465. Double denoVal = caculateResult(denoList);
  1466. if (0 != denoVal) {
  1467. Map<String, Object> map = new HashMap<>();
  1468. double result = (moleVal - denoVal) / denoVal * operationValue;
  1469. map.put("result", basesicUtil.decimalPointHandle(result));
  1470. map.put("firstColumn", "合计");
  1471. map.put(dimension, "合计");
  1472. divisionResultList.add(map);
  1473. }
  1474. return divisionResultList;
  1475. }
  1476. /**
  1477. * 计算增幅 环比和 同比
  1478. * @param dimension 不为空时,查询固定某个月或者某年的增幅
  1479. * @param filters 外部过滤条件 如 quotaDate >= '2018-03-01' and quotaDate <= '2018-03-31'
  1480. * @param esConfig 指标内部配置
  1481. * @param dateType 不为空时,查询某个时间区间的增幅
  1482. * @return
  1483. * @throws Exception
  1484. */
  1485. public List<Map<String, Object>> getGrowthByQuota(String dimension, String filters, EsConfig esConfig, String dateType, String top) throws Exception {
  1486. List<Map<String, Object>> resultList = new LinkedList<>();
  1487. String startQuotaDate = "";
  1488. String endQuotaDate = "";
  1489. String noDateFilter = "";
  1490. if(StringUtils.isNotEmpty(esConfig.getDenominatorFilter()) ){
  1491. noDateFilter = esConfig.getDenominatorFilter();
  1492. }
  1493. if (StringUtils.isNotEmpty(filters) && filters.contains(quotaDateField)) {// 外部指定时间
  1494. String params [] = filters.split("and ");
  1495. for(int i =0 ;i< params.length ;i++){
  1496. String quotaDateParam = params[i];
  1497. if(quotaDateParam.contains(quotaDateField)){
  1498. boolean b = quotaDateParam.indexOf("'") > -1;
  1499. int start = b ? quotaDateParam.indexOf("'") : quotaDateParam.indexOf("\"");//查询条件不是' ,就是"
  1500. if(quotaDateParam.contains(">")){
  1501. startQuotaDate = quotaDateParam.substring(start + 1, start + 11);
  1502. }
  1503. if(quotaDateParam.contains("<")){
  1504. endQuotaDate = quotaDateParam.substring(start + 1, start + 11);
  1505. }
  1506. }else{
  1507. if(StringUtils.isNotEmpty(noDateFilter)){
  1508. noDateFilter += " and " + quotaDateParam;
  1509. }else {
  1510. noDateFilter = quotaDateParam;
  1511. }
  1512. }
  1513. }
  1514. }
  1515. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  1516. Calendar lastDate = Calendar.getInstance();
  1517. String molecularFilter = "";
  1518. String denominatorFilter = "";
  1519. String growthFlag = esConfig.getGrowthFlag();
  1520. if(StringUtils.isEmpty(dateType)){
  1521. if ("1".equals(growthFlag)) { // 年增幅 没有传时间条件默认当前年份
  1522. int now;
  1523. int beforeNow;
  1524. now = lastDate.get(Calendar.YEAR);
  1525. if (StringUtils.isNotEmpty(endQuotaDate)) {// 外部指定时间
  1526. now = Integer.parseInt(endQuotaDate.substring(0,4));
  1527. }
  1528. beforeNow = now - 1;
  1529. molecularFilter = "quotaDate >= '" + now + "-01-01' and quotaDate <= '" + now + "-12-31'";
  1530. denominatorFilter = "quotaDate >= '" + beforeNow + "-01-01' and quotaDate <= '" + beforeNow + "-12-31'";
  1531. } else if ("2".equals(growthFlag)) { // 月增幅 没有传时间条件默认当前月份
  1532. // 如果有时间过滤条件,则按时间条件计算
  1533. if (StringUtils.isNotEmpty(endQuotaDate)) {
  1534. lastDate.setTime(sdf.parse(endQuotaDate));
  1535. }
  1536. lastDate.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号,当前日期既为本月第一天
  1537. String firstDay = sdf.format(lastDate.getTime());
  1538. lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
  1539. String lastDay = sdf.format(lastDate.getTime());
  1540. lastDate.add(Calendar.MONTH, -1);
  1541. lastDate.set(Calendar.DAY_OF_MONTH, 1);
  1542. String preMonthFirstDay = sdf.format(lastDate.getTime());
  1543. lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
  1544. String preMonthLastDay = sdf.format(lastDate.getTime());
  1545. log.info("firstDay = {}, lastDay = {}", firstDay, lastDay);
  1546. log.info("preMonthFirstDay = {}, preMonthLastDay = {}", preMonthFirstDay, preMonthLastDay);
  1547. molecularFilter = "quotaDate >= '" + firstDay + "' and quotaDate <= '" + lastDay + "'";
  1548. denominatorFilter = "quotaDate >= '" + preMonthFirstDay + "' and quotaDate <= '" + preMonthLastDay + "'";
  1549. } else if ("3".equals(growthFlag)) { // 季度增幅 没有传时间条件默认当前季度
  1550. // 如果有时间过滤条件,则按时间条件计算
  1551. if (StringUtils.isNotEmpty(endQuotaDate)) {
  1552. lastDate.setTime(sdf.parse(endQuotaDate));
  1553. }
  1554. String firstDay = "";
  1555. String lastDay = "";
  1556. int currentMonth = lastDate.get(Calendar.MONTH) + 1;
  1557. if (currentMonth >= 1 && currentMonth <= 3){
  1558. lastDate.set(Calendar.MONTH, 0);
  1559. }else if (currentMonth >= 4 && currentMonth <= 6){
  1560. lastDate.set(Calendar.MONTH, 3);
  1561. } else if (currentMonth >= 7 && currentMonth <= 9){
  1562. lastDate.set(Calendar.MONTH, 6);
  1563. } else if (currentMonth >= 10 && currentMonth <= 12){
  1564. lastDate.set(Calendar.MONTH, 9);
  1565. }
  1566. lastDate.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号,当前日期既为本月第一天
  1567. firstDay = sdf.format(lastDate.getTime());//这个季度第一天
  1568. lastDate.add(Calendar.MONTH, 3);
  1569. lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
  1570. lastDay = sdf.format(lastDate.getTime());//这个季度最后一天
  1571. lastDate.add(Calendar.MONTH, -6);//前一个季度第一天
  1572. lastDate.set(Calendar.DAY_OF_MONTH, 1);
  1573. lastDate.add(Calendar.MONTH, 3);
  1574. String preMonthFirstDay = sdf.format(lastDate.getTime());
  1575. lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
  1576. String preMonthLastDay = sdf.format(lastDate.getTime());
  1577. log.info("firstDay = {}, lastDay = {}", firstDay, lastDay);
  1578. log.info("preMonthFirstDay = {}, preMonthLastDay = {}", preMonthFirstDay, preMonthLastDay);
  1579. molecularFilter = "quotaDate >= '" + firstDay + "' and quotaDate <= '" + lastDay + "'";
  1580. denominatorFilter = "quotaDate >= '" + preMonthFirstDay + "' and quotaDate <= '" + preMonthLastDay + "'";
  1581. }
  1582. if (StringUtils.isNotEmpty(esConfig.getMolecularFilter())) {
  1583. molecularFilter += " and " + esConfig.getMolecularFilter();
  1584. }
  1585. if (StringUtils.isNotEmpty(esConfig.getDenominatorFilter())) {
  1586. denominatorFilter += " and " + esConfig.getDenominatorFilter();
  1587. }
  1588. if(StringUtils.isNotEmpty(noDateFilter)){
  1589. molecularFilter += " and " + noDateFilter;
  1590. denominatorFilter += " and " + noDateFilter;
  1591. }
  1592. List<Map<String, Object>> moleList = getSimpleQuotaReport(esConfig.getMolecular(), molecularFilter,dimension ,false , top);
  1593. List<Map<String, Object>> denoList = getSimpleQuotaReport(esConfig.getDenominator(),denominatorFilter,dimension,false, null);
  1594. resultList = divisionPercent(dimension, moleList, denoList, 1, 100);
  1595. return resultList;
  1596. }else {
  1597. String dateFilter = "";
  1598. //按时间维度 增幅,即时间区间增幅
  1599. int nowYear;
  1600. int beforeYear;
  1601. Date firstMonth = new Date();
  1602. Date endMonth = new Date();
  1603. nowYear = lastDate.get(Calendar.YEAR);
  1604. beforeYear = nowYear - 4;
  1605. if ("1".equals(growthFlag)) { // 年增幅 没有传时间条件默认当前年份 向前推3年
  1606. if (StringUtils.isNotEmpty(startQuotaDate)) {// 外部指定时间
  1607. beforeYear = Integer.parseInt(startQuotaDate.substring(0,4))-1;
  1608. }
  1609. if (StringUtils.isNotEmpty(endQuotaDate)) {// 外部指定时间
  1610. nowYear = Integer.parseInt(endQuotaDate.substring(0,4));
  1611. dateFilter = "quotaDate >= '" + beforeYear + "-01-01' and quotaDate <= '" + nowYear + "-12-31'";
  1612. }
  1613. } else if ("2".equals(growthFlag)) { // 月增幅 没有传时间条件默认当前月份 计算前6个月数据 计算向前推7个月
  1614. lastDate.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号,当前日期既为本月第一天
  1615. endMonth =lastDate.getTime();
  1616. lastDate.add(Calendar.MONTH, - 7);
  1617. lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
  1618. firstMonth = lastDate.getTime();
  1619. // 如果有时间过滤条件,则按时间条件计算
  1620. if (StringUtils.isNotEmpty(startQuotaDate)) {
  1621. if (StringUtils.isNotEmpty(startQuotaDate)) {// 外部指定时间
  1622. firstMonth = sdf.parse(startQuotaDate);
  1623. lastDate.setTime(firstMonth);
  1624. lastDate.add(Calendar.MONTH, -1);//向前推一个月 用于计算最后一个月增幅
  1625. firstMonth = lastDate.getTime();
  1626. }
  1627. if (StringUtils.isNotEmpty(endQuotaDate)) {// 外部指定时间
  1628. endMonth = sdf.parse(endQuotaDate);
  1629. }
  1630. }
  1631. dateFilter = "quotaDate >= '" + sdf.format(firstMonth) + "' and quotaDate <= '" + sdf.format(endMonth) + "'";
  1632. }else if ("3".equals(growthFlag)) { // 季度增幅 没有传时间条件默认当前季度,计算查询向前推一个季度
  1633. int currentMonth = lastDate.get(Calendar.MONTH) + 1;
  1634. if (currentMonth >= 1 && currentMonth <= 3){
  1635. lastDate.set(Calendar.MONTH, 0);
  1636. }else if (currentMonth >= 4 && currentMonth <= 6){
  1637. lastDate.set(Calendar.MONTH, 3);
  1638. } else if (currentMonth >= 7 && currentMonth <= 9){
  1639. lastDate.set(Calendar.MONTH, 6);
  1640. } else if (currentMonth >= 10 && currentMonth <= 12){
  1641. lastDate.set(Calendar.MONTH, 9);
  1642. }
  1643. lastDate.set(Calendar.MONTH, 3);//此季度最后一天
  1644. lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
  1645. endMonth =lastDate.getTime();
  1646. lastDate.set(Calendar.MONTH, -6);//向前推一个季度 向前推3个月
  1647. lastDate.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号,当前日期既为本月第一天
  1648. firstMonth = lastDate.getTime();
  1649. // 如果有时间过滤条件,则按时间条件计算
  1650. if (StringUtils.isNotEmpty(startQuotaDate)) {
  1651. if (StringUtils.isNotEmpty(startQuotaDate)) {// 外部指定时间
  1652. firstMonth = sdf.parse(startQuotaDate);
  1653. lastDate.setTime(firstMonth);
  1654. lastDate.add(Calendar.MONTH, -3);//向前推一季度 用于计算最后一个季度增幅
  1655. firstMonth = lastDate.getTime();
  1656. }
  1657. if (StringUtils.isNotEmpty(endQuotaDate)) {// 外部指定时间
  1658. endMonth = sdf.parse(endQuotaDate);
  1659. }
  1660. }
  1661. dateFilter = "quotaDate >= '" + sdf.format(firstMonth) + "' and quotaDate <= '" + sdf.format(endMonth) + "'";
  1662. }
  1663. if(StringUtils.isNotEmpty(noDateFilter)){
  1664. filters = dateFilter + " and " + noDateFilter;
  1665. }else{
  1666. filters = dateFilter;
  1667. }
  1668. dimension = dateType;
  1669. List<Map<String, Object>> dataList = getSimpleQuotaReport(esConfig.getMolecular(), filters,dimension ,false , null);
  1670. if(dataList != null && dataList.size() > 0){
  1671. if(dateType.toLowerCase().equals("year")){
  1672. for(int i = nowYear ; i > beforeYear ;i--){
  1673. double current = 0;
  1674. double last = 0;
  1675. Map<String,Object> map = new HashMap<>();
  1676. map.put(dimension, i);
  1677. for(Map<String,Object> dataMap : dataList){
  1678. int y = Integer.valueOf(dataMap.get(dimension).toString());
  1679. if( y == i ){
  1680. map.put(firstColumnField, dataMap.get(firstColumnField));
  1681. current = Double.valueOf(dataMap.get(resultField).toString());
  1682. }
  1683. if( y == i-1 ){
  1684. last = Double.valueOf(dataMap.get(resultField).toString());
  1685. }
  1686. }
  1687. if(last == 0){
  1688. map.put(resultField,"--");
  1689. }else {
  1690. double precent = (current - last)/last*100;
  1691. map.put(resultField,precent-0 ==0 ? 0 : basesicUtil.decimalPointHandle(precent));
  1692. }
  1693. resultList.add(map);
  1694. if(i-beforeYear <= 0){
  1695. break;
  1696. }
  1697. }
  1698. }else if(dateType.toLowerCase().equals("quarter")){
  1699. String startQuarter = "";
  1700. String endQuarter = "";
  1701. lastDate.setTime(firstMonth);
  1702. Date firstDate = lastDate.getTime();
  1703. int currentMonth = lastDate.get(Calendar.MONTH) + 1;
  1704. if (currentMonth >= 1 && currentMonth <= 3){
  1705. startQuarter = sdf.format(firstDate).substring(0,4)+"年1季度";
  1706. }else if (currentMonth >= 4 && currentMonth <= 6){
  1707. startQuarter = sdf.format(firstDate).substring(0,4)+"年2季度";
  1708. } else if (currentMonth >= 7 && currentMonth <= 9){
  1709. startQuarter = sdf.format(firstDate).substring(0,4)+"年3季度";
  1710. } else if (currentMonth >= 10 && currentMonth <= 12){
  1711. startQuarter = sdf.format(firstDate).substring(0,4)+"年4季度";
  1712. }
  1713. lastDate.setTime(endMonth);
  1714. Date downDate = lastDate.getTime();
  1715. if (currentMonth >= 1 && currentMonth <= 3){
  1716. endQuarter = sdf.format(downDate).substring(0,4)+"年1季度";
  1717. }else if (currentMonth >= 4 && currentMonth <= 6){
  1718. endQuarter = sdf.format(downDate).substring(0,4)+"年2季度";
  1719. } else if (currentMonth >= 7 && currentMonth <= 9){
  1720. endQuarter = sdf.format(downDate).substring(0,4)+"年3季度";
  1721. } else if (currentMonth >= 10 && currentMonth <= 12){
  1722. endQuarter = sdf.format(downDate).substring(0,4)+"年4季度";
  1723. }
  1724. String lastQuarter = "";
  1725. while ( !startQuarter.equals(lastQuarter)){
  1726. int eYear = Integer.valueOf(endQuarter.substring(0, 4));
  1727. int eQuarter = Integer.valueOf(endQuarter.substring(5, 6));
  1728. endQuarter = eYear+ "年" + eQuarter + "季度";
  1729. if( !endQuarter.equals(startQuarter)){
  1730. if(eQuarter == 1){
  1731. eYear--;
  1732. eQuarter = 4;
  1733. }else {
  1734. eQuarter--;
  1735. }
  1736. }
  1737. lastQuarter = eYear + "年" + eQuarter + "季度";
  1738. double current = 0;
  1739. double last = 0;
  1740. Map<String,Object> map = new HashMap<>();
  1741. for(Map<String,Object> dataMap : dataList){
  1742. if(dataMap.get(dimension) != null){
  1743. String val = dataMap.get(dimension).toString();
  1744. if(val.equals(endQuarter) ){
  1745. map.put(firstColumnField, endQuarter);
  1746. map.put(dimension, endQuarter);
  1747. current = Double.valueOf(dataMap.get(resultField).toString());
  1748. }
  1749. if(val.equals(lastQuarter) ){
  1750. last = Double.valueOf(dataMap.get(resultField).toString());
  1751. }
  1752. }
  1753. }
  1754. if(last == 0){
  1755. map.put(resultField,"--");
  1756. }else {
  1757. double precent = (current - last)/last*100;
  1758. if(precent == 0){
  1759. map.put(resultField,0);
  1760. }else {
  1761. map.put(resultField,precent-0 ==0 ? 0 : basesicUtil.decimalPointHandle(precent));
  1762. }
  1763. }
  1764. resultList.add(map);
  1765. endQuarter = lastQuarter;
  1766. }
  1767. }else if(dateType.toLowerCase().equals("month")){
  1768. String starthMonthStr = sdf.format(firstMonth).substring(0,7);
  1769. String endMonthStr = sdf.format(endMonth).substring(0,7);
  1770. while ( !starthMonthStr.equals(endMonthStr)){
  1771. double current = 0;
  1772. double last = 0;
  1773. Map<String,Object> map = new HashMap<>();
  1774. String nowMonthStr = endMonthStr;
  1775. String lastMontStr = "";
  1776. if( !starthMonthStr.equals(endMonthStr)){
  1777. Calendar calendar = Calendar.getInstance();
  1778. calendar.setTime(sdf.parse(endMonthStr + "-01"));
  1779. calendar.add(Calendar.MONTH, -1);//当前时间前去一个月,即一个月前的时间
  1780. lastMontStr = sdf.format(calendar.getTime()).substring(0,7);
  1781. }else {
  1782. lastMontStr = starthMonthStr;
  1783. }
  1784. for(Map<String,Object> dataMap : dataList){
  1785. if(dataMap.get(dimension) != null){
  1786. String val = dataMap.get(dimension).toString();
  1787. if(val.equals(nowMonthStr) ){
  1788. map.put(firstColumnField, val);
  1789. map.put(dimension, val);
  1790. current = Double.valueOf(dataMap.get(resultField).toString());
  1791. }
  1792. if(val.equals(lastMontStr) ){
  1793. last = Double.valueOf(dataMap.get(resultField).toString());
  1794. }
  1795. }
  1796. }
  1797. if(last == 0){
  1798. map.put(resultField,"--");
  1799. }else {
  1800. double precent = (current - last)/last*100;
  1801. if(precent == 0){
  1802. map.put(resultField,0);
  1803. }else {
  1804. map.put(resultField,precent-0 ==0 ? 0 : basesicUtil.decimalPointHandle(precent));
  1805. }
  1806. }
  1807. resultList.add(map);
  1808. endMonthStr = lastMontStr;
  1809. }
  1810. }
  1811. }
  1812. if(resultList != null ){
  1813. Collections.reverse(resultList); // 倒序排列
  1814. }
  1815. return resultList;
  1816. }
  1817. }
  1818. public String filtersExchangeHandle(String filters, EsConfig esConfig) {
  1819. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  1820. String otherFilter = "";
  1821. Calendar lastDate = Calendar.getInstance();
  1822. lastDate.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号,当前日期既为本月第一天
  1823. String firstDay = sdf.format(lastDate.getTime());
  1824. lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
  1825. String lastDay = sdf.format(lastDate.getTime());
  1826. lastDate.add(Calendar.MONTH, -1);
  1827. lastDate.set(Calendar.DAY_OF_MONTH, 1);
  1828. String preMonthFirstDay = sdf.format(lastDate.getTime());
  1829. lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
  1830. String preMonthLastDay = sdf.format(lastDate.getTime());
  1831. if (StringUtils.isNotEmpty(filters)) {
  1832. String[] split = filters.split("and");
  1833. for (String s : split) {
  1834. // 拼接非quotaDate的过滤条件
  1835. if (!s.contains(quotaDateField)) {
  1836. otherFilter += s + " and ";
  1837. }
  1838. }
  1839. }
  1840. // 如果有时间过滤条件,则计算时间条件计算
  1841. if (StringUtils.isNotEmpty(filters) && filters.contains(quotaDateField)) {
  1842. Calendar cal = Calendar.getInstance();
  1843. boolean b = filters.indexOf("'") > -1;
  1844. int start = b ? filters.indexOf("'") : filters.indexOf("\"");
  1845. String condition = filters.substring(start + 1, start + 5); // 获取年份
  1846. int year = Integer.parseInt(condition);
  1847. String condition2 = filters.substring(filters.indexOf("-") + 1, filters.indexOf("-") + 3); // 获取月份
  1848. int month = Integer.parseInt(condition2);
  1849. cal.set(Calendar.YEAR, year);
  1850. cal.set(Calendar.MONTH, month - 1);
  1851. cal.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号,当前日期既为本月第一天
  1852. firstDay = sdf.format(cal.getTime());
  1853. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  1854. lastDay = sdf.format(cal.getTime());
  1855. cal.add(Calendar.MONTH, -1);
  1856. cal.set(Calendar.DAY_OF_MONTH, 1);
  1857. preMonthFirstDay = sdf.format(cal.getTime());
  1858. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
  1859. preMonthLastDay = sdf.format(cal.getTime());
  1860. }
  1861. if ("1".equals(esConfig.getIncrementFlag())) {
  1862. // 上月
  1863. filters = "quotaDate >= '" + preMonthFirstDay + "' and quotaDate <= '" + preMonthLastDay + "'";
  1864. } else if ("2".equals(esConfig.getIncrementFlag())) {
  1865. // 当前月
  1866. filters = "quotaDate >= '" + firstDay + "' and quotaDate <= '" + lastDay + "'";
  1867. }
  1868. if (StringUtils.isNotEmpty(esConfig.getFilter())) {
  1869. filters += " and " + esConfig.getFilter();
  1870. }
  1871. if (StringUtils.isNotEmpty(otherFilter)) {
  1872. filters = otherFilter + filters;
  1873. }
  1874. return filters;
  1875. }
  1876. public Double caculateResult(List<Map<String, Object>> listMap) {
  1877. Map<String, Double> map = new HashMap<>();
  1878. map.put("result", 0d);
  1879. listMap.forEach(item -> {
  1880. item.forEach((k,v) -> {
  1881. if ("result".equals(k)) {
  1882. map.put("result", map.get("result") + Double.parseDouble(v + ""));
  1883. }
  1884. });
  1885. });
  1886. return map.get("result");
  1887. }
  1888. public Envelop searchEsByParam(String eventType, String time, Integer month) {
  1889. Envelop envelop = new Envelop();
  1890. String start = "", end = "";
  1891. StringBuffer sb = new StringBuffer();
  1892. List<String> xData = new ArrayList<>(); // x轴坐标
  1893. List<String> yData = new ArrayList<>(); // x轴坐标对应的值
  1894. // 设置查询时间范围
  1895. if (StringUtils.isNotEmpty(time)) {
  1896. SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd'T'23:59:59'Z'");
  1897. Date dateTime = DateUtil.strToDate(time);
  1898. Calendar calendar = Calendar.getInstance();
  1899. calendar.setTime(dateTime);
  1900. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
  1901. end = dfs.format(calendar.getTime());
  1902. calendar.add(Calendar.MONTH, -(month - 1));
  1903. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
  1904. start = DateUtil.utcToDate(calendar.getTime());
  1905. }
  1906. if ("0".equals(eventType)) {
  1907. sb.append("SELECT count(*) FROM medical_service_index where quotaCode='HC041100' and slaveKey1=0 and quotaDate>='")
  1908. .append(start).append("' and quotaDate<='").append(end).append("' group by date_histogram(field='quotaDate', 'interval'='month')");
  1909. } else if ("1".equals(eventType)) {
  1910. sb.append("SELECT count(*) FROM medical_service_index where quotaCode='HC041103' and quotaDate>='")
  1911. .append(start).append("' and quotaDate<='").append(end).append("' group by date_histogram(field='quotaDate', 'interval'='month')");
  1912. }
  1913. log.info(sb.toString());
  1914. List<Map<String, Object>> list = elasticsearchUtil.excuteDataModel(sb.toString());
  1915. list.forEach(one -> {
  1916. xData.add((one.get("date_histogram(field=quotaDate,interval=month)") + "").substring(0,7));
  1917. yData.add(one.get("COUNT(*)") + "");
  1918. });
  1919. envelop.setSuccessFlg(true);
  1920. envelop.setObj(xData);
  1921. envelop.setDetailModelList(yData);
  1922. return envelop;
  1923. }
  1924. }