//电缆参数自动生成
function cableAuto (event) {
    let resistance = $('#cable-positive-resistance').val() * $('#cable-coefficient').val();
    let reactance = $('#cable-positive-reactance').val() * $('#cable-coefficient').val();
    $("#cable-zero-resistance").prop("value",changeTwoDecimal(resistance));
    $("#cable-zero-reactance").prop("value",changeTwoDecimal(reactance));
}

//发电厂参数自动生成
function powerPlantAuto (event) {
    let resistance = $('#powerPlant-positive-resistance').val() * $('#powerPlant-coefficient').val();
    let reactance = $('#powerPlant-positive-reactance').val() * $('#powerPlant-coefficient').val();
    $("#powerPlant-zero-resistance").prop("value",changeTwoDecimal(resistance));
    $("#powerPlant-zero-reactance").prop("value",changeTwoDecimal(reactance));
}

//变压器参数自动生成
function transformerAuto (event) {
    let resistance = $('#transformer-positive-resistance').val() * $('#transformer-coefficient').val();
    let reactance = $('#transformer-positive-reactance').val() * $('#transformer-coefficient').val();
    $("#transformer-zero-resistance").prop("value",changeTwoDecimal(resistance));
    $("#transformer-zero-reactance").prop("value",changeTwoDecimal(reactance));
}

$(document).on('click','#cable-zero-resistance,#cable-zero-reactance,#powerPlant-zero-resistance,#powerPlant-zero-reactance,#transformer-zero-resistance,#transformer-zero-reactance',function (obj) {
    layer.msg('自动生成，暂不支持修改', {icon: 6, time: 3000});
})

//计算保留三位小数(四舍五入)
function changeTwoDecimal(x)
{
    var f_x = parseFloat(x);
    f_x = Math.round(f_x *1000)/1000;
    return f_x;
}

//图元编辑界面
function openCellParam(cell) {
    getData(cell.id);
    if (cell.edge === 1 || cell.edge === true){
        layerIndex = layer.open({
            type: 1,
            scrollbar: false,
            title: "参数设置(电缆)",
            area: ['600px', '520px'],
            content: $('#cable'),
            btn: ['确定', '取消'],
            yes: function () {
                submitData(cell, '电缆',1);
            }
        });
    }
    else if (cell.style.indexOf('ellipse') > -1){
        if (cell.style.indexOf('substation') > -1){
            layerIndex = layer.open({
                type: 1,
                scrollbar: false,
                title: "参数设置(变电站)",
                area: ['600px', '300px'],
                content: $('#substation'),
                btn: ['确定', '取消'],
                yes: function () {
                    let cellName = $('#substation-name').val();
                    submitData(cell, cellName,3);
                }
            });
        } else {
            layerIndex = layer.open({
                type: 1,
                scrollbar: false,
                title: "参数设置(暂降敏感点)",
                area: ['600px', '320px'],
                content: $('#sensitivePoint'),
                btn: ['确定', '取消'],
                yes: function () {
                    let cellName = $('#sensitivePoint-name').val();
                    submitData(cell, cellName,2);
                }
            });
        }
    }
    else if (cell.style.indexOf('PowerPlant_128x128.png') > -1 ){
        layerIndex = layer.open({
            type: 1,
            scrollbar: false,
            title: "参数设置(发电厂)",
            area: ['600px', '520px'],
            content: $('#powerPlant'),
            btn: ['确定', '取消'],
            yes: function () {
                let cellName =  $('#powerPlant-name').val();
                submitData(cell, cellName,4);
            }
        });
    }
    else if (cell.style.indexOf('Transformer_128x128.png') > -1 ){
        layerIndex = layer.open({
            type: 1,
            scrollbar: false,
            title: "参数设置(变压器)",
            area: ['600px', '580px'],
            content: $('#transformer'),
            btn: ['确定', '取消'],
            yes: function () {
                let cellName =  $('#transformer-name').val();
                submitData(cell, cellName,5);
            }
        });
    }
}

/**
 *  查询数据
 */
