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