12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937 |
- package com.yihu.quota.service.quota;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.yihu.ehr.util.datetime.DateUtil;
- import com.yihu.quota.dao.jpa.TjQuotaDao;
- import com.yihu.quota.dao.jpa.TjQuotaGovProvisionDao;
- import com.yihu.quota.etl.extract.es.EsResultExtract;
- import com.yihu.quota.etl.model.EsConfig;
- import com.yihu.quota.etl.util.ElasticsearchUtil;
- import com.yihu.quota.model.jpa.TjQuota;
- import com.yihu.quota.model.jpa.dimension.TjQuotaDimensionMain;
- import com.yihu.quota.model.jpa.dimension.TjQuotaDimensionSlave;
- import com.yihu.quota.model.jpa.save.TjQuotaDataSave;
- import com.yihu.quota.model.jpa.source.TjQuotaDataSource;
- import com.yihu.quota.service.dimension.TjDimensionMainService;
- import com.yihu.quota.service.dimension.TjDimensionSlaveService;
- import com.yihu.quota.service.orgHealthCategory.OrgHealthCategoryStatisticsService;
- import com.yihu.quota.service.save.TjDataSaveService;
- import com.yihu.quota.service.singledisease.SingleDiseaseService;
- import com.yihu.quota.service.source.TjDataSourceService;
- import com.yihu.quota.util.BasesicUtil;
- import com.yihu.quota.vo.DictModel;
- import com.yihu.quota.vo.SaveModel;
- import net.sf.json.JSONObject;
- import org.apache.commons.lang.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.jdbc.core.BeanPropertyRowMapper;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.stereotype.Service;
- import java.math.BigDecimal;
- import java.text.NumberFormat;
- import java.text.SimpleDateFormat;
- import java.util.*;
- /**
- * Created by janseny on 2018/01/15.
- */
- @Service
- public class BaseStatistsService {
- Logger log = LoggerFactory.getLogger(BaseStatistsService.class);
- @Autowired
- private TjQuotaDao quotaDao;
- @Autowired
- private EsResultExtract esResultExtract;
- @Autowired
- private TjDimensionMainService tjDimensionMainService;
- @Autowired
- private TjDimensionSlaveService tjDimensionSlaveService;
- @Autowired
- private JdbcTemplate jdbcTemplate;
- @Autowired
- private OrgHealthCategoryStatisticsService orgHealthCategoryStatisticsService;
- @Autowired
- private TjDataSourceService dataSourceService;
- @Autowired
- private ObjectMapper objectMapper;
- @Autowired
- private QuotaService quotaService;
- @Autowired
- private ElasticsearchUtil elasticsearchUtil;
- @Autowired
- private SingleDiseaseService singleDiseaseService;
- @Autowired
- private TjQuotaGovProvisionDao tjQuotaGovProvisionDao;
- @Autowired
- private TjDataSaveService dataSaveService;
- private static String orgHealthCategory = "orgHealthCategory";
- public static String orgHealthCategoryCode = "orgHealthCategoryCode";
- public static String resultField = "result";
- public static String quotaDateField = "quotaDate";
- public static String firstColumnField = "firstColumn";
- /**
- * 特殊机构类型 根据 上级基础指标code 获取基础数据集
- * @param code
- * @param filter
- * @param dimension
- * @return
- * @throws Exception
- */
- public List<Map<String, Object>> getOrgHealthCategoryQuotaResultList(String code,String dimension,String filter, String top) throws Exception {
- List<Map<String, Object>> dimenListResult = getOrgHealthCategoryAggregationResult(code, dimension, filter, top);
- return dimenListResult;
- }
- /**
- * 根据指标code 和维度及条件 分组获取指标查询结果集
- * @param code
- * @param filter
- * @param dimension
- * @return
- * @throws Exception
- */
- public List<Map<String, Object>> getQuotaResultList(String code,String dimension,String filter,String dateType, String top) throws Exception {
- code = getBasicQuota(code);
- List<Map<String, Object>> dimenListResult = new ArrayList<>();
- if(StringUtils.isNotEmpty(dateType)){
- dimenListResult = getTimeAggregationResult(code, dimension, filter, dateType);
- }else {
- dimenListResult = getAggregationResult(code, dimension, filter, top);
- }
- return dimenListResult;
- }
- /**
- * 查找指标的最底层指标code
- * @param quotaCode
- * @return
- */
- public String getBasicQuota(String quotaCode){
- String code = quotaCode;
- TjQuota tjQuota = quotaService.findByCode(quotaCode);
- if( tjQuota != null && StringUtils.isNotEmpty(tjQuota.getResultGetType())){
- if (tjQuota.getResultGetType().equals("2")){//二次统计
- TjQuotaDataSource quotaDataSorce = dataSourceService.findSourceByQuotaCode(quotaCode);
- if(quotaDataSorce != null){
- JSONObject obj = new JSONObject().fromObject(quotaDataSorce.getConfigJson());
- EsConfig esConfig= (EsConfig) JSONObject.toBean(obj,EsConfig.class);
- if(StringUtils.isNotEmpty(esConfig.getSuperiorBaseQuotaCode()) && StringUtils.isEmpty(esConfig.getEspecialType()) ){
- code = esConfig.getSuperiorBaseQuotaCode();
- }
- }
- }
- }
- return code;
- }
- public List<Map<String, Object>> addQuota(String addFirstQuotaCode,String firstFilter, String addSecondQuotaCode, String secondFilter,String operation ,String dimension,String dateType, String top) throws Exception {
- List<Map<String, Object>> firstList = getQuotaResultList(addFirstQuotaCode,dimension,firstFilter,dateType, top);
- List<Map<String, Object>> secondList = getQuotaResultList(addSecondQuotaCode,dimension,secondFilter,dateType, top);
- dimension = StringUtils.isNotEmpty(dateType)? (StringUtils.isNotEmpty(dimension)? dimension +";"+dateType : dateType):dimension;
- List<Map<String, Object>> addition = addition(dimension, firstList, secondList, Integer.valueOf(operation));
- if (StringUtils.isNotEmpty(top)) {
- addition = sortResultList(addition, top);
- }
- return addition;
- }
- public List<Map<String, Object>> addition(String dimension, List<Map<String, Object>> firstList, List<Map<String, Object>> secondList,int operation){
- List<Map<String, Object>> addResultList = new ArrayList<>();
- NumberFormat nf = NumberFormat.getInstance();
- nf.setGroupingUsed(false);
- nf.setMaximumFractionDigits(2);
- List<Map<String, Object>> otherList = new ArrayList<>();
- otherList = secondList;
- String [] moleDimensions = dimension.split(";");
- for(Map<String, Object> firstMap :firstList) {
- if (null != firstMap && firstMap.size() > 0 ) {
- Map<String, Object> map = new HashMap<>();
- double firstResultVal = Double.valueOf(firstMap.get("result") == null ? "0" : firstMap.get(resultField).toString());
- map.put(firstColumnField, firstMap.get(firstColumnField));
- String firstKeyVal = "";
- for(int i = 0 ;i < moleDimensions.length ; i++){
- if(i == 0){
- firstKeyVal = firstMap.get(moleDimensions[i]).toString();
- }else {
- firstKeyVal = firstKeyVal + "-" + firstMap.get(moleDimensions[i]).toString() ;
- }
- map.put(moleDimensions[i], firstMap.get(moleDimensions[i]).toString());
- if(moleDimensions[i].equals("town") || moleDimensions[i].equals("org") ){
- if(firstMap.get(moleDimensions[i] + "Code") != null){
- map.put(moleDimensions[i]+ "Code", firstMap.get(moleDimensions[i] + "Code").toString());
- }
- }
- }
- boolean pflag = true;
- for(Map<String, Object> secondMap :secondList) {
- String secondKeyVal = "";
- String [] dimeDimensions = dimension.split(";");
- for(int i = 0 ;i < dimeDimensions.length ; i++){
- if(i == 0){
- secondKeyVal = secondMap.get(dimeDimensions[i]).toString();
- }else {
- secondKeyVal = secondKeyVal + "-" + secondMap.get(dimeDimensions[i]).toString() ;
- }
- }
- if(firstKeyVal.equals(secondKeyVal) || "quotaName".equals(dimension)){ // 如果维度是quotaName,则进入逻辑
- double point = 0;
- double dimeResultVal = Double.valueOf(secondMap.get(resultField).toString());
- BigDecimal first = new BigDecimal(Double.toString(firstResultVal));
- BigDecimal second = new BigDecimal(Double.toString(dimeResultVal));
- if(operation == 1){ //1 加法 默认
- point = first.add(second).doubleValue();
- }else if(operation == 2){ //2 减法
- point = first.subtract(second).doubleValue();
- }
- map.put(resultField,nf.format(point));
- addResultList.add(map);
- pflag = false;
- otherList.remove(secondMap);
- break;
- }
- }
- if(pflag){
- map.put(resultField, nf.format(firstResultVal));
- addResultList.add(map);
- }
- }
- }
- if (null != otherList && otherList.size() > 0) {
- for(Map<String, Object> secondMap : otherList) {
- Map<String, Object> map = new HashMap<>();
- map.put(firstColumnField, secondMap.get(firstColumnField));
- for(int i = 0 ;i < moleDimensions.length ; i++){
- map.put(moleDimensions[i], secondMap.get(moleDimensions[i]).toString());
- }
- double point = 0;
- double secondResultVal = Double.valueOf(secondMap.get("result") == null ? "0" : secondMap.get(resultField).toString());
- if (secondResultVal != 0) {
- if(operation == 1){ //1 加法 默认
- point = secondResultVal ;
- }else if(operation == 2){ //2 减法
- point = -secondResultVal;
- }
- }
- map.put(resultField, nf.format(point));
- addResultList.add(map);
- }
- }
- //检查后面指标的维度是否全部有 累加进去
- /*Map<String, Object> addResuDimenMap = new HashMap<>();
- for(int k = 0;k < addResultList.size();k++) {
- Map<String, Object> addResultMap = addResultList.get(k);
- String addDimenStr = "";
- for (int i = 0; i < moleDimensions.length; i++) {
- addDimenStr += addResultMap.get(moleDimensions[i]).toString() + "-";
- }
- addResuDimenMap.put(addDimenStr,addDimenStr);
- }
- for(Map<String, Object> secondMap :secondList) {
- String secondDimenStr = "";
- String addDimenStr = "";
- for(int i = 0 ;i < moleDimensions.length ; i++){
- secondDimenStr += secondMap.get(moleDimensions[i]).toString() + "-";
- }
- if( !addResuDimenMap.containsKey(secondDimenStr)){
- if( !addDimenStr.equals(secondDimenStr)){
- Map<String, Object> map = new HashMap<>();
- float dimeResultVal = Float.valueOf(secondMap.get(resultField).toString());
- map.put(resultField,df.format(dimeResultVal));
- map.put(firstColumnField, secondMap.get(firstColumnField));
- for(int i = 0 ;i < moleDimensions.length ; i++){
- map.put(moleDimensions[i], secondMap.get(moleDimensions[i]).toString());
- }
- addResultList.add(map);
- }
- }
- }*/
- return addResultList;
- }
- public List<Map<String, Object>> sortResultList(List<Map<String, Object>> listMap, String top) {
- if (null != listMap && listMap.size() > 0) {
- Collections.sort(listMap, new Comparator<Map<String, Object>>() {
- @Override
- public int compare(Map<String, Object> o1, Map<String, Object> o2) {
- // 根据result进行降序
- double result = Double.parseDouble(o1.get("result") + "");
- double result2 = Double.parseDouble(o2.get("result") + "");
- double v = result2 - result;
- return v > 0 ? 1 : v == 0 ? 0 : -1;
- }
- });
- if (StringUtils.isNotEmpty(top)) {
- int t = Integer.parseInt(top);
- listMap = listMap.subList(0, listMap.size() > t ? t :listMap.size());
- }
- }
- return listMap;
- }
- /**
- * 两个维度相同指标除法运算
- * @param molecular
- * @param denominator
- * @param dimension
- * @param molecularFilter
- * @param denominatorFilters
- * @param operation
- * @param operationValue
- * @return
- * @throws Exception
- */
- public List<Map<String, Object>> divisionQuota(String molecular, String denominator, String dimension,
- String molecularFilter,String denominatorFilters,String operation,String operationValue,String dateType, String top) throws Exception {
- if(StringUtils.isEmpty(dimension) && StringUtils.isNotEmpty(dateType)){
- dimension = dateType;
- }
- List<Map<String, Object>> moleList = getSimpleQuotaReport(molecular, molecularFilter,dimension ,false , top);
- List<Map<String, Object>> denoList = getSimpleQuotaReport(denominator,denominatorFilters,dimension,false, null);
- dimension = StringUtils.isNotEmpty(dateType)? (StringUtils.isNotEmpty(dimension)? dimension +";"+dateType : dateType):dimension;
- return division(dimension,moleList,denoList,Integer.valueOf(operation),Integer.valueOf(operationValue));
- }
- /**
- * 指标除法运算 分母按年份获取总数
- * @param molecular 分子
- * @param dimension 分母
- * @param molecularFilter 分子条件
- * @param denominatorFilter 分母条件
- * @param operation
- * @param operationValue
- * @return
- * @throws Exception
- */
- public List<Map<String, Object>> divisionQuotaDenoConstant(String molecular, String dimension,String molecularFilter,String denominatorFilter,
- String operation,String operationValue,String dateType , String top) throws Exception {
- Double denominatorVal = 0.0;
- List<Map<String, Object>> moleList = getQuotaResultList(molecular,dimension,molecularFilter,dateType, top);
- String year = DateUtil.getYearFromYMD(new Date());
- String town = "";
- if(StringUtils.isNotEmpty(denominatorFilter)){
- String [] filter = denominatorFilter.split("and");
- if(filter.length > 0 ){
- for(String key : filter){
- if(key.contains(quotaDateField)){
- year = key.substring(key.indexOf("'")+1,key.indexOf("'")+5);//quotaDate >= '2018-03-01'
- }
- if(key.contains("town")){
- town = key.substring(key.indexOf("=")+1);
- }
- }
- }
- }
- Long sumValue = null;
- if(StringUtils.isNotEmpty(town)){
- sumValue = tjQuotaGovProvisionDao.getSumByDistrict(Long.parseLong(town),year);
- }else {
- sumValue = tjQuotaGovProvisionDao.getSumByDistrict(year);
- }
- // 获取分母的数值
- if(sumValue != null && sumValue.doubleValue() != 0){
- denominatorVal = sumValue.doubleValue();
- return divisionDenoConstant(dimension, moleList, denominatorVal, Integer.valueOf(operation), Integer.valueOf(operationValue));
- }else {
- return null;
- }
- }
- /**
- * 指标结果相除
- * 除法运算 分母按年份获取总数
- * @param dimension 维度
- * @param moleList 分子
- * @param denominatorVal 分母 数值
- * @param operation 运算方式 1 乘法 2 除法
- * @param operationValue 运算参数值
- *
- */
- public List<Map<String, Object>> divisionDenoConstant(String dimension, List<Map<String, Object>> moleList, Double denominatorVal,int operation,int operationValue) {
- List<Map<String, Object>> divisionResultList = new ArrayList<>();
- for (Map<String, Object> moleMap : moleList) {
- Map<String, Object> map = new HashMap<>();
- double moleResultVal = Double.valueOf(moleMap.get(resultField).toString());
- String moleKeyVal = "";
- String [] moleDimensions = dimension.split(";");
- for(int i = 0 ;i < moleDimensions.length ; i++){
- if(i == 0){
- moleKeyVal = moleMap.get(moleDimensions[i]).toString();
- }else {
- moleKeyVal = moleKeyVal + "-" + moleMap.get(moleDimensions[i]).toString() ;
- }
- map.put(moleDimensions[i], moleMap.get(moleDimensions[i]).toString());
- map.put(moleDimensions[i]+"Name", moleMap.get(moleDimensions[i]).toString());
- }
- if (moleResultVal == 0) {
- map.put(resultField,0);
- divisionResultList.add(map);
- } else {
- double point = 0;
- NumberFormat df = NumberFormat.getInstance();
- df.setGroupingUsed(false);
- df.setMaximumFractionDigits(2);
- if (operation == 1) {
- point = (moleResultVal / denominatorVal) * operationValue;
- } else if (operation == 2) {
- point = (moleResultVal / denominatorVal) / operationValue;
- }
- map.put(resultField, df.format(point));
- divisionResultList.add(map);
- }
- }
- return divisionResultList;
- }
- /**
- * 指标结果相除
- * @param dimension 维度
- * @param moleList 分子
- * @param denoList 分母
- * @param operation 运算方式 1 乘法 2 除法
- * @param operationValue 运算参数值
- *
- */
- public List<Map<String, Object>> division(String dimension, List<Map<String, Object>> moleList, List<Map<String, Object>> denoList,int operation,int operationValue){
- List<Map<String, Object>> divisionResultList = new ArrayList<>();
- for(Map<String, Object> moleMap :moleList) {
- if (null != moleMap && moleMap.size() > 0 ) {
- Map<String, Object> map = new HashMap<>();
- double moleResultVal = Double.valueOf(moleMap.get(resultField) == null ? "0" : moleMap.get(resultField).toString());
- String moleKeyVal = "";
- String [] moleDimensions = dimension.split(";");
- for(int i = 0 ;i < moleDimensions.length ; i++){
- if(i == 0){
- moleKeyVal = moleMap.get(moleDimensions[i]).toString();
- }else {
- moleKeyVal = moleKeyVal + "-" + moleMap.get(moleDimensions[i]).toString() ;
- }
- map.put(firstColumnField, moleMap.get(firstColumnField));
- map.put(moleDimensions[i], moleMap.get(moleDimensions[i]).toString());
- map.put(moleDimensions[i]+"Name", moleMap.get(moleDimensions[i]).toString());
- if("town".equals(moleDimensions[i]) || "org".equals(moleDimensions[i])) {
- if(moleMap.get(moleDimensions[i] + "Code") != null) {
- map.put(moleDimensions[i]+ "Code", moleMap.get(moleDimensions[i] + "Code").toString());
- }
- }
- }
- if (moleResultVal == 0) {
- map.put(resultField,0);
- divisionResultList.add(map);
- } else {
- for(Map<String, Object> denoMap :denoList) {
- String dimenKeyVal = "";
- String [] dimeDimensions = dimension.split(";");
- for(int i = 0 ;i < dimeDimensions.length ; i++){
- if(i == 0){
- dimenKeyVal = denoMap.get(dimeDimensions[i]).toString();
- }else {
- dimenKeyVal = dimenKeyVal + "-" + denoMap.get(dimeDimensions[i]).toString() ;
- }
- }
- if(moleKeyVal.equals(dimenKeyVal)){
- double point = 0;
- NumberFormat df = NumberFormat.getInstance();
- df.setGroupingUsed(false);
- df.setMaximumFractionDigits(2);
- float dimeResultVal = Float.valueOf(denoMap.get(resultField).toString());
- if(dimeResultVal != 0){
- if(operation == 1){
- point = (moleResultVal/dimeResultVal) * operationValue;
- }else if(operation == 2){
- point = (moleResultVal/dimeResultVal) / operationValue;
- }
- }
- map.put(resultField,df.format(point));
- divisionResultList.add(map);
- break;
- }
- }
- }
- }
- }
- return divisionResultList;
- }
- /**
- * 特殊机构类别 根据条件查询结果
- * @param code
- * @param filters
- * @param dateType 日期类型
- * @param isTrunTree 是否转为机构类型树状机构
- * @throws Exception
- */
- public List<Map<String, Object>> getOrgHealthCategory(String code,String filters,String dateType,boolean isTrunTree, String top) throws Exception {
- List<Map<String, Object>> dimenListResult = new ArrayList<>();
- if(dateType != null && (dateType.contains("year") || dateType.contains("quarter")|| dateType.contains("month") || dateType.contains("day"))){
- dimenListResult = getTimeAggregationResult(code,orgHealthCategoryCode,filters,dateType);//dimension 维度为 year,month,day
- }else {
- dimenListResult = getAggregationResult(code, orgHealthCategoryCode, filters, top);
- }
- if(isTrunTree){
- List<Map<String, Object>> orgHealthCategoryList = orgHealthCategoryStatisticsService.getOrgHealthCategoryTreeByPid(-1);
- List<Map<String, Object>> resultList = setResult(code,orgHealthCategoryList,dimenListResult,dateType);
- return resultList;
- }else {
- return dimenListResult;
- }
- }
- /**
- * 递归循环 查询机构类型对应的名称和父节点
- * @param orgHealthCategoryList
- * @param dimenListResult
- * @param
- * @return
- */
- public List<Map<String,Object>> setResult(String quotaCode,List<Map<String,Object>> orgHealthCategoryList,List<Map<String, Object>> dimenListResult,String dateType){
- List<Map<String,Object>> result = new ArrayList<>();
- for(int i=0 ; i < orgHealthCategoryList.size() ; i++ ){
- Map<String,Object> mapCategory = orgHealthCategoryList.get(i);
- String code = mapCategory.get("code").toString();
- boolean notExitFalg = true;
- for(Map<String, Object> dimenMap : dimenListResult){
- boolean flag = false;
- if(dimenMap.get(orgHealthCategoryCode) != null){
- flag = dimenMap.get(orgHealthCategoryCode).equals(code);
- }
- if(dimenMap.get(code) != null || flag ){
- // mapCategory.putAll(dimenMap);
- if(dimenMap.containsKey(code)){
- mapCategory.put(code,dimenMap.get(code));
- mapCategory.put(resultField,dimenMap.get(resultField)!=null ? dimenMap.get(resultField):dimenMap.get(code));
- }
- if(StringUtils.isNotEmpty(dateType)){
- mapCategory.put(dimenMap.get(dateType).toString(),dimenMap.get(resultField));
- }
- mapCategory.put(quotaCode,dimenMap.get(resultField));
- break;
- }
- }
- if(notExitFalg){
- mapCategory.put(resultField,0);
- mapCategory.put(quotaCode,0);
- }
- mapCategory.put(firstColumnField,mapCategory.get("text"));
- result.add(mapCategory);
- if(mapCategory.get("children") != null){
- List<Map<String,Object>> childrenOrgHealthCategoryList = (List<Map<String, Object>>) mapCategory.get("children");
- mapCategory.put("children",setResult(quotaCode,childrenOrgHealthCategoryList,dimenListResult,dateType));
- }
- }
- return result;
- }
- // /**
- // * 递归循环 查询机构类型对应的名称和父节点
- // * @param orgHealthCategoryList
- // * @param dimenListResult
- // * @param
- // * @return
- // */
- // public List<Map<String,Object>> setResultAllDimenMap(String quotaCode,List<Map<String,Object>> orgHealthCategoryList,List<Map<String, Object>> dimenListResult,String dateType){
- // List<Map<String,Object>> result = new ArrayList<>();
- // for(int i=0 ; i < orgHealthCategoryList.size() ; i++ ){
- // Map<String,Object> mapCategory = orgHealthCategoryList.get(i);
- // String code = mapCategory.get("code").toString();
- // boolean notExitFalg = true;
- // for(Map<String, Object> dimenMap : dimenListResult){
- // boolean flag = false;
- // if(dimenMap.get(orgHealthCategoryCode) != null){
- // flag = dimenMap.get(orgHealthCategoryCode).equals(code);
- // }
- // if(dimenMap.get(code) != null || flag ){
- // //补充所有信息
- // mapCategory.putAll(dimenMap);
- // if(dimenMap.containsKey(code)){
- // mapCategory.put(code,dimenMap.get(code));
- // mapCategory.put(resultField,dimenMap.get(resultField)!=null ? dimenMap.get(resultField):dimenMap.get(code));
- // }
- // if(StringUtils.isNotEmpty(dateType)){
- // mapCategory.put(dimenMap.get(dateType).toString(),dimenMap.get(resultField));
- // }
- // mapCategory.put(quotaCode,dimenMap.get(resultField));
- // notExitFalg = false;
- // break;
- // }
- // }
- // if(notExitFalg){
- // mapCategory.put(resultField,0);
- // mapCategory.put(quotaCode,0);
- // }
- // mapCategory.put(firstColumnField,mapCategory.get("text"));
- // result.add(mapCategory);
- // if(mapCategory.get("children") != null){
- // List<Map<String,Object>> childrenOrgHealthCategoryList = (List<Map<String, Object>>) mapCategory.get("children");
- // mapCategory.put("children",setResultAllDimenMap(quotaCode, childrenOrgHealthCategoryList, dimenListResult, dateType));
- // }
- // }
- // return result;
- // }
- /**
- * 递归循环 计算各目录结构的值
- * @param orgHealthCategoryList
- * @param dimenListResult
- * @param
- * @return
- */
- public List<Map<String,Object>> allCategoryResultMap(List<String> quotaCodes,List<Map<String,Object>> orgHealthCategoryList,List<Map<String, Object>> dimenListResult ){
- List<Map<String,Object>> resultMap = new ArrayList<>();
- for(int i=0 ; i < orgHealthCategoryList.size() ; i++ ){
- Map<String,Object> mapCategory = orgHealthCategoryList.get(i);
- Map<String,Object> map = new HashMap<>();
- double parentResult = 0;
- mapCategory.put(firstColumnField,mapCategory.get("text"));
- map = getParentAllChildren(quotaCodes,mapCategory,map, dimenListResult,parentResult);
- if(map == null || map.size() == 0 ){
- for(String quotaCode : quotaCodes){
- map.put(quotaCode,0);
- }
- map.put(resultField,0);
- }
- mapCategory.putAll(map);
- resultMap.add(mapCategory);
- if(mapCategory.get("children") != null){
- List<Map<String,Object>> childrenOrgHealthCategoryList = (List<Map<String, Object>>) mapCategory.get("children");
- mapCategory.put("children",allCategoryResultMap(quotaCodes,childrenOrgHealthCategoryList,dimenListResult));
- }
- }
- return resultMap;
- }
- //获取该节点下所有末节点的结果和
- public Map<String,Object> getParentAllChildren(List<String> quotaCodes, Map<String,Object> mapCategory,Map<String,Object> returnMap, List<Map<String, Object>> dimenListResult, double parentResult ){
- try {
- NumberFormat df = NumberFormat.getInstance();
- df.setGroupingUsed(false);
- df.setMaximumFractionDigits(2);
- boolean childrenFlag = false;
- if(mapCategory.get("children") != null){
- List<Map<String,Object>> childrenOrgHealthCategoryList = (List<Map<String, Object>>) mapCategory.get("children");
- if(childrenOrgHealthCategoryList != null && childrenOrgHealthCategoryList.size() > 0){
- childrenFlag = true;
- }
- }
- if(childrenFlag){
- List<Map<String,Object>> childrenOrgHealthCategoryList = (List<Map<String, Object>>) mapCategory.get("children");
- for(int j=0 ; j < childrenOrgHealthCategoryList.size() ; j++ ){
- Map<String,Object> childrenMapCategory = childrenOrgHealthCategoryList.get(j);
- returnMap = getParentAllChildren(quotaCodes ,childrenMapCategory,returnMap, dimenListResult,parentResult);
- }
- }else{
- for(Map<String, Object> dimenMap :dimenListResult){
- if(dimenMap.get(orgHealthCategoryCode) != null && dimenMap.get(resultField) != null){
- if(dimenMap.get(orgHealthCategoryCode).equals(mapCategory.get("code"))){
- double result = Double.parseDouble(dimenMap.get(resultField).toString());
- double oldResult = 0;
- if(returnMap.get(resultField) != null){
- oldResult = Double.parseDouble(returnMap.get(resultField).toString());
- }
- returnMap.put(resultField,df.format(result + oldResult));
- for(String quotaCode : quotaCodes){
- double quotaResult = Double.parseDouble(dimenMap.get(quotaCode).toString());
- double oldQuotaResult = 0;
- if( returnMap.get(quotaCode) != null ){
- oldQuotaResult = Double.parseDouble(returnMap.get(quotaCode).toString());
- }
- returnMap.put(quotaCode,df.format(quotaResult + oldQuotaResult));
- }
- break;
- }
- }
- }
- }
- }catch (Exception e){
- throw new NumberFormatException("统计数据转换异常");
- }
- return returnMap;
- }
- /**
- * 时间聚合查询指标结果
- * @param code
- * @param dimension 多维度 ; 分开
- * @param filter
- * @throws Exception
- */
- public List<Map<String, Object>> getTimeAggregationResult(String code,String dimension, String filter,String dateDime) throws Exception {
- TjQuota tjQuota= quotaDao.findByCode(code);
- Map<String,String> dimensionDicMap = getDimensionDicMap(code,dimension);
- List<String> dimenList = getDimenList(dimension);
- String groupDimension = joinDimen(dimension);
- List<Map<String, Object>> dimenListResult = esResultExtract.searcherSumByGroupByTime(tjQuota, groupDimension, filter, dateDime);
- List<Map<String, Object>> resultList = new ArrayList<>();
- String dateHist = "date_histogram(field=quotaDate,interval="+ dateDime +")";
- for(Map<String, Object> map : dimenListResult){
- Map<String,Object> dataMap = new HashMap<>();
- for(String key :map.keySet()){
- if(key.equals(dateHist)) {
- String value = "";
- if (dateDime.equals("year")) {
- value = map.get(key).toString().substring(0, 4);
- } else if (dateDime.contains("quarter")) {
- String y = map.get(key).toString().substring(0, 4);
- String q = map.get(key).toString().substring(5, 7);
- if(q.contains("01")){
- value = y + "年1季度";
- }else if(q.contains("04")){
- value = y + "年2季度";
- }else if(q.contains("07")){
- value = y + "年3季度";
- }else if(q.contains("10")){
- value = y + "年4季度";
- }
- } else if (dateDime.contains("month")) {
- value = map.get(key).toString().substring(0, 7);
- } else if (dateDime.contains("week")) {
- value = map.get(key).toString().substring(0, 7);
- } else if (dateDime.contains("day")) {
- value = map.get(key).toString().substring(0, 10);
- }
- dataMap.put(dateDime, value);
- }
- if(dimenList.contains(key)){
- if(dimensionDicMap.get(map.get(key).toString().toLowerCase()) != null){
- String dictVal = dimensionDicMap.get(map.get(key).toString().toLowerCase());
- dataMap.put(key+"Name",dictVal);
- dataMap.put(key,map.get(key).toString());
- dataMap.put(firstColumnField, dictVal);
- }else {
- if(key.equals(quotaDateField)){
- String dateFormat = "yyyy-MM-dd";
- if (dateDime.equals("year")) {
- dateFormat = "yyyy";
- }else if(dateDime.equals("month")){
- dateFormat = "yyyy-MM";
- }
- SimpleDateFormat format = new SimpleDateFormat(dateFormat);
- Long time = new Long(Long.valueOf(map.get(key).toString()));
- String quotaDate = format.format(time);
- dataMap.put(key, quotaDate);
- }else {
- dataMap.put(key,map.get(key));
- }
- }
- }
- //维度为特殊机构类型时
- if(key.equals(orgHealthCategoryCode)){
- dataMap.put(map.get(orgHealthCategoryCode).toString(),map.get(orgHealthCategoryCode));
- dataMap.put(firstColumnField,map.get("text"));
- }
- if(key.equals("SUM(result)")){
- NumberFormat nf = NumberFormat.getInstance();
- nf.setGroupingUsed(false);
- nf.setMaximumFractionDigits(2);
- dataMap.put(resultField, nf.format(map.get(key)));
- }
- }
- resultList.add(dataMap);
- }
- return resultList;
- }
- /**
- * 获取聚合查询指标结果
- * @param code
- * @param dimension 多维度 ; 分开
- * @param filter
- * @throws Exception
- */
- public List<Map<String, Object>> getOrgHealthCategoryAggregationResult(String code,String dimension, String filter, String top) throws Exception {
- TjQuota tjQuota= quotaDao.findByCode(code);
- String groupDimension = "";
- if(dimension.contains(";")){
- String[] dimens = dimension.split(";");
- for(int i =0 ;i<dimens.length ;i++){
- groupDimension += dimens[i] + ",";
- }
- groupDimension = groupDimension.substring(0,groupDimension.length()-1);
- }else {
- groupDimension = dimension;
- }
- List<Map<String, Object>> dimenListResult = null;
- if (StringUtils.isNotEmpty(top)) {
- dimenListResult = esResultExtract.searcherSumGroup(tjQuota, groupDimension, filter, resultField, groupDimension, "asc", "1000");
- if (dimenListResult != null && dimenListResult.size() > 0) {
- dimenListResult = dimenListResult.subList(0, dimenListResult.size() > Integer.parseInt(top) ? Integer.parseInt(top) : dimenListResult.size());
- }
- }else {
- dimenListResult = esResultExtract.searcherSumGroup(tjQuota, groupDimension, filter, resultField, groupDimension, "asc", top);
- }
- List<Map<String, Object>> resultList = new ArrayList<>();
- for(Map<String, Object> map : dimenListResult){
- Map<String,Object> dataMap = new HashMap<>();
- boolean quotaFlag = false;
- for(String key :map.keySet()){
- //维度为特殊机构类型时
- if(key.equals(orgHealthCategoryCode)){
- dataMap.put(map.get(orgHealthCategoryCode).toString(),map.get(orgHealthCategoryCode));
- }
- if(key.equals("SUM(result)")){
- NumberFormat nf = NumberFormat.getInstance();
- nf.setGroupingUsed(false);
- nf.setMaximumFractionDigits(2);
- dataMap.put(resultField, nf.format(map.get(key)));
- }
- if(key.equals(quotaDateField)){
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Long time = new Long(Long.valueOf(map.get(key).toString()));
- String quotaDate = format.format(time);
- dataMap.put(quotaDateField, quotaDate);
- quotaFlag = true;
- }
- }
- if(quotaFlag){
- map.remove(quotaDateField);
- }
- dataMap.putAll(map);
- resultList.add(dataMap);
- }
- return resultList;
- }
- /**
- * 获取 特殊机构类别 聚合查询指标结果
- * @param code
- * @param dimension 多维度 ; 分开
- * @param filter
- * @throws Exception
- */
- public List<Map<String, Object>> getAggregationResult(String code,String dimension, String filter, String top) throws Exception {
- TjQuota tjQuota= quotaDao.findByCode(code);
- Map<String,String> dimensionDicMap = getDimensionDicMap(code,dimension);
- List<String> dimenList = getDimenList(dimension);
- String groupDimension = joinDimen(dimension);
- List<TjQuotaDimensionSlave> slaves = tjDimensionSlaveService.findTjQuotaDimensionSlaveByQuotaCode(code);
- for(TjQuotaDimensionSlave slave :slaves) {
- if (slave.getSlaveCode().equals("dept")) {
- String dimenName = dimension + "Name not in('其他') ";
- if(StringUtils.isEmpty(filter)){
- filter = dimenName;
- }else {
- filter += " and " + dimenName;
- }
- }
- }
- List<Map<String, Object>> dimenListResult = null;
- if (StringUtils.isNotEmpty(top)) {
- dimenListResult = esResultExtract.searcherSumGroup(tjQuota, groupDimension, filter, resultField, groupDimension, "asc", "1000");
- if (dimenListResult != null && dimenListResult.size() > 0) {
- dimenListResult = dimenListResult.subList(0, dimenListResult.size() > Integer.parseInt(top) ? Integer.parseInt(top) : dimenListResult.size());
- }
- }else {
- dimenListResult = esResultExtract.searcherSumGroup(tjQuota, groupDimension, filter, resultField, groupDimension, "asc", top);
- }
- List<Map<String, Object>> resultList = new ArrayList<>();
- for(Map<String, Object> map : dimenListResult){
- Map<String,Object> dataMap = new HashMap<>();
- for(String key :map.keySet()){
- if(dimenList.contains(key)){
- if(dimensionDicMap.get(map.get(key).toString().toLowerCase()) != null){
- dataMap.put(key,dimensionDicMap.get(map.get(key).toString().toLowerCase()));
- dataMap.put(key+"Name",dimensionDicMap.get(map.get(key).toString().toLowerCase()));
- dataMap.put(key+"Code",map.get(key).toString());
- dataMap.put(firstColumnField,dimensionDicMap.get(map.get(key).toString().toLowerCase()));
- }else {
- dataMap.put(key,map.get(key));
- }
- }
- //维度为特殊机构类型时
- if(key.equals(orgHealthCategoryCode)){
- dataMap.put(map.get(orgHealthCategoryCode).toString(),map.get(orgHealthCategoryCode));
- dataMap.put(firstColumnField,map.get("text"));
- }
- if(key.equals("SUM(result)")){
- NumberFormat nf = NumberFormat.getInstance();
- nf.setGroupingUsed(false);
- nf.setMaximumFractionDigits(2);
- dataMap.put(resultField, nf.format(map.get(key)));
- }
- }
- resultList.add(dataMap);
- }
- if (StringUtils.isEmpty(top) && "town".equals(dimension)) {
- resultList = noDataDimenDictionary(resultList,dimension,filter);
- }
- for(TjQuotaDimensionSlave slave :slaves){
- if(slave.getSlaveCode().equals("sex") ){
- resultList = filteUnKnowSex(resultList, dimension);
- }
- }
- if(dimension.equals("level")){
- resultList = filteUnKnowLeve(resultList, dimension);
- }
- return resultList;
- }
- /**
- * 过滤未说明的性别,和其他为0的数据
- * @param dataList
- * @return
- */
- public List<Map<String, Object>> filteUnKnowSex(List<Map<String, Object>> dataList,String dimension){
- List<Map<String, Object>> resultList = new ArrayList<>();
- for(Map<String,Object> map : dataList){
- if(map.get(dimension) !=null){
- if( !map.get(dimension).toString().contains("未说明")){
- if(map.get(resultField) !=null){
- if(!map.get(dimension).toString().contains("未知") || ! map.get(resultField).toString().equals("0") ){
- resultList.add(map);
- }
- }
- }
- }
- }
- return resultList;
- }
- /**
- * 过滤未知的等级
- * @param dataList
- * @return
- */
- public List<Map<String, Object>> filteUnKnowLeve(List<Map<String, Object>> dataList,String dimension){
- List<Map<String, Object>> resultList = new ArrayList<>();
- for(Map<String,Object> map : dataList){
- if(map.get(dimension) !=null){
- if( !(map.get(dimension).toString().contains("未知") || map.get(dimension).toString().contains("9")) ){
- resultList.add(map);
- }
- }
- }
- return resultList;
- }
- /**
- * 查询结果 对无数据的字典项补0
- * @param dataList 数据集合
- * @param dimen 维度
- * @return
- */
- public List<Map<String, Object>> noDataDimenDictionary(List<Map<String, Object>> dataList,String dimen,String filter){
- Map<String, Object> dictMap = new HashMap<>();
- if(dimen.equals("town")){
- String sql = "";
- if(dimen.equals("town") ){
- sql = "SELECT id as code,name as name from address_dict where pid = '361100'";
- List<Map<String, Object>> dictDataList = jdbcTemplate.queryForList(sql);
- if(null != dictDataList) {
- for(int i = 0 ; i < dictDataList.size();i++){
- if(null != dictDataList.get(i).get("code") && null != dictDataList.get(i).get("name")){
- dictMap.put(dictDataList.get(i).get("code").toString(),dictDataList.get(i).get("name").toString());
- }
- }
- }
- }
- List<Map<String, Object>> resultList = new ArrayList<>();
- for(String code : dictMap.keySet()){
- Map<String,Object> oneMap = new HashMap<>();
- String result = "0";
- for(Map<String,Object> map : dataList){
- if(map.get(dimen) !=null && map.get(dimen).equals(dictMap.get(code))){
- result = map.get(resultField).toString();
- break;
- }
- }
- oneMap.put(firstColumnField,dictMap.get(code));
- oneMap.put(dimen,dictMap.get(code));
- oneMap.put(dimen+"Name",dictMap.get(code));
- oneMap.put(dimen+"Code",code);
- oneMap.put(resultField,result);
- resultList.add(oneMap);
- }
- return resultList;
- }
- return dataList;
- }
- /**
- * 拼接维度分组
- * @param dimension
- * @return
- */
- public String joinDimen(String dimension){
- String groupDimension = "";
- if(dimension.contains(";")){
- String[] dimens = dimension.split(";");
- for(int i =0 ;i<dimens.length ;i++){
- groupDimension += dimens[i] + ",";
- }
- groupDimension = groupDimension.substring(0,groupDimension.length()-1);
- }else {
- groupDimension = dimension;
- }
- return groupDimension;
- }
- /**
- * 获取维度List
- * @param dimension
- * @return 多维度 ;隔开
- */
- public List<String> getDimenList(String dimension){
- List<String> dimenList = new ArrayList<>();
- if(dimension.contains(";")){
- String[] dimens = dimension.split(";");
- for(int i =0 ;i<dimens.length ;i++){
- dimenList.add(dimens[i]);
- }
- }else {
- dimenList.add(dimension);
- }
- return dimenList;
- }
- /**
- * 获取指标维度字典项
- * @param quotaCode
- * @param dimension 多维度 ;隔开
- * @return
- */
- public Map<String,String> getDimensionDicMap(String quotaCode ,String dimension){
- Map<String,String> dimensionDicMap = new HashMap<>();
- String[] dimens = dimension.split(";");
- for(int i =0 ;i<dimens.length ;i++){
- String dictSql = getQuotaDimensionDictSql(quotaCode, dimens[i]);
- if(StringUtils.isNotEmpty(dictSql)){
- Map<String,String> dicMap = getDimensionMap(dictSql, dimens[i]);
- for(String key :dicMap.keySet()){
- dimensionDicMap.put(key.toLowerCase(),dicMap.get(key));
- }
- }
- }
- return dimensionDicMap;
- }
- /**
- * 获取维度的字典 sql -- 针对于查询结果 从ES库查询结果
- * @param quotaCode
- * @param dimension
- * @return
- */
- private String getQuotaDimensionDictSql(String quotaCode, String dimension) {
- boolean mainFlag = dimension.contains("province") || dimension.contains("city") ||dimension.contains("town")
- ||dimension.contains("org") ||dimension.contains("dept") ||dimension.contains("year") ||dimension.contains("quarter")
- ||dimension.contains("month") ||dimension.contains("day") || dimension.contains(quotaDateField) ;
- String dictSql = "";
- //查询维度 sql
- if( mainFlag){
- List<TjQuotaDimensionMain> dimensionMains = tjDimensionMainService.findTjQuotaDimensionMainByQuotaCode(quotaCode);
- if(dimensionMains != null && dimensionMains.size() > 0){
- for(TjQuotaDimensionMain main:dimensionMains){
- if(main.getMainCode().equals(dimension)){
- dictSql = main.getDictSql();
- }
- }
- }
- }else {
- if(StringUtils.isEmpty(dictSql)) {
- List<TjQuotaDimensionSlave> dimensionSlaves = tjDimensionSlaveService.findTjQuotaDimensionSlaveByQuotaCode(quotaCode);
- if (dimensionSlaves != null && dimensionSlaves.size() > 0) {
- if(StringUtils.isNotEmpty(dimension)){
- String n = dimension.substring(dimension.length() - 1, dimension.length());
- if(StringUtils.isNotEmpty(n) && (n.equals("1") || n.equals("2") || n.equals("3") || n.equals("4")) ){
- int slave = Integer.valueOf(n);
- if(dimensionSlaves.size() >= slave){
- dictSql = dimensionSlaves.get(slave-1).getDictSql();
- }
- }
- }
- }
- }
- }
- return dictSql;
- }
- /**
- *获取字典项数据集
- * @param dictSql
- * @param dimension
- * @return
- */
- public Map<String,String> getDimensionMap(String dictSql, String dimension) {
- Map<String,String> dimensionDicMap = new HashMap<>();
- if(StringUtils.isNotEmpty(dictSql)) {
- BasesicUtil baseUtil = new BasesicUtil();
- boolean main = dimension.contains("province") || dimension.contains("city") ||dimension.contains("town")
- ||dimension.contains("org") ||dimension.contains("dept") ||dimension.contains("year") ||dimension.contains("month")
- ||dimension.contains("quarter") ||dimension.contains("day");
- if( main){
- //主纬度字典项
- List<SaveModel> dictDatas = jdbcTemplate.query(dictSql, new BeanPropertyRowMapper(SaveModel.class));
- if(dictDatas != null ) {
- for (SaveModel saveModel : dictDatas) {
- String name = baseUtil.getFieldValueByName(dimension + "Name", saveModel);
- String val = baseUtil.getFieldValueByName(dimension, saveModel);
- if(StringUtils.isNotEmpty(val) && StringUtils.isNotEmpty(name)){
- dimensionDicMap.put(val.toLowerCase(),name);
- }
- }
- }
- } else{
- //查询细维度字典数据
- List<DictModel> dictDatas = jdbcTemplate.query(dictSql, new BeanPropertyRowMapper(DictModel.class));
- for (DictModel dictModel : dictDatas) {
- String name = baseUtil.getFieldValueByName("name", dictModel);
- String val = baseUtil.getFieldValueByName("code", dictModel).toLowerCase();
- dimensionDicMap.put(val,name);
- }
- }
- }
- return dimensionDicMap;
- }
- /**
- * 获取单个指标结果
- * @param code
- * @param filters 外部传入条件
- * @param dimension
- * @return
- * @throws Exception
- */
- public List<Map<String, Object>> getSimpleQuotaReport(String code,String filters,String dimension,boolean isTrunTree, String top) throws Exception {
- String dateType = "";
- //指标的展示维度,由视图中决定
- if(dimension.trim().equals("year")){
- dateType = "year";
- dimension = "";
- }else if(dimension.trim().equals("quarter")){
- dateType = "quarter";
- dimension = "";
- }else if(dimension.trim().equals("month")){
- dateType = "month";
- dimension = "";
- }else if(dimension.trim().equals("day")){
- dateType = "day";
- dimension = "";
- }
- List<Map<String, Object>> result = new ArrayList<>();
- TjQuotaDataSource quotaDataSource = dataSourceService.findSourceByQuotaCode(code);
- JSONObject obj = new JSONObject().fromObject(quotaDataSource.getConfigJson());
- EsConfig esConfig= (EsConfig) JSONObject.toBean(obj,EsConfig.class);
- String configFilter = esConfig.getFilter();
- if(StringUtils.isNotEmpty(configFilter) && quotaDataSource.getSourceCode().equals("1")){//数据源为ES库
- TjQuotaDataSave quotaDataSave = dataSaveService.findByQuota(code);
- if(quotaDataSave != null && StringUtils.isNotEmpty(quotaDataSave.getConfigJson())){
- JSONObject objSave = new JSONObject().fromObject(quotaDataSave.getConfigJson());
- EsConfig esConfigSave = (EsConfig) JSONObject.toBean(objSave,EsConfig.class);
- if(StringUtils.isEmpty(esConfig.getIndex()) || esConfig.getIndex().equals(esConfigSave.getIndex()) ){
- if(StringUtils.isNotEmpty(filters)){
- filters += " and " + configFilter;
- }else {
- filters = configFilter;
- }
- }
- }else {
- if(StringUtils.isNotEmpty(filters)){
- filters += " and " + configFilter;
- }else {
- filters = configFilter;
- }
- }
- }
- // 判断该指标是否需要同比, 需要的话拼接时间条件
- if (StringUtils.isNotEmpty(esConfig.getIncrementFlag())) {
- filters = filtersExchangeHandle(filters, esConfig);
- log.info("filters = {}", filters);
- }
- String molecularFilter = filters;
- String denominatorFilter = filters;
- if (StringUtils.isNotEmpty(esConfig.getGrowthFlag())) {
- //增幅运算(环比和同比)
- result = getGrowthByQuota(dimension, filters, esConfig, dateType);
- } else {
- if (StringUtils.isNotEmpty(esConfig.getDateComparisonType())) {
- //时间条件处理
- filters = getdateComparisonTypeFilter(esConfig,filters);
- }
- if( (StringUtils.isNotEmpty(esConfig.getEspecialType())) && esConfig.getEspecialType().equals(orgHealthCategory)){
- // //特殊机构类型查询输出结果 只有查询条件没有维度 默认是 机构类型维度
- // result = getOrgHealthCategory(code,filters,dateType,isTrunTree, top);
- }else if( (StringUtils.isNotEmpty(esConfig.getMolecular())) && StringUtils.isNotEmpty(esConfig.getDenominator())){//除法
- //除法指标查询输出结果
- molecularFilter = handleFilter(esConfig.getMolecularFilter(), molecularFilter);
- denominatorFilter = handleFilter(esConfig.getDenominatorFilter(), denominatorFilter);
- if (StringUtils.isNotEmpty(esConfig.getDivisionType()) && esConfig.getDivisionType().equals("2")) {
- result = divisionQuotaDenoConstant(esConfig.getMolecular(), dimension, molecularFilter, denominatorFilter,esConfig.getPercentOperation(), esConfig.getPercentOperationValue(), dateType, top);
- } else {
- result = divisionQuota(esConfig.getMolecular(), esConfig.getDenominator(), dimension, molecularFilter, denominatorFilter, esConfig.getPercentOperation(), esConfig.getPercentOperationValue(),dateType, top);
- }
- //计算除法 合计
- String moleQuotaCode = getBasicQuota(esConfig.getMolecular());
- moleQuotaCode = " quotaCode='" + moleQuotaCode.replaceAll("_", "") + "' ";
- String denoQuotaCode = getBasicQuota(esConfig.getDenominator());
- denoQuotaCode = " quotaCode='" + denoQuotaCode.replaceAll("_", "") + "' ";
- if(StringUtils.isEmpty(molecularFilter)){
- molecularFilter = moleQuotaCode;
- }else {
- molecularFilter += " and " + moleQuotaCode;
- }
- if(StringUtils.isEmpty(denominatorFilter)){
- denominatorFilter = denoQuotaCode;
- }else {
- denominatorFilter += " and " + denoQuotaCode;
- }
- String moleTotalSql = "select sum(result) result from medical_service_index where " + molecularFilter;
- String denoTotalSql = "select sum(result) result from medical_service_index where " + denominatorFilter;
- double moleTotal = 0;
- double denoTotal = 0;
- List<Map<String, Object>> moleListMap = elasticsearchUtil.excuteDataModel(moleTotalSql.toString());
- if(moleListMap != null && moleListMap.size() > 0){
- if(moleListMap.get(0).get("result") != null){
- moleTotal = Double.valueOf(moleListMap.get(0).get("result").toString());
- }
- }
- List<Map<String, Object>> denoListMap = elasticsearchUtil.excuteDataModel(denoTotalSql.toString());
- if(denoListMap != null && denoListMap.size() > 0){
- if(denoListMap.get(0).get("result") != null){
- denoTotal = Double.valueOf(denoListMap.get(0).get("result").toString());
- }
- }
- double point = 0;
- NumberFormat df = NumberFormat.getInstance();
- df.setGroupingUsed(false);
- df.setMaximumFractionDigits(2);
- int operation = Integer.valueOf(esConfig.getPercentOperation());
- int operationValue = Integer.valueOf(esConfig.getPercentOperationValue());
- if(denoTotal - 0 != 0){
- if(moleTotal != 0){
- if(operation == 1){
- point = (moleTotal/denoTotal) * operationValue;
- }else if(operation == 2){
- point = (moleTotal/denoTotal) / operationValue;
- }
- }
- }
- Map<String, Object> totalMap = new HashMap<>();
- totalMap.put(resultField,df.format(point));
- totalMap.put(dimension,"合计");
- totalMap.put(firstColumnField,"合计");
- result.add(totalMap);
- }else if(StringUtils.isNotEmpty(esConfig.getAddOperation())){
- String firstFilter = handleFilter(esConfig.getAddFirstFilter(), filters);
- String secondFilter = handleFilter(esConfig.getAddSecondFilter(), filters);
- result = addQuota(esConfig.getAddFirstQuotaCode(), firstFilter, esConfig.getAddSecondQuotaCode(), secondFilter, esConfig.getAddOperation(),dimension,dateType, top);
- }else if(StringUtils.isNotEmpty(esConfig.getSuperiorBaseQuotaCode())) {
- //二次统计 指标查询
- result = getQuotaResultList(esConfig.getSuperiorBaseQuotaCode(), dimension,filters,dateType, top);
- }else {
- //普通基础指标查询
- result = getQuotaResultList(code, dimension,filters,dateType, top);
- }
- }
- return result;
- }
- //时间对比类型 时的 查询条件处理
- public String getdateComparisonTypeFilter(EsConfig esConfig,String filters){
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Calendar c = Calendar.getInstance();
- Map<String,String> filterMap = new HashMap<>();
- if(StringUtils.isNotEmpty(filters)){
- String [] filter = filters.split("and");
- String year = "";
- String month = "";
- String day = "";
- if(filter.length > 0 ){
- for(String key : filter){
- filterMap.put(key,key);
- Date date = new Date();
- if(key.contains(quotaDateField)){
- //quotaDate >= '2018-03-01'
- String dateStr = key.substring(key.indexOf("'")+1,key.lastIndexOf("'"));
- date = DateUtil.parseDate(dateStr,DateUtil.DEFAULT_DATE_YMD_FORMAT);
- }
- if(esConfig.getDateComparisonType().equals("lastYear")){
- c.setTime(date);
- c.add(Calendar.YEAR, -1);
- Date lastYear = c.getTime();
- filterMap.put(key,format.format(lastYear));
- }else if(esConfig.getDateComparisonType().equals("lastMonth")) {
- c.setTime(date);
- c.add(Calendar.MONTH, -1);
- Date lastYear = c.getTime();
- filterMap.put(key,format.format(lastYear));
- }else if(esConfig.getDateComparisonType().equals("lastDay")) {
- c.setTime(date);
- c.add(Calendar.DATE, -1);
- Date lastYear = c.getTime();
- filterMap.put(key,format.format(lastYear));
- }
- }
- }
- filters = "";
- if(filterMap != null && filterMap.size() > 0){
- for(String key : filterMap.keySet()){
- if(StringUtils.isEmpty(filters)){
- filters = filterMap.get(key);
- }else {
- filters = " and " + filterMap.get(key);
- }
- }
- }
- }
- return filters;
- }
- public String handleFilter(String secondFilter, String resultFilter) {
- if (StringUtils.isNotEmpty(secondFilter)) {
- if (StringUtils.isEmpty(resultFilter)) {
- resultFilter = secondFilter;
- } else {
- resultFilter += " and " + secondFilter;
- }
- }
- return resultFilter;
- }
- /**
- * 门急诊费用
- * @return
- */
- public String getCostOfOutPatient() {
- String sum = "0";
- String sql = "select sum(result) from medical_service_index where quotaCode='HC041047'";
- List<Map<String, Object>> listData = elasticsearchUtil.excuteDataModel(sql);
- NumberFormat nf = NumberFormat.getInstance();
- nf.setGroupingUsed(false);
- nf.setMaximumFractionDigits(2);
- if (null != listData && listData.size() > 0 && listData.get(0).size() > 0) {
- for (Map<String, Object> map : listData) {
- sum = nf.format(map.get("SUM(result)"));
- }
- }
- return sum;
- }
- /**
- * 门急诊人次
- * @return
- */
- public String getNumOfOutPatient() {
- int sum = 0;
- String sql = "select sum(result) from medical_service_index where quotaCode='HC041008'";
- List<Map<String, Object>> listData = singleDiseaseService.parseIntegerValue(sql);
- NumberFormat nf = NumberFormat.getInstance();
- nf.setGroupingUsed(false);
- nf.setMaximumFractionDigits(2);
- if (null != listData && listData.size() > 0 && listData.get(0).size() > 0) {
- for (Map<String, Object> map : listData) {
- String value = nf.format(map.get("SUM(result)"));
- sum += Integer.parseInt(value);
- }
- }
- return sum + "";
- }
- /**
- * 入院费用
- * @return
- */
- public String getCostOfInPatient() {
- String sum = "0";
- String sql = "select sum(result) from medical_service_index where quotaCode='HC041068'";
- List<Map<String, Object>> listData = elasticsearchUtil.excuteDataModel(sql);
- NumberFormat nf = NumberFormat.getInstance();
- nf.setGroupingUsed(false);
- nf.setMaximumFractionDigits(2);
- if (null != listData && listData.size() > 0 && listData.get(0).size() > 0) {
- for (Map<String, Object> map : listData) {
- sum = nf.format(map.get("SUM(result)"));
- }
- }
- return sum;
- }
- /**
- * 入院人次
- * @return
- */
- public String getNumOfInPatient() {
- int sum = 0;
- String sql = "select sum(result) from medical_service_index where quotaCode='HC041000'";
- List<Map<String, Object>> listData = singleDiseaseService.parseIntegerValue(sql);
- NumberFormat nf = NumberFormat.getInstance();
- nf.setGroupingUsed(false);
- nf.setMaximumFractionDigits(2);
- if (null != listData && listData.size() > 0 && listData.get(0).size() > 0) {
- for (Map<String, Object> map : listData) {
- String value = nf.format(map.get("SUM(result)"));
- sum += Integer.parseInt(value);
- }
- }
- return sum + "";
- }
- public String getCostOfMedicalMonitor() {
- NumberFormat nf = NumberFormat.getInstance();
- nf.setGroupingUsed(false);
- nf.setMaximumFractionDigits(2);
- // 获取门急诊费用
- Double costOfOutPatient = Double.parseDouble(getCostOfOutPatient());
- // 获取入院费用
- Double costOfInPatient = Double.parseDouble(getCostOfInPatient());
- // 医疗费用监测 = 获取门急诊费用 + 获取入院费用
- Double costOfMedicalMonitor = costOfInPatient + costOfOutPatient;
- return nf.format(costOfMedicalMonitor);
- }
- /**
- * 数据查询
- * @return
- */
- public Map<String, List<String>> getDataInfo(String sql ,String xdataName) {
- List<Map<String, Object>> listData = parseIntegerValue(sql);
- Map<String, List<String>> map = new HashMap<>();
- List<String> xData = new ArrayList<>();
- List<String> valueData = new ArrayList<>();
- if (null != listData && listData.size() >0 && listData.get(0) !=null ) {
- listData.forEach(one -> {
- if(xdataName.contains("date_histogram")){
- if(xdataName.contains("year")){
- xData.add(one.get(xdataName).toString().substring(0,4) + "");
- }else if(xdataName.contains("quarter")){
- String quarter = "";
- if(one.get(xdataName) != null){
- String value = one.get(xdataName).toString();
- String y = value.substring(0, 4);
- String q = value.substring(5, 7);
- if(q.contains("01")){
- quarter = y + "年1季度";
- }else if(q.contains("04")) {
- value = y + "年2季度";
- }else if(q.contains("07")){
- quarter = y + "年3季度";
- }else if(q.contains("10")){
- quarter = y + "年4季度";
- }
- xData.add(quarter);
- }
- }else if(xdataName.contains("month")){
- xData.add(one.get(xdataName).toString().substring(0,7) + "");
- }
- }else {
- xData.add(one.get(xdataName) + "");
- }
- valueData.add(one.get("count") + "");
- });
- map.put("xData", xData);
- map.put("valueData", valueData);
- }
- return map;
- }
- /**
- * 对查询结果key包含count、sum的value去掉小数点
- * @param sql
- * @return
- */
- public List<Map<String, Object>> parseIntegerValue(String sql) {
- List<Map<String, Object>> listData = elasticsearchUtil.excuteDataModel(sql);
- List<Map<String, Object>> handleData = new ArrayList<>();
- listData.forEach(item -> {
- Map<String, Object> myMap = new HashMap<>();
- item.forEach((k,v) -> {
- if (k.contains("COUNT") || k.contains("SUM") || k.contains("count")) {
- v = (int) Double.parseDouble(v + "");
- }
- myMap.put(k,v);
- });
- handleData.add(myMap);
- });
- return handleData;
- }
- /**
- * 百分比 增幅运算 维度不是时间维度
- * @param dimension
- * @param moleList
- * @param denoList
- * @param operation
- * @param operationValue
- * @return
- */
- public List<Map<String, Object>> divisionPercent(String dimension, List<Map<String, Object>> moleList, List<Map<String, Object>> denoList,int operation,int operationValue){
- List<Map<String, Object>> divisionResultList = new ArrayList<>();
- for(Map<String, Object> denoMap : denoList) {
- if (null != denoMap && denoMap.size() > 0 ) {
- Map<String, Object> map = new HashMap<>();
- double denoResultVal = Double.valueOf(denoMap.get(resultField) == null ? "0" : denoMap.get(resultField).toString());
- String denoKeyVal = "";
- String [] denoDimensions = dimension.split(";");
- for(int i = 0 ;i < denoDimensions.length ; i++){
- if(i == 0){
- denoKeyVal = denoMap.get(denoDimensions[i]).toString();
- }else {
- denoKeyVal = denoKeyVal + "-" + denoMap.get(denoDimensions[i]).toString() ;
- }
- map.put(firstColumnField, denoMap.get(firstColumnField));
- map.put(denoDimensions[i], denoMap.get(denoDimensions[i]).toString());
- }
- if (denoResultVal == 0) {
- map.put(resultField, "--");
- divisionResultList.add(map);
- } else {
- if(moleList != null && moleList.size() > 0){
- for(Map<String, Object> moleMap :moleList) {
- String moleKeyVal = "";
- String [] moleDimensions = dimension.split(";");
- for(int i = 0 ;i < moleDimensions.length ; i++){
- if(i == 0){
- moleKeyVal = moleMap.get(moleDimensions[i]).toString();
- }else {
- moleKeyVal = moleKeyVal + "-" + moleMap.get(moleDimensions[i]).toString() ;
- }
- }
- if(denoKeyVal.equals(moleKeyVal)){
- double point = 0;
- NumberFormat nf = NumberFormat.getInstance();
- nf.setGroupingUsed(false);
- nf.setMaximumFractionDigits(2);
- float moleResultVal = Float.valueOf(moleMap.get(resultField).toString());
- point = ((moleResultVal - denoResultVal)/denoResultVal) * operationValue;
- map.put(resultField, nf.format(point));
- divisionResultList.add(map);
- break;
- }
- }
- }else {
- map.put(resultField, 0);
- divisionResultList.add(map);
- }
- }
- }
- }
- return divisionResultList;
- }
- /**
- * 计算增幅 环比和 同比
- * @param dimension 不为空时,查询固定某个月或者某年的增幅
- * @param filters 外部过滤条件 如 quotaDate >= '2018-03-01' and quotaDate <= '2018-03-31'
- * @param esConfig 指标内部配置
- * @param dateType 不为空时,查询某个时间区间的增幅
- * @return
- * @throws Exception
- */
- public List<Map<String, Object>> getGrowthByQuota(String dimension, String filters, EsConfig esConfig, String dateType) throws Exception {
- List<Map<String, Object>> resultList = new LinkedList<>();
- String startQuotaDate = "";
- String endQuotaDate = "";
- String noDateFilter = "";
- if(StringUtils.isNotEmpty(esConfig.getDenominatorFilter()) ){
- noDateFilter = esConfig.getDenominatorFilter();
- }
- if (StringUtils.isNotEmpty(filters) && filters.contains(quotaDateField)) {// 外部指定时间
- String params [] = filters.split("and ");
- for(int i =0 ;i< params.length ;i++){
- String quotaDateParam = params[i];
- if(quotaDateParam.contains(quotaDateField)){
- boolean b = quotaDateParam.indexOf("'") > -1;
- int start = b ? quotaDateParam.indexOf("'") : quotaDateParam.indexOf("\"");//查询条件不是' ,就是"
- if(quotaDateParam.contains(">")){
- startQuotaDate = quotaDateParam.substring(start + 1, start + 11);
- }
- if(quotaDateParam.contains("<")){
- endQuotaDate = quotaDateParam.substring(start + 1, start + 11);
- }
- }else{
- if(StringUtils.isNotEmpty(noDateFilter)){
- noDateFilter += " and " + quotaDateParam;
- }else {
- noDateFilter = quotaDateParam;
- }
- }
- }
- }
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Calendar lastDate = Calendar.getInstance();
- String molecularFilter = "";
- String denominatorFilter = "";
- String growthFlag = esConfig.getGrowthFlag();
- if(StringUtils.isEmpty(dateType)){
- if ("1".equals(growthFlag)) { // 年增幅 没有传时间条件默认当前年份
- int now;
- int beforeNow;
- now = lastDate.get(Calendar.YEAR);
- if (StringUtils.isNotEmpty(endQuotaDate)) {// 外部指定时间
- now = Integer.parseInt(endQuotaDate.substring(0,4));
- }
- beforeNow = now - 1;
- molecularFilter = "quotaDate >= '" + now + "-01-01' and quotaDate <= '" + now + "-12-31'";
- denominatorFilter = "quotaDate >= '" + beforeNow + "-01-01' and quotaDate <= '" + beforeNow + "-12-31'";
- } else if ("2".equals(growthFlag)) { // 月增幅 没有传时间条件默认当前月份
- // 如果有时间过滤条件,则按时间条件计算
- if (StringUtils.isNotEmpty(endQuotaDate)) {
- lastDate.setTime(sdf.parse(endQuotaDate));
- }
- lastDate.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号,当前日期既为本月第一天
- String firstDay = sdf.format(lastDate.getTime());
- lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
- String lastDay = sdf.format(lastDate.getTime());
- lastDate.add(Calendar.MONTH, -1);
- lastDate.set(Calendar.DAY_OF_MONTH, 1);
- String preMonthFirstDay = sdf.format(lastDate.getTime());
- lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
- String preMonthLastDay = sdf.format(lastDate.getTime());
- log.info("firstDay = {}, lastDay = {}", firstDay, lastDay);
- log.info("preMonthFirstDay = {}, preMonthLastDay = {}", preMonthFirstDay, preMonthLastDay);
- molecularFilter = "quotaDate >= '" + firstDay + "' and quotaDate <= '" + lastDay + "'";
- denominatorFilter = "quotaDate >= '" + preMonthFirstDay + "' and quotaDate <= '" + preMonthLastDay + "'";
- } else if ("3".equals(growthFlag)) { // 季度增幅 没有传时间条件默认当前季度
- // 如果有时间过滤条件,则按时间条件计算
- if (StringUtils.isNotEmpty(endQuotaDate)) {
- lastDate.setTime(sdf.parse(endQuotaDate));
- }
- String firstDay = "";
- String lastDay = "";
- int currentMonth = lastDate.get(Calendar.MONTH) + 1;
- if (currentMonth >= 1 && currentMonth <= 3){
- lastDate.set(Calendar.MONTH, 0);
- }else if (currentMonth >= 4 && currentMonth <= 6){
- lastDate.set(Calendar.MONTH, 3);
- } else if (currentMonth >= 7 && currentMonth <= 9){
- lastDate.set(Calendar.MONTH, 6);
- } else if (currentMonth >= 10 && currentMonth <= 12){
- lastDate.set(Calendar.MONTH, 9);
- }
- lastDate.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号,当前日期既为本月第一天
- firstDay = sdf.format(lastDate.getTime());//这个季度第一天
- lastDate.add(Calendar.MONTH, 3);
- lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
- lastDay = sdf.format(lastDate.getTime());//这个季度最后一天
- lastDate.add(Calendar.MONTH, -6);//前一个季度第一天
- lastDate.set(Calendar.DAY_OF_MONTH, 1);
- lastDate.add(Calendar.MONTH, 3);
- String preMonthFirstDay = sdf.format(lastDate.getTime());
- lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
- String preMonthLastDay = sdf.format(lastDate.getTime());
- log.info("firstDay = {}, lastDay = {}", firstDay, lastDay);
- log.info("preMonthFirstDay = {}, preMonthLastDay = {}", preMonthFirstDay, preMonthLastDay);
- molecularFilter = "quotaDate >= '" + firstDay + "' and quotaDate <= '" + lastDay + "'";
- denominatorFilter = "quotaDate >= '" + preMonthFirstDay + "' and quotaDate <= '" + preMonthLastDay + "'";
- }
- if (StringUtils.isNotEmpty(esConfig.getMolecularFilter())) {
- molecularFilter += " and " + esConfig.getMolecularFilter();
- }
- if (StringUtils.isNotEmpty(esConfig.getDenominatorFilter())) {
- denominatorFilter += " and " + esConfig.getDenominatorFilter();
- }
- if(StringUtils.isNotEmpty(noDateFilter)){
- molecularFilter += " and " + noDateFilter;
- denominatorFilter += " and " + noDateFilter;
- }
- List<Map<String, Object>> moleList = getSimpleQuotaReport(esConfig.getMolecular(), molecularFilter,dimension ,false , null);
- List<Map<String, Object>> denoList = getSimpleQuotaReport(esConfig.getDenominator(),denominatorFilter,dimension,false, null);
- resultList = divisionPercent(dimension, moleList, denoList, 1, 100);
- return resultList;
- }else {
- String dateFilter = "";
- //按时间维度 增幅,即时间区间增幅
- int nowYear;
- int beforeYear;
- Date firstMonth = new Date();
- Date endMonth = new Date();
- nowYear = lastDate.get(Calendar.YEAR);
- beforeYear = nowYear - 4;
- if ("1".equals(growthFlag)) { // 年增幅 没有传时间条件默认当前年份 向前推3年
- if (StringUtils.isNotEmpty(startQuotaDate)) {// 外部指定时间
- beforeYear = Integer.parseInt(startQuotaDate.substring(0,4))-1;
- }
- if (StringUtils.isNotEmpty(endQuotaDate)) {// 外部指定时间
- nowYear = Integer.parseInt(endQuotaDate.substring(0,4));
- dateFilter = "quotaDate >= '" + beforeYear + "-01-01' and quotaDate <= '" + nowYear + "-12-31'";
- }
- } else if ("2".equals(growthFlag)) { // 月增幅 没有传时间条件默认当前月份 计算前6个月数据 计算向前推7个月
- lastDate.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号,当前日期既为本月第一天
- endMonth =lastDate.getTime();
- lastDate.add(Calendar.MONTH, - 7);
- lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
- firstMonth = lastDate.getTime();
- // 如果有时间过滤条件,则按时间条件计算
- if (StringUtils.isNotEmpty(startQuotaDate)) {
- if (StringUtils.isNotEmpty(startQuotaDate)) {// 外部指定时间
- firstMonth = sdf.parse(startQuotaDate);
- lastDate.setTime(firstMonth);
- lastDate.add(Calendar.MONTH, -1);//向前推一个月 用于计算最后一个月增幅
- firstMonth = lastDate.getTime();
- }
- if (StringUtils.isNotEmpty(endQuotaDate)) {// 外部指定时间
- endMonth = sdf.parse(endQuotaDate);
- }
- }
- dateFilter = "quotaDate >= '" + sdf.format(firstMonth) + "' and quotaDate <= '" + sdf.format(endMonth) + "'";
- }else if ("3".equals(growthFlag)) { // 季度增幅 没有传时间条件默认当前季度,计算查询向前推一个季度
- int currentMonth = lastDate.get(Calendar.MONTH) + 1;
- if (currentMonth >= 1 && currentMonth <= 3){
- lastDate.set(Calendar.MONTH, 0);
- }else if (currentMonth >= 4 && currentMonth <= 6){
- lastDate.set(Calendar.MONTH, 3);
- } else if (currentMonth >= 7 && currentMonth <= 9){
- lastDate.set(Calendar.MONTH, 6);
- } else if (currentMonth >= 10 && currentMonth <= 12){
- lastDate.set(Calendar.MONTH, 9);
- }
- lastDate.set(Calendar.MONTH, 3);//此季度最后一天
- lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
- endMonth =lastDate.getTime();
- lastDate.set(Calendar.MONTH, -6);//向前推一个季度 向前推3个月
- lastDate.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号,当前日期既为本月第一天
- firstMonth = lastDate.getTime();
- // 如果有时间过滤条件,则按时间条件计算
- if (StringUtils.isNotEmpty(startQuotaDate)) {
- if (StringUtils.isNotEmpty(startQuotaDate)) {// 外部指定时间
- firstMonth = sdf.parse(startQuotaDate);
- lastDate.setTime(firstMonth);
- lastDate.add(Calendar.MONTH, -3);//向前推一季度 用于计算最后一个季度增幅
- firstMonth = lastDate.getTime();
- }
- if (StringUtils.isNotEmpty(endQuotaDate)) {// 外部指定时间
- endMonth = sdf.parse(endQuotaDate);
- }
- }
- dateFilter = "quotaDate >= '" + sdf.format(firstMonth) + "' and quotaDate <= '" + sdf.format(endMonth) + "'";
- }
- if(StringUtils.isNotEmpty(noDateFilter)){
- filters = dateFilter + " and " + noDateFilter;
- }else{
- filters = dateFilter;
- }
- dimension = dateType;
- List<Map<String, Object>> dataList = getSimpleQuotaReport(esConfig.getMolecular(), filters,dimension ,false , null);
- NumberFormat df = NumberFormat.getInstance();
- df.setGroupingUsed(false);
- df.setMaximumFractionDigits(2);
- if(dataList != null && dataList.size() > 0){
- if(dateType.toLowerCase().equals("year")){
- for(int i = nowYear ; i > beforeYear ;i--){
- double current = 0;
- double last = 0;
- Map<String,Object> map = new HashMap<>();
- map.put(dimension, i);
- for(Map<String,Object> dataMap : dataList){
- int y = Integer.valueOf(dataMap.get(dimension).toString());
- if( y == i ){
- map.put(firstColumnField, dataMap.get(firstColumnField));
- current = Double.valueOf(dataMap.get(resultField).toString());
- }
- if( y == i-1 ){
- last = Double.valueOf(dataMap.get(resultField).toString());
- }
- }
- if(last == 0){
- map.put(resultField,"--");
- }else {
- double precent = (current - last)/last*100;
- map.put(resultField,precent-0 ==0 ? 0 : df.format(precent));
- }
- resultList.add(map);
- if(i-beforeYear <= 0){
- break;
- }
- }
- }else if(dateType.toLowerCase().equals("quarter")){
- String startQuarter = "";
- String endQuarter = "";
- lastDate.setTime(firstMonth);
- Date firstDate = lastDate.getTime();
- int currentMonth = lastDate.get(Calendar.MONTH) + 1;
- if (currentMonth >= 1 && currentMonth <= 3){
- startQuarter = sdf.format(firstDate).substring(0,4)+"年1季度";
- }else if (currentMonth >= 4 && currentMonth <= 6){
- startQuarter = sdf.format(firstDate).substring(0,4)+"年2季度";
- } else if (currentMonth >= 7 && currentMonth <= 9){
- startQuarter = sdf.format(firstDate).substring(0,4)+"年3季度";
- } else if (currentMonth >= 10 && currentMonth <= 12){
- startQuarter = sdf.format(firstDate).substring(0,4)+"年4季度";
- }
- lastDate.setTime(endMonth);
- Date downDate = lastDate.getTime();
- if (currentMonth >= 1 && currentMonth <= 3){
- endQuarter = sdf.format(downDate).substring(0,4)+"年1季度";
- }else if (currentMonth >= 4 && currentMonth <= 6){
- endQuarter = sdf.format(downDate).substring(0,4)+"年2季度";
- } else if (currentMonth >= 7 && currentMonth <= 9){
- endQuarter = sdf.format(downDate).substring(0,4)+"年3季度";
- } else if (currentMonth >= 10 && currentMonth <= 12){
- endQuarter = sdf.format(downDate).substring(0,4)+"年4季度";
- }
- String lastQuarter = "";
- while ( !startQuarter.equals(lastQuarter)){
- int eYear = Integer.valueOf(endQuarter.substring(0, 4));
- int eQuarter = Integer.valueOf(endQuarter.substring(5, 6));
- endQuarter = eYear+ "年" + eQuarter + "季度";
- if( !endQuarter.equals(startQuarter)){
- if(eQuarter == 1){
- eYear--;
- eQuarter = 4;
- }else {
- eQuarter--;
- }
- }
- lastQuarter = eYear + "年" + eQuarter + "季度";
- double current = 0;
- double last = 0;
- Map<String,Object> map = new HashMap<>();
- for(Map<String,Object> dataMap : dataList){
- if(dataMap.get(dimension) != null){
- String val = dataMap.get(dimension).toString();
- if(val.equals(endQuarter) ){
- map.put(firstColumnField, endQuarter);
- map.put(dimension, endQuarter);
- current = Double.valueOf(dataMap.get(resultField).toString());
- }
- if(val.equals(lastQuarter) ){
- last = Double.valueOf(dataMap.get(resultField).toString());
- }
- }
- }
- if(last == 0){
- map.put(resultField,"--");
- }else {
- double precent = (current - last)/last*100;
- if(precent == 0){
- map.put(resultField,0);
- }else {
- map.put(resultField,precent-0 ==0 ? 0 : df.format(precent));
- }
- }
- resultList.add(map);
- endQuarter = lastQuarter;
- }
- }else if(dateType.toLowerCase().equals("month")){
- String starthMonthStr = sdf.format(firstMonth).substring(0,7);
- String endMonthStr = sdf.format(endMonth).substring(0,7);
- while ( !starthMonthStr.equals(endMonthStr)){
- double current = 0;
- double last = 0;
- Map<String,Object> map = new HashMap<>();
- String nowMonthStr = endMonthStr;
- String lastMontStr = "";
- if( !starthMonthStr.equals(endMonthStr)){
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(sdf.parse(endMonthStr + "-01"));
- calendar.add(Calendar.MONTH, -1);//当前时间前去一个月,即一个月前的时间
- lastMontStr = sdf.format(calendar.getTime()).substring(0,7);
- }else {
- lastMontStr = starthMonthStr;
- }
- for(Map<String,Object> dataMap : dataList){
- if(dataMap.get(dimension) != null){
- String val = dataMap.get(dimension).toString();
- if(val.equals(nowMonthStr) ){
- map.put(firstColumnField, val);
- map.put(dimension, val);
- current = Double.valueOf(dataMap.get(resultField).toString());
- }
- if(val.equals(lastMontStr) ){
- last = Double.valueOf(dataMap.get(resultField).toString());
- }
- }
- }
- if(last == 0){
- map.put(resultField,"--");
- }else {
- double precent = (current - last)/last*100;
- if(precent == 0){
- map.put(resultField,0);
- }else {
- map.put(resultField,precent-0 ==0 ? 0 : df.format(precent));
- }
- }
- resultList.add(map);
- endMonthStr = lastMontStr;
- }
- }
- }
- if(resultList != null ){
- Collections.reverse(resultList); // 倒序排列
- }
- return resultList;
- }
- }
- public String filtersExchangeHandle(String filters, EsConfig esConfig) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Calendar lastDate = Calendar.getInstance();
- lastDate.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号,当前日期既为本月第一天
- String firstDay = sdf.format(lastDate.getTime());
- lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
- String lastDay = sdf.format(lastDate.getTime());
- lastDate.add(Calendar.MONTH, -1);
- lastDate.set(Calendar.DAY_OF_MONTH, 1);
- String preMonthFirstDay = sdf.format(lastDate.getTime());
- lastDate.set(Calendar.DAY_OF_MONTH, lastDate.getActualMaximum(Calendar.DAY_OF_MONTH));
- String preMonthLastDay = sdf.format(lastDate.getTime());
- // 如果有时间过滤条件,则按时间条件计算
- if (StringUtils.isNotEmpty(filters) && filters.contains(quotaDateField)) {
- Calendar cal = Calendar.getInstance();
- boolean b = filters.indexOf("'") > -1;
- int start = b ? filters.indexOf("'") : filters.indexOf("\"");
- String condition = filters.substring(start + 1, start + 5); // 获取年份
- int year = Integer.parseInt(condition);
- String condition2 = filters.substring(filters.indexOf("-") + 1, filters.indexOf("-") + 3); // 获取月份
- int month = Integer.parseInt(condition2);
- cal.set(Calendar.YEAR, year);
- cal.set(Calendar.MONTH, month - 1);
- cal.set(Calendar.DAY_OF_MONTH, 1); // 设置为1号,当前日期既为本月第一天
- firstDay = sdf.format(cal.getTime());
- cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
- lastDay = sdf.format(cal.getTime());
- cal.add(Calendar.MONTH, -1);
- cal.set(Calendar.DAY_OF_MONTH, 1);
- preMonthFirstDay = sdf.format(cal.getTime());
- cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
- preMonthLastDay = sdf.format(cal.getTime());
- }
- if ("1".equals(esConfig.getIncrementFlag())) {
- // 上月
- filters = "quotaDate >= '" + preMonthFirstDay + "' and quotaDate <= '" + preMonthLastDay + "'";
- } else if ("2".equals(esConfig.getIncrementFlag())) {
- // 当前月
- filters = "quotaDate >= '" + firstDay + "' and quotaDate <= '" + lastDay + "'";
- }
- if (StringUtils.isNotEmpty(esConfig.getFilter())) {
- filters += " and " + esConfig.getFilter();
- }
- return filters;
- }
- }
|