'use strict';

/**
 *
 *
 * @author Mai
 * @date
 * @version
 */

const { app, assert } = require('egg-mock/bootstrap');
const AnalysisExcel = require('../../../app/lib/analysis_excel');
const Xlsx = require('js-xlsx');
const _ = require('lodash');
const mockData = {};

describe('test/app/lib/analysis_excel.test.js', () => {
    // 准备测试数据，新增测试标段
    before(function* () {
        const ctx = app.mockContext();
        // 模拟登录session
        const postData = {
            account: '734406061@qq.com',
            project: 'T201711273363',
            project_password: 'mai654321',
        };
        ctx.session = {};
        const loginResult = yield ctx.service.projectAccount.accountLogin(postData, 2);
        assert(loginResult);
        mockData.session = ctx.session;

        // 模拟打开标段
        mockData.tender = {};
        const addData = {
            name: 'test_analysis_excel',
            category: null,
        };
        const tenderData = yield ctx.service.tender.getDataByCondition({
                user_id: ctx.session.sessionUser.accountId,
                name: addData.name,
            });
        if (tenderData) {
            mockData.tender.data = tenderData;
        } else {
            const addResult = yield ctx.service.tender.add(addData);
            assert(addResult);
            mockData.tender.data = yield ctx.service.tender.getDataByCondition({
                user_id: ctx.session.sessionUser.accountId,
                name: addData.name,
            });
        }
        mockData.tender.id = mockData.tender.data.id;
        mockData.tender.info = yield ctx.service.tenderInfo.getTenderInfo(mockData.tender.id);
    });
    it('analysis Test Data', function* () {
        const ctx = app.mockContext(mockData);

        const analysisExcel = new AnalysisExcel(ctx);
        const testData = {
            rows: [
                ['项目节编号', '清单子目号', '部位明细', '名称', '单位', '清单数量', '设计数量1', '设计数量2', '单价', '合价', '图号', '备注'],
                ['1', null, null, '第一部分 建筑安装工程费', '公路公里'],
                ['1-1', null, null, '临时工程', '公路公里'],
                ['1-1-6', null, null, '临时道路修建、养护与拆除（包括原道路的养护费）'],
                [ null, '103-1', null, '临时道路修建、养护与拆除（包括原道路的养护费）', '总额', 1, null, null, 50000 ],
                [ '1-1-7', null, null, '临时占地' ],
                [ null, '103-2', null, '临时占地', '总额', 1, null, null, 57350 ],
                [ '1-1-8', null, null, '临时供电设施' ],
                [ null, '103-3-a', null, '设施架设、拆除', '总额', 1, null, null, 638406 ],
                [ null, '103-3-b', null, '设施维修', '总额', 1, null, null, 50000 ],
                [ '1-1-9', null, null, '电讯设施的提供、维修与拆除' ],
                [ null, '103-4', null, '电讯设施的提供、维修与拆除', '总额', 1, null, null, 50000 ],
                [ '1-1-10', null, null, '承包人驻地建设' ],
                [ null, '104-1', null, '承包人驻地建设', '总额', 1, null, null, 200000 ],
                [ '1-2', null, null, '路基工程', 'km' ],
                [ '1-2-2', null, null, '挖方', 'm3' ],
                [ '1-2-2-1', null, null, '挖土方', 'm3' ],
                [ '1-2-2-1-1', null, null, '挖路基土方', 'm3' ],
                [ null, '203-1-a', null, ' ', 'm3', null, null, null, 7.53 ],
                [ null, null, 1, 'K0+000-K1+000', null, 11619.34375, null, null, null, null, '第二册S-2-1' ],
                [ null, null, 2, 'K1+000-K1+800', null, 11619.34375, null, null, null, null, '第二册S-2-1' ],
                [ null, null, 3, 'K2+800-K3+004', null, 11619.34375, null, null, null, null, '第二册S-2-1' ],
                [ null, null, 4, 'K3+004-K4+040', null, 11619.34375, null, null, null, null, '第二册S-2-1' ],
                [ null, null, 5, 'K4+040-K5+000', null, 11619.34375, null, null, null, null, '第二册S-2-1' ],
                [ null, null, 6, 'K5+000-K6+000', null, 11619.34375, null, null, null, null, '第二册S-2-1' ],
                [ null, null, 7, 'K6+000-K7+000', null, 11619.34375, null, null, null, null, '第二册S-2-1' ],
                [ null, null, 8, 'K7+000-K7+700', null, 11619.34375, null, null, null, null, '第二册S-2-1' ],
                [ '1-2-2-2', null, null, '挖石方', 'm3' ],
                [ '1-2-2-2-1', null, null, '挖软石', 'm3' ],
                [ null, '203-1-b', null, '挖石方', 'm3', 105.36, null, null, 16.11 ],
                [ '1-3', null, null, '路面工程', 'km' ],
                [ '1-4', null, null, '桥梁涵洞工程', 'km' ],
                [ '1-6', null, null, '隧道工程', 'km' ],
            ],
            range: 'A1:L29',
        };
        const templateData = yield ctx.service.tenderNodeTemplate.getData(true);
        const result = analysisExcel.analysisData(testData, templateData);
        // 检查插入总量
        assert(result.items.length === 72);
        assert(result.roots.length === 6);
        assert(result.pos.length === 8);
        // 检查第一部分子节点排序
        const nodes = result.roots[0].children;
        assert(nodes[nodes.length - 2].code === '1-6');
        assert(nodes[nodes.length - 1].code === '1-10');
        // 检查第203-1-a的数量
        const xmj = result.codeNodes['1-2-2-1-1'];
        const gcl = xmj.children[0];
        assert(gcl.sgfh_qty === 92954.72);
        assert(gcl.quantity === 92954.72);
        // 检查部位明细
        const pos = result.pos[0];
        assert(pos.name === 'K0+000-K1+000');
        assert(pos.sgfh_qty === 11619.34);
        assert(pos.quantity === 11619.34);
    });
    it('Analysis Excel Test Data And Import', function* () {
        const ctx = app.mockContext(mockData);

        const excelFile = app.baseDir  + '/test/app/test_file/导入分项清单EXCEL格式.xls';
        const wb = Xlsx.readFile(excelFile);
        const name = wb.SheetNames[0];
        const sheetData = {
            rows: Xlsx.utils.sheet_to_json(wb.Sheets[name], {header: 1}),
            merge: wb.Sheets[name]["!merges"],
        };

        const analysisExcel = new AnalysisExcel(ctx);
        const templateData = yield ctx.service.tenderNodeTemplate.getData(true);
        const result = analysisExcel.analysisData(sheetData, templateData);
        assert(result.items.length === 216);
        const xmj = result.codeNodes['1-2-3-1-2'];
        const gcl = xmj.children[0];
        assert(gcl.sgfh_qty === 0);
        assert(gcl.quantity === 0);
        yield ctx.service.ledger.importExcel(sheetData);
    });
    it('analysis 渭武12标 Excel', function* () {
        const ctx = app.mockContext(mockData);

        const excelFile = app.baseDir  + '/test/app/test_file/渭武12标.xls';
        const wb = Xlsx.readFile(excelFile);
        const name = wb.SheetNames[0];
        const sheetData = {
            rows: Xlsx.utils.sheet_to_json(wb.Sheets[name], {header: 1}),
            merge: wb.Sheets[name]["!merges"],
        };

        yield ctx.service.ledger.importExcel(sheetData);
        const count = yield ctx.service.ledger.count({
            tender_id: ctx.tender.id
        });
        assert(count === 2867);
        const countPos = yield ctx.service.pos.count({
            tid: ctx.tender.id
        });
        assert(countPos === 21271);
    });
});
