提交 | 用户 | 时间
|
cf757d
|
1 |
package com.iailab.module.data.ind.collection.handler; |
潘 |
2 |
|
|
3 |
import com.alibaba.cloud.commons.lang.StringUtils; |
|
4 |
import com.iailab.framework.common.constant.CommonConstant; |
|
5 |
import com.iailab.module.data.common.enums.ItemTypeEnum; |
|
6 |
import com.iailab.module.data.common.enums.JsErrorCode; |
|
7 |
import com.iailab.module.data.common.utils.JavaScriptHandler; |
|
8 |
import com.iailab.module.data.ind.item.entity.IndItemEntity; |
|
9 |
import com.iailab.module.data.ind.item.service.IndItemCalService; |
|
10 |
import com.iailab.module.data.ind.item.service.IndItemService; |
|
11 |
import com.iailab.module.data.ind.item.vo.IndItemCalVO; |
|
12 |
import com.iailab.module.data.ind.item.vo.IndItemValueVO; |
|
13 |
import com.iailab.module.data.ind.value.service.IndItemValueService; |
|
14 |
import lombok.extern.slf4j.Slf4j; |
|
15 |
import org.springframework.beans.factory.annotation.Autowired; |
|
16 |
import org.springframework.stereotype.Component; |
|
17 |
|
|
18 |
import java.math.BigDecimal; |
|
19 |
import java.util.*; |
|
20 |
|
|
21 |
/** |
|
22 |
* @author PanZhibao |
|
23 |
* @Description |
|
24 |
* @createTime 2024年10月04日 |
|
25 |
*/ |
|
26 |
@Slf4j |
|
27 |
@Component |
|
28 |
public class CalItemHandler { |
|
29 |
|
|
30 |
@Autowired |
|
31 |
private JavaScriptHandler javaScriptHandler; |
|
32 |
|
|
33 |
public static final String regex = "[+\\-\\*\\/\\(\\)\\&\\|\\>\\<]"; |
|
34 |
|
|
35 |
@Autowired |
|
36 |
private IndItemCalService indItemCalService; |
|
37 |
|
|
38 |
@Autowired |
|
39 |
private AtomItemHandler atomItemHandler; |
|
40 |
|
|
41 |
@Autowired |
|
42 |
private DerItemHandler derItemHandler; |
|
43 |
|
|
44 |
@Autowired |
|
45 |
private IndItemService indItemService; |
|
46 |
|
|
47 |
public List<IndItemValueVO> queryValue(String itemId) { |
|
48 |
List<IndItemValueVO> result = new ArrayList<IndItemValueVO>(); |
|
49 |
try { |
|
50 |
IndItemCalVO indItem = indItemCalService.getByItemId(itemId); |
|
51 |
String expression = indItem.getExpression(); |
|
52 |
if (StringUtils.isBlank(expression)) { |
|
53 |
return result; |
|
54 |
} |
|
55 |
String[] arr = expression.split(regex); |
|
56 |
int dataLength = 0; |
|
57 |
List<List<IndItemValueVO>> valueMix = new ArrayList<>(); |
|
58 |
for (int i = 0; i < arr.length; i++) { |
|
59 |
String s = arr[i]; |
|
60 |
if (StringUtils.isBlank(s)) { |
|
61 |
continue; |
|
62 |
} |
|
63 |
IndItemEntity subItem = indItemService.getInfoByNo(s); |
|
64 |
ItemTypeEnum itemType = ItemTypeEnum.getEumByCode(subItem.getItemType()); |
|
65 |
switch (itemType) { |
|
66 |
case ATOM: |
|
67 |
valueMix.add(atomItemHandler.queryValue(subItem.getId())); |
|
68 |
break; |
|
69 |
case DER: |
|
70 |
valueMix.add(derItemHandler.queryValue(subItem.getId())); |
|
71 |
break; |
|
72 |
default: |
|
73 |
break; |
|
74 |
} |
|
75 |
} |
|
76 |
dataLength = valueMix.get(0).size(); |
|
77 |
|
|
78 |
for (int i = 0; i < dataLength; i++) { |
|
79 |
Map<String, IndItemValueVO> dataMap = new HashMap<>(); |
|
80 |
for (int j = 0; j < valueMix.size(); j++) { |
|
81 |
IndItemValueVO valueDTO = valueMix.get(j).get(i); |
|
82 |
dataMap.put(valueDTO.getItemNo(), valueDTO); |
|
83 |
} |
|
84 |
IndItemValueVO itemValue = this.singleCompute(indItem, dataMap); |
|
85 |
result.add(itemValue); |
|
86 |
} |
|
87 |
|
|
88 |
} catch (Exception ex) { |
|
89 |
ex.printStackTrace(); |
|
90 |
} |
|
91 |
return result; |
|
92 |
} |
|
93 |
|
|
94 |
public List<IndItemValueVO> queryValue(String itemId, Date startTime, Date endTime) { |
|
95 |
List<IndItemValueVO> result = new ArrayList<IndItemValueVO>(); |
|
96 |
try { |
|
97 |
IndItemCalVO indItem = indItemCalService.getByItemId(itemId); |
|
98 |
String expression = indItem.getExpression(); |
|
99 |
if (StringUtils.isBlank(expression)) { |
|
100 |
return result; |
|
101 |
} |
|
102 |
String[] arr = expression.split(regex); |
|
103 |
int dataLength = 0; |
|
104 |
List<List<IndItemValueVO>> valueMix = new ArrayList<>(); |
|
105 |
for (int i = 0; i < arr.length; i++) { |
|
106 |
String s = arr[i]; |
|
107 |
if (StringUtils.isBlank(s)) { |
|
108 |
continue; |
|
109 |
} |
|
110 |
IndItemEntity subItem = indItemService.getInfoByNo(s); |
|
111 |
ItemTypeEnum itemType = ItemTypeEnum.getEumByCode(subItem.getItemType()); |
|
112 |
switch (itemType) { |
|
113 |
case ATOM: |
|
114 |
valueMix.add(atomItemHandler.queryValue(subItem.getId())); |
|
115 |
break; |
|
116 |
case DER: |
|
117 |
valueMix.add(derItemHandler.queryValue(subItem.getId(), startTime, endTime)); |
|
118 |
break; |
|
119 |
default: |
|
120 |
break; |
|
121 |
} |
|
122 |
} |
|
123 |
dataLength = valueMix.get(0).size(); |
|
124 |
|
|
125 |
for (int i = 0; i < dataLength; i++) { |
|
126 |
Map<String, IndItemValueVO> dataMap = new HashMap<>(); |
|
127 |
for (int j = 0; j < valueMix.size(); j++) { |
|
128 |
IndItemValueVO valueDTO = valueMix.get(j).get(i); |
|
129 |
dataMap.put(valueDTO.getItemNo(), valueDTO); |
|
130 |
} |
|
131 |
IndItemValueVO itemValue = this.singleCompute(indItem, dataMap); |
|
132 |
result.add(itemValue); |
|
133 |
} |
|
134 |
|
|
135 |
} catch (Exception ex) { |
|
136 |
ex.printStackTrace(); |
|
137 |
} |
|
138 |
return result; |
|
139 |
} |
|
140 |
|
|
141 |
private IndItemValueVO singleCompute(IndItemCalVO dto, Map<String, IndItemValueVO> dataMap) { |
|
142 |
IndItemValueVO resultDto = new IndItemValueVO(); |
|
143 |
String dataTime = ""; |
|
144 |
BigDecimal dataValue = CommonConstant.ZERO_VALUE; |
|
145 |
String expression = dto.getExpression(); |
|
146 |
String[] arr = expression.split(regex); |
|
147 |
|
|
148 |
for (int i = 0; i < arr.length; i++) { |
|
149 |
String s = arr[i]; |
|
150 |
if (StringUtils.isNotBlank(s) && dataMap.containsKey(s)) { |
|
151 |
dataTime = dataMap.get(s).getDataTime(); |
|
152 |
if (dataMap.get(s) == null || dataMap.get(s).getDataValue() == null) { |
|
153 |
resultDto.setDataTime(dataTime); |
|
154 |
return resultDto; |
|
155 |
} |
|
156 |
expression = expression.replace(s, dataMap.get(s).getDataValue().toString()); |
|
157 |
} |
|
158 |
} |
|
159 |
expression = expression.replace("&", "&&"); |
|
160 |
expression = expression.replace("|", "||"); |
|
161 |
expression = expression.replace("False", "false"); |
|
162 |
expression = expression.replace("True", "true"); |
|
163 |
log.info("ItemId=" + dto.getItemId() + ";expression=" + expression); |
|
164 |
String result = javaScriptHandler.eval(expression); |
|
165 |
log.info("result=" + result); |
|
166 |
if (result == null) { |
|
167 |
return null; |
|
168 |
} else if (result.contains(JsErrorCode.Infinity.name()) || |
|
169 |
result.contains(JsErrorCode.NaN.name())) { |
|
170 |
log.info("计算异常,使用默认值"); |
|
171 |
} else { |
|
172 |
dataValue = new BigDecimal(result); |
|
173 |
} |
|
174 |
resultDto.setDataTime(dataTime); |
|
175 |
resultDto.setDataValue(dataValue); |
|
176 |
return resultDto; |
|
177 |
} |
|
178 |
} |