Browse Source

散点图取数优化

chenw 6 years ago
parent
commit
99637fe060

+ 88 - 179
bi-server/src/main/java/com/usoftchina/bi/server/service/chart/ShowScatterService.java

@@ -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.usoftchina.bi.server.model.bo.*;
@@ -12,11 +13,15 @@ import com.usoftchina.bi.core.jdbc.DynamicDataSourceContextHolder;
 import com.usoftchina.bi.server.utils.ScreenUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+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.Map;
+import java.util.stream.Collectors;
 
 @Service
 public class ShowScatterService {
@@ -37,208 +42,100 @@ public class ShowScatterService {
     Line数据展示
      */
     public RepEntity showScatter(ScatterConfigInfo scatterConfigInfo, String token, int dashId, String styleConfig){
+        Assert.notNull(scatterConfigInfo, "请求参数不能为空");
+        /* 变量定义 */
         ChartsDataInfo chartsDataInfo = new ChartsDataInfo();
-        chartsDataInfo.setStyleConfig(styleConfig);
-        TimeReture timeReture = new TimeReture();
-        if (scatterConfigInfo == null || "".equals(scatterConfigInfo)){
-            return new RepEntity(RepCode.Null);
-        }
+        String xColumn = scatterConfigInfo.getxAxis().getColumnRename(),
+               yColumn = scatterConfigInfo.getyAxis().getColumnRename(),
+               xColumnType = scatterConfigInfo.getxAxis().getColumnType(),
+               yType = scatterConfigInfo.getyAxis().getShowDataType(),
+               yAxisType = CalculationJudgeUtil.Judge(yType),
+               xAxisType = scatterConfigInfo.getxAxis().getShowDataType();
+        List<String> groupBy = scatterConfigInfo.getGroups();
+        List<LineSeriesMap> value = new ArrayList<LineSeriesMap>();
+        List<LineSeries> serieses = new ArrayList<LineSeries>();        //生成柱状图的系列
+        List<String> xAxisData = new ArrayList<String>();           //X轴自己用
+        List<String> xData = new ArrayList<String>();               //X轴前端用
+        List<Screen> filters = scatterConfigInfo.getFilters();
+        ScreenStr scr = new ScreenStr();
+        String screen = "",                     //正常筛选条件
+               screenToColumn = "",             //跟目标列相同的筛选条件
+               screenToColumnS = "";            //跟目标列相同的筛选条件
         int id = scatterConfigInfo.getId();
-        //获取列配置
+        int baseId = getChartsDataUtilService.getBaseId(id);
+        TimeReture timeReture = new TimeReture();
         ChartsColumnConfig chartsColumn = chartsConfigMapper.getChartsColumn(id);
-        if (chartsColumn == null || "".equals(chartsColumn)){
+        String tableName = chartsUtilService.getSqlStr(token, id, dashId);
+        /* 参数校验 */
+        if (ObjectUtils.isEmpty(chartsColumn)){
             return new RepEntity(RepCode.Null);
         }
-        String style = chartsColumn.getChartStyle();
-        String config = chartsColumn.getChartConfig();
-        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);
-
+        if (StringUtils.isEmpty(xColumn) || StringUtils.isEmpty(yColumn)){
+            return new RepEntity(RepCode.nullAxis);
+        }
+         /* 取数逻辑 */
         try{
             if (!DynamicDataSourceContextHolder.isContainsDataSource(baseId)) {
                 return new RepEntity(RepCode.DataSourceNull);
             } else {
                 DynamicDataSourceContextHolder.setDataSourceType(baseId);
             }
-
-        String xColumn = scatterConfigInfo.getxAxis().getColumnRename();
-        String yColumn = scatterConfigInfo.getyAxis().getColumnRename();
-        String xColumnType = scatterConfigInfo.getxAxis().getColumnType();
-
-        String xAxisType = scatterConfigInfo.getxAxis().getShowDataType();
-        List<String> groupBy = scatterConfigInfo.getGroups();
-
-        if (xColumn == null || "".equals(xColumn) || yColumn == null || "".equals(yColumn)){
-            return new RepEntity(RepCode.nullAxis);
-        }
-
-        String yType = scatterConfigInfo.getyAxis().getShowDataType();
-        String yAxisType = CalculationJudgeUtil.Judge(yType);
-
-
-
-        List<LineSeriesMap> value = new ArrayList<LineSeriesMap>();
-        List<LineSeries> serieses = new ArrayList<LineSeries>();    //生成柱状图的系列
-        List<String> xAxisData = new ArrayList<String>();  //X轴自己用
-        List<String> xData = new ArrayList<String>();      //X轴前端用
-
-            //取筛选列表
-            List<Screen> screens = scatterConfigInfo.getFilters();
-            ScreenStr scr = new ScreenStr();
-            String screen = "";               //正常筛选条件
-            String screenToColumn = "";       //跟目标列相同的筛选条件
-            String screenToColumnS = "";       //跟目标列相同的筛选条件
-            if ("".equals(screens) || screens == null || screens.size() == 0){
-                screen = "";
-            }else {
-                scr = screenUtil.screensUtil(screens, xColumn, xColumnType);
+            //条件语句处理
+            if (!CollectionUtils.isEmpty(filters)){
+                scr = screenUtil.screensUtil(filters, xColumn, xAxisType);
                 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 = screenToColumn.startsWith(" and ") ? (screenToColumn.substring(5) + " " + screen) : (screenToColumn+ " " + screen);
                 } else {
-                    StringBuilder sb1 = new StringBuilder(screen);
-                    screenToColumnS = screen;
-                    screen = String.valueOf(sb1 .replace(1, 5, "where "));
+                    screenToColumnS = screen.startsWith(" and ") ? screen.substring(5) : screen;
                 }
+            }else {
+                screenToColumnS = " 1 = 1 ";
             }
-
-        //X轴
-        //判断是否为日期类型
-        if ("time".equals(xColumnType)){
-            timeReture = timeConverterUtil.timeConverter(xColumn, tableName, xAxisType, screen, scatterConfigInfo.getMaxCount());
-            xAxisData = timeReture.getValues();
-            chartsDataInfo.setTooMany(timeReture.isOverdose());
-            xData = timeConverterUtil.toRespons(xAxisData,xAxisType );
-        }else {
-                int count = showChartsMapper.getNumForX(xColumn, tableName);
-                if (count > scatterConfigInfo.getMaxCount()){
-                    chartsDataInfo.setTooMany(true);
-                }
-            xAxisData = showChartsMapper.getXAxis(xColumn, tableName, screen, scatterConfigInfo.getMaxCount());
-
-        }
-
-        if ("".equals(xAxisData) || xAxisData == null || xAxisData.size() == 0){
-            return new RepEntity(RepCode.Null);
-        }
-        if (groupBy.size() == 0) {
-            //无分组时Y值
-            if (!"time".equals(xColumnType)) {
-                if (StringUtils.isEmpty(screenToColumnS)) {
-                    screenToColumnS = " and 1 = 1 ";
-                }
-                value = showChartsMapper.getXAndYValues(yColumn, xColumn, tableName, yAxisType, screenToColumnS);
-            } else {
-                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);
-                    }
-                    LineSeriesMap lineSeriesMap = new LineSeriesMap();
-                    lineSeriesMap.setValue(valueOne);
-                    lineSeriesMap.setDate(xdata);
-                    value.add(lineSeriesMap);
-                }
+            //日期类型
+            if ("time".equals(xColumnType)){
+                xColumn = TimeConverterUtil.convertToOracleDateStr(xColumn, xAxisType);         //X轴
             }
-            LineSeries series = new LineSeries();
-//        series.setName(yColumn);
-            series.setMdata(value);
-
-            serieses.add(series);
-        }
-        //有分组
-        if (groupBy.size() != 0){
-            Iterator itGroup = groupBy.iterator();  //分组数
-
-            //便利分组
-            while (itGroup.hasNext()){
-                String groupByName = (String) itGroup.next();
-                int counts = showChartsMapper.getGroupsCount(groupByName,tableName, screenToColumnS);
-                if (counts > 50){
+            String fieldName = yAxisType
+                    + "(" + yColumn + ") as value,"
+                    + ("scale".equals(xColumnType) ? "nvl(" + xColumn + ",0)" : "nvl(" + xColumn + ",'空')")
+                    + " as \"date\" ";
+            String condition = screenToColumnS + " GROUP BY " + xColumn;
+            String sort = xColumn, rule = "ASC";
+            if (CollectionUtils.isEmpty(groupBy)) {
+                List<LineSeriesMap> lineSeriesMapList = showChartsMapper.getLineValueWithoutGroup(fieldName, tableName, condition, sort, rule);
+                xAxisData = lineSeriesMapList.stream().map(LineSeriesMap::getDate).collect(Collectors.toList());
+                if (CollectionUtils.isEmpty(xAxisData)) {
+                    return new RepEntity(RepCode.Null);
+                }
+                if (xAxisData.size() > scatterConfigInfo.getMaxCount()) {
                     chartsDataInfo.setTooMany(true);
+                    xAxisData = xAxisData.subList(0, scatterConfigInfo.getMaxCount());
                 }
-                List<String> groupsData = showChartsMapper.getGroups(groupByName,tableName, screenToColumnS, 50); //查询每个分组系列
-                Iterator itGroupsData = groupsData.iterator();
-
-                //每个分组得到得每个系列
-                while(itGroupsData.hasNext()){
-                    String groupsName = (String)itGroupsData.next();
-                    if (groupsName == null || "".equals(groupsName)){
-                        continue;
-                    }
-
-                    List<LineSeriesMap> groupsValue = new ArrayList<LineSeriesMap>();
-                    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 +"'";
-                            }
-                            groupsValueOnes = showChartsMapper.getGroupsValue(yAxisType, yColumn, tableName, groupByKey, xColumnKey ,screenToColumnS);
-                        }
-                        if ("".equals(groupsValueOnes) || groupsValueOnes == null){
-                            groupsValueOne = 0;
-                        }else{
-                            groupsValueOne = Double.parseDouble(groupsValueOnes);
-                        }
-                        LineSeriesMap lineSeriesMap = new LineSeriesMap();
-                        lineSeriesMap.setDate(xAxisDataOne);
-                        lineSeriesMap.setValue(groupsValueOne);
-                        groupsValue.add(lineSeriesMap);
-                    }
-
-                    LineSeries lineSeries = new LineSeries();
-                    lineSeries.setMdata(groupsValue);
-                    lineSeries.setName(groupsName);
-                    serieses.add(lineSeries);
+                chartsDataInfo.setxAxis(xAxisData);
+                serieses.add(new LineSeries(null, lineSeriesMapList));
+            }else {
+                fieldName = fieldName + ", nvl(" + groupBy.get(0) + ",'空') as groupName ";
+                condition = screenToColumnS;
+                String groupCondition = xColumn + "," + groupBy.get(0);
+                List<LineSeriesGroupMap> lineSeriesGroupMapList = showChartsMapper.getLineValueWithGroup(fieldName, tableName, condition, groupCondition,sort, rule);
+                Map<String, List<LineSeriesGroupMap>> lineSeriesGroupMap = CollectionUtils.groupBy(lineSeriesGroupMapList, LineSeriesGroupMap::getGroupName);
+                Iterator<Map.Entry<String, List<LineSeriesGroupMap>>> it = lineSeriesGroupMap.entrySet().iterator();
+                List<LineSeriesMap> lineSeriesMapList = null;
+                while (it.hasNext()) {
+                    Map.Entry<String, List<LineSeriesGroupMap>> entry = it.next();
+                    lineSeriesMapList = new ArrayList<>();
+                    copyProperties(entry.getValue(), lineSeriesMapList);
+                    serieses.add(new LineSeries(entry.getKey(), lineSeriesMapList));
                 }
             }
-        }
-
-        chartsDataInfo.setSerieses(serieses);
+            chartsDataInfo.setStyleConfig(styleConfig);
+            chartsDataInfo.setChartsColumnConfig(chartsColumn);
+            chartsDataInfo.setSerieses(serieses);
         }catch (Exception e){
             e.printStackTrace();
             if (e.getCause().getMessage().contains("ORA-00904:")) {
@@ -252,5 +149,17 @@ public class ShowScatterService {
         return new RepEntity(RepCode.success, chartsDataInfo);
     }
 
+    /**
+     * LineSeriesGroupMap 转 LineSeriesMap
+     * @param source
+     * @param target
+     */
+    private void copyProperties(List<LineSeriesGroupMap> source, List<LineSeriesMap> target){
+        if (!CollectionUtils.isEmpty(source)) {
+            for (LineSeriesGroupMap lineSeriesGroupMap : source) {
+                target.add(new LineSeriesMap(lineSeriesGroupMap.getDate(), lineSeriesGroupMap.getValue()));
+            }
+        }
+    }
 
 }