test_rpt_test_template.js 7.5 KB

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