/**
 * 标段--台账 模型 单元测试
 *
 * @author Mai
 * @date 2017/2/1
 * @version
 */
'use strict';

const excel = require('node-xlsx');

/*const testNodeData = [
    { ledger_id: 1, ledger_pid: -1, order: 1, level: 1, full_path: '1', code: '1', is_leaf: false },
    { ledger_id: 2, ledger_pid: 1, order: 1, level: 2, full_path: '1.2', code: '1-1', is_leaf: false },
    { ledger_id: 6, ledger_pid: 2, order: 1, level: 3, full_path: '1.2.6', code: '1-1-1', is_leaf: false },
    { ledger_id: 7, ledger_pid: 6, order: 1, level: 4, full_path: '1.2.6.7', code: '202-1', is_leaf: false },
    { ledger_id: 10, ledger_pid: 7, order: 2, level: 5, full_path: '1.2.6.7.10', code: '202-1-a', is_leaf: true },
    { ledger_id: 9, ledger_pid: 7, order: 1, level: 5, full_path: '1.2.6.7.9', code: '202-1-b', is_leaf: true },
    { ledger_id: 8, ledger_pid: 6, order: 2, level: 4, full_path: '1.2.6.8', code: '202-2', is_leaf: false },
    { ledger_id: 11, ledger_pid: 8, order: 1, level: 5, full_path: '1.2.6.8.11', code: '202-2-c', is_leaf: true },
    { ledger_id: 12, ledger_pid: 8, order: 2, level: 5, full_path: '1.2.6.8.12', code: '202-2-e', is_leaf: true },
    { ledger_id: 13, ledger_pid: 2, order: 2, level: 3, full_path: '1.2.13', code: '1-1-2', is_leaf: true },
    { ledger_id: 14, ledger_pid: 2, order: 3, level: 3, full_path: '1.2.14', code: '1-1-3', is_leaf: true },
    { ledger_id: 3, ledger_pid: 1, order: 2, level: 2, full_path: '1.3', code: '1-2', is_leaf: false },
    { ledger_id: 15, ledger_pid: 3, order: 1, level: 3, full_path: '1.3.15', code: '1-2-1', is_leaf: true },
    { ledger_id: 4, ledger_pid: 1, order: 3, level: 2, full_path: '1.4', code: '1-3', is_leaf: false },
    { ledger_id: 16, ledger_pid: 4, order: 1, level: 3, full_path: '1.4.16', code: '1-3-1', is_leaf: true },
    { ledger_id: 5, ledger_pid: 1, order: 4, level: 2, full_path: '1.5', code: '1-4', is_leaf: true },
];*/
const testNodeData = [
    { id: 1, pid: -1, order: 1, level: 1, full_path: '1', code: '1', is_leaf: false },
    { id: 2, pid: 1, order: 1, level: 2, full_path: '1.2', code: '1-1', is_leaf: false },
    { id: 6, pid: 2, order: 1, level: 3, full_path: '1.2.6', code: '1-1-1', is_leaf: false },
    { id: 7, pid: 6, order: 1, level: 4, full_path: '1.2.6.7', code: '202-1', is_leaf: false },
    { id: 10, pid: 7, order: 2, level: 5, full_path: '1.2.6.7.10', code: '202-1-a', is_leaf: true },
    { id: 9, pid: 7, order: 1, level: 5, full_path: '1.2.6.7.9', code: '202-1-b', is_leaf: true },
    { id: 8, pid: 6, order: 2, level: 4, full_path: '1.2.6.8', code: '202-2', is_leaf: false },
    { id: 11, pid: 8, order: 1, level: 5, full_path: '1.2.6.8.11', code: '202-2-c', is_leaf: true },
    { id: 12, pid: 8, order: 2, level: 5, full_path: '1.2.6.8.12', code: '202-2-e', is_leaf: true },
    { id: 13, pid: 2, order: 2, level: 3, full_path: '1.2.13', code: '1-1-2', is_leaf: true },
    { id: 14, pid: 2, order: 3, level: 3, full_path: '1.2.14', code: '1-1-3', is_leaf: true },
    { id: 3, pid: 1, order: 2, level: 2, full_path: '1.3', code: '1-2', is_leaf: false },
    { id: 15, pid: 3, order: 1, level: 3, full_path: '1.3.15', code: '1-2-1', is_leaf: true },
    { id: 4, pid: 1, order: 3, level: 2, full_path: '1.4', code: '1-3', is_leaf: false },
    { id: 16, pid: 4, order: 1, level: 3, full_path: '1.4.16', code: '1-3-1', is_leaf: true },
    { id: 5, pid: 1, order: 4, level: 2, full_path: '1.5', code: '1-4', is_leaf: true },
];
const testTenderId = 3;

const { app, assert } = require('egg-mock/bootstrap');
const findById = function(nodes, Id) {
    const filters = nodes.filter(function(x) {
        return x.ledger_id === Id;
    });
    return filters.length > 0 ? filters[0] : undefined;
};

