/**
 * Created by Tony on 2017/6/21.
 */
var test = require('tape');
var calcUtil = require('../../public/calc_util');

let dummyFee = [
    {
        "ID" : 1,
        "ParentID" : null,
        "name" : "企业管理费",
        "rate" : null,
        "memo" : null
    },
    {
        "ID" : 2,
        "ParentID" : 1,
        "name" : "建筑工程",
        "rate" : null,
        "memo" : null
    },
    {
        "ID" : 3,
        "ParentID" : 2,
        "name" : "一类工程",
        "rate" : 16.03,
        "memo" : null
    },
    {
        "ID" : 4,
        "ParentID" : 2,
        "name" : "二类工程",
        "rate" : 14.95,
        "memo" : null
    },
    {
        "ID" : 5,
        "ParentID" : 2,
        "name" : "三类工程",
        "rate" : 12.47,
        "memo" : null
    },
    {
        "ID" : 6,
        "ParentID" : 2,
        "name" : "四类工程",
        "rate" : 9.3,
        "memo" : null
    },
    {
        "ID" : 7,
        "ParentID" : 1,
        "name" : "市政工程",
        "rate" : null,
        "memo" : null
    },
    {
        "ID" : 8,
        "ParentID" : 7,
        "name" : "一类工程",
        "rate" : 16.33,
        "memo" : null
    },
    {
        "ID" : 9,
        "ParentID" : 7,
        "name" : "二类工程",
        "rate" : 15,
        "memo" : null
    },
    {
        "ID" : 10,
        "ParentID" : 7,
        "name" : "三类工程",
        "rate" : 12.5,
        "memo" : null
    },
    {
        "ID" : 11,
        "ParentID" : 7,
        "name" : "四类工程",
        "rate" : 9.5,
        "memo" : null
    },
    {
        "ID" : 12,
        "ParentID" : 1,
        "name" : "机械土石方",
        "rate" : 15.5,
        "memo" : null
    },
    {
        "ID" : 13,
        "ParentID" : 1,
        "name" : "仿古建筑工程",
        "rate" : 12,
        "memo" : null
    },
    {
        "ID" : 14,
        "ParentID" : 1,
        "name" : "建筑修缮工程",
        "rate" : 12.47,
        "memo" : null
    },
    {
        "ID" : 15,
        "ParentID" : 1,
        "name" : "炉窑砌筑工程",
        "rate" : null,
        "memo" : null
    },
    {
        "ID" : 16,
        "ParentID" : 15,
        "name" : "一类工程",
        "rate" : 14.25,
        "memo" : null
    },
    {
        "ID" : 17,
        "ParentID" : 15,
        "name" : "二类工程",
        "rate" : 12.47,
        "memo" : null
    },
    {
        "ID" : 18,
        "ParentID" : 15,
        "name" : "三类工程",
        "rate" : 10.8,
        "memo" : null
    },
    {
        "ID" : 19,
        "ParentID" : 15,
        "name" : "四类工程",
        "rate" : null,
        "memo" : null
    },
    {
        "ID" : 20,
        "ParentID" : null,
        "name" : "规费",
        "rate" : null,
        "memo" : null
    },
    {
        "ID" : 21,
        "ParentID" : 20,
        "name" : "建筑工程",
        "rate" : 4.87,
        "memo" : null
    },
    {
        "ID" : 22,
        "ParentID" : 20,
        "name" : "市政工程",
        "rate" : 3.61,
        "memo" : null
    },
    {
        "ID" : 23,
        "ParentID" : 20,
        "name" : "机械土石方",
        "rate" : 2.15,
        "memo" : null
    },
    {
        "ID" : 24,
        "ParentID" : 20,
        "name" : "仿古建筑工程",
        "rate" : 2.84,
        "memo" : null
    },
    {
        "ID" : 25,
        "ParentID" : 20,
        "name" : "建筑修缮工程",
        "rate" : 2.84,
        "memo" : null
    },
    {
        "ID" : 26,
        "ParentID" : 20,
        "name" : "炉窑砌筑工程",
        "rate" : 3.61,
        "memo" : null
    },
    {
        "ID" : 27,
        "ParentID" : null,
        "name" : "利润",
        "rate" : null,
        "memo" : null
    },
    {
        "ID" : 28,
        "ParentID" : 27,
        "name" : "建筑工程",
        "rate" : null,
        "memo" : null
    },
    {
        "ID" : 29,
        "ParentID" : 28,
        "name" : "一类工程",
        "rate" : 8.73,
        "memo" : null
    },
    {
        "ID" : 30,
        "ParentID" : 28,
        "name" : "二类工程",
        "rate" : 6.94,
        "memo" : null
    }
]

