test_multi_rpts.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * Created by Tony on 2019/3/10.
  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 pm_facade = require('../../../modules/pm/facade/pm_facade');
  24. let demoRptIds = [];
  25. // demoRptIds.push(38); //5.2.2表
  26. // demoRptIds.push(6); //封面
  27. // demoRptIds.push(22); //03
  28. // demoRptIds.push(26); //07
  29. // demoRptIds.push(28); //09
  30. // demoRptIds.push(27); //08
  31. // demoRptIds.push(24); //05
  32. // demoRptIds.push(20); //01
  33. // demoRptIds.push(23); //04 综合费率表
  34. // demoRptIds.push(56); //24
  35. demoRptIds.push(36); //5.1
  36. demoRptIds.push(49); //5.5
  37. // demoRptIds.push(66); //5.4
  38. let pagesize = "A4";
  39. let userId_Leng = "5c3ffa9aa0a92732f41216e0"; //小冷User Id (养护的)
  40. // let userId_me = "5b6a60b1c4ba33000dd417c0"; //我的
  41. // let userId_HaiZhu = "5b5a66c4a3c23e000dccdd77"; //海珠user id
  42. let demoPrjId = - 1;
  43. // demoPrjId = 720; //QA: DW3
  44. //demoPrjId = 1626; //QA:
  45. // demoPrjId = 2260; //QA:
  46. // demoPrjId = 410; //QA:
  47. // demoPrjId = 313; //PROD:
  48. // demoPrjId = 455; //PROD:
  49. // demoPrjId = 618; //PROD:
  50. demoPrjId = 815; //PROD:
  51. // demoPrjId = 4107; //UAT:
  52. //*/
  53. let userId_Dft = userId_Leng;
  54. // let userId_Dft = "5a025c4c15074d134c2b9689";
  55. /*/
  56. let userId_Dft = "595328da1934dc327cad08eb";
  57. //*/
  58. let rptTplFacade = require("../../../modules/reports/facade/rpt_template_facade");
  59. let rptTplDataFacade = require("../../../modules/reports/facade/rpt_tpl_data_facade");
  60. import rptDataExtractor from "../../../modules/reports/util/rpt_yanghu_data_util";
  61. import rpt_xl_util from "../../../modules/reports/util/rpt_excel_util";
  62. import rpt_pdf_util from "../../../modules/reports/util/rpt_pdf_util";
  63. let fs = require('fs');
  64. //设置Date Format函数
  65. fs.readFile(__dirname.slice(0, __dirname.length - 18) + '/public/web/date_util.js', 'utf8', 'r', function (err, data) {
  66. eval(data);
  67. });
  68. test('测试 - 测试模板啦: ', function (t) {
  69. rptTplFacade.getRptTemplates(demoRptIds).then(function(rptTpls) {
  70. // console.log(rptTpls);
  71. let rptDataUtil = new rptDataExtractor();
  72. let filters = [];
  73. let summaryRst = [];
  74. let summaryIdxArr = [];
  75. //let promiseArr = [null, null];
  76. let promisesArr = []; //
  77. for (let tplFilterIdx = 0; tplFilterIdx < rptTpls.length; tplFilterIdx++) {
  78. let rptTpl = rptTpls[tplFilterIdx];
  79. rptDataUtil.initialize((rptTpl._doc)?rptTpl._doc:rptTpl);
  80. let filter = rptDataUtil.getDataRequestFilter(summaryRst);
  81. for (let dtlFilter of filter) {
  82. if (filters.indexOf(dtlFilter) < 0) {
  83. filters.push(dtlFilter);
  84. }
  85. }
  86. if (summaryRst.length > 0) {
  87. summaryIdxArr.push(tplFilterIdx);
  88. promisesArr.push([null, null]);
  89. if (summaryRst.indexOf(`Construct`) >= 0 || summaryRst.indexOf(`ConstructDetail`) >= 0) {
  90. promisesArr[promisesArr.length - 1][0] = pm_facade.getSummaryInfoByTender(demoPrjId, pm_facade.projectType.project);
  91. }
  92. if (summaryRst.indexOf(`Segment`) >= 0 || summaryRst.indexOf(`SegmentDetail`) >= 0) {
  93. promisesArr[promisesArr.length - 1][1] = pm_facade.getSummaryInfoByTender(demoPrjId, pm_facade.projectType.engineering);
  94. }
  95. }
  96. }
  97. //正常应该根据报表模板定义的数据类型来请求数据
  98. rptTplDataFacade.prepareProjectData(userId_Dft, demoPrjId, filters, function (err, msg, rawDataObj) {
  99. if (!err) {
  100. try {
  101. //1. 这里只用一份数据,根据实际应用情况,只需要保留copy三样数据: bills, ration, ration_glj, projectGLJ
  102. let savedBillsData = [], savedRationData = [], savedGljData = [];
  103. for (let dtlData of rawDataObj.prjData) {
  104. if (dtlData.moduleName === 'bills') {
  105. Object.assign(savedBillsData, dtlData.data);
  106. } else if (dtlData.moduleName === 'ration') {
  107. Object.assign(savedRationData, dtlData.data);
  108. } else if (dtlData.moduleName === 'ration_glj') {
  109. Object.assign(savedGljData, dtlData.data);
  110. } else if (dtlData.moduleName === 'projectGLJ') {
  111. //这个待定
  112. }
  113. }
  114. //2. 一个一个模板创建数据
  115. for (let tplIdx = 0; tplIdx < rptTpls.length; tplIdx++) {
  116. let rptTpl = (rptTpls[tplIdx]._doc)?rptTpls[tplIdx]._doc:rptTpls[tplIdx];
  117. rptDataUtil.initialize(rptTpl);
  118. let tplData = rptDataUtil.assembleData(rawDataObj);
  119. let printCom = JpcEx.createNew();
  120. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = pagesize;
  121. let defProperties = rpt_cfg;
  122. let dftOption = JV.PAGING_OPTION_NORMAL;
  123. printCom.initialize(rptTpl);
  124. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, JV.OUTPUT_TYPE_EXCEL);
  125. let maxPages = printCom.totalPages;
  126. let customizeCfg = {"fillZero": true};
  127. let pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, customizeCfg);
  128. if (pageRst) {
  129. fsUtil.writeObjToFile(pageRst, "D:/GitHome/YangHuCost/tmp/testBuiltPageResult_测试模板_" + tplIdx + ".jsp");
  130. // rpt_xl_util.exportExcel(pageRst, pagesize, "local_test_rpt_excel", true, null, null, function(uuidName){
  131. // console.log("excel uuid: " + uuidName);
  132. // });
  133. // rpt_pdf_util.export_pdf_file(pageRst, pagesize, 'local_test_rpt_pdf', function(uuidName){
  134. // console.log("pdf uuid: " + uuidName);
  135. // });
  136. } else {
  137. console.log("oh! no pages were created!");
  138. }
  139. //注意:这里需要把清单、定额、工料机数据assign回去!!!
  140. for (let dtlData of rawDataObj.prjData) {
  141. if (dtlData.moduleName === 'bills' && savedBillsData.length > 0) {
  142. Object.assign(dtlData.data, savedBillsData);
  143. } else if (dtlData.moduleName === 'ration' && savedRationData.length > 0) {
  144. Object.assign(dtlData.data, savedRationData);
  145. } else if (dtlData.moduleName === 'ration_glj' && savedGljData.length > 0) {
  146. Object.assign(dtlData.data, savedGljData);
  147. } else if (dtlData.moduleName === 'projectGLJ') {
  148. //这个待定
  149. }
  150. }
  151. }
  152. //let tplData = rptDataUtil.assembleData(rawDataObj);
  153. } catch (ex) {
  154. console.log(ex);
  155. t.pass('pass with exception!');
  156. t.end();
  157. } finally {
  158. t.pass('pass succeeded!');
  159. // t.end();
  160. mongoose.disconnect();
  161. t.pass('closing db connection');
  162. t.end();
  163. }
  164. } else {
  165. console.log(msg);
  166. t.pass('pass with error!');
  167. t.end();
  168. }
  169. })
  170. });
  171. });
  172. test('close the connection', function (t) {
  173. setTimeout(function () {
  174. mongoose.disconnect();
  175. t.pass('closing db connection');
  176. t.end();
  177. }, 1000);
  178. });