function getData(cellId) {
    $.ajax({
        type : "POST",
        url : "/pqs9200/svg/getCell",
        dataType : "json",
        data:{
            cellIndex: cellId
        },
        success : function(data) {
            if (data.body !== null){
                if (data.body.cellType === 1){
                    $('#cable-length').val(data.body.cableLength);
                    $('#cell-level').val(data.body.cellLevel);
                    $('#cable-positive-resistance').val(data.body.positiveSequenceResistance);
                    $('#cable-positive-reactance').val(data.body.positiveSequenceReactance);
                    $('#cable-zero-resistance').val(data.body.zeroSequenceResistance);
                    $('#cable-zero-reactance').val(data.body.zeroSequenceReactance);
                    $('#cable-coefficient').val(data.body.coefficient);
                }
                else if (data.body.cellType === 2) {
                    $('#sensitivePoint-name').val(data.body.cellName);
                    $('#sensitivePoint-level').val(data.body.cellLevel);
                    $('#run-level').val(data.body.runLevel);
                    $('#sensitivePoint-pic').val(data.body.wiringDiagram);
                }
                else if (data.body.cellType === 3) {
                    $('#substation-name').val(data.body.cellName);
                    $('#substation-level').val(data.body.cellLevel);
                    $('#substation-run-level').val(data.body.runLevel);
                }
                else if (data.body.cellType === 4) {
                    $('#powerPlant-name').val(data.body.cellName);
                    $('#powerPlant-positive-resistance').val(data.body.positiveSequenceResistance);
                    $('#powerPlant-positive-reactance').val(data.body.positiveSequenceReactance);
                    $('#powerPlant-zero-resistance').val(data.body.zeroSequenceResistance);
                    $('#powerPlant-zero-reactance').val(data.body.zeroSequenceReactance);
                    $('#powerPlant-coefficient').val(data.body.coefficient);
                }
                else if (data.body.cellType === 5) {
                    $('#transformer-name').val(data.body.cellName);
                    $('#transformer-level').val(data.body.cellLevel);
                    $('#transformer-positive-resistance').val(data.body.positiveSequenceResistance);
                    $('#transformer-positive-reactance').val(data.body.positiveSequenceReactance);
                    $('#transformer-zero-resistance').val(data.body.zeroSequenceResistance);
                    $('#transformer-zero-reactance').val(data.body.zeroSequenceReactance);
                    $('#transformer-coefficient').val(data.body.coefficient);
                    $('#transformer-positive-admittance').val(data.body.positiveAdmittance);
                    $('#transformer-zero-admittance').val(data.body.zeroAdmittance);
                }
            } else {
                $('#cable-length').val(1);
                $('#cell-level').val(110);
                $('#cable-positive-resistance').val(0.132);
                $('#cable-positive-reactance').val(0.388);
                $('#cable-zero-resistance').val(0.33);
                $('#cable-zero-reactance').val(0.97);
                $('#cable-coefficient').val(2.5);
                $('#sensitivePoint-name').val(cellId);
                $('#sensitivePoint-level').val(110);
                $('#sensitivePoint-pic').val('/');
                $('#substation-name').val(cellId);
                $('#substation-level').val(110);
                $('#substation-run-level').val(110);
                $('#powerPlant-name').val(cellId);
                $('#powerPlant-positive-resistance').val(0.132);
                $('#powerPlant-positive-reactance').val(0.388);
                $('#powerPlant-zero-resistance').val(0.33);
                $('#powerPlant-zero-reactance').val(0.97);
                $('#powerPlant-coefficient').val(2.5);
                $('#transformer-name').val(cellId);
                $('#transformer-level').val(110);
                $('#transformer-positive-resistance').val(0.132);
                $('#transformer-positive-reactance').val(0.388);
                $('#transformer-zero-resistance').val(0.33);
                $('#transformer-zero-reactance').val(0.97);
                $('#transformer-coefficient').val(2.5);
                $('#run-level').val(110);
                $('#transformer-positive-admittance').val(0);
                $('#transformer-zero-admittance').val(0);
            }
        }
    });
}



/**
 *  提交数据
 */
