/**
 * Created by Zhong on 2017/11/28.
 */


let cbTools = {
    isDef: function (v) {
        return v !== undefined && v !== null;
    },
    isUnDef: function (v) {
        return v === undefined || v === null;
    },
    isNum: function (v) {
        return this.isDef(v) && !isNaN(v) && v !== Infinity;
    },
    isFlag: function (v) {
        return this.isDef(v) && this.isDef(v.flagsIndex) && this.isDef(v.flagsIndex.fixed) && this.isDef(v.flagsIndex.fixed.flag);
    },
    returnV: function (v, r) {
        if(this.isDef(v)){
            return v;
        }
        return r;
    },
    findBill: function (fixedFlag) {
        return this.isDef(calcBase.fixedBills[fixedFlag]) ? calcBase.fixedBills[fixedFlag]['bill'] : null;
    },
    //通过行获取根节点清单
    getBillByRow: function (items, row) {
        if(cbTools.isDef(items[row]) &&
            cbTools.isDef(items[row]['sourceType']) &&
            items[row]['sourceType'] === calcBase.project.Bills.getSourceType()){
            return items[row];
        }
        return null;
    },
    /*getBillByRow: function (items, row) {
        if(cbTools.isDef(items[row]) &&
            cbTools.isUnDef(items[row]['parent'])&&
            cbTools.isDef(items[row]['sourceType']) &&
            items[row]['sourceType'] === calcBase.project.Bills.getSourceType()){
            return items[row];
        }
        return null;
    },*/
    //通过ID获取节点行
    getRowByID: function (items, ID) {
        for(let i = 0, len = items.length; i < len; i++){
            if(items[i]['data']['ID'] == ID){
                return i + 1;
            }
        }
        return null;
    },
    //通过ID获取节点
    getNodeByID: function (ID) {
        return this.isDef(calcBase.project.mainTree.nodes['id_' + ID]) ? calcBase.project.mainTree.nodes['id_' + ID] : null;
    },
    //获取该节点所有父节点
    getParents: function (node) {
        let rst = [];
        rst.push(node);
        rParent(node);
        return rst;
        function rParent(node){
            if(cbTools.isDef(node.parent)){
                rst.push(node.parent);
                rParent(node.parent);
            }
        }
    },
    /*getParents: function (node) {
        let rst = [];
        rParent(node);
        return rst;
        function rParent(node){
            if(cbTools.isDef(node.parent)){
                rst.push(node.parent);
                rParent(node.parent);
            }
        }
    },*/
    //获取所有节点的ID
    getNodeIDs: function (nodes) {
        let rst = [];
        for(let i = 0, len = nodes.length; i < len; i++){
            if(this.isDef(nodes[i]['data']['ID'])){
                rst.push(nodes[i]['data']['ID']);
            }
        }
        return rst;
    },
    //根据公式获取相关的节点
    getNodesByExp: function (node, formulaNodesArr) {
        let exp = node.data.calcBase;
        let rst = [], ids = [];
        if(this.isUnDef(exp) || exp === ''){
            return rst;
        }
        let findChildNodes = [];//直接引用的节点，这些节点可能存在子节点，子节点才有公式，因此获取这些节点的子公式节点
        //获取表达式中的基数和行引用
        let figureF = cbParser.getFigureF(cbParser.getFigure(exp), cbParser.getUID(cbParser.getFIDArr(exp)));
        //首先提取出多处引用的进行排序
        for(let i = 0, len = figureF.length; i < len; i++){
            let figure = figureF[i];
            if(figure.type === 'base' && cbTools.isDef(calcBase.baseFigures[figure.value])){
                let bill = this.isDef(calcBase.baseFigures[figure.value]['fixedBill']) ? calcBase.baseFigures[figure.value]['fixedBill']['bill'] : null;
                let figureMultiRef = calcBase.baseFigures[figure.value]['multiRef'];
                if(this.isDef(figureMultiRef)){
                    for(let flag of figureMultiRef){
                        let refNode = this.findBill(flag) ? this.getNodeByID(this.findBill(flag).ID) : null;
                        if(refNode && !ids.includes(refNode.data.ID)){
                            findChildNodes.push(refNode);
                            ids.push(refNode.data.ID);
                        }
                    }
                } else if(this.isDef(bill) && ids.indexOf(bill.ID) === -1){
                    let node = this.getNodeByID(bill.ID);
                    if(this.isDef(node) && !ids.includes(node.data.ID)){
                        findChildNodes.push(node);
                        ids.push(node.data.ID);
                    }
                }
            } else if(figure.type === 'id'){
                let node = this.getNodeByID(figure.value);
                if (this.isDef(node) && !ids.includes(node.data.ID)) {
                    findChildNodes.push(node);
                    ids.push(node.data.ID);
                }
            }
        }
        if (findChildNodes.length > 0) {
            let childrenNodes = calcTools.getChildrenFormulaNodes(node, formulaNodesArr, findChildNodes);
            rst = rst.concat(childrenNodes);
        }
        return rst;
    },
    //需要用到计算基数的时候，先找出所有的固定清单，避免每个基数都要去遍历寻找清单
    setFixedBills: function (project, billsObj, fixedFlag) {
        let bills = project.Bills.datas;
        for(let i = 0, len = bills.length; i < len; i++){
            if(this.isDef(bills[i].flagsIndex.fixed)){
                for(let flag in fixedFlag){
                    if(fixedFlag[flag] === bills[i].flagsIndex.fixed.flag){
                        billsObj[fixedFlag[flag]] = Object.create(null);
                        billsObj[fixedFlag[flag]]['base']  = Object.create(null);
                        billsObj[fixedFlag[flag]]['bill']  = bills[i];
                    }
                }
            }
        }
    },
    //清单基数设置所属固定清单属性
    setBaseBills: function (baseFigure, fixedBills) {
        for(let i in baseFigure){
            let calcBase = baseFigure[i];
            calcBase.fixedBill = null;
            if(cbTools.isDef(calcBase.fixedFlag) && cbTools.isDef(fixedBills[calcBase.fixedFlag])){
                fixedBills[calcBase.fixedFlag]['base'][i] = calcBase;
                calcBase.fixedBill = fixedBills[calcBase.fixedFlag];
            }
        }
    },
    //生成清单基数计算分类模板
    setBaseFigureClass: function (baseFigures, mapObj) {
        for(let figureClass in figureClassTemplate){
            mapObj[figureClass] = Object.create(null);
        }
        let needFixedBillsClass = ['FBFX', 'CXSM', 'QTXM', 'GF', 'SJ'];
        //不需要关联节点的、但是下挂在固定清单分类下的基数
        let noneFixedBillsFigures = ['JZMJ'];
        //安全文明施工专项费用只有税金和工程造价能用
        for(let figure in baseFigures){
            if(!noneFixedBillsFigures.includes(baseFigures[figure]['base'])){
                //过滤相关清单固定行不存在的
                if(needFixedBillsClass.includes(baseFigures[figure]['class']) && !baseFigures[figure]['fixedBill']){
                    continue;
                }
            }
            for(let figureClass in figureClassTemplate){
                let figureClassFilter = figureClassTemplate[figureClass]['filter'];
                if(!figureClassFilter.includes(baseFigures[figure]['base'])){
                    mapObj[figureClass][figure] = baseFigures[figure];
                }
            }
        }

    },
    getFigure: function (node) {
        let calcBase = projectObj.project.calcBase;
        let parent = node.parent;
        if(this.isFlag(node.data) && (node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.SUB_ENGINERRING
            || node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.CONSTRUCTION_TECH)){
            return null;
        }
        if(this.isFlag(node.data)){
            for(let figureClass in figureClassTemplate){
                let figureClassFlag = figureClassTemplate[figureClass]['flag'];
                if(figureClassFlag && node.data.flagsIndex.fixed.flag === figureClassFlag){
                    return calcBase.baseFigureClass[figureClass]
                }
            }
        }
        if(!parent){
            return calcBase.baseFigureClass.OTHERS;
        }
        else {
            return this.getFigure(parent);
        }
    },
    getBaseBill: function (node) {
        let calcBase = projectObj.project.calcBase;
        let parent = node.parent;
        if(this.isFlag(node.data) && (node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.SUB_ENGINERRING
            || node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.CONSTRUCTION_TECH)){
            return node;
        }
        else if(this.isFlag(node.data) && node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.CONSTRUCTION_ORGANIZATION){
            return node;
        }
        else if(this.isFlag(node.data) && node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.OTHER){
            return node;
        }
        else if(this.isFlag(node.data) && node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.CHARGE){
            return node;
        }
        else if(this.isFlag(node.data) && node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.TAX){
            return node;
        }
        else {
            if(!parent){
                return node;
            }
            else {
                return this.getBaseBill(parent);
            }
        }
    },
    //获取清单（有基数计算）引用了的其他清单,（循环引用栈中的一块）
    getStackBlock: function (billID) {
        let tempBases = [], block = [];//存引用的清单ID
        let node = getBill(billID);
        if(!node){
            return tempBases;
        }
        else {
            //获取基数和行引用
            getBase(node);
            let bases = Array.from(new Set(tempBases));
            //根据基数和行引用获取清单ID
            for(let i = 0, len = bases.length; i < len; i++){
                //基数是跟清单直接关联的
                if(bases[i]['type'] === 'base' && cbTools.isDef(calcBase.baseFigures[bases[i]['value']])){
                    let figureMultiRef= calcBase.baseFigures[bases[i]['value']]['multiRef'];
                    let cycleCalcRef = calcBase.baseFigures[bases[i]['value']]['cycleCalcRef'];
                    //重构后：
                    if(cbTools.isDef(figureMultiRef)){
                        if(cbTools.isDef(cycleCalcRef)){
                            figureMultiRef = cycleCalcRef;
                        }
                        for(let flag of figureMultiRef){
                            let bills = cbTools.findBill(flag);
                            if(cbTools.isDef(bills)){
                                block.push(bills.ID);
                            }
                        }
                    }
                    else if(cbTools.isDef(calcBase.baseFigures[bases[i]['value']]['fixedBill'])){
                        block.push(calcBase.baseFigures[bases[i]['value']]['fixedBill']['bill']['ID']);
                    }
                }
                else if(bases[i]['type'] === 'id'){
                    let node = cbTools.getNodeByID(bases[i]['value']);
                    if(cbTools.isDef(node)){
                        block.push(node.data.ID);
                    }
                }
            }
            return Array.from(new Set(block));
        }
        function getBase(node){
            if(node && node.children.length === 0){
                if(cbTools.isDef(node.data.calcBase) && node.data.calcBase !== ''){
                    let figureF = cbParser.getFigureF(cbParser.getFigure(node.data.calcBase), cbParser.getUID(cbParser.getFIDArr(node.data.calcBase)));
                    tempBases = tempBases.concat(figureF);
                }
            }
            else if(node && node.children.length > 0) {
                for(let i = 0, len = node.children.length; i < len; i++){
                    getBase(node.children[i]);
                }
            }
        }
        function getBill(ID){
            let nodes = calcBase.project.mainTree.nodes;
            let node = nodes['id_' + ID];
            if(cbTools.isDef(node) && node.sourceType === calcBase.project.Bills.getSourceType()){
                return node;
            }
            return null;
        }
    },

    // 获取全部有公式的树节点清单。 CSL, 2018-01-05
    getFormulaNodes: function (needOrder = false) {
        // 给公式结点清单换照引用计算顺序排序。
        function orderFormulaNodes (nodesArr) {
            let orderArr = [];
            function recursionNode(nodes) {
                for (let node of nodes){
                    if (orderArr.includes(node)) continue;    // 已排过序的节点则跳过
                    if (node.data.calcBase){
                        let subNodes = cbTools.getNodesByExp(node, nodesArr);
                        recursionNode(subNodes);
                    };
                    if (nodesArr.includes(node) && !orderArr.includes(node)) orderArr.push(node);
                };
            }
            recursionNode(nodesArr);
            return orderArr;
        };

        let nodes = [];
        for (let node of projectObj.project.mainTree.items){
            if (node.sourceType == ModuleNames.bills && node.data.calcBase && node.data.calcBase != '')
                nodes.push(node);
        };
        if (needOrder && nodes.length >= 2) return orderFormulaNodes(nodes)
        else return nodes;
    },
    // 刷新全部行引用的公式清单。 CSL, 2018-01-05
    refreshFormulaNodes: function () {
        try {
            let nodes = this.getFormulaNodes();
            if (nodes.length > 0) projectObj.mainController.refreshTreeNode(nodes,false,false);
        } catch (err) {
            alert('公式引用行号显示刷新失败：' + err.message);
        }
    },
    // 判断结点是否被其它结点的表达式引用。
    isUsedByFormula: function(node){
        let nodes = this.getFormulaNodes();
        if (nodes.length == 0) return false;
        let sID = '@' + node.data.ID;
        for (let node of nodes){
             if (node.data.calcBase.hasSubStr(sID)) return true;
        };
    },
    //获取清单节点的金额
    //@param {Number}fixedFlag(清单固定行类别) {String}feeField(外层金额字段: common) {String}subFeeField(子金额字段: totalFee)
    //@return {Number}
    getBillsFee: function(fixedFlag, feeField, subFeeField) {
        //固定清单类别与清单数据、关联基数的映射
        let fixedBills = calcBase.fixedBills[fixedFlag];
        if (this.isUnDef(fixedBills)) {
            return 0;
        }
        let bills = fixedBills.bill;
        if (this.isUnDef(bills)) {
            return 0;
        }
        if (this.isUnDef(bills.feesIndex) || _.isEmpty(bills.feesIndex)) {
            return 0;
        }
        return this.isDef(bills.feesIndex[feeField]) && this.isDef(bills.feesIndex[feeField][subFeeField]) ? bills.feesIndex[feeField][subFeeField] : 0;
    },
};

