/**
 * Created by Mai on 2017/7/13.
 */

/*
let rationCalcFields = [
    {
        type: 'baseDirect', code: "1", name: "基价直接工程费",
        dispExpr: "1.1+1.2+1.3+1.4", expression: "at('1.1') + at('1.2') + at('1.3') + at('1.4')", compiledExpr: "",
        statement: "基价人工费+基价材料费+基价机械费+未计价材料费"
    },
    {
        type: 'baseLabour', code: "1.1", name: "基价人工费",
        dispExpr: "1.1.1+1.1.2", expression: "at('1.1.1') + at('1.1.2')", compiledExpr: "",
        statement: "定额基价人工费+定额人工单价(基价)调整"
    },
    {
        type: 'rationBaseLabour', code: "1.1.1", name: "定额基价人工费",
        dispExpr: "定额基价人工费", expression: "定额基价人工费", compiledExpr: "",
        statement: "定额基价人工费"
    },
    {
        type: 'rationAdjustLabour', code: "1.1.2", name: "定额基价人工费（调整后）",
        dispExpr: "定额基价人工费（调整后）", expression: "定额基价人工费（调整后）", compiledExpr: "",
        statement: "定额基价人工费（调整后）"
    },
    {
        type: 'baseMaterial', code: "1.2", name: "基价材料费",
        dispExpr: "定额基价材料费", expression: "定额基价材料费", compiledExpr: "",
        statement: "定额基价材料费"
    },
    {
        type: 'baseMachine', code: "1.3", name: "基价机械费",
        dispExpr: "1.3.1+1.3.2", expression: "at('1.3.1') + at('1.3.2')", compiledExpr: "",
        statement: "定额基价机械费+定额基价机上人工费（调整后）"
    },
    {
        type: 'rationBaseMachine', code: "1.3.1", name: "定额基价机械费",
        dispExpr: "定额基价机械费", expression: "定额基价机械费", compiledExpr: "",
        statement: "定额基价机械费"
    },
    {
        type: 'rationBaseMachineLabour', code: "1.3.1.1", name: "其中：定额基价机上人工费",
        dispExpr: "定额基价机上人工费", expression: "定额基价机上人工费", compiledExpr: "",
        statement: "定额基价机上人工费"
    },
    {
        type: 'rationBaseMachineLabourFixed', code: "1.3.2", name: "定额基价机上人工费（调整后）",
        dispExpr: "定额基价机上人工费（调整后）", expression: "定额基价机上人工费（调整后）", compiledExpr: "",
        statement: "定额基价机上人工费（调整后）"
    },
    {
        type: 'unPriceMaterial', code: "1.4", name: "未计价材料费",
        dispExpr: "主材费+设备费", expression: "主材费+设备费", compiledExpr: "",
        statement: "主材费+设备费"
    },
    {
        type: 'management', code: "2", name: "企业管理费",
        dispExpr: "1.1.1", expression: "at('1.1.1')", compiledExpr: "",
        statement: "定额基价人工费"
    },
    {
        type: 'profit', code: "3", name: "利润",
        dispExpr: "1.1.1", expression: "at('1.1.1')", compiledExpr: "",
        statement: "定额基价人工费"
    },
    {
        type: 'risk', code: "4", name: "风险因素",
        dispExpr: "", expression: "0", compiledExpr: "",
        statement: ""
    },
    {
        type: 'gljDiff', code: "5", name: "人材机价差",
        dispExpr: "5.1+5.2+5.3", expression: "at('5.1') + at('5.2') + at('5.3')", compiledExpr: "",
        statement: "人工费价差+材料费价差+机械费价差"
    },
    {
        type: 'labourDiff', code: "5.1", name: "人工费价差",
        dispExpr: "市场价格人工费-定额基价人工费（调整后）", expression: "市场价格人工费-定额基价人工费（调整后）", compiledExpr: "",
        statement: "市场价格人工费-定额基价人工费（调整后）"
    },
    {
        type: 'materialDiff', code: "5.2", name: "材料费价差",
        dispExpr: "市场价格材料费-定额基价材料费", expression: "budget('材料') - base('材料')", compiledExpr: "",
        statement: "市场价格材料费-定额基价材料费"
    },
    {
        type: 'machineDiff', code: "5.3", name: "机械费价差",
        dispExpr: "市场价格机械费-定额基价机械费-定额基价机上人工费（调整后）", expression: "市场价格机械费-定额基价机械费-定额基价机上人工费（调整后）", compiledExpr: "",
        statement: "市场价格机械费-定额基价机械费-定额基价机上人工费（调整后）"
    },
    {
        type: 'common', code: "6", name: "综合单价",
        dispExpr: "1+2+3+4+5", expression: "at('1') + at('2') + at('3') + at('4') + at('5')", compiledExpr: "",
        statement: "基价直接工程费+企业管理费+利润+风险因素+人材机价差"
    }
];
*/
// let calcEvaluate = function (expr) {
//     let exp = new Expression('');
//     exp.Expression(expr);
//     return exp.Evaluate();
// }