function submitData(cell,cellName,cellType) {
    let cableLength,cellLevel,cpres,cprea,czres,czrea,coefficient,wiringDiagram,runLevel,tpa,tza;
    if (cellType === 1) {
        cableLength = changeTwoDecimal($('#cable-length').val());
        cellLevel = $('#cell-level').val();
        cpres = $('#cable-positive-resistance').val();
        cprea = $('#cable-positive-reactance').val();
        czres = $('#cable-zero-resistance').val();
        czrea = $('#cable-zero-reactance').val();
        coefficient = $('#cable-coefficient').val();
        wiringDiagram = '/';
        runLevel = 0;
        tpa = 0;
        tza = 0;
    }
    else if (cellType === 2) {
        cableLength = 0;
        cellLevel = $('#sensitivePoint-level').val();
        cpres = 0;
        cprea = 0;
        czres = 0;
        czrea = 0;
        coefficient = 0;
        wiringDiagram = $('#sensitivePoint-pic').val();
        runLevel = $('#run-level').val();
        tpa = 0;
        tza = 0;
    }
    else if (cellType === 3) {
        cableLength = 0;
        cellLevel = $('#substation-level').val();
        cpres = 0;
        cprea = 0;
        czres = 0;
        czrea = 0;
        coefficient = 0;
        wiringDiagram = '/';
        runLevel = $('#substation-run-level').val();
        tpa = 0;
        tza = 0;
    }
    else if (cellType === 4) {
        cableLength = 0;
        cellLevel = 0;
        cpres = $('#powerPlant-positive-resistance').val();
        cprea = $('#powerPlant-positive-reactance').val();
        czres = $('#powerPlant-zero-resistance').val();
        czrea = $('#powerPlant-zero-reactance').val();
        coefficient = $('#powerPlant-coefficient').val();
        wiringDiagram = '/';
        runLevel = 0;
        tpa = 0;
        tza = 0;
    }
    else if (cellType === 5) {
        cableLength = 0;
        cellLevel = $('#transformer-level').val();
        cpres = $('#transformer-positive-resistance').val();
        cprea = $('#transformer-positive-reactance').val();
        czres = $('#transformer-zero-resistance').val();
        czrea = $('#transformer-zero-reactance').val();
        coefficient = $('#transformer-coefficient').val();
        wiringDiagram = '/';
        runLevel = 0;
        tpa = $('#transformer-positive-admittance').val();
        tza = $('#transformer-zero-admittance').val();
    }
    if (
        cellName === '' || isNaN(cableLength) || cellLevel === '' || isNaN(cpres) || isNaN(cprea) || isNaN(czres) || isNaN(czrea) || isNaN(coefficient) || wiringDiagram === '' || isNaN(runLevel)
        || cpres === '' || cprea === '' || czres === '' || czrea === '' || coefficient === '' || runLevel === '' || isNaN(tpa) || isNaN(tza) || tpa === ''  || tza === ''  ){
        layer.msg('参数设置失败，参数不能为空', {icon: 5, time: 3000});
    } else {
        $.ajax({
            type: 'post',
            url: '/pqs9200/svg/saveCell',
            async:false,
            data: JSON.stringify({
                cellIndex: cell.id,
                cellName: cellName,
                cableLength: cableLength,
                cellLevel: cellLevel,
                positiveSequenceResistance: cpres,
                positiveSequenceReactance: cprea,
                zeroSequenceResistance: czres,
                zeroSequenceReactance: czrea,
                coefficient: coefficient,
                wiringDiagram: wiringDiagram,
                cellType: cellType,
                runLevel: runLevel,
                positiveAdmittance: tpa,
                zeroAdmittance: tza,
                state: 1
            }),
            dataType: 'json',
            contentType: "application/json;charset=UTF-8",
            success: function (data) {
                if (data.code === 200){
                    layer.msg('参数设置成功', {icon: 6, time: 3000});
                    layer.close(layerIndex);
                    let graph = editor.graph;
                    graph.refresh(cell);
                } else {
                    layer.msg('参数设置失败', {icon: 5, time: 3000});
                }
            }
        })
    }
}

