|
|
@@ -13,11 +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.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 ShowLineService {
|
|
|
@@ -38,132 +41,89 @@ public class ShowLineService {
|
|
|
Line数据展示
|
|
|
*/
|
|
|
public RepEntity showLine(LineConfigInfo lineConfigInfo, String token, int dashId, String styleConfig){
|
|
|
+ Assert.notNull(lineConfigInfo, "请求参数不能为空");
|
|
|
+ /* 变量定义 */
|
|
|
ChartsDataInfo chartsDataInfo = new ChartsDataInfo();
|
|
|
- chartsDataInfo.setStyleConfig(styleConfig);
|
|
|
+ String xColumn = lineConfigInfo.getxAxis().getColumnRename(),
|
|
|
+ yColumn = lineConfigInfo.getyAxis().getColumnRename(),
|
|
|
+ yType = lineConfigInfo.getyAxis().getShowDataType(),
|
|
|
+ yAxisType = CalculationJudgeUtil.Judge(yType),
|
|
|
+ xAxisType = lineConfigInfo.getxAxis().getShowDataType();
|
|
|
+ List<String> groupBy = lineConfigInfo.getGroups(),
|
|
|
+ xAxisData = new ArrayList<String>(); //X轴自己用
|
|
|
+ List<LineSeries> serieses = new ArrayList<LineSeries>(); //生成柱状图的系列
|
|
|
+ List<Screen> filters = lineConfigInfo.getFilters();
|
|
|
+ ScreenStr scr = new ScreenStr();
|
|
|
+ String screen = "", //正常筛选条件
|
|
|
+ screenToColumn = "", //跟目标列相同的筛选条件
|
|
|
+ screenToColumnS = ""; //跟目标列相同的筛选条件
|
|
|
TimeReture timeReture = new TimeReture();
|
|
|
- if (lineConfigInfo == null || "".equals(lineConfigInfo)){
|
|
|
- return new RepEntity(RepCode.Null);
|
|
|
- }
|
|
|
int id = lineConfigInfo.getId();
|
|
|
- //获取列配置
|
|
|
+ int baseId = getChartsDataUtilService.getBaseId(id);
|
|
|
ChartsColumnConfig chartsColumn = chartsConfigMapper.getChartsColumn(id);
|
|
|
- if (chartsColumn == null || "".equals(chartsColumn)){
|
|
|
+ String tableName = chartsUtilService.getSqlStr(token, id, dashId);
|
|
|
+ /* 参数校验 */
|
|
|
+ if (StringUtils.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 = lineConfigInfo.getxAxis().getColumnRename();
|
|
|
- String yColumn = lineConfigInfo.getyAxis().getColumnRename();
|
|
|
-
|
|
|
- String xAxisType = lineConfigInfo.getxAxis().getShowDataType();
|
|
|
- List<String> groupBy = lineConfigInfo.getGroups();
|
|
|
-
|
|
|
- if (xColumn == null || "".equals(xColumn) || yColumn == null || "".equals(yColumn)){
|
|
|
- return new RepEntity(RepCode.nullAxis);
|
|
|
- }
|
|
|
-
|
|
|
- String yType = lineConfigInfo.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<Screen> screens = lineConfigInfo.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, 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
|
|
|
- } else {
|
|
|
- StringBuilder sb1 = new StringBuilder(screen);
|
|
|
- screenToColumnS = screen;
|
|
|
- screen = String.valueOf(sb1 .replace(1, 5, "where "));
|
|
|
+ //条件语句处理
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ screenToColumnS = " 1 = 1 ";
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- //X轴
|
|
|
- //判断是否为日期类型
|
|
|
- timeReture = timeConverterUtil.timeConverter(xColumn, tableName, xAxisType, screen, lineConfigInfo.getMaxCount());
|
|
|
- xAxisData = timeReture.getValues();
|
|
|
- chartsDataInfo.setTooMany(timeReture.isOverdose());
|
|
|
-
|
|
|
- if ("".equals(xAxisData) || xAxisData == null || xAxisData.size() == 0){
|
|
|
- return new RepEntity(RepCode.Null);
|
|
|
- }
|
|
|
-
|
|
|
- //无分组时Y值
|
|
|
- value = showChartsMapper.getTimeValues(yAxisType, yColumn, tableName, xColumn, screen);
|
|
|
- LineSeries series = new LineSeries();
|
|
|
- series.setMdata(value);
|
|
|
-
|
|
|
- serieses.add(series);
|
|
|
-
|
|
|
- //有分组
|
|
|
- if (groupBy.size() != 0){
|
|
|
- serieses.remove(0);
|
|
|
- Iterator itGroup = groupBy.iterator(); //分组数
|
|
|
-
|
|
|
- //遍历分组
|
|
|
- while (itGroup.hasNext()){
|
|
|
- String groupByName = (String) itGroup.next();
|
|
|
- List<String> groupsData = showChartsMapper.getGroups(groupByName,tableName, screenToColumnS, lineConfigInfo.getMaxCount()); //查询每个分组系列
|
|
|
- if (groupsData.size() == 0)
|
|
|
- continue;
|
|
|
- Iterator itGroupsData = groupsData.iterator();
|
|
|
- if (groupsData.size() > lineConfigInfo.getMaxCount()){
|
|
|
+ xColumn = TimeConverterUtil.convertToOracleDateStr(xColumn, xAxisType); //X轴 -> 折线图X轴只能为日期类型
|
|
|
+ String fieldName = yAxisType + "(" + yColumn + ") as value,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() > lineConfigInfo.getMaxCount()) {
|
|
|
chartsDataInfo.setTooMany(true);
|
|
|
}
|
|
|
- String groupsName = (String)itGroupsData.next();
|
|
|
-
|
|
|
- List<LineSeriesGroupMap> lineSeriesGroupMapList = showChartsMapper.getGroupsValuesTime(yAxisType, yColumn, tableName, groupByName, groupsName, xColumn, "'YYYY-MM-DD'",screenToColumnS);
|
|
|
- Map<String, List<LineSeriesGroupMap>> lineSeriesGroupMap = CollectionUtils.groupBy(lineSeriesGroupMapList, LineSeriesGroupMap::getGroup);
|
|
|
+ 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();
|
|
|
- LineSeries lineSeries = new LineSeries();
|
|
|
- lineSeries.setName(entry.getKey());
|
|
|
lineSeriesMapList = new ArrayList<>();
|
|
|
copyProperties(entry.getValue(), lineSeriesMapList);
|
|
|
- lineSeries.setMdata(lineSeriesMapList);
|
|
|
- serieses.add(lineSeries);
|
|
|
+ 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:")) {
|