/*let rationCalcFields = [
    {
        type: 'rationBaseLabour', code: "1.1.1", name: "定额基价人工费",
        dispExpr: "定额基价人工费", expression: "定额基价人工费", compiledExpr: "",
        statement: "定额基价人工费"
    },
    {
        type: 'rationAdjustLabour', code: "1.1.2", name: "定额基价人工费（调整后）",
        dispExpr: "定额基价人工费（调整后）", expression: "at('1.1.1')*(1.89-1)", compiledExpr: "",
        statement: "定额基价人工费（调整后）"
    },
    {
        type: 'baseLabour', code: "1.1", name: "基价人工费",
        dispExpr: "1.1.1+1.1.2", expression: "at('1.1.1') + at('1.1.2')", compiledExpr: "",
        statement: "定额基价人工费+定额人工单价(基价)调整"
    },
    {
        type: 'baseMaterial', code: "1.2", name: "基价材料费",
        dispExpr: "定额基价材料费", expression: "定额基价材料费", compiledExpr: "",
        statement: "定额基价材料费"
    },
    {
        type: 'rationBaseMachine', code: "1.3.1", name: "定额基价机械费",
        dispExpr: "定额基价机械费", expression: "定额基价机械费", compiledExpr: "",
        statement: "定额基价机械费"
    },
    {
        type: 'rationBaseMachineLabour', code: "1.3.1.1", name: "其中：定额基价机上人工费",
        dispExpr: "定额基价机上人工费", expression: "定额基价机上人工费", compiledExpr: "",
        statement: "定额基价机上人工费"
    },
    {
        type: 'rationBaseMachineLabourFixed', code: "1.3.2", name: "定额基价机上人工费（调整后）",
        dispExpr: "定额基价机上人工费（调整后）", expression: "定额基价机上人工费*(1.89-1)", compiledExpr: "",
        statement: "定额基价机上人工费（调整后）"
    },
    {
        type: 'baseMachine', code: "1.3", name: "基价机械费",
        dispExpr: "1.3.1+1.3.2", expression: "at('1.3.1') + at('1.3.2')", compiledExpr: "",
        statement: "定额基价机械费+定额基价机上人工费（调整后）"
    },
    {
        type: 'unPriceMaterial', code: "1.4", name: "未计价材料费",
        dispExpr: "主材费+设备费", expression: "主材费+设备费", compiledExpr: "",
        statement: "主材费+设备费"
    },
    {
        type: 'baseDirect', code: "1", name: "基价直接工程费",
        dispExpr: "1.1+1.2+1.3+1.4", expression: "at('1.1') + at('1.2') + at('1.3') + at('1.4')", compiledExpr: "",
        statement: "基价人工费+基价材料费+基价机械费+未计价材料费"
    },
    {
        type: 'management', code: "2", name: "企业管理费", feeRate: 7,
        dispExpr: "1.1.1", expression: "at('1.1.1')", compiledExpr: "",
        statement: "定额基价人工费"
    },
    {
        type: 'profit', code: "3", name: "利润", feeRate: 13,
        dispExpr: "1.1.1", expression: "at('1.1.1')", compiledExpr: "",
        statement: "定额基价人工费"
    },
    {
        type: 'risk', code: "4", name: "风险因素",
        dispExpr: "", expression: "0", compiledExpr: "",
        statement: ""
    },
    {
        type: 'labourDiff', code: "5.1", name: "人工费价差",
        dispExpr: "市场价格人工费-定额基价人工费（调整后）", expression: "人工费价差", compiledExpr: "",
        statement: "市场价格人工费-定额基价人工费（调整后）"
    },
    {
        type: 'materialDiff', code: "5.2", name: "材料费价差",
        dispExpr: "市场价格材料费-定额基价材料费", expression: "材料费价差", compiledExpr: "",
        statement: "市场价格材料费-定额基价材料费"
    },
    {
        type: 'machineDiff', code: "5.3", name: "机械费价差",
        dispExpr: "市场价格机械费-定额基价机械费-定额基价机上人工费（调整后）", expression: "机械费价差", compiledExpr: "",
        statement: "市场价格机械费-定额基价机械费-定额基价机上人工费（调整后）"
    },
    {
        type: 'gljDiff', code: "5", name: "人材机价差",
        dispExpr: "5.1+5.2+5.3", expression: "at('5.1') + at('5.2') + at('5.3')", compiledExpr: "",
        statement: "人工费价差+材料费价差+机械费价差"
    },
    {
        type: 'common', code: "6", name: "综合单价",
        dispExpr: "1+2+3+4+5", expression: "at('1') + at('2') + at('3') + at('4') + at('5')", compiledExpr: "",
        statement: "基价直接工程费+企业管理费+利润+风险因素+人材机价差"
    }
];

let rationCalcObj = {
    calcGljs: [],
    calcFields: null,
    baseFields: rationCalcBase,
    getFee: calcFees.getFee,
    base: function (type) {
        let value = 0;
        for (let glj of this.calcGljs) {
            if (type.indexOf(glj.type) >= 0) {
                value = value + this.getFee(glj, 'quantity') * this.getFee(glj, 'basePrice');
            }
            glj = null;
        }
        return value;
    },
    adjust: function (type) {
        let value = 0;
        for (let glj of this.calcGljs) {
            if (type.indexOf(glj.type) >= 0) {
                value = value + this.getFee(glj, 'quantity') * this.getFee(glj, 'adjustPrice');
            }
            glj = null;
        }
        return value;
    },
    budget: function (type) {
        let value = 0;
        for (let glj of this.calcGljs) {
            if (type.indexOf(glj.type) >= 0) {
                value = value + this.getFee(glj, 'quantity') * this.getFee(glj, 'marketPrice');
            }
            glj = null;
        }
        return value;
    },
    diff: function (type) {
        let value = 0;
        for (let glj of this.calcGljs) {
            if (type.indexOf(glj.type) >= 0) {
                value = value + (this.getFee(glj, 'quantity') * this.getFee(glj, 'marketPrice')).toDecimal(2) - (this.getFee(glj, 'quantity') * this.getFee(glj, 'adjustPrice')).toDecimal(2);
            }
            glj = null;
        }
        return value;
    },
    at: function (calcCode) {
        for (let field of this.calcFields) {
            if (field.code === calcCode) {
                return field.fee;
            }
        }
        return 0;
    },
    initFields: function () {
        for (let field of this.calcFields) {
            field.fee = 0;
        }
    },
    getCalcExpr: function (expression) {
        let calcExpr = expression, reg;
        for (let base of this.baseFields) {
            reg = new RegExp(base.dispName);
            if (reg.test(calcExpr)) {
                let value = 0;
                switch (base.calcType) {
                    case baseCalc:
                        value = this.base(base.gljTypes);
                        break;
                    case adjustCalc:
                        value = this.adjust(base.gljTypes);
                        break;
                    case budgetCalc:
                        value = this.budget(base.gljTypes);
                        break;
                    case diffCalc:
                        value = this.diff(base.gljTypes);
                        break;
                    case offerCalc:
                        value = this.offer(base.gljTypes);
                        break;
                }
                calcExpr = calcExpr.replace(reg, value);
            }
            base = null;
        }
        reg = null;
        let atReg = /at\('([0-9\.]+)'\)/i, strs;
        while (strs = calcExpr.match(atReg)) {
            calcExpr = calcExpr.replace(strs[0], this.at(strs[1]));
        }
        atReg = null;
        strs = null;
        return calcExpr;
    },
    calculate () {
        let result = {};
        for (let field of this.calcFields) {
            let calcExpr = this.getCalcExpr(field.expression);
            let feeRate = (field.feeRate && field.feeRate !== 0) ? field.feeRate : 1;
            field.fee = (calcEvaluate(calcExpr) * feeRate).toDecimal(2);
            result[field.type] = field.fee;
        }
        return result;
    }
};*/

/*class RationCalcHelper {
    constructor (project) {
        this.project = project;
    }

    calculate (ration) {
        rationCalcObj.calcGljs = this.project.ration_glj.getGljArrByRation(ration.ID);
        rationCalcObj.calcFields = rationCalcFields;
        calcFees.checkFields(ration, rationCalcFields);
        for (let field of rationCalcFields) {
            let calcExpr = rationCalcObj.getCalcExpr(field.expression);
            let feeRate = (field.feeRate && field.feeRate !== 0) ? field.feeRate : 1;
            field.fee = (calcEvaluate(calcExpr) * feeRate).toDecimal(2);
            ration.feesIndex[field.type].unitFee = field.fee;
            ration.feesIndex[field.type].totalFee = (field.fee * calcFees.getFee(ration, 'quantity')).toDecimal(2);
            calcExpr = null;
        }
    }

    calculateAll () {
        for (let rationData of this.project.Ration.datas) {
            this.calculate(rationData);
        }
    };
}*/
