From dfac5c686a3e076034abd46349d60aeca0457f4a Mon Sep 17 00:00:00 2001
From: dengzedong <dengzedong@email>
Date: 星期二, 01 四月 2025 14:44:40 +0800
Subject: [PATCH] 派生指标 月周期开始日

---
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/collection/utils/IndSqlUtils.java |  118 ++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 92 insertions(+), 26 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 592ed51..8de5cd4 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
@@ -16,6 +16,9 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.text.SimpleDateFormat;
+import java.time.YearMonth;
+import java.time.format.DateTimeFormatter;
 import java.util.Calendar;
 import java.util.Date;
 
@@ -60,7 +63,7 @@
         }
         result.setDataSource(indItem.getDataSource());
         StringBuilder selectSql = new StringBuilder();
-        IndStatFuncEnum statFun = IndStatFuncEnum.getEumByCode(StringUtils.isBlank(indItem.getStatFunc())? IndStatFuncEnum.DEFAULT.getCode() : indItem.getStatFunc());
+        IndStatFuncEnum statFun = IndStatFuncEnum.getEumByCode(StringUtils.isBlank(indItem.getStatFunc()) ? IndStatFuncEnum.DEFAULT.getCode() : indItem.getStatFunc());
         switch (statFun) {
             case AVG:
                 selectSql.append("AVG(");
@@ -100,15 +103,13 @@
 
         // 拼接SELECT
         StringBuilder selectSql = new StringBuilder();
-        if (StringUtils.isNotBlank(indItem.getDimension()) && StringUtils.isNotBlank(result.getIndItemAtom().getStatFunc())){
+        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())){
+        } else if (StringUtils.isBlank(indItem.getDimension()) && StringUtils.isNotBlank(result.getIndItemAtom().getStatFunc())) {
             selectSql.append(result.getSelectSql());
-        }
-        else{
+        } else {
             selectSql.append(result.getSelectSql());
             selectSql.append(", ");
             selectSql.append(indItem.getTimeLabel());
@@ -141,7 +142,9 @@
                 whereSql.append(indItem.getTimeLabel());
                 whereSql.append(" <= '");
                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_DATE));
-                whereSql.append("' AND >= '");
+                whereSql.append("' AND ");
+                whereSql.append(indItem.getTimeLabel());
+                whereSql.append(" >= '");
                 calendar.add(Calendar.DAY_OF_YEAR, -7);
                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_DATE));
                 whereSql.append("'");
@@ -150,7 +153,9 @@
                 whereSql.append(indItem.getTimeLabel());
                 whereSql.append(" <= '");
                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_DATE));
-                whereSql.append("' AND >= '");
+                whereSql.append("' AND ");
+                whereSql.append(indItem.getTimeLabel());
+                whereSql.append(" >= '");
                 calendar.add(Calendar.DAY_OF_YEAR, -30);
                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_DATE));
                 whereSql.append("'");
@@ -158,25 +163,39 @@
             case THIS_MONTH:
                 whereSql.append(indItem.getTimeLabel());
                 whereSql.append("='");
-                whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
+                whereSql.append(getYearMonth(calendar,indItem.getMonthStart()));
+                whereSql.append("'");
+                break;
+            case LAST_MONTH:
+                whereSql.append(indItem.getTimeLabel());
+                whereSql.append("' < '");
+                whereSql.append(getYearMonth(calendar,indItem.getMonthStart()));
+                whereSql.append("' AND ");
+                whereSql.append(indItem.getTimeLabel());
+                whereSql.append(" >= '");
+                calendar.add(Calendar.MONTH, -1);
+                whereSql.append(getYearMonth(calendar,indItem.getMonthStart()));
                 whereSql.append("'");
                 break;
             case LAST_MONTH_12:
                 whereSql.append(indItem.getTimeLabel());
                 whereSql.append(" <= '");
-                whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
-                whereSql.append("' AND '");
+                whereSql.append(getYearMonth(calendar,indItem.getMonthStart()));
+                whereSql.append("' AND ");
                 whereSql.append(indItem.getTimeLabel());
                 whereSql.append("' >= '");
                 calendar.add(Calendar.MONTH, -12);
