/**
 * Created by Tony on 2018/3/23.
 */
let test = require('tape');

let config = require("../../../config/config.js");
config.setupDb(process.env.NODE_ENV);
let mongoose = require("mongoose");
let fileUtils = require("../../../modules/common/fileUtils");
let path = require('path');
let dbm = require("../../../config/db/db_manager");
let consts = require('../../../modules/main/models/project_consts');
let projectConsts = consts.projectConst;
dbm.connect(process.env.NODE_ENV);

//统一引用models
fileUtils.getGlobbedFiles('../../../modules/all_models/*.js').forEach(function(modelPath) {
    require(path.resolve(modelPath));
});

//config.setupCache();
let cfgCacheUtil = require("../../../config/cacheCfg");
cfgCacheUtil.setupDftCache();

let fsUtil = require("../../../public/fsUtil");

let prjMdl = require('../../../modules/pm/models/project_model');
let projectDataMdl = require('../../../modules/main/models/project');

let demoPrjId = - 1;

// demoPrjId = 720; //QA: DW3
demoPrjId = 1626; //QA:
//*/
let userId_Leng = "59cdf14a0034a1000ba52b97"; //小冷User Id 换成_id了
let userId_Dft = userId_Leng;
/*/
 let userId_Dft = "595328da1934dc327cad08eb";
 //*/

let fs = require('fs');
//设置Date Format函数
fs.readFile(__dirname.slice(0, __dirname.length - 18) + '/public/web/date_util.js', 'utf8', 'r', function (err, data) {
    eval(data);
});

//*/
test('测试 - 获取project数据: ', function (t) {
    projectDataMdl.getData(demoPrjId, function (err, message, result) {
        if (!err) {
            fsUtil.writeObjToFile(result, "D:/GitHome/ConstructionCost/tmp/ProjectDataFullObject.jsp");
            t.pass('pass succeeded!');
            t.end();
        } else {
            //callback(req, res, err, message, null);
            t.pass('pass failed!');
            t.end();
        }
    });
});
//*/

//*/
test('测试 - 获取project部分数据: ', function (t) {
    let filter = [];
    filter.push(projectConsts.BILLS);
    filter.push(projectConsts.RATION);
    filter.push(projectConsts.RATION_GLJ);
    filter.push(projectConsts.PROJECTGLJ);
    filter.push(projectConsts.FEERATE);
    filter.push(projectConsts.CALC_PROGRAM);
    prjMdl.project.getUserProject(userId_Dft, demoPrjId, function(err, msg, prjObj){
        if (!err) {
            projectDataMdl.getFilterData(demoPrjId, filter, function (results) {
                if (results) {
                    // let newData = [];
                    // for (let item of results) {
                    //     newData.push(JSON.stringify(item));
                    // }
                    // fsUtil.writeArrayToFile(newData, "D:/GitHome/ConstructionCost/tmp/getProjectData_partial.jsp");
                    // fsUtil.writeObjToFile(prjObj, "D:/GitHome/ConstructionCost/tmp/getProjectObjectNew.jsp");
                    fsUtil.writeObjToFile(results, "D:/GitHome/ConstructionCost/tmp/getProjectData_partialNew.jsp");
                    t.pass('pass succeeded!');
                    t.end();
                } else {
                    //callback(req, res, err, message, null);
                    t.pass('get project data failed!');
                    t.end();
                }
            });
        } else {
            t.pass('get project failed!');
            t.end();
        }
    });
});
//*/

test('close the connection', function (t) {
    setTimeout(function () {
        mongoose.disconnect();
        t.pass('closing db connection');
        t.end();
    }, 3000);
    // mongoose.disconnect();
    // t.pass('closing db connection');
    // t.end();
});