dengzedong
2024-10-14 3e18d4bfbf2c657b08b21512c2d884cc9d59df7b
提交 | 用户 | 时间
cf757d 1 package com.iailab.module.data.ind.collection.utils;
2
3 import com.iailab.framework.common.util.date.DateUtils;
4 import com.iailab.module.data.common.enums.IndStatFuncEnum;
5 import com.iailab.module.data.common.enums.IndTimeLimitEnum;
6 import com.iailab.module.data.ind.data.dto.IndDataSetDTO;
cfa57a 7 import com.iailab.module.data.ind.data.service.IndDataSetFieldService;
cf757d 8 import com.iailab.module.data.ind.data.service.IndDataSetService;
9 import com.iailab.module.data.ind.item.service.IndItemAtomService;
10 import com.iailab.module.data.ind.item.service.IndItemDerService;
11 import com.iailab.module.data.ind.item.vo.IndItemAtomVO;
12 import com.iailab.module.data.ind.item.vo.IndItemDerVO;
13 import com.iailab.module.data.ind.value.dto.QuerySourceValueDTO;
cfa57a 14 import org.apache.commons.lang3.StringUtils;
cf757d 15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.stereotype.Component;
17
18 import java.util.Calendar;
19 import java.util.Date;
20
21 /**
22  * @author PanZhibao
23  * @Description
24  * @createTime 2024年10月04日
25  */
26 @Component
27 public class IndSqlUtils {
28
29     @Autowired
30     private IndItemAtomService indItemAtomService;
31
32     @Autowired
33     private IndDataSetService indDataSetService;
cfa57a 34
J 35     @Autowired
36     private IndDataSetFieldService indDataSetFieldService;
cf757d 37
38     @Autowired
39     private IndItemDerService indItemDerService;
40
41     public final static String PATTERN_YEAR = "yyyy";
42
43     public final static String PATTERN_MON = "yyyy-MM";
44
45     public final static String PATTERN_DATE = "yyyy-MM-dd";
46
47     public QuerySourceValueDTO getAtomSql(String itemId) {
48         QuerySourceValueDTO result = new QuerySourceValueDTO();
49         IndItemAtomVO indItem = indItemAtomService.getByItemId(itemId);
50         if (indItem == null) {
51             return result;
52         }
53         IndDataSetDTO dataSet = indDataSetService.getDet(indItem.getDataSet());
54         if (dataSet == null) {
55             return result;
56         }
57         result.setDataSource(indItem.getDataSource());
58         StringBuilder selectSql = new StringBuilder();
cfa57a 59         IndStatFuncEnum statFun = IndStatFuncEnum.getEumByCode(StringUtils.isBlank(indItem.getStatFunc())? IndStatFuncEnum.DEFAULT.getCode() : indItem.getStatFunc());
cf757d 60         switch (statFun) {
61             case AVG:
62                 selectSql.append("AVG(");
63                 break;
64             case COUNT:
65                 selectSql.append("COUNT(");
66                 break;
67             case MAX:
68                 selectSql.append("MAX(");
69                 break;
70             case MIN:
71                 selectSql.append("MIN(");
72                 break;
73             case SUM:
74                 selectSql.append("SUM(");
75                 break;
cfa57a 76             case DEFAULT:
cf757d 77                 selectSql.append("(");
78                 break;
79         }
80         selectSql.append(indItem.getUsingField());
81         selectSql.append(") data_value");
82         result.setSelectSql(selectSql.toString());
83         result.setViewSql(dataSet.getQuerySql());
84         return result;
85     }
86
87     public QuerySourceValueDTO getDerSql(String itemId) {
88         QuerySourceValueDTO result = getAtomSql(itemId);
89         IndItemDerVO indItem = indItemDerService.getByItemId(itemId);
90         if (indItem == null) {
91             return result;
92         }
93         result = getAtomSql(indItem.getAtomItemId());
94
95         // 拼接SELECT
96         StringBuilder selectSql = new StringBuilder();
cfa57a 97         if (StringUtils.isNotBlank(indItem.getDimension())){
J 98             selectSql.append(indItem.getDimension());
99             selectSql.append(", ");
100         }
cf757d 101         selectSql.append(result.getSelectSql());
102         selectSql.append(", ");
103         selectSql.append(indItem.getTimeLabel());
104         selectSql.append(" data_time");
105         result.setSelectSql(selectSql.toString());
106
107         // 拼接WHERE
108         StringBuilder whereSql = new StringBuilder();
109         whereSql.append(" ");
110         Calendar calendar = Calendar.getInstance();
111         calendar.set(Calendar.MILLISECOND, 0);
112         calendar.set(Calendar.SECOND, 0);
113         IndTimeLimitEnum timeLimit = IndTimeLimitEnum.getEumByCode(indItem.getTimeLimit());
114         switch (timeLimit) {
115             case TODAY:
116                 whereSql.append(indItem.getTimeLabel());
117                 whereSql.append("='");
118                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_DATE));
119                 whereSql.append("'");
120                 break;
121             case YESTERDAY:
122                 calendar.add(Calendar.DAY_OF_YEAR, -1);
123                 whereSql.append(indItem.getTimeLabel());
124                 whereSql.append("='");
125                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_DATE));
126                 whereSql.append("'");
127                 break;
128             case LAST_DAY_7:
129                 whereSql.append(indItem.getTimeLabel());
130                 whereSql.append(" <= '");
131                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_DATE));
132                 whereSql.append("' AND >= '");
133                 calendar.add(Calendar.DAY_OF_YEAR, -7);
134                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_DATE));
135                 whereSql.append("'");
136                 break;
137             case LAST_DAY_30:
138                 whereSql.append(indItem.getTimeLabel());
139                 whereSql.append(" <= '");
140                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_DATE));
141                 whereSql.append("' AND >= '");
142                 calendar.add(Calendar.DAY_OF_YEAR, -30);
143                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_DATE));
144                 whereSql.append("'");
145                 break;
146             case THIS_MONTH:
147                 whereSql.append(indItem.getTimeLabel());
148                 whereSql.append("='");
149                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
150                 whereSql.append("'");
151                 break;
152             case LAST_MONTH_12:
153                 whereSql.append(indItem.getTimeLabel());
154                 whereSql.append(" <= '");
155                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
156                 whereSql.append("' AND >= '");
157                 calendar.add(Calendar.MONTH, -12);
158                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
159                 whereSql.append("'");
160                 break;
161             case THIS_YEAR_MONTH:
162                 calendar.set(Calendar.MONTH, 11);
163                 whereSql.append(" <= '");
164                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
165                 whereSql.append("' AND >= '");
166                 calendar.set(Calendar.MONTH, 0);
167                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_MON));
168                 whereSql.append("'");
169                 break;
170             case LAST_YEAR:
171                 calendar.add(Calendar.YEAR, -1);
172                 whereSql.append(indItem.getTimeLabel());
173                 whereSql.append("='");
174                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_YEAR));
175                 whereSql.append("'");
176                 break;
177             case THIS_YEAR:
178                 whereSql.append(indItem.getTimeLabel());
179                 whereSql.append("='");
180                 whereSql.append(DateUtils.format(calendar.getTime(), PATTERN_YEAR));
181                 whereSql.append("'");
182                 break;
183             case CUSTOM:
cfa57a 184                 whereSql.append(indItem.getTimeLabel());
cf757d 185                 whereSql.append(" <= '");
186                 whereSql.append(DateUtils.format(indItem.getTimeEnd(), PATTERN_MON));
cfa57a 187                 whereSql.append("' AND '");
J 188                 whereSql.append(indItem.getTimeLabel());
189                 whereSql.append("' >= '");
cf757d 190                 whereSql.append(DateUtils.format(indItem.getTimeStart(), PATTERN_MON));
191                 whereSql.append("'");
192                 break;
193             default:
194                 break;
195         }
196         result.setWhereSql(whereSql.toString());
197
198         // 拼接GROUP
199         StringBuilder groupSql = new StringBuilder();
200         groupSql.append(" GROUP BY ");
201         groupSql.append(indItem.getDimension());
202         result.setGroupSql(groupSql.toString());
203
204         // 拼接ORDER
205         StringBuilder orderBySql = new StringBuilder();
206         orderBySql.append(indItem.getTimeLabel());
207         result.setOrderBySql(orderBySql.toString());
208         return result;
209     }
210
211     public QuerySourceValueDTO getDerSql(String itemId, Date startTime, Date endTime) {
212         QuerySourceValueDTO result = getAtomSql(itemId);
213         IndItemDerVO indItem = indItemDerService.getByItemId(itemId);
214         if (indItem == null) {
215             return result;
216         }
217         result = getAtomSql(indItem.getAtomItemId());
218
219         // 拼接SELECT
220         StringBuilder selectSql = new StringBuilder();
221         selectSql.append(indItem.getDimension());
222         selectSql.append(", ");
223         selectSql.append(result.getSelectSql());
224         selectSql.append(", ");
225         selectSql.append(indItem.getTimeLabel());
226         selectSql.append(" data_time");
227         result.setSelectSql(selectSql.toString());
228
229         // 拼接WHERE
230         StringBuilder whereSql = new StringBuilder();
231         whereSql.append(" ");
232         whereSql.append(indItem.getTimeLabel());
233         whereSql.append(" <= '");
234         whereSql.append(DateUtils.format(endTime, PATTERN_MON));
235         whereSql.append("' AND >= '");
236         whereSql.append(DateUtils.format(startTime, PATTERN_MON));
237         whereSql.append("'");
238         result.setWhereSql(whereSql.toString());
239
240         // 拼接GROUP
241         StringBuilder groupSql = new StringBuilder();
242         groupSql.append(" GROUP BY ");
243         groupSql.append(indItem.getDimension());
244         result.setGroupSql(groupSql.toString());
245
246         // 拼接ORDER
247         StringBuilder orderBySql = new StringBuilder();
248         orderBySql.append(indItem.getTimeLabel());
249         result.setOrderBySql(orderBySql.toString());
250         return result;
251     }
252 }