/**
 * Created by Tony on 2018/4/19.
 */

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 rpt_cfg = require('./rpt_cfg');
dbm.connect(process.env.NODE_ENV);

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

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

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

//*/
let userId_Leng = "59cdf14a0034a1000ba52b97"; //小冷User Id 换成_id了
let userId_Dft = userId_Leng;
/*/
 let userId_Dft = "595328da1934dc327cad08eb";
 //*/

let rptCustCfgFacade = require("../../../modules/reports/facade/rpt_cust_cfg_facade");

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('测试 - 用户自定义报表配置: ', function (t) {
    let userIds = [];
    userIds.push(userId_Leng);
    userIds.push('-100');
    rptCustCfgFacade.getCustomizeCfg(userIds).then(function(custCfg) {
        // console.log(custCfg);
        if (custCfg.length > 1) {
            // let rst = null;
            for (let itemCfg of custCfg) {
                let doc = (itemCfg._doc)?itemCfg._doc:itemCfg;
                if (doc.userId === userId_Leng) {
                    // rst = doc;
                    console.log(doc);
                    break;
                }
            }
        }
        // console.log(err);
        // console.log(msg);
        t.pass('pass succeeded!');
        t.end();
    });
});
//*/

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