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