report_controller.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. 'use strict';
  2. /**
  3. * Created by Tony on 2019/7/5.
  4. */
  5. const tenderMenu = require('../../config/menu').tenderMenu;
  6. const measureType = require('../const/tender').measureType;
  7. const accountGroup = require('../const/account_group').group;
  8. const JpcEx = require('../reports/rpt_component/jpc_ex');
  9. const JV = require('../reports/rpt_component/jpc_value_define');
  10. const rpt_xl_util = require('../reports/util/rpt_excel_util');
  11. const rptDataExtractor = require('../reports/util/rpt_calculation_data_util');
  12. const fsUtil = require('../public/js/fsUtil');
  13. const fs = require('fs');
  14. const rptCronJob = require('../reports/util/rpt_tmp_file_sweep');
  15. const RPT_DEF_PROPERTIES = require('../const/report_defined_properties');
  16. module.exports = app => {
  17. class ReportController extends app.BaseController {
  18. /**
  19. * 报表显示页面
  20. *
  21. * @param {Object} ctx - egg全局context
  22. * @return {void}
  23. */
  24. async index(ctx) {
  25. try {
  26. const tender = ctx.tender;
  27. const stage = ctx.stage;
  28. // console.log(tender.data);
  29. // console.log(tender.info);
  30. let stage_id = -1;
  31. let stage_order = -1;
  32. let sorder = -1;
  33. let stage_times = -1;
  34. let stage_status = -1;
  35. const treeNodes = await ctx.service.rptTreeNode.getNodesByProjectId([-1, tender.data.project_id]);
  36. const custCfg = await ctx.service.rptCustomizeCfg.getCustomizeCfgByUserId('Administrator');
  37. const stageList = await ctx.service.stage.getValidStagesShort(tender.id);
  38. const prjAccList = await ctx.service.projectAccount.getAllAccountByProjectId(tender.data.project_id);
  39. const roleList = await ctx.service.signatureRole.getSignatureRolesByTenderId(tender.id);
  40. // const allTpls = await ctx.service.rptTpl.getAllTplByIds(tmpRptIds);
  41. // for (const tpl of allTpls) {
  42. // const fName = tpl.id + '_' + tpl.rpt_tpl_name + '.js';
  43. // console.log('fName: ' + fName);
  44. // fsUtil.writeObjToFile(tpl, 'D:/Temp/计量临时/' + fName);
  45. // }
  46. // 完成历史任务,下一批
  47. // const filePath = 'D:/Temp/计量临时/';
  48. // const tplFiles = [];
  49. // fs.readdir(filePath, (err, files) => {
  50. // if (err) throw err;
  51. // files.forEach(file => {
  52. // const fPath = filePath + file;
  53. // // console.log('\'' + fPath + '\'');
  54. // tplFiles.push(fPath);
  55. // });
  56. // });
  57. //
  58. // setTimeout(function() {
  59. // console.log(tplFiles);
  60. // }, 2000);
  61. // 下一批
  62. // let cnt = 0;
  63. // for (const file of rptFiles) {
  64. // const tplObj = JSON.parse(fs.readFileSync(file, 'utf-8'));
  65. // const rst = await ctx.service.rptTpl.insertRptTpl(tplObj);
  66. // cnt++;
  67. // console.log(file);
  68. // }
  69. // console.log(cnt + ' 完成!');
  70. // console.log(maxStageAmt[0].maxAmt);
  71. // if ((stage === null || stage === undefined) && stageList.length > 0) {
  72. // await ctx.service.stage.checkStage(stageList[stageList.length - 1].id);
  73. // stage = ctx.stage;
  74. // }
  75. if (stage !== null && stage !== undefined) {
  76. stage_id = stage.id;
  77. stage_order = stage.order;
  78. sorder = stage.curOrder;
  79. stage_times = stage.times;
  80. stage_status = stage.status;
  81. }
  82. for (const prjAcc of prjAccList) {
  83. prjAcc.account_group = accountGroup[prjAcc.account_group];
  84. }
  85. const renderData = {
  86. tender: tender.data,
  87. tenderInfo: tender.info,
  88. rpt_tpl_data: JSON.stringify(treeNodes),
  89. cust_cfg: JSON.stringify(custCfg),
  90. project_id: tender.data.project_id,
  91. tender_id: tender.id,
  92. tender_name: tender.data.name,
  93. stg_id: stage_id,
  94. stg_order: stage_order,
  95. cur_order: sorder,
  96. stg_times: stage_times,
  97. stg_status: stage_status,
  98. stage_list: JSON.stringify(stageList),
  99. prj_account_list: JSON.stringify(prjAccList),
  100. role_list: JSON.stringify(roleList),
  101. tenderMenu,
  102. preUrl: '/tender/' + tender.id,
  103. measureType,
  104. // jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.ledger.explode),
  105. };
  106. await this.layout('report/index.ejs', renderData, 'report/rpt_all_popup.ejs');
  107. // await this.layout('report/index.ejs', renderData);
  108. // 在这里启动一个cron job,删除临时创建的excel文件
  109. if (!rptCronJob.started) {
  110. try {
  111. // console.log('rptCronJob.started: ' + rptCronJob.started);
  112. rptCronJob.started = true;
  113. rptCronJob.createJob(null, this.app.baseDir + '/app/public/download');
  114. } catch (ex) {
  115. rptCronJob.started = false;
  116. }
  117. }
  118. } catch (err) {
  119. this.log(err);
  120. console.log(err);
  121. // ctx.redirect('/tender/' + ctx.tender.id);
  122. }
  123. }
  124. /**
  125. * 报表打印页面
  126. *
  127. * @param {Object} ctx - egg全局context
  128. * @return {void}
  129. */
  130. async showPrintPage(ctx) {
  131. try {
  132. // const params = JSON.parse(ctx.request.body.params);
  133. const pageSize = ctx.params.size;
  134. // console.log('pageSize: ' + pageSize);
  135. const renderData = {
  136. size: pageSize,
  137. // orientation: params.orientation,
  138. };
  139. if (pageSize === 'A3') {
  140. await ctx.render('report/rpt_printA3.ejs', renderData);
  141. } else {
  142. await ctx.render('report/rpt_print.ejs', renderData);
  143. }
  144. } catch (err) {
  145. this.log(err);
  146. console.log(err);
  147. }
  148. }
  149. /**
  150. * 获取报表数据
  151. *
  152. * @param {Object} ctx - egg全局context
  153. * @return {void}
  154. */
  155. async getReport(ctx) {
  156. try {
  157. // console.log('in getReport');
  158. const params = JSON.parse(ctx.request.body.params);
  159. // console.log(params);
  160. let rptTpl = await ctx.service.rptTpl.getTplById(params.rpt_tpl_id);
  161. if (!rptTpl || rptTpl.length !== 1) {
  162. throw '获取模板失败';
  163. }
  164. rptTpl = JSON.parse(rptTpl[0].rpt_content);
  165. // console.log('get the template!');
  166. const pageRst = await getAllPagesCommon(ctx, rptTpl, params, JV.PAGING_OPTION_NORMAL, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir);
  167. // console.log(pageRst);
  168. const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id)) : [];
  169. const stgAudit = await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times);
  170. // console.log('after role stage!');
  171. // console.log(roleRel);
  172. await encodeSignatureDataUri(roleRel, this.app.baseDir);
  173. await encodeDummySignatureDataUri(pageRst, this.app.baseDir);
  174. // console.log('encodeSignatureDataUri!');
  175. ctx.body = { data: pageRst, signatureRelInfo: roleRel, stageAudit: stgAudit };
  176. // ctx.body = { data: { msg: 'test the network' } };
  177. ctx.status = 201;
  178. } catch (ex) {
  179. console.log(ex);
  180. this.setMessage(ex.toString(), this.messageType.ERROR);
  181. }
  182. }
  183. /**
  184. * 获取多批次报表数据
  185. *
  186. * @param {Object} ctx - egg全局context
  187. * @return {void}
  188. */
  189. async getMultiReportsEx(ctx) {
  190. // 原则说明:把所有报表模板集中获取,统一filter,只读一次数据!
  191. const params = JSON.parse(ctx.request.body.params);
  192. const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir);
  193. const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids)) : [];
  194. // const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id)) : [];
  195. await encodeSignatureDataUri(roleRel, this.app.baseDir);
  196. for (const pageRst of pageRstArr) {
  197. await encodeDummySignatureDataUri(pageRst, this.app.baseDir);
  198. }
  199. // console.log(pageRstArr);
  200. ctx.body = { data: pageRstArr, signatureRelInfo: roleRel };
  201. ctx.status = 201;
  202. }
  203. async createExcelFilesEx(ctx) {
  204. const params = JSON.parse(ctx.request.body.params);
  205. const baseDir = this.app.baseDir;
  206. function getExcelByPageData(pageRst, rpt_name, innerRoleRel) {
  207. return new Promise(function(resolve, reject) {
  208. rpt_xl_util.exportExcel(pageRst, params.pageSize, rpt_name, params.isOneSheet, null, null, baseDir, innerRoleRel,
  209. (err, uuidName) => {
  210. if (err) return reject(err);
  211. const fileRst = { uuid: uuidName, reportName: rpt_name };
  212. resolve(fileRst);
  213. }
  214. );
  215. });
  216. }
  217. const roleRelArr = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids)) : [];
  218. // const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id)) : [];
  219. const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir);
  220. await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  221. const runnableRst = [];
  222. for (let idx = 0; idx < pageRstArr.length; idx++) {
  223. let roleRel = null;
  224. for (const roleR of roleRelArr) {
  225. if (roleR.rpt_id === params.rpt_ids[idx]) {
  226. roleRel = JSON.parse(roleR.rel_content);
  227. break;
  228. }
  229. }
  230. // console.log('roleRel.rel_content: ' + roleRel.rel_content);
  231. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/testBuiltPageResult.jsp');
  232. runnableRst.push(getExcelByPageData(pageRstArr[idx], params.rpt_names[idx], roleRel));
  233. }
  234. const uuidRst = await Promise.all(runnableRst);
  235. ctx.body = { data: uuidRst };
  236. ctx.status = 201;
  237. }
  238. async createExcelFilesInOneBookEx(ctx) {
  239. const params = JSON.parse(ctx.request.body.params);
  240. const baseDir = this.app.baseDir;
  241. function getExcelByPageData(pageRstArr, rpt_name, innerRoleRel) {
  242. return new Promise(function(resolve, reject) {
  243. rpt_xl_util.exportExcelInOneBook(pageRstArr, params.pageSize, rpt_name, baseDir, innerRoleRel,
  244. (err, uuidName) => {
  245. if (err) return reject(err);
  246. const fileRst = { uuid: uuidName, reportName: rpt_name };
  247. resolve(fileRst);
  248. }
  249. );
  250. });
  251. }
  252. const roleRelArr = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids)) : [];
  253. const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir);
  254. await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  255. const runnableRst = [];
  256. let rptRoleRelArr = [];
  257. const reAssignSignatureName = function(pageData, roleRel, rpt_name_key) {
  258. const keyMap = {};
  259. for (const pageItem of pageData.items) {
  260. for (const signCell of pageItem[JV.PROP_SIGNATURE_CELLS]) {
  261. if (!keyMap.hasOwnProperty(signCell.signature_name)) {
  262. keyMap[signCell.signature_name] = rpt_name_key + '_' + signCell.signature_name;
  263. }
  264. }
  265. }
  266. for (const pageItem of pageData.items) {
  267. for (const signCell of pageItem[JV.PROP_SIGNATURE_CELLS]) {
  268. if (keyMap.hasOwnProperty(signCell.signature_name)) {
  269. signCell.signature_name = keyMap[signCell.signature_name];
  270. }
  271. }
  272. }
  273. for (const roleSign of roleRel) {
  274. if (keyMap.hasOwnProperty(roleSign.signature_name)) {
  275. roleSign.signature_name = keyMap[roleSign.signature_name];
  276. }
  277. }
  278. // console.log('keyMap of : ' + rpt_name_key);
  279. // console.log(keyMap);
  280. };
  281. for (let idx = 0; idx < pageRstArr.length; idx++) {
  282. let roleRel = null;
  283. for (const roleR of roleRelArr) {
  284. if (roleR.rpt_id === params.rpt_ids[idx]) {
  285. roleRel = JSON.parse(roleR.rel_content);
  286. // 这里要做些电子签名的signature_name转换,以防重名
  287. reAssignSignatureName(pageRstArr[idx], roleRel, params.rpt_names[idx]);
  288. rptRoleRelArr = rptRoleRelArr.concat(roleRel);
  289. break;
  290. }
  291. }
  292. }
  293. runnableRst.push(getExcelByPageData(pageRstArr, params.rptName, rptRoleRelArr));
  294. const uuidRst = await Promise.all(runnableRst);
  295. ctx.body = { data: uuidRst };
  296. ctx.status = 201;
  297. }
  298. async getFileByUUID(ctx) {
  299. // console.log('downloading : ' + ctx.params.uuid);
  300. const uuid = ctx.params.uuid;
  301. const rptName = ctx.params.rptName;
  302. const suffix = '.' + ctx.params.suffix;
  303. function getUuidFile(filestream) {
  304. return new Promise(function(resolve, reject) {
  305. filestream.on('data', function(chunk) {
  306. resolve(chunk);
  307. });
  308. });
  309. }
  310. try {
  311. const rptNameURI = encodeURI(rptName);
  312. const filePath = this.app.baseDir + '/app/public/download/';
  313. // console.log('filePath: ' + filePath);
  314. // await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  315. const filestream = fs.createReadStream(filePath + uuid + suffix);
  316. const chunk = await getUuidFile(filestream);
  317. ctx.set({
  318. 'Content-Type': 'application/vnd.openxmlformats',
  319. 'Content-Disposition': 'attachment; filename="' + rptNameURI + suffix + "\"; filename*=utf-8''" + rptNameURI + suffix,
  320. 'Content-Length': chunk.length,
  321. });
  322. ctx.body = chunk;
  323. } catch (e) {
  324. console.log(e);
  325. }
  326. }
  327. }
  328. return ReportController;
  329. };
  330. async function getAllPagesCommon(ctx, rptTpl, params, option, outputType, baseDir) {
  331. // let rptTpl = null;
  332. const rptDataUtil = new rptDataExtractor();
  333. rptDataUtil.initialize(rptTpl);
  334. // console.log(rptTpl);
  335. const filter = rptDataUtil.getDataRequestFilter();
  336. // console.log(filter.tables);
  337. const rawDataObj = await ctx.service.report.getReportData(params, filter.tables, filter.memFieldKeys);
  338. // console.log(rawDataObj);
  339. try {
  340. const printCom = JpcEx.createNew();
  341. if (params.pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = params.pageSize;
  342. if (params.orientation && (params.orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = params.orientation;
  343. // let defProperties = await ctx.service.rptPreDefineCfg.getCfgById('Administrator');
  344. // defProperties = JSON.parse(defProperties[0].defined_content);
  345. const defProperties = RPT_DEF_PROPERTIES;
  346. // console.log('defProperties: ');
  347. // console.log(defProperties);
  348. if (params.custCfg) {
  349. setupCustomizeCfg(params.custCfg, rptTpl, defProperties);
  350. } else {
  351. // setupCustomizeCfg(defProperties, rptTpl, defProperties);
  352. }
  353. const dftOption = params.option || JV.PAGING_OPTION_NORMAL;
  354. printCom.initialize(rptTpl);
  355. // console.log('before assemble');
  356. const tplData = rptDataUtil.assembleData(ctx, rawDataObj, baseDir, printCom);
  357. // console.log(tplData);
  358. // ctx.helper
  359. printCom.analyzeData(ctx.helper, rptTpl, tplData, defProperties, dftOption, outputType);
  360. const maxPages = printCom.totalPages;
  361. let pageRst = null;
  362. if (maxPages > 0) {
  363. pageRst = printCom.outputAsSimpleJSONPageArray(ctx.helper, rptTpl, tplData, 1, maxPages, defProperties, params.custCfg);
  364. } else {
  365. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  366. }
  367. if (pageRst) {
  368. // fsUtil.writeObjToFile(pageRst, 'D:/GitHome/temp/testBuiltPageResult.jsp');
  369. } else {
  370. //
  371. }
  372. // console.log(pageRst);
  373. return pageRst;
  374. } catch (ex) {
  375. // console.log("报表数据异常: userId " + user_id + ", project id: " + prj_id);
  376. console.log(ex);
  377. }
  378. }
  379. async function getMultiRptsCommon(ctx, params, outputType, baseDir) {
  380. for (let idx = 0; idx < params.rpt_ids.length; idx++) {
  381. params.rpt_ids[idx] = parseInt(params.rpt_ids[idx]); // 转换一下,以防万一
  382. }
  383. const rptTpls = await ctx.service.rptTpl.getAllTplByIds(params.rpt_ids);
  384. // console.log(rptTpls);
  385. for (let rtIdx = 0; rtIdx < rptTpls.length; rtIdx++) {
  386. rptTpls[rtIdx] = JSON.parse(rptTpls[rtIdx].rpt_content);
  387. }
  388. const rptDataUtil = new rptDataExtractor();
  389. const filterTables = [];
  390. const memFieldKeys = {};
  391. if (rptTpls.length > 1) {
  392. rptTpls.sort(function(item1, item2) {
  393. const i1 = (item1._doc) ? item1._doc : item1;
  394. const i2 = (item2._doc) ? item2._doc : item2;
  395. const ID1 = params.rpt_ids.indexOf(i1[JV.PROP_ID]);
  396. const ID2 = params.rpt_ids.indexOf(i2[JV.PROP_ID]);
  397. return ID1 - ID2;
  398. });
  399. }
  400. for (const rptTpl of rptTpls) {
  401. rptDataUtil.initialize(rptTpl);
  402. const filter = rptDataUtil.getDataRequestFilter();
  403. // console.log(filter);
  404. for (const table of filter.tables) {
  405. if (filterTables.indexOf(table) < 0) {
  406. filterTables.push(table);
  407. }
  408. // memFieldKeys[table] = [];
  409. }
  410. for (const tableKeyProp in filter.memFieldKeys) {
  411. if (!memFieldKeys.hasOwnProperty(tableKeyProp)) {
  412. memFieldKeys[tableKeyProp] = [];
  413. }
  414. for (const mfKey of filter.memFieldKeys[tableKeyProp]) {
  415. if (memFieldKeys[tableKeyProp].indexOf(mfKey) < 0) {
  416. memFieldKeys[tableKeyProp].push(mfKey);
  417. }
  418. }
  419. }
  420. }
  421. const rawDataObj = await ctx.service.report.getReportData(params, filterTables, memFieldKeys);
  422. try {
  423. const rptPageRstArray = [];
  424. // 1. 这里只用一份数据,根据实际应用情况,先备份
  425. const backupData = {};
  426. for (let filterIdx = 0; filterIdx < filterTables.length; filterIdx++) {
  427. backupData[filterTables[filterIdx]] = [];
  428. Object.assign(backupData[filterTables[filterIdx]], rawDataObj[filterTables[filterIdx]]);
  429. }
  430. // 2. 一个一个模板创建数据
  431. let defProperties = await ctx.service.rptPreDefineCfg.getCfgById('Administrator');
  432. // console.log('defProperties: ');
  433. // console.log(defProperties[0].defined_content);
  434. defProperties = JSON.parse(defProperties[0].defined_content);
  435. for (let tplIdx = 0; tplIdx < rptTpls.length; tplIdx++) {
  436. const rptTpl = (rptTpls[tplIdx]._doc) ? rptTpls[tplIdx]._doc : rptTpls[tplIdx];
  437. rptDataUtil.initialize(rptTpl);
  438. const tplData = rptDataUtil.assembleData(ctx, rawDataObj, baseDir);
  439. const printCom = JpcEx.createNew();
  440. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = params.pageSize;
  441. if (params.pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = params.pageSize;
  442. if (params.orientation && (params.orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = params.orientation;
  443. if (params.custCfg) setupCustomizeCfg(params.custCfg, rptTpl, defProperties);
  444. const dftOption = params.option || JV.PAGING_OPTION_NORMAL;
  445. printCom.initialize(rptTpl);
  446. printCom.analyzeData(rptTpl, tplData, defProperties, dftOption, outputType);
  447. const maxPages = printCom.totalPages;
  448. let pageRst = null;
  449. if (maxPages > 0) {
  450. pageRst = printCom.outputAsSimpleJSONPageArray(rptTpl, tplData, 1, maxPages, defProperties, params.custCfg);
  451. } else {
  452. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  453. }
  454. rptPageRstArray.push(pageRst);
  455. // 注意:这里需要把备份数据assign回去!!!
  456. if (tplIdx < rptTpls.length - 1) {
  457. for (let filterIdx = 0; filterIdx < filterTables.length; filterIdx++) {
  458. backupData[filterTables[filterIdx]] = [];
  459. Object.assign(rawDataObj[filterTables[filterIdx]], backupData[filterTables[filterIdx]]);
  460. }
  461. }
  462. }
  463. return rptPageRstArray;
  464. } catch (ex) {
  465. console.log('报表数据异常(getMultiRptsCommon): project_id ' + params.project_id + ', tender_id: ' + params.tender_id + ', stage_id: ' + params.stage_id);
  466. console.log(ex);
  467. } finally {
  468. //
  469. }
  470. }
  471. function setupCustomizeCfg(customizeCfg, rptTpl, defProperties) {
  472. const tmpObj = {};
  473. // 1. 字体
  474. const newFonts = [];
  475. for (const font of defProperties.fonts) {
  476. const copyFont = {};
  477. copyFont.ID = font.ID;
  478. for (const fontProp of JV.FONT_PROPS) {
  479. copyFont[fontProp] = font[fontProp];
  480. }
  481. newFonts.push(copyFont);
  482. tmpObj[font.ID] = copyFont;
  483. }
  484. const private_setup_font = function(propStr, newFont) {
  485. if (tmpObj[propStr]) {
  486. tmpObj[propStr].Name = newFont.Name;
  487. tmpObj[propStr].FontHeight = String(newFont.FontHeight);
  488. tmpObj[propStr].FontBold = newFont.FontBold;
  489. tmpObj[propStr].FontItalic = newFont.FontItalic;
  490. tmpObj[propStr].FontUnderline = newFont.FontUnderline;
  491. }
  492. };
  493. for (const custFont of customizeCfg.fonts) {
  494. switch (custFont.CfgDispName) {
  495. case '表标题':
  496. private_setup_font('ReportTitle_Main', custFont);
  497. break;
  498. case '列标题':
  499. private_setup_font('HeaderColumn', custFont);
  500. private_setup_font('FooterColumn', custFont);
  501. break;
  502. case '正文内容':
  503. private_setup_font('Content', custFont);
  504. break;
  505. case '合计':
  506. private_setup_font('GrandTotal', custFont);
  507. private_setup_font('SectionTotal', custFont);
  508. break;
  509. case '表眉/表脚':
  510. private_setup_font('Header', custFont);
  511. private_setup_font('Footer', custFont);
  512. break;
  513. default:
  514. break;
  515. }
  516. }
  517. // 1.1 窄体
  518. if (tmpObj.Content_Narrow) {
  519. if (customizeCfg.isNarrow) {
  520. tmpObj.Content_Narrow.Name = 'Arial Narrow';
  521. } else {
  522. if (tmpObj.Content) {
  523. tmpObj.Content_Narrow.Name = tmpObj.Content.Name;
  524. } else {
  525. tmpObj.Content_Narrow.Name = '宋体';
  526. }
  527. }
  528. }
  529. defProperties.fonts = newFonts;
  530. // 2. 页边距
  531. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT] = customizeCfg.margins[JV.PROP_LEFT] / 10;
  532. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT] = customizeCfg.margins[JV.PROP_RIGHT] / 10;
  533. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP] = customizeCfg.margins[JV.PROP_TOP] / 10;
  534. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM] = customizeCfg.margins[JV.PROP_BOTTOM] / 10;
  535. // 3. 边框宽度
  536. if (customizeCfg.hasOwnProperty('borderThick')) {
  537. for (let i = 0; i < defProperties.styles.length; i++) {
  538. const style = defProperties.styles[i];
  539. if (style.ID === 'BORDER_ALL_AROUND') {
  540. for (const border of style.border_style) {
  541. border.LineWeight = customizeCfg.borderThick;
  542. }
  543. }
  544. }
  545. }
  546. // 4. 边框竖线
  547. if (!(customizeCfg.showVerticalLine)) {
  548. const private_copy_border = function(src) {
  549. const rst = {};
  550. rst.Position = src.Position;
  551. rst.LineWeight = src.LineWeight;
  552. rst.DashStyle = src.DashStyle;
  553. rst.Color = src.Color;
  554. return rst;
  555. };
  556. const newStyles = [];
  557. for (let i = 0; i < defProperties.styles.length; i++) {
  558. const style = defProperties.styles[i];
  559. newStyles.push(style);
  560. if (style.ID === 'BORDER_ALL_AROUND') {
  561. const newStyle = {};
  562. newStyle.ID = style.ID;
  563. newStyle.CfgDispName = style.CfgDispName;
  564. newStyle.border_style = [];
  565. for (const border of style.border_style) {
  566. const newBorder = private_copy_border(border);
  567. newStyle.border_style.push(newBorder);
  568. if (border.Position === 'Left' || border.Position === 'Right') {
  569. newBorder.LineWeight = 0;
  570. }
  571. }
  572. newStyles[newStyles.length - 1] = newStyle;
  573. }
  574. }
  575. defProperties.styles = newStyles;
  576. }
  577. // 5. 补0
  578. const private_Setup_Format = function(tabFields) {
  579. if (tabFields) {
  580. for (const tabField of tabFields) {
  581. if (tabField[JV.PROP_FORMAT]) {
  582. tabField[JV.PROP_FORMAT] = tabField[JV.PROP_FORMAT].replace(new RegExp('#', 'gm'), '0');
  583. }
  584. }
  585. }
  586. };
  587. if (customizeCfg.fillZero) {
  588. if (rptTpl[JV.NODE_FLOW_INFO]) {
  589. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_FLOW_FIELDS]);
  590. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_PAGE_SUM]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_PAGE_SUM][JV.PROP_SUM_FIELDS]);
  591. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_SEG_SUM]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_SEG_SUM][JV.PROP_SUM_FIELDS]);
  592. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_GROUP] && rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES][JV.PROP_SUM_FIELDS]);
  593. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO]) {
  594. for (const discrete of rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO]) {
  595. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  596. }
  597. }
  598. if (rptTpl[JV.NODE_FLOW_INFO_EX]) {
  599. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_CONTENT]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_CONTENT][JV.PROP_FLOW_FIELDS]);
  600. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_PAGE_SUM]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_PAGE_SUM][JV.PROP_SUM_FIELDS]);
  601. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_SEG_SUM]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_SEG_SUM][JV.PROP_SUM_FIELDS]);
  602. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_GROUP]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_GROUP][JV.PROP_SUM_FIELDS]);
  603. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_GROUP] && rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES]) private_Setup_Format(rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_FLOW_GROUP][JV.PROP_GROUP_LINES][JV.PROP_SUM_FIELDS]);
  604. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_DISCRETE_INFO]) {
  605. for (const discrete of rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_DISCRETE_INFO]) {
  606. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  607. }
  608. }
  609. }
  610. } else if (rptTpl[JV.NODE_BILL_INFO]) {
  611. if (rptTpl[JV.NODE_BILL_INFO][JV.NODE_BILL_CONTENT]) private_Setup_Format(rptTpl[JV.NODE_BILL_INFO][JV.NODE_BILL_CONTENT][JV.PROP_BILL_FIELDS]);
  612. if (rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO]) {
  613. for (const discrete of rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO]) {
  614. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  615. }
  616. }
  617. } else if (rptTpl[JV.NODE_CROSS_INFO]) {
  618. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_CONTENT]) private_Setup_Format(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_CONTENT][JV.TAB_CROSS_FIELDS]);
  619. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW]) private_Setup_Format(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW][JV.TAB_CROSS_FIELDS]);
  620. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_COL]) private_Setup_Format(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_COL][JV.TAB_CROSS_FIELDS]);
  621. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_COL_SUM]) private_Setup_Format(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_COL_SUM][JV.TAB_CROSS_FIELDS]);
  622. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_EXT]) private_Setup_Format(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_EXT][JV.TAB_CROSS_FIELDS]);
  623. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_SUM_EXT]) private_Setup_Format(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_SUM_EXT][JV.TAB_CROSS_FIELDS]);
  624. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO]) {
  625. for (const discrete of rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO]) {
  626. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  627. }
  628. }
  629. }
  630. }
  631. }
  632. function isFileExisted(file) {
  633. return new Promise(function(resolve, reject) {
  634. fs.access(file, err => {
  635. if (err) {
  636. reject(false);
  637. } else {
  638. resolve(true);
  639. }
  640. });
  641. });
  642. }
  643. async function encodeSignatureDataUri(roleRel, baseDir) {
  644. if (roleRel) {
  645. for (const singleRoleRel of roleRel) {
  646. const roleRelContent = JSON.parse(singleRoleRel.rel_content);
  647. for (const role of roleRelContent) {
  648. // console.log(role);
  649. if (role.sign_path !== '') {
  650. const filePath = baseDir + '/app' + role.sign_path;
  651. try {
  652. const res = await isFileExisted(filePath);
  653. if (res) {
  654. const bData = fs.readFileSync(filePath);
  655. const base64Str = bData.toString('base64');
  656. const datauri = 'data:image/png;base64,' + base64Str;
  657. role.sign_pic = datauri;
  658. } else {
  659. console.log('文件不存在:' + filePath);
  660. }
  661. } catch (err) {
  662. console.error(err);
  663. }
  664. }
  665. }
  666. singleRoleRel.rel_content = JSON.stringify(roleRelContent);
  667. }
  668. }
  669. }
  670. async function encodeDummySignatureDataUri(pageRst, baseDir) {
  671. if (pageRst) {
  672. for (const page of pageRst.items) {
  673. for (const signature of page.signature_cells) {
  674. if (signature.signature_name === JV.SIGNATURE_NAME_DUMMY) {
  675. if (signature.path !== '') {
  676. const filePath = baseDir + '/app' + signature.path;
  677. try {
  678. const res = await isFileExisted(filePath);
  679. if (res) {
  680. const bData = fs.readFileSync(filePath);
  681. const base64Str = bData.toString('base64');
  682. const datauri = 'data:image/png;base64,' + base64Str;
  683. signature.pic = datauri;
  684. signature.path = '';
  685. } else {
  686. console.log('文件不存在:' + filePath);
  687. }
  688. } catch (err) {
  689. console.error(err);
  690. }
  691. }
  692. }
  693. }
  694. }
  695. }
  696. }