/**
 * 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;
    },
    findNodeByFlag: function (fixedFlag) {
        let bills = this.findBill(fixedFlag);
        if (!bills) {
            return null;
        }
        return this.getNodeByID(bills.ID);
    },
    /*//通过行获取根节点清单
    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;
    },*/
    //通过行获取节点清单
    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;
    },
    //通过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);
            }
        }
    },
    //获取所有节点的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' &&  calcBase.baseFigures && 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];
            }
        }
    },
    //设置清单固定行下可用的基数映射
    //@param {Object}baseFigures(当前项目可用总基数配置表) {Object}mapping(可用基数映射,初始为空object,目标:{flag: Array(baseList)}) eg: {'1': ['xx费']}
    setValidBaseMapping: function (baseFigures, mapping) {
        //清单固定行数组[1, 2...]
        let allFlags = [];
        //清单固定行与子清单固定行映射
        let subFlagMapping = {};
        for (let attr in fixedFlag) {
            let flag = fixedFlag[attr];
            allFlags.push(flag);
            let subFlagList = this.getSubFlagList(flag);
            subFlagMapping[flag] = subFlagList;
        }
        for(let baseName in baseFigures) {
            let calcBase = baseFigures[baseName],
                filter = calcBase.filter || Object.values(fixedFlag), // filter为空则全部部分都可用该基数
                pick = calcBase.pick; //挑选或过滤
            /* if (!filter) {
                continue;
            } */
            //pick为true，则filter中的清单固定行可使用此基数(及其子清单固定行)，
            //pick为false除去filter中的清单固定行（及其子清单固定行），其他可使用此基数（包括新增的大项费用）
            let allFilter = []; //filter及其子项
            for (let flag of filter) {
                if (subFlagMapping[flag].length > 0) {
                    allFilter = allFilter.concat(subFlagMapping[flag]);
                }
            }
            allFilter = allFilter.concat(filter);
            allFilter = Array.from(new Set(allFilter));
            //获取可使用此基数的清单固定行
            let validFlags = pick ? allFilter : allFlags.filter(function (flag) {
                return !allFilter.includes(flag);
            });
            //其他节点可使用的基数（新增的大项费用）,即基数配置表中过滤条件为“只允许非固定类别是xxx”的
            //允许非固定类别xx可用，则新增的大项费用也可用，新增的大项费用flag为null
            if (!pick) {
                if (mapping['other']) {
                    mapping['other'].push(baseName);
                } else {
                    mapping['other'] = [baseName];
                }
            }
            //设置清单固定行可使用此基数
            for (let flag of validFlags) {
                if (mapping[flag]) {
                    mapping[flag].push(baseName)
                } else {
                    mapping[flag] = [baseName];
                }
            }
        }
    },
    //该节点可使用的基数列表
    getValidFigures: function (node) {
        let filterMap = {},
            avaBaseNames = [];
        //该节点所属的固定行
        let belongFlag = this.getBelongFlag(node);
        //没有所属固定行，则属于新增的大项费用
        //获取可使用的基数
        if (!belongFlag) {
            avaBaseNames = calcBase.flagValidBase['other'];
        } else {
            avaBaseNames = calcBase.flagValidBase[belongFlag] ? calcBase.flagValidBase[belongFlag] : [];
        }
        for (let baseName of avaBaseNames) {
            let base = calcBase.baseFigures[baseName];
            if (baseName) {
                filterMap[baseName] = base;
            }
        }
       return filterMap

    },
    //根据清单固定行，获取子固定行
    getSubFlagList: function (flag) {
        let flagList = [];
        let node = this.findNodeByFlag(flag);
        if (!node) {
            return flagList;
        }
        let allChildren = [];
        function getChildren(nodes) {
            allChildren = allChildren.concat(nodes);
            for (let node of nodes) {
                if (node.children.length > 0) {
                    getChildren(node.children);
                }
            }
        }
        getChildren(node.children);
        for (let child of allChildren) {
            if (child.data && this.isFlag(child.data)) {
                flagList.push(child.data.flagsIndex.fixed.flag);
            }
        }
        return flagList
    },
    //获取节点所属的清单固定行
    getBelongFlag: function (node) {
        while (node) {
            if (node.data && this.isFlag(node.data)) {
                return node.data.flagsIndex.fixed.flag;
            }
            node = node.parent;
        }
        return null;
    },
    //获取节点所属的清单固定列表
    getBelongFlagList: function (node) {
        let rst = [];
        while (node) {
            if (node.data && this.isFlag(node.data)) {
                rst.push(node.data.flagsIndex.fixed.flag);
            }
            node = node.parent;
        }
        return rst;
    },
    //获取清单（有基数计算）引用了的其他清单,（循环引用栈中的一块）
    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]) &&  !isNaN(bills.feesIndex[feeField][subFeeField])
               ? bills.feesIndex[feeField][subFeeField]
               : 0;
    },
    getFee: function (bills, feeField, subFeeField) {
        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]) &&
            !isNaN(bills.feesIndex[feeField][subFeeField])   
            ? bills.feesIndex[feeField][subFeeField]
            : 0;
    },
    //获取扣除固定项后的金额，扣除其节点后重新汇总
    //@param {Number}fixedFlag(基数取值固定行类别) {Array}deductFlags(扣除的固定类别组) {String}fullFeeField(完整的取费字段: 'rationCommon.totalFee')
    //@return {Number}
    getFeeWithDeduction: function (fixedFlag, deductFlags, fullFeeField) {
        let baseNode = this.findNodeByFlag(fixedFlag);
        if (!baseNode) {
            return 0;
        }
        //要扣除的节点
        let deductNodes = [];
        for (let deFlag of deductFlags) {
            let node = this.findNodeByFlag(deFlag);
            if (node) {
                deductNodes.push(node);
            }
        }
        return projectObj.project.calcProgram.getTotalFee([baseNode], deductNodes, fullFeeField);
    },
    //获取累进办法计算的金额
    //@param {Number}baseFee(相关基数金额) {String}name(使用累进计算的基数名称)
    //@return {Number}
    getProgressiveFee: function (baseFee, name) {
        const progressiveData = calcBase.project.property.progressiveInterval;
        if (!progressiveData) {
            throw '该项目不存在累进区间数据';
        }
        //根据基数名称匹配累进库数据,标准化以免(（)）等不同导致不匹配
        const matchProgressiveData = progressiveData.find(item => cbAnalyzer.standar(item.name) === cbAnalyzer.standar(name));
        if (!matchProgressiveData) {
            throw `计算基数{${name}}不存在累进区间数据`;
        }
        // 将原始数据转换成方便处理的数据：[{feeRate: xx, min: 0, max: 200, minOpr: '(', maxOpr: ']'}]
        const progression = matchProgressiveData.progression.map(item => {
            // item.interval内容： eg (0,200]、[300,500) [1000,+)....
            const interval = cbAnalyzer.standar(item.interval);
            // ( => 大于 [ => 大于等于 ) => 小于 ] => 小于等于
            const minReg = /([\(\[])(\d+)/;
            const minMatch = minReg.exec(interval);
            if (!minMatch || !minMatch[1] || !minMatch[2]) {
                throw `计算基数{${name}}累进区间数据错误`;
            }
            const minOpr = minMatch[1];
            // 后台数据单位为万元，这里转为为元
            const min = parseFloat(minMatch[2]) * 10000;
            const maxReg = /[\,，]([\d\+]+)([\)\]])/;
            const maxMatch = maxReg.exec(interval);
            if (!maxMatch || !maxMatch[1] || !maxMatch[2]) {
                throw `计算基数{${name}}累进区间数据错误`;
            }
            const max = maxMatch[1] === '+' ? 'infinity' : parseFloat(maxMatch[1]) * 10000;
            const maxOpr = maxMatch[2];
            return {
                feeRate: item.feeRate,
                min,
                minOpr,
                max,
                maxOpr
            }
        });
        progression.sort((a, b) => a.min - b.min);
        // 基数所在区间
        const withinData = progression.find(item => {
            const oprMiddle = item.max === 'infinity' ? '+' : '';
            const oprLink = item.minOpr + oprMiddle + item.maxOpr;
            switch (oprLink) {
                case '()':
                    return baseFee > item.min && baseFee < item.max;
                case '(]':
                    return baseFee > item.min && baseFee <= item.max;
                case '[)':
                    return baseFee >= item.min && baseFee < item.max;
                case '[]':
                    return baseFee >= item.min && baseFee <= item.max;
                case '(+)':
                case '(+]':
                    return baseFee > item.min;
                case '[+)':
                case '[+]':
                    return baseFee >= item.min;
                default:
                    return false;
            }
        });
        if (!withinData) {
            return 0;
        }
        // 累进计算
        let fee = 0;
        //累进之前的区间
        for (let i = 0; i < progression.indexOf(withinData); i++) {
            const perData = progression[i];
            fee += (perData.max - perData.min) * perData.feeRate * 0.01;
        }
        //累进所在区间
        fee += (baseFee - withinData.min) * withinData.feeRate * 0.01;
        return fee.toDecimal(decimalObj.bills.totalPrice);
    },
    //获取清单100章下的节点（只需要找最底层的，排除了底层，父项金额即排除了子项）
    //@param {Object}node(判断的节点，最底层清单节点)
    //@return {Boolean}
    withingOneHundred: function (node) {
        if (!node || node.sourceType !== calcBase.project.Bills.getSourceType() || node.source.children.length > 0) {
            return false;
        }
        //节点所属的清单固定行为第100章清单
        let belongFlags = cbTools.getBelongFlagList(node);
        return belongFlags.includes(fixedFlag.ONE_HUNDRED_BILLS);
    }
};


