|
|
@@ -3,6 +3,7 @@ package com.usoftchina.bi.server.service.chart;
|
|
|
import com.usoftchina.bi.core.base.RepCode;
|
|
|
import com.usoftchina.bi.core.base.RepEntity;
|
|
|
import com.usoftchina.bi.core.utils.CalculationJudgeUtil;
|
|
|
+import com.usoftchina.bi.core.utils.CollectionUtils;
|
|
|
import com.usoftchina.bi.server.dao.chart.ChartsConfigMapper;
|
|
|
import com.usoftchina.bi.server.dao.chart.ShowChartsMapper;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
@@ -14,10 +15,12 @@ import com.usoftchina.bi.server.utils.ScreenUtil;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.stereotype.Repository;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+import org.springframework.util.ObjectUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Repository
|
|
|
public class ShowHistogramService {
|
|
|
@@ -40,193 +43,132 @@ public class ShowHistogramService {
|
|
|
柱状图数据展示
|
|
|
*/
|
|
|
@Cacheable(value = "Histogram", key = "#histogramConfigInfo.toString()+#dashId")
|
|
|
- public RepEntity showHistogram(HistogramConfigInfo histogramConfigInfo, String token, int dashId){
|
|
|
+ public RepEntity<ChartsDataInfo> showHistogram(HistogramConfigInfo histogramConfigInfo, String token, int dashId){
|
|
|
+ Assert.notNull(histogramConfigInfo, "参数不能为空");
|
|
|
+ /* 变量定义 */
|
|
|
ChartsDataInfo chartsDataInfo = new ChartsDataInfo();
|
|
|
TimeReture timeReture = new TimeReture();
|
|
|
- if (histogramConfigInfo == null || "".equals(histogramConfigInfo)){
|
|
|
- return new RepEntity(RepCode.Null);
|
|
|
- }
|
|
|
-
|
|
|
- //取表名
|
|
|
+ Series series = new Series();
|
|
|
+ String xColumn = histogramConfigInfo.getxAxis().getColumnRename(),
|
|
|
+ yColumn = histogramConfigInfo.getyAxis().getColumnRename(),
|
|
|
+ yType = histogramConfigInfo.getyAxis().getShowDataType(),
|
|
|
+ xColumnType = histogramConfigInfo.getxAxis().getColumnType(),
|
|
|
+ yAxisType = CalculationJudgeUtil.Judge(yType),
|
|
|
+ xAxisType = histogramConfigInfo.getxAxis().getShowDataType(),
|
|
|
+ sort = StringUtils.isEmpty(histogramConfigInfo.getSort()) ? xColumn : histogramConfigInfo.getSort(),
|
|
|
+ rule = StringUtils.isEmpty(histogramConfigInfo.getRule()) ? "ASC" : histogramConfigInfo.getRule();
|
|
|
+ List<String> groupBy = histogramConfigInfo.getGroups(), //分组
|
|
|
+ xAxisData = new ArrayList<String>(), //X轴自己用
|
|
|
+ xData = new ArrayList<String>(); //X轴前端用
|
|
|
+ List<Double> value = new ArrayList<Double>();
|
|
|
+ List<Series> serieses = new ArrayList<Series>(); //生成柱状图的系列
|
|
|
int id = histogramConfigInfo.getId();
|
|
|
- //获取列配置
|
|
|
- ChartsColumnConfig chartsColumn = chartsConfigMapper.getChartsColumn(id);
|
|
|
- if (chartsColumn == null || "".equals(chartsColumn)){
|
|
|
+ int baseId = getChartsDataUtilService.getBaseId(id);
|
|
|
+ List<Screen> screens = histogramConfigInfo.getFilters();
|
|
|
+ ScreenStr scr = new ScreenStr();
|
|
|
+ String screen = "", //正常筛选条件
|
|
|
+ screenToColumn = "", //跟目标列相同的筛选条件
|
|
|
+ screenToColumnS = ""; //跟目标列相同的筛选条件
|
|
|
+ String tableName = chartsUtilService.getSqlStr(token, id, dashId);
|
|
|
+ ChartsColumnConfig chartsColumnConfig = chartsConfigMapper.getChartsColumn(id);
|
|
|
+ /* 参数校验 */
|
|
|
+ if (StringUtils.isEmpty(xColumn) || StringUtils.isEmpty(yColumn)){
|
|
|
+ return new RepEntity(RepCode.nullAxis);
|
|
|
+ }
|
|
|
+ if (ObjectUtils.isEmpty(chartsColumnConfig)){
|
|
|
return new RepEntity(RepCode.Null);
|
|
|
}
|
|
|
- chartsDataInfo.setChartsColumnConfig(chartsColumn);
|
|
|
-
|
|
|
- String tableName = chartsUtilService.getSqlStr(token, id, dashId);
|
|
|
- if ("".equals(tableName)){
|
|
|
+ if (StringUtils.isEmpty(tableName)){
|
|
|
return new RepEntity(RepCode.NoAuthority);
|
|
|
}
|
|
|
-
|
|
|
- int baseId = getChartsDataUtilService.getBaseId(id);
|
|
|
-
|
|
|
+ /* 取数逻辑 */
|
|
|
try{
|
|
|
if (!DynamicDataSourceContextHolder.isContainsDataSource(baseId)) {
|
|
|
return new RepEntity(RepCode.DataSourceNull);
|
|
|
} else {
|
|
|
DynamicDataSourceContextHolder.setDataSourceType(baseId);
|
|
|
}
|
|
|
-
|
|
|
- String xColumn = histogramConfigInfo.getxAxis().getColumnRename();
|
|
|
- String yColumn = histogramConfigInfo.getyAxis().getColumnRename();
|
|
|
- String xColumnType = histogramConfigInfo.getxAxis().getColumnType();
|
|
|
-
|
|
|
- String xAxisType = histogramConfigInfo.getxAxis().getShowDataType();
|
|
|
- List<String> groupBy = histogramConfigInfo.getGroups();
|
|
|
-
|
|
|
- if (xColumn == null || "".equals(xColumn) || yColumn == null || "".equals(yColumn)){
|
|
|
- return new RepEntity(RepCode.nullAxis);
|
|
|
- }
|
|
|
-
|
|
|
- String yType = histogramConfigInfo.getyAxis().getShowDataType();
|
|
|
- String yAxisType = CalculationJudgeUtil.Judge(yType);
|
|
|
-
|
|
|
-
|
|
|
- Series series = new Series();
|
|
|
- List<Double> value = new ArrayList<Double>();
|
|
|
- List<Series> serieses = new ArrayList<Series>(); //生成柱状图的系列
|
|
|
- List<String> xAxisData = new ArrayList<String>(); //X轴自己用
|
|
|
- List<String> xData = new ArrayList<String>(); //X轴前端用
|
|
|
-
|
|
|
- //取筛选列表
|
|
|
- List<Screen> screens = histogramConfigInfo.getFilters();
|
|
|
- ScreenStr scr = new ScreenStr();
|
|
|
- String screen = ""; //正常筛选条件
|
|
|
- String screenToColumn = ""; //跟目标列相同的筛选条件
|
|
|
- String screenToColumnS = ""; //跟目标列相同的筛选条件
|
|
|
- if ("".equals(screens) || screens == null || screens.size() == 0){
|
|
|
- screen = "";
|
|
|
- }else {
|
|
|
+ //条件语句处理
|
|
|
+ if (!CollectionUtils.isEmpty(screens)){
|
|
|
scr = screenUtil.screensUtil(screens, xColumn, xColumnType);
|
|
|
screen = scr.getRet();
|
|
|
screenToColumn = scr.getWithColumnRet();
|
|
|
StringBuilder sb = new StringBuilder(screenToColumn);
|
|
|
- if (screenToColumn != null && !("".equals(screenToColumn))){
|
|
|
- screenToColumnS = screenToColumn + " " + screen; //and
|
|
|
- screenToColumn = String.valueOf(sb .replace(1, 5, "where "));
|
|
|
- screen = screenToColumn + " " + screen; //where
|
|
|
+ if (StringUtils.isEmpty(screenToColumn)){
|
|
|
+ screenToColumnS = screen; //and
|
|
|
} else {
|
|
|
- StringBuilder sb1 = new StringBuilder(screen);
|
|
|
- screenToColumnS = screen;
|
|
|
- screen = String.valueOf(sb1 .replace(1, 5, "where "));
|
|
|
+ screenToColumnS = sb.replace(1, 5, "") + screen;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- //X轴
|
|
|
- //判断是否为日期类型
|
|
|
- if ("time".equals(xColumnType)){
|
|
|
- timeReture = timeConverterUtil.timeConverter(xColumn, tableName, xAxisType, screen, histogramConfigInfo.getMaxCount());
|
|
|
- xAxisData = timeReture.getValues();
|
|
|
- chartsDataInfo.setTooMany(timeReture.isOverdose());
|
|
|
- xData = timeConverterUtil.toRespons(xAxisData,xAxisType );
|
|
|
}else {
|
|
|
- int count = showChartsMapper.getNumForX(xColumn, tableName);
|
|
|
- if (count > histogramConfigInfo.getMaxCount()){
|
|
|
- chartsDataInfo.setTooMany(true);
|
|
|
- }
|
|
|
- xAxisData = showChartsMapper.getXAxis(xColumn, tableName, screen, histogramConfigInfo.getMaxCount());
|
|
|
-
|
|
|
+ screenToColumnS = " 1 = 1 ";
|
|
|
}
|
|
|
- if ("".equals(xAxisData) || xAxisData == null || xAxisData.size() == 0){
|
|
|
- return new RepEntity(RepCode.Null);
|
|
|
- }
|
|
|
- if ("week".equals(xAxisType) || "month".equals(xAxisType) || "quarter".equals(xAxisType)){
|
|
|
- chartsDataInfo.setxAxis(xData);
|
|
|
- }else {
|
|
|
- chartsDataInfo.setxAxis(xAxisData);
|
|
|
+ chartsDataInfo.setChartsColumnConfig(chartsColumnConfig);
|
|
|
+ if ("time".equals(xColumnType)) {
|
|
|
+ xColumn = TimeConverterUtil.convertToOracleDateStr(xColumn, xAxisType);
|
|
|
}
|
|
|
- //无分组时Y值
|
|
|
- if (groupBy.size() == 0) {
|
|
|
- Iterator itX = xAxisData.iterator();
|
|
|
- while (itX.hasNext()) {
|
|
|
- String xdata = (String) itX.next();
|
|
|
- double valueOne = 0;
|
|
|
- String valueOnes = null;
|
|
|
- if ("time".equals(xColumnType)) {
|
|
|
- valueOnes = timeConverterUtil.getTimeValueConverter(yColumn, xColumn, tableName, yAxisType, xAxisType, xdata, screenToColumnS);
|
|
|
- } else {
|
|
|
- if (xdata == null || "".equals(xdata)) {
|
|
|
- valueOnes = showChartsMapper.getValuesIsNull(yColumn, xColumn, tableName, yAxisType, screenToColumnS);
|
|
|
- } else {
|
|
|
- valueOnes = showChartsMapper.getXValue(yColumn, xColumn, tableName, yAxisType, xdata, screenToColumnS);
|
|
|
- }
|
|
|
- }
|
|
|
- if (valueOnes == null || "".equals(valueOnes)) {
|
|
|
- valueOne = 0;
|
|
|
- } else {
|
|
|
- valueOne = Double.parseDouble(valueOnes);
|
|
|
- }
|
|
|
- value.add(valueOne);
|
|
|
+ String fieldName = yAxisType + "(" + yColumn + ") as value,nvl(" + xColumn + ",'空') as name ";
|
|
|
+ String condition = screenToColumnS + " GROUP BY " + xColumn;
|
|
|
+ sort = xColumn;
|
|
|
+ if (CollectionUtils.isEmpty(groupBy)) {
|
|
|
+ List<HistogramData> histogramList = showChartsMapper.getHistogramValueWithoutGroup(fieldName, tableName, condition, sort, rule);
|
|
|
+ if (histogramList.size() > histogramConfigInfo.getMaxCount()) {
|
|
|
+ chartsDataInfo.setTooMany(true);
|
|
|
}
|
|
|
-
|
|
|
+ histogramList = histogramList.subList(0, histogramConfigInfo.getMaxCount());
|
|
|
+ xAxisData = histogramList.stream().map(HistogramData::getName).collect(Collectors.toList());
|
|
|
+ chartsDataInfo.setxAxis(xAxisData);
|
|
|
+ value = histogramList.stream().map(HistogramData::getValue).collect(Collectors.toList());
|
|
|
series.setName(xColumn);
|
|
|
series.setValue(value);
|
|
|
serieses.add(series);
|
|
|
- }
|
|
|
- //有分组
|
|
|
- if (groupBy.size() != 0){
|
|
|
- Iterator<String> itGroup = groupBy.iterator(); //分组数
|
|
|
-
|
|
|
- //便利分组
|
|
|
- while (itGroup.hasNext()){
|
|
|
- String groupByName = itGroup.next(); //每个分组的组名
|
|
|
- int counts = showChartsMapper.getGroupsCount(groupByName,tableName, screenToColumnS);
|
|
|
- if (counts > histogramConfigInfo.getMaxCount()){
|
|
|
- chartsDataInfo.setTooMany(true);
|
|
|
- }
|
|
|
- List<String> groupsData = showChartsMapper.getGroups(groupByName,tableName, screenToColumnS, histogramConfigInfo.getMaxCount()); //查询每个分组系列
|
|
|
- //每个分组得到得每个系列
|
|
|
- for(int i = 0, len = groupsData.size() > 5 ? 5 : groupsData.size(); i < len; i++){
|
|
|
- Series ne = new Series();
|
|
|
- String groupsName = groupsData.get(i);
|
|
|
- ne.setName(groupsName);
|
|
|
- ne.setStack(groupByName);
|
|
|
-
|
|
|
- List<Double> groupsValue = new ArrayList<Double>();
|
|
|
- Iterator itXAxisData = xAxisData.iterator();
|
|
|
-
|
|
|
- //每个系列对应X轴上的值
|
|
|
- while (itXAxisData.hasNext()){
|
|
|
- String xAxisDataOne = (String) itXAxisData.next();
|
|
|
- double groupsValueOne = 0;
|
|
|
- String groupsValueOnes = null;
|
|
|
- if ("time".equals(xColumnType)){
|
|
|
- groupsValueOnes = timeConverterUtil.getGroupTimeConverter(yAxisType, yColumn, tableName, groupByName, xAxisType,
|
|
|
- groupsName, xColumn, xAxisDataOne, screenToColumnS);
|
|
|
- }else {
|
|
|
- String xColumnKey = "";
|
|
|
- if (xAxisDataOne == null || "".equals(xAxisDataOne)){
|
|
|
- xColumnKey = xColumn + " is null";
|
|
|
- }else {
|
|
|
- xColumnKey = xColumn + " = '" +xAxisDataOne +"'";
|
|
|
- }
|
|
|
-
|
|
|
- String groupByKey = "";
|
|
|
- if (groupsName == null || "".equals(groupsName)){
|
|
|
- groupByKey = groupByName +" is null";
|
|
|
- }else {
|
|
|
- groupByKey = groupByName + " = '" +groupsName +"'";
|
|
|
- }
|
|
|
- //groupsValue = showChartsMapper.getGroupsValue(yAxisType, yColumn, tableName, groupByKey, xColumnKey, screenToColumnS);
|
|
|
- groupsValueOnes = showChartsMapper.getGroupsValue(yAxisType, yColumn, tableName, groupByKey, xColumnKey, screenToColumnS);
|
|
|
- }
|
|
|
- if ("".equals(groupsValueOnes) || groupsValueOnes == null){
|
|
|
- groupsValueOne = 0;
|
|
|
- }else{
|
|
|
- groupsValueOne = Double.parseDouble(groupsValueOnes);
|
|
|
- }
|
|
|
-
|
|
|
- groupsValue.add(groupsValueOne);
|
|
|
+ }else {
|
|
|
+ fieldName = fieldName + ", nvl(" + groupBy.get(0) + ",'空') as groupName ";
|
|
|
+ condition = screenToColumnS;
|
|
|
+ String groupCondition = xColumn + "," + groupBy.get(0);
|
|
|
+ List<HistogramGroupData> histogramGroupList = showChartsMapper.getHistogramValueWithGroup(fieldName, tableName, condition, groupCondition, sort, rule);
|
|
|
+ //X轴数据
|
|
|
+ xAxisData = histogramGroupList.stream().map(HistogramGroupData::getName).distinct().collect(Collectors.toList());
|
|
|
+ if (CollectionUtils.isEmpty(xAxisData)) {
|
|
|
+ return new RepEntity(RepCode.Null);
|
|
|
+ }
|
|
|
+ if (xAxisData.size() > histogramConfigInfo.getMaxCount()) {
|
|
|
+ xAxisData = xAxisData.subList(0, histogramConfigInfo.getMaxCount());
|
|
|
+ }
|
|
|
+ chartsDataInfo.setxAxis(xAxisData);
|
|
|
+ /* 数据处理 */
|
|
|
+ List<String> groupList = histogramGroupList.stream().map(HistogramGroupData::getGroupName).distinct().limit(5).collect(Collectors.toList());
|
|
|
+ Map<String, List<HistogramGroupData>> histogramNameMap = histogramGroupList.stream().collect(Collectors.groupingBy(HistogramGroupData::getName));
|
|
|
+ Set<String> keyset = histogramNameMap.keySet();
|
|
|
+ Iterator<String> keyIt= keyset.iterator();
|
|
|
+ List<HistogramGroupData> histogramGroupListAfterHandle = new ArrayList<>();
|
|
|
+ while (keyIt.hasNext()) {
|
|
|
+ String key = keyIt.next();
|
|
|
+ List<HistogramGroupData> histogramGroupDataList = histogramNameMap.get(key);
|
|
|
+ groupList.forEach(groupName -> {
|
|
|
+ boolean matched = histogramGroupDataList.stream().noneMatch(histogramGroupData -> groupName.equals(histogramGroupData.getGroupName()));
|
|
|
+ if (matched) {
|
|
|
+ histogramGroupDataList.add(new HistogramGroupData(key, 0d, groupName));
|
|
|
}
|
|
|
- ne.setValue(groupsValue);
|
|
|
- serieses.add(ne);
|
|
|
- }
|
|
|
+ });
|
|
|
+ histogramGroupListAfterHandle.addAll(histogramGroupDataList);
|
|
|
}
|
|
|
+ /* 返回数据对象组装 */
|
|
|
+ int i = 0;
|
|
|
+ Map<String, List<HistogramGroupData>> histogramGroupMap = histogramGroupListAfterHandle.stream().sorted(Comparator.comparing(HistogramGroupData::getName)).filter(histogramGroupData -> groupList.contains(histogramGroupData.getGroupName())).collect(Collectors.groupingBy(HistogramGroupData::getGroupName));
|
|
|
+ Set<String> groupSet = histogramGroupMap.keySet();
|
|
|
+ Iterator<String> it= groupSet.iterator();
|
|
|
+ do {
|
|
|
+ i++;
|
|
|
+ String key = it.next();
|
|
|
+ List<HistogramGroupData> histogramGroupDataList = histogramGroupMap.get(key);
|
|
|
+ value = histogramGroupDataList.stream().map(HistogramGroupData::getValue).collect(Collectors.toList()).subList(0, xAxisData.size());
|
|
|
+ series = new Series();
|
|
|
+ series.setName(key);
|
|
|
+ series.setValue(value);
|
|
|
+ series.setStack(groupBy.get(0));
|
|
|
+ serieses.add(series);
|
|
|
+ }while (it.hasNext() && i <= 20);
|
|
|
}
|
|
|
-
|
|
|
chartsDataInfo.setSerieses(serieses);
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
@@ -241,5 +183,4 @@ public class ShowHistogramService {
|
|
|
return new RepEntity(RepCode.success, chartsDataInfo);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
}
|