//计算功能
function calculateValue(cell) {
    $('#dip').val(70);
    $("#model option[value='0']").prop("selected","selected");
    if (cell.style.indexOf('ellipse') > -1 && cell.style.indexOf('substation') <= -1){
        layerIndex = layer.open({
            type: 1,
            scrollbar: false,
            title: "计算参数设置",
            area: ['500px', '300px'],
            content: $('#calculate'),
            btn: ['确定', '取消'],
            yes: function () {
                /**
                 * 当前图元id ---> cell.id
                 * 暂降幅值   ---> $('#dip').val()
                 * 所选类型   ---> $('#model').val()
                 */
                if ($('#dip').val() > 100 || $('#dip').val() == '' || $('#dip').val() < 0) {
                    //$('#dip').val(70);
                    layer.msg('参数错误，暂降幅值的范围为0-100', {icon: 5, time: 3000});
                } else {
                    let uuid = generateUUID();
                    //先进行算法 改变线路颜色
                    calculateParams(uuid,cell.id,$('#dip').val(),$('#model').val());
                }
            }
        });
    } else {
        layer.msg('仅暂降敏感点可计算', {icon: 5, time: 3000});
    }
}


//展示一次接线图
function showDiagram(cell) {
    if (cell.style.indexOf('ellipse') > -1 && cell.style.indexOf('substation') <= -1){
        $.ajax({
            type : "POST",
            url : "/pqs9200/svg/getCell",
            dataType : "json",
            data:{
                cellIndex: cell.id
            },
            success : function(data) {
                if (data.body.wiringDiagram !== null && data.body.wiringDiagram !== '/'){
                    let title = data.body.wiringDiagram.split('.')[0];
                    parent.layer.open({
                        type: 2,
                        title: title,
                        area: ['100%', '100%'],
                        content: 'sagDomainDiagram?svgName=' + data.body.wiringDiagram,
                        scrollbar: false
                    });
                } else {
                    layer.msg('暂无一次接线图，请先绑定', {icon: 5, time: 3000});
                }
            }
        });
    } else {
        layer.msg('仅暂降敏感点可展示接线图', {icon: 5, time: 3000});
    }
}

//获取画布中的所有Cells
function getCells() {
    let graph = editor.graph;
    let cells = graph.getModel().cells;
    return cells;
}


//获取不同图元的id,批量入库
function getEachCellArr() {
    let arr = getCells();
    let lineArr = [], sensitiveArr = [], substationArr = [],powerArr = [],transformerArr = [];
    if (arr !== null) {
        for(let pojo in arr){
            if (arr[pojo].style !== undefined){
                if (arr[pojo].edge === 1){
                    lineArr.push(arr[pojo].id);
                }
                else if (arr[pojo].style.indexOf('ellipse') > -1){
                    if (arr[pojo].style.indexOf('substation') > -1){
                        substationArr.push(arr[pojo].id);
                    } else {
                        sensitiveArr.push(arr[pojo].id);
                    }
                }
                else if (arr[pojo].style.indexOf('PowerPlant_128x128.png') > -1 ){
                    powerArr.push(arr[pojo].id);
                }
                else if (arr[pojo].style.indexOf('Transformer_128x128.png') > -1 ){
                    transformerArr.push(arr[pojo].id);
                }
            }
        }
    }
    $.ajax({
        type: 'post',
        url: '/pqs9200/svg/saveAllCells',
        data: JSON.stringify({
            lineArr: lineArr,
            sensitiveArr: sensitiveArr,
            substationArr: substationArr,
            powerArr: powerArr,
            transformerArr: transformerArr
        }),
        dataType: 'json',
        contentType: "application/json;charset=UTF-8",
        success: function (data) {

        }
    })
}

//计算功能
function calculateParams(uuid,id,dip,model) {
    if(model > 0){
        analysisGraph(uuid,id,dip,model);
    }
}

//解析图元信息
let mxCells_vertex = [];//存放顶点元素;
/***********************************
 *
 * @param sensitiveSpotId 敏感点计算元素Id
 ***********************************/
