'use strict';

/**
 *
 *
 * @author Mai
 * @date 2018/5/6
 * @version
 */

const { app, assert } = require('egg-mock/bootstrap');
// excel解析
const excel = require('node-xlsx');
const fs = require('fs');

describe('test/app/service/match.test.js', () => {
    it('match bills', function* () {
        const ctx = app.mockContext();

        // 初始化测试用指标模板
        const fileName = app.baseDir + '/test/app/service/test_template.xls';
        const sheets = excel.parse(fileName), templateId = 0;
        yield ctx.service.templateNode.importData(sheets, templateId);

        // 读取清单，并匹配指标
        const fileStream = fs.readFileSync(app.baseDir + '/test/app/service/FS08.json', 'utf8');
        const proj = JSON.parse(fileStream);
        const bills = proj.bills;
        yield ctx.service.match.matchBills(bills, templateId);

        const assertBillsMatch = function (billsCode, nodeName, firstIndexValue) {
            const bill = ctx.helper.findObj(bills, 'code', billsCode);
            assert(bills && bill.match_node > 0);
            const node = ctx.helper.findObj(ctx.service.match.nodes, 'bills_id', bill.n_id);
            assert(node.name === nodeName);
            const indexes = ctx.service.match.indexes.filter(function (i) {
                return i.node_id === node.node_id;
            })
            const firstIndex = ctx.helper.findObj(indexes, 'code', node.code + '-1');
            if (firstIndexValue) {
                assert(firstIndex.value.toFixed(4) == firstIndexValue);
            } else {
                assert(firstIndex.value === firstIndexValue);
            }

        };
        assertBillsMatch('1-1', '临时工程', null);
        assertBillsMatch('1-1-1', '临时道路', 973218.3702);
        assertBillsMatch('1-1-2', '临时道路', 1000075.7324);
        assertBillsMatch('1-1-4', '临时道路', 213610.5923);
        assertBillsMatch('1-1-5', '临时道路', 1650.8891);
        assertBillsMatch('1-1-6', '临时道路', 176719.3297);
        assertBillsMatch('1-1-7', '临时道路', 2614445.5674);
        assertBillsMatch('1-1-8', '临时道路', null);
        assertBillsMatch('1-2', '路基工程', null);
        assert(ctx.service.match.nodes.length === 9);
    });
});