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 |   37 ++++++++++++++++++++++++++++++++-----
 1 files changed, 32 insertions(+), 5 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 127c42e..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;
 
@@ -160,29 +163,29 @@
             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(DateUtils.format(calendar.getTime(), PATTERN_MON));
+                whereSql.append(getYearMonth(calendar,indItem.getMonthStart()));
                 whereSql.append("' AND ");
                 whereSql.append(indItem.getTimeLabel());
                 whereSql.append(" >= '");
                 calendar.add(Calendar.MONTH, -1);
-                whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
+                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(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:
@@ -231,6 +234,9 @@
                 whereSql.append(DateUtils.format(indItem.getTimeStart(), PATTERN_MON));
                 whereSql.append("'");
                 break;
+            case ALL:
+                whereSql.append("1 = 1");
+                break;
             default:
                 break;
         }
@@ -252,6 +258,27 @@
         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) {
         QuerySourceValueDTO result = getAtomSql(itemId);
         IndItemDerVO indItem = indItemDerService.getByItemId(itemId);

--
Gitblit v1.9.3