提交 | 用户 | 时间
|
ed4f78
|
1 |
package com.iailab.module.data.plan.item.collection; |
潘 |
2 |
|
|
3 |
import com.iailab.framework.tenant.core.context.DataContextHolder; |
|
4 |
import com.iailab.module.data.common.utils.DateUtils; |
|
5 |
import com.iailab.module.data.plan.data.entity.PlanDataSetEntity; |
|
6 |
import com.iailab.module.data.plan.data.service.PlanDataSetService; |
|
7 |
import com.iailab.module.data.plan.item.entity.PlanItemEntity; |
|
8 |
import com.iailab.module.data.plan.item.service.PlanItemService; |
|
9 |
import com.iailab.module.data.plan.item.vo.PlanItemDataVO; |
|
10 |
import com.iailab.module.data.plan.item.vo.PlanItemValueVO; |
|
11 |
import lombok.extern.slf4j.Slf4j; |
|
12 |
import org.apache.commons.lang3.StringUtils; |
|
13 |
import org.springframework.beans.factory.annotation.Autowired; |
|
14 |
import org.springframework.stereotype.Component; |
|
15 |
import org.springframework.util.CollectionUtils; |
|
16 |
|
|
17 |
import java.util.*; |
|
18 |
|
|
19 |
/** |
|
20 |
* @author PanZhibao |
|
21 |
* @Description |
|
22 |
* @createTime 2024年11月02日 |
|
23 |
*/ |
|
24 |
@Slf4j |
|
25 |
@Component |
|
26 |
public class PlanItemCollector { |
|
27 |
|
|
28 |
@Autowired |
|
29 |
private PlanItemService planItemService; |
|
30 |
|
|
31 |
@Autowired |
|
32 |
private PlanDataSetService planDataSetService; |
|
33 |
|
|
34 |
public List<PlanItemValueVO> queryValue(String itemNo, Date startTime, Date endTime) { |
|
35 |
List<PlanItemValueVO> result = new ArrayList<>(); |
|
36 |
PlanItemEntity itemEntity = planItemService.getInfoByNo(itemNo); |
|
37 |
PlanDataSetEntity dataSet = planDataSetService.get(itemEntity.getDataSet()); |
|
38 |
if (dataSet == null) { |
|
39 |
log.warn("数据集不能为空"); |
|
40 |
return null; |
|
41 |
} |
|
42 |
if (StringUtils.isEmpty(dataSet.getDataSource())) { |
|
43 |
log.warn("数据源不能为空"); |
|
44 |
return null; |
|
45 |
} |
a3a072
|
46 |
Map<String, Object> params = getSqlParams(dataSet, startTime, endTime); |
ed4f78
|
47 |
DataContextHolder.setDataSourceId(Long.valueOf(dataSet.getDataSource())); |
潘 |
48 |
List<PlanItemDataVO> dataList = planItemService.getSourceValue(params); |
|
49 |
|
|
50 |
Calendar calendar = Calendar.getInstance(); |
|
51 |
calendar.setTime(startTime); |
a3a072
|
52 |
int dataLength = (int) ((endTime.getTime() - startTime.getTime()) / (1000 * 60)) + 1; |
潘 |
53 |
for (int i = 0; i < dataLength; i++) { |
ed4f78
|
54 |
PlanItemValueVO itemValue = new PlanItemValueVO(); |
潘 |
55 |
Date dataTime = calendar.getTime(); |
|
56 |
itemValue.setDataTime(dataTime); |
|
57 |
itemValue.setDataValue(0d); |
|
58 |
if (!CollectionUtils.isEmpty(dataList)) { |
|
59 |
dataList.forEach(item -> { |
|
60 |
Date s = DateUtils.parse(item.getStartTime(), DateUtils.DATE_NUMBER_PATTERN); |
|
61 |
Date e = DateUtils.parse(item.getEndTime(), DateUtils.DATE_NUMBER_PATTERN); |
|
62 |
if ((dataTime.after(s) && dataTime.before(e)) |
|
63 |
|| dataTime.equals(s) || dataTime.equals(e)) { |
|
64 |
itemValue.setDataValue(1d); |
|
65 |
} |
|
66 |
}); |
|
67 |
} |
|
68 |
calendar.add(Calendar.MINUTE, 1); |
|
69 |
result.add(itemValue); |
|
70 |
} |
|
71 |
return result; |
|
72 |
} |
a3a072
|
73 |
|
潘 |
74 |
public List<PlanItemDataVO> getSourceValue(String itemNo, Date startTime, Date endTime) { |
|
75 |
PlanItemEntity itemEntity = planItemService.getInfoByNo(itemNo); |
|
76 |
PlanDataSetEntity dataSet = planDataSetService.get(itemEntity.getDataSet()); |
|
77 |
if (dataSet == null) { |
|
78 |
log.warn("数据集不能为空"); |
|
79 |
return null; |
|
80 |
} |
|
81 |
if (StringUtils.isEmpty(dataSet.getDataSource())) { |
|
82 |
log.warn("数据源不能为空"); |
|
83 |
return null; |
|
84 |
} |
|
85 |
Map<String, Object> params = getSqlParams(dataSet, startTime, endTime); |
|
86 |
return planItemService.getSourceValue(params); |
|
87 |
} |
|
88 |
|
|
89 |
private Map<String, Object> getSqlParams(PlanDataSetEntity dataSet, Date startTime, Date endTime) { |
|
90 |
Map<String, Object> params = new HashMap<String, Object>(); |
|
91 |
params.put("selectSql", " plan_t.start_time, plan_t.end_time"); |
|
92 |
params.put("viewSql", dataSet.getQuerySql()); |
|
93 |
StringBuilder whereSql = new StringBuilder(); |
|
94 |
String startStr = DateUtils.format(startTime, DateUtils.DATE_NUMBER_PATTERN); |
|
95 |
String endStr = DateUtils.format(endTime, DateUtils.DATE_NUMBER_PATTERN); |
05a1bb
|
96 |
whereSql.append(" plan_t.start_time <= '") |
a3a072
|
97 |
.append(endStr) |
05a1bb
|
98 |
.append("'") |
潘 |
99 |
.append(" and plan_t.end_time >= '") |
|
100 |
.append(startStr) |
|
101 |
.append("'"); |
a3a072
|
102 |
params.put("whereSql", whereSql.toString()); |
潘 |
103 |
return params; |
|
104 |
} |
ed4f78
|
105 |
} |