-                whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
+                whereSql.append(getYearMonth(calendar,indItem.getMonthStart()));
                 whereSql.append("'");
                 break;
             case THIS_YEAR_MONTH:
-                calendar.set(Calendar.MONTH, 11);
+                whereSql.append(indItem.getTimeLabel());
                 whereSql.append(" <= '");
+                calendar.set(Calendar.MONTH, 11);
                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
-                whereSql.append("' AND >= '");
+                whereSql.append("' AND ");
+                whereSql.append(indItem.getTimeLabel());
+                whereSql.append(" >= '");
                 calendar.set(Calendar.MONTH, 0);
                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
                 whereSql.append("'");
@@ -194,6 +213,17 @@
                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_YEAR));
                 whereSql.append("'");
                 break;
+            case LAST_HOUR_24:
+                whereSql.append(indItem.getTimeLabel());
+                whereSql.append(" <= '");
+                whereSql.append(DateUtils.format(calendar.getTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
+                whereSql.append("' AND ");
+                whereSql.append(indItem.getTimeLabel());
+                whereSql.append(" >= '");
+                calendar.add(Calendar.HOUR_OF_DAY, -24);
+                whereSql.append(DateUtils.format(calendar.getTime(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
+                whereSql.append("'");
+                break;
             case CUSTOM:
                 whereSql.append(indItem.getTimeLabel());
                 whereSql.append(" <= '");
@@ -204,6 +234,9 @@
                 whereSql.append(DateUtils.format(indItem.getTimeStart(), PATTERN_MON));
                 whereSql.append("'");
                 break;
+            case ALL:
+                whereSql.append("1 = 1");
+                break;
             default:
                 break;
         }
@@ -211,18 +244,39 @@
 
         // 拼接GROUP
         StringBuilder groupSql = new StringBuilder();
-        if (StringUtils.isNotBlank(indItem.getDimension())){
+        if (StringUtils.isNotBlank(indItem.getDimension())) {
             groupSql.append(indItem.getDimension());
         }
         result.setGroupSql(groupSql.toString());
 
         // 拼接ORDER
         StringBuilder orderBySql = new StringBuilder();
-        if (StringUtils.isNotBlank(indItem.getDimension()) && indItem.getDimension().equals(indItem.getTimeLabel())){
+        if (StringUtils.isNotBlank(indItem.getDimension()) && indItem.getDimension().equals(indItem.getTimeLabel())) {
             orderBySql.append(indItem.getTimeLabel());
             result.setOrderBySql(orderBySql.toString());
         }
         return result;
+    }
+
+    public static String getYearMonth(Calendar now,Integer monthStart) {
+        int day = now.get(Calendar.DAY_OF_MONTH);
+
+        // 当前月
+        if (monthStart > 0) {
+            if (day < monthStart) {
+                now.add(Calendar.MONTH, -1);
+            }
+        }
+
+        if (monthStart < 0) {
+            if (day >= Math.abs(monthStart)) {
+                now.add(Calendar.MONTH, 1);
+            }
+        }
+        // 定义日期格式化对象,指定格式为 yyyy-MM
+        SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_MON);
+        // 格式化日期并返回结果
+        return sdf.format(now.getTime());
     }
 
     public QuerySourceValueDTO getDerSql(String itemId, Date startTime, Date endTime) {
@@ -238,9 +292,15 @@
 
         // 拼接SELECT
         StringBuilder selectSql = new StringBuilder();
-        selectSql.append(indItem.getDimension());
-        selectSql.append(", ");
-        selectSql.append(result.getSelectSql());
+        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");
@@ -251,22 +311,28 @@
         whereSql.append(" ");
         whereSql.append(indItem.getTimeLabel());
         whereSql.append(" <= '");
-        whereSql.append(DateUtils.format(endTime, PATTERN_MON));
-        whereSql.append("' AND >= '");
-        whereSql.append(DateUtils.format(startTime, PATTERN_MON));
+        whereSql.append(DateUtils.format(endTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
+        whereSql.append("' AND ");
+        whereSql.append(indItem.getTimeLabel());
+        whereSql.append(" >= '");
+        whereSql.append(DateUtils.format(startTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
         whereSql.append("'");
         result.setWhereSql(whereSql.toString());
 
         // 拼接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;
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3