/**
|
* 数组的扁平数据变为树形结构
|
* [{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;
|
};
|