let dummyCalcTpl = {
    calType: 3,
    calTypeName: "测试用_重庆",
    compiledSeq: [],
    calcItems: [{
        code: "1",
        name: "基价直接工程费",
        dispExpr: "1.1+1.2+1.3+1.4",
        expression: "@('1.1') + @('1.2') + @('1.3') + @('1.4')",
        compiledExpr: "",
        statement: "基价人工费+基价材料费+基价机械费+未计价材料费"
    },{
        code: "1.1",
        name: "基价人工费",
        dispExpr: "1.1.1+1.1.2",
        expression: "@('1.1.1') + @('1.1.2')",
        compiledExpr: "",
        statement: "定额基价人工费+定额人工单价(基价)调整"
    },{
        code: "1.1.1",
        name: "定额基价人工费",
        dispExpr: "定额基价人工费",
        //expression: "base('定额基价人工费')",
        expression: "base('定额基价人工费').toFixed(2)",
        compiledExpr: "",
        statement: "定额基价人工费"
    },{
        code: "1.1.2",
        name: "定额人工单价(基价)调整",
        dispExpr: "1.1.1*[1.89-1]",
        expression: "@('1.1.1') * fee('3')",
        compiledExpr: "",
        statement: "定额基价人工费*[定额人工单价(基价)调整系数-1]"
    },{
        code: "1.2",
        name: "基价材料费",
        dispExpr: "定额基价材料费",
        expression: "base('定额基价材料费')",
        compiledExpr: "",
        statement: "定额基价材料费"
    },{
        code: "1.3",
        name: "基价机械费",
        dispExpr: "1.3.1+1.3.2",
        expression: "@('1.3.1') + @('1.3.2')",
        compiledExpr: "",
        statement: "定额基价机械费+定额机上人工单价(基价)调整"
    },{
        code: "1.3.1",
        name: "定额基价机械费",
        dispExpr: "定额基价机械费",
        expression: "base('定额基价机械费')",
        compiledExpr: "",
        statement: "定额基价机械费"
    },{
        code: "1.3.1.1",
        name: "其中：定额基价机上人工费",
        dispExpr: "定额基价机上人工费",
        expression: "base('定额基价机上人工费')",
        compiledExpr: "",
        statement: "定额基价机上人工费"
    },{
        code: "1.3.2",
        name: "定额人工单价(基价)调整",
        dispExpr: "1.3.1.1*[1.89-1]",
        expression: "@('1.3.1.1') * fee('30')",
        compiledExpr: "",
        statement: "定额基价人工费*[定额人工单价(基价)调整系数-1]"
    },{
        code: "1.4",
        name: "未计价材料费",
        dispExpr: "主材费+设备费",
        expression: "base('主材费') + base('设备费')",
        compiledExpr: "",
        statement: "主材费+设备费"
    },{
        code: "2",
        name: "企业管理费",
        dispExpr: "1.1.1",
        expression: "@('1.1.1')",
        compiledExpr: "",
        statement: "定额基价人工费"
    },{
        code: "3",
        name: "利润",
        dispExpr: "1.1.1",
        expression: "@('1.1.1')",
        compiledExpr: "",
        statement: "定额基价人工费"
    },{
        code: "4",
        name: "风险因素",
        dispExpr: "",
        expression: "0",
        compiledExpr: "",
        statement: ""
    },{
        code: "5",
        name: "人材机价差",
        dispExpr: "5.1+5.2+5.3",
        expression: "@('5.1') + @('5.2') + @('5.3')",
        compiledExpr: "",
        statement: "人工费价差+材料费价差+机械费价差"
    },{
        code: "5.1",
        name: "人工费价差",
        dispExpr: "信息价或市场价格-调整后的定额人工费(基价)",
        expression: "base('市场价格人工费') - base('定额基价人工费(调整后)')",
        compiledExpr: "",
        statement: "市场价格人工费-调整后的定额人工费(基价)"
    },{
        code: "5.2",
        name: "材料费价差",
        dispExpr: "信息价或市场价格-定额基价材料费",
        expression: "base('市场价格材料费') - base('定额基价材料费(调整后)')",
        compiledExpr: "",
        statement: "市场价格材料费-定额基价材料费"
    },{
        code: "5.3",
        name: "机械费价差",
        dispExpr: "信息价或市场价格-调整后的定额基价机械费(基价)",
        expression: "base('市场价格机械费') - base('定额基价机械费(调整后)')",
        compiledExpr: "",
        statement: "市场价格机械费-调整后的定额基价机械费(基价)"
    },{
        code: "6",
        name: "综合单价",
        dispExpr: "1+2+3+4+5",
        expression: "@('1') + @('2') + @('3') + @('4') + @('5')",
        compiledExpr: "",
        statement: "基价直接工程费+企业管理费+利润+风险因素+人材机价差"
    }
    ]
};

