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

const excel = require('js-xlsx');
const _ = require('lodash');
const mockData = {};

/*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 { 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', () => {
    // 准备测试数据
    // 准备测试数据，新增测试标段
    before(function* () {
        const ctx = app.mockContext(mockData);
        // 模拟登录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_ledger',
            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);
        ctx.tender = mockData.tender;

        // 清理旧测试数据
        let result = yield ctx.service.ledger.db.delete(ctx.service.ledger.tableName, { tender_id: ctx.tender.id });
        const posResult = yield ctx.service.pos.db.delete(ctx.service.ledger.tableName, { tender_id: ctx.tender.id });
        assert(result.affectedRows >= 0 && posResult.affectedRows >= 0);
        // 初始化测试基础数据
        for (const data of testNodeData) {
            data.tender_id = ctx.tender.id;
        }
        result = yield ctx.service.ledger.add(testNodeData, ctx.tender.id);
        assert(result);
        ctx.service.ledger.cache.set('tender_node_maxId:' + ctx.tender.id, 16, 'EX', ctx.app.config.cacheTime);
    });
    // 统一校验
    afterEach(function* () {
        const ctx = app.mockContext(mockData);

        const nodes = yield ctx.service.ledger.getDataByTenderId(ctx.tender.id, -1);
        const paths = _.uniqBy(nodes, 'full_path');
        assert(nodes.length === paths.length);
    });
    /* 期望运行结果:
        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(mockData);

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

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

        // 查询节点202-1-a与201-1-b
        const result = yield ctx.service.ledger.getDataByNodeIds(ctx.tender.id, [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(mockData);

        // 查询节点202-1
        const node = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 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(mockData);

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

        // 查询节点202-1 第1子节点
        const result1 = yield ctx.service.ledger.getDataByParentAndOrder(ctx.tender.id, 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(ctx.tender.id, 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(mockData);

        // 查询节点202-1最后一个子节点
        const result = yield ctx.service.ledger.getChildrenByParentId(ctx.tender.id, 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(mockData);

        // 查询节点1-1-1的全部子节点
        const result = yield ctx.service.ledger.getNextsData(ctx.tender.id, 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(mockData);

        // 查询节点202-2及其子节点
        const result = yield ctx.service.ledger.getDataByFullPath(ctx.tender.id, '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(ctx.tender.id, '1.2.6.%');
        assert(result1.length === 6);
    });
    it('test getFullLevelDataByFullPath', function* () {
        const ctx = app.mockContext(mockData);

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

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

    // 测试CUD类方法
    // 基本树结构操作
    it('test addNode', function* () {
        const ctx = app.mockContext(mockData);
        // 选中1-1-1，插入节点
        const resultData = yield ctx.service.ledger.addNode(ctx.tender.id, 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(mockData);
        // 选中202-1，删除节点
        const resultData = yield ctx.service.ledger.deleteNode(ctx.tender.id, 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(mockData);
        // 选中202-2-e上移
        const resultData = yield ctx.service.ledger.upMoveNode(ctx.tender.id, 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(mockData);
        // 选中202-2-e下移
        const resultData = yield ctx.service.ledger.downMoveNode(ctx.tender.id, 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(mockData);
        // 选中 1-1-2 升级
        const resultData = yield ctx.service.ledger.upLevelNode(ctx.tender.id, 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(mockData);
        // 选中1-3 降级
        const resultData = yield ctx.service.ledger.downLevelNode(ctx.tender.id, 4);
        // 1-2/1-3/1-3-1/1-4修改
        assert(resultData.update.length === 4);

        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(mockData);
        // 选中1-2-1, 粘贴1-1-1和new
        const node = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 15);
        const resultData = yield ctx.service.ledger.pasteBlock(ctx.tender.id, 15, [6, 17]);

        const create = _.sortBy(resultData.ledger.create, ['level', 'order']);
        assert(create.length === 5);

        const node1 = _.find(create, {code: '1-1-1'});
        assert(node1.order === 2);
        assert(node1.level === 3);
        assert(node1.full_path === node.full_path.replace('.' + node.ledger_id, '.' + node1.ledger_id));
        const node2 = _.find(create, function (c) {
            return c.ledger_pid === node.ledger_pid && c.ledger_id !== node1.ledger_id;
        });
        assert(node2.order === 3);
        assert(node2.level === 3);
        assert(node2.full_path === node.full_path.replace('.' + node.ledger_id, '.' + node2.ledger_id));

        assert(resultData.ledger.update.length === 1);
        assert(resultData.ledger.update[0].order === 4);

        assert(resultData.pos.length === 0);
    });
    /* 期望运行结果:
        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(mockData);

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

        const resultData = yield ctx.service.ledger.updateInfo(ctx.tender.id, {
            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(mockData);

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

        const resultData = yield ctx.service.ledger.updateInfos(ctx.tender.id, [{
            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);
        let node = _.find(resultData, {id: node1.id});
        assert(node.code === '1-2-2');
        node = _.find(resultData, {id: node2.id});
        assert(node.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(mockData);
        // 计算需使用清单精度、小数位数
        ctx.tender = {id: ctx.tender.id};
        ctx.tender.data = yield ctx.service.tender.getTender(ctx.tender.id);
        ctx.tender.info = yield ctx.service.tenderInfo.getTenderInfo(ctx.tender.id);
        // 修改202-2-e(1-1-1下)(id=12)的quantity为2.00000001, unit_price位3.0000005
        const qty = 2.00000001, fQty = ctx.helper.round(qty, ctx.tender.info.precision.other.value);
        const up = 3.0000005, fUp = ctx.helper.round(up, ctx.tender.info.decimal.up);
        const tp = 6.00000103, fTp = ctx.helper.mul(fQty, fUp);
        const node1 = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 12);
        assert(node1);
        const resultData = yield ctx.service.ledger.updateCalc(ctx.tender.id, {
            id: node1.id,
            tender_id: node1.tender_id,
            ledger_id: node1.ledger_id,
            sgfh_qty: qty,
            unit_price: up
        });
        assert(resultData.update.length === 1);
        let node = findById(resultData.update, 12);
        assert(node.sgfh_qty === fQty);
        assert(node.quantity === fQty);
        assert(node.unit_price === fUp);
        assert(node.sgfh_tp === fTp);
        assert(node.total_price == fTp);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c
        │   │       └── 202-2-e         2           3           6
        │   └── 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(mockData);
        // 计算需使用清单精度、小数位数
        ctx.tender = {id: ctx.tender.id};
        ctx.tender.data = yield ctx.service.tender.getTender(ctx.tender.id);
        ctx.tender.info = yield ctx.service.tenderInfo.getTenderInfo(ctx.tender.id);
        // 修改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 fQty = [], fUp = [], fTp = [];
        for (const q of qty) {
            fQty.push(ctx.helper.round(q, ctx.tender.info.precision.other.value));
        }
        for (const p of up) {
            fUp.push(ctx.helper.round(p, ctx.tender.info.decimal.up));
        }
        for (const i in qty) {
            fTp.push(ctx.helper.mul(fQty[i], fUp[i]));
        }

        const node1 = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 11);
        assert(node1);
        const node2 = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 20);
        assert(node2);
        const node3 = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 21);
        assert(node3);

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

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

        let node = findById(resultData.update, 11);
        assert(node.sgfh_qty === fQty[0]);
        assert(node.quantity === fQty[0]);
        assert(node.unit_price === fUp[0]);
        assert(node.sgfh_tp === fTp[0]);
        assert(node.total_price === fTp[0]);
        node = findById(resultData.update, 20);
        assert(node.sjcl_qty === fQty[1]);
        assert(node.quantity === fQty[1]);
        assert(node.unit_price === fUp[1]);
        assert(node.sjcl_tp === fTp[1]);
        assert(node.total_price === fTp[1]);
        node = findById(resultData.update, 21);
        assert(node.qtcl_qty === fQty[2]);
        assert(node.quantity === fQty[2]);
        assert(node.unit_price === fUp[2]);
        assert(node.qtcl_tp === fTp[2]);
        assert(node.total_price === fTp[2]);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c     4           6           24
        │   │       └── 202-2-e     2           3           6
        │   └── 1-1-4
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       ├── 202-2-c     2           5           10
        │   │       └── 202-2-e     8           4           32
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    // 复制整块+实时计算
    it('test pasteBlock - with Increment Calculate', function* () {
        const ctx = app.mockContext(mockData);
        // 选中1-1-4, 粘贴202-2(1-1-1下)
        const resultData = yield ctx.service.ledger.pasteBlock(ctx.tender.id, 17, [8]);

        assert(resultData.ledger.create.length === 3);
        assert(resultData.ledger.update.length === 0);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c     4           6           24
        │   │       └── 202-2-e     2           3           6
        │   ├── 1-1-4
        │   └── 202-2
        │       ├── 202-2-c         4           6           24
        │       └── 202-2-e         2           3           6
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       ├── 202-2-c     2           5           10
        │   │       └── 202-2-e     8           4           32
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    // 树结构基本操作+实时计算
    it('test downLevel - with Increment Calculate', function* () {
        const ctx = app.mockContext(mockData);
        // 选中202-2(1-1-4后兄弟节点) 降级
        const resultData = yield ctx.service.ledger.downLevelNode(ctx.tender.id, 23);
        assert(resultData.update.length === 4);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c     4           6           24
        │   │       └── 202-2-e     2           3           6
        │   └── 1-1-4
        │       └── 202-2
        │           ├── 202-2-c     4           6           24
        │           └── 202-2-e     2           3           6
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       ├── 202-2-c     2           5           10
        │   │       └── 202-2-e     8           4           32
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    it('test upLevel - with Increment Calculate', function* () {
        const ctx = app.mockContext(mockData);
        yield ctx.service.ledger.pasteBlock(ctx.tender.id, 23, [23]);
        /* 期望运行结果:
            1
            ├── 1-1
            │   ├── 1-1-1
            │   │   └── 202-2
            │   │       ├── 202-2-c     4           6           24
            │   │       └── 202-2-e     2           3           6
            │   └── 1-1-4
            │       ├── 202-2
            │       │   ├── 202-2-c     4           6           24
            │       │   └── 202-2-e     2           3           6
            │       └── 202-2
            │           ├── 202-2-c     4           6           24
            │           └── 202-2-e     2           3           6
            ├── 1-1-2
            │   └── 1-1-3
            ├── 1-2
            │   ├── 1-2-1
            │   ├── 1-2-2
            │   │   └── 202-2
            │   │       ├── 202-2-c     2           5           10
            │   │       └── 202-2-e     8           4           32
            │   ├── 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(ctx.tender.id, 23);
        assert(resultData.update.length === 7);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c     4           6           24
        │   │       └── 202-2-e     2           3           6
        │   ├── 1-1-4
        │   └── 202-2
        │       ├── 202-2-c         4           6           24
        │       ├── 202-2-e         2           3           6
        │       └── 202-2
        │           ├── 202-2-c     4           6           24
        │           └── 202-2-e     2           3           6
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       ├── 202-2-c     2           5           10
        │   │       └── 202-2-e     8           4           32
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    it('test deleteNode - with Increment Calculate', function* () {
        const ctx = app.mockContext(mockData);

        // 选中202-2-c(1-2-2下)(id=20)，删除节点
        const node = yield ctx.service.ledger.getDataByCondition({
            tender_id: ctx.tender.id,
            code: '202-2-c',
            level: 5,
        });
        const resultData = yield ctx.service.ledger.deleteNode(ctx.tender.id, node.ledger_id);
        assert(resultData.delete.length === 1);
        assert(resultData.update.length === 1);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c     4           6           24
        │   │       └── 202-2-e     2           3           6
        │   ├── 1-1-4
        │   └── 202-2
        │       ├── 202-2-c         4           6           24
        │       ├── 202-2-e         2           3           6
        │       └── 202-2
        │           ├── 202-2-c     4           6           24
        │           └── 202-2-e     2           3           6
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       └── 202-2-e     8           4           32
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    // 从标准库添加数据
    // 检查添加，直接添加为选中节点子节点
    it('test addStdNode', function* () {
        const ctx = app.mockContext(mockData);

        // 选中1-1-4
        const selectData = yield ctx.service.ledger.getDataByCondition({ tender_id: ctx.tender.id, 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(ctx.tender.id, selectData.ledger_id, stdData1);
        assert(result1);
        assert(result1.create.length === 1);
        assert(result1.update.length === 1);
        // 从标准库中添加101-1
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 202-2-c     4           6           24
        │   │       └── 202-2-e     2           3           6
        │   ├── 1-1-4
        │   ├── 1-1-5
        │   └── 202-2
        │       ├── 202-2-c         4           6           24
        │       ├── 202-2-e         2           3           6
        │       └── 202-2
        │           ├── 202-2-c     4           6           24
        │           └── 202-2-e     2           3           6
        ├── 1-1-2
        │   └── 1-1-3
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       └── 202-2-e     8           4           32
        │   ├── 1-2-3
        │   └── 1-3
        │       └── 1-3-1
        └── 1-4
     */
    it('test addStdNodeWithParent', function* () {
        const ctx = app.mockContext(mockData);

        // 从标准库添加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(ctx.tender.id, 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(ctx.tender.id, 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);
        // 从标准库添加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(ctx.tender.id, 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);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 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
        │       ├── 202-2-c         4.00000025  6.0000083   24.0000347
        │       ├── 202-2-e         2.00000001  3.0000005   6.00000103
        │       └── 202-2
        │           ├── 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
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       └── 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(mockData);

        const batchData = [
            {
                b_code: 401-1, name: 'A1', unit: 'B1', price: 2,
                pos: [{name: 'X1', quantity: 1}, {name: 'X2', quantity: 2}],
            },
            {
                b_code: 402-1, name: 'A2', unit: 'B2', price: 3,
                pos: [{name: 'X1', quantity: 3}, {name: 'X2', quantity: 4}],
            },
        ];
        // 选中1-1-3(id=14)
        const result = yield ctx.service.ledger.batchInsertChild(ctx.tender.id, 14, batchData);
        assert(result.ledger.create.length === 2);
        let node = _.find(result.ledger.create, {name: 'A1'});
        assert(node.quantity === 3);
        assert(node.total_price === 6);
        node = _.find(result.ledger.create, {name: 'A2'});
        assert(node.quantity === 7);
        assert(node.total_price === 21);

        assert(result.pos.length === 4);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 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
        │       ├── 202-2-c         4.00000025  6.0000083   24.0000347
        │       ├── 202-2-e         2.00000001  3.0000005   6.00000103
        │       └── 202-2
        │           ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │           └── 202-2-e     2.00000001  3.0000005   6.00000103
        ├── 1-1-2
        │   └── 1-1-3
        │       ├── 401-1           2           3           6
        │       │   ├── (pos)X1                 1
        │       │   └── (pos)X1                 2
        │       └── 402-1           3           7           21
        │           ├── (pos)X1                 3
        │           └── (pos)X2                 4
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       └── 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(mockData);

        const batchData = [
            {
                b_code: 403-1, name: 'A3', unit: 'B1', price: 2,
                pos: [{name: 'Y1', quantity: 5}, {name: 'Y2', quantity: 6}],
            },
            {
                b_code: 404-1, name: 'A4', unit: 'B2', price: 3,
                pos: [{name: 'Y1', quantity: 7}, {name: 'Y2', quantity: 8}],
            },
        ];
        // 选中1-1-3(id=14)
        const result = yield ctx.service.ledger.batchInsertNext(ctx.tender.id, 14, batchData);

        assert(result.ledger.create.length === 2);
        let node = _.find(result.ledger.create, {name: 'A3'});
        assert(node.quantity === 11);
        assert(node.total_price === 22);
        node = _.find(result.ledger.create, {name: 'A4'});
        assert(node.quantity === 15);
        assert(node.total_price === 45);

        assert(result.pos.length === 4);
    });
    /* 期望运行结果:
        1
        ├── 1-1
        │   ├── 1-1-1
        │   │   └── 202-2
        │   │       ├── 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
        │       ├── 202-2-c         4.00000025  6.0000083   24.0000347
        │       ├── 202-2-e         2.00000001  3.0000005   6.00000103
        │       └── 202-2
        │           ├── 202-2-c     4.00000025  6.0000083   24.0000347
        │           └── 202-2-e     2.00000001  3.0000005   6.00000103
        ├── 1-1-2
        │   ├── 1-1-3
        │   │   ├── 401-1           2           3           6
        │   │   │   ├── (pos)X1                 1
        │   │   │   └── (pos)X1                 2
        │   │   └── 402-1           3           7           21
        │   │       ├── (pos)X1                 3
        │   │       └── (pos)X2                 4
        │   ├── 403-1               2           11          22
        │   │   ├── (pos)Y1                     5
        │   │   └── (pos)Y2                     6
        │   └── 404-1               3           15          45
        │       ├── (pos)Y1                     7
        │       └── (pos)Y2                     8
        ├── 1-2
        │   ├── 1-2-1
        │   ├── 1-2-2
        │   │   └── 202-2
        │   │       └── 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(mockData);

        // 统计202-2(id=23)前两个子节点的金额
        const result1 = yield ctx.service.ledger.addUpChildren(ctx.tender.id, 23, 2, '<=');
        assert(result1 && result1 === 30);
        // 数据库不再存储父项金额，以下两个查询不能获得实际金额
        // 统计202-2(id=23)后两个子节点的金额
        //const result2 = yield ctx.service.ledger.addUpChildren(ctx.tender.id, 23, 2, '>=');
        //assert(result2 && result2.toFixed(8) == 36.00003676);
        // 统计202-2(id=23)全部子节点的金额
        //const result3 = yield ctx.service.ledger.addUpChildren(ctx.tender.id, 23, 0, '>');
        //assert(result3 && result3.toFixed(8) == 60.00007146);
    });

    // 测试搜索类方法
    it('test search', function* () {
        const ctx = app.mockContext(mockData);

        const result = yield ctx.service.ledger.search(ctx.tender.id, {
            value: app.mysql.escape('%1-3%'),
            operate: 'Like',
            fields: ['code', 'name'],
        });
        assert(result.length === 3);
    });
    it('test searchRange', function* () {
        const ctx = app.mockContext(mockData);
        const result = yield ctx.service.ledger.searchRange(ctx.tender.id, {
            value: app.mysql.escape('%1-3%'),
            operate: 'Like',
            fields: ['code', 'name'],
        }, [{
            type: 'And',
            value: app.mysql.escape('1.13.%'),
            operate: 'Like',
            fields: ['full_path'],
        }]);
        assert(result.length === 1);
        assert(result[0].code === '1-1-3');
    });

    // 测试导入excel 在\lib\analysis_excel.test.js中进行单元测试

    // 小数位数策略示例：
    /*  先加总再保留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
     */
});
