new-bi 多个视图处理

2022-07-09 17:15:41

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package com.amoros.newbi.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.thread.AsyncUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.SerializeUtil;
import com.amoros.newbi.service.context.TenantIdHolder;
import com.amoros.newbi.service.dataobject.dto.DateTimeBo;
import com.amoros.newbi.service.dataobject.dto.QueryResultWrapper;
import com.amoros.newbi.service.dataobject.dto.QueryResultWrappers;
import com.amoros.newbi.service.dataobject.properties.BiConfig;
import com.amoros.newbi.service.dataobject.properties.BiReportProperties;
import com.amoros.newbi.service.dataobject.properties.ViewConfig;
import com.amoros.newbi.service.dataobject.qry.BiQueryParams;
import com.amoros.newbi.service.dataobject.qry.BiReportQry;
import com.amoros.newbi.service.processor.ProcessorManager;
import com.amoros.newbi.service.processor.agg.AggProcessor;
import com.amoros.newbi.service.processor.post.PostProcessor;
import com.amoros.newbi.service.processor.pre.PreProcessor;
import com.amoros.newbi.service.service.BiService;
import com.amoros.newbi.service.util.MDCUtil;
import datart.api.service.DataProviderService;
import datart.api.service.custom.qry.TableVariable;
import datart.api.service.custom.qry.ViewQry;
import datart.api.service.custom.result.QueryResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service
public class BiServiceImpl implements BiService {

@Autowired
private BiReportProperties biReportProperties;

@Autowired
private DataProviderService dataProviderService;

@Override
public QueryResultWrappers biReport(BiReportQry biReportQry) {
// [0] 初始化参数
String reportCode = biReportQry.getReportCode();
BiQueryParams queryParams = biReportQry.getBiQueryParams();

// [1] 正常时间 => 财年时间
updateFiscalTime(queryParams);

// [2] 获取配置 reportCode => bi.yml 配置
BiConfig biConfig = biReportProperties.getConfig().get(reportCode);
Assert.notNull(biConfig, "获取不到 bi 相关配置,reportCode=[{}]", reportCode);

// [3] 依次处理所有视图
QueryResultWrappers wrappers = handleMultiView(biConfig.getViewConfigList(), queryParams);

// [4] 所有视图处理完毕 => 聚合处理器
wrappers = aggProcess(wrappers, biConfig, queryParams);

// [5] 返回结果
return wrappers;
}

private QueryResultWrappers handleMultiView2(List<ViewConfig> viewConfigList, BiQueryParams queryParams) {
// 查询 => List

List<ViewQry> viewQryList = viewConfigList.stream()
.flatMap(viewConfig -> {
BiQueryParams _queryParams = SerializeUtil.clone(queryParams);
preProcess(viewConfig, _queryParams);
ViewQry viewQry = getViewQry(viewConfig, _queryParams);

if (!BooleanUtil.isTrue(viewConfig.getCompareData())) {
return Stream.of(viewQry);
}

_queryParams.setStartDate(_queryParams.getDateTimeBo().getComparedStartDate());
_queryParams.setEndDate(_queryParams.getDateTimeBo().getComparedEndDate());
ViewQry viewQry_yoy = getViewQry(viewConfig, _queryParams);

_queryParams.setStartDate(_queryParams.getDateTimeBo().getChainStartDate());
_queryParams.setEndDate(_queryParams.getDateTimeBo().getChainEndDate());
ViewQry viewQry_mom = getViewQry(viewConfig, _queryParams);
return Stream.of(viewQry, viewQry_yoy, viewQry_mom);
}).collect(Collectors.toList());


// yoy
// queryParams.setStartDate(queryParams.getDateTimeBo().getComparedStartDate());
// queryParams.setEndDate(queryParams.getDateTimeBo().getComparedEndDate());
// mom
// queryParams.setStartDate(queryParams.getDateTimeBo().getChainStartDate());
// queryParams.setEndDate(queryParams.getDateTimeBo().getChainEndDate());

long start = System.currentTimeMillis();
List<QueryResult> queryResultList = dataProviderService.selectListView(viewQryList);
Console.error(System.currentTimeMillis() - start);

// 组装
QueryResultWrappers wrappers = new QueryResultWrappers();
int k = 0;
for (int i = 0; i < queryResultList.size(); i++) {
ViewConfig viewConfig = viewConfigList.get(i - 2 * k);
QueryResultWrapper wrapper = new QueryResultWrapper();

BiQueryParams _queryParams = SerializeUtil.clone(queryParams);
preProcess(viewConfig, _queryParams);
QueryResult source = queryResultList.get(i);
source = postProcess(source, viewConfig, _queryParams);
wrapper.setSource(source);

// 处理比较数据(同比、环比)
if (BooleanUtil.isTrue(viewConfig.getCompareData())) {
k++;
QueryResult yoy = queryResultList.get(++i);
yoy = postProcess(yoy, viewConfig, _queryParams);
wrapper.setYoy(yoy);

QueryResult mom = queryResultList.get(++i);
mom = postProcess(mom, viewConfig, _queryParams);
wrapper.setMom(mom);
}
wrappers.add(wrapper);
}

return wrappers;
}

private ViewQry getViewQry(ViewConfig viewConfig, BiQueryParams _queryParams) {
// [1] 查询参数 => SQL 查询变量
String viewPath = viewConfig.getViewPath();
ViewQry viewQry = ViewQry.of(viewPath).put(_queryParams);

// [2] 表名变量(yml取变量值) => 表名变量放入时,不加单引号
if (CollectionUtil.isNotEmpty(viewConfig.getTableVariable())) {
TableVariable tableVariable = viewConfig.getTableVariable().get(_queryParams.getDateType());
tableVariable.forEach(viewQry::put);
}

// [3] 列名变量(查询参数中取变量值) => 列名变量放入时,不加单引号
_queryParams.getColumnVariable().forEach(viewQry::put);

// [4] 租户变量自动填充
viewQry.putSingleQuote("tenant_id", TenantIdHolder.get());
return viewQry;
}

/**
* 处理多个视图
*
* @param viewConfigList 多个视图配置
* @param queryParams 查询参数
* @return 视图查询结果封装集
*/
private QueryResultWrappers handleMultiView(List<ViewConfig> viewConfigList, BiQueryParams queryParams) {
// 1、遍历 视图配置列表
// 2、异步处理 视图配置 ===> 每个视图都会被处理为 QueryResultWrapper 对象
// 3、获取异步处理结果 => 封装为 QueryResultWrappers
return viewConfigList.stream()
.map(e -> handleSingleViewAsync(e, queryParams))
.map(AsyncUtil::get)
.collect(Collectors.toCollection(QueryResultWrappers::new));
}

/**
* 异步处理单个视图
*
* @param tenantId 防止经销商上下文的丢失 => 异步处理时,重新 set
* @param traceId 防止日志追踪id上下文的丢失 => 异步处理时,重新 set
* @return 异步处理对象 => get() 阻塞获取结果
*/
private CompletableFuture<QueryResultWrapper> handleSingleViewAsync(ViewConfig viewConfig, BiQueryParams queryParams) {
// 0、防止经销商上下文的丢失 => 异步处理时,重新 set
// 0、防止日志追踪id上下文的丢失 => 异步处理时,重新 set
final String tenantId = TenantIdHolder.get();
final String traceId = MDCUtil.get();

return CompletableFuture.supplyAsync(() -> {
TenantIdHolder.set(tenantId);
MDCUtil.set(traceId);
return handleSingleView2(viewConfig, SerializeUtil.clone(queryParams));
});
}

/**
* 处理单个视图
*
* @param viewConfig 视图配置
* @param queryParams 查询参数
* @return 单个视图查询结果包装【查询结果,同比,环比,...】
*/
private QueryResultWrapper handleSingleView(ViewConfig viewConfig, BiQueryParams queryParams) {
DateTimeBo dateTimeBo = queryParams.getDateTimeBo();
QueryResultWrapper wrapper = new QueryResultWrapper();

// [1] 前置处理、视图查询、后置处理
preProcess(viewConfig, queryParams);//-------------------------------pre-前置处理
QueryResult source = viewSelect(viewConfig, queryParams);//----------视图查询
source = postProcess(source, viewConfig, queryParams);//-------------post-后置处理
wrapper.setSource(source);

// [2] 处理比较数据(同比、环比)
handleCompareData(wrapper, viewConfig, queryParams);
return wrapper;
}

private QueryResultWrapper handleSingleView2(ViewConfig viewConfig, BiQueryParams queryParams) {
String tenantId = TenantIdHolder.get();
String traceId = MDCUtil.get();
TenantIdHolder.set(tenantId);
MDCUtil.set(traceId);

DateTimeBo dateTimeBo = queryParams.getDateTimeBo();
QueryResultWrapper wrapper = new QueryResultWrapper();

CompletableFuture<Object> f1;
CompletableFuture<Object> f2;
CompletableFuture<Object> f3;

// [1] 前置处理、视图查询、后置处理
if (1 == 1) {
f1 = CompletableFuture.supplyAsync(() -> {
BiQueryParams _biQueryParams = SerializeUtil.clone(queryParams);
preProcess(viewConfig, _biQueryParams);//-------------------------------pre-前置处理
QueryResult source = viewSelect(viewConfig, _biQueryParams);//----------视图查询
source = postProcess(source, viewConfig, _biQueryParams);//-------------post-后置处理
wrapper.setSource(source);
return null;
});

}

// [2] 处理比较数据(同比、环比)
// handleCompareData(wrapper, viewConfig, queryParams);
boolean isCompareData = BooleanUtil.isTrue(viewConfig.getCompareData());
if (2 == 2) {
f2 = CompletableFuture.supplyAsync(() -> {
if (isCompareData) {
BiQueryParams _biQueryParams = SerializeUtil.clone(queryParams);
// 2、yoy (year on year) 同比数据查询
_biQueryParams.setStartDate(dateTimeBo.getComparedStartDate());
_biQueryParams.setEndDate(dateTimeBo.getComparedEndDate());
QueryResult yoy = viewSelect(viewConfig, _biQueryParams);
yoy = postProcess(yoy, viewConfig, _biQueryParams);
wrapper.setYoy(yoy);
}
return null;
});
}


if (3 == 3) {
f3 = CompletableFuture.supplyAsync(() -> {
if (isCompareData) {
BiQueryParams _biQueryParams = SerializeUtil.clone(queryParams);
// 3、mon (month on month) 环比数据查询
_biQueryParams.setStartDate(dateTimeBo.getChainStartDate());
_biQueryParams.setEndDate(dateTimeBo.getChainEndDate());
QueryResult mom = viewSelect(viewConfig, _biQueryParams);
mom = postProcess(mom, viewConfig, _biQueryParams);
wrapper.setMom(mom);
}
return null;
});
}

AsyncUtil.waitAll(f1, f2, f3);
return wrapper;
}

/**
* 处理比较数据(同比、环比)
*
* @param wrapper 查询结果包装器
* @param viewConfig 视图配置
* @param queryParams 查询参数
*/
private void handleCompareData(QueryResultWrapper wrapper, ViewConfig viewConfig, BiQueryParams queryParams) {
// 1、未开启同环比 => 直接返回
if (!BooleanUtil.isTrue(viewConfig.getCompareData())) {
return;
}

String startDate = queryParams.getStartDate();
String endDate = queryParams.getEndDate();

// 2、yoy (year on year) 同比数据查询
DateTimeBo dateTimeBo = queryParams.getDateTimeBo();
queryParams.setStartDate(dateTimeBo.getComparedStartDate());
queryParams.setEndDate(dateTimeBo.getComparedEndDate());
QueryResult yoy = viewSelect(viewConfig, queryParams);
yoy = postProcess(yoy, viewConfig, queryParams);
wrapper.setYoy(yoy);

// 3、mon (month on month) 环比数据查询
queryParams.setStartDate(dateTimeBo.getChainStartDate());
queryParams.setEndDate(dateTimeBo.getChainEndDate());
QueryResult mom = viewSelect(viewConfig, queryParams);
mom = postProcess(mom, viewConfig, queryParams);
wrapper.setMom(mom);

// 4、时间重置 => 因为上面把时间替换为了 同比/环比时间
queryParams.setStartDate(startDate);
queryParams.setEndDate(endDate);
}

/**
* 更新财年时间
*
* @param queryParams 查询参数
*/
private void updateFiscalTime(BiQueryParams queryParams) {
// 1. 日:无需更新财年时间
if (queryParams.getDateType().isDay()) {
return;
}

// 2. 更新财年时间
DateTimeBo dateTimeBo = queryParams.getDateTimeBo();
queryParams.setStartDate(dateTimeBo.getFiscalStartDate());
queryParams.setEndDate(dateTimeBo.getFiscalEndDate());
}

/**
* 视图查询(构建查询变量,feign调用 datart)
*
* @param viewConfig 视图配置
* @param queryParams 查询参数
* @return 视图查询结果
*/
private QueryResult viewSelect(ViewConfig viewConfig, BiQueryParams queryParams) {
// [1] 查询参数 => SQL 查询变量
String viewPath = viewConfig.getViewPath();
ViewQry viewQry = ViewQry.of(viewPath).put(queryParams);

// [2] 表名变量(yml取变量值) => 表名变量放入时,不加单引号
if (CollectionUtil.isNotEmpty(viewConfig.getTableVariable())) {
TableVariable tableVariable = viewConfig.getTableVariable().get(queryParams.getDateType());
tableVariable.forEach(viewQry::put);
}

// [3] 列名变量(查询参数中取变量值) => 列名变量放入时,不加单引号
queryParams.getColumnVariable().forEach(viewQry::put);

// [4] 租户变量自动填充
viewQry.putSingleQuote("tenant_id", TenantIdHolder.get());

// [5] 视图查询 => feign 调用 datart 服务
return dataProviderService.selectList(viewQry);
}

/**
* 前置处理
*
* @param viewConfig 视图配置
* @param queryParams 查询参数
*/
private void preProcess(ViewConfig viewConfig, BiQueryParams queryParams) {
Map<String, String> preProcessors = viewConfig.getPreProcessor();
if (MapUtil.isEmpty(preProcessors)) {
return;
}

for (Map.Entry<String, String> e : preProcessors.entrySet()) {
String key = e.getKey();
String value = e.getValue();

PreProcessor preProcessor = ProcessorManager.getPreProcessor(key);
Assert.notNull(preProcessor, "找不到前置处理器,bi.yml 配置有误 key=[{}]", key);
preProcessor.process(viewConfig, value, queryParams);
}

}

/**
* 后置处理
*
* @param queryResult 视图查询结果
* @param viewConfig 视图配置
* @param queryParams 查询参数
* @return 视图查询结果
*/
private QueryResult postProcess(QueryResult queryResult, ViewConfig viewConfig, BiQueryParams queryParams) {
Map<String, String> postProcessor = viewConfig.getPostProcessor();
if (MapUtil.isEmpty(postProcessor)) {
return queryResult;
}

for (Map.Entry<String, String> e : postProcessor.entrySet()) {
String key = e.getKey();
String value = e.getValue();

PostProcessor processor = ProcessorManager.getPostProcessor(key);
Assert.notNull(processor, "找不到后置处理器,bi.yml 配置有误 key=[{}]", key);
queryResult = processor.process(queryResult, value, queryParams);
}

return queryResult;
}

/**
* 聚合处理
*
* @param wrappers 视图查询结果包装集
* @param biConfig 报表配置
* @param queryParams 查询参数
* @return 视图查询结果包装集
*/
private QueryResultWrappers aggProcess(QueryResultWrappers wrappers, BiConfig biConfig, BiQueryParams queryParams) {
Map<String, String> aggProcessor = biConfig.getAggProcessor();
if (MapUtil.isEmpty(aggProcessor)) {
return wrappers;
}

for (Map.Entry<String, String> e : aggProcessor.entrySet()) {
String key = e.getKey();
String value = e.getValue();

AggProcessor processor = ProcessorManager.getAggProcessor(key);
Assert.notNull(processor, "找不到聚合处理器,bi.yml 配置有误 key=[{}]", key);
wrappers = processor.process(wrappers, value, queryParams);
}

return wrappers;
}

}

