提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.ind.collection.handler; |
H |
2 |
|
|
3 |
import com.iailab.module.data.common.enums.CommonConstant; |
|
4 |
import com.iailab.module.data.common.enums.JsErrorCode; |
|
5 |
import com.iailab.module.data.common.utils.JavaScriptHandler; |
|
6 |
import com.iailab.module.data.ind.service.IndItemService; |
|
7 |
import com.iailab.module.data.ind.dto.IndItemDTO; |
|
8 |
import com.iailab.module.data.ind.dto.IndItemValueDTO; |
|
9 |
import lombok.extern.slf4j.Slf4j; |
|
10 |
import org.apache.commons.lang3.StringUtils; |
|
11 |
import javax.annotation.Resource; |
|
12 |
import org.springframework.stereotype.Component; |
|
13 |
|
|
14 |
import java.math.BigDecimal; |
|
15 |
import java.util.ArrayList; |
|
16 |
import java.util.HashMap; |
|
17 |
import java.util.List; |
|
18 |
import java.util.Map; |
|
19 |
|
|
20 |
/** |
|
21 |
* @author PanZhibao |
|
22 |
* @Description |
|
23 |
* @createTime 2024年05月25日 |
|
24 |
*/ |
|
25 |
@Slf4j |
|
26 |
@Component |
|
27 |
public class CalItemHandle { |
|
28 |
|
|
29 |
@Resource |
|
30 |
private JavaScriptHandler javaScriptHandler; |
|
31 |
|
|
32 |
public static final String regex = "[+\\-\\*\\/\\(\\)\\&\\|\\>\\<]"; |
|
33 |
|
|
34 |
@Resource |
|
35 |
private IndItemService indItemService; |
|
36 |
|
|
37 |
@Resource |
|
38 |
private AtomItemHandle atomItemHandle; |
|
39 |
|
|
40 |
|
|
41 |
public List<IndItemValueDTO> getItemCalValue(String itemNo) { |
|
42 |
List<IndItemValueDTO> result = new ArrayList<>(); |
|
43 |
IndItemDTO indItemDTO = indItemService.getItemCal(itemNo); |
|
44 |
if (indItemDTO == null) { |
|
45 |
return result; |
|
46 |
} |
|
47 |
String expression = indItemDTO.getExpression(); |
|
48 |
if (StringUtils.isBlank(expression)) { |
|
49 |
return result; |
|
50 |
} |
|
51 |
String[] arr = expression.split(regex); |
|
52 |
int dataLength = 0; |
|
53 |
List<List<IndItemValueDTO>> valueMix = new ArrayList<>(); |
|
54 |
|
|
55 |
for (int i = 0; i < arr.length; i++) { |
|
56 |
String s = arr[i]; |
|
57 |
if (StringUtils.isBlank(s)) { |
|
58 |
continue; |
|
59 |
} |
|
60 |
valueMix.add(atomItemHandle.getItemSourceValue(s)); |
|
61 |
} |
|
62 |
dataLength = valueMix.get(0).size(); |
|
63 |
|
|
64 |
for (int i = 0; i < dataLength; i++) { |
|
65 |
Map<String, IndItemValueDTO> dataMap = new HashMap<>(); |
|
66 |
for (int j = 0; j < valueMix.size(); j++) { |
|
67 |
IndItemValueDTO valueDTO = valueMix.get(j).get(i); |
|
68 |
dataMap.put(valueDTO.getItemNo(), valueDTO); |
|
69 |
} |
|
70 |
IndItemValueDTO itemValue = this.singleCompute(indItemDTO, dataMap); |
|
71 |
result.add(itemValue); |
|
72 |
} |
|
73 |
return result; |
|
74 |
} |
|
75 |
|
|
76 |
public List<IndItemValueDTO> getItemCalValue(String itemNo, String start, String end) { |
|
77 |
List<IndItemValueDTO> result = new ArrayList<>(); |
|
78 |
IndItemDTO indItemDTO = indItemService.getItemCal(itemNo); |
|
79 |
if (indItemDTO == null) { |
|
80 |
return result; |
|
81 |
} |
|
82 |
String expression = indItemDTO.getExpression(); |
|
83 |
if (StringUtils.isBlank(expression)) { |
|
84 |
return result; |
|
85 |
} |
|
86 |
String[] arr = expression.split(regex); |
|
87 |
int dataLength = 0; |
|
88 |
List<List<IndItemValueDTO>> valueMix = new ArrayList<>(); |
|
89 |
|
|
90 |
for (int i = 0; i < arr.length; i++) { |
|
91 |
String s = arr[i]; |
|
92 |
if (StringUtils.isBlank(s)) { |
|
93 |
continue; |
|
94 |
} |
|
95 |
valueMix.add(atomItemHandle.getItemSourceValue(s, start, end)); |
|
96 |
} |
|
97 |
dataLength = valueMix.get(0).size(); |
|
98 |
|
|
99 |
for (int i = 0; i < dataLength; i++) { |
|
100 |
Map<String, IndItemValueDTO> dataMap = new HashMap<>(); |
|
101 |
for (int j = 0; j < valueMix.size(); j++) { |
|
102 |
IndItemValueDTO valueDTO = valueMix.get(j).get(i); |
|
103 |
dataMap.put(valueDTO.getItemNo(), valueDTO); |
|
104 |
} |
|
105 |
IndItemValueDTO itemValue = this.singleCompute(indItemDTO, dataMap); |
|
106 |
result.add(itemValue); |
|
107 |
} |
|
108 |
return result; |
|
109 |
} |
|
110 |
|
|
111 |
private IndItemValueDTO singleCompute(IndItemDTO dto, Map<String, IndItemValueDTO> dataMap) { |
|
112 |
IndItemValueDTO resultDto = new IndItemValueDTO(); |
|
113 |
String dataTime = ""; |
|
114 |
BigDecimal dataValue = CommonConstant.ZERO_VALUE; |
|
115 |
String expression = dto.getExpression(); |
|
116 |
String[] arr = expression.split(regex); |
|
117 |
|
|
118 |
for (int i = 0; i < arr.length; i++) { |
|
119 |
String s = arr[i]; |
|
120 |
if (StringUtils.isNotBlank(s) && dataMap.containsKey(s)) { |
|
121 |
dataTime = dataMap.get(s).getDataTime(); |
|
122 |
if (dataMap.get(s) == null || dataMap.get(s).getDataValue() == null) { |
|
123 |
resultDto.setDataTime(dataTime); |
|
124 |
return resultDto; |
|
125 |
} |
|
126 |
expression = expression.replace(s, dataMap.get(s).getDataValue().toString()); |
|
127 |
} |
|
128 |
} |
|
129 |
expression = expression.replace("&", "&&"); |
|
130 |
expression = expression.replace("|", "||"); |
|
131 |
expression = expression.replace("False", "false"); |
|
132 |
expression = expression.replace("True", "true"); |
|
133 |
log.info("ItemNo=" + dto.getItemNo() + ";expression=" + expression); |
|
134 |
String result = javaScriptHandler.eval(expression); |
|
135 |
log.info("result=" + result); |
|
136 |
if (result == null) { |
|
137 |
return null; |
|
138 |
} else if (result.contains(JsErrorCode.Infinity.name()) || |
|
139 |
result.contains(JsErrorCode.NaN.name())) { |
|
140 |
log.info("计算异常,使用默认值"); |
|
141 |
} else { |
|
142 |
dataValue = new BigDecimal(result); |
|
143 |
} |
|
144 |
resultDto.setDataTime(dataTime); |
|
145 |
|
|
146 |
if (dto.getCoefficient() != null) { |
|
147 |
dataValue = dataValue.multiply(dto.getCoefficient()); |
|
148 |
} |
|
149 |
if (dto.getPrecision() != null) { |
|
150 |
dataValue = dataValue.setScale(dto.getPrecision(), BigDecimal.ROUND_HALF_UP); |
|
151 |
} |
|
152 |
resultDto.setDataValue(dataValue); |
|
153 |
return resultDto; |
|
154 |
} |
|
155 |
|
|
156 |
} |