let mxCells_element;//图形中所有顶点信息
let mxCells_line;//图形中所有线路信息
let myGraph;//最短路径计算算法
let neightborsMap;//相连节点信息
let nodesMap;//存放所有参与计算的节点信息
let nodesCount;//所有节点的序号，映射，转义后的
let lines_substation;//变电站线路
let lines_transformer;//变压器线路
function analysisGraph(uuid,sensitiveSpotId,dip,model){
    //获取所有的元素信息
    let cells = getCells();
    let baseDrawSvg =new BaseDrawSvg(1);
    //获取顶点元素信息
    mxCells_element = baseDrawSvg.getCells_element(cells);
    //获取线条信息
    mxCells_line = baseDrawSvg.getCells_Line(cells);
    let element_id=[];
    //初始化顶点
    for (let i = 0; i < mxCells_element.length; i++) {
        element_id.push(mxCells_element[i].id)
    }
    // 初始化顶点矩阵
    myGraph = new ShortestGraph(1);// 1表示有向图
    myGraph.initVertex(element_id);
    //初始化线条矩阵，组装成线条矩阵
    for (let i = 0; i < mxCells_line.length; i++) {
        //排除没有源或者没有目标得线条
        if (mxCells_line[i].source != null && mxCells_line[i].target != null) {
            //排除没有源或者没有目标得线条
            myGraph.addEdge(mxCells_line[i].source.id, mxCells_line[i].target.id, 1);
        }
    }
    //初始化值
    nodesMap = new Map();
    nodesCount = 0;
    neightborsMap = new Map();
    lines_substation = [];//变电站线路
    lines_transformer = [];//变压器线路

    //开始进行计算
    recursion(sensitiveSpotId);

    //进行暂降域算法
    $.ajax({
        type: 'post',
        url: '/pqs9200/svg/getSagDomain',
        data: JSON.stringify({
            nodesMap: mapToObj(nodesMap),
            lines_substation: lines_substation,
            lines_transformer: lines_transformer,
            eventPoint:sensitiveSpotId,
            dip: dip,
            eventType: model
        }),
        dataType: 'json',
        contentType: "application/json;charset=UTF-8",
        success: function (data) {
            if(data.body != null){
                let graph = editor.graph;
                let resultData = data.body;
                let dipZoneModel;
                let judgeType;
                for (let i = 0;i < resultData.length;i++ ){
                    dipZoneModel = resultData[i];
                    let lineCell =getLineCell(dipZoneModel.head + "", dipZoneModel.tail + "");
                    judgeType = dipZoneModel.judgeType;
                    if (lineCell != undefined){
                        switch (judgeType){
                            case "SAG_NONE":
                                //黑色
                                changeLineColor(lineCell,'black');
                                break;
                            case "SAG_SIGLE":
                                //黄色（一段暂降）
                                changeLineColor(lineCell,'yellow');
                                lineCell.value ='(影响范围：'+dipZoneModel.nodeLength1+'km)';
                                graph.refresh(lineCell);
                                break;
                            case "SAG_MUL":
                                //黄色（二段暂降）
                                changeLineColor(lineCell,'yellow');
                                break;
                            case "SAG_ALL":
                                //红色
                                changeLineColor(lineCell,'red');
                                break;
                        }
                    }
                }
            }
            // 保存暂降敏感点发生事件
            let time = getFormatDate();
            $.ajax({
                type: 'post',
                url: '/pqs9200/svg/saveSensitivePoint',
                data: JSON.stringify({
                    uuidIndex: uuid,
                    cellIndex: sensitiveSpotId,
                    amplitude: $('#dip').val(),
                    descentType: $('#model').val(),
                    time: time
                }),
                dataType: 'json',
                contentType: "application/json;charset=UTF-8",
                success: function (data) {
                    if (data.code === 200){
                        //再保存当前画布
                        editorUI.save(uuid);
                        layer.close(layerIndex);
                    } else {
                        layer.msg('计算失败', {icon: 5, time: 3000});
                    }
                }
            })
        }
    })
}

//修改线路颜色
function changeLineColor(cell,color) {
    let graph = editor.graph;
    let style=graph.getModel().getStyle(cell);
    let newStyle=mxUtils.setStyle(style,mxConstants.STYLE_STROKECOLOR,color);
    let cs= new Array();
    cs[0] = cell;
    graph.setCellStyle(newStyle,cs);
}