2022-08-02 18:35:38

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package com.amoros.newbi.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.thread.AsyncUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.SerializeUtil;
import com.amoros.newbi.service.context.TenantIdHolder;
import com.amoros.newbi.service.dataobject.dto.DateTimeBo;
import com.amoros.newbi.service.dataobject.dto.QueryResultWrapper;
import com.amoros.newbi.service.dataobject.dto.QueryResultWrappers;
import com.amoros.newbi.service.dataobject.properties.BiConfig;
import com.amoros.newbi.service.dataobject.properties.BiReportProperties;
import com.amoros.newbi.service.dataobject.properties.ViewConfig;
import com.amoros.newbi.service.dataobject.qry.BiQueryParams;
import com.amoros.newbi.service.dataobject.qry.BiReportQry;
import com.amoros.newbi.service.processor.ProcessorManager;
import com.amoros.newbi.service.processor.agg.AggProcessor;
import com.amoros.newbi.service.processor.post.PostProcessor;
import com.amoros.newbi.service.processor.pre.PreProcessor;
import com.amoros.newbi.service.service.BiService;
import com.amoros.newbi.service.util.MDCUtil;
import datart.api.service.DataProviderService;
import datart.api.service.custom.qry.TableVariable;
import datart.api.service.custom.qry.ViewQry;
import datart.api.service.custom.result.QueryResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BiServiceImpl implements BiService {

@Autowired
private BiReportProperties biReportProperties;

@Autowired
private DataProviderService dataProviderService;

@Override
public QueryResultWrappers biReport(BiReportQry biReportQry) {
// [0] 初始化参数
String reportCode = biReportQry.getReportCode();
BiQueryParams queryParams = biReportQry.getBiQueryParams();

// [1] 正常时间 => 财年时间
updateFiscalTime(queryParams);

// [2] 获取配置 reportCode => bi.yml 配置
BiConfig biConfig = biReportProperties.getConfig().get(reportCode);
Assert.notNull(biConfig, "获取不到 bi 相关配置,reportCode=[{}]", reportCode);

// [3] 依次处理所有视图
QueryResultWrappers wrappers = handleMultiView(biConfig.getViewConfigList(), queryParams);

// [4] 所有视图处理完毕 => 聚合处理器
wrappers = aggProcess(wrappers, biConfig, queryParams);

// [5] 返回结果
return wrappers;
}

/**
* 处理多个视图 => 异步处理每一个视图配置 ↓↓↓
*
* @param viewConfigList 多个视图配置
* @param queryParams 查询参数
* @return 视图查询结果封装集
*/
private QueryResultWrappers handleMultiView(List<ViewConfig> viewConfigList, BiQueryParams queryParams) {
// 1、遍历 视图配置列表
// 2、异步处理 视图配置 ===> 每个视图都会被处理为 QueryResultWrapper 对象
CompletableFuture<QueryResultWrapper>[] completableFutures = viewConfigList.stream()
.map(viewConfig -> supplyAsync(() -> handleSingleView(viewConfig, queryParams)))
.toArray(CompletableFuture[]::new);

// 3、等待所有视图查询完成
AsyncUtil.waitAll(completableFutures);

// 4、获取异步处理结果 => 封装为 QueryResultWrappers
return Arrays.stream(completableFutures)
.map(AsyncUtil::get)
.collect(Collectors.toCollection(QueryResultWrappers::new));
}

/**
* 异步处理每一个视图配置 => 多线程处理 source、yoy、mom
*
* @param viewConfig 视图配置
* @param queryParams 查询参数
* @return 单个视图查询结果包装【查询结果,同比,环比,...】
*/
private QueryResultWrapper handleSingleView(ViewConfig viewConfig, BiQueryParams queryParams) {
QueryResultWrapper wrapper = new QueryResultWrapper();
DateTimeBo bo = queryParams.getDateTimeBo();

// [1] source 源数据查询
CompletableFuture<Void> f_source = runAsync(() -> {
wrapper.setSource(handleView(viewConfig, queryParams, bo.getStartDate(), bo.getEndDate()));
});

if (!BooleanUtil.isTrue(viewConfig.getCompareData())) {
AsyncUtil.get(f_source);
return wrapper;
}

/**
* ↓↓↓ 同环比处理 ↓↓↓
*/

// [2] yoy (year on year) 同比数据查询
CompletableFuture<Void> f_yoy = runAsync(() -> {
wrapper.setYoy(handleView(viewConfig, queryParams, bo.getComparedStartDate(), bo.getComparedEndDate()));
});

// [3] mon (month on month) 环比数据查询
CompletableFuture<Void> f_mom = runAsync(() -> {
wrapper.setMom(handleView(viewConfig, queryParams, bo.getChainStartDate(), bo.getChainEndDate()));
});

AsyncUtil.waitAll(f_source, f_yoy, f_mom);// 等待 source、yoy、mom 都处理完毕 => return
return wrapper;
}

/**
* 处理视图 => 前置处理、视图查询、后置处理
*
* @param viewConfig 视图配置
* @param queryParams 查询参数
* @param startDate 开始时间
* @param endDate 结束时间
* @return 视图查询结果
*/
private QueryResult handleView(ViewConfig viewConfig, BiQueryParams queryParams, Date startDate, Date endDate) {
// deep clone => 防并发修改
BiQueryParams _biQueryParams = SerializeUtil.clone(queryParams);
_biQueryParams.setStartDate(startDate);
_biQueryParams.setEndDate(endDate);

// 前置处理、视图查询、后置处理
preProcess(viewConfig, _biQueryParams);
QueryResult queryResult = viewSelect(viewConfig, _biQueryParams);
queryResult = postProcess(queryResult, viewConfig, _biQueryParams);
return queryResult;
}

/**
* 更新财年时间
*
* @param queryParams 查询参数
*/
private void updateFiscalTime(BiQueryParams queryParams) {
// 1. 日:无需更新财年时间
if (queryParams.getDateType().isDay()) {
return;
}

// 2. 更新财年时间
DateTimeBo dateTimeBo = queryParams.getDateTimeBo();
queryParams.setStartDate(dateTimeBo.getFiscalStartDate());
queryParams.setEndDate(dateTimeBo.getFiscalEndDate());
}

/**
* 视图查询(构建查询变量,feign调用 datart)
*
* @param viewConfig 视图配置
* @param queryParams 查询参数
* @return 视图查询结果
*/
private QueryResult viewSelect(ViewConfig viewConfig, BiQueryParams queryParams) {
// [1] 查询参数 => SQL 查询变量
String viewPath = viewConfig.getViewPath();
ViewQry viewQry = ViewQry.of(viewPath).put(queryParams);

// [2] 表名变量(yml取变量值) => 表名变量放入时,不加单引号
if (CollectionUtil.isNotEmpty(viewConfig.getTableVariable())) {
TableVariable tableVariable = viewConfig.getTableVariable().get(queryParams.getDateType());
tableVariable.forEach(viewQry::put);
}

// [3] 列名变量(查询参数中取变量值) => 列名变量放入时,不加单引号
queryParams.getColumnVariable().forEach(viewQry::put);

// [4] 租户变量自动填充
viewQry.putSingleQuote("tenant_id", TenantIdHolder.get());

// [5] 视图查询 => feign 调用 datart 服务
return dataProviderService.selectList(viewQry);
}

/**
* 前置处理
*
* @param viewConfig 视图配置
* @param queryParams 查询参数
*/
private void preProcess(ViewConfig viewConfig, BiQueryParams queryParams) {
Map<String, String> preProcessors = viewConfig.getPreProcessor();
if (MapUtil.isEmpty(preProcessors)) {
return;
}

for (Map.Entry<String, String> e : preProcessors.entrySet()) {
String key = e.getKey();
String value = e.getValue();

PreProcessor preProcessor = ProcessorManager.getPreProcessor(key);
Assert.notNull(preProcessor, "找不到前置处理器,bi.yml 配置有误 key=[{}]", key);
preProcessor.process(viewConfig, value, queryParams);
}

}

/**
* 后置处理
*
* @param queryResult 视图查询结果
* @param viewConfig 视图配置
* @param queryParams 查询参数
* @return 视图查询结果
*/
private QueryResult postProcess(QueryResult queryResult, ViewConfig viewConfig, BiQueryParams queryParams) {
Map<String, String> postProcessor = viewConfig.getPostProcessor();
if (MapUtil.isEmpty(postProcessor)) {
return queryResult;
}

for (Map.Entry<String, String> e : postProcessor.entrySet()) {
String key = e.getKey();
String value = e.getValue();

PostProcessor processor = ProcessorManager.getPostProcessor(key);
Assert.notNull(processor, "找不到后置处理器,bi.yml 配置有误 key=[{}]", key);
queryResult = processor.process(queryResult, value, queryParams);
}

return queryResult;
}

/**
* 聚合处理
*
* @param wrappers 视图查询结果包装集
* @param biConfig 报表配置
* @param queryParams 查询参数
* @return 视图查询结果包装集
*/
private QueryResultWrappers aggProcess(QueryResultWrappers wrappers, BiConfig biConfig, BiQueryParams queryParams) {
Map<String, String> aggProcessor = biConfig.getAggProcessor();
if (MapUtil.isEmpty(aggProcessor)) {
return wrappers;
}

for (Map.Entry<String, String> e : aggProcessor.entrySet()) {
String key = e.getKey();
String value = e.getValue();

AggProcessor processor = ProcessorManager.getAggProcessor(key);
Assert.notNull(processor, "找不到聚合处理器,bi.yml 配置有误 key=[{}]", key);
wrappers = processor.process(wrappers, value, queryParams);
}

return wrappers;
}

/**
* 异步处理 [有返回值]
*/
private <T> CompletableFuture<T> supplyAsync(Supplier<T> supplier) {
// 0、防止经销商上下文的丢失 => 异步处理时,重新 set
// 0、防止日志追踪id上下文的丢失 => 异步处理时,重新 set
final String tenantId = TenantIdHolder.get();
final String traceId = MDCUtil.get();

return CompletableFuture.supplyAsync(() -> {
TenantIdHolder.set(tenantId);
MDCUtil.set(traceId);
return supplier.get();
});
}

/**
* 异步处理 [无返回值]
*/
private CompletableFuture<Void> runAsync(Runnable runnable) {
// 0、防止经销商上下文的丢失 => 异步处理时,重新 set
// 0、防止日志追踪id上下文的丢失 => 异步处理时,重新 set
final String tenantId = TenantIdHolder.get();
final String traceId = MDCUtil.get();

return CompletableFuture.runAsync(() -> {
TenantIdHolder.set(tenantId);
MDCUtil.set(traceId);
runnable.run();
});
}
}