report_controller.js 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  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 auditConst = require('../const/audit');
  8. const shenpiConst = require('../const/shenpi');
  9. const accountGroup = require('../const/account_group').group;
  10. const JpcEx = require('../reports/rpt_component/jpc_ex');
  11. const JV = require('../reports/rpt_component/jpc_value_define');
  12. const rpt_xl_util = require('../reports/util/rpt_excel_util');
  13. const rptDataExtractor = require('../reports/util/rpt_calculation_data_util');
  14. const fsUtil = require('../public/js/fsUtil');
  15. const fs = require('fs');
  16. const rptCronJob = require('../reports/util/rpt_tmp_file_sweep');
  17. const RPT_DEF_PROPERTIES = require('../const/report_defined_properties');
  18. const reportConst = require('../const/report');
  19. // const stringUtil = require('../public/js/string_util_light');
  20. const needCustomTables = [
  21. 'mem_custom_select',
  22. 'mem_gather_stage_bills', 'mem_gather_deal_bills', 'mem_gather_stage_pay', 'mem_gather_tender_info',
  23. 'mem_stage_sum_bills', 'mem_stage_sum_pay',
  24. 'mem_jh_gather_im_change', 'mem_jh_im_change', 'mem_jh_gather_stage_bills_compare',
  25. ];
  26. module.exports = app => {
  27. class ReportController extends app.BaseController {
  28. /**
  29. * 获取审批界面所需的 原报、审批人数据等
  30. * @param ctx
  31. * @return {Promise<void>}
  32. * @private
  33. */
  34. async _getStageAuditViewData(ctx) {
  35. if (!ctx.stage) return;
  36. const times = ctx.stage.status === auditConst.stage.status.checkNo ? ctx.stage.times - 1 : ctx.stage.times;
  37. ctx.stage.user = await ctx.service.projectAccount.getAccountInfoById(ctx.stage.user_id);
  38. ctx.stage.auditHistory = [];
  39. if (times >= 1) {
  40. for (let i = 1; i <= times; i++) {
  41. ctx.stage.auditHistory.push(await ctx.service.stageAudit.getAuditors(ctx.stage.id, i));
  42. }
  43. }
  44. // 获取审批流程中左边列表
  45. ctx.stage.auditors2 = await ctx.service.stageAudit.getAuditGroupByListWithOwner(ctx.stage.id, times);
  46. if (ctx.stage.status === auditConst.stage.status.uncheck || ctx.stage.status === auditConst.stage.status.checkNo) {
  47. ctx.stage.auditorList = await ctx.service.stageAudit.getAuditors(ctx.stage.id, ctx.stage.times);
  48. }
  49. }
  50. async _chkIfStageAuditor(ctx, stage) {
  51. //
  52. }
  53. async _filterStageList(ctx, stageList) {
  54. //
  55. // throw
  56. }
  57. /**
  58. * 报表显示页面
  59. *
  60. * @param {Object} ctx - egg全局context
  61. * @return {void}
  62. */
  63. async index(ctx) {
  64. try {
  65. await this._getStageAuditViewData(ctx);
  66. const pageShow = ctx.session.sessionProject.page_show;
  67. pageShow.showArchive = 1;
  68. const tender = ctx.tender;
  69. const stage = ctx.stage;
  70. let stage_id = -1;
  71. let stage_order = -1;
  72. let sorder = -1;
  73. let stage_times = -1;
  74. let stage_status = -1;
  75. const treeNodes = await ctx.service.rptTreeNode.getNodesByProjectId([-1, tender.data.project_id]);
  76. const custTreeNodes = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(this.ctx.session.sessionUser.accountId);
  77. const custCfg = await ctx.service.rptCustomizeCfg.getCustomizeCfgByUserId('Administrator');
  78. const stageList = await ctx.service.stage.getValidStagesShort(tender.id);
  79. // console.log(stage);
  80. // console.log(stageList);
  81. // console.log('ctx.stage.id: ' + ctx.stage.id);
  82. // console.log('ctx.stage.stage_id: ' + ctx.stage.stage_id);
  83. // console.log('this.ctx.session.sessionUser.accountId: ' + this.ctx.session.sessionUser.accountId);
  84. if (stage && stage.status === auditConst.stage.status.uncheck) {
  85. // 得判断账号是否在审核人列表中(不含原报)
  86. if (stage.auditorList === undefined || stage.auditorList === null) {
  87. stage.auditorList = await ctx.service.stageAudit.getAuditors(ctx.stage.id, ctx.stage.times);
  88. }
  89. let isAudit = false;
  90. // console.log('stage.auditorList');
  91. // console.log(stage.auditorList);
  92. for (const audit of stage.auditorList) {
  93. if (audit.aid === this.ctx.session.sessionUser.accountId) {
  94. isAudit = true;
  95. break;
  96. }
  97. }
  98. if (isAudit) {
  99. throw '未上报!';
  100. }
  101. } else {
  102. // 剔除未上报期(审核人(非原报人)才需要判断)
  103. for (let idx = stageList.length - 1; idx >= 0; idx--) {
  104. if (stageList[idx].status === auditConst.stage.status.uncheck) {
  105. if (stageList[idx].auditorList === undefined || stageList[idx].auditorList === null) {
  106. stageList[idx].auditorList = await ctx.service.stageAudit.getAuditors(stageList[idx].id, stageList[idx].times);
  107. }
  108. let isAudit = false;
  109. // console.log('stageList[idx].auditorList');
  110. // console.log(stageList[idx].auditorList);
  111. for (const audit of stageList[idx].auditorList) {
  112. if (audit.aid === this.ctx.session.sessionUser.accountId) {
  113. isAudit = true;
  114. break;
  115. }
  116. }
  117. if (isAudit) {
  118. stageList.splice(idx, 1);
  119. }
  120. }
  121. }
  122. }
  123. let lastAuditor = null;
  124. if (stage) {
  125. lastAuditor = await ctx.service.stageAudit.getLastestAuditor(ctx.stage.id, ctx.stage.times, auditConst.stage.status.checked);
  126. } else {
  127. lastAuditor = stageList.length > 0 ? await ctx.service.stageAudit.getLastestAuditor(stageList[stageList.length - 1].id, stageList[stageList.length - 1].times, auditConst.stage.status.checked) : null;
  128. }
  129. let archiveList = [];
  130. if (stage) {
  131. const archives = await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, ctx.stage.id);
  132. if (archives.length > 0) {
  133. archiveList = JSON.parse(archives[0].content);
  134. }
  135. } else {
  136. const archives = stageList.length > 0 ? await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, stageList[stageList.length - 1].id) : [];
  137. // console.log(archives);
  138. if (archives && archives.length > 0) {
  139. archiveList = JSON.parse(archives[0].content);
  140. }
  141. }
  142. // const archiveList = await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, ctx.stage.id);
  143. let prjAccList = await ctx.service.projectAccount.getAllAccountByProjectId(tender.data.project_id);
  144. const roleList = await ctx.service.signatureRole.getSignatureRolesByTenderId(tender.id);
  145. const usedList = await ctx.service.signatureUsed.getSignatureUsedByTenderId(tender.id);
  146. // ctx.session.sessionUser.is_admin
  147. // const pageShow = ctx.session.sessionProject.page_show;
  148. // 根据需求,如果开启了‘开启个人「签字」’功能,则只能是本身用户签名,管理员例外
  149. if (!ctx.session.sessionUser.is_admin && pageShow.individualSign === 1) {
  150. // 削减其他account
  151. const newAccList = [];
  152. for (const acc of prjAccList) {
  153. if (acc.id === this.ctx.session.sessionUser.accountId) {
  154. newAccList.push(acc);
  155. }
  156. }
  157. prjAccList = newAccList;
  158. }
  159. // 分类列表
  160. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.session.sessionProject.id);
  161. // 获取用户权限
  162. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  163. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  164. // 获取用户可查看的标段
  165. const tenderList = await this.ctx.service.tender.getList('', userPermission);
  166. for (const t of tenderList) {
  167. if (t.ledger_status === auditConst.ledger.status.checked) {
  168. t.lastStage = await this.ctx.service.stage.getLastestStage(t.id, true);
  169. // if (t.lastStage) {
  170. // await this.ctx.service.stage.checkStageGatherData(t.lastStage);
  171. // }
  172. }
  173. }
  174. // 获取汇总表,历史选择
  175. const cid = this.ctx.helper._.map(treeNodes, 'id');
  176. const customSelects = {};
  177. if (ctx.stage) {
  178. customSelects.audit_select = await ctx.service.rptCustomDefine.getCustomSelectByRpt(cid,
  179. reportConst.rptCustomType[JV.NODE_CUS_AUDIT_SELECT], ctx.tender.id, ctx.stage.id);
  180. customSelects.stageFlow = await ctx.service.stageAudit.getAuditGroupByListWithOwner(
  181. ctx.stage.stage_id, ctx.stage.stage_times);
  182. } else {
  183. customSelects.audit_select = [];
  184. }
  185. customSelects.gather_select = await ctx.service.rptCustomDefine.getCustomSelectByRpt(cid,
  186. reportConst.rptCustomType[JV.NODE_CUS_GATHER_SELECT], ctx.tender.id, -1);
  187. customSelects.stage_select = await ctx.service.rptCustomDefine.getCustomSelectByRpt(cid,
  188. reportConst.rptCustomType[JV.NODE_CUS_STAGE_SELECT], ctx.tender.id, -1);
  189. const dataSelects = {};
  190. dataSelects.material_select = await ctx.service.rptCustomDefine.getDataSelectByRpt(cid,
  191. reportConst.rptDataType[JV.NODE_CUS_MATERIAL_SELECT]);
  192. const materialList = await ctx.service.material.getValidMaterials(ctx.tender.id);
  193. // const allTpls = await ctx.service.rptTpl.getAllTplByIds(tmpRptIds);
  194. // for (const tpl of allTpls) {
  195. // const fName = tpl.id + '_' + tpl.rpt_tpl_name + '.js';
  196. // console.log('fName: ' + fName);
  197. // fsUtil.writeObjToFile(tpl, 'D:/Temp/计量临时/' + fName);
  198. // }
  199. // 完成历史任务,下一批
  200. // const filePath = 'D:/Temp/计量临时/';
  201. // const tplFiles = [];
  202. // fs.readdir(filePath, (err, files) => {
  203. // if (err) throw err;
  204. // files.forEach(file => {
  205. // const fPath = filePath + file;
  206. // // console.log('\'' + fPath + '\'');
  207. // tplFiles.push(fPath);
  208. // });
  209. // });
  210. //
  211. // setTimeout(function() {
  212. // console.log(tplFiles);
  213. // }, 2000);
  214. // 下一批
  215. // let cnt = 0;
  216. // for (const file of rptFiles) {
  217. // const tplObj = JSON.parse(fs.readFileSync(file, 'utf-8'));
  218. // const rst = await ctx.service.rptTpl.insertRptTpl(tplObj);
  219. // cnt++;
  220. // console.log(file);
  221. // }
  222. // console.log(cnt + ' 完成!');
  223. // console.log(maxStageAmt[0].maxAmt);
  224. // 备注:在报表界面初始化的时候,暂时不需要check stage,只有在点中某张表的时候才需要(签名交互需要)
  225. // if ((stage === null || stage === undefined) && stageList.length > 0) {
  226. // await ctx.service.stage.checkStage(stageList[stageList.length - 1].id);
  227. // stage = ctx.stage;
  228. // console.log(stage);
  229. // }
  230. if (stage !== null && stage !== undefined) {
  231. stage_id = stage.id;
  232. stage_order = stage.order;
  233. sorder = stage.curOrder;
  234. stage_times = stage.times;
  235. stage_status = stage.status;
  236. }
  237. for (const prjAcc of prjAccList) {
  238. prjAcc.account_group = accountGroup[prjAcc.account_group];
  239. }
  240. let rpt_tpl_items = '{ customize: [], common: [] }';
  241. if (custTreeNodes.length > 0) {
  242. rpt_tpl_items = custTreeNodes[0].rpt_tpl_items;
  243. }
  244. // 获取所有项目参与者
  245. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  246. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  247. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  248. });
  249. const newAccountGroup = accountGroup.map((item, idx) => {
  250. const groupList = accountList.filter(item => item.account_group === idx);
  251. return { groupName: item, groupList };
  252. });
  253. const renderData = {
  254. accountGroup: newAccountGroup,
  255. accountList,
  256. tender: tender.data,
  257. tenderInfo: tender.info,
  258. rpt_tpl_data: JSON.stringify(treeNodes),
  259. cust_tpl_data: rpt_tpl_items,
  260. cust_cfg: JSON.stringify(custCfg),
  261. project_id: tender.data.project_id,
  262. tender_id: tender.id,
  263. tender_name: tender.data.name,
  264. stg_id: stage_id,
  265. stg_order: stage_order,
  266. cur_order: sorder,
  267. stg_times: stage_times,
  268. stg_status: stage_status,
  269. stage_list: JSON.stringify(stageList),
  270. prj_account_list: JSON.stringify(prjAccList),
  271. role_list: JSON.stringify(roleList),
  272. used_list: JSON.stringify(usedList),
  273. tenderMenu,
  274. preUrl: '/tender/' + ctx.tender.id + '/measure/stage/' + ctx.params.order,
  275. measureType,
  276. categoryData,
  277. tenderList,
  278. auditConst: auditConst.stage,
  279. ledgerAuditConst: auditConst.ledger,
  280. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.report.main),
  281. customSelects,
  282. rptCustomType: reportConst.rptCustomType,
  283. materialList,
  284. stages: stageList,
  285. dataSelects,
  286. // pageShow: JSON.stringify(pageShow),
  287. pageShow,
  288. authMobile: accountInfo.auth_mobile,
  289. shenpiConst,
  290. archiveList,
  291. lastAuditor,
  292. };
  293. await this.layout('report/index.ejs', renderData, 'report/rpt_all_popup.ejs');
  294. // await this.layout('report/index.ejs', renderData);
  295. // 在这里启动一个cron job,删除临时创建的excel文件
  296. if (!rptCronJob.started) {
  297. try {
  298. // console.log('rptCronJob.started: ' + rptCronJob.started);
  299. rptCronJob.started = true;
  300. // console.log('this.app.baseDir: ' + this.app.baseDir);
  301. rptCronJob.createJob(null, this.app.baseDir + '/app/public/download');
  302. } catch (ex) {
  303. rptCronJob.started = false;
  304. }
  305. }
  306. } catch (err) {
  307. this.log(err);
  308. ctx.redirect('/tender/' + ctx.tender.id + '/measure/stage');
  309. }
  310. }
  311. /**
  312. * 报表打印页面
  313. *
  314. * @param {Object} ctx - egg全局context
  315. * @return {void}
  316. */
  317. async showPrintPage(ctx) {
  318. try {
  319. // const params = JSON.parse(ctx.request.body.params);
  320. const pageSize = ctx.params.size;
  321. // console.log('pageSize: ' + pageSize);
  322. const renderData = {
  323. size: pageSize,
  324. // orientation: params.orientation,
  325. };
  326. await ctx.render('report/rpt_print.ejs', renderData);
  327. } catch (err) {
  328. this.log(err);
  329. console.log(err);
  330. }
  331. }
  332. async showIndividualPage(ctx) {
  333. try {
  334. // const params = JSON.parse(ctx.request.body.params);
  335. const pageSize = ctx.params.size;
  336. // console.log('pageSize: ' + pageSize);
  337. const renderData = {
  338. size: pageSize,
  339. // orientation: params.orientation,
  340. };
  341. await ctx.render('report/rpt_individual.ejs', renderData);
  342. } catch (err) {
  343. this.log(err);
  344. console.log(err);
  345. }
  346. }
  347. async _getReport(ctx, params) {
  348. // console.log('in getReport');
  349. // console.log(params);
  350. let rptTpl = await ctx.service.rptTpl.getTplById(params.rpt_tpl_id);
  351. if (!rptTpl || rptTpl.length !== 1) {
  352. throw '获取模板失败';
  353. }
  354. rptTpl = JSON.parse(rptTpl[0].rpt_content);
  355. // console.log('get the template!');
  356. const customSelect = rptTpl[JV.NODE_CUSTOM_DEFINE] && rptTpl[JV.NODE_CUSTOM_DEFINE][JV.NODE_CUS_AUDIT_SELECT].enable
  357. ? await ctx.service.rptCustomDefine.getCustomDefine(params.tender_id, params.stage_id, params.rpt_tpl_id)
  358. : await ctx.service.rptCustomDefine.getCustomDefine(params.tender_id, -1, params.rpt_tpl_id);
  359. const copyCustomSelect = this.ctx.helper.clone(customSelect);
  360. if (!params.gather_select && copyCustomSelect) delete copyCustomSelect.gather_select;
  361. if (!params.stage_select && copyCustomSelect) delete copyCustomSelect.stage_select;
  362. const pageRst = await getAllPagesCommon(ctx, rptTpl, params, JV.PAGING_OPTION_NORMAL, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir, copyCustomSelect);
  363. for (const page of pageRst.items) {
  364. page[JV.PROP_WATERMARK_CELLS] = [];
  365. }
  366. let waterMarkStr = null;
  367. if (params.stage_status !== 3 && params.closeWatermark === 0) {
  368. // 加水印
  369. fillWaterMark([pageRst]);
  370. if (params.needWaterMark) {
  371. waterMarkStr = await getWatermarkPicData(pageRst, this.app.baseDir);
  372. }
  373. }
  374. // console.log(pageRst);
  375. // const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id)) : [];
  376. const roleRel = await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id, params.stage_id); // 新需求中,允许在非审核状态下设置签名
  377. const stgAudit = await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times);
  378. const stgAuditForOrg = await ctx.service.stageAudit.getStageAudit(params.stage_id, 1);
  379. // console.log('after role stage!');
  380. // console.log(roleRel);
  381. await encodeSignatureDataUri(roleRel, this.app.baseDir);
  382. if (params.getPicFlag) {
  383. await encodeDummySignatureDataUri(pageRst, this.app.baseDir); // 注意草图数据量问题!
  384. }
  385. const stageFlow = await ctx.service.stageAudit.getAuditGroupByListWithOwner(params.stage_id, params.stage_times);
  386. // console.log('encodeSignatureDataUri!');
  387. return {
  388. data: pageRst,
  389. signatureRelInfo: roleRel,
  390. stageAudit: stgAudit,
  391. stageAuditOrg: stgAuditForOrg,
  392. debugInfo: ctx.app.config.is_debug ? ctx.debugInfo : null,
  393. customDefine: rptTpl[JV.NODE_CUSTOM_DEFINE],
  394. stageFlow,
  395. customSelect,
  396. waterMarkStr,
  397. };
  398. }
  399. /**
  400. * 获取报表数据
  401. *
  402. * @param {Object} ctx - egg全局context
  403. * @return {void}
  404. */
  405. async getReport(ctx) {
  406. try {
  407. // console.log('in getReport');
  408. const params = JSON.parse(ctx.request.body.params);
  409. ctx.body = await this._getReport(ctx, params);
  410. // // console.log(params);
  411. // let rptTpl = await ctx.service.rptTpl.getTplById(params.rpt_tpl_id);
  412. // if (!rptTpl || rptTpl.length !== 1) {
  413. // throw '获取模板失败';
  414. // }
  415. // rptTpl = JSON.parse(rptTpl[0].rpt_content);
  416. // // console.log('get the template!');
  417. // const pageRst = await getAllPagesCommon(ctx, rptTpl, params, JV.PAGING_OPTION_NORMAL, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir);
  418. // // console.log(pageRst);
  419. // // const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id)) : [];
  420. // const roleRel = await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id); // 新需求中,允许在非审核状态下设置签名
  421. // const stgAudit = await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times);
  422. // // console.log('after role stage!');
  423. // // console.log(roleRel);
  424. // await encodeSignatureDataUri(roleRel, this.app.baseDir);
  425. // await encodeDummySignatureDataUri(pageRst, this.app.baseDir);
  426. // const stageFlow = await ctx.service.stageAudit.getAuditGroupByListWithOwner(params.stage_id, params.stage_times);
  427. // const customSelect = await ctx.service.rptCustomDefine.getDataByCondition({
  428. // tid: params.tender_id, sid: params.stage_id, rid: params.rpt_tpl_id
  429. // });
  430. //
  431. // // console.log('encodeSignatureDataUri!');
  432. // ctx.body = {
  433. // data: pageRst,
  434. // signatureRelInfo: roleRel,
  435. // stageAudit: stgAudit,
  436. // debugInfo: ctx.app.config.is_debug ? ctx.debugInfo : null,
  437. // customDefine: rptTpl[JV.NODE_CUSTOM_DEFINE],
  438. // stageFlow,
  439. // customSelect,
  440. // };
  441. // // ctx.body = { data: { msg: 'test the network' } };
  442. ctx.status = 201;
  443. } catch (ex) {
  444. console.log(ex);
  445. this.setMessage(ex.toString(), this.messageType.ERROR);
  446. }
  447. }
  448. /**
  449. * 获取多批次报表数据
  450. *
  451. * @param {Object} ctx - egg全局context
  452. * @return {void}
  453. */
  454. async getMultiReportsEx(ctx) {
  455. // 原则说明:把所有报表模板集中获取,统一filter,只读一次数据!
  456. const params = JSON.parse(ctx.request.body.params);
  457. const pageShow = ctx.session.sessionProject.page_show;
  458. if (pageShow !== null && pageShow !== undefined && parseInt(pageShow.closeExportPdf) === 1) {
  459. ctx.body = { data: [], signatureRelInfo: [], stageAudit: [] };
  460. ctx.status = 201;
  461. return;
  462. }
  463. await this._saveCustomSelects(params);
  464. const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir) || [];
  465. for (const pageRst of pageRstArr) {
  466. for (const page of pageRst.items) {
  467. page[JV.PROP_WATERMARK_CELLS] = [];
  468. }
  469. }
  470. let waterMarkStr = null;
  471. if (params.stage_status !== 3 && params.closeWatermark === 0) {
  472. // 加水印
  473. fillWaterMark(pageRstArr);
  474. if (params.needWaterMark) {
  475. waterMarkStr = await getWatermarkPicData(pageRstArr[0], this.app.baseDir);
  476. }
  477. }
  478. const stgAudit = await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times);
  479. const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids, params.stage_id)) : [];
  480. // const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id)) : [];
  481. roleRel.sort(function(rr1, rr2) {
  482. return params.rpt_ids.indexOf(parseInt(rr1.rpt_id)) - params.rpt_ids.indexOf(parseInt(rr2.rpt_id));
  483. });
  484. // console.log('roleRel: ');
  485. // console.log(roleRel);
  486. await encodeSignatureDataUri(roleRel, this.app.baseDir);
  487. if (params.getPicFlag) {
  488. for (const pageRst of pageRstArr) {
  489. await encodeDummySignatureDataUri(pageRst, this.app.baseDir); // 注意草图数据量问题!
  490. }
  491. }
  492. // console.log(pageRstArr);
  493. ctx.body = { data: pageRstArr, signatureRelInfo: roleRel, stageAudit: stgAudit, waterMarkStr };
  494. ctx.status = 201;
  495. }
  496. async _saveCustomSelects(params) {
  497. for (const [i, cs] of params.customSelect.entries()) {
  498. if (!cs) continue;
  499. cs.tender_id = params.tender_id;
  500. cs.stage_id = params.stage_id;
  501. cs.rpt_tpl_id = params.rpt_ids[i];
  502. await this.ctx.service.rptCustomDefine.saveCustomSelect(cs);
  503. }
  504. }
  505. async createExcelFilesEx(ctx) {
  506. const params = JSON.parse(ctx.request.body.params);
  507. const pageShow = ctx.session.sessionProject.page_show;
  508. if (pageShow !== null && pageShow !== undefined && parseInt(pageShow.closeExportExcel) === 1) {
  509. ctx.body = { data: [] };
  510. ctx.status = 201;
  511. return;
  512. }
  513. await this._saveCustomSelects(params);
  514. const baseDir = this.app.baseDir;
  515. function getExcelByPageData(pageRst, rpt_name, innerRoleRel) {
  516. return new Promise(function(resolve, reject) {
  517. rpt_xl_util.exportExcel(pageRst, params.pageSize, rpt_name, params.isOneSheet, null, null, baseDir, innerRoleRel,
  518. (err, uuidName) => {
  519. if (err) return reject(err);
  520. const fileRst = { uuid: uuidName, reportName: rpt_name.replace(new RegExp('%', 'gm'), '_') };
  521. resolve(fileRst);
  522. }
  523. );
  524. });
  525. }
  526. const stgAudit = (params.stage_status === 3) ? (await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times)) : [];
  527. const stgAuditForOrg = (params.stage_status === 3) ? (await ctx.service.stageAudit.getStageAudit(params.stage_id, 1)) : [];
  528. const stageList = (params.stage_status === 3) ? (await ctx.service.stage.getValidStagesShort(params.tender_id)) : [];
  529. const roleRelArr = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids, params.stage_id)) : [];
  530. // const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id)) : [];
  531. const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, baseDir);
  532. // console.log('params.stage_status: ' + params.stage_status);
  533. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/testBuiltPageResult1.js');
  534. for (const pageRst of pageRstArr) {
  535. for (const page of pageRst.items) {
  536. page[JV.PROP_WATERMARK_CELLS] = [];
  537. }
  538. }
  539. let waterMarkStr = null;
  540. if (params.stage_status !== 3 && params.closeWatermark === 0) {
  541. // if (params.stage_status !== 3 && params.closeWatermark === 0 && params.option === JV.PAGING_OPTION_NORMAL) {
  542. // 加水印(注意:还得看用户设置是否需要加水印)
  543. fillWaterMark(pageRstArr);
  544. if (params.needWaterMark) {
  545. waterMarkStr = await getWatermarkPicData(pageRstArr[0], baseDir);
  546. }
  547. }
  548. await this.ctx.helper.recursiveMkdirSync(baseDir + '/app/public/download');
  549. const runnableRst = [];
  550. /*
  551. for (let idx = 0; idx < pageRstArr.length; idx++) {
  552. let roleRel = null;
  553. for (const roleR of roleRelArr) {
  554. if (roleR.rpt_id === params.rpt_ids[idx]) {
  555. roleRel = JSON.parse(roleR.rel_content);
  556. break;
  557. }
  558. }
  559. // console.log('roleRel.rel_content: ' + roleRel.rel_content);
  560. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/testBuiltPageResult.jsp');
  561. if (params.stage_status === 3) {
  562. mergeSignAudit(pageRstArr[idx], roleRel, stgAudit);
  563. mergeSignDate(pageRstArr[idx], params.stage_id, roleRel, stgAudit, stgAuditForOrg, stageList);
  564. }
  565. runnableRst.push(getExcelByPageData(pageRstArr[idx], params.rpt_names[idx], roleRel));
  566. }
  567. /*/
  568. // 1. merge签名相关信息
  569. // const pageBrkSize = 6291456; // 6M, 1024*1024*6
  570. const pageBrkSize = 62914560; // 60M, 1024*1024*6*10
  571. for (let idx = 0; idx < pageRstArr.length; idx++) {
  572. let roleRel = null;
  573. for (const roleR of roleRelArr) {
  574. if (roleR.rpt_id === params.rpt_ids[idx]) {
  575. roleRel = JSON.parse(roleR.rel_content);
  576. break;
  577. }
  578. }
  579. if (params.stage_status === 3) {
  580. mergeSignAudit(pageRstArr[idx], roleRel, stgAudit);
  581. mergeSignDate(pageRstArr[idx], params.stage_id, roleRel, stgAudit, stgAuditForOrg, stageList);
  582. }
  583. // 2. 判断草图大小是否需要分页
  584. const picSize = await _chkRawPicSize(pageRstArr[idx], baseDir);
  585. // const picSize = 262914560; // 测试用,强制分页
  586. if (picSize > pageBrkSize) {
  587. // let pageBrkAmt = Math.ceil(picSize / pageBrkSize);
  588. let pageBrkAmt = Math.floor(pageRstArr[idx].items.length / Math.ceil(picSize / pageBrkSize));
  589. if (pageBrkAmt < 10) {
  590. // 不用管
  591. } else if (pageBrkAmt < 100) {
  592. pageBrkAmt = Math.floor(pageBrkAmt / 10) * 10;
  593. } else if (pageBrkAmt < 1000) {
  594. pageBrkAmt = Math.floor(pageBrkAmt / 100) * 100;
  595. } else {
  596. // 暂时不管
  597. }
  598. const newRptNames = []; // 这个为导出控制分页用
  599. const newPageDataRst = _resetPageDataByBreaks([pageRstArr[idx]], pageBrkAmt, [params.rpt_names[idx]], newRptNames);
  600. for (let dtlIdx = 0; dtlIdx < newPageDataRst.length; dtlIdx++) {
  601. runnableRst.push(getExcelByPageData(newPageDataRst[dtlIdx], newRptNames[dtlIdx], roleRel));
  602. }
  603. } else {
  604. runnableRst.push(getExcelByPageData(pageRstArr[idx], params.rpt_names[idx], roleRel));
  605. }
  606. }
  607. // */
  608. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/计量导出pageArr.js');
  609. const uuidRst = await Promise.all(runnableRst);
  610. ctx.body = { data: uuidRst, waterMarkStr };
  611. ctx.status = 201;
  612. }
  613. _getExcelByPageData(pageRst, rpt_name, pageSize, baseDir, innerRoleRel) {
  614. return new Promise(function(resolve, reject) {
  615. rpt_xl_util.exportExcel(pageRst, pageSize, rpt_name, true, null, null, baseDir, innerRoleRel,
  616. (err, uuidName) => {
  617. if (err) return reject(err);
  618. const fileRst = { uuid: uuidName, reportName: rpt_name.replace(new RegExp('%', 'gm'), '_') };
  619. resolve(fileRst);
  620. }
  621. );
  622. });
  623. }
  624. async createExcelFilesFromDirectData(ctx) {
  625. const params = JSON.parse(ctx.request.body.params);
  626. const names = params.rptNames;
  627. const pageDataArr = params.pageData;
  628. const runnableRst = [];
  629. for (let idx = 0; idx < names.length; idx++) {
  630. runnableRst.push(this._getExcelByPageData(pageDataArr[idx], names[idx], 'A4', this.app.baseDir, null));
  631. }
  632. const uuidRst = await Promise.all(runnableRst);
  633. ctx.body = { data: uuidRst, waterMarkStr: null };
  634. ctx.status = 201;
  635. }
  636. async createExcelFilesFromLocal(ctx) {
  637. const baseDir = this.app.baseDir;
  638. function getExcelByPageData(pageRst, rpt_name, innerRoleRel) {
  639. return new Promise(function(resolve, reject) {
  640. rpt_xl_util.exportExcel(pageRst, 'A4', rpt_name, true, null, null, baseDir, innerRoleRel,
  641. (err, uuidName) => {
  642. if (err) return reject(err);
  643. const fileRst = { uuid: uuidName, reportName: rpt_name.replace(new RegExp('%', 'gm'), '_') };
  644. resolve(fileRst);
  645. }
  646. );
  647. });
  648. }
  649. try {
  650. console.log('start:');
  651. const pageRst = JSON.parse(fs.readFileSync('D:/GitHome/temp/计量excel原始数据_A3BZ6_5384_112表(清单).js', 'utf-8'));
  652. console.log('read it');
  653. const runnableRst = [];
  654. const pageBrkSize = 6291456; // 6M, 1024*1024*6
  655. // const pageBrkSize = 1048576; // 1M, 1024*1024*1
  656. console.log('pageBrkSize:' + pageBrkSize);
  657. const picSize = await _chkRawPicSize(pageRst, baseDir);
  658. console.log('picSize:' + picSize);
  659. if (picSize > pageBrkSize) {
  660. let pageBrkAmt = Math.floor(pageRst.items.length / Math.ceil(picSize / pageBrkSize));
  661. console.log('pageBrkAmt:' + pageBrkAmt);
  662. if (pageBrkAmt < 10) {
  663. // 不用管
  664. } else if (pageBrkAmt < 100) {
  665. pageBrkAmt = Math.floor(pageBrkAmt / 10) * 10;
  666. } else if (pageBrkAmt < 1000) {
  667. pageBrkAmt = Math.floor(pageBrkAmt / 100) * 100;
  668. } else {
  669. // 暂时不管
  670. }
  671. const newRptNames = []; // 这个为导出控制分页用
  672. const newPageDataRst = _resetPageDataByBreaks([pageRst], pageBrkAmt, ['工程计量表(计量单元-清单)'], newRptNames);
  673. for (let dtlIdx = 0; dtlIdx < newPageDataRst.length; dtlIdx++) {
  674. runnableRst.push(getExcelByPageData(newPageDataRst[dtlIdx], newRptNames[dtlIdx], null));
  675. }
  676. } else {
  677. runnableRst.push(getExcelByPageData(pageRst, '工程计量表(计量单元-清单)', null));
  678. }
  679. const uuidRst = await Promise.all(runnableRst);
  680. ctx.body = { data: uuidRst, waterMarkStr: null };
  681. ctx.status = 201;
  682. } catch (ex) {
  683. console.log(ex.toString());
  684. ctx.status = 500;
  685. }
  686. }
  687. async createExcelFilesInOneBookEx(ctx) {
  688. const params = JSON.parse(ctx.request.body.params);
  689. const baseDir = this.app.baseDir;
  690. function getExcelByPageData(pageRstArr, rpt_name, innerRoleRel) {
  691. return new Promise(function(resolve, reject) {
  692. rpt_xl_util.exportExcelInOneBook(pageRstArr, params.pageSize, rpt_name, baseDir, innerRoleRel,
  693. (err, uuidName) => {
  694. if (err) return reject(err);
  695. const fileRst = { uuid: uuidName, reportName: rpt_name };
  696. resolve(fileRst);
  697. }
  698. );
  699. });
  700. }
  701. const roleRelArr = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids, params.stage_id)) : [];
  702. // console.log(`baseDir: ${baseDir}`);
  703. // fsUtil.writeObjToFile(roleRelArr, 'D:/GitHome/temp/签名信息.js');
  704. const stgAudit = (params.stage_status === 3) ? (await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times)) : [];
  705. const stgAuditForOrg = (params.stage_status === 3) ? (await ctx.service.stageAudit.getStageAudit(params.stage_id, 1)) : [];
  706. const stageList = (params.stage_status === 3) ? (await ctx.service.stage.getValidStagesShort(params.tender_id)) : [];
  707. const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir);
  708. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/pageRstArrForExcel.js');
  709. for (const pageRst of pageRstArr) {
  710. for (const page of pageRst.items) {
  711. page[JV.PROP_WATERMARK_CELLS] = [];
  712. }
  713. }
  714. let waterMarkStr = null;
  715. if (params.stage_status !== 3 && params.closeWatermark === 0) {
  716. // if (params.stage_status !== 3 && params.closeWatermark === 0 && params.option === JV.PAGING_OPTION_NORMAL) {
  717. // 加水印
  718. fillWaterMark(pageRstArr);
  719. if (params.needWaterMark) {
  720. waterMarkStr = await getWatermarkPicData(pageRstArr[0], this.app.baseDir);
  721. }
  722. }
  723. await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  724. const runnableRst = [];
  725. let rptRoleRelArr = [];
  726. const reAssignSignatureName = function(pageData, roleRel, rpt_name_key) {
  727. const keyMap = {};
  728. for (const pageItem of pageData.items) {
  729. for (const signCell of pageItem[JV.PROP_SIGNATURE_CELLS]) {
  730. if (!keyMap.hasOwnProperty(signCell.signature_name)) {
  731. keyMap[signCell.signature_name] = rpt_name_key + '_' + signCell.signature_name;
  732. }
  733. }
  734. }
  735. for (const pageItem of pageData.items) {
  736. for (const signCell of pageItem[JV.PROP_SIGNATURE_CELLS]) {
  737. if (keyMap.hasOwnProperty(signCell.signature_name)) {
  738. signCell.signature_name = keyMap[signCell.signature_name];
  739. }
  740. }
  741. }
  742. for (const roleSign of roleRel) {
  743. if (keyMap.hasOwnProperty(roleSign.signature_name)) {
  744. roleSign.signature_name = keyMap[roleSign.signature_name];
  745. }
  746. }
  747. // console.log('keyMap of : ' + rpt_name_key);
  748. // console.log(keyMap);
  749. };
  750. for (let idx = 0; idx < pageRstArr.length; idx++) {
  751. let roleRel = null;
  752. for (const roleR of roleRelArr) {
  753. if (roleR.rpt_id === params.rpt_ids[idx]) {
  754. roleRel = JSON.parse(roleR.rel_content);
  755. if (params.stage_status === 3) {
  756. mergeSignAudit(pageRstArr[idx], roleRel, stgAudit);
  757. mergeSignDate(pageRstArr[idx], params.stage_id, roleRel, stgAudit, stgAuditForOrg, stageList);
  758. }
  759. // 这里要做些电子签名的signature_name转换,以防重名
  760. reAssignSignatureName(pageRstArr[idx], roleRel, params.rpt_names[idx]);
  761. rptRoleRelArr = rptRoleRelArr.concat(roleRel);
  762. break;
  763. }
  764. }
  765. }
  766. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/pageRstArrForExcelAfterReAssign.js');
  767. // fsUtil.writeObjToFile(rptRoleRelArr, 'D:/GitHome/temp/签名信息_新.js');
  768. runnableRst.push(getExcelByPageData(pageRstArr, params.rptName, rptRoleRelArr));
  769. const uuidRst = await Promise.all(runnableRst);
  770. ctx.body = { data: uuidRst, waterMarkStr };
  771. ctx.status = 201;
  772. }
  773. async getFileByUUID(ctx) {
  774. // console.log('downloading : ' + ctx.params.uuid);
  775. const uuid = ctx.params.uuid;
  776. const rptName = ctx.params.rptName;
  777. const suffix = '.' + ctx.params.suffix;
  778. try {
  779. const rptNameURI = encodeURI(rptName);
  780. const filePath = this.app.baseDir + '/app/public/download/';
  781. // console.log('filePath: ' + filePath);
  782. // await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  783. ctx.set({
  784. 'Content-Type': 'application/vnd.openxmlformats',
  785. 'Content-Disposition': 'attachment; filename="' + rptNameURI + suffix + "\"; filename*=utf-8''" + rptNameURI + suffix,
  786. });
  787. ctx.body = await fs.readFileSync(filePath + uuid + suffix);
  788. } catch (e) {
  789. console.log(e);
  790. }
  791. }
  792. /**
  793. * 更新用户通用报表显示配置
  794. *
  795. * @param {Object} ctx - egg全局context
  796. * @return {void}
  797. */
  798. async updateCustNode(ctx) {
  799. const params = JSON.parse(ctx.request.body.params);
  800. // console.log(params);
  801. const custId = this.ctx.session.sessionUser.accountId;
  802. const newNodeItems = JSON.stringify(params.nodeItems);
  803. const rst = await ctx.service.rptTreeNodeCust.updateCustNode(custId, newNodeItems);
  804. // console.log(rst);
  805. ctx.body = { data: rst };
  806. // ctx.body = { data: { msg: 'test the network' } };
  807. ctx.status = 201;
  808. }
  809. async setCustomDefine(ctx) {
  810. try {
  811. const data = JSON.parse(ctx.request.body.data);
  812. await this.ctx.service.rptCustomDefine.saveCustomSelect(data);
  813. // const sid = data.gather_select ? -1 : data.stage_id;
  814. // const filter = {tid: data.tender_id, sid: sid, rid: data.rpt_tpl_id};
  815. // const count = await ctx.service.rptCustomDefine.count(filter);
  816. // const updateData = {};
  817. // if (data.audit_select) updateData.audit_select = JSON.stringify(data.audit_select);
  818. // if (data.gather_select) updateData.gather_select = JSON.stringify(data.gather_select);
  819. // if (count > 0) {
  820. // await ctx.service.rptCustomDefine.update(updateData, filter);
  821. // } else {
  822. // updateData.tid = data.tender_id;
  823. // updateData.sid = sid;
  824. // updateData.rid = data.rpt_tpl_id;
  825. // await ctx.service.rptCustomDefine.db.insert(ctx.service.rptCustomDefine.tableName, updateData);
  826. // }
  827. const result = await this._getReport(ctx, data);
  828. ctx.body = { err: 0, msg: '', data: result };
  829. } catch (err) {
  830. ctx.helper.log(err);
  831. ctx.body = this.ajaxErrorBody(err, '保存数据出错');
  832. }
  833. }
  834. }
  835. return ReportController;
  836. };
  837. async function checkStg(ctx, params) {
  838. if (ctx.stage === null || ctx.stage === undefined || parseInt(ctx.stage.id) !== parseInt(params.stage_id)) {
  839. await ctx.service.stage.checkStage(params.stage_id);
  840. if (ctx.stage) {
  841. params.stage_order = ctx.stage.curOrder;
  842. }
  843. }
  844. }
  845. async function getAllPagesCommon(ctx, rptTpl, params, option, outputType, baseDir, customSelect) {
  846. const rptDataUtil = new rptDataExtractor();
  847. rptDataUtil.initialize(rptTpl);
  848. const filter = rptDataUtil.getDataRequestFilter();
  849. const rawDataObj = await ctx.service.report.getReportData(params, filter.tables, filter.memFieldKeys,
  850. rptTpl[JV.NODE_CUSTOM_DEFINE], customSelect);
  851. // console.log(rawDataObj);
  852. try {
  853. const printCom = JpcEx.createNew();
  854. if (params.pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = params.pageSize;
  855. if (params.orientation && (params.orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = params.orientation;
  856. // let defProperties = await ctx.service.rptPreDefineCfg.getCfgById('Administrator');
  857. // defProperties = JSON.parse(defProperties[0].defined_content);
  858. const defProperties = RPT_DEF_PROPERTIES;
  859. // console.log('defProperties: ');
  860. // console.log(defProperties);
  861. // console.log('before assemble');
  862. // fsUtil.writeObjToFile(rawDataObj, 'D:/GitHome/temp/JL_Assemble_RawDataDataBefore' + (new Date()).getTime() + '.js');
  863. const tplData = rptDataUtil.assembleData(ctx, rawDataObj, baseDir, printCom, customSelect);
  864. // console.log(tplData);
  865. if (params.custCfg) {
  866. setupCustomizeCfg(params.custCfg, rptTpl, defProperties);
  867. } else {
  868. // setupCustomizeCfg(defProperties, rptTpl, defProperties);
  869. }
  870. const dftOption = params.option || JV.PAGING_OPTION_NORMAL;
  871. printCom.initialize(rptTpl);
  872. // 在ctx.helper里夹带私货,增加当前期status及times
  873. // ctx.helper.current_stage_status = params.stage_status;
  874. // ctx.helper.current_stage_times = params.stage_times;
  875. printCom.analyzeData(ctx.helper, rptTpl, tplData, defProperties, dftOption, outputType, customSelect);
  876. // console.log(JSON.stringify(rptTpl));
  877. const maxPages = printCom.totalPages;
  878. let pageRst = null;
  879. if (maxPages > 0) {
  880. pageRst = printCom.outputAsSimpleJSONPageArray(ctx.helper, rptTpl, tplData, 1, maxPages, defProperties, params.custCfg, customSelect);
  881. } else {
  882. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  883. }
  884. // fsUtil.writeObjToFile(pageRst, 'D:/GitHome/temp/计量testBuiltPageResult.jsp');
  885. // console.log(pageRst);
  886. return pageRst;
  887. } catch (ex) {
  888. console.log('报表数据异常, tender id: ' + params.tender_id);
  889. console.log(ex);
  890. }
  891. }
  892. async function getMultiRptsCommon(ctx, params, outputType, baseDir) {
  893. for (let idx = 0; idx < params.rpt_ids.length; idx++) {
  894. params.rpt_ids[idx] = parseInt(params.rpt_ids[idx]); // 转换一下,以防万一
  895. }
  896. const rptTpls = await ctx.service.rptTpl.getAllTplByIds(params.rpt_ids);
  897. rptTpls.sort(function(rpt1, rpt2) {
  898. return params.rpt_ids.indexOf(rpt1.id) - params.rpt_ids.indexOf(rpt2.id);
  899. });
  900. for (let rtIdx = 0; rtIdx < rptTpls.length; rtIdx++) {
  901. const id = rptTpls[rtIdx].id;
  902. rptTpls[rtIdx] = JSON.parse(rptTpls[rtIdx].rpt_content);
  903. rptTpls[rtIdx].id = id;
  904. }
  905. const rptDataUtil = new rptDataExtractor();
  906. const filterTables = [];
  907. const memFieldKeys = {};
  908. for (const rptTpl of rptTpls) {
  909. rptDataUtil.initialize(rptTpl);
  910. const filter = rptDataUtil.getDataRequestFilter();
  911. // console.log(filter);
  912. for (const table of filter.tables) {
  913. if (filterTables.indexOf(table) < 0 && needCustomTables.indexOf(table) < 0) {
  914. filterTables.push(table);
  915. }
  916. // memFieldKeys[table] = [];
  917. }
  918. for (const tableKeyProp in filter.memFieldKeys) {
  919. if (needCustomTables.indexOf(tableKeyProp) >= 0) continue;
  920. if (!memFieldKeys.hasOwnProperty(tableKeyProp)) {
  921. memFieldKeys[tableKeyProp] = [];
  922. }
  923. for (const mfKey of filter.memFieldKeys[tableKeyProp]) {
  924. if (memFieldKeys[tableKeyProp].indexOf(mfKey) < 0) {
  925. memFieldKeys[tableKeyProp].push(mfKey);
  926. }
  927. }
  928. }
  929. }
  930. const rawDataObj = await ctx.service.report.getReportData(params, filterTables, memFieldKeys, {}, {});
  931. try {
  932. const rptPageRstArray = [];
  933. // 1. 这里只用一份数据,根据实际应用情况,先备份
  934. // const backupData = {};
  935. // for (let filterIdx = 0; filterIdx < filterTables.length; filterIdx++) {
  936. // backupData[filterTables[filterIdx]] = [];
  937. // Object.assign(backupData[filterTables[filterIdx]], rawDataObj[filterTables[filterIdx]]);
  938. // }
  939. // 2. 一个一个模板创建数据
  940. // let defProperties = await ctx.service.rptPreDefineCfg.getCfgById('Administrator');
  941. // defProperties = JSON.parse(defProperties[0].defined_content);
  942. const defProperties = RPT_DEF_PROPERTIES;
  943. for (let tplIdx = 0; tplIdx < rptTpls.length; tplIdx++) {
  944. const rptTpl = (rptTpls[tplIdx]._doc) ? rptTpls[tplIdx]._doc : rptTpls[tplIdx];
  945. rptDataUtil.initialize(rptTpl);
  946. const customSelect = rptTpl[JV.NODE_CUSTOM_DEFINE] && rptTpl[JV.NODE_CUSTOM_DEFINE][JV.NODE_CUS_AUDIT_SELECT].enable
  947. ? await ctx.service.rptCustomDefine.getCustomDefine(params.tender_id, params.stage_id, rptTpl.id)
  948. : await ctx.service.rptCustomDefine.getCustomDefine(params.tender_id, -1, rptTpl.id);
  949. // 从汇总的rawDataObj中拷贝所需数据表至curRawDataObj,以供后续使用
  950. const curRawDataObj = {};
  951. const filter = rptDataUtil.getDataRequestFilter();
  952. for (const table of filter.tables) {
  953. curRawDataObj[table] = ctx.helper.clone(rawDataObj[table]);
  954. }
  955. // 如果是用户交互类型的表,则应该单独获取数据
  956. if ((params.customSelect && params.customSelect[tplIdx]) || rptTpl[JV.NODE_CUSTOM_DEFINE][JV.NODE_CUS_OPTION]) {
  957. const cfTables = [],
  958. cmFieldKeys = [];
  959. const curFilter = rptDataUtil.getDataRequestFilter();
  960. for (const table of curFilter.tables) {
  961. if (needCustomTables.indexOf(table) >= 0) {
  962. cfTables.push(table);
  963. }
  964. }
  965. for (const tableKeyProp in filter.memFieldKeys) {
  966. if (needCustomTables.indexOf(tableKeyProp) < 0) continue;
  967. if (!cmFieldKeys.hasOwnProperty(tableKeyProp)) {
  968. cmFieldKeys[tableKeyProp] = [];
  969. }
  970. for (const mfKey of filter.memFieldKeys[tableKeyProp]) {
  971. if (cmFieldKeys[tableKeyProp].indexOf(mfKey) < 0) {
  972. cmFieldKeys[tableKeyProp].push(mfKey);
  973. }
  974. }
  975. }
  976. const customRawDataObj = await ctx.service.report.getReportData(params, cfTables, cmFieldKeys,
  977. rptTpl[JV.NODE_CUSTOM_DEFINE], customSelect);
  978. for (const prop in customRawDataObj) {
  979. curRawDataObj[prop] = customRawDataObj[prop];
  980. }
  981. }
  982. const tplData = rptDataUtil.assembleData(ctx, curRawDataObj, baseDir, null, customSelect);
  983. const printCom = JpcEx.createNew();
  984. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = params.pageSize;
  985. if (params.pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = params.pageSize;
  986. if (params.orientation && (params.orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = params.orientation;
  987. if (params.custCfg) setupCustomizeCfg(params.custCfg, rptTpl, defProperties);
  988. const dftOption = params.option || JV.PAGING_OPTION_NORMAL;
  989. printCom.initialize(rptTpl);
  990. // console.log(rptTpl);
  991. printCom.analyzeData(ctx.helper, rptTpl, tplData, defProperties, dftOption, outputType, customSelect);
  992. const maxPages = printCom.totalPages;
  993. let pageRst = null;
  994. // console.log(maxPages);
  995. if (maxPages > 0) {
  996. pageRst = printCom.outputAsSimpleJSONPageArray(ctx.helper, rptTpl, tplData, 1, maxPages, defProperties, params.custCfg, customSelect);
  997. } else {
  998. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  999. }
  1000. rptPageRstArray.push(pageRst);
  1001. // 注意:这里需要把备份数据assign回去!!!
  1002. // if (tplIdx < rptTpls.length - 1) {
  1003. // for (let filterIdx = 0; filterIdx < filterTables.length; filterIdx++) {
  1004. // backupData[filterTables[filterIdx]] = [];
  1005. // Object.assign(rawDataObj[filterTables[filterIdx]], backupData[filterTables[filterIdx]]);
  1006. // }
  1007. // }
  1008. }
  1009. return rptPageRstArray;
  1010. } catch (ex) {
  1011. console.log('报表数据异常(getMultiRptsCommon): project_id ' + params.project_id + ', tender_id: ' + params.tender_id + ', stage_id: ' + params.stage_id);
  1012. console.log(ex);
  1013. } finally {
  1014. //
  1015. }
  1016. }
  1017. function setupCustomizeCfg(customizeCfg, rptTpl, defProperties) {
  1018. const tmpObj = {};
  1019. // 1. 字体
  1020. const newFonts = [];
  1021. for (const font of defProperties.fonts) {
  1022. const copyFont = {};
  1023. copyFont.ID = font.ID;
  1024. for (const fontProp of JV.FONT_PROPS) {
  1025. copyFont[fontProp] = font[fontProp];
  1026. }
  1027. newFonts.push(copyFont);
  1028. tmpObj[font.ID] = copyFont;
  1029. }
  1030. const private_setup_font = function(propStr, newFont) {
  1031. if (tmpObj[propStr]) {
  1032. tmpObj[propStr].Name = newFont.Name;
  1033. tmpObj[propStr].FontHeight = String(newFont.FontHeight);
  1034. tmpObj[propStr].FontBold = newFont.FontBold;
  1035. tmpObj[propStr].FontItalic = newFont.FontItalic;
  1036. tmpObj[propStr].FontUnderline = newFont.FontUnderline;
  1037. }
  1038. };
  1039. for (const custFont of customizeCfg.fonts) {
  1040. switch (custFont.CfgDispName) {
  1041. case '表标题':
  1042. private_setup_font('ReportTitle_Main', custFont);
  1043. break;
  1044. case '列标题':
  1045. private_setup_font('HeaderColumn', custFont);
  1046. private_setup_font('FooterColumn', custFont);
  1047. break;
  1048. case '正文内容':
  1049. private_setup_font('Content', custFont);
  1050. break;
  1051. case '合计':
  1052. private_setup_font('GrandTotal', custFont);
  1053. private_setup_font('SectionTotal', custFont);
  1054. break;
  1055. case '表眉/表脚':
  1056. private_setup_font('Header', custFont);
  1057. private_setup_font('Footer', custFont);
  1058. break;
  1059. default:
  1060. break;
  1061. }
  1062. }
  1063. // 1.1 窄体
  1064. if (tmpObj.Content_Narrow) {
  1065. if (customizeCfg.isNarrow) {
  1066. tmpObj.Content_Narrow.Name = 'Arial Narrow';
  1067. } else {
  1068. if (tmpObj.Content) {
  1069. tmpObj.Content_Narrow.Name = tmpObj.Content.Name;
  1070. } else {
  1071. tmpObj.Content_Narrow.Name = '宋体';
  1072. }
  1073. }
  1074. }
  1075. defProperties.fonts = newFonts;
  1076. // 2. 页边距
  1077. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT] = customizeCfg.margins[JV.PROP_LEFT] / 10;
  1078. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT] = customizeCfg.margins[JV.PROP_RIGHT] / 10;
  1079. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP] = customizeCfg.margins[JV.PROP_TOP] / 10;
  1080. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM] = customizeCfg.margins[JV.PROP_BOTTOM] / 10;
  1081. // 3. 边框宽度
  1082. if (customizeCfg.hasOwnProperty('borderThick')) {
  1083. for (let i = 0; i < defProperties.styles.length; i++) {
  1084. const style = defProperties.styles[i];
  1085. if (style.ID === 'BORDER_ALL_AROUND') {
  1086. for (const border of style.border_style) {
  1087. border.LineWeight = customizeCfg.borderThick;
  1088. }
  1089. }
  1090. }
  1091. }
  1092. // 4. 边框竖线
  1093. if (!(customizeCfg.showVerticalLine)) {
  1094. const private_copy_border = function(src) {
  1095. const rst = {};
  1096. rst.Position = src.Position;
  1097. rst.LineWeight = src.LineWeight;
  1098. rst.DashStyle = src.DashStyle;
  1099. rst.Color = src.Color;
  1100. return rst;
  1101. };
  1102. const newStyles = [];
  1103. for (let i = 0; i < defProperties.styles.length; i++) {
  1104. const style = defProperties.styles[i];
  1105. newStyles.push(style);
  1106. if (style.ID === 'BORDER_ALL_AROUND') {
  1107. const newStyle = {};
  1108. newStyle.ID = style.ID;
  1109. newStyle.CfgDispName = style.CfgDispName;
  1110. newStyle.border_style = [];
  1111. for (const border of style.border_style) {
  1112. const newBorder = private_copy_border(border);
  1113. newStyle.border_style.push(newBorder);
  1114. if (border.Position === 'Left' || border.Position === 'Right') {
  1115. newBorder.LineWeight = 0;
  1116. }
  1117. }
  1118. newStyles[newStyles.length - 1] = newStyle;
  1119. }
  1120. }
  1121. defProperties.styles = newStyles;
  1122. }
  1123. // 5. 补0
  1124. const private_Setup_Format = function(tabFields) {
  1125. if (tabFields) {
  1126. for (const tabField of tabFields) {
  1127. if (tabField[JV.PROP_FORMAT]) {
  1128. tabField[JV.PROP_FORMAT] = tabField[JV.PROP_FORMAT].replace(new RegExp('#', 'gm'), '0');
  1129. }
  1130. }
  1131. }
  1132. };
  1133. if (customizeCfg.fillZero) {
  1134. if (rptTpl[JV.NODE_FLOW_INFO]) {
  1135. 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]);
  1136. 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]);
  1137. 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]);
  1138. 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]);
  1139. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO]) {
  1140. for (const discrete of rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO]) {
  1141. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  1142. }
  1143. }
  1144. if (rptTpl[JV.NODE_FLOW_INFO_EX]) {
  1145. 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]);
  1146. 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]);
  1147. 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]);
  1148. 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]);
  1149. 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]);
  1150. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_DISCRETE_INFO]) {
  1151. for (const discrete of rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_DISCRETE_INFO]) {
  1152. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  1153. }
  1154. }
  1155. }
  1156. } else if (rptTpl[JV.NODE_BILL_INFO]) {
  1157. 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]);
  1158. if (rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO]) {
  1159. for (const discrete of rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO]) {
  1160. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  1161. }
  1162. }
  1163. } else if (rptTpl[JV.NODE_CROSS_INFO]) {
  1164. 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]);
  1165. 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]);
  1166. 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]);
  1167. 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]);
  1168. 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]);
  1169. 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]);
  1170. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO]) {
  1171. for (const discrete of rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO]) {
  1172. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  1173. }
  1174. }
  1175. }
  1176. }
  1177. }
  1178. function isFileExisted(file) {
  1179. return new Promise(function(resolve, reject) {
  1180. fs.access(file, err => {
  1181. if (err) {
  1182. reject(false);
  1183. } else {
  1184. resolve(true);
  1185. }
  1186. });
  1187. });
  1188. }
  1189. function fillWaterMark(pageRstArray) {
  1190. const orgWaterMarkWidth = 600;
  1191. const orgWaterMarkHeight = 288;
  1192. const createWaterCell = function(area) {
  1193. const w = area[JV.PROP_RIGHT] - area[JV.PROP_LEFT];
  1194. const h = area[JV.PROP_BOTTOM] - area[JV.PROP_TOP];
  1195. const left = Math.round(area[JV.PROP_LEFT] + w / 2 - orgWaterMarkWidth / 2);
  1196. const right = left + orgWaterMarkWidth;
  1197. const top = Math.round(area[JV.PROP_TOP] + h / 2 - orgWaterMarkHeight / 2);
  1198. const bottom = top + orgWaterMarkHeight;
  1199. return {
  1200. signature_name: JV.SIGNATURE_NAME_DUMMY_WATER_MARK,
  1201. path: '/public/images/not_Approve.png',
  1202. sign_path: '/public/images/not_Approve.png',
  1203. pic: null,
  1204. control: 'Title',
  1205. style: 'Default_None',
  1206. area: {
  1207. Left: left,
  1208. Right: right,
  1209. Top: top,
  1210. Bottom: bottom,
  1211. },
  1212. };
  1213. };
  1214. for (const pageRst of pageRstArray) {
  1215. for (const page of pageRst.items) {
  1216. page[JV.PROP_WATERMARK_CELLS] = [];
  1217. if (page[JV.PROP_PAGE_MERGE_BORDER]) {
  1218. const wmCell = createWaterCell(page[JV.PROP_PAGE_MERGE_BORDER]);
  1219. page[JV.PROP_WATERMARK_CELLS].push(wmCell);
  1220. } else if (page[JV.PAGE_SPECIAL_MERGE_POS]) {
  1221. if (page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_LEFT]) {
  1222. for (let i = 0; i < page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_LEFT].length; i++) {
  1223. const area = {};
  1224. area[JV.PROP_LEFT] = page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_LEFT][i];
  1225. area[JV.PROP_RIGHT] = page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_RIGHT][i];
  1226. area[JV.PROP_TOP] = page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_TOP][i];
  1227. area[JV.PROP_BOTTOM] = page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_BOTTOM][i];
  1228. const wmCell = createWaterCell(area);
  1229. page[JV.PROP_WATERMARK_CELLS].push(wmCell);
  1230. }
  1231. }
  1232. }
  1233. }
  1234. }
  1235. }
  1236. async function encodeSignatureDataUri(roleRel, baseDir) {
  1237. if (roleRel) {
  1238. for (const singleRoleRel of roleRel) {
  1239. if (singleRoleRel.rel_content !== null && singleRoleRel.rel_content !== undefined && singleRoleRel.rel_content !== '') {
  1240. const roleRelContent = JSON.parse(singleRoleRel.rel_content);
  1241. for (const role of roleRelContent) {
  1242. // console.log(role);
  1243. if (role.sign_path !== '') {
  1244. const filePath = baseDir + '/app' + role.sign_path;
  1245. try {
  1246. const res = await isFileExisted(filePath);
  1247. if (res) {
  1248. const bData = fs.readFileSync(filePath);
  1249. const base64Str = bData.toString('base64');
  1250. const datauri = 'data:image/png;base64,' + base64Str;
  1251. role.sign_pic = datauri;
  1252. } else {
  1253. console.log('文件不存在:' + filePath);
  1254. }
  1255. } catch (err) {
  1256. console.error(err);
  1257. }
  1258. }
  1259. }
  1260. singleRoleRel.rel_content = JSON.stringify(roleRelContent);
  1261. } else {
  1262. singleRoleRel.rel_content = [];
  1263. }
  1264. }
  1265. }
  1266. }
  1267. async function encodeDummySignatureDataUri(pageRst, baseDir) {
  1268. if (pageRst) {
  1269. for (const page of pageRst.items) {
  1270. for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1271. if (signature.signature_name === JV.SIGNATURE_NAME_DUMMY) {
  1272. if (signature.path !== '') {
  1273. const filePath = baseDir + '/app' + signature.path;
  1274. try {
  1275. const res = await isFileExisted(filePath);
  1276. if (res) {
  1277. const bData = fs.readFileSync(filePath);
  1278. const base64Str = bData.toString('base64');
  1279. const datauri = 'data:image/png;base64,' + base64Str;
  1280. signature.pic = datauri;
  1281. signature.path = '';
  1282. } else {
  1283. console.log('文件不存在:' + filePath);
  1284. }
  1285. } catch (err) {
  1286. console.error(err);
  1287. }
  1288. }
  1289. }
  1290. }
  1291. // 考虑优化,水印只需要一张即可,不需要每页都设置
  1292. // for (const signature of page[JV.PROP_WATERMARK_CELLS]) {
  1293. // if (signature.path !== '') {
  1294. // const filePath = baseDir + '/app' + signature.path;
  1295. // try {
  1296. // const res = await isFileExisted(filePath);
  1297. // if (res) {
  1298. // const bData = fs.readFileSync(filePath);
  1299. // const base64Str = bData.toString('base64');
  1300. // const datauri = 'data:image/png;base64,' + base64Str;
  1301. // signature.pic = datauri;
  1302. // signature.path = '';
  1303. // } else {
  1304. // console.log('文件不存在:' + filePath);
  1305. // }
  1306. // } catch (err) {
  1307. // console.error(err);
  1308. // }
  1309. // }
  1310. // }
  1311. }
  1312. }
  1313. }
  1314. async function getWatermarkPicData(pageRst, baseDir) {
  1315. let rst = '';
  1316. if (pageRst) {
  1317. for (const page of pageRst.items) {
  1318. for (const signature of page[JV.PROP_WATERMARK_CELLS]) {
  1319. if (signature.path !== '') {
  1320. const filePath = baseDir + '/app' + signature.path;
  1321. try {
  1322. const res = await isFileExisted(filePath);
  1323. if (res) {
  1324. const bData = fs.readFileSync(filePath);
  1325. const base64Str = bData.toString('base64');
  1326. rst = 'data:image/png;base64,' + base64Str;
  1327. break;
  1328. } else {
  1329. console.log('文件不存在:' + filePath);
  1330. }
  1331. } catch (err) {
  1332. console.error(err);
  1333. }
  1334. }
  1335. if (rst !== null) break;
  1336. }
  1337. }
  1338. }
  1339. return rst;
  1340. }
  1341. async function createExportRequestJob(ctx) {
  1342. const params = JSON.parse(ctx.request.body.params);
  1343. // const uuidArr = params.uuidArr;
  1344. const baseDir = this.app.baseDir;
  1345. const roleRelArr = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids, params.stage_id)) : [];
  1346. const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir);
  1347. await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  1348. for (let idx = 0; idx < pageRstArr.length; idx++) {
  1349. let roleRel = null;
  1350. for (const roleR of roleRelArr) {
  1351. if (roleR.rpt_id === params.rpt_ids[idx]) {
  1352. roleRel = JSON.parse(roleR.rel_content);
  1353. break;
  1354. }
  1355. }
  1356. if (params.exportType === 'xlsx') {
  1357. const zip = rpt_xl_util.exportExcel(pageRstArr[idx], params.pageSize, null, params.isOneSheet, null, null, baseDir, roleRel, null); // 给个null报表名称,返回zip对象自己处理,这样就不用改接口结构了
  1358. zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
  1359. .pipe(fs.createWriteStream(baseDir + '/app/public/download/' + params.uuidArr[idx] + '.xlsx'));
  1360. } else if (params.exportType === 'pdf') {
  1361. // PDF的后期补上
  1362. }
  1363. }
  1364. }
  1365. function mergeSignAudit(pageData, currRoleRelList, currAuditList) {
  1366. for (const page of pageData.items) {
  1367. if (page.signature_audit_cells) {
  1368. for (const sCell of page.signature_audit_cells) {
  1369. sCell.Value = ''; // 这里要先清除原有信息
  1370. if (currRoleRelList && currRoleRelList.length > 0) {
  1371. for (const role_rel of currRoleRelList) {
  1372. if (sCell.signature_name === role_rel.signature_name + '_审核意见') {
  1373. let preDate = '';
  1374. sCell.Value = '同意'; // 只有选择了签名的,才需要初始化一个默认的意见(之前的逻辑在有多个签名,哪怕只选择了一个,其他的意见都会有默认意见)
  1375. if (currAuditList && currAuditList.length > 0) {
  1376. for (const audit_rel of currAuditList) {
  1377. if (role_rel.acc_id === audit_rel.aid) {
  1378. if (audit_rel.end_time > preDate && audit_rel.status === 3) {
  1379. sCell.Value = audit_rel.opinion;
  1380. preDate = audit_rel.end_time;
  1381. }
  1382. // 不能break,实际会有多个审核意见,以最后一个为准
  1383. }
  1384. }
  1385. }
  1386. break;
  1387. }
  1388. }
  1389. }
  1390. }
  1391. }
  1392. }
  1393. }
  1394. function mergeSignDate(pageData, current_stage_id, currRoleRelList, STAGE_AUDIT, STAGE_AUDIT_ORG, STAGE_LIST) {
  1395. if (currRoleRelList && currRoleRelList.length > 0 && STAGE_AUDIT && STAGE_AUDIT.length > 0) {
  1396. for (let rridx = 0; rridx < currRoleRelList.length; rridx++) {
  1397. const role_rel = currRoleRelList[rridx];
  1398. if (role_rel.sign_date === undefined || role_rel.sign_date === null || role_rel.sign_date === '') {
  1399. const dftDate = _getSignDateByAllScenarios(role_rel.acc_id, current_stage_id, STAGE_AUDIT, STAGE_AUDIT_ORG, STAGE_LIST);
  1400. role_rel.sign_date = dftDate;
  1401. }
  1402. }
  1403. }
  1404. if (currRoleRelList && currRoleRelList.length > 0) {
  1405. for (const page of pageData.items) {
  1406. if (page.signature_date_cells) {
  1407. for (const sCell of page.signature_date_cells) {
  1408. sCell.Value = _getSignDateDftName();
  1409. for (const role_rel of currRoleRelList) {
  1410. if (sCell.signature_name === role_rel.signature_name + '_签字日期') {
  1411. if (role_rel.sign_date !== '') {
  1412. if (typeof role_rel.sign_date === 'string') {
  1413. role_rel.sign_date = new Date(role_rel.sign_date);
  1414. }
  1415. sCell.Value = role_rel.sign_date.Format(role_rel.sign_date_format);
  1416. }
  1417. break;
  1418. }
  1419. }
  1420. }
  1421. }
  1422. }
  1423. }
  1424. }
  1425. function _getSignDateByAllScenarios(userAccId, current_stage_id, STAGE_AUDIT, STAGE_AUDIT_ORG, STAGE_LIST) {
  1426. let rst = '';
  1427. let hasAudit = false;
  1428. for (const stg_audit of STAGE_AUDIT) {
  1429. if (stg_audit.aid === userAccId) {
  1430. hasAudit = true;
  1431. if (stg_audit.status === 3) {
  1432. rst = stg_audit.end_time;
  1433. } else {
  1434. rst = '';
  1435. }
  1436. // break; //因为实际业务中会有反复,所以就不break了,一直判断,以最后一个为准
  1437. }
  1438. }
  1439. let isOrgRpt = false;
  1440. for (const stg of STAGE_LIST) {
  1441. if (stg.id === current_stage_id) {
  1442. if (stg.user_id === userAccId) {
  1443. isOrgRpt = true;
  1444. }
  1445. break;
  1446. }
  1447. }
  1448. if (isOrgRpt && !hasAudit && STAGE_AUDIT_ORG && STAGE_AUDIT_ORG.length > 0) {
  1449. if (STAGE_AUDIT_ORG[0].begin_time && STAGE_AUDIT_ORG[0].begin_time !== '' && STAGE_AUDIT_ORG[0].begin_time.length > 20) {
  1450. rst = STAGE_AUDIT_ORG[0].begin_time;
  1451. }
  1452. }
  1453. if (!isOrgRpt && !hasAudit) {
  1454. // 非审批流程人员以及非原报,则显示期截至时间
  1455. for (const stg of STAGE_LIST) {
  1456. if (stg.id === current_stage_id && stg.period) {
  1457. const period = stg.period.split(' ~ ');
  1458. if (period.length === 2) {
  1459. rst = period[1];
  1460. }
  1461. }
  1462. }
  1463. }
  1464. return rst;
  1465. }
  1466. function _getSignDateDftName() {
  1467. return ' 年 月 日';
  1468. }
  1469. async function _chkRawPicSize(pageData, baseDir) {
  1470. let rst = 0;
  1471. const picKeys = [];
  1472. for (let pi = 0; pi < pageData.items.length; pi++) {
  1473. // 每页判断
  1474. for (let ci = 0; ci < pageData.items[pi][JV.PROP_SIGNATURE_CELLS].length; ci++) {
  1475. const picPath = pageData.items[pi][JV.PROP_SIGNATURE_CELLS][ci].path;
  1476. // if (picPath && picPath.indexOf('/sign') < 0 && picKeys.indexOf(picPath) < 0) {
  1477. if (picPath && picKeys.indexOf(picPath) < 0) {
  1478. // 签名图片也要计算在内,反正不重复
  1479. picKeys.push(picPath);
  1480. }
  1481. // signature_cells PROP_SIGNATURE_CELLS
  1482. }
  1483. }
  1484. // console.log(picKeys);
  1485. for (const pKey of picKeys) {
  1486. const filePath = `${baseDir}/app${pKey}`;
  1487. console.log('filePath: ' + filePath);
  1488. let isExist = false;
  1489. try {
  1490. isExist = await isFileExisted(filePath);
  1491. } catch (ex) {
  1492. isExist = false;
  1493. }
  1494. console.log('isExist: ' + isExist);
  1495. if (isExist) {
  1496. const obj = fs.statSync(filePath);
  1497. if (obj) {
  1498. rst += obj.size;
  1499. } else {
  1500. console.log('no statSync resutl: ' + filePath);
  1501. }
  1502. // rst += fs.statSync(filePath).size;
  1503. } else {
  1504. console.log(`${pKey} not exists!`);
  1505. }
  1506. }
  1507. return rst;
  1508. }
  1509. function _resetPageDataByBreaks(pageDataArr, breakAmt, rpt_names, newRptNames) {
  1510. const rst = [];
  1511. for (let pi = 0; pi < pageDataArr.length; pi++) {
  1512. const pageItem = pageDataArr[pi];
  1513. const currentRptName = rpt_names[pi];
  1514. if (pageItem.items.length > breakAmt) {
  1515. let pa = Math.floor(pageItem.items.length / breakAmt);
  1516. if (pageItem.items.length % breakAmt > 0) {
  1517. pa++;
  1518. }
  1519. for (let idx = 0; idx < pa; idx++) {
  1520. const newPageDataObj = { items: [] };
  1521. // newPageDataObj[JV.NODE_PAGE_INFO] = JSON.parse(JSON.stringify(pageItem[JV.NODE_PAGE_INFO]));
  1522. newPageDataObj[JV.NODE_PAGE_INFO] = pageItem[JV.NODE_PAGE_INFO];
  1523. // newPageDataObj[JV.BAND_PROP_MERGE_BAND] = JSON.parse(JSON.stringify(pageItem[JV.BAND_PROP_MERGE_BAND]));
  1524. newPageDataObj[JV.BAND_PROP_MERGE_BAND] = pageItem[JV.BAND_PROP_MERGE_BAND];
  1525. newPageDataObj[JV.NODE_FONT_COLLECTION] = pageItem[JV.NODE_FONT_COLLECTION];
  1526. newPageDataObj[JV.NODE_STYLE_COLLECTION] = pageItem[JV.NODE_STYLE_COLLECTION];
  1527. newPageDataObj[JV.NODE_CONTROL_COLLECTION] = pageItem[JV.NODE_CONTROL_COLLECTION];
  1528. for (let dIdx = idx * breakAmt; dIdx < (Math.min((idx + 1) * breakAmt, pageItem.items.length)); dIdx++) {
  1529. newPageDataObj.items.push(pageItem.items[dIdx]);
  1530. }
  1531. rst.push(newPageDataObj);
  1532. newRptNames.push(currentRptName + '_' + (idx + 1));
  1533. }
  1534. } else {
  1535. rst.push(pageItem);
  1536. newRptNames.push(currentRptName);
  1537. }
  1538. }
  1539. return rst;
  1540. };