package com.iailab.module.data.ind.collection.utils; import com.iailab.framework.common.util.date.DateUtils; import com.iailab.module.data.ind.common.IndTimeRangeConst; import java.util.Calendar; /** * @author PanZhibao * @Description * @createTime 2024年05月25日 */ public class IndSqlUtils { public final static String PATTERN_YEAR = "yyyy"; public final static String PATTERN_MON = "yyyy-MM"; public final static String PATTERN_DATE = "yyyy-MM-dd"; public static String getSqlWhere(String timeRange, String timeGranularity) { StringBuilder sqlWhere = new StringBuilder(); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MILLISECOND, 0); switch (timeRange) { case IndTimeRangeConst.LAST_YEAR: calendar.add(Calendar.YEAR, -1); sqlWhere.append("ind_t.data_time = '"); sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_YEAR)); sqlWhere.append("'"); break; case IndTimeRangeConst.THIS_YEAR: sqlWhere.append("ind_t.data_time = '"); sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_YEAR)); sqlWhere.append("'"); break; case IndTimeRangeConst.THIS_YEAR_MONTH: sqlWhere.append("ind_t.data_time >= '"); sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_YEAR) + "-01' "); sqlWhere.append("and ind_t.data_time <= '"); sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_YEAR) + "-12' "); break; case IndTimeRangeConst.THIS_MONTH: sqlWhere.append("ind_t.data_time = '"); sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_MON)); sqlWhere.append("'"); break; case IndTimeRangeConst.YESTERDAY: calendar.add(Calendar.DAY_OF_YEAR, -1); sqlWhere.append("ind_t.data_time = '"); sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_DATE)); sqlWhere.append("'"); break; case IndTimeRangeConst.TODAY: sqlWhere.append("ind_t.data_time = '"); sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_DATE)); sqlWhere.append("'"); break; case IndTimeRangeConst.LAST_DAY_7: calendar.add(Calendar.DAY_OF_YEAR, -7); sqlWhere.append("ind_t.data_time >= '"); sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_DATE)); sqlWhere.append("'"); break; case IndTimeRangeConst.LAST_DAY_30: calendar.add(Calendar.DAY_OF_YEAR, -30); sqlWhere.append("ind_t.data_time >= '"); sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_DATE)); sqlWhere.append("'"); break; default: break; } return sqlWhere.toString(); } public static String getSqlWhereByRange(String start, String end) { StringBuilder sqlWhere = new StringBuilder(); sqlWhere.append("ind_t.data_time >= '"); sqlWhere.append(start); sqlWhere.append("' "); sqlWhere.append("and ind_t.data_time <= '"); sqlWhere.append(end); sqlWhere.append("' "); return sqlWhere.toString(); } }