chenw 6 лет назад
Родитель
Сommit
a8ea5c34c6

+ 1 - 1
bi-server/src/main/java/com/usoftchina/bi/server/controller/chart/ShowChartsController.java

@@ -43,7 +43,7 @@ public class ShowChartsController {
     @CheckToken
     @PostMapping("/showHistogram")
     public RepEntity showHistogram(@RequestHeader String token,@RequestBody HistogramConfigInfo body){
-        return showHistogramService.showHistogram(body, token, 0, null);
+        return showHistogramService.showHistogram(body, token, 0);
     }
 
     /*

+ 23 - 3
bi-server/src/main/java/com/usoftchina/bi/server/dao/chart/ShowChartsMapper.java

@@ -1,9 +1,7 @@
 package com.usoftchina.bi.server.dao.chart;
 
 import com.usoftchina.bi.core.base.TestPage;
-import com.usoftchina.bi.server.model.bo.LineSeriesGroupMap;
-import com.usoftchina.bi.server.model.bo.LineSeriesMap;
-import com.usoftchina.bi.server.model.bo.PieSeriesMap;
+import com.usoftchina.bi.server.model.bo.*;
 import com.usoftchina.bi.server.model.po.TargetData;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
@@ -258,4 +256,26 @@ public interface ShowChartsMapper {
     @Select("select * from (select distinct ${columnName} from ${tableName} )where ${columnName} like '%'||#{keyword,jdbcType=VARCHAR}||'%' and rownum <= #{count, jdbcType=INTEGER}")
     List<Object> getScreenData(@Param("columnName") String columnName, @Param("tableName") String tableName, @Param("keyword") String keyword, @Param("count") int count);
 
+    /**
+     * 柱状图无分组取数
+     * @param fieldName
+     * @param columnName
+     * @param condition
+     * @return
+     */
+    @Select("SELECT ${fieldName} FROM ${tableName} WHERE ${condition} ORDER BY ${sort} ${rule}")
+    List<HistogramData> getHistogramValueWithoutGroup(@Param("fieldName") String fieldName, @Param("tableName") String columnName, @Param("condition") String condition,
+                                                      @Param("sort") String sort, @Param("rule") String rule);
+
+    /**
+     * 柱状图分组取数
+     * @param fieldName
+     * @param columnName
+     * @param condition
+     * @param groupCondition
+     * @return
+     */
+    @Select("SELECT ${fieldName} FROM ${tableName} WHERE ${condition} GROUP BY ${groupCondition} ORDER BY ${sort} ${rule}")
+    List<HistogramGroupData> getHistogramValueWithGroup(@Param("fieldName") String fieldName, @Param("tableName") String columnName, @Param("condition") String condition, @Param("groupCondition") String groupCondition,
+                                                        @Param("sort") String sort, @Param("rule") String rule);
 }

+ 28 - 0
bi-server/src/main/java/com/usoftchina/bi/server/model/bo/HistogramData.java

@@ -0,0 +1,28 @@
+package com.usoftchina.bi.server.model.bo;
+
+/**
+ * @Author chenwei
+ * @Date 2019-06-21
+ */
+public class HistogramData {
+
+    private String name;
+
+    private Double value;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Double getValue() {
+        return value;
+    }
+
+    public void setValue(Double value) {
+        this.value = value;
+    }
+}

+ 48 - 0
bi-server/src/main/java/com/usoftchina/bi/server/model/bo/HistogramGroupData.java

@@ -0,0 +1,48 @@
+package com.usoftchina.bi.server.model.bo;
+
+/**
+ * @Author chenwei
+ * @Date 2019-06-21
+ */
+public class HistogramGroupData {
+
+    private String name;
+
+    private Double value;
+
+    private String groupName;
+
+    public String getGroupName() {
+        return groupName;
+    }
+
+    public void setGroupName(String groupName) {
+        this.groupName = groupName;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Double getValue() {
+        return value;
+    }
+
+    public void setValue(Double value) {
+        this.value = value;
+    }
+
+    public HistogramGroupData() {
+    }
+
+    public HistogramGroupData(String name, Double value, String groupName) {
+
+        this.name = name;
+        this.value = value;
+        this.groupName = groupName;
+    }
+}

+ 9 - 0
bi-server/src/main/java/com/usoftchina/bi/server/model/bo/Series.java

@@ -31,6 +31,15 @@ public class Series {
         this.stack = stack;
     }
 
+    public Series(String name, List<Double> value, String stack) {
+        this.name = name;
+        this.value = value;
+        this.stack = stack;
+    }
+
+    public Series() {
+    }
+
     @Override
     public String toString() {
         return "Series{" +

+ 19 - 0
bi-server/src/main/java/com/usoftchina/bi/server/model/vo/configVo/HistogramConfigInfo.java

@@ -14,11 +14,30 @@ public class HistogramConfigInfo {
     private Column yAxis;
     private List<String> groups;
     private List<Screen> filters;
+    private String sort;
+    private String rule;
+
     /**
      * 需要展示的条数
      */
     private int maxCount = 20;
 
+    public String getSort() {
+        return sort;
+    }
+
+    public void setSort(String sort) {
+        this.sort = sort;
+    }
+
+    public String getRule() {
+        return rule;
+    }
+
+    public void setRule(String rule) {
+        this.rule = rule;
+    }
+
     public int getMaxCount() {
         return maxCount;
     }

+ 103 - 162
bi-server/src/main/java/com/usoftchina/bi/server/service/chart/ShowHistogramService.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.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);
     }
 
-
 }

+ 17 - 0
bi-server/src/main/java/com/usoftchina/bi/server/service/chart/TimeConverterUtil.java

@@ -295,4 +295,21 @@ public class TimeConverterUtil {
         }
         return showChartsMapper.getHistogramTimeValueGroupValuesTime(dataType, yColumn, tableName, groupByName, groupsName, xColumn, timeGroup);
     }
+
+    public static String convertToOracleDateStr(String xColumn, String timeType) {
+        if ("year".equals(timeType)){
+            return "TO_CHAR(" + xColumn + ", 'YYYY')";
+        }else if("month".equals(timeType)) {
+            return "TO_CHAR(" + xColumn + ", 'YYYY-MM')";
+        }else if ("day".equals(timeType)) {
+            return "TO_CHAR(" + xColumn + ", 'YYYY-MM-DD')";
+        }else if ("quarter".equals(timeType)) {
+            return "TO_CHAR(" + xColumn + ", 'YYYY-Q')";
+        }else if ("week".equals(timeType)) {
+            return "TO_CHAR(" + xColumn + ", 'YYYY-WW')";
+        }else {
+            return "TO_CHAR(" + xColumn + ", 'YYYY-MM-DD')";
+        }
+    }
+
 }

+ 1 - 1
bi-server/src/main/java/com/usoftchina/bi/server/service/dashboard/DashboardsToChartsUtilService.java

@@ -99,7 +99,7 @@ public class DashboardsToChartsUtilService {
         } catch (Exception e) {
             e.printStackTrace();
         }
-        return showHistogramService.showHistogram(histogramConfigInfo, token, chartId, styleConfig);
+        return showHistogramService.showHistogram(histogramConfigInfo, token, chartId);
     }
 
     public RepEntity getScatter(String fetchConfig, String token, List<Screen> filters, int chartId, String styleConfig){