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