test_rpt_test_template.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 = 448; //封3
  30. // let demoRptId = 450; //09
  31. // let demoRptId = 704; //05
  32. // let demoRptId = 626; //06
  33. // let demoRptId = 743; //09-1
  34. // let demoRptId = 452; //04
  35. // let demoRptId = 612; //09-3
  36. // let demoRptId = 530; //封2
  37. // let demoRptId = 386; //04
  38. // let demoRptId = 389; //10
  39. // let demoRptId = 280; //11-1 暂列金
  40. // let demoRptId = 514; //12
  41. // let demoRptId = 2260; //测试基本信息
  42. // let demoRptId = 613; //09-4 2018
  43. // let demoRptId = 726; //定制:清单子目表
  44. // let demoRptId = 756; //江西 封1
  45. // let demoRptId = 710; //qa 01
  46. // let demoRptId = 931; //qa 内蒙古2017 【扉-1】招标工程量清单扉页
  47. // let demoRptId = 957; //qa 内蒙古2017 12-4
  48. // let demoRptId = 1006; //qa 重庆2018 A03
  49. // let demoRptId = 1007; //qa 重庆2018 A01
  50. // let demoRptId = 475; //计日工
  51. // let demoRptId = 551; //表05
  52. // let demoRptId = 1104; //内蒙2017 表09
  53. let demoRptId = 1104;
  54. let pagesize = "A4";
  55. //288: 11-2表(新)
  56. //279: 04
  57. // let userId_Leng = "59cdf14a0034a1000ba52b97"; //小冷User Id 换成_id了 QQ号
  58. let userId_Leng = "5acac1e885bf55000bd055ba"; //小冷User Id2
  59. let userId_me = "5b6a60b1c4ba33000dd417c0"; //我的
  60. // let userId_HaiZhu = "5b5a66c4a3c23e000dccdd77"; //海珠user id
  61. // demoPrjId = 720; //QA: DW3
  62. //demoPrjId = 1626; //QA:
  63. // demoPrjId = 2260; //QA:
  64. // demoPrjId = 5029; //
  65. // demoPrjId = 5029; //项目名称过长
  66. demoPrjId = 11355; //
  67. // demoPrjId = 4107; //UAT:
  68. //*/
  69. let userId_Dft = userId_Leng;
  70. // let userId_Dft = "5a025c4c15074d134c2b9689";
  71. /*/
  72. let userId_Dft = "595328da1934dc327cad08eb";
  73. //*/
  74. let rptTplFacade = require("../../../modules/reports/facade/rpt_template_facade");
  75. let rptTplDataFacade = require("../../../modules/reports/facade/rpt_tpl_data_facade");
  76. import rptDataExtractor from "../../../modules/reports/util/rpt_construct_data_util";
  77. import rpt_xl_util from "../../../modules/reports/util/rpt_excel_util";
  78. import rpt_pdf_util from "../../../modules/reports/util/rpt_pdf_util";
  79. let fs = require('fs');
  80. //设置Date Format函数
  81. // fs.readFile(__dirname.slice(0, __dirname.length - 18) + '/public/web/date_util.js', 'utf8', 'r', function (err, data) {
  82. // eval(data);
  83. // });
  84. //备注: 经过测试nodejs 8.9.3版本不支持eval的方式修改prototype,为兼容考虑,把方法调整到stringUtil文件里
  85. require('../../../public/stringUtil').setupDateFormat();
  86. test('测试 - 测试模板啦: ', function (t) {
  87. rptTplFacade.getRptTemplate(demoRptId).then(function(rptTpl) {
  88. let rptDataUtil = new rptDataExtractor();
  89. rptDataUtil.initialize(rptTpl._doc);
  90. // fsUtil.writeObjToFile(rptTpl._doc, "D:/GitHome/建筑造价支持文件/报表模板/重庆市2018建筑工程_【表-09-4】施工技术措施项目清单计价表_招投标1.js");
  91. // t.pass('pass : only save the template!');
  92. // t.end();
  93. // return;
  94. let filter = rptDataUtil.getDataRequestFilter();
  95. // filter.push("project_info");
  96. filter.push("contractor_list");
  97. // console.log(filter);
  98. //正常应该根据报表模板定义的数据类型来请求数据
  99. rptTplDataFacade.prepareProjectData(userId_Dft, demoPrjId, filter, function (err, msg, rawDataObj) {
  100. if (!err) {
  101. try {
  102. let dt = new Date();
  103. fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/rptTplRawDataObject_" + dt.getTime() + "_测试模板.jsp");
  104. let tplData = rptDataUtil.assembleData(rawDataObj);
  105. fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/ConstructionCost/tmp/rptTplRawDataAfterCacl_" + dt.getTime() + "测试模板.jsp");
  106. // fsUtil.writeObjToFile(tplData, "D:/GitHome/ConstructionCost/tmp/rptTplAssembledData_测试模板.jsp");
  107. //it's time to build the report!!!
  108. let printCom = JpcEx.createNew();
  109. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pagesize;
  110. let defProperties = rpt_cfg;
  111. let dftOption = JV.PAGING_OPTION_NORMAL;
  112. printCom.initialize(rptTpl);
  113. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, JV.OUTPUT_TYPE_EXCEL);
  114. let maxPages = printCom.totalPages;
  115. let customizeCfg = {"fillZero": true};
  116. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
  117. if (pageRst) {
  118. // fsUtil.writeObjToFile(pageRst, "D:/Temp/testBuiltPageResult_测试模板.jsp");
  119. // rpt_xl_util.exportExcel(pageRst, pagesize, "local_test_rpt_excel", true, null, null, function(uuidName){
  120. // console.log("excel uuid: " + uuidName);
  121. // });
  122. // rpt_pdf_util.export_pdf_file(pageRst, pagesize, 'local_test_rpt_pdf', function(uuidName){
  123. // console.log("pdf uuid: " + uuidName);
  124. // });
  125. } else {
  126. console.log("oh! no pages were created!");
  127. }
  128. } catch (ex) {
  129. console.log(ex);
  130. t.pass('pass with exception!');
  131. t.end();
  132. }
  133. t.pass('pass succeeded!');
  134. t.end();
  135. } else {
  136. console.log(msg);
  137. t.pass('pass with error!');
  138. t.end();
  139. }
  140. })
  141. });
  142. });
  143. test('close the connection', function (t) {
  144. setTimeout(function () {
  145. mongoose.disconnect();
  146. t.pass('closing db connection');
  147. t.end();
  148. }, 1000);
  149. // mongoose.disconnect();
  150. // t.pass('closing db connection');
  151. // t.end();
  152. });