From bbe7acfbe5a4c08d6edc91eaf81dcecf9d630e18 Mon Sep 17 00:00:00 2001
From: 潘志宝 <979469083@qq.com>
Date: 星期日, 15 十二月 2024 15:10:50 +0800
Subject: [PATCH] collectQuality

---
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/collection/utils/IndSqlUtils.java |   50 ++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/collection/utils/IndSqlUtils.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/collection/utils/IndSqlUtils.java
index 54c1faa..4636904 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/collection/utils/IndSqlUtils.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/collection/utils/IndSqlUtils.java
@@ -4,12 +4,14 @@
 import com.iailab.module.data.common.enums.IndStatFuncEnum;
 import com.iailab.module.data.common.enums.IndTimeLimitEnum;
 import com.iailab.module.data.ind.data.dto.IndDataSetDTO;
+import com.iailab.module.data.ind.data.service.IndDataSetFieldService;
 import com.iailab.module.data.ind.data.service.IndDataSetService;
 import com.iailab.module.data.ind.item.service.IndItemAtomService;
 import com.iailab.module.data.ind.item.service.IndItemDerService;
 import com.iailab.module.data.ind.item.vo.IndItemAtomVO;
 import com.iailab.module.data.ind.item.vo.IndItemDerVO;
 import com.iailab.module.data.ind.value.dto.QuerySourceValueDTO;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -31,6 +33,9 @@
     private IndDataSetService indDataSetService;
 
     @Autowired
+    private IndDataSetFieldService indDataSetFieldService;
+
+    @Autowired
     private IndItemDerService indItemDerService;
 
     public final static String PATTERN_YEAR = "yyyy";
@@ -45,13 +50,14 @@
         if (indItem == null) {
             return result;
         }
+        result.setIndItemAtom(indItem);
         IndDataSetDTO dataSet = indDataSetService.getDet(indItem.getDataSet());
         if (dataSet == null) {
             return result;
         }
         result.setDataSource(indItem.getDataSource());
         StringBuilder selectSql = new StringBuilder();
-        IndStatFuncEnum statFun = IndStatFuncEnum.getEumByCode(indItem.getStatFunc());
+        IndStatFuncEnum statFun = IndStatFuncEnum.getEumByCode(StringUtils.isBlank(indItem.getStatFunc())? IndStatFuncEnum.DEFAULT.getCode() : indItem.getStatFunc());
         switch (statFun) {
             case AVG:
                 selectSql.append("AVG(");
@@ -68,7 +74,7 @@
             case SUM:
                 selectSql.append("SUM(");
                 break;
-            default:
+            case DEFAULT:
                 selectSql.append("(");
                 break;
         }
@@ -89,12 +95,20 @@
 
         // 拼接SELECT
         StringBuilder selectSql = new StringBuilder();
-        selectSql.append(indItem.getDimension());
-        selectSql.append(", ");
-        selectSql.append(result.getSelectSql());
-        selectSql.append(", ");
-        selectSql.append(indItem.getTimeLabel());
-        selectSql.append(" data_time");
+        if (StringUtils.isNotBlank(indItem.getDimension()) && StringUtils.isNotBlank(result.getIndItemAtom().getStatFunc())){
+            selectSql.append(indItem.getDimension());
+            selectSql.append(", ");
+            selectSql.append(result.getSelectSql());
+        }
+        else if(StringUtils.isBlank(indItem.getDimension()) && StringUtils.isNotBlank(result.getIndItemAtom().getStatFunc())){
+            selectSql.append(result.getSelectSql());
+        }
+        else{
+            selectSql.append(result.getSelectSql());
+            selectSql.append(", ");
+            selectSql.append(indItem.getTimeLabel());
+            selectSql.append(" data_time");
+        }
         result.setSelectSql(selectSql.toString());
 
         // 拼接WHERE
@@ -146,7 +160,9 @@
                 whereSql.append(indItem.getTimeLabel());
                 whereSql.append(" <= '");
                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
-                whereSql.append("' AND >= '");
+                whereSql.append("' AND '");
+                whereSql.append(indItem.getTimeLabel());
+                whereSql.append("' >= '");
                 calendar.add(Calendar.MONTH, -12);
                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
                 whereSql.append("'");
@@ -174,9 +190,12 @@
                 whereSql.append("'");
                 break;
             case CUSTOM:
+                whereSql.append(indItem.getTimeLabel());
                 whereSql.append(" <= '");
                 whereSql.append(DateUtils.format(indItem.getTimeEnd(), PATTERN_MON));
-                whereSql.append("' AND >= '");
+                whereSql.append("' AND ");
+                whereSql.append(indItem.getTimeLabel());
+                whereSql.append(" >= '");
                 whereSql.append(DateUtils.format(indItem.getTimeStart(), PATTERN_MON));
                 whereSql.append("'");
                 break;
@@ -187,14 +206,17 @@
 
         // 拼接GROUP
         StringBuilder groupSql = new StringBuilder();
-        groupSql.append(" GROUP BY ");
-        groupSql.append(indItem.getDimension());
+        if (StringUtils.isNotBlank(indItem.getDimension())){
+            groupSql.append(indItem.getDimension());
+        }
         result.setGroupSql(groupSql.toString());
 
         // 拼接ORDER
         StringBuilder orderBySql = new StringBuilder();
-        orderBySql.append(indItem.getTimeLabel());
-        result.setOrderBySql(orderBySql.toString());
+        if (StringUtils.isNotBlank(indItem.getDimension()) && indItem.getDimension().equals(indItem.getTimeLabel())){
+            orderBySql.append(indItem.getTimeLabel());
+            result.setOrderBySql(orderBySql.toString());
+        }
         return result;
     }
 

--
Gitblit v1.9.3