工业互联网平台2.0版本后端代码
houzhongjian
2025-05-29 24996ea75ec4ca3b7d154387bfe37ec9dd387255
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
package com.iailab.module.data.ind.collection;
 
import com.iailab.module.data.common.enums.ItemTypeEnum;
import com.iailab.module.data.ind.collection.handler.AtomItemHandler;
import com.iailab.module.data.ind.collection.handler.CalItemHandler;
import com.iailab.module.data.ind.collection.handler.DerItemHandler;
import com.iailab.module.data.ind.item.entity.IndItemEntity;
import com.iailab.module.data.ind.item.service.IndItemService;
import com.iailab.module.data.ind.item.vo.IndItemValueVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2024年10月04日
 */
@Slf4j
@Component
public class IndItemCollector {
 
    @Autowired
    private IndItemService indItemService;
 
    @Autowired
    private AtomItemHandler atomItemHandler;
 
    @Autowired
    private DerItemHandler derItemHandler;
 
    @Autowired
    private CalItemHandler calItemHandler;
 
    public List<IndItemValueVO> queryValue(String itemNo) {
        List<IndItemValueVO> result = new ArrayList<IndItemValueVO>();
        IndItemEntity indItem = indItemService.getInfoByNo(itemNo);
        if (indItem == null) {
            return result;
        }
        ItemTypeEnum itemType = ItemTypeEnum.getEumByCode(indItem.getItemType());
        switch (itemType) {
            case ATOM:
                result = atomItemHandler.queryValue(indItem.getId());
                break;
            case DER:
                result = derItemHandler.queryValue(indItem.getId());
                break;
            case CAL:
                result = calItemHandler.queryValue(indItem.getId());
                break;
            default:
                break;
        }
        // 考虑指标精度和转换系数
        handleResult(result,indItem);
        return result;
    }
 
    public List<IndItemValueVO> queryValue(String itemNo, Date startTime, Date endTime) {
        List<IndItemValueVO> result = new ArrayList<IndItemValueVO>();
        IndItemEntity indItem = indItemService.getInfoByNo(itemNo);
        if (indItem == null) {
            return result;
        }
        ItemTypeEnum itemType = ItemTypeEnum.getEumByCode(indItem.getItemType());
        switch (itemType) {
            case ATOM:
                result = atomItemHandler.queryValue(indItem.getId());
                break;
            case DER:
                result = derItemHandler.queryValue(indItem.getId(), startTime, endTime);
                break;
            case CAL:
                result = calItemHandler.queryValue(indItem.getId(), startTime, endTime);
                break;
            default:
                break;
        }
        // 考虑指标精度和转换系数
        handleResult(result,indItem);
        return result;
    }
 
    private void handleResult(List<IndItemValueVO> result, IndItemEntity indItem) {
        if (!CollectionUtils.isEmpty(result)) {
            result.forEach(e -> {
                if (e != null) {
                    Object dataValue = e.getDataValue();
                    if (dataValue != null && dataValue instanceof Number) {
                        BigDecimal value = new BigDecimal(dataValue.toString());
                        if (StringUtils.isNotBlank(indItem.getCoefficient())) {
                            value = value.multiply(new BigDecimal(indItem.getCoefficient()));
                        }
                        if (indItem.getPrecision() != null) {
                            value = value.setScale(indItem.getPrecision(), BigDecimal.ROUND_HALF_UP);
                        }
                        e.setDataValue(value.doubleValue());
                    }
                }
 
            });
        }
    }
}