/***********************************
 * 递归算法获取所有的路径信息
 * @param id 获取顶点的Id
 * @returns yexb
 ***********************************/
function recursion(id) {
    //获取首个节点
    let allNeightbors = myGraph.allNeightbors(id);
    //根据距离重新排序
    let tmpAllNeightbors;
    let lineMap = new Map();
    if(allNeightbors != null) {
        //用‘->’去分割所有数据
        let split_Node = allNeightbors.split('=>');
        let startVertexType =getVertexType(id);
        //遍历所有的节点
        for(let i = 0;i < split_Node.length;i++){
            //节点信息
            let vertexId = split_Node[i];
            //节点类型
            let endVertexType = getVertexType(vertexId);
            //自己和自己无需形成矩阵
            if(i >= 1){
                //节点类型为变电站时候
                if(startVertexType === 1){
                    if(endVertexType === 1){
                        let lineCell = getLineCell(id, vertexId);
                        lineMap.set(vertexId,getLineLength(lineCell.id))
                    }
                    else{
                        if(!lineMap.has(vertexId)){
                            lineMap.set(vertexId,0)
                        }
                    }
                }
                else{
                    if(!lineMap.has(vertexId)){
                        lineMap.set(vertexId,0)
                    }
                }
            }
        }
    }
    //排序并重新组装路径
    let arrayObj = Array.from(lineMap);
    arrayObj.sort(function(a,b){return a[1]-b[1]})
    tmpAllNeightbors = id;
    for(let i = 0;i < arrayObj.length;i++){
        tmpAllNeightbors += '=>';
        tmpAllNeightbors += arrayObj[i][0];
    }

    //节点不为空判断
    if(tmpAllNeightbors != null){
        //用‘->’去分割所有数据
        let split_Node =tmpAllNeightbors.split('=>');
        let startVertexType = getVertexType(id);//起始顶点类型
        let endVertexType;//结束顶点类型
        let vertexId;//顶点Id
        //遍历所有的节点
        for(let i = 0;i < split_Node.length;i++){
            //节点信息
            vertexId = split_Node[i];
            //节点类型
            endVertexType = getVertexType(vertexId);
            //节点类型为变电站时候
            if(endVertexType === 1){
                // 变电站则加入顶点节点
                if(!nodesMap.has(vertexId)){
                    nodesCount = nodesCount + 1;
                    nodesMap.set(vertexId,nodesCount);
                }
            }

            //第一个点为每次的计算起始点
            if(i === 0){
                //计算过的则为false，不需要递归（主要是第一个点）
                neightborsMap.set(vertexId,false);
                if(startVertexType === 2){
                    // 变电站则加入顶点节点
                    getLineInfo(vertexId);
                }
            }
            //自己和自己无需形成矩阵
            else if(i >= 1){
                //没计算过的则为true，需要递归
                if(!neightborsMap.has(vertexId)){
                    neightborsMap.set(vertexId,true);
                }
                //节点类型为变电站时候
                if(startVertexType === 1){
                    if(endVertexType === 1){
                        // 变电站则加入顶点节点
                        /********************************
                         * 需要排除掉回头的点
                         ********************************/
                        if(judgeArrayHas(lines_substation,id,vertexId) === -1){
                            //字符串中& 1、前面的为起始点和结束点；2、后面为线路Id
                            let lineCell =getLineCell(id, vertexId);
                            lines_substation.push(id + "->" + vertexId + "&" + lineCell.id);
                        }
                    }
                }
            }
        }
    }

    // 递归遍历下一个元素值
    if(getMapLength(neightborsMap) > 0){
        recursion(getFirstKey(neightborsMap))
    }
}

/***********************************
 * 获取顶点信息类型
 * @param id 获取顶点的Id
 * @returns number
 ***********************************/
