提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.ind.collection.utils; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.util.date.DateUtils; |
|
4 |
import com.iailab.module.data.ind.common.IndTimeRangeConst; |
|
5 |
|
|
6 |
import java.util.Calendar; |
|
7 |
|
|
8 |
/** |
|
9 |
* @author PanZhibao |
|
10 |
* @Description |
|
11 |
* @createTime 2024年05月25日 |
|
12 |
*/ |
|
13 |
public class IndSqlUtils { |
|
14 |
|
|
15 |
public final static String PATTERN_YEAR = "yyyy"; |
|
16 |
|
|
17 |
public final static String PATTERN_MON = "yyyy-MM"; |
|
18 |
|
|
19 |
public final static String PATTERN_DATE = "yyyy-MM-dd"; |
|
20 |
|
|
21 |
|
|
22 |
public static String getSqlWhere(String timeRange, String timeGranularity) { |
|
23 |
StringBuilder sqlWhere = new StringBuilder(); |
|
24 |
Calendar calendar = Calendar.getInstance(); |
|
25 |
calendar.set(Calendar.MILLISECOND, 0); |
|
26 |
switch (timeRange) { |
|
27 |
case IndTimeRangeConst.LAST_YEAR: |
|
28 |
calendar.add(Calendar.YEAR, -1); |
|
29 |
sqlWhere.append("ind_t.data_time = '"); |
|
30 |
sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_YEAR)); |
|
31 |
sqlWhere.append("'"); |
|
32 |
break; |
|
33 |
case IndTimeRangeConst.THIS_YEAR: |
|
34 |
sqlWhere.append("ind_t.data_time = '"); |
|
35 |
sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_YEAR)); |
|
36 |
sqlWhere.append("'"); |
|
37 |
break; |
|
38 |
case IndTimeRangeConst.THIS_YEAR_MONTH: |
|
39 |
sqlWhere.append("ind_t.data_time >= '"); |
|
40 |
sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_YEAR) + "-01' "); |
|
41 |
sqlWhere.append("and ind_t.data_time <= '"); |
|
42 |
sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_YEAR) + "-12' "); |
|
43 |
break; |
|
44 |
case IndTimeRangeConst.THIS_MONTH: |
|
45 |
sqlWhere.append("ind_t.data_time = '"); |
|
46 |
sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_MON)); |
|
47 |
sqlWhere.append("'"); |
|
48 |
break; |
|
49 |
case IndTimeRangeConst.YESTERDAY: |
|
50 |
calendar.add(Calendar.DAY_OF_YEAR, -1); |
|
51 |
sqlWhere.append("ind_t.data_time = '"); |
|
52 |
sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_DATE)); |
|
53 |
sqlWhere.append("'"); |
|
54 |
break; |
|
55 |
case IndTimeRangeConst.TODAY: |
|
56 |
sqlWhere.append("ind_t.data_time = '"); |
|
57 |
sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_DATE)); |
|
58 |
sqlWhere.append("'"); |
|
59 |
break; |
|
60 |
case IndTimeRangeConst.LAST_DAY_7: |
|
61 |
calendar.add(Calendar.DAY_OF_YEAR, -7); |
|
62 |
sqlWhere.append("ind_t.data_time >= '"); |
|
63 |
sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_DATE)); |
|
64 |
sqlWhere.append("'"); |
|
65 |
break; |
|
66 |
case IndTimeRangeConst.LAST_DAY_30: |
|
67 |
calendar.add(Calendar.DAY_OF_YEAR, -30); |
|
68 |
sqlWhere.append("ind_t.data_time >= '"); |
|
69 |
sqlWhere.append(DateUtils.format(calendar.getTime(), PATTERN_DATE)); |
|
70 |
sqlWhere.append("'"); |
|
71 |
break; |
|
72 |
default: |
|
73 |
break; |
|
74 |
|
|
75 |
} |
|
76 |
return sqlWhere.toString(); |
|
77 |
} |
|
78 |
|
|
79 |
public static String getSqlWhereByRange(String start, String end) { |
|
80 |
StringBuilder sqlWhere = new StringBuilder(); |
|
81 |
sqlWhere.append("ind_t.data_time >= '"); |
|
82 |
sqlWhere.append(start); |
|
83 |
sqlWhere.append("' "); |
|
84 |
sqlWhere.append("and ind_t.data_time <= '"); |
|
85 |
sqlWhere.append(end); |
|
86 |
sqlWhere.append("' "); |
|
87 |
return sqlWhere.toString(); |
|
88 |
} |
|
89 |
} |