/**
 * 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);
    },
    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.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 = [];
        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 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)){
                    let findChildNodes = [];
                    for(let flag of figureMultiRef){
                        let refNode = this.findBill(flag) ? this.getNodeByID(this.findBill(flag).ID) : null;
                        if(refNode){
                            findChildNodes.push(refNode);
                        }
                    }
                    let childrenNodes = calcTools.getChildrenFormulaNodes(node, formulaNodesArr, findChildNodes);
                    for(let cNode of childrenNodes){
                        ids.push(cNode.data.ID);
                    }
                    rst = rst.concat(childrenNodes);
                }
                else if(this.isDef(bill) && ids.indexOf(bill.ID) === -1){
                    let node = this.getNodeByID(bill.ID);
                    if(this.isDef(node)){
                        ids.push(node.data.ID);
                        rst.push(node);
                    }
                }
            }
            else if(figure.type === 'id'){
                let node = this.getNodeByID(figure.value);
                if(this.isDef(node) && ids.indexOf(node.data.ID) === -1){
                    ids.push(node.data.ID);
                    rst.push(node);
                }
            }
        }
        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];
                }
            }
            /*if(filter.indexOf(baseFigures[figure]['base']) === -1){
                mapObj['CONSTRUCTION_ORGANIZATION'][figure] = baseFigures[figure];
            }
            if(baseFigures[figure]['base'] !== 'QTXMF' && baseFigures[figure]['base'] !== 'SQGCZJ' && baseFigures[figure]['base'] !== 'AQWMSGZXF'){
                mapObj['OTHER'][figure] = baseFigures[figure];
            }
            if(baseFigures[figure]['base'] !== 'GF' && baseFigures[figure]['base'] !== 'SQGCZJ' && baseFigures[figure]['base'] !== 'AQWMSGZXF'){
                mapObj['CHARGE'][figure] = baseFigures[figure];
            }
            if(baseFigures[figure]['base'] !== 'SJ' && baseFigures[figure]['base'] !== 'SQGCZJ'){
                mapObj['TAX'][figure] = baseFigures[figure];
            }
            if(baseFigures[figure]['base'] !== 'SQGCZJ'){
                mapObj['ENGINEERINGCOST'][figure] = baseFigures[figure];
            }
            if(baseFigures[figure]['base'] !== 'SQGCZJ' && baseFigures[figure]['base'] !== 'AQWMSGZXF'){
                mapObj['OTHERS'][figure] = baseFigures[figure];
            }
            if(baseFigures[figure]['base'] === 'SQGCZJ'){
                mapObj['SAFETY_CONSTRUCTION'][figure] = baseFigures[figure];
            }*/
        }
       // mapObj['SAFETY_CONSTRUCTION'] = Object.assign(mapObj['SAFETY_CONSTRUCTION'], mapObj['CONSTRUCTION_ORGANIZATION']);

    },
    /*setBaseFigureClass: function (baseFigures, mapObj) {
        mapObj['CONSTRUCTION_ORGANIZATION'] = Object.create(null);
        mapObj['SAFETY_CONSTRUCTION'] = Object.create(null);
        mapObj['OTHER'] = Object.create(null);
        mapObj['CHARGE'] = Object.create(null);
        mapObj['TAX'] = Object.create(null);
        mapObj['OTHERS'] = Object.create(null);
        mapObj['ENGINEERINGCOST'] = Object.create(null);
        let filter = ['CSXMF', 'ZZCSXMF', 'ZZCSXMDEJJZJGCF', 'ZZCSXMDEJJRGF', 'ZZCSXMDEJJCLF', 'ZZCSXMDEJJJXF', 'QTXMF', 'GF', 'SJ', 'SQGCZJ', 'AQWMSGZXF'];
        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;
                }
            }
            if(filter.indexOf(baseFigures[figure]['base']) === -1){
                mapObj['CONSTRUCTION_ORGANIZATION'][figure] = baseFigures[figure];
            }
            if(baseFigures[figure]['base'] !== 'QTXMF' && baseFigures[figure]['base'] !== 'SQGCZJ' && baseFigures[figure]['base'] !== 'AQWMSGZXF'){
                mapObj['OTHER'][figure] = baseFigures[figure];
            }
            if(baseFigures[figure]['base'] !== 'GF' && baseFigures[figure]['base'] !== 'SQGCZJ' && baseFigures[figure]['base'] !== 'AQWMSGZXF'){
                mapObj['CHARGE'][figure] = baseFigures[figure];
            }
            if(baseFigures[figure]['base'] !== 'SJ' && baseFigures[figure]['base'] !== 'SQGCZJ'){
                mapObj['TAX'][figure] = baseFigures[figure];
            }
            if(baseFigures[figure]['base'] !== 'SQGCZJ'){
                mapObj['ENGINEERINGCOST'][figure] = baseFigures[figure];
            }
            if(baseFigures[figure]['base'] !== 'SQGCZJ' && baseFigures[figure]['base'] !== 'AQWMSGZXF'){
                mapObj['OTHERS'][figure] = baseFigures[figure];
            }
            if(baseFigures[figure]['base'] === 'SQGCZJ'){
                mapObj['SAFETY_CONSTRUCTION'][figure] = baseFigures[figure];
            }
        }
        mapObj['SAFETY_CONSTRUCTION'] = Object.assign(mapObj['SAFETY_CONSTRUCTION'], mapObj['CONSTRUCTION_ORGANIZATION']);

    },*/
    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);
        }
    },
    /*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;
        }
        else if(this.isFlag(node.data) && node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.CONSTRUCTION_ORGANIZATION){
            return calcBase.baseFigureClass.CONSTRUCTION_ORGANIZATION;
        }
        else if(this.isFlag(node.data) && node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.SAFETY_CONSTRUCTION){
            return calcBase.baseFigureClass.SAFETY_CONSTRUCTION;
        }
        else if(this.isFlag(node.data) && node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.OTHER){
            return calcBase.baseFigureClass.OTHER;
        }
        else if(this.isFlag(node.data) && node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.CHARGE){
            return calcBase.baseFigureClass.CHARGE;
        }
        else if(this.isFlag(node.data) && node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.TAX){
            return calcBase.baseFigureClass.TAX;
        }
        else if(this.isFlag(node.data) && node.data.flagsIndex.fixed.flag === calcBase.fixedFlag.ENGINEERINGCOST){
            return calcBase.baseFigureClass.ENGINEERINGCOST;
        }
        else {
            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);
                            }
                        }
                    }

                    //税前工程造价算法用了分部分项、项目措施(已排除)、其他项目、规费里的底层价格，相当于引用了4条固定清单，特殊处理
                   /* if(bases[i]['value'] === '税前工程造价'){
                        let subEngineering = cbTools.findBill(calcBase.fixedFlag.SUB_ENGINERRING),
                            other = cbTools.findBill(calcBase.fixedFlag.OTHER),
                            charge = cbTools.findBill(calcBase.fixedFlag.CHARGE);
                        if(subEngineering){
                            block.push(subEngineering.ID);
                        }
                        if(other){
                            block.push(other.ID);
                        }
                        if(charge){
                            block.push(charge.ID);
                        }
                    }*/
                    else if(cbTools.isDef(calcBase.baseFigures[bases[i]['value']]['fixedBill'])){
                        block.push(calcBase.baseFigures[bases[i]['value']]['fixedBill']['bill']['ID']);
                    }
                }
               /* if(bases[i]['type'] === 'base' && cbTools.isDef(calcBase.baseFigures[bases[i]['value']])
                    && 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);
        } 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;
        };
    }
};

let baseFigureTemplate = {
    'FBFXGCF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.common) && cbTools.isDef(bill.feesIndex.common[totalFeeType]) ? bill.feesIndex.common[totalFeeType] : 0;
    },
    'FBFXDEJJRGF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.labour) && cbTools.isDef(bill.feesIndex.labour[totalFeeType]) ? bill.feesIndex.labour[totalFeeType] : 0;
    },
    'FBFXDEJJCLF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.material) && cbTools.isDef(bill.feesIndex.material[totalFeeType]) ? bill.feesIndex.material[totalFeeType] : 0;
    },
    'FBFXDEJJJXF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.machine) && cbTools.isDef(bill.feesIndex.machine[totalFeeType]) ? bill.feesIndex.machine[totalFeeType] : 0;
    },
    'FBFXTZRGF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.adjustLabour) && cbTools.isDef(bill.feesIndex.adjustLabour[totalFeeType]) ? bill.feesIndex.adjustLabour[totalFeeType] : 0;
    },
    'FBFXTZJSRGF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.adjustMachineLabour) && cbTools.isDef(bill.feesIndex.adjustMachineLabour[totalFeeType]) ? bill.feesIndex.adjustMachineLabour[totalFeeType] : 0;
    },
    'FBFXZCF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.mainMaterial) && cbTools.isDef(bill.feesIndex.mainMaterial[totalFeeType]) ? bill.feesIndex.mainMaterial[totalFeeType] : 0;
    },
    'FBFXSBF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.SUB_ENGINERRING]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.equipment) && cbTools.isDef(bill.feesIndex.equipment[totalFeeType]) ? bill.feesIndex.equipment[totalFeeType] : 0;
    },
    '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) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.MEASURE])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.MEASURE]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.common) && cbTools.isDef(bill.feesIndex.common[totalFeeType]) ? bill.feesIndex.common[totalFeeType] : 0;
    },
    'ZZCSXMF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_ORGANIZATION])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_ORGANIZATION]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.common) && cbTools.isDef(bill.feesIndex.common[totalFeeType]) ? bill.feesIndex.common[totalFeeType] : 0;
    },
    'ZZCSXMDEJJZJGCF': function (tender) {
        return (this['ZZCSXMDEJJRGF'](tender) + this['ZZCSXMDEJJCLF'](tender) + this['ZZCSXMDEJJJXF'](tender)).toDecimal(decimalObj.bills.totalPrice);
    },
    'ZZCSXMDEJJRGF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_ORGANIZATION])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_ORGANIZATION]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.labour) && cbTools.isDef(bill.feesIndex.labour[totalFeeType]) ? bill.feesIndex.labour[totalFeeType] : 0;
    },
    'ZZCSXMDEJJCLF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_ORGANIZATION])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_ORGANIZATION]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.material) && cbTools.isDef(bill.feesIndex.material[totalFeeType]) ? bill.feesIndex.material[totalFeeType] : 0;
    },
    'ZZCSXMDEJJJXF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_ORGANIZATION])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_ORGANIZATION]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.machine) && cbTools.isDef(bill.feesIndex.machine[totalFeeType]) ? bill.feesIndex.machine[totalFeeType] : 0;
    },
    'JSCSXMF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.common) && cbTools.isDef(bill.feesIndex.common[totalFeeType]) ? bill.feesIndex.common[totalFeeType] : 0;
    },
    'JSCSXMDEJJRGF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.labour) && cbTools.isDef(bill.feesIndex.labour[totalFeeType]) ? bill.feesIndex.labour[totalFeeType] : 0;
    },
    'JSCSXMDEJJCLF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.material) && cbTools.isDef(bill.feesIndex.material[totalFeeType]) ? bill.feesIndex.material[totalFeeType] : 0;
    },
    'JSCSXMDEJJJXF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.machine) && cbTools.isDef(bill.feesIndex.machine[totalFeeType]) ? bill.feesIndex.machine[totalFeeType] : 0;
    },
    'JSCSXMTZRGF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.adjustLabour) && cbTools.isDef(bill.feesIndex.adjustLabour[totalFeeType]) ? bill.feesIndex.adjustLabour[totalFeeType] : 0;
    },
    'JSCSXMTZJSRGF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.adjustMachineLabour) && cbTools.isDef(bill.feesIndex.adjustMachineLabour[totalFeeType]) ? bill.feesIndex.adjustMachineLabour[totalFeeType] : 0;
    },
    'JSCSXMZCF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.mainMaterial) && cbTools.isDef(bill.feesIndex.mainMaterial[totalFeeType]) ? bill.feesIndex.mainMaterial[totalFeeType] : 0;
    },
    'JSCSXMSBF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CONSTRUCTION_TECH]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.equipment) && cbTools.isDef(bill.feesIndex.equipment[totalFeeType]) ? bill.feesIndex.equipment[totalFeeType] : 0;
    },
    '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) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.OTHER])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.OTHER]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.common) && cbTools.isDef(bill.feesIndex.common[totalFeeType]) ? bill.feesIndex.common[totalFeeType] : 0;
    },
    'GF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.CHARGE])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.CHARGE]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.common) && cbTools.isDef(bill.feesIndex.common[totalFeeType]) ? bill.feesIndex.common[totalFeeType] : 0;
    },
    'SJ': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.TAX])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.TAX]['bill'];
        if(cbTools.isUnDef(bill)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.common) && cbTools.isDef(bill.feesIndex.common[totalFeeType]) ? bill.feesIndex.common[totalFeeType] : 0;
    },
    'AQWMSGZXF': function (tender) {
        if(cbTools.isUnDef(calcBase.fixedBills[calcBase.fixedFlag.SAFETY_CONSTRUCTION])){
            return 0;
        }
        const totalFeeType = tender ? 'tenderTotalFee' : 'totalFee';
        let bill = calcBase.fixedBills[calcBase.fixedFlag.SAFETY_CONSTRUCTION]['bill'];
        if(cbTools.isUnDef(bill) || cbTools.isUnDef(bill.ID)) return 0;
        if(cbTools.isUnDef(bill.feesIndex) || Object.keys(bill.feesIndex).length === 0) return 0;
        return cbTools.isDef(bill.feesIndex.common) && cbTools.isDef(bill.feesIndex.common[totalFeeType]) ? bill.feesIndex.common[totalFeeType] : 0;
    },
    '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;
        return projectObj.project.calcProgram.getBeforeTaxTotalFee([node], tender);
    },
    '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) : glj.unit_price.market_price;
                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) : glj.unit_price.market_price;
                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) : glj.unit_price.market_price;
                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) : glj.unit_price.market_price;
                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) : glj.unit_price.market_price;
                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) : glj.unit_price.market_price;
                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) : glj.unit_price.market_price;
                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) : glj.unit_price.market_price;
                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) : glj.unit_price.market_price;
                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 () {//甲供定额基价人工费
        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) * glj.unit_price.base_price).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGDEJJCLF': function () {//甲供定额基价材料费
        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) * glj.unit_price.base_price).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGDEJJJXF': function () {//甲供定额基价机械费
        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) * glj.unit_price.base_price).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGRGF': function () {//甲供人工费
        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) * glj.unit_price.market_price).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGCLF': function () {//甲供材料费
        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) * glj.unit_price.market_price).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGJXF': function () {//甲供机械费
        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) * glj.unit_price.market_price).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGZCF': function () {//甲供主材费
        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) * glj.unit_price.base_price).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;
    },
    'JGSBF': function () {//甲供设备费
        let rst = 0;
        let projGljs = calcBase.project.projectGLJ.datas.gljList;
        for(let glj of projGljs){
            if(glj.type === gljType.EQUIPMENT){
                rst = (rst + parseFloat(projectGljObject.getSupplyQuantityByGlj(glj) * glj.unit_price.market_price).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] * glj.unit_price.base_price).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] * glj.unit_price.base_price).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] * glj.unit_price.base_price).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) : glj.unit_price.market_price;
                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) : glj.unit_price.market_price;
                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) : glj.unit_price.market_price;
                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)){
                let marketPrice = tender ? calcBase.project.projectGLJ.getTenderMarketPrice(glj) : glj.unit_price.market_price;
                rst = (rst + parseFloat(glj[quantityType] * marketPrice).toDecimal(decimalObj.bills.totalPrice)).toDecimal(decimalObj.bills.totalPrice);
            }
        }
        return rst;*/
        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] * glj.unit_price.base_price).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) : glj.unit_price.market_price;
                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;
    },
   /* 'ZGCLFFGLJ': function () {//暂估材料费(从工料机汇总表汇总)
        return 0;
    },*/
    '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;
    }
};

