dongyukun
4 天以前 4624b6fd58f262ab95f2d60472560b9fc162d677
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("' AND '");
                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("' 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:
@@ -214,7 +217,7 @@
                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("' AND ");
                whereSql.append(indItem.getTimeLabel());
                whereSql.append(" >= '");
                calendar.add(Calendar.HOUR_OF_DAY, -24);
@@ -230,6 +233,9 @@
                whereSql.append(" >= '");
                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);