test_rpt_test_template.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 = 38; //5.2.2表
  25. // let demoRptId = 6; //封面
  26. // let demoRptId = 22; //03
  27. // let demoRptId = 26; //07
  28. // let demoRptId = 28; //09
  29. // let demoRptId = 27; //08
  30. // let demoRptId = 24; //05
  31. // let demoRptId = 20; //01
  32. // let demoRptId = 23; //04 综合费率表
  33. // let demoRptId = 56; //24
  34. // let demoRptId = 36; //5.1
  35. // let demoRptId = 46; //5.5测试
  36. // let demoRptId = 49; //5.5
  37. // let demoRptId = 66; //5.4
  38. // let demoRptId = 67; //21-2
  39. // let demoRptId = 31; //21-1
  40. // let demoRptId = 37; //5.2.1 计日工劳务
  41. // let demoRptId = 68; //01-2
  42. // let demoRptId = 71; //21-2
  43. // let demoRptId = 74; //22
  44. let demoRptId = 83; //02 预算
  45. // let demoRptId = 84; //02 清单
  46. let pagesize = "A4";
  47. //288: 11-2表(新)
  48. //279: 04
  49. let userId_Leng = "5c3ffa9aa0a92732f41216e0"; //小冷User Id (养护的)
  50. // let userId_me = "5b6a60b1c4ba33000dd417c0"; //我的
  51. // let userId_HaiZhu = "5b5a66c4a3c23e000dccdd77"; //海珠user id
  52. // demoPrjId = 720; //QA: DW3
  53. //demoPrjId = 1626; //QA:
  54. // demoPrjId = 2260; //QA:
  55. // demoPrjId = 410; //QA:
  56. // demoPrjId = 313; //PROD:
  57. // demoPrjId = 455; //PROD:
  58. // demoPrjId = 776; //PROD:
  59. // demoPrjId = 671; //PROD:
  60. // demoPrjId = 2056; //UAT
  61. // demoPrjId = 815; //UAT
  62. // demoPrjId = 1510; //PROD:
  63. // demoPrjId = 618; //PROD:
  64. // demoPrjId = 2580; //PROD:
  65. // demoPrjId = 815; //PROD:
  66. demoPrjId = 3261; //UAT:
  67. // demoPrjId = 738; //UAT:
  68. // demoPrjId = 670; //UAT: 清单项目
  69. //*/
  70. let userId_Dft = userId_Leng;
  71. // userId_Dft = '5c641205950967000d20d35b'; //临时用
  72. // let userId_Dft = "5a025c4c15074d134c2b9689";
  73. /*/
  74. let userId_Dft = "595328da1934dc327cad08eb";
  75. //*/
  76. let rptTplFacade = require("../../../modules/reports/facade/rpt_template_facade");
  77. let rptTplDataFacade = require("../../../modules/reports/facade/rpt_tpl_data_facade");
  78. import rptDataExtractor from "../../../modules/reports/util/rpt_yanghu_data_util";
  79. import rpt_xl_util from "../../../modules/reports/util/rpt_excel_util";
  80. import rpt_pdf_util from "../../../modules/reports/util/rpt_pdf_util";
  81. let fs = require('fs');
  82. //设置Date Format函数
  83. fs.readFile(__dirname.slice(0, __dirname.length - 18) + '/public/web/date_util.js', 'utf8', 'r', function (err, data) {
  84. eval(data);
  85. });
  86. test('测试 - 测试模板啦: ', function (t) {
  87. // console.log(new Date());
  88. rptTplFacade.getRptTemplate(demoRptId).then(function(rptTpl) {
  89. // console.log(new Date());
  90. let rptDataUtil = new rptDataExtractor();
  91. rptDataUtil.initialize(rptTpl._doc);
  92. let filter = rptDataUtil.getDataRequestFilter();
  93. console.log('开始时间:' + (new Date()));
  94. // filter.push('ration'); //临时用
  95. // filter.push('ration_coe'); //临时用2
  96. // filter.push('projectGLJ'); //临时用3
  97. // filter.push('calc_program'); //临时用4
  98. console.log(filter);
  99. //正常应该根据报表模板定义的数据类型来请求数据
  100. rptTplDataFacade.prepareProjectData(userId_Dft, demoPrjId, filter, function (err, msg, rawDataObj) {
  101. if (!err) {
  102. try {
  103. let dt = new Date();
  104. console.log('取完项目数据时间:' + (dt));
  105. fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/YangHuCost/tmp/rptTplRawDataObject_测试模板.jsp");
  106. let tplData = rptDataUtil.assembleData(rawDataObj);
  107. fsUtil.writeObjToFile(rawDataObj, "D:/GitHome/YangHuCost/tmp/rptTplRawDataAfterCacl_测试模板.jsp");
  108. // fsUtil.writeObjToFile(tplData, "D:/GitHome/YangHuCost/tmp/rptTplAssembledData_测试模板.jsp");
  109. //it's time to build the report!!!
  110. let printCom = JpcEx.createNew();
  111. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pagesize;
  112. let defProperties = rpt_cfg;
  113. let dftOption = JV.PAGING_OPTION_NORMAL;
  114. printCom.initialize(rptTpl);
  115. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, JV.OUTPUT_TYPE_EXCEL);
  116. let maxPages = printCom.totalPages;
  117. let customizeCfg = {"fillZero": true};
  118. let pageRst = null;
  119. if (maxPages > 0) {
  120. pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
  121. } else {
  122. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  123. }
  124. if (pageRst) {
  125. // fsUtil.writeObjToFile(pageRst, "D:/GitHome/YangHuCost/tmp/testBuiltPageResult_测试模板" + dt.getTime() + ".jsp");
  126. // fsUtil.writeObjToFile(pageRst, "D:/GitHome/YangHuCost/tmp/testBuiltPageResult_测试模板.jsp");
  127. rpt_xl_util.exportExcel(pageRst, pagesize, "local_test_rpt_excel", true, null, null, function(uuidName){
  128. console.log("excel uuid: " + uuidName);
  129. });
  130. // rpt_pdf_util.export_pdf_file(pageRst, pagesize, 'local_test_rpt_pdf', function(uuidName){
  131. // console.log("pdf uuid: " + uuidName);
  132. // });
  133. } else {
  134. console.log("oh! no pages were created!");
  135. }
  136. // console.log(new Date());
  137. } catch (ex) {
  138. console.log(ex);
  139. t.pass('pass with exception!');
  140. t.end();
  141. }
  142. t.pass('pass succeeded!');
  143. t.end();
  144. } else {
  145. console.log(msg);
  146. t.pass('pass with error!');
  147. t.end();
  148. }
  149. })
  150. });
  151. });
  152. test('close the connection', function (t) {
  153. setTimeout(function () {
  154. mongoose.disconnect();
  155. t.pass('closing db connection');
  156. t.end();
  157. }, 1000);
  158. // mongoose.disconnect();
  159. // t.pass('closing db connection');
  160. // t.end();
  161. });