wujingjing
2024-04-22 f106e4dffb8279cb90726e83e7edd631f4c77699
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
/**
 * 数组的扁平数据变为树形结构
 * [{ParentID}] => [{Children}]
 */
export const flatToTree = (data: Array<any>) => {
    if (!Array.isArray(data) || data?.length === 0) return [];
 
    const map = new Map(
        data.map((item) => {
            if (!item.Children) {
                item.Children = [];
            }
            return [item.ID, item];
        })
    );
    const result = [];
 
    for (const item of data) {
        const { ParentID } = item;
        if (ParentID === '0') {
            result.push(item);
        } else {
            const parent = map.get(ParentID);
            parent.Children.push(item);
        }
    }
    return result;
};
 
/**
 * 格式化表达式,以文本形式展示
 * @param isAuto 是否是自动
 * @param data
 * @returns 字符串或字符串数组
 */
export const formatExpression = (isAuto, data, isStrArray = false) => {
    if (!data || data.length === 0) {
        return isStrArray ? [] : '';
    }
    if (!isAuto && isStrArray) return data;
 
    let resultStr = '';
    let resultStrArr = [];
    if (isAuto) {
        (data as []).forEach((value, index) => {
            const el = value as any;
 
            if (el.MinValue == null && el.MaxValue != null) {
                const result = '≤' + el.MaxValue + '=' + el.Expression;
                isStrArray ? resultStrArr.push(result) : (resultStr += index + 1 + '.' + result + ';');
            }
            if (el.MinValue != null && el.MaxValue == null) {
                const result = '≥' + el.MinValue + '=' + el.Expression;
 
                isStrArray ? resultStrArr.push(result) : (resultStr += index + 1 + '.' + result + ';');
            }
            if (el.MinValue != null && el.MaxValue != null) {
                const result = '[' + el.MinValue + ',' + el.MaxValue + ']' + '=' + el.Expression;
                isStrArray ? resultStrArr.push(result) : (resultStr += index + 1 + '.' + result + ';');
            }
        });
    } else {
        (data as []).forEach((value, index) => {
            const el = value as any;
            resultStr += index + 1 + '.' + el + ';';
        });
    }
 
    const result = isStrArray ? resultStrArr : resultStr;
    return result;
};
 
/**
 * 用于趋势分析中,该表达式过滤掉不想要的数据
 * @param isRangeStyle,是否是类似于{MinValue,MaxValue} 这样的表达式
 * @param data
 * @param isStrArray 是否以每一个字符串写在一个数组中,这样的形式返回
 * @returns
 */
export const formatFilterExpression = (isRangeStyle, data, isStrArray = false) => {
    if (!data || data.length === 0) {
        return isStrArray ? [] : '';
    }
    if (!isRangeStyle && isStrArray) return data;
 
    let resultStr = '';
    let resultStrArr = [];
    if (isRangeStyle) {
        (data as []).forEach((value, index) => {
            const el = value as any;
 
            if (el.MinValue == null && el.MaxValue != null) {
                const result = '≤' + el.MaxValue;
                isStrArray ? resultStrArr.push(result) : (resultStr += index + 1 + '.' + result + ';');
            }
            if (el.MinValue != null && el.MaxValue == null) {
                const result = '≥' + el.MinValue;
 
                isStrArray ? resultStrArr.push(result) : (resultStr += index + 1 + '.' + result + ';');
            }
            if (el.MinValue != null && el.MaxValue != null) {
                const result = '[' + el.MinValue + ',' + el.MaxValue + ']';
                isStrArray ? resultStrArr.push(result) : (resultStr += index + 1 + '.' + result + ';');
            }
        });
    } else {
        (data as []).forEach((value, index) => {
            const el = value as any;
            resultStr += index + 1 + '.' + el + ';';
        });
    }
 
    const result = isStrArray ? resultStrArr : resultStr;
    return result;
};
 
/** 按 ID 查找事件树中的某个结点 */
export const findSignalType = (id: string, signalTypeTree) => {
    if (!id || !signalTypeTree) return;
    if (signalTypeTree.length === 0) return;
    let result = null;
 
    (signalTypeTree as any[]).forEach((node) => {
        if (node.Children && node.Children.length > 0) {
            for (const child of node.Children) {
                if (child.LogicalID === id && child.LogicalType === 'signal-type') {
                    result = child.LogicalName;
                }
            }
        }
    });
    return result;
};