/**
 * 项目数据模型单元测试
 *
 * @author CaiAoLin
 * @date 2017/11/17
 * @version
 */

'use strict';

const { app, assert } = require('egg-mock/bootstrap');
const tenderConst = require('../../../app/const/tender');
const addData = {
    name: 'test_addTender',
    category: null,
};
const renderbody = {
    name : '测试保存项目信息',
    type: 1,
};
const mockData = {};
let testTenderId;
const _ = require('lodash');
const PayConst = require('../../../app/const/deal_pay.js');

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 tender = yield ctx.service.tender.getDataByCondition({
            name: renderbody.name,
            project_id: ctx.session.sessionProject.id,
        });
        if (tender) {
            const result = yield ctx.service.tender.deleteTenderNoBackup(tender.id);
            assert(result);
        }
    });
    // 新增标段
    it('test addTender', function* () {
        const ctx = app.mockContext(mockData);
        const result = yield ctx.service.tender.add(addData);
        assert(result);
        const data = yield ctx.service.tender.getDataByCondition({
            name: addData.name,
            project_id: ctx.session.sessionProject.id,
        });
        assert(data);
        testTenderId = data.id;

        const ledger = yield ctx.service.ledger.getDataByTenderId(testTenderId, -1);
        assert(ledger.length === 53);

        const payNodes = yield ctx.service.pay.getAllDataByCondition({where: {tid: testTenderId}});
        assert(payNodes.length === 5);
        let payNode = _.find(payNodes, {ptype: PayConst.payType.wc});
        assert(payNode.name === '本期完成计量');
        payNode = _.find(payNodes, {ptype: PayConst.payType.yf});
        assert(payNode.name === '本期应付');
        payNode = _.find(payNodes, {ptype: PayConst.payType.sf});
        assert(payNode.name === '本期实付');
    });
    // 获取标段信息
    it('test getTenderData', function* () {
        const ctx = app.mockContext(mockData);
        const tender = yield ctx.service.tender.getDataById(testTenderId);
        assert(tender.id === testTenderId);
        assert(tender.name === addData.name);
    });
    // 修改标段信息
    it('test save',function* () {
        const ctx = app.mockContext(mockData);
        const result = yield ctx.service.tender.save(renderbody, testTenderId);
        assert(result);
        const data = yield ctx.service.tender.getTender(testTenderId);
        assert(data.name === renderbody.name);
    });
    // 假删除
    it('test deleteTenderById', function* () {
        const ctx = app.mockContext(mockData);
        const result = yield ctx.service.tender.deleteTenderById(testTenderId);
        assert(result);
        const tender = yield ctx.service.tender.getTender(testTenderId);
        assert(tender.status === ctx.service.tender.status.DISABLE);
    });
    // 真删除
    it('test deleteTenderNoBackup', function* () {
        const ctx = app.mockContext(mockData);
        const result = yield ctx.service.tender.deleteTenderNoBackup(testTenderId);
        assert(result);
        let count;

        count = yield ctx.service.tender.count({id: testTenderId});
        assert(count === 0);
        count = yield ctx.service.tenderInfo.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.ledger.count({tender_id: testTenderId});
        assert(count === 0);
        count = yield ctx.service.ledgerAudit.count({tender_id: testTenderId});
        assert(count === 0);
        count = yield app.mysql.count(ctx.service.ledgerAudit.tableName + '_copy', {tender_id: testTenderId});
        assert(count === 0);
        count = yield ctx.service.pos.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.pay.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.stage.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.stageAudit.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.stageBills.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.stagePos.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.stageDetail.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.stagePay.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.change.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.changeAudit.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.changeAuditList.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.changeCompany.count({tid: testTenderId});
        assert(count === 0);
        count = yield ctx.service.changeAtt.count({tid: testTenderId});
        assert(count === 0);
    });

});