describe('test/app/service/ledger.test.js', () => {
    // 准备测试数据
    it('clear history test data', function* () {
        const ctx = app.mockContext();
        const result = yield ctx.service.ledger.db.delete(ctx.service.ledger.tableName, { tender_id: testTenderId });
        assert(result.affectedRows >= 0);
    });
    it('add test data(test add)', function* () {
        const ctx = app.mockContext();
        for (const data of testNodeData) {
            data.tender_id = testTenderId;
        }
        //const result = yield ctx.service.ledger.db.insert(ctx.service.ledger.tableName, testNodeData);
        //assert(result.affectedRows === testNodeData.length);
        const result = yield ctx.service.ledger.add(testNodeData, testTenderId);
        assert(result);
        ctx.service.ledger.cache.set('tender_node_maxId:' + testTenderId, 16, 'EX', ctx.app.config.cacheTime);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   ├── 202-1
        │   │   │   ├── 202-1-a
        │   │   │   └── 202-1-b
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-1-3
        │   ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   └── 1-2-1
        ├── 1-3
        │   └── 1-3-1
        └── 1-4
     */

    // 测试R类方法
    it('test getDataByTenderId', function* () {
        const ctx = app.mockContext();

        // 查询前4层节点
        const result1 = yield ctx.service.ledger.getDataByTenderId(testTenderId);
        assert(result1.length === 12);
        // 查询前3层节点
        const result2 = yield ctx.service.ledger.getDataByTenderId(testTenderId, 3);
        assert(result2.length === 10);
    });
    it('test getDataByNodeId', function* () {
        const ctx = app.mockContext();

        // 查询节点202-1
        const node = yield ctx.service.ledger.getDataByNodeId(testTenderId, 7);
        assert(node);
        assert(node.code === '202-1');
        assert(node.full_path === '1.2.6.7');
    });
    it('test getDataByNodeIds', function* () {
        const ctx = app.mockContext();

        // 查询节点202-1-a与201-1-b
        const result = yield ctx.service.ledger.getDataByNodeIds(testTenderId, [10, 9]);
        assert(result.length === 2);

        let node = findById(result, 10);
        assert(node.code === '202-1-a');

        node = findById(result, 9);
        assert(node.full_path === '1.2.6.7.9');
    });
    it('test getDataByIds', function* () {
        const ctx = app.mockContext();

        // 查询节点202-1
        const node = yield ctx.service.ledger.getDataByNodeId(testTenderId, 7);
        const result = yield ctx.service.ledger.getDataByIds([node.id]);
        assert(result.length === 1);
        assert(node.code === result[0].code);
        assert(node.full_path === result[0].full_path);
    });
    it('test getLastChildData', function* () {
        const ctx = app.mockContext();

        // 查询节点202-1最后一个子节点
        const result = yield ctx.service.ledger.getLastChildData(testTenderId, 7);
        assert(result.ledger_id === 10);
        assert(result.full_path === '1.2.6.7.10');
    });
    it('test getDataByParentAndOrder', function* () {
        const ctx = app.mockContext();

        // 查询节点202-1 第1子节点
        const result1 = yield ctx.service.ledger.getDataByParentAndOrder(testTenderId, 8, 1);
        assert(result1.ledger_id === 11);
        assert(result1.full_path === '1.2.6.8.11');

        // 查询节点1-1 第2/3子节点
        const result2 = yield ctx.service.ledger.getDataByParentAndOrder(testTenderId, 2, [2, 3]);
        assert(result2.length === 2);

        let node = findById(result2, 13);
        assert(node);
        assert(node.code === '1-1-2');

        node = findById(result2, 14);
        assert(node);
        assert(node.code === '1-1-3');
    });
    it('test getChildrenByParentId', function* () {
        const ctx = app.mockContext();

        // 查询节点202-1最后一个子节点
        const result = yield ctx.service.ledger.getChildrenByParentId(testTenderId, 8);
        assert(result.length === 2);

        let node = findById(result, 11);
        assert(node);
        assert(node.code === '202-2-c');

        node = findById(result, 12);
        assert(node);
        assert(node.full_path === '1.2.6.8.12');
    });
    it('test getNextsData', function* () {
        const ctx = app.mockContext();

        // 查询节点1-1-1的全部子节点
        const result = yield ctx.service.ledger.getNextsData(testTenderId, 2, 1);
        assert(result.length === 2);

        let node = findById(result, 13);
        assert(node);
        assert(node.code === '1-1-2');

        node = findById(result, 14);
        assert(node);
        assert(node.code === '1-1-3');
    });
    it('test getDataByFullPath', function* () {
        const ctx = app.mockContext();

        // 查询节点202-2及其子节点
        const result = yield ctx.service.ledger.getDataByFullPath(testTenderId, '1.2.6.8%');
        assert(result.length === 3);

        let node = findById(result, 8);
        assert(node);
        assert(node.code === '202-2');

        node = findById(result, 11);
        assert(node);
        assert(node.code === '202-2-c');

        node = findById(result, 12);
        assert(node);
        assert(node.full_path === '1.2.6.8.12');

        // 查询1-1-1的子孙节点
        const result1 = yield ctx.service.ledger.getDataByFullPath(testTenderId, '1.2.6.%');
        assert(result1.length === 6);
    });
    it('test getFullLevelDataByFullPath', function* () {
        const ctx = app.mockContext();

        // 查询202-2-c及其全部父节点
        const result1 = yield ctx.service.ledger.getFullLevelDataByFullPath(testTenderId, '1.2.6.8.11');
        assert(result1.length === 5);

        const result2 = yield ctx.service.ledger.getFullLevelDataByFullPath(testTenderId, ['1.2.6.8.11', '1.2.6.7.9']);
        assert(result2.length === 7);
    });

    // 测试CUD类方法
    // 基本树结构操作
    it('test addNode', function* () {
        const ctx = app.mockContext();
        // 选中1-1-1，插入节点
        const resultData = yield ctx.service.ledger.addNode(testTenderId, 6);
        assert(resultData.create.length === 1);
        assert(resultData.update.length === 2);
        assert(resultData.create[0].is_leaf === 1);
        assert(resultData.create[0].ledger_id === 17);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   ├── 202-1
        │   │   │   ├── 202-1-a
        │   │   │   └── 202-1-b
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-1-3
        │   ├── new
        │   ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   └── 1-2-1
        ├── 1-3
        │   └── 1-3-1
        └── 1-4
     */
    it('test deleteNode', function* () {
        const ctx = app.mockContext();
        // 选中202-1，删除节点
        const resultData = yield ctx.service.ledger.deleteNode(testTenderId, 7);
        assert(resultData.delete.length === 3);
        assert(resultData.update.length === 1);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   ├── new
        │   ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   └── 1-2-1
        ├── 1-3
        │   └── 1-3-1
        └── 1-4
     */
    it('test upMoveNode', function* () {
        const ctx = app.mockContext();
        // 选中202-2-e上移
        const resultData = yield ctx.service.ledger.upMoveNode(testTenderId, 12);
        resultData.update.sort(function(x, y) {
            return x.order - y.order;
        });
        assert(resultData.update.length === 2);
        assert(resultData.update[0].code === '202-2-e');
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   ├── new
        │   ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   └── 1-2-1
        ├── 1-3
        │   └── 1-3-1
        └── 1-4
     */
    it('test downMoveNode', function* () {
        const ctx = app.mockContext();
        // 选中202-2-e下移
        const resultData = yield ctx.service.ledger.downMoveNode(testTenderId, 12);
        resultData.update.sort(function(x, y) {
            return x.order - y.order;
        });
        assert(resultData.update.length === 2);
        assert(resultData.update[0].code === '202-2-c');
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   ├── new
        │   ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   └── 1-2-1
        ├── 1-3
        │   └── 1-3-1
        └── 1-4
     */
    it('test upLevelNode', function* () {
        const ctx = app.mockContext();
        // 选中 1-1-2 升级
        const resultData = yield ctx.service.ledger.upLevelNode(testTenderId, 13);
        assert(resultData);
        assert(resultData.update.length === 6);

        let node = findById(resultData.update, 13);
        assert(node.full_path === '1.13');
        assert(node.ledger_pid === 1);
        assert(!node.is_leaf);

        node = findById(resultData.update, 14);
        assert(node.ledger_pid === 13);
        assert(node.full_path === '1.13.14');

        node = findById(resultData.update, 3);
        assert(node.order === 3);

        node = findById(resultData.update, 4);
        assert(node.order === 4);

        node = findById(resultData.update, 5);
        assert(node.order === 5);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   └── new
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   └── 1-2-1
        ├── 1-3
        │   └── 1-3-1
        └── 1-4
     */
    it('test downLevelNode', function* () {
        const ctx = app.mockContext();
        // 选中1-3 降级
        const resultData = yield ctx.service.ledger.downLevelNode(testTenderId, 4);
        // 1-3/1-3-1/1-4修改
        assert(resultData.update.length === 3);

        let node = findById(resultData.update, 4);
        assert(node.full_path === '1.3.4');
        assert(node.ledger_pid === 3);
        assert(node.level === 3);
        assert(node.order === 2);

        node = findById(resultData.update, 16);
        assert(node.level === 4);
        assert(node.full_path === '1.3.4.16');

        node = findById(resultData.update, 5);
        assert(node.order === 4);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   └── new
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    // 复制整块
    it('test pasteBlock', function* () {
        const ctx = app.mockContext();
        // 选中1-2-1, 粘贴1-1-1和new
        const resultData = yield ctx.service.ledger.pasteBlock(testTenderId, 15, [6, 17]);

        assert(resultData.create.length === 2);
        assert(resultData.create[0].order = 2);
        assert(resultData.create[0].level = 3);
        assert(resultData.create[0].full_path = '1.3.18');

        assert(resultData.create[1].order = 3);
        assert(resultData.create[1].level = 3);
        assert(resultData.create[1].full_path = '1.3.22');

        assert(resultData.update.length === 1);
        assert(resultData.update[0].order = 3);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   └── new
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   ├── new
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    // 增量计算
    it('test updateInfo', function* () {
        const ctx = app.mockContext();

        // 修改new(id=17)的code 为 1-1-4
        const node = yield ctx.service.ledger.getDataByNodeId(testTenderId, 17);
        assert(node);

        const resultData = yield ctx.service.ledger.updateInfo(testTenderId, {
            id: node.id,
            tender_id: node.tender_id,
            ledger_id: node.ledger_id,
            code: '1-1-4',
        });
        assert(resultData.code === '1-1-4');
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   └── 1-1-4
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   ├── new
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    it('test updateInfos', function* () {
        const ctx = app.mockContext();

        // 修改1-1-1(id=18)的code 为 1-2-2、修改new(id=22)的code 为 1-2-3
        const node1 = yield ctx.service.ledger.getDataByNodeId(testTenderId, 18);
        assert(node1);
        const node2 = yield ctx.service.ledger.getDataByNodeId(testTenderId, 22);
        assert(node2);

        const resultData = yield ctx.service.ledger.updateInfos(testTenderId, [{
            id: node1.id,
            tender_id: node1.tender_id,
            ledger_id: node1.ledger_id,
            code: '1-2-2',
        }, {
            id: node2.id,
            tender_id: node2.tender_id,
            ledger_id: node2.ledger_id,
            code: '1-2-3',
        }]);
        assert(resultData.length === 2);
        assert(resultData[0].code === '1-2-2');
        assert(resultData[1].code === '1-2-3');
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   └── 1-1-4
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    it('test updateCalc - update 1', function* () {
        const ctx = app.mockContext();
        // 修改202-2-e(1-1-1下)(id=12)的quantity为2.00000001, unit_price位3.0000005
        const qty = 2.00000001;
        const up = 3.0000005;
        const tp = 6.00000103;
        const node1 = yield ctx.service.ledger.getDataByNodeId(testTenderId, 12);
        assert(node1);
        const resultData = yield ctx.service.ledger.updateCalc(testTenderId, {
            id: node1.id,
            tender_id: node1.tender_id,
            ledger_id: node1.ledger_id,
            quantity: qty,
            unit_price: up
        });
        assert(resultData.length === 5);
        let node = findById(resultData, 12);
        assert(node.total_price.toFixed(8) == tp);
        node = findById(resultData, 8);
        assert(node.total_price.toFixed(8) == tp);
        node = findById(resultData, 6);
        assert(node.total_price.toFixed(8) == tp);
        node = findById(resultData, 2);
        assert(node.total_price.toFixed(8) == tp);
        node = findById(resultData, 1);
        assert(node.total_price.toFixed(8) == tp);
    });
    /* 期望运行结果:
        1                                                   6.00000103
        ├── 1-1                                             6.00000103
        │   ├── 1-1-1                                       6.00000103
        │   │   └── 202-2                                   6.00000103
        │   │       ├── 202-2-c
        │   │       └── 202-2-e     2.00000001  3.0000005   6.00000103
        │   └── 1-1-4
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    it('test updateCalc - update N', function* () {
        const ctx = app.mockContext();
        // 修改202-2-c(1-1-1下)(id=11)的quantity为4.00000025, unit_price为6.0000083
        //    202-2-c(1-2-2下)(id=20)的quantity为2.0000001, unit_price为5.000065
        //    202-2-e(1-2-2下)(id=21)的quantity为8.0000579, unit_price为4.0000086
        const qty = [4.00000025, 2.0000001, 8.0000579];
        const up = [6.0000083, 5.000065, 4.0000086];
        const tp = [24.0000347, 10.0001305, 32.0003004];
        const sum = [30.00003573, 42.0004309, 72.00046663]
        const node1 = yield ctx.service.ledger.getDataByNodeId(testTenderId, 11);
        assert(node1);
        const node2 = yield ctx.service.ledger.getDataByNodeId(testTenderId, 20);
        assert(node2);
        const node3 = yield ctx.service.ledger.getDataByNodeId(testTenderId, 21);
        assert(node3);

        const resultData = yield ctx.service.ledger.updateCalc(testTenderId, [{
            id: node1.id,
            tender_id: node1.tender_id,
            ledger_id: node1.ledger_id,
            quantity: qty[0],
            unit_price: up[0]
        }, {
            id: node2.id,
            tender_id: node2.tender_id,
            ledger_id: node2.ledger_id,
            quantity: qty[1],
            unit_price: up[1]
        }, {
            id: node3.id,
            tender_id: node3.tender_id,
            ledger_id: node3.ledger_id,
            quantity: qty[2],
            unit_price: up[2]
        }]);

        assert(resultData.length === 10);

        let node = findById(resultData, 11);
        assert(node.total_price.toFixed(8) == tp[0]);

        node = findById(resultData, 8);
        assert(node.total_price.toFixed(8) == sum[0]);
        node = findById(resultData, 6);
        assert(node.total_price.toFixed(8) == sum[0]);
        node = findById(resultData, 2);
        assert(node.total_price.toFixed(8) == sum[0]);

        node = findById(resultData, 20);
        assert(node.total_price.toFixed(8) == tp[1]);
        node = findById(resultData, 21);
        assert(node.total_price.toFixed(8) == tp[2]);

        node = findById(resultData, 19);
        assert(node.total_price.toFixed(8) == sum[1]);
        node = findById(resultData, 18);
        assert(node.total_price.toFixed(8) == sum[1]);
        node = findById(resultData, 3);
        assert(node.total_price.toFixed(8) == sum[1]);

        node = findById(resultData, 1);
        assert(node.total_price.toFixed(8) == sum[2]);
    });
    /* 期望运行结果:
        1                                                   72.00046663
        ├── 1-1                                             30.00003573
        │   ├── 1-1-1                                       30.00003573
        │   │   └── 202-2                                   30.00003573
        │   │       ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │   │       └── 202-2-e     2.00000001  3.0000005   6.00000103
        │   └── 1-1-4
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2                                             42.0004309
        │   ├── 1-2-1
        │   ├── 1-2-2                                       42.0004309
        │   │   └── 202-2                                   42.0004309
        │   │       ├── 202-2-c     2.0000001   5.000065    10.0001305
        │   │       └── 202-2-e     8.0000579   4.0000086   32.0003004
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    // 复制整块+实时计算
    it('test pasteBlock - with Increment Calculate', function* () {
        const ctx = app.mockContext();
        // 选中1-1-4, 粘贴202-2(1-1-1下)
        const resultData = yield ctx.service.ledger.pasteBlock(testTenderId, 17, [8]);

        assert(resultData.update.length === 2);

        let node = findById(resultData.update, 2);
        assert(node.total_price.toFixed(8) == 60.00007146);

        node = findById(resultData.update, 1);
        assert(node.total_price.toFixed(8) == 102.00050236);
    });
    /* 期望运行结果:
        1                                                   102.00050236
        ├── 1-1                                             60.00007146
        │   ├── 1-1-1                                       30.00003573
        │   │   └── 202-2                                   30.00003573
        │   │       ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │   │       └── 202-2-e     2.00000001  3.0000005   6.00000103
        │   ├── 1-1-4
        │   └── 202-2                                       30.00003573
        │       ├── 202-2-c         4.00000025  6.0000083   24.0000347
        │       └── 202-2-e         2.00000001  3.0000005   6.00000103
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2                                             42.0004309
        │   ├── 1-2-1
        │   ├── 1-2-2                                       42.0004309
        │   │   └── 202-2                                   42.0004309
        │   │       ├── 202-2-c     2.0000001   5.000065    10.0001305
        │   │       └── 202-2-e     8.0000579   4.0000086   32.0003004
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    // 树结构基本操作+实时计算
    it('test downLevel - with Increment Calculate', function* () {
        const ctx = app.mockContext();
        // 选中202-2(1-1-4后兄弟节点) 降级
        const resultData = yield ctx.service.ledger.downLevelNode(testTenderId, 23);
        assert(resultData.update.length === 4);
        const node = findById(resultData.update, 17);
        assert(node.total_price.toFixed(8) == 30.00003573);
    });
    /* 期望运行结果:
        1                                                   102.00050236
        ├── 1-1                                             60.00007146
        │   ├── 1-1-1                                       30.00003573
        │   │   └── 202-2                                   30.00003573
        │   │       ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │   │       └── 202-2-e     2.00000001  3.0000005   6.00000103
        │   └── 1-1-4                                       30.00003573
        │       └── 202-2                                   30.00003573
        │           ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │           └── 202-2-e     2.00000001  3.0000005   6.00000103
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2                                             42.0004309
        │   ├── 1-2-1
        │   ├── 1-2-2                                       42.0004309
        │   │   └── 202-2                                   42.0004309
        │   │       ├── 202-2-c     2.0000001   5.000065    10.0001305
        │   │       └── 202-2-e     8.0000579   4.0000086   32.0003004
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    it('test upLevel - with Increment Calculate', function* () {
        const ctx = app.mockContext();
        yield ctx.service.ledger.pasteBlock(testTenderId, 23, [23]);
        /* 期望运行结果:
            1                                                   132.00053809
            ├── 1-1                                             90.00009719
            │   ├── 1-1-1                                       30.00003573
            │   │   └── 202-2                                   30.00003573
            │   │       ├── 202-2-c     4.00000025  6.0000083   24.0000347
            │   │       └── 202-2-e     2.00000001  3.0000005   6.00000103
            │   └── 1-1-4                                       60.00007146
            │       ├── 202-2                                   30.00003573
            │       │   ├── 202-2-c     4.00000025  6.0000083   24.0000347
            │       │   └── 202-2-e     2.00000001  3.0000005   6.00000103
            │       └── 202-2                                   30.00003573
            │           ├── 202-2-c     4.00000025  6.0000083   24.0000347
            │           └── 202-2-e     2.00000001  3.0000005   6.00000103
            ├── 1-1-2
            │   └── 1-1-3
            ├── 1-2                                             42.0004309
            │   ├── 1-2-1
            │   ├── 1-2-2                                       42.0004309
            │   │   └── 202-2                                   42.0004309
            │   │       ├── 202-2-c     2.0000001   5.000065    10.0001305
            │   │       └── 202-2-e     8.0000579   4.0000086   32.0003004
            │   ├── 1-2-3
            │   └── 1-3
            │       └── 1-3-1
            └── 1-4
         */
        // 选中202-2-c(1-1-4下)(id=23)升级
        const resultData = yield ctx.service.ledger.upLevelNode(testTenderId, 23);
        assert(resultData.update.length === 7);

        let node = findById(resultData.update, 23);
        assert(node.total_price.toFixed(8) == 60.00007146);
        node = findById(resultData.update, 17);
        assert(node.total_price.toFixed(8) == 0);
    });
    /* 期望运行结果:
        1                                                   132.00053809
        ├── 1-1                                             90.00009719
        │   ├── 1-1-1                                       30.00003573
        │   │   └── 202-2                                   30.00003573
        │   │       ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │   │       └── 202-2-e     2.00000001  3.0000005   6.00000103
        │   ├── 1-1-4
        │   └── 202-2                                       60.00007146
        │       ├── 202-2-c         4.00000025  6.0000083   24.0000347
        │       ├── 202-2-e         2.00000001  3.0000005   6.00000103
        │       └── 202-2                                   30.00003573
        │           ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │           └── 202-2-e     2.00000001  3.0000005   6.00000103
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2                                             42.0004309
        │   ├── 1-2-1
        │   ├── 1-2-2                                       42.0004309
        │   │   └── 202-2                                   42.0004309
        │   │       ├── 202-2-c     2.0000001   5.000065    10.0001305
        │   │       └── 202-2-e     8.0000579   4.0000086   32.0003004
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    it('test deleteNode - with Increment Calculate', function* () {
        const ctx = app.mockContext();

        // 选中202-2-c(1-2-2下)(id=20)，删除节点
        const resultData = yield ctx.service.ledger.deleteNode(testTenderId, 20);
        assert(resultData.delete.length === 1);
        assert(resultData.update.length === 5);

        let node = findById(resultData.update, 21);
        assert(node.order === 1);

        node = findById(resultData.update, 19);
        assert(node.total_price.toFixed(8) == 32.0003004);

        node = findById(resultData.update, 18);
        assert(node.total_price.toFixed(8) == 32.0003004);

        node = findById(resultData.update, 3);
        assert(node.total_price.toFixed(8) == 32.0003004);

        node = findById(resultData.update, 1);
        assert(node.total_price.toFixed(8) == 122.00040759);
    });
    /* 期望运行结果:
        1                                                   122.00040759
        ├── 1-1                                             90.00009719
        │   ├── 1-1-1                                       30.00003573
        │   │   └── 202-2                                   30.00003573
        │   │       ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │   │       └── 202-2-e     2.00000001  3.0000005   6.00000103
        │   ├── 1-1-4
        │   └── 202-2                                       60.00007146
        │       ├── 202-2-c         4.00000025  6.0000083   24.0000347
        │       ├── 202-2-e         2.00000001  3.0000005   6.00000103
        │       └── 202-2                                   30.00003573
        │           ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │           └── 202-2-e     2.00000001  3.0000005   6.00000103
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2                                             32.0003004
        │   ├── 1-2-1
        │   ├── 1-2-2                                       32.0003004
        │   │   └── 202-2                                   32.0003004
        │   │       └── 202-2-e     8.0000579   4.0000086   32.0003004
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    // 从标准库添加数据
    // 检查添加，直接添加为选中节点子节点
    it('test addStdNode', function* () {
        const ctx = app.mockContext();

        // 选中1-1-4
        const selectData = yield ctx.service.ledger.getDataByCondition({ tender_id: testTenderId, code: '1-1-4' });
        assert(selectData);
        // 从标准库中添加1-1-5
        const condition1 = { list_id: 1, code: '1-1-5' };
        const libData1 = yield ctx.service.stdChapter.getDataByCondition(condition1);
        assert(libData1);
        const stdData1 = yield ctx.service.stdChapter.getDataByDataId(1, libData1.chapter_id);
        assert(stdData1);
        assert(stdData1.id === libData1.id);
        const result1 = yield ctx.service.ledger.addStdNode(testTenderId, selectData.ledger_id, stdData1);
        assert(result1);
        assert(result1.create.length === 1);
        assert(result1.update.length === 1);
        // 从标准库中添加101-1
    });
    /* 期望运行结果:
        1                                                   122.00040759
        ├── 1-1                                             90.00009719
        │   ├── 1-1-1                                       30.00003573
        │   │   └── 202-2                                   30.00003573
        │   │       ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │   │       └── 202-2-e     2.00000001  3.0000005   6.00000103
        │   ├── 1-1-4
        │   ├── 1-1-5
        │   └── 202-2                                       60.00007146
        │       ├── 202-2-c         4.00000025  6.0000083   24.0000347
        │       ├── 202-2-e         2.00000001  3.0000005   6.00000103
        │       └── 202-2                                   30.00003573
        │           ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │           └── 202-2-e     2.00000001  3.0000005   6.00000103
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2                                             32.0003004
        │   ├── 1-2-1
        │   ├── 1-2-2                                       32.0003004
        │   │   └── 202-2                                   32.0003004
        │   │       └── 202-2-e     8.0000579   4.0000086   32.0003004
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    it('test addStdNodeWithParent', function* () {
        const ctx = app.mockContext();

        // 从标准库添加1-4-2-1
        const condition1 = { list_id: 1, code: '1-4-2-1'};
        const libData1 = yield ctx.service.stdChapter.getDataByCondition(condition1);
        assert(libData1);
        const stdData1 = yield ctx.service.stdChapter.getDataByDataId(1, libData1.chapter_id);
        assert(stdData1);
        assert(stdData1.id === libData1.id);
        const result1 = yield ctx.service.ledger.addStdNodeWithParent(testTenderId, stdData1, ctx.service.stdChapter);
        assert(result1);
        assert(result1.create.length === 4);
        assert(!result1.update || result1.update.length === 0);
        assert(!result1.expand || result1.expand.length === 0);
        // 从标准库添加1-4-2-1-2
        const condition2 = { list_id: 1, code: '1-4-2-1-2'};
        const libData2 = yield ctx.service.stdChapter.getDataByCondition(condition2);
        assert(libData2);
        const stdData2 = yield ctx.service.stdChapter.getDataByDataId(1, libData2.chapter_id);
        assert(stdData2);
        assert(stdData2.id === libData2.id);
        const result2 = yield ctx.service.ledger.addStdNodeWithParent(testTenderId, stdData2, ctx.service.stdChapter);
        assert(result2);
        assert(result2.create.length === 1);
        assert(result2.update.length === 1);
        assert(result2.update[0].code = '1-4-2-1');
        assert(!result2.update[0].is_leaf);
        assert(result2.expand.length === 4);
        // 从标准库添加1-4-2-1-1
        const condition3 = { list_id: 1, code: '1-4-2-1-1'};
        const libData3 = yield ctx.service.stdChapter.getDataByCondition(condition3);
        assert(libData3);
        const stdData3 = yield ctx.service.stdChapter.getDataByDataId(1, libData3.chapter_id);
        assert(stdData3);
        assert(stdData3.id === libData3.id);
        const result3 = yield ctx.service.ledger.addStdNodeWithParent(testTenderId, stdData3, ctx.service.stdChapter);
        assert(result3);
        assert(result3.create.length === 1);
        assert(result3.create[0].order === 1);
        assert(result3.update.length === 1);
        assert(result3.update[0].code === '1-4-2-1-2');
        assert(result3.update[0].order === 2);
        assert(result3.expand.length === 5);
    });
    /* 期望运行结果:
        1                                                   122.00040759
        ├── 1-1                                             90.00009719
        │   ├── 1-1-1                                       30.00003573
        │   │   └── 202-2                                   30.00003573
        │   │       ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │   │       └── 202-2-e     2.00000001  3.0000005   6.00000103
        │   ├── 1-1-4
        │   ├── 1-1-5
        │   └── 202-2                                       60.00007146
        │       ├── 202-2-c         4.00000025  6.0000083   24.0000347
        │       ├── 202-2-e         2.00000001  3.0000005   6.00000103
        │       └── 202-2                                   30.00003573
        │           ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │           └── 202-2-e     2.00000001  3.0000005   6.00000103
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2                                             32.0003004
        │   ├── 1-2-1
        │   ├── 1-2-2                                       32.0003004
        │   │   └── 202-2                                   32.0003004
        │   │       └── 202-2-e     8.0000579   4.0000086   32.0003004
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
            └── 1-4-2
                └── 1-4-2-1
                    ├── 1-4-2-1-1
                    └── 1-4-2-1-2
     */
    // 批量插入
    it('test batchInsertChild', function* () {
        const ctx = app.mockContext();

        const batchData = [
            {
                name: 'X1',
                children: [
                    {b_code: 401-1, name: 'A1', unit: 'B1', unit_price: 2, quantity: 3},
                    {b_code: 402-1, name: 'A2', unit: 'B2', unit_price: 3, quantity: 4},
                ]
            }, {
                name: 'X2',
                children: [
                    {b_code: 401-1, name: 'A1', unit: 'B1', unit_price: 2, quantity: 3},
                    {b_code: 402-1, name: 'A2', unit: 'B2', unit_price: 3, quantity: 4},
                ]
            }
        ]
        // 选中1-1-3(id=14)
        const result = yield ctx.service.ledger.batchInsertChild(testTenderId, 14, batchData);
        assert(result.create.length === 6);

        assert(result.update.length === 3);
        let node = findById(result.update, 1);
        assert(node.total_price.toFixed(8) == 158.00040759);
        node = findById(result.update, 13);
        assert(node.total_price.toFixed(8) == 36);
        node = findById(result.update, 14);
        assert(node.total_price.toFixed(8) == 36);
    });
    /* 期望运行结果:
        1                                                   158.00040759
        ├── 1-1                                             90.00009719
        │   ├── 1-1-1                                       30.00003573
        │   │   └── 202-2                                   30.00003573
        │   │       ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │   │       └── 202-2-e     2.00000001  3.0000005   6.00000103
        │   ├── 1-1-4
        │   ├── 1-1-5
        │   └── 202-2                                       60.00007146
        │       ├── 202-2-c         4.00000025  6.0000083   24.0000347
        │       ├── 202-2-e         2.00000001  3.0000005   6.00000103
        │       └── 202-2                                   30.00003573
        │           ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │           └── 202-2-e     2.00000001  3.0000005   6.00000103
        ├── 1-1-2                                           36
        │   └── 1-1-3                                       36
        │       ├──     X1                                  18
        │       │   ├── 401-1       2           3           6
        │       │   └── 402-1       3           4           12
        │       └──     X2                                  18
        │           ├── 401-1       2           3           6
        │           └── 402-1       3           4           12
        ├── 1-2                                             32.0003004
        │   ├── 1-2-1
        │   ├── 1-2-2                                       32.0003004
        │   │   └── 202-2                                   32.0003004
        │   │       └── 202-2-e     8.0000579   4.0000086   32.0003004
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
            └── 1-4-2
                └── 1-4-2-1
                    ├── 1-4-2-1-1
                    └── 1-4-2-1-2
     */
    // 批量插入
    it('test batchInsertNext', function* () {
        const ctx = app.mockContext();

        const batchData = [
            {
                name: 'Y1',
                children: [
                    {b_code: 401-1, name: 'A1', unit: 'B1', unit_price: 2, quantity: 3},
                    {b_code: 402-1, name: 'A2', unit: 'B2', unit_price: 3, quantity: 4},
                ]
            }, {
                name: 'Y2',
                children: [
                    {b_code: 401-1, name: 'A1', unit: 'B1', unit_price: 2, quantity: 3},
                    {b_code: 402-1, name: 'A2', unit: 'B2', unit_price: 3, quantity: 4},
                ]
            }
        ]
        // 选中1-1-3(id=14)
        const result = yield ctx.service.ledger.batchInsertNext(testTenderId, 14, batchData);

        assert(result.create.length === 6);

        assert(result.update.length === 2);
        let node = findById(result.update, 1);
        assert(node.total_price.toFixed(8) == 194.00040759);
        node = findById(result.update, 13);
        assert(node.total_price.toFixed(8) == 72);
    });
    /* 期望运行结果:
        1                                                   194.00040759
        ├── 1-1                                             90.00009719
        │   ├── 1-1-1                                       30.00003573
        │   │   └── 202-2                                   30.00003573
        │   │       ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │   │       └── 202-2-e     2.00000001  3.0000005   6.00000103
        │   ├── 1-1-4
        │   ├── 1-1-5
        │   └── 202-2                                       60.00007146
        │       ├── 202-2-c         4.00000025  6.0000083   24.0000347
        │       ├── 202-2-e         2.00000001  3.0000005   6.00000103
        │       └── 202-2                                   30.00003573
        │           ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │           └── 202-2-e     2.00000001  3.0000005   6.00000103
        ├── 1-1-2                                           72
        │   ├── 1-1-3                                       36
        │   │   ├──     X1                                  18
        │   │   │   ├── 401-1       2           3           6
        │   │   │   └── 402-1       3           4           12
        │   │   └──     X2                                  18
        │   │       ├── 401-1       2           3           6
        │   │       └── 402-1       3           4           12
        │   ├──     X1                                      18
        │   │   ├── 401-1           2           3           6
        │   │   └── 402-1           3           4           12
        │   └──     X2                                      18
        │       ├── 401-1           2           3           6
        │       └── 402-1           3           4           12
        ├── 1-2                                             32.0003004
        │   ├── 1-2-1
        │   ├── 1-2-2                                       32.0003004
        │   │   └── 202-2                                   32.0003004
        │   │       └── 202-2-e     8.0000579   4.0000086   32.0003004
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
            └── 1-4-2
                └── 1-4-2-1
                    ├── 1-4-2-1-1
                    └── 1-4-2-1-2
     */

    // 测试统计类方法
    it('test addUpChildren', function* () {
        const ctx = app.mockContext();

        // 统计202-2(id=23)前两个子节点的金额
        const result1 = yield ctx.service.ledger.addUpChildren(testTenderId, 23, 2, '<=');
        assert(result1 && result1.toFixed(8) == 30.00003573);
        // 统计202-2(id=23)后两个子节点的金额
        const result2 = yield ctx.service.ledger.addUpChildren(testTenderId, 23, 2, '>=');
        assert(result2 && result2.toFixed(8) == 36.00003676);
        // 统计202-2(id=23)全部子节点的金额
        const result3 = yield ctx.service.ledger.addUpChildren(testTenderId, 23, 0, '>');
        assert(result3 && result3.toFixed(8) == 60.00007146);
    });

    // it('test ImportExcelData', function* () {
    //     const ctx = app.mockContext();
    //
    //     const file = ctx.app.baseDir + '/test/app/test_file/ledger-upload-test.xls';
    //     const sheets = excel.parse(file);
    //     const result = yield ctx.service.
    // });

    // 小数位数策略示例：
    /*  先加总再保留3位小数:
        1                                        35.585
        ├── 1-1                                  35.585
        │   ├── 1-1-1                            35.585
        │   │   └── 202-2                        35.585
        │   │       ├── 202-2-c     4.25  6.83   29.028(29.0275)
        │   │       └── 202-2-e     2.15  3.05   6.558(6.5575)
        │   └── 1-1-3
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    /*  先保留3位小数再加总:
        1                                        35.586
        ├── 1-1                                  35.586
        │   ├── 1-1-1                            35.586
        │   │   └── 202-2                        35.586
        │   │       ├── 202-2-c     4.25  6.83   29.028(29.0275)
        │   │       └── 202-2-e     2.15  3.05   6.558(6.5575)
        │   └── 1-1-3
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
});