let dummyRation = {
    "sectionId" : 76,
    "ID" : 15,
    "code" : "AA0001",
    "name" : "人工挖土方",
    "unit" : "100m3",
    "basePrice" : 840.84,
    "caption" : "人工挖土方",
    "feeType" : 2,
    "rationGljList" : [
        {
            "glj" : {
                "repositoryId" : 3,
                "ID" : 17,
                "code" : "00010201",
                "name" : "土石方综合工日",
                "specs" : null,
                "unit" : "工日",
                "basePrice" : 22,
                "gljDistType" : "人工",
                "gljType" : 2
            },
            "consumeAmt" : 38.22,
            "proportion" : 0
        },{
            "glj": {
                "repositoryId" : 3,
                "ID" : 68,
                "code" : "85030207",
                "name" : "履带式起重机",
                "specs" : "50t",
                "unit" : "台班",
                "basePrice" : 1194.05,
                "gljDistType" : "机械",
                "gljType" : 64
            },
            "consumeAmt" : 1.22,
            "proportion" : 0
        },{
            "glj": {
                "repositoryId" : 3,
                "ID" : 200,
                "code" : "36290101",
                "name" : "水",
                "specs" : "",
                "unit" : "m3",
                "basePrice" : 2,
                "gljDistType" : "材料",
                "gljType" : 6
            },
            "consumeAmt" : 9.2,
            "proportion" : 0
        }
    ],
    "rationRepId" : 3
}

/*
test('计算式测试', function(t){
    calcUtil.init(dummyCalcTpl, dummyFee);
    calcUtil.compile();
    calcUtil.calculate(dummyRation);
    for (let idx of dummyCalcTpl.compiledSeq) {
        let item = dummyCalcTpl.calcItems[idx];
        console.log('code: ' + item.code + ' | expression: ' + item.compiledExpr +  ' | result: ' + item.execRst);
    }
    t.pass('just pass for calculation initialization!');
    t.end();
})

/*/
let cnt = 100000;
test('极限计算式测试：' + cnt + '次', function(t){
    let dt0 = new Date();
    calcUtil.init(dummyCalcTpl, dummyFee);
    calcUtil.compile();

    let exRlist = [];

    let rStr = JSON.stringify(dummyRation);
    for (let i = 0; i < cnt; i++) {
        exRlist.push(JSON.parse(rStr));
    }

    let dt1 = new Date();
    for (let i = 0; i < cnt; i++) {
        calcUtil.calculate(exRlist[i]);
    }
    let dt2 = new Date();
    console.log("准备对象时间(毫秒): " + (dt1 - dt0));
    console.log("总计算时间(毫秒): " + (dt2 - dt1));
    console.log("总测试时间(毫秒): " + (dt2 - dt0));
    t.pass('just pass for calculation initialization!');
    t.end();
})


//*/