test_multi_rpts.js 9.2 KB

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