report_controller.js 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  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. mergeTextSignature(params.isTextSignature, params.stage_status, pageRst, roleRel[0]);
  382. await encodeSignatureDataUri(roleRel, this.app.baseDir);
  383. if (params.getPicFlag) {
  384. await encodeDummySignatureDataUri(pageRst, this.app.baseDir); // 注意草图数据量问题!
  385. }
  386. const stageFlow = await ctx.service.stageAudit.getAuditGroupByListWithOwner(params.stage_id, params.stage_times);
  387. // console.log('encodeSignatureDataUri!');
  388. return {
  389. data: pageRst,
  390. signatureRelInfo: roleRel,
  391. stageAudit: stgAudit,
  392. stageAuditOrg: stgAuditForOrg,
  393. debugInfo: ctx.app.config.is_debug ? ctx.debugInfo : null,
  394. customDefine: rptTpl[JV.NODE_CUSTOM_DEFINE],
  395. stageFlow,
  396. customSelect,
  397. waterMarkStr,
  398. };
  399. }
  400. /**
  401. * 获取报表数据
  402. *
  403. * @param {Object} ctx - egg全局context
  404. * @return {void}
  405. */
  406. async getReport(ctx) {
  407. try {
  408. // console.log('in getReport');
  409. const params = JSON.parse(ctx.request.body.params);
  410. ctx.body = await this._getReport(ctx, params);
  411. // // console.log(params);
  412. // let rptTpl = await ctx.service.rptTpl.getTplById(params.rpt_tpl_id);
  413. // if (!rptTpl || rptTpl.length !== 1) {
  414. // throw '获取模板失败';
  415. // }
  416. // rptTpl = JSON.parse(rptTpl[0].rpt_content);
  417. // // console.log('get the template!');
  418. // const pageRst = await getAllPagesCommon(ctx, rptTpl, params, JV.PAGING_OPTION_NORMAL, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir);
  419. // // console.log(pageRst);
  420. // // const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id)) : [];
  421. // const roleRel = await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id); // 新需求中,允许在非审核状态下设置签名
  422. // const stgAudit = await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times);
  423. // // console.log('after role stage!');
  424. // // console.log(roleRel);
  425. // await encodeSignatureDataUri(roleRel, this.app.baseDir);
  426. // await encodeDummySignatureDataUri(pageRst, this.app.baseDir);
  427. // const stageFlow = await ctx.service.stageAudit.getAuditGroupByListWithOwner(params.stage_id, params.stage_times);
  428. // const customSelect = await ctx.service.rptCustomDefine.getDataByCondition({
  429. // tid: params.tender_id, sid: params.stage_id, rid: params.rpt_tpl_id
  430. // });
  431. //
  432. // // console.log('encodeSignatureDataUri!');
  433. // ctx.body = {
  434. // data: pageRst,
  435. // signatureRelInfo: roleRel,
  436. // stageAudit: stgAudit,
  437. // debugInfo: ctx.app.config.is_debug ? ctx.debugInfo : null,
  438. // customDefine: rptTpl[JV.NODE_CUSTOM_DEFINE],
  439. // stageFlow,
  440. // customSelect,
  441. // };
  442. // // ctx.body = { data: { msg: 'test the network' } };
  443. ctx.status = 201;
  444. } catch (ex) {
  445. console.log(ex);
  446. this.setMessage(ex.toString(), this.messageType.ERROR);
  447. }
  448. }
  449. /**
  450. * 获取多批次报表数据
  451. *
  452. * @param {Object} ctx - egg全局context
  453. * @return {void}
  454. */
  455. async getMultiReportsEx(ctx) {
  456. // 原则说明:把所有报表模板集中获取,统一filter,只读一次数据!
  457. const params = JSON.parse(ctx.request.body.params);
  458. const pageShow = ctx.session.sessionProject.page_show;
  459. if (pageShow !== null && pageShow !== undefined && parseInt(pageShow.closeExportPdf) === 1) {
  460. ctx.body = { data: [], signatureRelInfo: [], stageAudit: [] };
  461. ctx.status = 201;
  462. return;
  463. }
  464. await this._saveCustomSelects(params);
  465. const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir) || [];
  466. for (const pageRst of pageRstArr) {
  467. for (const page of pageRst.items) {
  468. page[JV.PROP_WATERMARK_CELLS] = [];
  469. }
  470. }
  471. let waterMarkStr = null;
  472. if (params.stage_status !== 3 && params.closeWatermark === 0) {
  473. // 加水印
  474. fillWaterMark(pageRstArr);
  475. if (params.needWaterMark) {
  476. waterMarkStr = await getWatermarkPicData(pageRstArr[0], this.app.baseDir);
  477. }
  478. }
  479. const stgAudit = await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times);
  480. const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids, params.stage_id)) : [];
  481. // const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id)) : [];
  482. roleRel.sort(function(rr1, rr2) {
  483. return params.rpt_ids.indexOf(parseInt(rr1.rpt_id)) - params.rpt_ids.indexOf(parseInt(rr2.rpt_id));
  484. });
  485. // console.log('roleRel: ');
  486. // console.log(roleRel);
  487. mergeTextSignature(params.isTextSignature, params.stage_status, pageRstArr, roleRel, params.rpt_ids);
  488. await encodeSignatureDataUri(roleRel, this.app.baseDir);
  489. if (params.getPicFlag) {
  490. for (const pageRst of pageRstArr) {
  491. await encodeDummySignatureDataUri(pageRst, this.app.baseDir); // 注意草图数据量问题!
  492. }
  493. }
  494. // console.log(pageRstArr);
  495. ctx.body = { data: pageRstArr, signatureRelInfo: roleRel, stageAudit: stgAudit, waterMarkStr };
  496. ctx.status = 201;
  497. }
  498. async _saveCustomSelects(params) {
  499. for (const [i, cs] of params.customSelect.entries()) {
  500. if (!cs) continue;
  501. cs.tender_id = params.tender_id;
  502. cs.stage_id = params.stage_id;
  503. cs.rpt_tpl_id = params.rpt_ids[i];
  504. await this.ctx.service.rptCustomDefine.saveCustomSelect(cs);
  505. }
  506. }
  507. async createExcelFilesEx(ctx) {
  508. const params = JSON.parse(ctx.request.body.params);
  509. const pageShow = ctx.session.sessionProject.page_show;
  510. if (pageShow !== null && pageShow !== undefined && parseInt(pageShow.closeExportExcel) === 1) {
  511. ctx.body = { data: [] };
  512. ctx.status = 201;
  513. return;
  514. }
  515. await this._saveCustomSelects(params);
  516. const baseDir = this.app.baseDir;
  517. function getExcelByPageData(pageRst, rpt_name, innerRoleRel) {
  518. return new Promise(function(resolve, reject) {
  519. rpt_xl_util.exportExcel(pageRst, params.pageSize, rpt_name, params.isOneSheet, null, null, baseDir, innerRoleRel,
  520. (err, uuidName) => {
  521. if (err) return reject(err);
  522. const fileRst = { uuid: uuidName, reportName: rpt_name.replace(new RegExp('%', 'gm'), '_') };
  523. resolve(fileRst);
  524. }
  525. );
  526. });
  527. }
  528. const stgAudit = (params.stage_status === 3) ? (await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times)) : [];
  529. const stgAuditForOrg = (params.stage_status === 3) ? (await ctx.service.stageAudit.getStageAudit(params.stage_id, 1)) : [];
  530. const stageList = (params.stage_status === 3) ? (await ctx.service.stage.getValidStagesShort(params.tender_id)) : [];
  531. const roleRelArr = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids, params.stage_id)) : [];
  532. // const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id)) : [];
  533. const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, baseDir);
  534. // console.log('params.stage_status: ' + params.stage_status);
  535. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/testBuiltPageResult1.js');
  536. for (const pageRst of pageRstArr) {
  537. for (const page of pageRst.items) {
  538. page[JV.PROP_WATERMARK_CELLS] = [];
  539. }
  540. }
  541. let waterMarkStr = null;
  542. if (params.stage_status !== 3 && params.closeWatermark === 0) {
  543. // if (params.stage_status !== 3 && params.closeWatermark === 0 && params.option === JV.PAGING_OPTION_NORMAL) {
  544. // 加水印(注意:还得看用户设置是否需要加水印)
  545. fillWaterMark(pageRstArr);
  546. if (params.needWaterMark) {
  547. waterMarkStr = await getWatermarkPicData(pageRstArr[0], baseDir);
  548. }
  549. }
  550. mergeTextSignature(params.isTextSignature, params.stage_status, pageRstArr, roleRelArr, params.rpt_ids);
  551. await this.ctx.helper.recursiveMkdirSync(baseDir + '/app/public/download');
  552. const runnableRst = [];
  553. /*
  554. for (let idx = 0; idx < pageRstArr.length; idx++) {
  555. let roleRel = null;
  556. for (const roleR of roleRelArr) {
  557. if (roleR.rpt_id === params.rpt_ids[idx]) {
  558. roleRel = JSON.parse(roleR.rel_content);
  559. break;
  560. }
  561. }
  562. // console.log('roleRel.rel_content: ' + roleRel.rel_content);
  563. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/testBuiltPageResult.jsp');
  564. if (params.stage_status === 3) {
  565. mergeSignAudit(pageRstArr[idx], roleRel, stgAudit);
  566. mergeSignDate(pageRstArr[idx], params.stage_id, roleRel, stgAudit, stgAuditForOrg, stageList);
  567. }
  568. runnableRst.push(getExcelByPageData(pageRstArr[idx], params.rpt_names[idx], roleRel));
  569. }
  570. /*/
  571. // 1. merge签名相关信息
  572. // const pageBrkSize = 6291456; // 6M, 1024*1024*6
  573. const pageBrkSize = 62914560; // 60M, 1024*1024*6*10
  574. for (let idx = 0; idx < pageRstArr.length; idx++) {
  575. let roleRel = null;
  576. for (const roleR of roleRelArr) {
  577. if (roleR.rpt_id === params.rpt_ids[idx]) {
  578. roleRel = JSON.parse(roleR.rel_content);
  579. break;
  580. }
  581. }
  582. if (params.stage_status === 3) {
  583. mergeSignAudit(pageRstArr[idx], roleRel, stgAudit);
  584. mergeSignDate(pageRstArr[idx], params.stage_id, roleRel, stgAudit, stgAuditForOrg, stageList);
  585. }
  586. // 2. 判断草图大小是否需要分页
  587. const picSize = await _chkRawPicSize(pageRstArr[idx], baseDir);
  588. // const picSize = 262914560; // 测试用,强制分页
  589. if (picSize > pageBrkSize) {
  590. // let pageBrkAmt = Math.ceil(picSize / pageBrkSize);
  591. let pageBrkAmt = Math.floor(pageRstArr[idx].items.length / Math.ceil(picSize / pageBrkSize));
  592. if (pageBrkAmt < 10) {
  593. // 不用管
  594. } else if (pageBrkAmt < 100) {
  595. pageBrkAmt = Math.floor(pageBrkAmt / 10) * 10;
  596. } else if (pageBrkAmt < 1000) {
  597. pageBrkAmt = Math.floor(pageBrkAmt / 100) * 100;
  598. } else {
  599. // 暂时不管
  600. }
  601. const newRptNames = []; // 这个为导出控制分页用
  602. const newPageDataRst = _resetPageDataByBreaks([pageRstArr[idx]], pageBrkAmt, [params.rpt_names[idx]], newRptNames);
  603. for (let dtlIdx = 0; dtlIdx < newPageDataRst.length; dtlIdx++) {
  604. runnableRst.push(getExcelByPageData(newPageDataRst[dtlIdx], newRptNames[dtlIdx], roleRel));
  605. }
  606. } else {
  607. runnableRst.push(getExcelByPageData(pageRstArr[idx], params.rpt_names[idx], roleRel));
  608. }
  609. }
  610. // */
  611. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/计量导出pageArr.js');
  612. const uuidRst = await Promise.all(runnableRst);
  613. ctx.body = { data: uuidRst, waterMarkStr };
  614. ctx.status = 201;
  615. }
  616. _getExcelByPageData(pageRst, rpt_name, pageSize, baseDir, innerRoleRel) {
  617. return new Promise(function(resolve, reject) {
  618. rpt_xl_util.exportExcel(pageRst, pageSize, rpt_name, true, null, null, baseDir, innerRoleRel,
  619. (err, uuidName) => {
  620. if (err) return reject(err);
  621. const fileRst = { uuid: uuidName, reportName: rpt_name.replace(new RegExp('%', 'gm'), '_') };
  622. resolve(fileRst);
  623. }
  624. );
  625. });
  626. }
  627. async createExcelFilesFromDirectData(ctx) {
  628. const params = JSON.parse(ctx.request.body.params);
  629. const names = params.rptNames;
  630. const pageDataArr = params.pageData;
  631. const runnableRst = [];
  632. for (let idx = 0; idx < names.length; idx++) {
  633. runnableRst.push(this._getExcelByPageData(pageDataArr[idx], names[idx], 'A4', this.app.baseDir, null));
  634. }
  635. const uuidRst = await Promise.all(runnableRst);
  636. ctx.body = { data: uuidRst, waterMarkStr: null };
  637. ctx.status = 201;
  638. }
  639. async createExcelFilesFromLocal(ctx) {
  640. const baseDir = this.app.baseDir;
  641. function getExcelByPageData(pageRst, rpt_name, innerRoleRel) {
  642. return new Promise(function(resolve, reject) {
  643. rpt_xl_util.exportExcel(pageRst, 'A4', rpt_name, true, null, null, baseDir, innerRoleRel,
  644. (err, uuidName) => {
  645. if (err) return reject(err);
  646. const fileRst = { uuid: uuidName, reportName: rpt_name.replace(new RegExp('%', 'gm'), '_') };
  647. resolve(fileRst);
  648. }
  649. );
  650. });
  651. }
  652. try {
  653. console.log('start:');
  654. const pageRst = JSON.parse(fs.readFileSync('D:/GitHome/temp/计量excel原始数据_A3BZ6_5384_112表(清单).js', 'utf-8'));
  655. console.log('read it');
  656. const runnableRst = [];
  657. const pageBrkSize = 6291456; // 6M, 1024*1024*6
  658. // const pageBrkSize = 1048576; // 1M, 1024*1024*1
  659. console.log('pageBrkSize:' + pageBrkSize);
  660. const picSize = await _chkRawPicSize(pageRst, baseDir);
  661. console.log('picSize:' + picSize);
  662. if (picSize > pageBrkSize) {
  663. let pageBrkAmt = Math.floor(pageRst.items.length / Math.ceil(picSize / pageBrkSize));
  664. console.log('pageBrkAmt:' + pageBrkAmt);
  665. if (pageBrkAmt < 10) {
  666. // 不用管
  667. } else if (pageBrkAmt < 100) {
  668. pageBrkAmt = Math.floor(pageBrkAmt / 10) * 10;
  669. } else if (pageBrkAmt < 1000) {
  670. pageBrkAmt = Math.floor(pageBrkAmt / 100) * 100;
  671. } else {
  672. // 暂时不管
  673. }
  674. const newRptNames = []; // 这个为导出控制分页用
  675. const newPageDataRst = _resetPageDataByBreaks([pageRst], pageBrkAmt, ['工程计量表(计量单元-清单)'], newRptNames);
  676. for (let dtlIdx = 0; dtlIdx < newPageDataRst.length; dtlIdx++) {
  677. runnableRst.push(getExcelByPageData(newPageDataRst[dtlIdx], newRptNames[dtlIdx], null));
  678. }
  679. } else {
  680. runnableRst.push(getExcelByPageData(pageRst, '工程计量表(计量单元-清单)', null));
  681. }
  682. const uuidRst = await Promise.all(runnableRst);
  683. ctx.body = { data: uuidRst, waterMarkStr: null };
  684. ctx.status = 201;
  685. } catch (ex) {
  686. console.log(ex.toString());
  687. ctx.status = 500;
  688. }
  689. }
  690. async createExcelFilesInOneBookEx(ctx) {
  691. const params = JSON.parse(ctx.request.body.params);
  692. const baseDir = this.app.baseDir;
  693. function getExcelByPageData(pageRstArr, rpt_name, innerRoleRel) {
  694. return new Promise(function(resolve, reject) {
  695. rpt_xl_util.exportExcelInOneBook(pageRstArr, params.pageSize, rpt_name, baseDir, innerRoleRel,
  696. (err, uuidName) => {
  697. if (err) return reject(err);
  698. const fileRst = { uuid: uuidName, reportName: rpt_name };
  699. resolve(fileRst);
  700. }
  701. );
  702. });
  703. }
  704. const roleRelArr = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids, params.stage_id)) : [];
  705. // console.log(`baseDir: ${baseDir}`);
  706. // fsUtil.writeObjToFile(roleRelArr, 'D:/GitHome/temp/签名信息.js');
  707. const stgAudit = (params.stage_status === 3) ? (await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times)) : [];
  708. const stgAuditForOrg = (params.stage_status === 3) ? (await ctx.service.stageAudit.getStageAudit(params.stage_id, 1)) : [];
  709. const stageList = (params.stage_status === 3) ? (await ctx.service.stage.getValidStagesShort(params.tender_id)) : [];
  710. const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir);
  711. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/pageRstArrForExcel.js');
  712. for (const pageRst of pageRstArr) {
  713. for (const page of pageRst.items) {
  714. page[JV.PROP_WATERMARK_CELLS] = [];
  715. }
  716. }
  717. let waterMarkStr = null;
  718. if (params.stage_status !== 3 && params.closeWatermark === 0) {
  719. // if (params.stage_status !== 3 && params.closeWatermark === 0 && params.option === JV.PAGING_OPTION_NORMAL) {
  720. // 加水印
  721. fillWaterMark(pageRstArr);
  722. if (params.needWaterMark) {
  723. waterMarkStr = await getWatermarkPicData(pageRstArr[0], this.app.baseDir);
  724. }
  725. }
  726. mergeTextSignature(params.isTextSignature, params.stage_status, pageRstArr, roleRelArr, params.rpt_ids);
  727. await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  728. const runnableRst = [];
  729. let rptRoleRelArr = [];
  730. const reAssignSignatureName = function(pageData, roleRel, rpt_name_key) {
  731. const keyMap = {};
  732. for (const pageItem of pageData.items) {
  733. for (const signCell of pageItem[JV.PROP_SIGNATURE_CELLS]) {
  734. if (!keyMap.hasOwnProperty(signCell.signature_name)) {
  735. keyMap[signCell.signature_name] = rpt_name_key + '_' + signCell.signature_name;
  736. }
  737. }
  738. }
  739. for (const pageItem of pageData.items) {
  740. for (const signCell of pageItem[JV.PROP_SIGNATURE_CELLS]) {
  741. if (keyMap.hasOwnProperty(signCell.signature_name)) {
  742. signCell.signature_name = keyMap[signCell.signature_name];
  743. }
  744. }
  745. }
  746. for (const roleSign of roleRel) {
  747. if (keyMap.hasOwnProperty(roleSign.signature_name)) {
  748. roleSign.signature_name = keyMap[roleSign.signature_name];
  749. }
  750. }
  751. // console.log('keyMap of : ' + rpt_name_key);
  752. // console.log(keyMap);
  753. };
  754. for (let idx = 0; idx < pageRstArr.length; idx++) {
  755. let roleRel = null;
  756. for (const roleR of roleRelArr) {
  757. if (roleR.rpt_id === params.rpt_ids[idx]) {
  758. roleRel = JSON.parse(roleR.rel_content);
  759. if (params.stage_status === 3) {
  760. mergeSignAudit(pageRstArr[idx], roleRel, stgAudit);
  761. mergeSignDate(pageRstArr[idx], params.stage_id, roleRel, stgAudit, stgAuditForOrg, stageList);
  762. }
  763. // 这里要做些电子签名的signature_name转换,以防重名
  764. reAssignSignatureName(pageRstArr[idx], roleRel, params.rpt_names[idx]);
  765. rptRoleRelArr = rptRoleRelArr.concat(roleRel);
  766. break;
  767. }
  768. }
  769. }
  770. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/pageRstArrForExcelAfterReAssign.js');
  771. // fsUtil.writeObjToFile(rptRoleRelArr, 'D:/GitHome/temp/签名信息_新.js');
  772. runnableRst.push(getExcelByPageData(pageRstArr, params.rptName, rptRoleRelArr));
  773. const uuidRst = await Promise.all(runnableRst);
  774. ctx.body = { data: uuidRst, waterMarkStr };
  775. ctx.status = 201;
  776. }
  777. async getFileByUUID(ctx) {
  778. // console.log('downloading : ' + ctx.params.uuid);
  779. const uuid = ctx.params.uuid;
  780. const rptName = ctx.params.rptName;
  781. const suffix = '.' + ctx.params.suffix;
  782. try {
  783. const rptNameURI = encodeURI(rptName);
  784. const filePath = this.app.baseDir + '/app/public/download/';
  785. // console.log('filePath: ' + filePath);
  786. // await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  787. ctx.set({
  788. 'Content-Type': 'application/vnd.openxmlformats',
  789. 'Content-Disposition': 'attachment; filename="' + rptNameURI + suffix + "\"; filename*=utf-8''" + rptNameURI + suffix,
  790. });
  791. ctx.body = await fs.readFileSync(filePath + uuid + suffix);
  792. } catch (e) {
  793. console.log(e);
  794. }
  795. }
  796. /**
  797. * 更新用户通用报表显示配置
  798. *
  799. * @param {Object} ctx - egg全局context
  800. * @return {void}
  801. */
  802. async updateCustNode(ctx) {
  803. const params = JSON.parse(ctx.request.body.params);
  804. // console.log(params);
  805. const custId = this.ctx.session.sessionUser.accountId;
  806. const newNodeItems = JSON.stringify(params.nodeItems);
  807. const rst = await ctx.service.rptTreeNodeCust.updateCustNode(custId, newNodeItems);
  808. // console.log(rst);
  809. ctx.body = { data: rst };
  810. // ctx.body = { data: { msg: 'test the network' } };
  811. ctx.status = 201;
  812. }
  813. async setCustomDefine(ctx) {
  814. try {
  815. const data = JSON.parse(ctx.request.body.data);
  816. await this.ctx.service.rptCustomDefine.saveCustomSelect(data);
  817. // const sid = data.gather_select ? -1 : data.stage_id;
  818. // const filter = {tid: data.tender_id, sid: sid, rid: data.rpt_tpl_id};
  819. // const count = await ctx.service.rptCustomDefine.count(filter);
  820. // const updateData = {};
  821. // if (data.audit_select) updateData.audit_select = JSON.stringify(data.audit_select);
  822. // if (data.gather_select) updateData.gather_select = JSON.stringify(data.gather_select);
  823. // if (count > 0) {
  824. // await ctx.service.rptCustomDefine.update(updateData, filter);
  825. // } else {
  826. // updateData.tid = data.tender_id;
  827. // updateData.sid = sid;
  828. // updateData.rid = data.rpt_tpl_id;
  829. // await ctx.service.rptCustomDefine.db.insert(ctx.service.rptCustomDefine.tableName, updateData);
  830. // }
  831. const result = await this._getReport(ctx, data);
  832. ctx.body = { err: 0, msg: '', data: result };
  833. } catch (err) {
  834. ctx.helper.log(err);
  835. ctx.body = this.ajaxErrorBody(err, '保存数据出错');
  836. }
  837. }
  838. }
  839. return ReportController;
  840. };
  841. async function checkStg(ctx, params) {
  842. if (ctx.stage === null || ctx.stage === undefined || parseInt(ctx.stage.id) !== parseInt(params.stage_id)) {
  843. await ctx.service.stage.checkStage(params.stage_id);
  844. if (ctx.stage) {
  845. params.stage_order = ctx.stage.curOrder;
  846. }
  847. }
  848. }
  849. async function getAllPagesCommon(ctx, rptTpl, params, option, outputType, baseDir, customSelect) {
  850. const rptDataUtil = new rptDataExtractor();
  851. rptDataUtil.initialize(rptTpl);
  852. const filter = rptDataUtil.getDataRequestFilter();
  853. const rawDataObj = await ctx.service.report.getReportData(params, filter.tables, filter.memFieldKeys,
  854. rptTpl[JV.NODE_CUSTOM_DEFINE], customSelect);
  855. // console.log(rawDataObj);
  856. try {
  857. const printCom = JpcEx.createNew();
  858. if (params.pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = params.pageSize;
  859. if (params.orientation && (params.orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = params.orientation;
  860. // let defProperties = await ctx.service.rptPreDefineCfg.getCfgById('Administrator');
  861. // defProperties = JSON.parse(defProperties[0].defined_content);
  862. const defProperties = RPT_DEF_PROPERTIES;
  863. // console.log('defProperties: ');
  864. // console.log(defProperties);
  865. // console.log('before assemble');
  866. // fsUtil.writeObjToFile(rawDataObj, 'D:/GitHome/temp/JL_Assemble_RawDataDataBefore' + (new Date()).getTime() + '.js');
  867. const tplData = rptDataUtil.assembleData(ctx, rawDataObj, baseDir, printCom, customSelect);
  868. // console.log(tplData);
  869. if (params.custCfg) {
  870. setupCustomizeCfg(params.custCfg, rptTpl, defProperties);
  871. } else {
  872. // setupCustomizeCfg(defProperties, rptTpl, defProperties);
  873. }
  874. const dftOption = params.option || JV.PAGING_OPTION_NORMAL;
  875. printCom.initialize(rptTpl);
  876. // 在ctx.helper里夹带私货,增加当前期status及times
  877. // ctx.helper.current_stage_status = params.stage_status;
  878. // ctx.helper.current_stage_times = params.stage_times;
  879. printCom.analyzeData(ctx.helper, rptTpl, tplData, defProperties, dftOption, outputType, customSelect);
  880. // console.log(JSON.stringify(rptTpl));
  881. const maxPages = printCom.totalPages;
  882. let pageRst = null;
  883. if (maxPages > 0) {
  884. pageRst = printCom.outputAsSimpleJSONPageArray(ctx.helper, rptTpl, tplData, 1, maxPages, defProperties, params.custCfg, customSelect);
  885. } else {
  886. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  887. }
  888. // fsUtil.writeObjToFile(pageRst, 'D:/GitHome/temp/计量testBuiltPageResult.jsp');
  889. // console.log(pageRst);
  890. return pageRst;
  891. } catch (ex) {
  892. console.log('报表数据异常, tender id: ' + params.tender_id);
  893. console.log(ex);
  894. }
  895. }
  896. async function getMultiRptsCommon(ctx, params, outputType, baseDir) {
  897. for (let idx = 0; idx < params.rpt_ids.length; idx++) {
  898. params.rpt_ids[idx] = parseInt(params.rpt_ids[idx]); // 转换一下,以防万一
  899. }
  900. const rptTpls = await ctx.service.rptTpl.getAllTplByIds(params.rpt_ids);
  901. rptTpls.sort(function(rpt1, rpt2) {
  902. return params.rpt_ids.indexOf(rpt1.id) - params.rpt_ids.indexOf(rpt2.id);
  903. });
  904. for (let rtIdx = 0; rtIdx < rptTpls.length; rtIdx++) {
  905. const id = rptTpls[rtIdx].id;
  906. rptTpls[rtIdx] = JSON.parse(rptTpls[rtIdx].rpt_content);
  907. rptTpls[rtIdx].id = id;
  908. }
  909. const rptDataUtil = new rptDataExtractor();
  910. const filterTables = [];
  911. const memFieldKeys = {};
  912. for (const rptTpl of rptTpls) {
  913. rptDataUtil.initialize(rptTpl);
  914. const filter = rptDataUtil.getDataRequestFilter();
  915. // console.log(filter);
  916. for (const table of filter.tables) {
  917. if (filterTables.indexOf(table) < 0 && needCustomTables.indexOf(table) < 0) {
  918. filterTables.push(table);
  919. }
  920. // memFieldKeys[table] = [];
  921. }
  922. for (const tableKeyProp in filter.memFieldKeys) {
  923. if (needCustomTables.indexOf(tableKeyProp) >= 0) continue;
  924. if (!memFieldKeys.hasOwnProperty(tableKeyProp)) {
  925. memFieldKeys[tableKeyProp] = [];
  926. }
  927. for (const mfKey of filter.memFieldKeys[tableKeyProp]) {
  928. if (memFieldKeys[tableKeyProp].indexOf(mfKey) < 0) {
  929. memFieldKeys[tableKeyProp].push(mfKey);
  930. }
  931. }
  932. }
  933. }
  934. const rawDataObj = await ctx.service.report.getReportData(params, filterTables, memFieldKeys, {}, {});
  935. try {
  936. const rptPageRstArray = [];
  937. // 1. 这里只用一份数据,根据实际应用情况,先备份
  938. // const backupData = {};
  939. // for (let filterIdx = 0; filterIdx < filterTables.length; filterIdx++) {
  940. // backupData[filterTables[filterIdx]] = [];
  941. // Object.assign(backupData[filterTables[filterIdx]], rawDataObj[filterTables[filterIdx]]);
  942. // }
  943. // 2. 一个一个模板创建数据
  944. // let defProperties = await ctx.service.rptPreDefineCfg.getCfgById('Administrator');
  945. // defProperties = JSON.parse(defProperties[0].defined_content);
  946. const defProperties = RPT_DEF_PROPERTIES;
  947. for (let tplIdx = 0; tplIdx < rptTpls.length; tplIdx++) {
  948. const rptTpl = (rptTpls[tplIdx]._doc) ? rptTpls[tplIdx]._doc : rptTpls[tplIdx];
  949. rptDataUtil.initialize(rptTpl);
  950. const customSelect = rptTpl[JV.NODE_CUSTOM_DEFINE] && rptTpl[JV.NODE_CUSTOM_DEFINE][JV.NODE_CUS_AUDIT_SELECT].enable
  951. ? await ctx.service.rptCustomDefine.getCustomDefine(params.tender_id, params.stage_id, rptTpl.id)
  952. : await ctx.service.rptCustomDefine.getCustomDefine(params.tender_id, -1, rptTpl.id);
  953. // 从汇总的rawDataObj中拷贝所需数据表至curRawDataObj,以供后续使用
  954. const curRawDataObj = {};
  955. const filter = rptDataUtil.getDataRequestFilter();
  956. for (const table of filter.tables) {
  957. curRawDataObj[table] = ctx.helper.clone(rawDataObj[table]);
  958. }
  959. // 如果是用户交互类型的表,则应该单独获取数据
  960. if ((params.customSelect && params.customSelect[tplIdx]) || rptTpl[JV.NODE_CUSTOM_DEFINE][JV.NODE_CUS_OPTION]) {
  961. const cfTables = [],
  962. cmFieldKeys = [];
  963. const curFilter = rptDataUtil.getDataRequestFilter();
  964. for (const table of curFilter.tables) {
  965. if (needCustomTables.indexOf(table) >= 0) {
  966. cfTables.push(table);
  967. }
  968. }
  969. for (const tableKeyProp in filter.memFieldKeys) {
  970. if (needCustomTables.indexOf(tableKeyProp) < 0) continue;
  971. if (!cmFieldKeys.hasOwnProperty(tableKeyProp)) {
  972. cmFieldKeys[tableKeyProp] = [];
  973. }
  974. for (const mfKey of filter.memFieldKeys[tableKeyProp]) {
  975. if (cmFieldKeys[tableKeyProp].indexOf(mfKey) < 0) {
  976. cmFieldKeys[tableKeyProp].push(mfKey);
  977. }
  978. }
  979. }
  980. const customRawDataObj = await ctx.service.report.getReportData(params, cfTables, cmFieldKeys,
  981. rptTpl[JV.NODE_CUSTOM_DEFINE], customSelect);
  982. for (const prop in customRawDataObj) {
  983. curRawDataObj[prop] = customRawDataObj[prop];
  984. }
  985. }
  986. const tplData = rptDataUtil.assembleData(ctx, curRawDataObj, baseDir, null, customSelect);
  987. const printCom = JpcEx.createNew();
  988. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = params.pageSize;
  989. if (params.pageSize) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE] = params.pageSize;
  990. if (params.orientation && (params.orientation !== 'null')) rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] = params.orientation;
  991. if (params.custCfg) setupCustomizeCfg(params.custCfg, rptTpl, defProperties);
  992. const dftOption = params.option || JV.PAGING_OPTION_NORMAL;
  993. printCom.initialize(rptTpl);
  994. // console.log(rptTpl);
  995. printCom.analyzeData(ctx.helper, rptTpl, tplData, defProperties, dftOption, outputType, customSelect);
  996. const maxPages = printCom.totalPages;
  997. let pageRst = null;
  998. // console.log(maxPages);
  999. if (maxPages > 0) {
  1000. pageRst = printCom.outputAsSimpleJSONPageArray(ctx.helper, rptTpl, tplData, 1, maxPages, defProperties, params.custCfg, customSelect);
  1001. } else {
  1002. pageRst = printCom.outputAsPreviewPage(rptTpl, defProperties);
  1003. }
  1004. rptPageRstArray.push(pageRst);
  1005. // 注意:这里需要把备份数据assign回去!!!
  1006. // if (tplIdx < rptTpls.length - 1) {
  1007. // for (let filterIdx = 0; filterIdx < filterTables.length; filterIdx++) {
  1008. // backupData[filterTables[filterIdx]] = [];
  1009. // Object.assign(rawDataObj[filterTables[filterIdx]], backupData[filterTables[filterIdx]]);
  1010. // }
  1011. // }
  1012. }
  1013. return rptPageRstArray;
  1014. } catch (ex) {
  1015. console.log('报表数据异常(getMultiRptsCommon): project_id ' + params.project_id + ', tender_id: ' + params.tender_id + ', stage_id: ' + params.stage_id);
  1016. console.log(ex);
  1017. } finally {
  1018. //
  1019. }
  1020. }
  1021. function setupCustomizeCfg(customizeCfg, rptTpl, defProperties) {
  1022. const tmpObj = {};
  1023. // 1. 字体
  1024. const newFonts = [];
  1025. for (const font of defProperties.fonts) {
  1026. const copyFont = {};
  1027. copyFont.ID = font.ID;
  1028. for (const fontProp of JV.FONT_PROPS) {
  1029. copyFont[fontProp] = font[fontProp];
  1030. }
  1031. newFonts.push(copyFont);
  1032. tmpObj[font.ID] = copyFont;
  1033. }
  1034. const private_setup_font = function(propStr, newFont) {
  1035. if (tmpObj[propStr]) {
  1036. tmpObj[propStr].Name = newFont.Name;
  1037. tmpObj[propStr].FontHeight = String(newFont.FontHeight);
  1038. tmpObj[propStr].FontBold = newFont.FontBold;
  1039. tmpObj[propStr].FontItalic = newFont.FontItalic;
  1040. tmpObj[propStr].FontUnderline = newFont.FontUnderline;
  1041. }
  1042. };
  1043. for (const custFont of customizeCfg.fonts) {
  1044. switch (custFont.CfgDispName) {
  1045. case '表标题':
  1046. private_setup_font('ReportTitle_Main', custFont);
  1047. break;
  1048. case '列标题':
  1049. private_setup_font('HeaderColumn', custFont);
  1050. private_setup_font('FooterColumn', custFont);
  1051. break;
  1052. case '正文内容':
  1053. private_setup_font('Content', custFont);
  1054. break;
  1055. case '合计':
  1056. private_setup_font('GrandTotal', custFont);
  1057. private_setup_font('SectionTotal', custFont);
  1058. break;
  1059. case '表眉/表脚':
  1060. private_setup_font('Header', custFont);
  1061. private_setup_font('Footer', custFont);
  1062. break;
  1063. default:
  1064. break;
  1065. }
  1066. }
  1067. // 1.1 窄体
  1068. if (tmpObj.Content_Narrow) {
  1069. if (customizeCfg.isNarrow) {
  1070. tmpObj.Content_Narrow.Name = 'Arial Narrow';
  1071. } else {
  1072. if (tmpObj.Content) {
  1073. tmpObj.Content_Narrow.Name = tmpObj.Content.Name;
  1074. } else {
  1075. tmpObj.Content_Narrow.Name = '宋体';
  1076. }
  1077. }
  1078. }
  1079. defProperties.fonts = newFonts;
  1080. // 2. 页边距
  1081. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT] = customizeCfg.margins[JV.PROP_LEFT] / 10;
  1082. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT] = customizeCfg.margins[JV.PROP_RIGHT] / 10;
  1083. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP] = customizeCfg.margins[JV.PROP_TOP] / 10;
  1084. rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM] = customizeCfg.margins[JV.PROP_BOTTOM] / 10;
  1085. // 3. 边框宽度
  1086. if (customizeCfg.hasOwnProperty('borderThick')) {
  1087. for (let i = 0; i < defProperties.styles.length; i++) {
  1088. const style = defProperties.styles[i];
  1089. if (style.ID === 'BORDER_ALL_AROUND') {
  1090. for (const border of style.border_style) {
  1091. border.LineWeight = customizeCfg.borderThick;
  1092. }
  1093. }
  1094. }
  1095. }
  1096. // 4. 边框竖线
  1097. if (!(customizeCfg.showVerticalLine)) {
  1098. const private_copy_border = function(src) {
  1099. const rst = {};
  1100. rst.Position = src.Position;
  1101. rst.LineWeight = src.LineWeight;
  1102. rst.DashStyle = src.DashStyle;
  1103. rst.Color = src.Color;
  1104. return rst;
  1105. };
  1106. const newStyles = [];
  1107. for (let i = 0; i < defProperties.styles.length; i++) {
  1108. const style = defProperties.styles[i];
  1109. newStyles.push(style);
  1110. if (style.ID === 'BORDER_ALL_AROUND') {
  1111. const newStyle = {};
  1112. newStyle.ID = style.ID;
  1113. newStyle.CfgDispName = style.CfgDispName;
  1114. newStyle.border_style = [];
  1115. for (const border of style.border_style) {
  1116. const newBorder = private_copy_border(border);
  1117. newStyle.border_style.push(newBorder);
  1118. if (border.Position === 'Left' || border.Position === 'Right') {
  1119. newBorder.LineWeight = 0;
  1120. }
  1121. }
  1122. newStyles[newStyles.length - 1] = newStyle;
  1123. }
  1124. }
  1125. defProperties.styles = newStyles;
  1126. }
  1127. // 5. 补0
  1128. const private_Setup_Format = function(tabFields) {
  1129. if (tabFields) {
  1130. for (const tabField of tabFields) {
  1131. if (tabField[JV.PROP_FORMAT]) {
  1132. tabField[JV.PROP_FORMAT] = tabField[JV.PROP_FORMAT].replace(new RegExp('#', 'gm'), '0');
  1133. }
  1134. }
  1135. }
  1136. };
  1137. if (customizeCfg.fillZero) {
  1138. if (rptTpl[JV.NODE_FLOW_INFO]) {
  1139. 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]);
  1140. 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]);
  1141. 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]);
  1142. 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]);
  1143. if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO]) {
  1144. for (const discrete of rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO]) {
  1145. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  1146. }
  1147. }
  1148. if (rptTpl[JV.NODE_FLOW_INFO_EX]) {
  1149. 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]);
  1150. 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]);
  1151. 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]);
  1152. 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]);
  1153. 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]);
  1154. if (rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_DISCRETE_INFO]) {
  1155. for (const discrete of rptTpl[JV.NODE_FLOW_INFO_EX][JV.NODE_DISCRETE_INFO]) {
  1156. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  1157. }
  1158. }
  1159. }
  1160. } else if (rptTpl[JV.NODE_BILL_INFO]) {
  1161. 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]);
  1162. if (rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO]) {
  1163. for (const discrete of rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO]) {
  1164. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  1165. }
  1166. }
  1167. } else if (rptTpl[JV.NODE_CROSS_INFO]) {
  1168. 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]);
  1169. 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]);
  1170. 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]);
  1171. 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]);
  1172. 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]);
  1173. 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]);
  1174. if (rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO]) {
  1175. for (const discrete of rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO]) {
  1176. private_Setup_Format(discrete[JV.PROP_DISCRETE_FIELDS]);
  1177. }
  1178. }
  1179. }
  1180. }
  1181. }
  1182. function isFileExisted(file) {
  1183. return new Promise(function(resolve, reject) {
  1184. fs.access(file, err => {
  1185. if (err) {
  1186. reject(false);
  1187. } else {
  1188. resolve(true);
  1189. }
  1190. });
  1191. });
  1192. }
  1193. function fillWaterMark(pageRstArray) {
  1194. const orgWaterMarkWidth = 600;
  1195. const orgWaterMarkHeight = 288;
  1196. const createWaterCell = function(area) {
  1197. const w = area[JV.PROP_RIGHT] - area[JV.PROP_LEFT];
  1198. const h = area[JV.PROP_BOTTOM] - area[JV.PROP_TOP];
  1199. const left = Math.round(area[JV.PROP_LEFT] + w / 2 - orgWaterMarkWidth / 2);
  1200. const right = left + orgWaterMarkWidth;
  1201. const top = Math.round(area[JV.PROP_TOP] + h / 2 - orgWaterMarkHeight / 2);
  1202. const bottom = top + orgWaterMarkHeight;
  1203. return {
  1204. signature_name: JV.SIGNATURE_NAME_DUMMY_WATER_MARK,
  1205. path: '/public/images/not_Approve.png',
  1206. sign_path: '/public/images/not_Approve.png',
  1207. pic: null,
  1208. control: 'Title',
  1209. style: 'Default_None',
  1210. area: {
  1211. Left: left,
  1212. Right: right,
  1213. Top: top,
  1214. Bottom: bottom,
  1215. },
  1216. };
  1217. };
  1218. for (const pageRst of pageRstArray) {
  1219. for (const page of pageRst.items) {
  1220. page[JV.PROP_WATERMARK_CELLS] = [];
  1221. if (page[JV.PROP_PAGE_MERGE_BORDER]) {
  1222. const wmCell = createWaterCell(page[JV.PROP_PAGE_MERGE_BORDER]);
  1223. page[JV.PROP_WATERMARK_CELLS].push(wmCell);
  1224. } else if (page[JV.PAGE_SPECIAL_MERGE_POS]) {
  1225. if (page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_LEFT]) {
  1226. for (let i = 0; i < page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_LEFT].length; i++) {
  1227. const area = {};
  1228. area[JV.PROP_LEFT] = page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_LEFT][i];
  1229. area[JV.PROP_RIGHT] = page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_RIGHT][i];
  1230. area[JV.PROP_TOP] = page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_TOP][i];
  1231. area[JV.PROP_BOTTOM] = page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_BOTTOM][i];
  1232. const wmCell = createWaterCell(area);
  1233. page[JV.PROP_WATERMARK_CELLS].push(wmCell);
  1234. }
  1235. }
  1236. }
  1237. }
  1238. }
  1239. }
  1240. async function encodeSignatureDataUri(roleRel, baseDir) {
  1241. if (roleRel) {
  1242. for (const singleRoleRel of roleRel) {
  1243. if (singleRoleRel.rel_content !== null && singleRoleRel.rel_content !== undefined && singleRoleRel.rel_content !== '') {
  1244. const roleRelContent = JSON.parse(singleRoleRel.rel_content);
  1245. for (const role of roleRelContent) {
  1246. // console.log(role);
  1247. if (role.sign_path !== '') {
  1248. const filePath = baseDir + '/app' + role.sign_path;
  1249. try {
  1250. const res = await isFileExisted(filePath);
  1251. if (res) {
  1252. const bData = fs.readFileSync(filePath);
  1253. const base64Str = bData.toString('base64');
  1254. const datauri = 'data:image/png;base64,' + base64Str;
  1255. role.sign_pic = datauri;
  1256. } else {
  1257. console.log('文件不存在:' + filePath);
  1258. }
  1259. } catch (err) {
  1260. console.error(err);
  1261. }
  1262. }
  1263. }
  1264. singleRoleRel.rel_content = JSON.stringify(roleRelContent);
  1265. } else {
  1266. singleRoleRel.rel_content = [];
  1267. }
  1268. }
  1269. }
  1270. }
  1271. async function encodeDummySignatureDataUri(pageRst, baseDir) {
  1272. if (pageRst) {
  1273. for (const page of pageRst.items) {
  1274. for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1275. if (signature.signature_name === JV.SIGNATURE_NAME_DUMMY) {
  1276. if (signature.path !== '') {
  1277. const filePath = baseDir + '/app' + signature.path;
  1278. try {
  1279. const res = await isFileExisted(filePath);
  1280. if (res) {
  1281. const bData = fs.readFileSync(filePath);
  1282. const base64Str = bData.toString('base64');
  1283. const datauri = 'data:image/png;base64,' + base64Str;
  1284. signature.pic = datauri;
  1285. signature.path = '';
  1286. } else {
  1287. console.log('文件不存在:' + filePath);
  1288. }
  1289. } catch (err) {
  1290. console.error(err);
  1291. }
  1292. }
  1293. }
  1294. }
  1295. // 考虑优化,水印只需要一张即可,不需要每页都设置
  1296. // for (const signature of page[JV.PROP_WATERMARK_CELLS]) {
  1297. // if (signature.path !== '') {
  1298. // const filePath = baseDir + '/app' + signature.path;
  1299. // try {
  1300. // const res = await isFileExisted(filePath);
  1301. // if (res) {
  1302. // const bData = fs.readFileSync(filePath);
  1303. // const base64Str = bData.toString('base64');
  1304. // const datauri = 'data:image/png;base64,' + base64Str;
  1305. // signature.pic = datauri;
  1306. // signature.path = '';
  1307. // } else {
  1308. // console.log('文件不存在:' + filePath);
  1309. // }
  1310. // } catch (err) {
  1311. // console.error(err);
  1312. // }
  1313. // }
  1314. // }
  1315. }
  1316. }
  1317. }
  1318. async function getWatermarkPicData(pageRst, baseDir) {
  1319. let rst = '';
  1320. if (pageRst) {
  1321. for (const page of pageRst.items) {
  1322. for (const signature of page[JV.PROP_WATERMARK_CELLS]) {
  1323. if (signature.path !== '') {
  1324. const filePath = baseDir + '/app' + signature.path;
  1325. try {
  1326. const res = await isFileExisted(filePath);
  1327. if (res) {
  1328. const bData = fs.readFileSync(filePath);
  1329. const base64Str = bData.toString('base64');
  1330. rst = 'data:image/png;base64,' + base64Str;
  1331. break;
  1332. } else {
  1333. console.log('文件不存在:' + filePath);
  1334. }
  1335. } catch (err) {
  1336. console.error(err);
  1337. }
  1338. }
  1339. if (rst !== null) break;
  1340. }
  1341. }
  1342. }
  1343. return rst;
  1344. }
  1345. async function createExportRequestJob(ctx) {
  1346. const params = JSON.parse(ctx.request.body.params);
  1347. // const uuidArr = params.uuidArr;
  1348. const baseDir = this.app.baseDir;
  1349. const roleRelArr = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids, params.stage_id)) : [];
  1350. const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir);
  1351. await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  1352. for (let idx = 0; idx < pageRstArr.length; idx++) {
  1353. let roleRel = null;
  1354. for (const roleR of roleRelArr) {
  1355. if (roleR.rpt_id === params.rpt_ids[idx]) {
  1356. roleRel = JSON.parse(roleR.rel_content);
  1357. break;
  1358. }
  1359. }
  1360. if (params.exportType === 'xlsx') {
  1361. const zip = rpt_xl_util.exportExcel(pageRstArr[idx], params.pageSize, null, params.isOneSheet, null, null, baseDir, roleRel, null); // 给个null报表名称,返回zip对象自己处理,这样就不用改接口结构了
  1362. zip.generateNodeStream({ type: 'nodebuffer', streamFiles: true })
  1363. .pipe(fs.createWriteStream(baseDir + '/app/public/download/' + params.uuidArr[idx] + '.xlsx'));
  1364. } else if (params.exportType === 'pdf') {
  1365. // PDF的后期补上
  1366. }
  1367. }
  1368. }
  1369. function mergeTextSignature(isTxtSignature, status, pageData, singleRoleRel, rpt_ids) {
  1370. // 依据需求,有些用户不需要签名图片,那么从源头上把签名从signature_cells转移到cells下,省得后续 预览、打印、导出PDF、导出EXCEL每个地方都改动
  1371. const _mergeSingle = function(_page, _roleRelList, isDeleteSignCell) {
  1372. // console.log(_roleRelList);
  1373. let roleRelContent = [];
  1374. if (_roleRelList && _roleRelList.rel_content !== null && _roleRelList.rel_content !== undefined && _roleRelList.rel_content !== '') {
  1375. roleRelContent = JSON.parse(_roleRelList.rel_content);
  1376. };
  1377. for (const page of _page.items) {
  1378. if (page.signature_cells) {
  1379. const deleteSCellsIdx = [];
  1380. for (let scIdx = 0; scIdx < page.signature_cells.length; scIdx++) {
  1381. const sCell = page.signature_cells[scIdx];
  1382. sCell.Value = ''; // 这里要先清除原有信息
  1383. if (sCell.signature_name !== JV.SIGNATURE_NAME_DUMMY) {
  1384. // console.log(sCell);
  1385. sCell.path = '';
  1386. sCell.pic = '';
  1387. }
  1388. for (const role_rel of roleRelContent) {
  1389. if (sCell.signature_name === role_rel.signature_name) {
  1390. if (status === 3) {
  1391. sCell.Value = role_rel.user_name; // 只有审核通过了才需要文本签名内容,但不管如何,都不影响迁移
  1392. }
  1393. const newCell = {
  1394. font: 'Footer',
  1395. control: sCell.control,
  1396. style: sCell.style,
  1397. Value: sCell.Value,
  1398. area: { Left: sCell.area.Left, Right: sCell.area.Right, Top: sCell.area.Top, Bottom: sCell.area.Bottom },
  1399. };
  1400. page.cells.push(newCell); // 迁移
  1401. deleteSCellsIdx.push(scIdx);
  1402. break;
  1403. }
  1404. }
  1405. }
  1406. // 删除 page.signature_cells 签名(草图不能删);
  1407. if (isDeleteSignCell) {
  1408. for (let dIdx = deleteSCellsIdx.length - 1; dIdx >= 0; dIdx--) {
  1409. page.signature_cells.splice(deleteSCellsIdx[dIdx], 1);
  1410. }
  1411. }
  1412. }
  1413. }
  1414. };
  1415. if (isTxtSignature) {
  1416. if (pageData instanceof Array) {
  1417. pageData.forEach((dtlPage, index) => {
  1418. let dftRolRel = [];
  1419. const rpt_id = rpt_ids[index];
  1420. for (let roleRel of singleRoleRel) {
  1421. if (roleRel.rpt_id === rpt_id) {
  1422. dftRolRel = roleRel;
  1423. }
  1424. }
  1425. // console.log(rptTpls[rtIdx].id)
  1426. _mergeSingle(dtlPage, dftRolRel, true);
  1427. });
  1428. } else {
  1429. _mergeSingle(pageData, singleRoleRel, false);
  1430. }
  1431. }
  1432. }
  1433. function mergeSignAudit(pageData, currRoleRelList, currAuditList) {
  1434. for (const page of pageData.items) {
  1435. if (page.signature_audit_cells) {
  1436. for (const sCell of page.signature_audit_cells) {
  1437. sCell.Value = ''; // 这里要先清除原有信息
  1438. if (currRoleRelList && currRoleRelList.length > 0) {
  1439. for (const role_rel of currRoleRelList) {
  1440. if (sCell.signature_name === role_rel.signature_name + '_审核意见') {
  1441. let preDate = '';
  1442. sCell.Value = '同意'; // 只有选择了签名的,才需要初始化一个默认的意见(之前的逻辑在有多个签名,哪怕只选择了一个,其他的意见都会有默认意见)
  1443. if (currAuditList && currAuditList.length > 0) {
  1444. for (const audit_rel of currAuditList) {
  1445. if (role_rel.acc_id === audit_rel.aid) {
  1446. if (audit_rel.end_time > preDate && audit_rel.status === 3) {
  1447. sCell.Value = audit_rel.opinion;
  1448. preDate = audit_rel.end_time;
  1449. }
  1450. // 不能break,实际会有多个审核意见,以最后一个为准
  1451. }
  1452. }
  1453. }
  1454. break;
  1455. }
  1456. }
  1457. }
  1458. }
  1459. }
  1460. }
  1461. }
  1462. function mergeSignDate(pageData, current_stage_id, currRoleRelList, STAGE_AUDIT, STAGE_AUDIT_ORG, STAGE_LIST) {
  1463. if (currRoleRelList && currRoleRelList.length > 0 && STAGE_AUDIT && STAGE_AUDIT.length > 0) {
  1464. for (let rridx = 0; rridx < currRoleRelList.length; rridx++) {
  1465. const role_rel = currRoleRelList[rridx];
  1466. if (role_rel.sign_date === undefined || role_rel.sign_date === null || role_rel.sign_date === '') {
  1467. const dftDate = _getSignDateByAllScenarios(role_rel.acc_id, current_stage_id, STAGE_AUDIT, STAGE_AUDIT_ORG, STAGE_LIST);
  1468. role_rel.sign_date = dftDate;
  1469. }
  1470. }
  1471. }
  1472. if (currRoleRelList && currRoleRelList.length > 0) {
  1473. for (const page of pageData.items) {
  1474. if (page.signature_date_cells) {
  1475. for (const sCell of page.signature_date_cells) {
  1476. sCell.Value = _getSignDateDftName();
  1477. for (const role_rel of currRoleRelList) {
  1478. if (sCell.signature_name === role_rel.signature_name + '_签字日期') {
  1479. if (role_rel.sign_date !== '') {
  1480. if (typeof role_rel.sign_date === 'string') {
  1481. role_rel.sign_date = new Date(role_rel.sign_date);
  1482. }
  1483. sCell.Value = role_rel.sign_date.Format(role_rel.sign_date_format);
  1484. }
  1485. break;
  1486. }
  1487. }
  1488. }
  1489. }
  1490. }
  1491. }
  1492. }
  1493. function _getSignDateByAllScenarios(userAccId, current_stage_id, STAGE_AUDIT, STAGE_AUDIT_ORG, STAGE_LIST) {
  1494. let rst = '';
  1495. let hasAudit = false;
  1496. for (const stg_audit of STAGE_AUDIT) {
  1497. if (stg_audit.aid === userAccId) {
  1498. hasAudit = true;
  1499. if (stg_audit.status === 3) {
  1500. rst = stg_audit.end_time;
  1501. } else {
  1502. rst = '';
  1503. }
  1504. // break; //因为实际业务中会有反复,所以就不break了,一直判断,以最后一个为准
  1505. }
  1506. }
  1507. let isOrgRpt = false;
  1508. for (const stg of STAGE_LIST) {
  1509. if (stg.id === current_stage_id) {
  1510. if (stg.user_id === userAccId) {
  1511. isOrgRpt = true;
  1512. }
  1513. break;
  1514. }
  1515. }
  1516. if (isOrgRpt && !hasAudit && STAGE_AUDIT_ORG && STAGE_AUDIT_ORG.length > 0) {
  1517. if (STAGE_AUDIT_ORG[0].begin_time && STAGE_AUDIT_ORG[0].begin_time !== '' && STAGE_AUDIT_ORG[0].begin_time.length > 20) {
  1518. rst = STAGE_AUDIT_ORG[0].begin_time;
  1519. }
  1520. }
  1521. if (!isOrgRpt && !hasAudit) {
  1522. // 非审批流程人员以及非原报,则显示期截至时间
  1523. for (const stg of STAGE_LIST) {
  1524. if (stg.id === current_stage_id && stg.period) {
  1525. const period = stg.period.split(' ~ ');
  1526. if (period.length === 2) {
  1527. rst = period[1];
  1528. }
  1529. }
  1530. }
  1531. }
  1532. return rst;
  1533. }
  1534. function _getSignDateDftName() {
  1535. return ' 年 月 日';
  1536. }
  1537. async function _chkRawPicSize(pageData, baseDir) {
  1538. let rst = 0;
  1539. const picKeys = [];
  1540. for (let pi = 0; pi < pageData.items.length; pi++) {
  1541. // 每页判断
  1542. for (let ci = 0; ci < pageData.items[pi][JV.PROP_SIGNATURE_CELLS].length; ci++) {
  1543. const picPath = pageData.items[pi][JV.PROP_SIGNATURE_CELLS][ci].path;
  1544. // if (picPath && picPath.indexOf('/sign') < 0 && picKeys.indexOf(picPath) < 0) {
  1545. if (picPath && picPath !== '' && picKeys.indexOf(picPath) < 0) {
  1546. // 签名图片也要计算在内,反正不重复
  1547. picKeys.push(picPath);
  1548. }
  1549. // signature_cells PROP_SIGNATURE_CELLS
  1550. }
  1551. }
  1552. // console.log(picKeys);
  1553. for (const pKey of picKeys) {
  1554. const filePath = `${baseDir}/app${pKey}`;
  1555. console.log('filePath: ' + filePath);
  1556. let isExist = false;
  1557. try {
  1558. isExist = await isFileExisted(filePath);
  1559. } catch (ex) {
  1560. isExist = false;
  1561. }
  1562. console.log('isExist: ' + isExist);
  1563. if (isExist) {
  1564. const obj = fs.statSync(filePath);
  1565. if (obj) {
  1566. rst += obj.size;
  1567. } else {
  1568. console.log('no statSync resutl: ' + filePath);
  1569. }
  1570. // rst += fs.statSync(filePath).size;
  1571. } else {
  1572. console.log(`${pKey} not exists!`);
  1573. }
  1574. }
  1575. return rst;
  1576. }
  1577. function _resetPageDataByBreaks(pageDataArr, breakAmt, rpt_names, newRptNames) {
  1578. const rst = [];
  1579. for (let pi = 0; pi < pageDataArr.length; pi++) {
  1580. const pageItem = pageDataArr[pi];
  1581. const currentRptName = rpt_names[pi];
  1582. if (pageItem.items.length > breakAmt) {
  1583. let pa = Math.floor(pageItem.items.length / breakAmt);
  1584. if (pageItem.items.length % breakAmt > 0) {
  1585. pa++;
  1586. }
  1587. for (let idx = 0; idx < pa; idx++) {
  1588. const newPageDataObj = { items: [] };
  1589. // newPageDataObj[JV.NODE_PAGE_INFO] = JSON.parse(JSON.stringify(pageItem[JV.NODE_PAGE_INFO]));
  1590. newPageDataObj[JV.NODE_PAGE_INFO] = pageItem[JV.NODE_PAGE_INFO];
  1591. // newPageDataObj[JV.BAND_PROP_MERGE_BAND] = JSON.parse(JSON.stringify(pageItem[JV.BAND_PROP_MERGE_BAND]));
  1592. newPageDataObj[JV.BAND_PROP_MERGE_BAND] = pageItem[JV.BAND_PROP_MERGE_BAND];
  1593. newPageDataObj[JV.NODE_FONT_COLLECTION] = pageItem[JV.NODE_FONT_COLLECTION];
  1594. newPageDataObj[JV.NODE_STYLE_COLLECTION] = pageItem[JV.NODE_STYLE_COLLECTION];
  1595. newPageDataObj[JV.NODE_CONTROL_COLLECTION] = pageItem[JV.NODE_CONTROL_COLLECTION];
  1596. for (let dIdx = idx * breakAmt; dIdx < (Math.min((idx + 1) * breakAmt, pageItem.items.length)); dIdx++) {
  1597. newPageDataObj.items.push(pageItem.items[dIdx]);
  1598. }
  1599. rst.push(newPageDataObj);
  1600. newRptNames.push(currentRptName + '_' + (idx + 1));
  1601. }
  1602. } else {
  1603. rst.push(pageItem);
  1604. newRptNames.push(currentRptName);
  1605. }
  1606. }
  1607. return rst;
  1608. };