/**
 * 清洗风险分析数据
 * @param nodes
 * @returns {*}
 */
export function cleanRiskNetData(nodes) {
    for (let i = 0, len = nodes.length; i < len; i++) {
        // 生成唯一标识
        nodes[i].key = getUUID();
        // 下级失效集合
        if (nodes[i].children && nodes[i].children.length > 0) {
            cleanRiskNetData(nodes[i].children);
        }
        // 上级结构
        if (nodes[i].structureParent) {
            cleanRiskNetData([nodes[i].structureParent]);
        }
        // 上级功能
        if (nodes[i].functionParent) {
            cleanRiskNetData([nodes[i].functionParent]);
        }
        // 上级要求
        if (nodes[i].requireParent) {
            cleanRiskNetData([nodes[i].requireParent]);
        }
        // 下级措施状态集合
        if (nodes[i].measureChildren && nodes[i].measureChildren.length > 0) {
            cleanRiskNetData(nodes[i].measureChildren);
        }
    }
}

/**
 * 取得UUID
 */
function getUUID() {
    return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        let r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

/**
 * 展示当前措施转换按钮（0：显示，1：不显示，2：校验）
 * @param nodes 节点数组
 * @param checkNode 选中节点
 * @returns {number|*}
 */
export function showCurConvertBtn(nodes, checkNode) {
    let isValidate = 0;
    for (let i = 0; i < nodes.length; i++) {
        if (nodes[i].id === checkNode.pid) {
            isValidate = 2;
            let curNode = nodes[i].measureChildren.filter((item) => {
                return item.struTypeCode === '14' && checkNode.id !== item.id;
            })[0];
            if (curNode) return 1;
        }
        if (nodes[i].measureChildren && nodes[i].measureChildren.length > 0) {
            isValidate = showCurConvertBtn(nodes[i].measureChildren, checkNode);
        }
        if (isValidate !== 0) return isValidate;
        if (nodes[i].children && nodes[i].children.length > 0) {
            isValidate = showCurConvertBtn(nodes[i].children, checkNode);
        }
        if (isValidate !== 0) return isValidate;
    }
    return isValidate;
}

/**
 * 取得顶部结构/接口，层级
 * @param nodes 节点数组
 * @param checkNode 选中节点
 * @param topStruct 顶部结构（01）/接口（99）
 * @returns {*}
 */
export function getTopStructDeep(nodes, checkNode, topStruct) {
    let topStructDeep;
    for (let i = 0, len = nodes.length; i < len; i++) {
        if (nodes[i].structureParent) {
            topStruct = nodes[i].structureParent;
        }
        if (nodes[i].id === checkNode.id) return topStruct.deep;
        if (nodes[i].measureChildren && nodes[i].measureChildren.length > 0) {
            topStructDeep = getTopStructDeep(nodes[i].measureChildren, checkNode, topStruct);
        }
        if (topStructDeep) break;
        if (nodes[i].children && nodes[i].children.length > 0) {
            topStructDeep = getTopStructDeep(nodes[i].children, checkNode, topStruct);
        }
        if (topStructDeep) break;
    }
    return topStructDeep;
}

/**
 * 展示合并按钮（0：显示，1：不显示，2：校验）
 * @param nodes 节点数组
 * @param checkNode 选中节点
 * @returns {number|*}
 */
export function showMergeBtn(nodes, checkNode) {
    let isValidate = 0;
    for (let i = 0, len = nodes.length; i < len; i++) {
        if (nodes[i].id === checkNode.pid) {
            isValidate = 2;
            // 同级别、同类型。（09，10除外）
            if ("09，10".indexOf(checkNode.struTypeCode) === -1) {
                let eqLevelNodes = nodes[i].measureChildren.filter((item) => {
                    return item.struTypeCode === checkNode.struTypeCode;
                });
                if (eqLevelNodes.length < 2) return 1;
            }
        }
        if (nodes[i].measureChildren && nodes[i].measureChildren.length > 0) {
            isValidate = showMergeBtn(nodes[i].measureChildren, checkNode);
        }
        if (isValidate !== 0) return isValidate;
        if (nodes[i].children && nodes[i].children.length > 0) {
            isValidate = showMergeBtn(nodes[i].children, checkNode);
        }
        if (isValidate !== 0) return isValidate;
    }
    return isValidate;
}