let figureClassTemplate = {
    'CONSTRUCTION_ORGANIZATION': {flag: fixedFlag.CONSTRUCTION_ORGANIZATION, filter: ['CSXMF', 'ZZCSXMF', 'ZZCSXMDEJJZJGCF', 'ZZCSXMDEJJRGF', 'ZZCSXMDEJJCLF', 'ZZCSXMDEJJJXF', 'QTXMF', 'GF', 'SJ', 'SQGCZJ', 'AQWMSGZXF']},
    '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']},
    'CHARGE': {flag: fixedFlag.CHARGE, filter: ['GF', 'SQGCZJ', 'AQWMSGZXF']},
    'TAX': {flag: fixedFlag.TAX, filter: ['SJ', 'SQGCZJ']},
    'ENGINEERINGCOST': {flag: fixedFlag.ENGINEERINGCOST, filter: ['SQGCZJ']},
    'OTHERS': {flag: fixedFlag.ENGINEERINGCOST, filter: ['SQGCZJ', 'AQWMSGZXF']},
};

//基数的值不是通过清单节点获得的，则该基数的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;
        }
        //判断合法性和存在性
        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 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);
                        }
                    }
                }
              /*  if(figure.value === '税前工程造价'){
                    //税前工程造价算法在措施项目已排除自身，税前工程造价与措施项目无会造成循环的引用关系
                    let subEngineering = cbTools.findBill(calcBase.fixedFlag.SUB_ENGINERRING),
                        other = cbTools.findBill(calcBase.fixedFlag.OTHER),
                        charge = cbTools.findBill(calcBase.fixedFlag.CHARGE);
                    if(subEngineering){
                        billsIDs.push(subEngineering.ID);
                    }
                    if(other){
                        billsIDs.push(other.ID);
                    }
                    if(charge){
                        billsIDs.push(charge.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)));
            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+/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));
        }
        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;
    },
    //获取表达式中的基数
    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, '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, '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;
    }
};

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;
    },
    //计算
    exec: function () {

    }
};

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');
            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);
        }
    }
};