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/db/mysql/tenant.sql                                                        |    2 ++
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/collection/utils/IndSqlUtils.java |   34 +++++++++++++++++++++++++++++-----
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/item/entity/IndItemDerEntity.java |    5 +++++
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/item/vo/IndItemDerVO.java         |    5 +++++
 4 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/iailab-module-data/iailab-module-data-biz/db/mysql/tenant.sql b/iailab-module-data/iailab-module-data-biz/db/mysql/tenant.sql
index 9f63ba2..31bcd5a 100644
--- a/iailab-module-data/iailab-module-data-biz/db/mysql/tenant.sql
+++ b/iailab-module-data/iailab-module-data-biz/db/mysql/tenant.sql
@@ -324,6 +324,8 @@
                                 UNIQUE KEY `uk_item_id` (`item_id`) USING BTREE
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT = '派生指标';
 
+ALTER TABLE t_ind_item_der ADD COLUMN month_start int COMMENT '月周期开始日';
+
 CREATE TABLE t_ind_item_cal(
                              `id` VARCHAR(36) NOT NULL  COMMENT 'ID' ,
                              `item_id` VARCHAR(36)   COMMENT '指标ID' ,
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 96a9661..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:
@@ -255,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);
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/item/entity/IndItemDerEntity.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/item/entity/IndItemDerEntity.java
index 1c1b12e..0e2591f 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/item/entity/IndItemDerEntity.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/item/entity/IndItemDerEntity.java
@@ -58,4 +58,9 @@
      * 分析维度
      */
     private String dimension;
+
+    /**
+     * 月周期开始日
+     */
+    private Integer monthStart;
 }
\ No newline at end of file
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/item/vo/IndItemDerVO.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/item/vo/IndItemDerVO.java
index af9424d..ade22f8 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/item/vo/IndItemDerVO.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/item/vo/IndItemDerVO.java
@@ -53,4 +53,9 @@
      * 分析维度
      */
     private String dimension;
+
+    /**
+     * 月周期开始日
+     */
+    private Integer monthStart;
 }
\ No newline at end of file

--
Gitblit v1.9.3