/**
 * Created by Tony on 2018/6/12.
 */

let test = require('tape');
import JpcEx from "../../../modules/reports/rpt_component/jpc_ex";
import JV from "../../../modules/reports/rpt_component/jpc_value_define";
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));
});

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

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

let demoPrjId = - 1;
// let demoRptId = 38; //5.2.2表
// let demoRptId = 6; //封面
// let demoRptId = 22; //03
// let demoRptId = 23; //04
// let demoRptId = 26; //07
// let demoRptId = 28; //09
// let demoRptId = 27; //08
// let demoRptId = 24; //05
// let demoRptId = 20; //01
// let demoRptId = 23; //04 综合费率表

// let demoRptId = 56; //24
// let demoRptId = 36; //5.1
// let demoRptId = 46; //5.5测试
// let demoRptId = 49; //5.5
// let demoRptId = 66; //5.4
// let demoRptId = 67; //21-2
// let demoRptId = 31; //21-1
// let demoRptId = 37; //5.2.1 计日工劳务
// let demoRptId = 68; //01-2
// let demoRptId = 71; //21-2
// let demoRptId = 74; //22
// let demoRptId = 83; //02 预算
// let demoRptId = 84; //02 清单
let demoRptId = 108;

let pagesize = "A4";
//288: 11-2表(新)
//279: 04

let userId_Leng = "5c3ffa9aa0a92732f41216e0"; //小冷User Id (养护的)
// let userId_me = "5b6a60b1c4ba33000dd417c0"; //我的
// let userId_HaiZhu = "5b5a66c4a3c23e000dccdd77"; //海珠user id
// demoPrjId = 720; //QA: DW3
//demoPrjId = 1626; //QA:
// demoPrjId = 2260; //QA:
// demoPrjId = 410; //QA:
// demoPrjId = 313; //PROD:
// demoPrjId = 455; //PROD:
// demoPrjId = 776; //PROD:
// demoPrjId = 671; //PROD:
// demoPrjId = 2056; //UAT
// demoPrjId = 815; //UAT
// demoPrjId = 1510; //PROD:
// demoPrjId = 618; //PROD:
// demoPrjId = 2580; //PROD:
// demoPrjId = 815; //PROD:
// demoPrjId = 20690;
demoPrjId = 21767;
// demoPrjId = 738; //UAT:
// demoPrjId = 670; //UAT: 清单项目
//*/
let userId_Dft = userId_Leng;
// userId_Dft = '5c641205950967000d20d35b'; //临时用
userId_Dft = '5c49310d9bda24000b444117'; //临时用
// let userId_Dft = "5a025c4c15074d134c2b9689";
/*/
 let userId_Dft = "595328da1934dc327cad08eb";
 //*/

let rptTplFacade = require("../../../modules/reports/facade/rpt_template_facade");
let rptTplDataFacade = require("../../../modules/reports/facade/rpt_tpl_data_facade");

import rptDataExtractor from "../../../modules/reports/util/rpt_yanghu_data_util";
import rpt_xl_util from "../../../modules/reports/util/rpt_excel_util";
import rpt_pdf_util from "../../../modules/reports/util/rpt_pdf_util";

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) {
    // console.log(new Date());
    rptTplFacade.getRptTemplate(demoRptId).then(function(rptTpl) {
        // console.log(new Date());
        let rptDataUtil = new rptDataExtractor();
        rptDataUtil.initialize(rptTpl._doc);
        let filter = rptDataUtil.getDataRequestFilter();
        console.log('开始时间：' + (new Date()));
        // filter.push('ration');  //临时用
        // filter.push('ration_coe'); //临时用2
        // filter.push('projectGLJ'); //临时用3
        // filter.push('calc_program'); //临时用4
        console.log(filter);
        //正常应该根据报表模板定义的数据类型来请求数据
        rptTplDataFacade.prepareProjectData(userId_Dft, demoPrjId, filter, function (err, msg, rawDataObj) {
            if (!err) {
                try {
                    let dt = new Date();
                    console.log('取完项目数据时间：' + (dt));
                    fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/YangHuCost/tmp/rptTplRawDataObject_测试模板.jsp");
                    let tplData = rptDataUtil.assembleData(rawDataObj);
                    // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/YangHuCost/tmp/rptTplRawDataAfterCacl_测试模板.jsp");
                    // fsUtil.writeObjToFile(tplData, "D:/GitHome/YangHuCost/tmp/rptTplAssembledData_测试模板.jsp");
                    //it's time to build the report!!!
                    let printCom = JpcEx.createNew();
                    rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pagesize;
                    let defProperties = rpt_cfg;
                    let dftOption = JV.PAGING_OPTION_NORMAL;
                    printCom.initialize(rptTpl);
                    printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, JV.OUTPUT_TYPE_EXCEL);
                    let maxPages = printCom.totalPages;
                    let customizeCfg = {"fillZero": true};
                    let pageRst = null;
                    if (maxPages > 0) {
                        pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
                    } else {
                        pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
                    }
                    if (pageRst) {
                        // fsUtil.writeObjToFile(pageRst, "D:/GitHome/YangHuCost/tmp/testBuiltPageResult_测试模板" + dt.getTime() + ".jsp");
                        // fsUtil.writeObjToFile(pageRst, "D:/GitHome/YangHuCost/tmp/testBuiltPageResult_测试模板.jsp");
                        // rpt_xl_util.exportExcel(pageRst, pagesize, "local_test_rpt_excel", true, null, null, function(uuidName){
                        //     console.log("excel uuid: " + uuidName);
                        // });
                        // rpt_pdf_util.export_pdf_file(pageRst, pagesize, 'local_test_rpt_pdf', function(uuidName){
                        //     console.log("pdf uuid: " + uuidName);
                        // });
                    } else {
                        console.log("oh! no pages were created!");
                    }
                    // console.log(new Date());
                } catch (ex) {
                    console.log(ex);
                    t.pass('pass with exception!');
                    t.end();
                }

                t.pass('pass succeeded!');
                t.end();
            } else {
                console.log(msg);
                t.pass('pass with error!');
                t.end();
            }
        })
    });
});

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