let baseFigureTemplate = {
    /*
     * 预算项目
     * */
    'budget': {
        //{定额建筑安装工程费(不含定额设备购置费及专项费用)}
        //取清单固定类别是“建筑安装工程”的定额建安费，但要扣除清单固定类别是“设备购置费”、及“专项费用”的定额建安费
        'DEJZAZGCFBHSBZX': function (tender) {
            let fullFeeField = tender ? 'rationCommon.tenderTotalFee' : 'rationCommon.totalFee',
                deductFlags = [fixedFlag.EQUIPMENT_ACQUISITION_FEE, fixedFlag.SPECIAL_COST];
            return cbTools.getFeeWithDeduction(fixedFlag.CONSTRUCTION_INSTALL_FEE, deductFlags, fullFeeField).toDecimal(decimalObj.bills.totalPrice);
        },
        //{定额建筑安装工程（其中定额设备购置费按 40%计）} (定额建筑安装工程设备四十)
        //扣除设备购置费，再加上设备购置费的40%，扣除汇总算法不四舍五入，相当于汇总当中定额设备购置费就按照了40%计
        'DEJZAZGCSBSS': function (tender) {
            let feeField = 'rationCommon',
                subFeeField = tender ? 'tenderTotalFee' : 'totalFee',
                deductFlags = [fixedFlag.EQUIPMENT_ACQUISITION_FEE];
            //建安费扣除定额设备购置费
            let afterDeductFee = cbTools.getFeeWithDeduction(fixedFlag.CONSTRUCTION_INSTALL_FEE, deductFlags, `${feeField}.${subFeeField}`);
            //定额设备购置费
            let equipmentAcFee = cbTools.getBillsFee(deductFlags[0], feeField, subFeeField);
            return (afterDeductFee + equipmentAcFee * 0.4).toDecimal(decimalObj.bills.totalPrice);
        },
        //{建筑安装工程费(不含安全生产费)}
        // 取清单固定类别是“建筑安装工程”的金额，但要扣除清单固定类别是“安全生产费”的金额
        'JZAZGCFBHSC': function (tender) {
            let fullFeeField = tender ? 'common.tenderTotalFee' : 'common.totalFee',
                deductFlags = [fixedFlag.SAFE_COST];
            //建安费扣除安全生产费
            return cbTools.getFeeWithDeduction(fixedFlag.CONSTRUCTION_INSTALL_FEE, deductFlags, fullFeeField).toDecimal(decimalObj.bills.totalPrice);
        },
        //{建筑安装工程费(不含设备费)}
        // 取清单固定类别是“建筑安装工程”的金额，但要扣除清单固定类别是“设备购置费”的金额
        'JZAZGCFBHSB': function (tender) {
            let fullFeeField = tender ? 'common.tenderTotalFee' : 'common.totalFee',
                deductFlags = [fixedFlag.EQUIPMENT_ACQUISITION_FEE];
            //建安费扣除设备费
            return cbTools.getFeeWithDeduction(fixedFlag.CONSTRUCTION_INSTALL_FEE, deductFlags, fullFeeField).toDecimal(decimalObj.bills.totalPrice);
        },
        //{建筑安装工程费}
        // 取清单固定类别是“建筑安装工程”的金额
        'JZAZGCF': function (tender) {
            let feeField = 'common',
                subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
            return cbTools.getBillsFee(calcBase.fixedFlag.CONSTRUCTION_INSTALL_FEE, feeField, subFeeField);
        },
        //{土地使用及拆迁补偿费}
        // 取清单固定类别是“土地使用及拆迁补偿费”的金额
        'TDSYJCQBCF': function (tender) {
            let feeField = 'common',
                subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
            return cbTools.getBillsFee(calcBase.fixedFlag.LAND_USED_DEMOLITION, feeField, subFeeField);
        },
        //{养护工程其他费}
        // 取清单固定类别是“养护工程其他费”的金额
        'YHGCQTF': function (tender) {
            let feeField = 'common',
                subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
            return cbTools.getBillsFee(calcBase.fixedFlag.MAINTENANCE_EXPENSES, feeField, subFeeField);
        },
        //{预备费}
        // 取清单固定类别是“预备费”的金额
        'YBF': function(tender) {
            let feeField = 'common',
                subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
            return cbTools.getBillsFee(calcBase.fixedFlag.BUDGET_FEE, feeField, subFeeField);
        },
        //{施工场地建设费}
        //使用累进办法计算，基数为{定额建筑安装工程费（不含定额设备购置费及专项费用）}
        'SGCDJSF': function (tender) {
            let baseFee = this['DEJZAZGCFBHSBZX'](tender);
            if (!tender) {
                calcBase.baseProgressiveFee = baseFee;
            }
            return calculateUtil.getProgressiveFee(baseFee, '施工场地建设费', projectObj.project.property.progressiveInterval, decimalObj.bills.totalPrice, deficiency);
        },
        //{养护单位(业主)管理费}
        // 使用累进办法计算，计算基数为{定额建筑安装工程（其中定额设备购置费按 40%计）}
        'YHDWYZGLF': function (tender) {
            let baseFee = this['DEJZAZGCSBSS'](tender);
            if (!tender) {
                calcBase.baseProgressiveFee = baseFee;
            }
            return calculateUtil.getProgressiveFee(baseFee, '养护单位(业主)管理费', projectObj.project.property.progressiveInterval, decimalObj.bills.totalPrice, deficiency);
        },
        //{信息化费}
        // 使用累进办法计算,计算基数为{定额建筑安装工程（其中定额设备购置费按 40%计）}
        'XXHF': function (tender) {
            let baseFee = this['DEJZAZGCSBSS'](tender);
            if (!tender) {
                calcBase.baseProgressiveFee = baseFee;
            }
            return calculateUtil.getProgressiveFee(baseFee, '信息化费', projectObj.project.property.progressiveInterval, decimalObj.bills.totalPrice, deficiency);
        },
        //{路线工程监理费}
        //使用累进办法计算，不足2万按2万，计算基数为{定额建筑安装工程（其中定额设备购置费按 40%计）}
        'LXGCJLF': function (tender) {
            let baseFee = this['DEJZAZGCSBSS'](tender);
            if (!tender) {
                calcBase.baseProgressiveFee = baseFee;
            }
            return calculateUtil.getProgressiveFee(baseFee, '路线工程监理费', projectObj.project.property.progressiveInterval, decimalObj.bills.totalPrice, deficiency);
        },
        //{独立桥梁隧道工程监理费}
        //使用累进办法计算，不足2万按2万，计算基数为{定额建筑安装工程（其中定额设备购置费按 40%计）}
        'QLSDGCJLF': function (tender) {
            let baseFee = this['DEJZAZGCSBSS'](tender);
            if (!tender) {
                calcBase.baseProgressiveFee = baseFee;
            }
            return calculateUtil.getProgressiveFee(baseFee, '独立桥梁隧道工程监理费', projectObj.project.property.progressiveInterval, decimalObj.bills.totalPrice, deficiency);
        },
        //{设计文件审查费}
        // 使用累进办法计算，不足3千按3千，计算基数为{定额建筑安装工程（其中定额设备购置费按 40%计）}
        'SJWJSCF': function (tender) {
            let baseFee = this['DEJZAZGCSBSS'](tender);
            if (!tender) {
                calcBase.baseProgressiveFee = baseFee;
            }
            return calculateUtil.getProgressiveFee(baseFee, '设计文件审查费', projectObj.project.property.progressiveInterval, decimalObj.bills.totalPrice, deficiency);
        },
        //{路线勘察设计费}
        // 使用累进办法计算，计算基数为{定额建筑安装工程（其中定额设备购置费按 40%计）}
        'LXKCSJF': function (tender) {
            let baseFee = this['DEJZAZGCSBSS'](tender);
            if (!tender) {
                calcBase.baseProgressiveFee = baseFee;
            }
            return calculateUtil.getProgressiveFee(baseFee, '路线勘察设计费', projectObj.project.property.progressiveInterval, decimalObj.bills.totalPrice, deficiency);
        },
        //{独立桥梁隧道维修加固勘察设计费}
        // 使用累进办法计算，计算基数为{定额建筑安装工程（其中定额设备购置费按 40%计）}
        'QLSDKCSJF': function (tender) {
            let baseFee = this['DEJZAZGCSBSS'](tender);
            if (!tender) {
                calcBase.baseProgressiveFee = baseFee;
            }
            return calculateUtil.getProgressiveFee(baseFee, '独立桥梁隧道维修加固勘察设计费', projectObj.project.property.progressiveInterval, decimalObj.bills.totalPrice, deficiency);
        },
        //{招标代理及标底（最高投标限价）编制费} （招标代理及标底编制费ZBDLJBDBZF）
        // 使用累进办法计算，计算基数为{定额建筑安装工程（其中定额设备购置费按 40%计）}
        'ZBDLJBDBZF': function (tender) {
            let baseFee = this['DEJZAZGCSBSS'](tender);
            if (!tender) {
                calcBase.baseProgressiveFee = baseFee;
            }
            return calculateUtil.getProgressiveFee(baseFee, '招标代理及标底(最高投标限价)编制费', projectObj.project.property.progressiveInterval, decimalObj.bills.totalPrice, deficiency);
        },
        //{价差预备费}
        //以建筑安装工程费为基数
        'JCYBF': function (tender) {
            //建筑安装工程费作为基数
            let installFee = this['JZAZGCF'](tender);
            //年造价增涨
            let costGrowthRate = calcBase.project.property.costGrowthRate ?
                calcBase.project.property.costGrowthRate : 0;
            //增涨计费年限
            let growthPeriod = projectObj.project.property.growthPeriod ?
                calcBase.project.property.growthPeriod : 0;
            //= P * [(1+i)^(n-1) -1]
            return (installFee * (Math.pow(1 + costGrowthRate, growthPeriod - 1) - 1)).toDecimal(decimalObj.bills.totalPrice);
        }
    },
    /*
    * 工程量清单项目(bills of quantities)
    * */
    'boq': {
        //{各章清单合计}
        // 取清单固定类别是“第100章至700章清单”的金额
        'GZQDHJ': function (tender) {
            let feeField = 'common',
                subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
            return cbTools.getBillsFee(calcBase.fixedFlag.ONE_SEVEN_BILLS, feeField, subFeeField);
        },
        //{专项暂定合计}
        // 汇总专项暂定列有值的清单的金额
        'ZXZDHJ': function (tender) {
            let rst = 0,
                feeField = 'common',
                subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
            let billsData = calcBase.project.Bills.datas,
                filterData = billsData.filter(function (data) {
                    return data.specialProvisional;
                });
            for (let data of filterData) {
                if (cbTools.isUnDef(data.feesIndex) || _.isEmpty(data.feesIndex) ||
                    cbTools.isUnDef(data.feesIndex[feeField]) || cbTools.isUnDef(data.feesIndex[feeField][subFeeField])) {
                    continue;
                }
                rst += data.feesIndex[feeField][subFeeField];
            }
            return rst.toDecimal(decimalObj.bills.totalPrice);
        },
        //{100章以外清单合计}
        // 取清单固定清单[第100章至700章清单]的金额，但扣除清单100章下的金额。
        // 如果是固定清单[第100章至700章清单]下100章以外清单引用此基数，要排除自身(目前只允许100章的清单使用，所以暂时不需要此判断)
        'YBZYHQDHJ': function (tender) {
            let oneToSeven = cbTools.findNodeByFlag(fixedFlag.ONE_SEVEN_BILLS);
            if (!oneToSeven) {
                return 0;
            }
            //100-700章固定节点的所有子节点
            let allChildren = [];
            function getChildren(nodes) {
                allChildren = allChildren.concat(nodes);
                for (let node of nodes) {
                    if (node.children.length > 0) {
                        getChildren(node.children);
                    }
                }
            }
            getChildren(oneToSeven.children);
            //扣除的节点：100章的节点[100-200)
            let deductNodes = allChildren.filter(cbTools.withingOneHundred);
            //计算金额
            let fullFeeField = tender ? 'common.tenderTotalFee' : 'common.totalFee';
            return projectObj.project.calcProgram.getTotalFee([oneToSeven], deductNodes, fullFeeField).toDecimal(decimalObj.bills.totalPrice);
        },
        // {定额建安费(不含定额设备购置费)} 汇总非设备类型的定额的定额建安费
        'DEJAFBHDESBGZF': function (tender) {
            const feeField = 'rationCommon';
            const subFeeField = tender ? 'tenderTotalFee' : 'totalFee';
            const rations = projectObj.project.Ration.datas.filter(ration => !(ration.type === rationType.gljRation && ration.subType === gljType.EQUIPMENT));
            const summaryFee = rations.reduce((total, ration) => {
                const fee = cbTools.getFee(ration, feeField, subFeeField);
                return total += fee;
            }, 0);
            return summaryFee.toDecimal(decimalObj.bills.totalPrice);
        }
    }
};

