'use strict';

/**
 * 台账审批流程单元测试
 *
 * @author Mai
 * @date 2018/5/25
 * @version
 */

const { app, assert } = require('egg-mock/bootstrap');
const testTenderId = 3, testProjectId = 17;
const addData = {
    name: 'test_ledger_audit',
    category: null,
};
const mockData = {};
let times = 1;
const auditConst = require('../../../app/const/audit');

describe('test/app/service/ledger_audit.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;
        // 是否存在旧测试数据，存在则清除
        const tenderData = yield ctx.service.tender.getDataByCondition({
            user_id: ctx.session.sessionUser.accountId,
            name: addData.name,
        });
        if (tenderData) {
            const result = yield ctx.service.tender.deleteTenderNoBackup(tenderData.id);
            assert(result);
        }
        // 新建测试标段
        const addResult = yield ctx.service.tender.add(addData);
        assert(addResult);
        // 模拟打开标段
        mockData.tender = {};
        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);
    });
    /**
     * 流程   名称          id
     * 原报   CinYung      11
     */
    // 新增审核人
    it('test addAuditor', function* () {
        const ctx = app.mockContext(mockData);
        // 该测试项目存在4个用户，11/13/14/15
        const result1 = yield ctx.service.ledgerAudit.addAuditor(ctx.tender.id, 14, ctx.tender.data.ledger_times);
        assert(result1);
        const result2 = yield ctx.service.ledgerAudit.addAuditor(ctx.tender.id, 15, ctx.tender.data.ledger_times);
        assert(result2);
    });
    /**
     * 流程   名称          id
     * 原报   CinYung      11
     * 1审    陈特         14
     * 2审    付青青       15
     */
    // 查询审核人
    it('test getAuditor', function* () {
        const ctx = app.mockContext(mockData);
        const result = yield ctx.service.ledgerAudit.getAuditor(ctx.tender.id, 14, ctx.tender.data.ledger_times);
        assert(result);
        assert(result.audit_order === 1);
        assert(result.name === '陈特');
    });
    // 获取台账审核人列表
    it('test getAuditors', function* () {
        const ctx = app.mockContext(mockData);
        const result = yield ctx.service.ledgerAudit.getAuditors(ctx.tender.id, ctx.tender.data.ledger_times);
        assert(result);
        assert(result.length === 2);
        assert(result[0].audit_order === 1);
        assert(result[0].name === '陈特');
        assert(result[1].audit_order === 2);
        assert(result[1].name === '付青青');
    });
    // 获取New Order
    it('test getNewOrder', function* () {
        const ctx = app.mockContext(mockData);
        const result = yield ctx.service.ledgerAudit.getNewOrder(ctx.tender.id, ctx.tender.data.ledger_times);
        assert(result === 3);
    });
    it('test deleteAuditor', function* () {
        const ctx = app.mockContext(mockData);
        const result = yield ctx.service.ledgerAudit.deleteAuditor(ctx.tender.id, 14, ctx.tender.data.ledger_times);
        assert(result);
        const auditors = yield ctx.service.ledgerAudit.getAuditors(ctx.tender.id, ctx.tender.data.ledger_times);
        assert(auditors.length === 1);
        assert(auditors[0].audit_order === 1);
        assert(auditors[0].audit_id === 15);
        assert(auditors[0].name === '付青青');
    });
    /**
     * 流程   名称          id
     * 原报   CinYung      11
     * 1审    付青青        15
     */
    // 获取当前审核人 未上报
    it('test getCurAuditor 未上报', function* () {
        const ctx = app.mockContext(mockData);
        let curAuditor = yield ctx.service.ledgerAudit.getCurAuditor(ctx.tender.id, ctx.tender.data.ledger_times);
        assert(curAuditor === null);
    });
    // 上报
    it('test start', function* () {
        const ctx = app.mockContext(mockData);
        const result = yield ctx.service.ledgerAudit.start(ctx.tender.id, ctx.tender.data.ledger_times);
        assert(result);

        const tender = yield ctx.service.tender.getTender(ctx.tender.id);
        assert(tender.ledger_status === auditConst.ledger.status.checking);

        const auditors = yield ctx.service.ledgerAudit.getAuditors(ctx.tender.id, ctx.tender.data.ledger_times);
        assert(auditors);
        assert(auditors.length === 1);
        assert(auditors[0].status === auditConst.ledger.status.checking);
    });
    // 获取当前审核人 审批中
    it('test getCurAuditor 审批中', function* () {
        const ctx = app.mockContext(mockData);
        let curAuditor = yield ctx.service.ledgerAudit.getCurAuditor(ctx.tender.id, ctx.tender.data.ledger_times);
        assert(curAuditor);
        assert(curAuditor.name === '付青青');
    });
    // 审批退回
    it('test check 审批退回', function* () {
        const ctx = app.mockContext(mockData);
        yield ctx.service.ledgerAudit.check(ctx.tender.id, auditConst.ledger.status.checkNo, '审批不通过', ctx.tender.data.ledger_times);

        const copyData = yield app.mysql.select(ctx.service.ledgerAudit.tableName + '_copy', {where: {tender_id: ctx.tender.id, times: ctx.tender.data.ledger_times}});
        assert(copyData.length === 53);

        const auditors = yield ctx.service.ledgerAudit.getAuditors(ctx.tender.id, ctx.tender.data.ledger_times);
        assert(auditors);
        assert(auditors.length === 1);
        assert(auditors[0].status === auditConst.ledger.status.checkNo);

        mockData.tender.data = yield ctx.service.tender.getTender(ctx.tender.id);
        assert(mockData.tender.data.ledger_times === 1);
        assert(mockData.tender.data.ledger_status === auditConst.ledger.status.checkNo);
    });
    // 获取当前审核人 审批退回
    it('test getCurAuditor 审批退回', function* () {
        const ctx = app.mockContext(mockData);
        let curAuditor = yield ctx.service.ledgerAudit.getCurAuditor(ctx.tender.id, ctx.tender.data.ledger_times);
        assert(curAuditor === null);
    });
    it('test start 重新上报', function* () {
        const ctx = app.mockContext(mockData);
        const addRst = yield ctx.service.ledgerAudit.addAuditor(ctx.tender.id, 14, ctx.tender.data.ledger_times);
        assert(addRst);

        const auditors = yield ctx.service.ledgerAudit.getAuditors(ctx.tender.id, ctx.tender.data.ledger_times);
        assert(auditors);
        assert(auditors.length === 2);
        assert(auditors[0].audit_order === 1);
        assert(auditors[0].name === '付青青');
        assert(auditors[1].audit_order === 2);
        assert(auditors[1].name === '陈特');

        const result = yield ctx.service.ledgerAudit.start(ctx.tender.id, ctx.tender.data.ledger_times);
        assert(result);
        const tender = yield ctx.service.tender.getTender(ctx.tender.id);
        assert(tender.ledger_status === auditConst.ledger.status.checking);

        const curAuditor = yield ctx.service.ledgerAudit.getCurAuditor(ctx.tender.id, times);
        assert(curAuditor);
        assert(curAuditor.audit_order === 1);
        assert(curAuditor.name === '付青青');
    });
    it('test check 审批通过', function* () {
        const ctx = app.mockContext(mockData);

        yield ctx.service.ledgerAudit.check(ctx.tender.id, auditConst.ledger.status.checked, '审批通过', ctx.tender.data.ledger_times);

        const tender = yield ctx.service.tender.getTender(ctx.tender.id);
        assert(tender.ledger_status === auditConst.ledger.status.checking);

        const curAuditor = yield ctx.service.ledgerAudit.getCurAuditor(ctx.tender.id, times);
        assert(curAuditor);
        assert(curAuditor.audit_order === 2);
        assert(curAuditor.name === '陈特');
    });
    it('test check 终审 审批通过', function* () {
        const ctx = app.mockContext(mockData);

        yield ctx.service.ledgerAudit.check(ctx.tender.id, auditConst.ledger.status.checked, '审批通过', ctx.tender.data.ledger_times);

        const tender = yield ctx.service.tender.getTender(ctx.tender.id);
        assert(tender.ledger_status === auditConst.ledger.status.checked);

        const curAuditor = yield ctx.service.ledgerAudit.getCurAuditor(ctx.tender.id, times);
        assert(curAuditor === null);
    });
    it('test getCurAuditor 审批完成', function* () {
        const ctx = app.mockContext(mockData);
        let curAuditor = yield ctx.service.ledgerAudit.getCurAuditor(ctx.tender.id, ctx.tender.data.ledger_times);
        assert(curAuditor === null);
    });
});