test_rpt_test_template.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Created by Tony on 2018/6/12.
  3. */
  4. let test = require('tape');
  5. import JpcEx from "../../../modules/reports/rpt_component/jpc_ex";
  6. import JV from "../../../modules/reports/rpt_component/jpc_value_define";
  7. let config = require("../../../config/config.js");
  8. config.setupDb(process.env.NODE_ENV);
  9. let mongoose = require("mongoose");
  10. let fileUtils = require("../../../modules/common/fileUtils");
  11. let path = require('path');
  12. let dbm = require("../../../config/db/db_manager");
  13. let rpt_cfg = require('./rpt_cfg');
  14. dbm.connect(process.env.NODE_ENV);
  15. //统一引用models
  16. fileUtils.getGlobbedFiles('../../../modules/all_models/*.js').forEach(function(modelPath) {
  17. require(path.resolve(modelPath));
  18. });
  19. //config.setupCache();
  20. let cfgCacheUtil = require("../../../config/cacheCfg");
  21. cfgCacheUtil.setupDftCache();
  22. let fsUtil = require("../../../public/fsUtil");
  23. let demoPrjId = - 1;
  24. // let demoRptId = 279;
  25. // let demoRptId = 275; //测试模板-流水式
  26. // let demoRptId = 337; //19表
  27. // let demoRptId = 361; //封1
  28. // let demoRptId = 279; //表04
  29. // let demoRptId = 261; //封3
  30. let demoRptId = 450; //09
  31. // let demoRptId = 451; //09-1
  32. // let demoRptId = 612; //09-3
  33. // let demoRptId = 530; //封2
  34. // let demoRptId = 386; //04
  35. // let demoRptId = 389; //10
  36. // let demoRptId = 280; //11-1 暂列金
  37. // let demoRptId = 2260; //测试基本信息
  38. let pagesize = "A4";
  39. //288: 11-2表(新)
  40. //279: 04
  41. // let userId_Leng = "59cdf14a0034a1000ba52b97"; //小冷User Id 换成_id了 QQ号
  42. // let userId_Leng = "5acac1e885bf55000bd055ba"; //小冷User Id2
  43. let userId_HaiZhu = "5b5a66c4a3c23e000dccdd77"; //海珠user id
  44. // demoPrjId = 720; //QA: DW3
  45. //demoPrjId = 1626; //QA:
  46. // demoPrjId = 2260; //QA:
  47. demoPrjId = 4529; //QA:
  48. //*/
  49. let userId_Dft = userId_HaiZhu;
  50. // let userId_Dft = "5a025c4c15074d134c2b9689";
  51. /*/
  52. let userId_Dft = "595328da1934dc327cad08eb";
  53. //*/
  54. let rptTplFacade = require("../../../modules/reports/facade/rpt_template_facade");
  55. let rptTplDataFacade = require("../../../modules/reports/facade/rpt_tpl_data_facade");
  56. import rptDataExtractor from "../../../modules/reports/util/rpt_construct_data_util";
  57. import rpt_xl_util from "../../../modules/reports/util/rpt_excel_util";
  58. import rpt_pdf_util from "../../../modules/reports/util/rpt_pdf_util";
  59. let fs = require('fs');
  60. //设置Date Format函数
  61. fs.readFile(__dirname.slice(0, __dirname.length - 18) + '/public/web/date_util.js', 'utf8', 'r', function (err, data) {
  62. eval(data);
  63. });
  64. test('测试 - 测试模板啦: ', function (t) {
  65. rptTplFacade.getRptTemplate(demoRptId).then(function(rptTpl) {
  66. let rptDataUtil = new rptDataExtractor();
  67. rptDataUtil.initialize(rptTpl._doc);
  68. let filter = rptDataUtil.getDataRequestFilter();
  69. console.log(filter);
  70. //正常应该根据报表模板定义的数据类型来请求数据
  71. rptTplDataFacade.prepareProjectData(userId_Dft, demoPrjId, filter, function (err, msg, rawDataObj) {
  72. if (!err) {
  73. try {
  74. // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/rptTplRawDataObject_测试模板.jsp");
  75. let tplData = rptDataUtil.assembleData(rawDataObj);
  76. // fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/rptTplRawDataAfterCacl_测试模板.jsp");
  77. // fsUtil.writeObjToFile(tplData, "D:/GitHome/ConstructionCost/tmp/rptTplAssembledData_测试模板.jsp");
  78. //it's time to build the report!!!
  79. let printCom = JpcEx.createNew();
  80. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pagesize;
  81. let defProperties = rpt_cfg;
  82. let dftOption = JV.PAGING_OPTION_NORMAL;
  83. printCom.initialize(rptTpl);
  84. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption);
  85. let maxPages = printCom.totalPages;
  86. let customizeCfg = {"fillZero": true};
  87. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
  88. if (pageRst) {
  89. // fsUtil.writeObjToFile(pageRst, "D:/GitHome/ConstructionCost/tmp/testBuiltPageResult_测试模板.jsp");
  90. // rpt_xl_util.exportExcel(pageRst, pagesize, "local_test_rpt_excel", true, null, null, function(uuidName){
  91. // console.log("excel uuid: " + uuidName);
  92. // });
  93. // rpt_pdf_util.export_pdf_file(pageRst, pagesize, 'local_test_rpt_pdf', function(uuidName){
  94. // console.log("pdf uuid: " + uuidName);
  95. // });
  96. } else {
  97. console.log("oh! no pages were created!");
  98. }
  99. } catch (ex) {
  100. console.log(ex);
  101. t.pass('pass with exception!');
  102. t.end();
  103. }
  104. t.pass('pass succeeded!');
  105. t.end();
  106. } else {
  107. console.log(msg);
  108. t.pass('pass with error!');
  109. t.end();
  110. }
  111. })
  112. });
  113. });
  114. test('close the connection', function (t) {
  115. setTimeout(function () {
  116. mongoose.disconnect();
  117. t.pass('closing db connection');
  118. t.end();
  119. }, 1000);
  120. // mongoose.disconnect();
  121. // t.pass('closing db connection');
  122. // t.end();
  123. });