/**
 * Created by CSL on 2017-06-06.
 * public functions for web.
 */

// 忽略大小写判断字符串是否和参数指定的字符串相同
String.prototype.sameText = function (str) {
    return this.toLowerCase() == str.toLowerCase();
};

// 忽略大小写判断字符串是否有参数指定的子串
String.prototype.hasSubStr = function (str) {
    return this.toLowerCase().indexOf(str.toLowerCase()) > -1;
};

// 判断字符串是否是数字形式的字符串
String.prototype.isNumberStr = function () {
    return this == +this;
};

// 树结点计算时，取费会出现值为NaN的情况，导致往父节点汇总(递归相加)会出现错误。
function parseFloatPlus(value){
    let rst = parseFloat(value);
    return  isNaN(rst) ? 0 : rst;
};

// 数组合并，并去重复。
Array.prototype.merge = function (arr) {
    if (arr.length > 0){
        for (let e of arr){
            if (!this.includes(e)) this.push(e);
        };
    }
};

// 数组是否包含另一个数组。
Array.prototype.hasSubArr = function (subArr){
    for(var i = 0, len = subArr.length; i < len; i++){
        if(this.indexOf(subArr[i]) == -1) return false;
    }
    return true;
};

Array.prototype.delete = function (elem){
    let idx = this.findIndex(function (e){return e == elem});
    if (idx != -1) this.splice(idx, 1);
};

function seqString(num,length){
    var numstr = num.toString();
    var l=numstr.length;
    if (numstr.length>=length) {return numstr;}

    for(var  i = 0 ;i<length - l;i++){
        numstr = "0" + numstr;
    }
    return numstr;
};

function customRowHeader(sheet, dataLength) {
    sheet.suspendPaint();   //提升焦点变换性能 2019年4月12日
    for (let i = 0; i < dataLength; i++) {
        sheet.setValue(i, 0, `F${i + 1}`, GC.Spread.Sheets.SheetArea.rowHeader);
    }
    sheet.resumePaint();    //提升焦点变换性能 2019年4月12日
};

function changePropNames(object, oldNames, newNames) {
    if (!object) return;
    for (let i = 0; i < oldNames.length; i++) {
        if (object[oldNames[i]]){
            object[newNames[i]] = object[oldNames[i]];
            delete object[oldNames[i]];
        };
    }
};

function changePropNames(object, oldNames, newNames) {
    if (!object) return;
    for (let i = 0; i < oldNames.length; i++) {
        if (object[oldNames[i]]){
            object[newNames[i]] = object[oldNames[i]];
            delete object[oldNames[i]];
        };
    }
};

function deletePropNames(object, namesArr) {
    if (!object) return;
    for (let name of namesArr){
        if (object[name]) delete object[name];
    };
};


function sortTreeChildren(lists) {//树结构排序
    let IDMap ={},nextMap = {}, firstNode = null,newList=[];
    for(let l of lists){
        if(l.children&&l.children.length > 0) l.children = sortTreeChildren(l.children);//递规排序
        IDMap[l.ID] = l;
        if(l.NextSiblingID!=-1) nextMap[l.NextSiblingID] = l;
    }
    for(let t of lists){
        if(!nextMap[t.ID]){ //如果在下一节点映射没找到，则是第一个节点
            firstNode = t;
            break;
        }
    }
    if(firstNode){
        newList.push(firstNode);
        delete IDMap[firstNode.ID];
        setNext(firstNode,newList);
    }
    //容错处理，如果链断了的情况，直接添加到后面
    for(let key in IDMap){
        if(IDMap[key]) newList.push(IDMap[key])
    }
    return newList;

    function setNext(node,array) {
        if(node.NextSiblingID != -1){
            let next = IDMap[node.NextSiblingID];
            if(next){
                array.push(next);
                delete IDMap[next.ID];
                setNext(next,array);
            }
        }
    }
} 

function setTreeChildren(children,list,parentMap){//按顺序设置树节点
    for(let c of children){
      list.push(c);
      if(parentMap[c.ID]){
        setTreeChildren(parentMap[c.ID],list,parentMap)
      }
    }
  }