'use strict';

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

'use strict';

const { app, assert } = require('egg-mock/bootstrap');
const addData = {
    name: 'test_tenderInfo',
    category: null,
};
const mockData = {};
const TenderInfo = require('../../../app/const/tender_info.js');
const _ = require('lodash');

describe('test/app/service/tender.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;
        // 移除测试数据
        const tender1 = yield ctx.service.tender.getDataByCondition({
            name: addData.name,
            project_id: ctx.session.sessionProject.id,
        });
        if (tender1) {
            const tRst = yield ctx.service.tender.db.delete(ctx.service.tender.tableName, {
                id: tender1.id
            });
            assert(tRst.affectedRows === 1);
            const lRst = yield ctx.service.ledger.db.delete(ctx.service.ledger.tableName, {
                tender_id: tender1.id
            });
            assert(lRst.affectedRows === 53);
            const pRst = yield ctx.service.pay.db.delete(ctx.service.pay.tableName, {
                tid: tender1.id
            });
            assert(pRst.affectedRows === 5);
            const iRst = yield ctx.service.tenderInfo.db.delete(ctx.service.tenderInfo.tableName, {
                tid: tender1.id
            });
            assert(iRst.affectedRows === 1);
        }
        // 新增测试用标段
        const result = yield ctx.service.tender.add(addData);
        assert(result);
        const tender = yield ctx.service.tender.getDataByCondition({
            name: addData.name,
            project_id: ctx.session.sessionProject.id,
        });
        mockData.tender = {id: tender.id, data: tender};
    });
    // 移除测试数据
    after(function* () {
        const ctx = app.mockContext(mockData);
        // 移除测试数据
        // const tender = yield ctx.service.tender.getDataByCondition({
        //     name: addData.name,
        //     project_id: ctx.session.sessionProject.id,
        // });
        // if (tender) {
        //     const tRst = yield ctx.service.tender.db.delete(ctx.service.tender.tableName, {
        //         id: tender.id
        //     });
        //     assert(tRst.affectedRows === 1);
        //     const lRst = yield ctx.service.ledger.db.delete(ctx.service.ledger.tableName, {
        //         tender_id: tender.id
        //     });
        //     assert(lRst.affectedRows === 53);
        //     const pRst = yield ctx.service.pay.db.delete(ctx.service.pay.tableName, {
        //         tid: tender.id
        //     });
        //     assert(pRst.affectedRows === 5);
        // }
    });

    it('test getTenderInfo', function* () {
        const ctx = app.mockContext(mockData);
        mockData.tender.info = yield ctx.service.tenderInfo.getTenderInfo(mockData.tender.id);
        for (const field in TenderInfo.parseInfo) {
            assert(_.isEqual(TenderInfo.defaultInfo[field], mockData.tender.info[field]));
        }
    });

    it('test saveTenderInfo 标段属性',function* () {
        const ctx = app.mockContext(mockData);

        const dealInfo = JSON.parse(JSON.stringify(ctx.tender.info.deal_info));
        dealInfo.buildName = 'testBuildName';
        dealInfo.dealCode = 'testDealCode';
        dealInfo.dealName = 'testDealName';

        const unit = JSON.parse(JSON.stringify(ctx.tender.info.construction_unit));
        unit.build.company = '建设单位';
        unit.build.corporation = '张三';
        unit.build.date = '2019/06/10';
        unit.contract1.company = '承包单位1';
        unit.contract1.corporation = '李四';
        unit.contract1.date = '2019/06/11';
        unit.contract2.company = '承包单位2';
        unit.contract2.corporation = '王五';
        unit.contract2.date = '2019/06/11';
        unit.supervision1.company = '监理单位1';
        unit.supervision1.corporation = '路人甲';
        unit.supervision1.date = '2019/06/12';
        unit.supervision2.company = '监理单位1';
        unit.supervision2.corporation = '友人A';
        unit.supervision2.date = '2019/06/12';

        const param = JSON.parse(JSON.stringify(ctx.tender.info.tech_param));
        param.loadLevel = 1;
        param.loadLength = 3.5;
        param.startPeg = 'K0+000';
        param.endPeg = 'k3+500';
        param.laneCount = 4;
        param.dealPeriod = '2019年3月~2020年3月';
        param.startDate = '2019/3/20';
        param.planEndDate = '2020/3/5';
        yield ctx.service.tenderInfo.saveTenderInfo(ctx.tender.id, {
            deal_info: dealInfo,
            construction_unit: unit,
            tech_param: param,
        });
        mockData.tender.info = yield ctx.service.tenderInfo.getTenderInfo(ctx.tender.id);
        assert(_.isEqual(mockData.tender.info.deal_info, dealInfo));
        assert(_.isEqual(mockData.tender.info.construction_unit, unit));
        assert(_.isEqual(mockData.tender.info.tech_param, param));
    });

    it('test saveTenderInfo 清单精度', function* () {
        const ctx = app.mockContext(mockData);

        const precision = JSON.parse(JSON.stringify(ctx.tender.info.precision));
        precision.other.value = 4;
        yield ctx.service.tenderInfo.saveTenderInfo(ctx.tender.id, {precision: precision});
        mockData.tender.info = yield ctx.service.tenderInfo.getTenderInfo(ctx.tender.id);
        assert(mockData.tender.info.precision.other.value === 4);
    });

    it('test saveTenderInfo 合同参数', function* () {
        const ctx = app.mockContext(mockData);

        const param = JSON.parse(JSON.stringify(ctx.tender.info.deal_param));
        param.contractPrice = 20000000;
        param.zanLiePrice = 100000;
        param.startAdvance = 200000;
        param.materialAdvance = 150000;
        yield ctx.service.tenderInfo.saveTenderInfo(ctx.tender.id, {deal_param: param});
        mockData.tender.info = yield ctx.service.tenderInfo.getTenderInfo(ctx.tender.id);
        assert(_.isEqual(mockData.tender.info.deal_param, param));
    });

    it('test saveTenderInfo 小数位数', function* () {
        const ctx = app.mockContext(mockData);

        const decimal = JSON.parse(JSON.stringify(ctx.tender.info.decimal));
        decimal.up = 3;
        decimal.tp = 2;
        decimal.pay = true;
        decimal.payTp = 4;
        yield ctx.service.tenderInfo.saveTenderInfo(ctx.tender.id, {decimal: decimal});
        mockData.tender.info = yield ctx.service.tenderInfo.getTenderInfo(ctx.tender.id);
        assert(_.isEqual(mockData.tender.info.decimal, decimal));
    });

    it('test saveTenderInfo 显示设置', function* () {
        const ctx = app.mockContext(mockData);

        const display = JSON.parse(JSON.stringify(ctx.tender.info.display));
        display.ledger.dgnQty = true;
        yield ctx.service.tenderInfo.saveTenderInfo(ctx.tender.id, {display: display});
        mockData.tender.info = yield ctx.service.tenderInfo.getTenderInfo(ctx.tender.id);
        assert(_.isEqual(mockData.tender.info.display, display));
    });

    it('test saveTenderInfo 章节设置', function* () {
        const ctx = app.mockContext(mockData);

        const chapter = JSON.parse(JSON.stringify(ctx.tender.info.chapter));
        const chapterCode = ['100', '200', '300', '400', '500', '600', '700', '800', '900', '1000', '1100', '1200', '1300'];
        for (const code of chapterCode) {
            const data = _.find(chapter, {code: code});
            data.name = data.name + '1';
        }
        yield ctx.service.tenderInfo.saveTenderInfo(ctx.tender.id, {chapter: chapter});
        mockData.tender.info = yield ctx.service.tenderInfo.getTenderInfo(ctx.tender.id);
        assert(_.isEqual(mockData.tender.info.chapter, chapter));
    });
});