test_multi_rpts.js 9.3 KB

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