//基数的值不是通过直接引用某清单节点获得的，则该基数的fixedBill为空，如价差、甲供、分包; class:分类，用于基数选择界面分类显示
//基数本身不与清单节点关联、但是其由与清单关联的节点四则运算得到，则拥有字段multiRef: [flags...]
/*
 * 基数的过滤filter ,根据这个配置最终可以转换成清单固定行可使用的相应基数
 * 筛选和过滤由pick决定
 * 控制基数可被哪些清单固定行下的节点使用 //挑选 pick === true
 * 控制基数不可被哪些清单固定行下的节点使用 //过滤 pick === false
 * */
//暂时特殊处理专项费用需要引用{定额建筑安装工程费(其中定额设备购置费按40%计)}等跟专项费用父项有关联的基数：将fixedFlag设置为null
let baseFigureMap = {
    /*
     * 预算项目
     * */
    'budget': {
        //只允许固定类别是“专项费用”、“土地使用及拆迁补偿费“、“养护工程其他费”下的清单使用
        '定额建筑安装工程费(不含定额设备购置费及专项费用)': {
            base: 'DEJZAZGCFBHSBZX',
            fixedFlag: null,
            filter: [fixedFlag.SPECIAL_COST, fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: true
        },
        '定额建筑安装工程费(其中定额设备购置费按40%计)': {
            base: 'DEJZAZGCSBSS',
            fixedFlag: null,
            filter: [fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: true
        },
        //只允许固定类别是“安全生产费”
        '建筑安装工程费(不含安全生产费)': {
            base: 'JZAZGCFBHSC',
            fixedFlag: null,
            filter: [fixedFlag.SAFE_COST],
            pick: true
        },
        //只允许固定类别是“土地使用及拆迁补偿费“、“养护工程其他费”下的清单使用
        '建筑安装工程费(不含设备费)': {
            base: 'JZAZGCFBHSB',
            fixedFlag: fixedFlag.CONSTRUCTION_INSTALL_FEE,
            filter: [fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: true
        },
        //只允许非固定类别是“建筑安装工程费”下的清单引用
        '建筑安装工程费': {
            base: 'JZAZGCF',
            fixedFlag: fixedFlag.CONSTRUCTION_INSTALL_FEE,
            filter: [fixedFlag.CONSTRUCTION_INSTALL_FEE],
            pick: false
        },
        //只允许非固定类别是“建筑安装工程费”、非固定类别是“土地使用及拆迁补偿费”下的清单引用
        '土地使用及拆迁补偿费': {
            base: 'TDSYJCQBCF',
            fixedFlag: fixedFlag.LAND_USED_DEMOLITION,
            filter: [fixedFlag.CONSTRUCTION_INSTALL_FEE, fixedFlag.LAND_USED_DEMOLITION],
            pick: false,
        },
        //只允许非固定类别是“建筑安装工程费”、非固定类别是“土地使用及拆迁补偿费”、非固定类别是“养护工程其他费”下的清单引用
        '养护工程其他费': {
            base: 'YHGCQTF',
            fixedFlag: fixedFlag.MAINTENANCE_EXPENSES,
            filter: [fixedFlag.CONSTRUCTION_INSTALL_FEE, fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: false
        },
        //只允许非固定类别是“建筑安装工程费”、非固定类别是“土地使用及拆迁补偿费”、非固定类别是“养护工程其他费”、非固定类别是“预备费”下的清单引用。
        '预备费': {
            base: 'YBF',
            fixedFlag: fixedFlag.BUDGET_FEE,
            filter: [fixedFlag.CONSTRUCTION_INSTALL_FEE, fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES, fixedFlag.BUDGET_FEE],
            pick: false
        },
        //只允许固定类别是“专项费用”、“土地使用及拆迁补偿费“、“养护工程其他费”下的清单使用
        '施工场地建设费': {
            base: 'SGCDJSF',
            fixedFlag: null,
            filter: [fixedFlag.SPECIAL_COST, fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: true
        },
        //只允许固定类别是“专项费用”、“土地使用及拆迁补偿费“、“养护工程其他费”下的清单使用
        '养护单位(业主)管理费': {
            base: 'YHDWYZGLF',
            fixedFlag: null,
            filter: [fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: true
        },
        //只允许固定类别是“专项费用”、“土地使用及拆迁补偿费“、“养护工程其他费”下的清单使用
        '信息化费': {
            base: 'XXHF',
            fixedFlag: null,
            filter: [fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: true
        },
        //只允许固定类别是“专项费用”、“土地使用及拆迁补偿费“、“养护工程其他费”下的清单使用
        '路线工程监理费': {
            base: 'LXGCJLF',
            fixedFlag: null,
            filter: [fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: true
        },
        //只允许固定类别是“专项费用”、“土地使用及拆迁补偿费“、“养护工程其他费”下的清单使用
        '独立桥梁隧道工程监理费': {
            base: 'QLSDGCJLF',
            fixedFlag: null,
            filter: [ fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: true
        },
        //只允许固定类别是“专项费用”、“土地使用及拆迁补偿费“、“养护工程其他费”下的清单使用
        '设计文件审查费': {
            base: 'SJWJSCF',
            fixedFlag: null,
            filter: [fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: true
        },
        //只允许固定类别是“专项费用”、“土地使用及拆迁补偿费“、“养护工程其他费”下的清单使用
        '路线勘察设计费': {
            base: 'LXKCSJF',
            fixedFlag: null,
            filter: [fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: true
        },
        //只允许固定类别是“专项费用”、“土地使用及拆迁补偿费“、“养护工程其他费”下的清单使用
        '独立桥梁隧道维修加固勘察设计费': {
            base: 'QLSDKCSJF',
            fixedFlag: null,
            filter: [fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: true
        },
        '招标代理及标底(最高投标限价)编制费': {
            base: 'ZBDLJBDBZF',
            fixedFlag: null,
            filter: [fixedFlag.LAND_USED_DEMOLITION, fixedFlag.MAINTENANCE_EXPENSES],
            pick: true
        },
        //只允许固定类别是“价差预备费”的清单使用
        '价差预备费': {
            base: 'JCYBF',
            fixedFlag: fixedFlag.CONSTRUCTION_INSTALL_FEE,
            filter: [fixedFlag.SPREAD_BUDGET_FEE],
            pick: true
        }
    },

    /*
    * 工程量清单项目
    * */
    'boq': {
        //仅允许用于固定类别是“第100章至700章清单”以外的清单
        '各章清单合计': {
            base: 'GZQDHJ',
            fixedFlag: fixedFlag.ONE_SEVEN_BILLS,
            filter: [fixedFlag.ONE_SEVEN_BILLS],
            pick: false
        },
        //仅允许用于固定类别是“第100章至700章清单”以外的清单
        '专项暂定合计': {
            base: 'ZXZDHJ',
            fixedFlag: null,
            filter: [fixedFlag.ONE_SEVEN_BILLS],
            pick: false
        },
        /*
        *  清单固定行[第100章至700章清单]下的[第100章清单]需要允许清单可使用基数{100章以外合计}
        *  因此{100章以外合计}不设置关联的清单固定行
        * */
        //仅允许用于固定类别为“100章清单”引用
        '100章以外清单合计': {
            base: 'YBZYHQDHJ',
            fixedFlag: null,
            filter: [fixedFlag.ONE_HUNDRED_BILLS],
            pick: true
        },
        // 各部分都能用
        '定额建安费(不含定额设备购置费)': {
            base: 'DEJAFBHDESBGZF',
            fixedFlag: null,
            filter: null,
            pick: true
        }
    }
};

//输入式分析器
let cbAnalyzer = {
    standar: function (exp) {
        //去空格
        exp = exp.replace(/\s/g, '');
        //（ to (
        exp = exp.replace(/（/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(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;
        }
    },
    //四则运算合法性,控制不允许重复出现运算符,这里再判断一次,控制行引用只能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.getValidFigures(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) {
        return scMathUtil.getFIDArr(exp);//统一前后端调用方法
    },
    //获取表达式中的中文式
    getCN: function(expr){
        let cnRex = /\d*[\u4e00-\u9fa5]{1,}\({0,}[\u4e00-\u9fa5]{0,}\d*%*[\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 = /[\+,\-,\*,\/]{1}\d+(\.\d+)?%[\u4e00-\u9fa5]{0}/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),
                    oprtor = percents[i].replace(`${percentNum}%`, '');
                if(cbTools.isDef(percentNum) && percentNum.length === 1){
                    exp = exp.replace(new RegExp(`\\${percents[i]}`, 'g'), `${oprtor}${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);
        }
        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;
        }
        if (calcBase.project.property.valuationType === 'bill') {//预算
            return baseFigureTemplate.budget[calcBase.baseFigures[figure]['base']]();
        } else {//工程量清单
            return baseFigureTemplate.boq[calcBase.baseFigures[figure]['base']]();
        }
    },
    //调价后计算基数
    tenderBase: function (figure) {
        if(figure === 'NONE'){
            return 0;
        }
        if (calcBase.project.property.valuationType === 'bill') {//预算
            return baseFigureTemplate.budget[calcBase.baseFigures[figure]['base']](true);
        } else {//工程量清单
            return baseFigureTemplate.boq[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;
    }
};

let calcBase = {
    // 累进基数中的基准值，报表需要使用这个中间数据，因此需要入库处理，这里作为暂存
    baseProgressiveFee: 0,
    //正在执行计算的节点
    activeNode: null,
    errMsg: '表达式不正确',
    success: false,
    //清单固定行
    fixedFlag: null,
    fixedBills: Object.create(null),
    //清单基数
    baseFigures: Object.create(null),
    //清单固定行可用基数对应 {flag: Number, baseList: Array}
    flagValidBase: 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);
        if (project.property.valuationType === 'bill') {//预算
            me.baseFigures = baseFigureMap.budget;
        } else {//工程量清单
            me.baseFigures = baseFigureMap.boq;
        }
        cbTools.setBaseBills(me.baseFigures, me.fixedBills);
        //设置清单固定行可用基数映射
        cbTools.setValidBaseMapping(me.baseFigures, me.flagValidBase);
    },
    getBase: function (figure) {
        return cbCalctor.base(figure);

    },
    getBaseByClass: function (node) {
        return cbTools.getValidFigures(node);
    },
    calculate: function (node, reCalc = null) {
            let me = calcBase,
            $CBA = cbAnalyzer,
            $CBP = cbParser,
            $CBC = cbCalctor;
        try {
            me.activeNode = node;
            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');
            let tenderCalcBaseValue = eval(tenderCalcExp);
            if(!cbTools.isNum(tenderCalcBaseValue)){
                throw '调价基数计算结果不为数值';
            }

            //存储
            me.success = true;
            node.updateData.calcBase = exp;
            node.updateData.calcBaseValue = parseFloat(calcBaseValue).toDecimal(decimalObj.decimal('totalPrice', node));
            node.updateData.tenderCalcBaseValue = parseFloat(tenderCalcBaseValue).toDecimal(decimalObj.decimal('totalPrice', node));
            // progression来自overwrite里配置的全局变量
            if (typeof progression !== 'undefined' && calculateUtil.isProgressive(exp, progression)) {
                node.updateData.baseProgressiveFee = me.baseProgressiveFee;
            }
            node.changed = true;
        }
        catch (err){
            console.log(err);
            if(typeof err === 'object'){
                err = '表达式不正确'
            }
            if (node) {
                err = `第${node.serialNo() + 1}行${err}`;
            }
            alert(err);
        }
    }
};