|
|
@@ -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,10 +13,14 @@ 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.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class ShowPieService {
|
|
|
@@ -36,134 +41,82 @@ public class ShowPieService {
|
|
|
Pie数据展示
|
|
|
*/
|
|
|
public RepEntity showPie(PieConfigInfo pieConfigInfo, String token, int dashId, String styleConfig){
|
|
|
+ Assert.notNull(pieConfigInfo, "请求参数不能为空");
|
|
|
+ /* 变量定义 */
|
|
|
ChartsDataInfo<PieSeries> chartsDataInfo = new ChartsDataInfo();
|
|
|
- chartsDataInfo.setStyleConfig(styleConfig);
|
|
|
+ String xColumn = pieConfigInfo.getLegendData().getColumnRename(),
|
|
|
+ yColumn = pieConfigInfo.getSeries().getColumnRename(),
|
|
|
+ xColumnType = pieConfigInfo.getLegendData().getColumnType(),
|
|
|
+ xAxisType = pieConfigInfo.getLegendData().getShowDataType(),
|
|
|
+ yType = pieConfigInfo.getSeries().getShowDataType(),
|
|
|
+ yAxisType = CalculationJudgeUtil.Judge(yType);
|
|
|
+ PieSeries series = new PieSeries();
|
|
|
+ List<PieSeriesMap> value = new ArrayList<PieSeriesMap>();
|
|
|
+ List<PieSeries> serieses = new ArrayList<PieSeries>(); //生成柱状图的系列
|
|
|
+ List<String> xAxisData = new ArrayList<String>(); //X轴自己用
|
|
|
+ List<String> xData = new ArrayList<String>(); //X轴前端用
|
|
|
+
|
|
|
+ List<Screen> filters = pieConfigInfo.getFilters();
|
|
|
+ ScreenStr scr = new ScreenStr();
|
|
|
+ String screen = "", //正常筛选条件
|
|
|
+ screenToColumn = "", //跟目标列相同的筛选条件
|
|
|
+ screenToColumnS = ""; //跟目标列相同的筛选条件
|
|
|
+
|
|
|
TimeReture timeReture = new TimeReture();
|
|
|
- if (pieConfigInfo == null || "".equals(pieConfigInfo)){
|
|
|
- return new RepEntity(RepCode.Null);
|
|
|
- }
|
|
|
int id = pieConfigInfo.getId();
|
|
|
- //获取列配置
|
|
|
+ int baseId = getChartsDataUtilService.getBaseId(id);
|
|
|
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);
|
|
|
}
|
|
|
- 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 = pieConfigInfo.getLegendData().getColumnRename();
|
|
|
- String yColumn = pieConfigInfo.getSeries().getColumnRename();
|
|
|
- String xColumnType = pieConfigInfo.getLegendData().getColumnType();
|
|
|
-
|
|
|
- String xAxisType = pieConfigInfo.getLegendData().getShowDataType();
|
|
|
-
|
|
|
- if (xColumn == null || "".equals(xColumn) || yColumn == null || "".equals(yColumn)){
|
|
|
- return new RepEntity(RepCode.nullAxis);
|
|
|
- }
|
|
|
-
|
|
|
- String yType = pieConfigInfo.getSeries().getShowDataType();
|
|
|
- String yAxisType = CalculationJudgeUtil.Judge(yType);
|
|
|
-
|
|
|
-
|
|
|
- PieSeries series = new PieSeries();
|
|
|
- List<PieSeriesMap> value = new ArrayList<PieSeriesMap>();
|
|
|
- List<PieSeries> serieses = new ArrayList<PieSeries>(); //生成柱状图的系列
|
|
|
- List<String> xAxisData = new ArrayList<String>(); //X轴自己用
|
|
|
- List<String> xData = new ArrayList<String>(); //X轴前端用
|
|
|
-
|
|
|
- //取筛选列表
|
|
|
- List<Screen> screens = pieConfigInfo.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);
|
|
|
- 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
|
|
|
- } else {
|
|
|
- StringBuilder sb1 = new StringBuilder(screen);
|
|
|
- screenToColumnS = screen;
|
|
|
- screen = String.valueOf(sb1 .replace(1, 5, "where "));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //X轴
|
|
|
- //判断是否为日期类型
|
|
|
- if ("time".equals(xColumnType)){
|
|
|
- timeReture = timeConverterUtil.timeConverter(xColumn, tableName, xAxisType, screen, pieConfigInfo.getMaxCount());
|
|
|
- xAxisData = timeReture.getValues();
|
|
|
- chartsDataInfo.setTooMany(timeReture.isOverdose());
|
|
|
- xData = timeConverterUtil.toRespons(xAxisData,xAxisType );
|
|
|
- }else {
|
|
|
- int count = showChartsMapper.getNumForX(xColumn, tableName);
|
|
|
- if (count > pieConfigInfo.getMaxCount()){
|
|
|
- chartsDataInfo.setTooMany(true);
|
|
|
- }
|
|
|
- xAxisData = showChartsMapper.getXAxis(xColumn, tableName, screen, pieConfigInfo.getMaxCount());
|
|
|
-
|
|
|
- }
|
|
|
- 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);
|
|
|
- }
|
|
|
-
|
|
|
- //无分组时Y值
|
|
|
- Iterator itX = xAxisData.iterator();
|
|
|
- while (itX.hasNext()){
|
|
|
- String xdata = (String)itX.next();
|
|
|
- if ("".equals(xdata) || xdata == null){
|
|
|
- //continue;
|
|
|
- }
|
|
|
- double valueOne = 0;
|
|
|
- String valueOnes = null;
|
|
|
- PieSeriesMap pieSeriesMap = new PieSeriesMap();
|
|
|
- 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 (!CollectionUtils.isEmpty(filters)){
|
|
|
+ scr = screenUtil.screensUtil(filters, xColumn, xAxisType);
|
|
|
+ screen = scr.getRet();
|
|
|
+ screenToColumn = scr.getWithColumnRet();
|
|
|
+ if (!StringUtils.isEmpty(screenToColumn)){
|
|
|
+ screenToColumnS = screenToColumn.startsWith(" and ") ? (screenToColumn.substring(5) + " " + screen) : (screenToColumn+ " " + screen);
|
|
|
+ } else {
|
|
|
+ screenToColumnS = screen.startsWith(" and ") ? screen.substring(5) : screen;
|
|
|
}
|
|
|
- }
|
|
|
- if (valueOnes == null || "".equals(valueOnes)){
|
|
|
- valueOne = 0;
|
|
|
}else {
|
|
|
- valueOne = Double.parseDouble(valueOnes);
|
|
|
+ screenToColumnS = " 1 = 1 ";
|
|
|
}
|
|
|
- pieSeriesMap.setName(xdata);
|
|
|
- pieSeriesMap.setValue(valueOne);
|
|
|
- value.add(pieSeriesMap);
|
|
|
- }
|
|
|
|
|
|
- series.setName(pieConfigInfo.getSeries().getColumnName());
|
|
|
- series.setValue(value);
|
|
|
- serieses.add(series);
|
|
|
+ //日期类型
|
|
|
+ if ("time".equals(xColumnType)) {
|
|
|
+ xColumn = TimeConverterUtil.convertToOracleDateStr(xColumn, xAxisType);
|
|
|
+ }
|
|
|
+ String fieldName = yAxisType + "(" + yColumn + ") as value,nvl(" + xColumn + ",'空') as name ";
|
|
|
+ String condition = screenToColumnS + " GROUP BY " + xColumn;
|
|
|
+ value = showChartsMapper.getPieValueWithoutGroup(fieldName, tableName, condition);
|
|
|
+ xAxisData = value.stream().map(PieSeriesMap::getName).collect(Collectors.toList());
|
|
|
+ if (CollectionUtils.isEmpty(xAxisData)) {
|
|
|
+ return new RepEntity(RepCode.Null);
|
|
|
+ }
|
|
|
+ chartsDataInfo.setxAxis(xAxisData);
|
|
|
|
|
|
- chartsDataInfo.setSerieses(serieses);
|
|
|
+ series.setName(pieConfigInfo.getSeries().getColumnName());
|
|
|
+ series.setValue(value);
|
|
|
+ serieses.add(series);
|
|
|
+ chartsDataInfo.setStyleConfig(styleConfig);
|
|
|
+ chartsDataInfo.setChartsColumnConfig(chartsColumn);
|
|
|
+ chartsDataInfo.setSerieses(serieses);
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
if (e.getCause().getMessage().contains("ORA-00904:")) {
|