let baseFigureTemplate = {
    'FBFXGCF': function (tender) {
        let feeField = 'common',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.SUB_ENGINERRING, feeField, subFeeField);
    },
    'FBFXDEJJRGF': function (tender) {
        let feeField = 'labour',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.SUB_ENGINERRING, feeField, subFeeField);
    },
    'FBFXDEJJCLF': function (tender) {
        let feeField = 'material',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.SUB_ENGINERRING, feeField, subFeeField);
    },
    'FBFXDEJJJXF': function (tender) {
        let feeField = 'machine',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.SUB_ENGINERRING, feeField, subFeeField);
    },
    'FBFXTZRGF': function (tender) {
        let feeField = 'adjustLabour',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.SUB_ENGINERRING, feeField, subFeeField);
    },
    'FBFXTZJSRGF': function (tender) {
        let feeField = 'adjustMachineLabour',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.SUB_ENGINERRING, feeField, subFeeField);
    },
    'FBFXZCF': function (tender) {
        let feeField = 'mainMaterial',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.SUB_ENGINERRING, feeField, subFeeField);
    },
    'FBFXSBF': function (tender) {
        let feeField = 'equipment',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.SUB_ENGINERRING, feeField, subFeeField);
    },
    'FBFXWJJCLF': function (tender) {
        return (this['FBFXZCF'](tender) + this['FBFXSBF'](tender)).toDecimal(decimalObj.bills.totalPrice);
    },
    //统计分部分项下工料机类型为“人工”的总消耗量
    'FBFXRGGR': function (tender) {
        const subQuantityType = tender ? 'tenderSubdivisionQuantity' : 'subdivisionQuantity';
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        let rst = 0;
        for(let glj of projGljs){
            if(glj.type === gljType.LABOUR){
                if(cbTools.isDef(glj[subQuantityType])){
                    rst = parseFloat(rst + glj[subQuantityType]).toDecimal(decimalObj.glj.quantity);
                }
            }
        }
        return rst;
    },
    'FBFXDEJJZJGCF': function (tender) {
        return (this['FBFXDEJJRGF'](tender) + this['FBFXDEJJCLF'](tender) + this['FBFXDEJJJXF'](tender)).toDecimal(decimalObj.bills.totalPrice);
    },
    'CSXMF': function (tender) {
        let feeField = 'common',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.MEASURE, feeField, subFeeField);
    },
    'ZZCSXMF': function (tender) {
        let feeField = 'common',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CONSTRUCTION_ORGANIZATION, feeField, subFeeField);
    },
    'ZZCSXMDEJJZJGCF': function (tender) {
        return (this['ZZCSXMDEJJRGF'](tender) + this['ZZCSXMDEJJCLF'](tender) + this['ZZCSXMDEJJJXF'](tender)).toDecimal(decimalObj.bills.totalPrice);
    },
    'ZZCSXMDEJJRGF': function (tender) {
        let feeField = 'labour',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CONSTRUCTION_ORGANIZATION, feeField, subFeeField);
    },
    'ZZCSXMDEJJCLF': function (tender) {
        let feeField = 'material',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CONSTRUCTION_ORGANIZATION, feeField, subFeeField);
    },
    'ZZCSXMDEJJJXF': function (tender) {
        let feeField = 'machine',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CONSTRUCTION_ORGANIZATION, feeField, subFeeField);
    },
    'JSCSXMF': function (tender) {
        let feeField = 'common',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CONSTRUCTION_TECH, feeField, subFeeField);
    },
    'JSCSXMDEJJRGF': function (tender) {
        let feeField = 'labour',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CONSTRUCTION_TECH, feeField, subFeeField);
    },
    'JSCSXMDEJJCLF': function (tender) {
        let feeField = 'material',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CONSTRUCTION_TECH, feeField, subFeeField);
    },
    'JSCSXMDEJJJXF': function (tender) {
        let feeField = 'machine',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CONSTRUCTION_TECH, feeField, subFeeField);
    },
    'JSCSXMTZRGF': function (tender) {
        let feeField = 'adjustLabour',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CONSTRUCTION_TECH, feeField, subFeeField);
    },
    'JSCSXMTZJSRGF': function (tender) {
        let feeField = 'adjustMachineLabour',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CONSTRUCTION_TECH, feeField, subFeeField);
    },
    'JSCSXMZCF': function (tender) {
        let feeField = 'mainMaterial',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CONSTRUCTION_TECH, feeField, subFeeField);
    },
    'JSCSXMSBF': function (tender) {
        let feeField = 'equipment',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CONSTRUCTION_TECH, feeField, subFeeField);
    },
    'JSCSXMWJJCLF': function (tender) {
        return (this['JSCSXMZCF'](tender) + this['JSCSXMSBF'](tender)).toDecimal(decimalObj.bills.totalPrice);
    },
    'JSCSXMRGGR': function (tender) {
        const techQuantityType = tender ? 'tenderTechQuantity' : 'techQuantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.LABOUR){
                if(cbTools.isDef(glj[techQuantityType])){
                    rst = parseFloat(rst + glj[techQuantityType]).toDecimal(decimalObj.glj.quantity);
                }
            }
        }
        return rst;
    },
    'JSCSXMDEJJZJGCF': function (tender) {
        return (this['JSCSXMDEJJRGF'](tender) + this['JSCSXMDEJJCLF'](tender) + this['JSCSXMDEJJJXF'](tender)).toDecimal(decimalObj.bills.totalPrice);
    },
    'QTXMF': function (tender) {
        let feeField = 'common',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.OTHER, feeField, subFeeField);
    },
    'GF': function (tender) {
        let feeField = 'common',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.CHARGE, feeField, subFeeField);
    },
    'SJ': function (tender) {
        let feeField = 'common',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.TAX, feeField, subFeeField);
    },
    'AQWMSGZXF': function (tender) {
        let feeField = 'common',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        return cbTools.getBillsFee(fixedFlag.SAFETY_CONSTRUCTION, feeField, subFeeField);
    },
    'JZMJ': function () {
        let buildingArea = projFeatureView.getFeature('buildingArea');
        return buildingArea ? parseFloat(buildingArea) : 0;
    },
    'SQGCZJ': function (tender) {//税前工程造价
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.SAFETY_CONSTRUCTION])){
            return 0;
        }
        let bill = calcBase.fixedBills[calcBase.fixedFlag.SAFETY_CONSTRUCTION]['bill'];
        if(cbTools.isUnDef(bill) || cbTools.isUnDef(bill.ID)) return 0;
        let node = cbTools.getNodeByID(bill.ID);
        if(cbTools.isUnDef(node)) return 0;
        // 排除的节点：调用的节点、材料(工程设备)暂估价
        let excludeNodes = [node];
        let materialProvisional = calcTools.getNodeByFlag(fixedFlag.MATERIAL_PROVISIONAL);
        if (materialProvisional) {
            excludeNodes.push(materialProvisional);
        }
        return projectObj.project.calcProgram.getBeforeTaxTotalFee(excludeNodes, tender);
    },
    // 市政道路工程安全文明施工费: 按“税前工程造价”为基数，以累进办法计算。
    SZDLGCAQWMSGF(tender) {
        const baseFee = this['SQGCZJ'](tender);
        return calculateUtil.getProgressiveFee(baseFee, '市政道路工程安全文明施工费', projectObj.project.property.progressiveInterval, projectObj.project.property.taxType, decimalObj.bills.totalPrice);
    },
    // 市政桥梁工程安全文明施工费: 按“税前工程造价”为基数，以累进办法计算。
    SZQLGCAQWMSGF(tender) {
        const baseFee = this['SQGCZJ'](tender);
        return calculateUtil.getProgressiveFee(baseFee, '市政桥梁工程安全文明施工费', projectObj.project.property.progressiveInterval, projectObj.project.property.taxType, decimalObj.bills.totalPrice);
    },
    // 市政隧道工程安全文明施工费: 按“税前工程造价”为基数，以累进办法计算。
    SZSDGCAQWMSGF(tender) {
        const baseFee = this['SQGCZJ'](tender);
        return calculateUtil.getProgressiveFee(baseFee, '市政隧道工程安全文明施工费', projectObj.project.property.progressiveInterval, projectObj.project.property.taxType, decimalObj.bills.totalPrice);
    },
    // 城轨盾构工程安全文明施工费: 按“税前工程造价”为基数，以累进办法计算。
    CGDGGCAQWMSGF(tender) {
        const baseFee = this['SQGCZJ'](tender);
        return calculateUtil.getProgressiveFee(baseFee, '城轨盾构工程安全文明施工费', projectObj.project.property.progressiveInterval, projectObj.project.property.taxType, decimalObj.bills.totalPrice);
    },
    // 城轨高架桥工程安全文明施工费: 按“税前工程造价”为基数，以累进办法计算。
    CGGJQGCAQWMSGF(tender) {
        const baseFee = this['SQGCZJ'](tender);
        return calculateUtil.getProgressiveFee(baseFee, '城轨高架桥工程安全文明施工费', projectObj.project.property.progressiveInterval, projectObj.project.property.taxType, decimalObj.bills.totalPrice);
    },
    // 城轨地下工程安全文明施工费: 按“税前工程造价”为基数，以累进办法计算。
    CGDXGCAQWMSGF(tender) {
        const baseFee = this['SQGCZJ'](tender);
        return calculateUtil.getProgressiveFee(baseFee, '城轨地下工程安全文明施工费', projectObj.project.property.progressiveInterval, projectObj.project.property.taxType, decimalObj.bills.totalPrice);
    },
    // 城轨轨道工程安全文明施工费: 按“税前工程造价”为基数，以累进办法计算。
    CGGDGCAQWMSGF(tender) {
        const baseFee = this['SQGCZJ'](tender);
        return calculateUtil.getProgressiveFee(baseFee, '城轨轨道工程安全文明施工费', projectObj.project.property.progressiveInterval, projectObj.project.property.taxType, decimalObj.bills.totalPrice);
    },
    'RCJJC': function (tender) {//人材机价差
        return (this['RGJC'](tender) + this['CLJC'](tender) + this['JXJC'](tender)).toDecimal(decimalObj.bills.totalPrice);
    },
    'RGJC': function (tender) {//人工价差
        const quantityType = tender ? 'tenderQuantity' : 'quantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.LABOUR){
                //调整价
                let adjPrc = calcBase.project.projectGLJ.getAdjustPrice(glj);
                //价差
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                let dffPrc = parseFloat(marketPrice - adjPrc).toDecimal(decimalObj.glj.unitPrice);
                rst = (rst + parseFloat(glj[quantityType] * dffPrc).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'CLJC': function (tender) {//材料价差
        const quantityType = tender ? 'tenderQuantity' : 'quantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMaterialTypes.includes(glj.type) && glj.ratio_data.length === 0){//含有组成物的则用组成物计算，不含则自身算
                //调整价
                let adjPrc = calcBase.project.projectGLJ.getAdjustPrice(glj);
                //价差
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                let dffPrc = parseFloat(marketPrice - adjPrc).toDecimal(decimalObj.glj.unitPrice);
                rst = (rst + parseFloat(glj[quantityType] * dffPrc).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JXJC': function (tender) {//机械价差
        const quantityType = tender ? 'tenderQuantity' : 'quantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMachineTypes.includes(glj.type) && glj.ratio_data.length === 0){
                //调整价
                let adjPrc = calcBase.project.projectGLJ.getAdjustPrice(glj);
                //价差
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                let dffPrc = parseFloat(marketPrice - adjPrc).toDecimal(decimalObj.glj.unitPrice);
                rst = (rst + parseFloat(glj[quantityType] * dffPrc).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'FBFXRCJJC': function (tender) {//分部分项人材机价差
        return (this['FBFXRGJC'](tender) + this['FBFXCLJC'](tender) + this['FBFXJXJC'](tender)).toDecimal(decimalObj.bills.totalPrice);
    },
    'FBFXRGJC': function (tender) {//分部分项人工价差
        const quantityType = tender ? 'tenderSubdivisionQuantity' : 'subdivisionQuantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.LABOUR && cbTools.isDef(glj.subdivisionQuantity)){
                //调整价
                let adjPrc = calcBase.project.projectGLJ.getAdjustPrice(glj);
                //价差
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                let dffPrc = parseFloat(marketPrice - adjPrc).toDecimal(decimalObj.glj.unitPrice);
                rst = (rst + parseFloat(glj[quantityType] * dffPrc).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'FBFXCLJC': function (tender) {//分部分项材料价差
        const quantityType = tender ? 'tenderSubdivisionQuantity' : 'subdivisionQuantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMaterialTypes.includes(glj.type) && cbTools.isDef(glj.subdivisionQuantity) &&glj.ratio_data.length === 0){//含有组成物的则用组成物计算，不含则自身算
                //调整价
                let adjPrc = calcBase.project.projectGLJ.getAdjustPrice(glj);
                //价差
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                let dffPrc = parseFloat(marketPrice - adjPrc).toDecimal(decimalObj.glj.unitPrice);
                rst = (rst + parseFloat(glj[quantityType] * dffPrc).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'FBFXJXJC': function (tender) {//分部分项机械价差
        const quantityType = tender ? 'tenderSubdivisionQuantity' : 'subdivisionQuantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMachineTypes.includes(glj.type) && cbTools.isDef(glj.subdivisionQuantity) && glj.ratio_data.length === 0){
                //调整价
                let adjPrc = calcBase.project.projectGLJ.getAdjustPrice(glj);
                //价差
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                let dffPrc = parseFloat(marketPrice - adjPrc).toDecimal(decimalObj.glj.unitPrice);
                rst = (rst + parseFloat(glj[quantityType] * dffPrc).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JSCSXMRCJJC': function (tender) {//技术措施项目人材机价差
        return (this['JSCSXMRGJC'](tender) + this['JSCSXMCLJC'](tender) + this['JSCSXMJXJC'](tender)).toDecimal(decimalObj.bills.totalPrice);
    },
    'JSCSXMRGJC': function (tender) {//技术措施项目人工价差
        const quantityType = tender ? 'tenderTechQuantity' : 'techQuantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.LABOUR && cbTools.isDef(glj.techQuantity)){
                //调整价
                let adjPrc = calcBase.project.projectGLJ.getAdjustPrice(glj);
                //价差
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                let dffPrc = parseFloat(marketPrice - adjPrc).toDecimal(decimalObj.glj.unitPrice);
                rst = (rst + parseFloat(glj[quantityType] * dffPrc).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JSCSXMCLJC': function (tender) {//技术措施项目材料价差
        const quantityType = tender ? 'tenderTechQuantity' : 'techQuantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMaterialTypes.includes(glj.type) && cbTools.isDef(glj.techQuantity) &&glj.ratio_data.length === 0){//含有组成物的则用组成物计算，不含则自身算
                //调整价
                let adjPrc = calcBase.project.projectGLJ.getAdjustPrice(glj);
                //价差
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                let dffPrc = parseFloat(marketPrice - adjPrc).toDecimal(decimalObj.glj.unitPrice);
                rst = (rst + parseFloat(glj[quantityType] * dffPrc).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JSCSXMJXJC': function (tender) {//技术措施项目机械价差
        const quantityType = tender ? 'tenderTechQuantity' : 'techQuantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMachineTypes.includes(glj.type) && cbTools.isDef(glj.techQuantity) && glj.ratio_data.length === 0){
                //调整价
                let adjPrc = calcBase.project.projectGLJ.getAdjustPrice(glj);
                //价差
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                let dffPrc = parseFloat(marketPrice - adjPrc).toDecimal(decimalObj.glj.unitPrice);
                rst = (rst + parseFloat(glj[quantityType] * dffPrc).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGDEJJRGF': function (tender) {//甲供定额基价人工费
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.LABOUR){
                rst = (rst + parseFloat(projectGljObject.getSupplyQuantityByGlj(glj, tender) * calcBase.project.projectGLJ.getBasePrice(glj)).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGDEJJCLF': function (tender) {//甲供定额基价材料费
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMaterialTypes.includes(glj.type) && glj.ratio_data.length === 0){
                rst = (rst + parseFloat(projectGljObject.getSupplyQuantityByGlj(glj, tender) * calcBase.project.projectGLJ.getBasePrice(glj)).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGDEJJJXF': function (tender) {//甲供定额基价机械费
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMachineTypes.includes(glj.type) && glj.ratio_data.length === 0){
                rst = (rst + parseFloat(projectGljObject.getSupplyQuantityByGlj(glj, tender) * calcBase.project.projectGLJ.getBasePrice(glj)).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGRGF': function (tender) {//甲供人工费
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.LABOUR){
                const marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                rst = (rst + parseFloat(projectGljObject.getSupplyQuantityByGlj(glj, tender) * marketPrice).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGCLF': function (tender) {//甲供材料费
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMaterialTypes.includes(glj.type) && glj.ratio_data.length === 0){
                const marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                rst = (rst + parseFloat(projectGljObject.getSupplyQuantityByGlj(glj, tender) * marketPrice).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGJXF': function (tender) {//甲供机械费
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMachineTypes.includes(glj.type) && glj.ratio_data.length === 0){
                const marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                rst = (rst + parseFloat(projectGljObject.getSupplyQuantityByGlj(glj, tender) * marketPrice).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGZCF': function (tender) {//甲供主材费
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.MAIN_MATERIAL && glj.ratio_data.length === 0){
                rst = (rst + parseFloat(projectGljObject.getSupplyQuantityByGlj(glj, tender) * calcBase.project.projectGLJ.getBasePrice(glj)).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGSBF': function (tender) {//甲供设备费
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.EQUIPMENT){
                const marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                rst = (rst + parseFloat(projectGljObject.getSupplyQuantityByGlj(glj, tender) * marketPrice).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JDDEJJRGF': function (tender) {//甲定定额基价人工费
        const quantityType = tender ? 'tenderQuantity' : 'quantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.LABOUR && (glj.supply === supplyType.JDYG || glj.supply === supplyText.JDYG)){
                rst = (rst + parseFloat(glj[quantityType] * calcBase.project.projectGLJ.getBasePrice(glj)).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JDDEJJCLF': function (tender) {//甲定定额基价材料费
        const quantityType = tender ? 'tenderQuantity' : 'quantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMaterialTypes.includes(glj.type) && glj.ratio_data.length === 0 && (glj.supply === supplyType.JDYG || glj.supply === supplyText.JDYG)){
                rst = (rst + parseFloat(glj[quantityType] * calcBase.project.projectGLJ.getBasePrice(glj)).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JDDEJJJXF': function (tender) {//甲定定额基价机械费
        const quantityType = tender ? 'tenderQuantity' : 'quantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMachineTypes.includes(glj.type) && glj.ratio_data.length === 0 && (glj.supply === supplyType.JDYG || glj.supply === supplyText.JDYG)){
                rst = (rst + parseFloat(glj[quantityType] * calcBase.project.projectGLJ.getBasePrice(glj)).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JDRGF': function (tender) {//甲定人工费
        const quantityType = tender ? 'tenderQuantity' : 'quantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.LABOUR && (glj.supply === supplyType.JDYG || glj.supply === supplyText.JDYG)){
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                rst = (rst + parseFloat(glj[quantityType] * marketPrice).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JDCLF': function (tender) {//甲定材料费
        const quantityType = tender ? 'tenderQuantity' : 'quantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMaterialTypes.includes(glj.type) && glj.ratio_data.length === 0 && (glj.supply === supplyType.JDYG || glj.supply === supplyText.JDYG)){
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                rst = (rst + parseFloat(glj[quantityType] * marketPrice).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JDJXF': function (tender) {//甲定机械费
        const quantityType = tender ? 'tenderQuantity' : 'quantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(baseMachineTypes.includes(glj.type) && glj.ratio_data.length === 0 && (glj.supply === supplyType.JDYG || glj.supply === supplyText.JDYG)){
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                rst = (rst + parseFloat(glj[quantityType] * marketPrice).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JDZCF': function (tender) {//甲定主材费
        const quantityType = tender ? 'tenderQuantity' : 'quantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.MAIN_MATERIAL && (glj.supply === supplyType.JDYG || glj.supply === supplyText.JDYG)){
                rst = (rst + parseFloat(glj[quantityType] * calcBase.project.projectGLJ.getBasePrice(glj)).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JDSBF': function (tender) {//甲定设备费
        const quantityType = tender ? 'tenderQuantity' : 'quantity';
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.EQUIPMENT && (glj.supply === supplyType.JDYG || glj.supply === supplyText.JDYG)){
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : calcBase.project.projectGLJ.getMarketPrice(glj);
                rst = (rst + parseFloat(glj[quantityType] * marketPrice).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'ZGCLFFZM': function (tender) {//暂估材料费(从子目汇总)
        /*const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.ENGINEERINGCOST]['bill'];
        projectObj.project.calcProgram.calculate(cbTools.getNodeByID(bill.ID), false, false, true);
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.estimate) && cbTools.isDef(bill.feesIndex.estimate[totalFeeType]) ? bill.feesIndex.estimate[totalFeeType] : 0;*/
        let feeField = 'estimate',
            subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
        // 分部分项暂估合价
        let subEngineeringFee = cbTools.getBillsFee(fixedFlag.SUB_ENGINERRING, feeField, subFeeField);
        // 技术措施项目暂估合价
        let techFee = cbTools.getBillsFee(fixedFlag.CONSTRUCTION_TECH, feeField, subFeeField);
        return (subEngineeringFee + techFee).toDecimal(decimalObj.bills.totalPrice);

    },
    'FBF': function (tender) {//分包费
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let rst = 0;
        let rations = calcBase.project.Ration.datas;
        for(let ration of rations){
            if(ration.isSubcontract && ration.feesIndex && ration.feesIndex.common){
                rst = parseFloat(rst + ration.feesIndex.common[totalFeeType]).toDecimal(decimalObj.ration.totalPrice)
            }
        }
        return rst;
    },
    'FBDEJJRGF': function (tender) {//分包定额基价人工费
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let rst = 0;
        let rations = calcBase.project.Ration.datas;
        for(let ration of rations){
            if(ration.isSubcontract && ration.feesIndex && ration.feesIndex.labour){
                rst = parseFloat(rst + ration.feesIndex.labour[totalFeeType]).toDecimal(decimalObj.ration.totalPrice)
            }
        }
        return rst;
    },
    'FBDEJJCLF': function (tender) {//分包定额基价材料费
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let rst = 0;
        let rations = calcBase.project.Ration.datas;
        for(let ration of rations){
            if(ration.isSubcontract && ration.feesIndex && ration.feesIndex.material){
                rst = parseFloat(rst + ration.feesIndex.material[totalFeeType]).toDecimal(decimalObj.ration.totalPrice)
            }
        }
        return rst;
    },
    'FBDEJJJXF': function(tender){//分包定额基价机械费
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let rst = 0;
        let rations = calcBase.project.Ration.datas;
        for(let ration of rations){
            if(ration.isSubcontract && ration.feesIndex && ration.feesIndex.machine){
                rst = parseFloat(rst + ration.feesIndex.machine[totalFeeType]).toDecimal(decimalObj.ration.totalPrice)
            }
        }
        return rst;
    },
    'FBZCF': function (tender) {//分包主材费
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let rst = 0;
        let rations = calcBase.project.Ration.datas;
        for(let ration of rations){
            if(ration.isSubcontract && ration.feesIndex && ration.feesIndex.mainMaterial){
                rst = parseFloat(rst + ration.feesIndex.mainMaterial[totalFeeType]).toDecimal(decimalObj.ration.totalPrice)
            }
        }
        return rst;
    },
    'FBSBF': function (tender) {//分包设备费
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let rst = 0;
        let rations = calcBase.project.Ration.datas;
        for(let ration of rations){
            if(ration.isSubcontract && ration.feesIndex && ration.feesIndex.equipment){
                rst = parseFloat(rst + ration.feesIndex.equipment[totalFeeType]).toDecimal(decimalObj.ration.totalPrice)
            }
        }
        return rst;
    },
    'FBRGGR': function (tender) {//分包人工工日
        const quantityType = tender ? 'tenderQuantity' : 'quantity';
        let rst = 0;
        let rationObjs = [];//查找定额工料机
        let rations = calcBase.project.Ration.datas;
        let rationGljs = calcBase.project.ration_glj.datas;
        for(let ration of rations){
            if(ration.isSubcontract){
                //定额
                if(ration.type === rationType.ration){
                    rationObjs.push({ID: ration.ID, quantity: ration[quantityType]});
                }
                //量人 type 2, subType 1
                else if(ration.type === rationType.volumePrice && ration.subType === volumePriceMaps['量人']){
                    rst = parseFloat(rst + parseFloat(ration[quantityType])).toDecimal(decimalObj.glj.quantity);
                }
                //定额类型的人工工料机,type 3, subType 1
                else if(ration.type === rationType.gljRation && ration.subType === gljType.LABOUR){
                    rst = parseFloat(rst + parseFloat(ration[quantityType])).toDecimal(decimalObj.glj.quantity);
                }
            }
        }
        for(let ration of rationObjs){
            for(let glj of rationGljs){
                if(ration.ID === glj.rationID && glj.type === gljType.LABOUR){
                    rst = parseFloat(rst + parseFloat(glj[quantityType] * ration.quantity).toDecimal(decimalObj.glj.quantity)).toDecimal(decimalObj.glj.quantity);
                }
            }
        }
        return rst;
    }
};
const progreesiveBases = ['SZDLGCAQWMSGF', 'SZQLGCAQWMSGF', 'SZSDGCAQWMSGF', 'CGDGGCAQWMSGF', 'CGGJQGCAQWMSGF', 'CGDXGCAQWMSGF', 'CGGDGCAQWMSGF'];
let figureClassTemplate = {
    'CONSTRUCTION_ORGANIZATION': {flag: fixedFlag.CONSTRUCTION_ORGANIZATION, filter: ['CSXMF', 'ZZCSXMF', 'ZZCSXMDEJJZJGCF', 'ZZCSXMDEJJRGF', 'ZZCSXMDEJJCLF', 'ZZCSXMDEJJJXF', 'QTXMF', 'GF', 'SJ', 'SQGCZJ', 'AQWMSGZXF', ...progreesiveBases]},
    'SAFETY_CONSTRUCTION': {flag: fixedFlag.SAFETY_CONSTRUCTION, filter: ['CSXMF', 'ZZCSXMF', 'ZZCSXMDEJJZJGCF', 'ZZCSXMDEJJRGF', 'ZZCSXMDEJJCLF', 'ZZCSXMDEJJJXF', 'QTXMF', 'GF', 'SJ', 'AQWMSGZXF']},
    'OTHER': {flag: fixedFlag.OTHER, filter: ['QTXMF', 'SQGCZJ', 'AQWMSGZXF', ...progreesiveBases]},
    'CHARGE': {flag: fixedFlag.CHARGE, filter: ['GF', 'SQGCZJ', 'AQWMSGZXF', ...progreesiveBases]},
    'TAX': {flag: fixedFlag.TAX, filter: ['SJ', 'SQGCZJ', ...progreesiveBases]},
    'ENGINEERINGCOST': {flag: fixedFlag.ENGINEERINGCOST, filter: ['SQGCZJ', ...progreesiveBases]},
    'OTHERS': {flag: fixedFlag.ENGINEERINGCOST, filter: ['SQGCZJ', 'AQWMSGZXF', ...progreesiveBases]},
};

//基数的值不是通过清单节点获得的，则该基数的fixedBill为空，如价差、甲供、分包; class:分类，用于基数选择界面分类显示
//基数本身不与清单节点关联、但是其由与清单关联的节点四则运算得到，则拥有字段multiRef: [flags...]
let baseFigureMap = {
    //与清单直接关联=======
    '分部分项工程费': {base: 'FBFXGCF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
    '分部分项定额基价人工费': {base: 'FBFXDEJJRGF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
    '分部分项定额基价材料费': {base: 'FBFXDEJJCLF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
    '分部分项定额基价机械费': {base: 'FBFXDEJJJXF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
    '分部分项调整人工费': {base: 'FBFXTZRGF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
    '分部分项调整机上人工费': {base: 'FBFXTZJSRGF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
    '分部分项主材费': {base: 'FBFXZCF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
    '分部分项设备费': {base: 'FBFXSBF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
    '分部分项未计价材料费': {base: 'FBFXWJJCLF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
    '分部分项人工工日': {base: 'FBFXRGGR', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
    '分部分项定额基价直接工程费': {base: 'FBFXDEJJZJGCF', fixedFlag: fixedFlag.SUB_ENGINERRING, class: 'FBFX'},
    '措施项目费': {base: 'CSXMF', fixedFlag: fixedFlag.MEASURE, class: 'CSXM'},
    '组织措施项目费': {base: 'ZZCSXMF', fixedFlag: fixedFlag.CONSTRUCTION_ORGANIZATION, class: 'CSXM'},
    '组织措施项目定额基价直接工程费': {base: 'ZZCSXMDEJJZJGCF', fixedFlag: fixedFlag.CONSTRUCTION_ORGANIZATION, class: 'CSXM'},
    '组织措施项目定额基价人工费': {base: 'ZZCSXMDEJJRGF', fixedFlag: fixedFlag.CONSTRUCTION_ORGANIZATION, class: 'CSXM'},
    '组织措施项目定额基价材料费': {base: 'ZZCSXMDEJJCLF', fixedFlag: fixedFlag.CONSTRUCTION_ORGANIZATION, class: 'CSXM'},
    '组织措施项目定额基价机械费': {base: 'ZZCSXMDEJJJXF', fixedFlag: fixedFlag.CONSTRUCTION_ORGANIZATION, class: 'CSXM'},
    '技术措施项目费': {base: 'JSCSXMF', fixedFlag: fixedFlag.CONSTRUCTION_TECH, class: 'CSXM'},
    '技术措施项目定额基价人工费': {base: 'JSCSXMDEJJRGF', fixedFlag: fixedFlag.CONSTRUCTION_TECH, class: 'CSXM'},
    '技术措施项目定额基价材料费': {base: 'JSCSXMDEJJCLF', fixedFlag: fixedFlag.CONSTRUCTION_TECH, class: 'CSXM'},
    '技术措施项目定额基价机械费': {base: 'JSCSXMDEJJJXF', fixedFlag: fixedFlag.CONSTRUCTION_TECH, class: 'CSXM'},
    '技术措施项目调整人工费': {base: 'JSCSXMTZRGF', fixedFlag: fixedFlag.CONSTRUCTION_TECH, class: 'CSXM'},
    '技术措施项目调整机上人工费': {base: 'JSCSXMTZJSRGF', fixedFlag: fixedFlag.CONSTRUCTION_TECH, class: 'CSXM'},
    '技术措施项目主材费': {base: 'JSCSXMZCF', fixedFlag: fixedFlag.CONSTRUCTION_TECH, class: 'CSXM'},
    '技术措施项目设备费': {base: 'JSCSXMSBF', fixedFlag: fixedFlag.CONSTRUCTION_TECH, class: 'CSXM'},
    '技术措施项目未计价材料费': {base: 'JSCSXMWJJCLF', fixedFlag: fixedFlag.CONSTRUCTION_TECH, class: 'CSXM'},
    '技术措施项目人工工日': {base: 'JSCSXMRGGR', fixedFlag: fixedFlag.CONSTRUCTION_TECH, class: 'CSXM'},
    '技术措施项目定额基价直接工程费': {base: 'JSCSXMDEJJZJGCF', fixedFlag: fixedFlag.CONSTRUCTION_TECH, class: 'CSXM'},
    '其他项目费': {base: 'QTXMF',  fixedFlag: fixedFlag.OTHER, class: 'QTXM'},
    '规费': {base: 'GF', fixedFlag: fixedFlag.CHARGE, class: 'GF'},
    '税金': {base: 'SJ', fixedFlag: fixedFlag.TAX, class: 'SJ'},
    '安全文明施工专项费': {base: 'AQWMSGZXF', fixedFlag: fixedFlag.SAFETY_CONSTRUCTION, class: 'CSXM'},
    //不于清单直接关联==========
    '建筑面积': {base: 'JZMJ', class: 'FBFX'},
    '税前工程造价': {base: 'SQGCZJ', class: 'SQGCZJ', //安全文明施工专项费用使用
        cycleCalcRef: [fixedFlag.SUB_ENGINERRING, fixedFlag.OTHER, fixedFlag.CHARGE], //循环计算相关固定行，由于计算排除了本身，不用判断措施项目
        multiRef: [fixedFlag.SUB_ENGINERRING, fixedFlag.OTHER, fixedFlag.CHARGE]}, //相关固定行，需要关联措施项目，因为需要提取出多处引用的进行排序
    '人材机价差': {base: 'RCJJC', class: 'RCJ'},
    '人工价差': {base: 'RGJC', class: 'RCJ'},
    '材料价差': {base: 'CLJC', class: 'RCJ'},
    '机械价差': {base: 'JXJC', class: 'RCJ'},
    '分部分项人材机价差': {base: 'FBFXRCJJC', class: 'RCJ'},
    '分部分项人工价差': {base: 'FBFXRGJC', class: 'RCJ'},
    '分部分项材料价差': {base: 'FBFXCLJC', class: 'RCJ'},
    '分部分项机械价差': {base: 'FBFXJXJC', class: 'RCJ'},
    '技术措施项目人材机价差': {base: 'JSCSXMRCJJC', class: 'RCJ'},
    '技术措施项目人工价差': {base: 'JSCSXMRGJC', class: 'RCJ'},
    '技术措施项目材料价差': {base: 'JSCSXMCLJC', class: 'RCJ'},
    '技术措施项目机械价差': {base: 'JSCSXMJXJC', class: 'RCJ'},
    '甲供定额基价人工费': {base: 'JGDEJJRGF', class: 'RCJ'},
    '甲供定额基价材料费': {base: 'JGDEJJCLF', class: 'RCJ'},
    '甲供定额基价机械费': {base: 'JGDEJJJXF', class: 'RCJ'},
    '甲供人工费': {base: 'JGRGF', class: 'RCJ'},
    '甲供材料费': {base: 'JGCLF', class: 'RCJ'},
    '甲供机械费': {base: 'JGJXF', class: 'RCJ'},
    '甲供主材费': {base: 'JGZCF', class: 'RCJ'},
    '甲供设备费': {base: 'JGSBF', class: 'RCJ'},
    '甲定定额基价人工费': {base: 'JDDEJJRGF', class: 'RCJ'},
    '甲定定额基价材料费': {base: 'JDDEJJCLF', class: 'RCJ'},
    '甲定定额基价机械费': {base: 'JDDEJJJXF', class: 'RCJ'},
    '甲定人工费': {base: 'JDRGF', class: 'RCJ'},
    '甲定材料费': {base: 'JDCLF', class: 'RCJ'},
    '甲定机械费': {base: 'JDJXF', class: 'RCJ'},
    '甲定主材费': {base: 'JDZCF', class: 'RCJ'},
    '甲定设备费': {base: 'JDSBF', class: 'RCJ'},
    '暂估材料费(从子目汇总)': {base: 'ZGCLFFZM', class: 'RCJ'},
    /*'暂估材料费(从工料机汇总表汇总)': {base: 'ZGCLFFGLJ', class: 'RCJ'},*/
    '分包费': {base: 'FBF', class: 'FBF'},
    '分包定额基价人工费': {base: 'FBDEJJRGF', class: 'FBF'},
    '分包定额基价材料费': {base: 'FBDEJJCLF', class: 'FBF'},
    '分包定额基价机械费': {base: 'FBDEJJJXF', class: 'FBF'},
    '分包主材费': {base: 'FBZCF', class: 'FBF'},
    '分包设备费': {base: 'FBSBF', class: 'FBF'},
    '分包人工工日': {base: 'FBRGGR', class: 'FBF'}
};

//输入式分析器
let cbAnalyzer = {
    standar: function (exp) {
        //去空格
        exp = exp.replace(/\s/g, '');
        //（ to (
        exp = exp.replace(/（/g, '(');
        //）to )
        exp = exp.replace(/）/g, ')');
        //f to F
        exp = exp.replace(new RegExp('f', 'g'), 'F');
        return exp;
    },
    //输入合法性
    inputLegal: function (exp) {
        let ilegalRex = /[^0-9,\u4e00-\u9fa5,\+,\-,\/,\*,\(,\),.,{,},F,%]/g;
        return !ilegalRex.test(exp);
    },
    //基数合法性、存在性
    baseLegal: function (baseFigures, exp) {
        //保证中文表达式在{}里
        let cnExps = cbParser.getCN(exp);
        let expFigures = cbParser.getFigure(exp);
        if(cnExps.length !== expFigures.length){
            throw '清单基数必须要用花括号{}括起来'
            return false;
        }
        for(let i = 0, len = cnExps.length; i < len; i++){
            if(cnExps[i] !== expFigures[i]){
                throw '清单基数必须要用花括号{}括起来'
                return false;
            }
        }
        //基数存在性
        for(let i = 0, len = expFigures.length; i < len; i++){
            if(cbTools.isUnDef(baseFigures[expFigures[i]])){
                throw `清单基数{${expFigures[i]}}不存在`;
                return false;
            }
        }
        return true;
    },
    //行引用合法性、存在性
    fLegal: function (items, exp) {
        //提取F标记
        let fmArr = cbParser.getFMArr(exp);
        //提取行引用
        let fArr = cbParser.getFArr(exp);
        if(fmArr.length !== fArr.length){
            return false;
        }
        //提取行数
        let rArr = cbParser.getXNum(fArr);
        if(fArr.length !== rArr.length){
            return false;
        }
        rArr = Array.from(new Set(rArr));
        //判断合法性和存在性
        for(let i = 0, len = rArr.length; i < len; i++){
            let idx = rArr[i] - 1;
            if(cbTools.isUnDef(cbTools.getBillByRow(items, idx))){
                return false;
            }
        }
        return true;
    },
    //循环计算
    cycleCalc: function (node, baseFigures, exp) {
        let stack = [];
        if(node.sourceType !== calcBase.project.Bills.getSourceType()){
            return false;
        }
        //用于判断的起始清单ID
        //let sIDs = cbTools.getNodeIDs(Array.from(new Set([cbTools.getBaseBill(node)].concat(cbTools.getParents(node)))));
        let sIDs = cbTools.getNodeIDs(cbTools.getParents(node));
        let figureF = cbParser.getFigureF(cbParser.getFigure(exp), cbParser.getUID(cbParser.getFIDArr(exp)));
        for(let i = 0, len = figureF.length; i < len; i++){
            let figure = figureF[i];
            let billsIDs = [];
            if(figure.type === 'base' && cbTools.isDef(baseFigures[figure.value])){
                //重构后：
                //多重引用基数
                let figureMultiRef = baseFigures[figure.value]['multiRef'];
                let cycleCalcRef = baseFigures[figure.value]['cycleCalcRef'];
                if(cbTools.isDef(figureMultiRef)){
                    if(cbTools.isDef(cycleCalcRef)){
                        figureMultiRef = cycleCalcRef;
                    }
                    for(let flag of figureMultiRef){
                        let bills = cbTools.findBill(flag);
                        if(bills){
                            billsIDs.push(bills.ID);
                        }
                    }
                } else {
                    billsIDs = cbTools.isDef(baseFigures[figure.value]['fixedBill']) ? [baseFigures[figure.value]['fixedBill']['bill']['ID']] : [];
                }
            } else if(figure.type === 'id'){
                let node = cbTools.getNodeByID(figure.value);
                billsIDs = cbTools.isDef(node) ? [node.data.ID] : [];
            }
            if(cbTools.isDef(billsIDs) && billsIDs.length > 0 && isCycle(billsIDs)){
                console.log('循环计算');
                calcBase.errMsg = '表达式出现循环计算';
                return true;
            }
        }
        return false;
        function checkStack(stack, sIDs){
            //引用栈发现了初始引用
            for(let i = 0, len = sIDs.length; i < len; i++){
                if(stack.indexOf(sIDs[i]) !== -1){
                    return true;
                }
            }
            return false;
        }
        function isCycle(billIDs){
            stack = Array.from(new Set(stack.concat(billIDs)));
            if (checkStack(stack, sIDs)) {
                return true;
            }
            for(let i = 0, len = billIDs.length; i < len; i++){
                let block = cbTools.getStackBlock(billIDs[i]);
                if(block.length > 0){
                    stack = Array.from(new Set(stack.concat(block)));
                    let cycleFlag = isCycle(block);
                    if (cycleFlag === true) {
                        return cycleFlag;
                    }
                    //return isCycle(block);
                }
            }
            return false;
        }
        /*function isCycle(billIDs){
            stack = Array.from(new Set(stack.concat(billIDs)));
            /!*if (checkStack(stack, sIDs)) {
                return true;
            }*!/
            for(let i = 0, len = billIDs.length; i < len; i++){
                let block = cbTools.getStackBlock(billIDs[i]);
                if(block.length > 0){
                    stack = Array.from(new Set(stack.concat(block)));
                    if(checkStack(stack, sIDs)){
                        return true;
                    }
                    return isCycle(block);
                }
            }
        }*/
    },
    //四则运算合法性,控制不允许重复出现运算符,这里再判断一次,控制行引用只能F
    arithmeticLegal: function (exp) {
        let ilegalRex = /[\+,\-,\*,\/]{2}/g;
        let rex2 = /[{]{2}/g;
        let rex3 = /[}]{2}/g;
        let rex4 = /[F]{2}/g;
        let rex5 = /[.]{2}/g;
        let rex6 = /[%]{2}/g;
        return !ilegalRex.test(exp) && !rex2.test(exp) && !rex3.test(exp) && !rex4.test(exp) && !rex5.test(exp) && !rex6.test(exp);
    },
    //
    legalExp: function (node) {
        let exp = this.standar(node.data.userCalcBase);
        if(!this.inputLegal(exp)){
            throw '表达式含有无效字符';
        }
        if(!this.arithmeticLegal(exp)){
            throw '表达式含有无效字符';
        }
        if(!this.baseLegal(cbTools.getFigure(node), exp)){
            throw '清单基数不合法';
        }
        if(!this.fLegal(calcBase.project.mainTree.items, exp)){
            throw '行引用不合法';
        }
        //转换成ID引用
        exp = cbParser.toIDExpr(exp);
        if(this.cycleCalc(node, calcBase.baseFigures, exp)){
            throw '出现循环计算';
        }
        return exp;
    }
};

//输入式转换器
let cbParser = {
    //获取标记F
    getFMArr: function (exp) {
        let fmRex = /F/g;
        let fmArr = exp.match(fmRex);
        return cbTools.isDef(fmArr) ? fmArr : [];
    },
    //获取行引用 eg: F10
    getFArr: function (exp) {
        let fRex = /F\d+\b/g;
        let fArr = exp.match(fRex);
        return cbTools.isDef(fArr) ? fArr : [];
    },
    //获取X+num eg: F10  10 @105 105
    getXNum: function (arr) {
        let rRex = /\d+/g;
        let tempArr = [];
        for(let i = 0, len = arr.length; i < len; i++){
            tempArr = tempArr.concat(arr[i].match(rRex));
        }
        return tempArr;
        /*let rArr = Array.from(new Set(tempArr));
        return rArr;*/
    },
    //获取uuid
    getUID: function (arr) {
        let rRex = /[\d,a-z,A-Z,-]{36}/g;
        let tempArr = [];
        for(let i = 0, len = arr.length; i < len; i++){
            tempArr = tempArr.concat(arr[i].match(rRex));
        }
        let rArr = Array.from(new Set(tempArr));
        return rArr;
    },
    //获取ID引用
    getFIDArr: function (exp) {
        let fidRex = /@[\d,a-z,A-Z,-]{36}/g;
        let fidArr = exp.match(fidRex);
        return cbTools.isDef(fidArr) ? fidArr : [];
    },
    //获取表达式中的中文式
    getCN: function(expr){
       /* let rst = [];
        let cnRex = /[^\u4e00-\u9fa5]/;
        let temp = expr.split(cnRex);
        //暂估材料费特殊处理：CN(CN)
        let isZG = false;
        for(let i = 0, len = temp.length; i < len; i++){
            if(temp[i] !== ''){
            //if(temp[i] !== '' && rst.indexOf(temp[i]) === -1){
                if(temp[i] === '暂估材料费'){//处理暂估材料基数
                    isZG = true;
                    continue;
                }
                if(isZG){//上一个是暂估材料费
                    //拼接成完整的暂估材料费(CN);
                    temp[i] = `${temp[i-1]}(${temp[i]})`;
                    isZG = false;
                }
                rst.push(temp[i]);
            }
        }
        return rst;*/
        let cnRex = /[\u4e00-\u9fa5]{1,}\({0,}[\u4e00-\u9fa5]{0,}\){0,}[\u4e00-\u9fa5]{0,}/g;
        return _.filter(expr.match(cnRex), function (data) {
            return data
        });
    },
    //获取表达式中的基数
    getFigure: function(expr){
        let rst = [];
        let rex = /\{([^}]*)\}/g;
        let temp = expr.match(rex);
        if(cbTools.isDef(temp)){
            for(let i = 0, len = temp.length; i < len; i++){
                rst.push(temp[i].replace(/[{,}]/g, ''));
            }
        }
        return rst;
    },
    //获取表达式中的基数和ID引用
    getFigureF: function (figures, fidArr) {
        let rst = [];
        for(let i = 0, len = figures.length; i < len; i++){
            let obj = Object.create(null);
            obj.type = 'base';
            obj.value = figures[i];
            rst.push(obj);
        }
        for(let i = 0, len = fidArr.length; i < len; i++){
            let obj = Object.create(null);
            obj.type = 'id';
            obj.value = fidArr[i];
            rst.push(obj);
        }
        return rst;
    },
    //表达式中的百分数转换成小数
    percentToNum: function (exp) {
        let rex = /\d+(\.\d+)?%/g;
        let percents = exp.match(rex);
        let numRex = /\d+(\.\d+)?/g;
        if(cbTools.isDef(percents)){
            for(let i = 0, len = percents.length; i < len; i++){
                let percentNum = percents[i].match(numRex);
                if(cbTools.isDef(percentNum) && percentNum.length === 1){
                    exp = exp.replace(new RegExp(percents[i], 'g'), percentNum[0]/100);
                }
            }
        }
        return exp;
    },
    //将行引用转换成ID引用
    toIDExpr: function (exp) {
        let exps = [];
        //获得行引用
        let fArr = this.getFArr(exp);
        for(let i = 0, len = fArr.length; i < len; i++){
            let r = this.getXNum([fArr[i]]);
            if(r.length === 1){
                let node = cbTools.getBillByRow(calcBase.project.mainTree.items, r[0] - 1);
                if(cbTools.isUnDef(node)){
                    //continue;
                    calcBase.errMsg = '行引用错误';
                    throw '行引用错误';
                }
                exps.push({orgExp: fArr[i], newExp: '@' + node.data.ID});
            }
            else {
                calcBase.errMsg = '行引用错误';
                throw '行引用错误';
            }
        }
        for(let i = 0, len = exps.length; i < len; i++){
            exp = exp.replace(new RegExp(`${exps[i].orgExp}\\b`, 'g'), exps[i].newExp);
        }
        return exp;
    },
    //将ID引用转换成行引用
    toFExpr: function (exp) {
        let exps = [];
        //获得ID引用
        let fidArr = this.getFIDArr(exp);
        for(let i = 0, len = fidArr.length; i < len; i++){
            let id = this.getUID([fidArr[i]]);
            if(id.length === 1){
                let row = cbTools.getRowByID(calcBase.project.mainTree.items, id[0]);
                if(cbTools.isUnDef(row)){
                    continue;
                }
                exps.push({orgExp: fidArr[i], newExp: 'F' + row});
            }
        }
        for(let i = 0, len = exps.length; i < len; i++){
            exp = exp.replace(new RegExp(`${exps[i].orgExp}\\b`, 'g'), exps[i].newExp);
        }
        return exp;
    },
    //将表达式转换为可编译的表达式
    toCompileExpr: function(v){
        /*if(v === ''){
            return '$CBC.base(\'NONE\')';
        }
        //基数
        let strs = _.uniq(this.getFigure(v));
        let exps = [];
        for(let i = 0, len = strs.length; i < len; i++){
            let exp = Object.create(null);
            exp.orgExp = `{${strs[i]}}`;
            exps.push(exp);
        }
        //去{}
        //v = v.replace(/[{, },]/g, '');
        for(let i = 0, len = exps.length;i < len; i++){
            exps[i].compileExp = '$CBC.base(\'' + exps[i].orgExp + '\')';
            //暂估材料费作特殊处理
            if(exps[i].orgExp === '{暂估材料费(从子目汇总)}' || exps[i].orgExp === '{暂估材料费(从工料机汇总表汇总)}'){
                let reCount = v.split(exps[i].orgExp).length - 1;
                while (reCount > 0){
                    v = v.replace(exps[i].orgExp, exps[i].compileExp);
                    reCount --;
                }
            }
            else {
                v = v.replace(new RegExp(exps[i].orgExp, 'g'), exps[i].compileExp);
            }
        }
        //去{}
        v = v.replace(/[{, },]/g, '');
        //行引用
        let fidArr = this.getFIDArr(v);
        let fExps = [];
        for(let i = 0, len = fidArr.length; i < len; i++){
            let fExp = Object.create(null);
            fExp.orgExp = fidArr[i];
            fExps.push(fExp);
        }
        for(let i = 0, len = fExps.length; i < len; i++){
            fExps[i].compileExp = '$CBC.ref(\'' + fExps[i].orgExp + '\')';
            v = v.replace(new RegExp(fExps[i].orgExp, 'g'), fExps[i].compileExp);
        }
        return v;*/
        if(v === ''){
            return '$CBC.base(\'NONE\')';
        }
        //基数
        let strs = _.uniq(this.getFigure(v));
        let exps = [];
        for(let i = 0, len = strs.length; i < len; i++){
            let exp = Object.create(null);
            exp.orgExp = `{${strs[i]}}`;
            exps.push(exp);
        }
        for(let i = 0, len = exps.length;i < len; i++){
            exps[i].compileExp = '$CBC.base(\'' + exps[i].orgExp + '\')';
            let regStr = exps[i].orgExp.replace(/\(/g, '\\\(');
            regStr = regStr.replace(/\)/g, '\\\)');
            v = v.replace(new RegExp(regStr, 'g'), exps[i].compileExp);
        }
        //去{}
        v = v.replace(/[{, },]/g, '');
        //行引用
        let fidArr = _.uniq(this.getFIDArr(v));
        let fExps = [];
        for(let i = 0, len = fidArr.length; i < len; i++){
            let fExp = Object.create(null);
            fExp.orgExp = fidArr[i];
            fExps.push(fExp);
        }
        for(let i = 0, len = fExps.length; i < len; i++){
            fExps[i].compileExp = '$CBC.ref(\'' + fExps[i].orgExp + '\')';
            v = v.replace(new RegExp(fExps[i].orgExp, 'g'), fExps[i].compileExp);
        }
        return v;
    }
};

let cbCalctor = {
    //计算基数
    base: function (figure) {
        if(figure === 'NONE'){
            return 0;
        }
        return baseFigureTemplate[calcBase.baseFigures[figure]['base']]();
    },
    //调价后计算基数
    tenderBase: function (figure) {
        if(figure === 'NONE'){
            return 0;
        }
        return baseFigureTemplate[calcBase.baseFigures[figure]['base']](true);
    },
    //ID引用
    ref: function (fExp) {
        let ID = cbParser.getUID([fExp]);
        if(ID.length === 1){
            let node = cbTools.getNodeByID(ID[0]);
            return cbTools.isDef(node) &&
                    cbTools.isDef(node.data.feesIndex) &&
                    cbTools.isDef(node.data.feesIndex.common) &&
                    cbTools.isDef(node.data.feesIndex.common.totalFee) ?
                    node.data.feesIndex.common.totalFee : 0;
        }
        return 0;
    },
    tenderRef: function (fExp) {
        let ID = cbParser.getUID([fExp]);
        if(ID.length === 1){
            let node = cbTools.getNodeByID(ID[0]);
            return cbTools.isDef(node) &&
                    cbTools.isDef(node.data.feesIndex) &&
                    cbTools.isDef(node.data.feesIndex.common) &&
                    cbTools.isDef(node.data.feesIndex.common.tenderTotalFee) ?
                    node.data.feesIndex.common.tenderTotalFee : 0;
        }
        return 0;
    }
};

let calcBase = {
    errMsg: '表达式不正确',
    success: false,
    //清单固定行
    fixedFlag: null,
    fixedBills: Object.create(null),
    //清单基数
    baseFigures: Object.create(null),
    //清单可选基数映射，分两类：组织措施项目：排除父项和计算的父项; 其他项目、规费、税金、工程造价，及新增部分：显示所有计算基数
    baseFigureClass: Object.create(null),
    //初始化
    init: function (project) {
        let me = this;
        me.project = project;
        me.fixedFlag = fixedFlag;
        cbTools.setFixedBills(project, me.fixedBills, me.fixedFlag);
        me.baseFigures = baseFigureMap;
        cbTools.setBaseBills(me.baseFigures, me.fixedBills);
        cbTools.setBaseFigureClass(me.baseFigures, me.baseFigureClass);
    },
    getBase: function (figure) {
        return cbCalctor.base(figure);

    },
    getBaseByClass: function (node) {
        return cbTools.getFigure(node);
    },
    getBaseBill: function (node) {
        return cbTools.getBaseBill(node);
    },
    calculate: function (node, reCalc = null) {
            let me = calcBase,
            $CBA = cbAnalyzer,
            $CBP = cbParser,
            $CBC = cbCalctor;
        try {
            me.success = false;
            me.errMsg = '表达式不正确';
            //分析输入式合法性
            let exp = reCalc
                ? cbTools.isDef(node.data.calcBase)
                    ? node.data.calcBase
                    : ''
                : $CBA.legalExp(node);
            if(!cbTools.isDef(exp)){
                throw '表达式不正确';
            }
            //输入式转换表达式
            let compileExp = $CBP.toCompileExpr(exp);
            //计算
            let calcExp = $CBP.percentToNum(compileExp);
            let calcBaseValue = eval(calcExp);
            if(!cbTools.isNum(calcBaseValue)){
                throw '基数计算结果不为数值';
            }
            //调价
            let tenderCalcExp = calcExp
                .replace(new RegExp('base', 'g'), 'tenderBase')
                .replace(new RegExp('ref', 'g'), 'tenderRef');
            let tenderCalcBaseValue = eval(tenderCalcExp);
            if(!cbTools.isNum(tenderCalcBaseValue)){
                throw '调价基数计算结果不为数值';
            }

            //存储
            me.success = true;
            node.data.calcBase = exp;
            node.data.calcBaseValue = parseFloat(calcBaseValue).toDecimal(decimalObj.decimal('totalPrice', node));
            node.data.tenderCalcBaseValue = parseFloat(tenderCalcBaseValue).toDecimal(decimalObj.decimal('totalPrice', node));
            node.changed = true;
        }
        catch (err){
            if(typeof err === 'object'){
                err = '表达式不正确'
            }
            if (node) {
                err = `第${node.serialNo() + 1}行${err}`;
            }
            alert(err);
        }
    }
};