function getVertexType(id){
    let type = 1;//变电站
    for(let i = 0;i < mxCells_element.length;i++){
      let mxCell = mxCells_element[i];
      //节点Id相等
      if(mxCell.id === id){
          if (mxCell.style.indexOf('PowerPlant_128x128.png') > -1 ){
              type = 0;//发电厂
          }
          else if (mxCell.style.indexOf('Transformer_128x128.png') > -1 ){
              type = 2;//变压器
          }
          break;
       }
    }
    return type;
}

/***********************************
 * 获取顶点信息类型
 * @param id 获取顶点的Id
 * @returns number
 ***********************************/
function getLineInfo(id){
    let startId = null;
    let endId = null;
    for(let i = 0;i < mxCells_line.length;i++){
        let mxCell = mxCells_line[i];
        if (mxCell.source != null && mxCell.target != null) {
            if(mxCell.target.id === id){
                startId = mxCell.source.id;
            }
            else if(mxCell.source.id === id){
                endId  = mxCell.target.id;
            }
        }
        //假如起始节点和结束节点都找到则跳出
        if(startId != null && endId != null){
            //字符串中& 1、前面的为起始点和结束点；2、后面为变压器Id
            lines_transformer.push(startId + "->" + endId  + "&" + id);
            break;
        }
    }
}

/***********************************
 * 获取起始点->结束点中间的线路
 * @param startId 起始点Id
 * @param endId 结束点Id
 * @returns yxb
 ***********************************/
function getLineCell(startId,endId){
    let lineCell;
    for(let i = 0;i < mxCells_line.length;i++){
        let mxCell = mxCells_line[i];
        if (mxCell.source != null && mxCell.target != null) {
            if(mxCell.source.id === startId && mxCell.target.id === endId){
                lineCell = mxCell;
                break;
            }
        }
    }
    return lineCell;
}

/***********************************
 * 获取map中第一个Key值
 * @param myMap map
 * @returns yexb
 ***********************************/
function getFirstKey(myMap){
    let key = null;//用来记录第一个Key的值
    if(myMap != null){
      for(let tmpKey of myMap.keys()){
         if(myMap.get(tmpKey) ===  true) {
            key = tmpKey;
            break;
        }
      }
    }
    return key;
}

/***********************************
 * 获取map 未执行的长度
 * @param myMap map
 * @returns number
 ***********************************/
function getMapLength(myMap){
    let count = 0;
    if(myMap != null){
        for(let tmpKey of myMap.keys())
        {
            if(myMap.get(tmpKey) ===  true) {
                count++;
            }
        }
    }

    return count;
}

/***********************************
 * 判断一个集合中是否包含了起始点---结束点的反向
 * @param startId 起始点Id
 * @param endId 结束点Id
 * @returns number
 ***********************************/
function judgeArrayHas(array,startId,endId){
    let result = -1;//不包含
    let tmpData = endId + "->" + startId;
    for(let i = 0;i < array.length; i++){
        if(array[i].indexOf(tmpData) > -1){
            result = 1;
            break;
        }
    }
    return  result;
}

/***********************************
 * 把map转成obj
 * @param strMap 需要转换得map
 * @returns number
 ***********************************/
function mapToObj(strMap){
    let obj= Object.create(null);
    for (let[k,v] of strMap) {
        obj[k] = v;
    }
    return obj;
}

function generateUUID() {
    var d = new Date().getTime();
    if (window.performance && typeof window.performance.now === "function") {
        d += performance.now(); //use high-precision timer if available
    }
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
}

/***********************************
 * 获取线路的长度信息
 * @param Id 线路Id
 * @returns number
 ***********************************/
function getLineLength(lineId){
    let lineLength = 0;
    //进行暂降域算法
    $.ajax({
        type : "POST",
        url : "/pqs9200/svg/getCell",
        dataType : "json",
        // async: false,
        data:{
            cellIndex: lineId
        },
        success : function(data) {
            if (data.body !== null){
                lineLength = data.body.cableLength;
            }
        }
    });
    return lineLength;
}
//获取所有图元信息的ajax
// $.ajax({
//     type: 'post',
//     url: '/pqs9200/svg/getAllCells',
//     dataType: 'json',
//     contentType: "application/json;charset=UTF-8",
//     success: function (data) {
//         console.log(data)
//     }
// })