report_controller.js 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  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 advanceConst = require('../const/advance');
  20. // const stringUtil = require('../public/js/string_util_light');
  21. const needCustomTables = [
  22. 'mem_custom_select',
  23. 'mem_gather_stage_bills', 'mem_gather_deal_bills', 'mem_gather_stage_pay', 'mem_gather_tender_info',
  24. 'mem_stage_sum_bills', 'mem_stage_sum_pay',
  25. 'mem_jh_gather_im_change', 'mem_jh_im_change', 'mem_jh_gather_stage_bills_compare',
  26. 'mem_material_sum_gl',
  27. ];
  28. const PermissionCheck = require('../const/account_permission').PermissionCheck;
  29. const STD_COMP_STAMP_SIZE_WIDTH = Math.round(5 * 96 / 2.54); // 公章标准尺寸(宽4.2厘米)转成像素
  30. const STD_COMP_STAMP_SIZE_HEIGHT = STD_COMP_STAMP_SIZE_WIDTH; // 公章标准尺寸(高4.2厘米)转成像素
  31. const NORMAL_SIGN_STR = 'normal_sign';
  32. const COMPANY_SIGN_STR = 'company_stamp';
  33. const PRIVATE_SIGN_STR = 'private_stamp';
  34. const sourceTypeConst = require('../const/source_type');
  35. module.exports = app => {
  36. class ReportController extends app.BaseController {
  37. /**
  38. * 获取审批界面所需的 原报、审批人数据等
  39. * @param {Object} ctx - egg全局context
  40. * @return {void}
  41. * @private
  42. */
  43. async _getStageAuditViewData(ctx) {
  44. if (!ctx.stage) return;
  45. await ctx.service.stage.loadStageAuditViewData(ctx.stage);
  46. }
  47. async _chkIfStageAuditor(ctx, stage) {
  48. //
  49. }
  50. async _filterStageList(ctx, stageList) {
  51. //
  52. // throw
  53. }
  54. /**
  55. * 报表显示页面
  56. *
  57. * @param {Object} ctx - egg全局context
  58. * @return {void}
  59. */
  60. async index(ctx) {
  61. // 普通情况(计量期入口)
  62. try {
  63. await this._getStageAuditViewData(ctx);
  64. const pageShow = ctx.session.sessionProject.page_show;
  65. pageShow.showArchive = 1;
  66. const tender = ctx.tender;
  67. const stage = ctx.stage;
  68. let stage_id = -1;
  69. let stage_order = -1;
  70. let sorder = -1;
  71. let stage_times = -1;
  72. let stage_status = -1;
  73. const treeNodes = await ctx.service.rptTreeNode.getNodesByProjectId([-1, tender.data.project_id]);
  74. const filterNames = ['01.支付审批报表'];
  75. if (treeNodes && treeNodes.length > 0) {
  76. for (let tIdx = treeNodes.length - 1; tIdx >= 0; tIdx--) {
  77. if (filterNames.includes(treeNodes[tIdx].name)) {
  78. treeNodes.splice(tIdx, 1);
  79. } else if (treeNodes[tIdx].hasOwnProperty('source_type') && treeNodes[tIdx].source_type !== 1) {
  80. treeNodes.splice(tIdx, 1);
  81. }
  82. }
  83. }
  84. const custTreeNodes = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(this.ctx.session.sessionUser.accountId);
  85. const custCfg = await ctx.service.rptCustomizeCfg.getCustomizeCfgByUserId('Administrator');
  86. const stageList = await ctx.service.stage.getValidStagesShort(tender.id);
  87. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: tender.data.project_id } }); // 找公司章用的
  88. const isAdmin = ctx.session.sessionUser.is_admin;
  89. // console.log('this.ctx.session.sessionUser.accountId: ' + this.ctx.session.sessionUser.accountId);
  90. if (stage && stage.status === auditConst.stage.status.uncheck) {
  91. // 得判断账号是否在审核人列表中(不含原报)
  92. if (stage.auditorList === undefined || stage.auditorList === null) {
  93. stage.auditorList = await ctx.service.stageAudit.getAuditors(ctx.stage.id, ctx.stage.times);
  94. }
  95. let isAudit = false;
  96. for (const audit of stage.auditorList) {
  97. if (audit.aid === this.ctx.session.sessionUser.accountId) {
  98. isAudit = true;
  99. break;
  100. }
  101. }
  102. if (isAudit) {
  103. throw '未上报!';
  104. }
  105. } else {
  106. // 剔除未上报期(审核人(非原报人)才需要判断)
  107. for (let idx = stageList.length - 1; idx >= 0; idx--) {
  108. if (stageList[idx].status === auditConst.stage.status.uncheck) {
  109. if (stageList[idx].auditorList === undefined || stageList[idx].auditorList === null) {
  110. stageList[idx].auditorList = await ctx.service.stageAudit.getAuditors(stageList[idx].id, stageList[idx].times);
  111. }
  112. let isAudit = false;
  113. for (const audit of stageList[idx].auditorList) {
  114. if (audit.aid === this.ctx.session.sessionUser.accountId) {
  115. isAudit = true;
  116. break;
  117. }
  118. }
  119. if (isAudit) {
  120. stageList.splice(idx, 1);
  121. }
  122. }
  123. }
  124. }
  125. let lastAuditor = null;
  126. if (stage) {
  127. lastAuditor = await ctx.service.stageAudit.getLastestAuditor(ctx.stage.id, ctx.stage.times, auditConst.stage.status.checked);
  128. } else {
  129. lastAuditor = stageList.length > 0 ? await ctx.service.stageAudit.getLastestAuditor(stageList[stageList.length - 1].id, stageList[stageList.length - 1].times, auditConst.stage.status.checked) : null;
  130. }
  131. let archiveList = [];
  132. if (stage) {
  133. const archives = await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, ctx.stage.id);
  134. if (archives.length > 0) {
  135. archiveList = JSON.parse(archives[0].content);
  136. }
  137. } else {
  138. const archives = stageList.length > 0 ? await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, stageList[stageList.length - 1].id) : [];
  139. // console.log(archives);
  140. if (archives && archives.length > 0) {
  141. archiveList = JSON.parse(archives[0].content);
  142. }
  143. }
  144. // const archiveList = await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, ctx.stage.id);
  145. let prjAccList = await ctx.service.projectAccount.getAllAccountByProjectId(tender.data.project_id);
  146. const roleList = await ctx.service.signatureRole.getSignatureRolesByTenderId(tender.id);
  147. const usedList = await ctx.service.signatureUsed.getSignatureUsedByTenderId(tender.id);
  148. // ctx.session.sessionUser.is_admin
  149. // const pageShow = ctx.session.sessionProject.page_show;
  150. // 根据需求,如果开启了‘开启个人「签字」’功能,则只能是本身用户签名,管理员例外
  151. if (!ctx.session.sessionUser.is_admin && pageShow.individualSign === 1) {
  152. // 削减其他account
  153. const newAccList = [];
  154. for (const acc of prjAccList) {
  155. if (acc.id === this.ctx.session.sessionUser.accountId) {
  156. newAccList.push(acc);
  157. }
  158. }
  159. prjAccList = newAccList;
  160. }
  161. // 分类列表
  162. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.session.sessionProject.id);
  163. // 获取用户权限
  164. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  165. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  166. // 获取用户可查看的标段
  167. const tenderList = await this.ctx.service.tender.getList('', userPermission);
  168. for (const t of tenderList) {
  169. if (t.ledger_status === auditConst.ledger.status.checked) {
  170. t.lastStage = await this.ctx.service.stage.getLastestStage(t.id, true);
  171. // if (t.lastStage) {
  172. // await this.ctx.service.stage.checkStageGatherData(t.lastStage, this.ctx.session.sessionUser.is_admin);
  173. // }
  174. }
  175. }
  176. // 获取汇总表,历史选择
  177. const cid = this.ctx.helper._.map(treeNodes, 'id');
  178. const customSelects = {};
  179. if (ctx.stage) {
  180. customSelects.audit_select = await ctx.service.rptCustomDefine.getCustomSelectByRpt(cid,
  181. reportConst.rptCustomType[JV.NODE_CUS_AUDIT_SELECT], ctx.tender.id, ctx.stage.id);
  182. customSelects.stageFlow = await ctx.service.stageAudit.getAuditGroupByListWithOwner(
  183. ctx.stage.stage_id, ctx.stage.stage_times);
  184. customSelects.stageFlow.forEach(x => {
  185. if (!x) return;
  186. x.visible = (!ctx.session.sessionUser.is_admin && ctx.session.sessionProject.page_show.individualSign === 1)
  187. ? x.aid === ctx.session.sessionUser.accountId : true;
  188. });
  189. } else {
  190. customSelects.audit_select = [];
  191. }
  192. customSelects.gather_select = await ctx.service.rptCustomDefine.getCustomSelectByRpt(cid,
  193. reportConst.rptCustomType[JV.NODE_CUS_GATHER_SELECT], ctx.tender.id, -1);
  194. customSelects.stage_select = await ctx.service.rptCustomDefine.getCustomSelectByRpt(cid,
  195. reportConst.rptCustomType[JV.NODE_CUS_STAGE_SELECT], ctx.tender.id, -1);
  196. customSelects.change_select = await ctx.service.rptCustomDefine.getCustomSelectByRpt(cid,
  197. reportConst.rptCustomType[JV.NODE_CUS_CHANGE_SELECT], ctx.tender.id, -1);
  198. customSelects.material_sum_select = await ctx.service.rptCustomDefine.getCustomSelectByRpt(cid,
  199. reportConst.rptCustomType[JV.NODE_CUS_MATERIAL_SUM_SELECT], ctx.tender.id, -1);
  200. const dataSelects = {};
  201. dataSelects.material_select = await ctx.service.rptCustomDefine.getDataSelectByRpt(cid,
  202. reportConst.rptDataType[JV.NODE_CUS_MATERIAL_SELECT]);
  203. const materialList = await ctx.service.material.getValidMaterials(ctx.tender.id);
  204. if (stage !== null && stage !== undefined) {
  205. stage_id = stage.id;
  206. stage_order = stage.order;
  207. sorder = stage.curOrder;
  208. stage_times = stage.times;
  209. stage_status = stage.status;
  210. }
  211. for (const prjAcc of prjAccList) {
  212. prjAcc.account_group = accountGroup[prjAcc.account_group];
  213. }
  214. let rpt_tpl_items = '{ customize: [], common: [] }';
  215. if (custTreeNodes.length > 0) {
  216. rpt_tpl_items = custTreeNodes[0].rpt_tpl_items;
  217. }
  218. // 获取所有项目参与者
  219. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  220. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  221. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  222. });
  223. const newAccountGroup = accountGroup.map((item, idx) => {
  224. const groupList = accountList.filter(item => item.account_group === idx);
  225. return { groupName: item, groupList };
  226. });
  227. const where = { tid: this.ctx.tender.id };
  228. const advance = await ctx.service.advance.getAllDataByCondition({
  229. columns: ['id', 'order', 'status', 'selected', 'type'],
  230. where,
  231. orders: [['type', 'asc'], ['create_time', 'desc']],
  232. });
  233. advance.forEach(x => {
  234. x.statusStr = auditConst.advance.statusString[x.status];
  235. x.typeStr = advanceConst.typeColMap[x.type].text;
  236. });
  237. const renderData = {
  238. accountGroup: newAccountGroup,
  239. accountList,
  240. unitList: JSON.stringify(unitList),
  241. tender: tender.data,
  242. tenderInfo: tender.info,
  243. rpt_tpl_data: JSON.stringify(treeNodes),
  244. cust_tpl_data: rpt_tpl_items,
  245. cust_cfg: JSON.stringify(custCfg),
  246. project_id: tender.data.project_id,
  247. tender_id: tender.id,
  248. tender_name: tender.data.name,
  249. detail_id: -1,
  250. stg_id: stage_id,
  251. stg_order: stage_order,
  252. cur_order: sorder,
  253. stg_times: stage_times,
  254. stg_status: stage_status,
  255. stage_list: JSON.stringify(stageList),
  256. prj_account_list: JSON.stringify(prjAccList),
  257. role_list: JSON.stringify(roleList),
  258. used_list: JSON.stringify(usedList),
  259. tenderMenu,
  260. preUrl: '/tender/' + ctx.tender.id + '/measure/stage/' + ctx.params.order,
  261. measureType,
  262. categoryData,
  263. tenderList,
  264. auditConst: auditConst.stage,
  265. ledgerAuditConst: auditConst.ledger,
  266. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.report.main),
  267. customSelects,
  268. rptCustomType: reportConst.rptCustomType,
  269. materialList,
  270. stages: stageList,
  271. dataSelects,
  272. // pageShow: JSON.stringify(pageShow),
  273. pageShow,
  274. authMobile: accountInfo.auth_mobile,
  275. shenpiConst,
  276. archiveList,
  277. lastAuditor,
  278. rpt_id: ctx.query.rpt_id,
  279. isAdmin,
  280. prePay: JSON.stringify(advance),
  281. OSS_PATH: ctx.app.config.fujianOssPath,
  282. viewPmData: PermissionCheck.viewPmData(this.ctx.session.sessionUser.permission),
  283. auditType: auditConst.auditType,
  284. };
  285. await this.layout('report/index.ejs', renderData, 'report/rpt_all_popup.ejs');
  286. // await this.layout('report/index.ejs', renderData);
  287. // 在这里启动一个cron job,删除临时创建的excel文件
  288. if (!rptCronJob.started) {
  289. try {
  290. // console.log('rptCronJob.started: ' + rptCronJob.started);
  291. rptCronJob.started = true;
  292. // console.log('this.app.baseDir: ' + this.app.baseDir);
  293. rptCronJob.createJob(null, this.app.baseDir + '/app/public/download');
  294. } catch (ex) {
  295. rptCronJob.started = false;
  296. }
  297. }
  298. } catch (err) {
  299. this.log(err);
  300. ctx.redirect('/tender/' + ctx.tender.id + '/measure/stage');
  301. }
  302. }
  303. async indexForPaymentSafe(ctx) {
  304. // 安全生产费入口
  305. try {
  306. await this._getStageAuditViewData(ctx);
  307. const pageShow = ctx.session.sessionProject.page_show;
  308. pageShow.showArchive = 1;
  309. // pageShow.closeWatermark = 1;
  310. pageShow.showArchive = 0;
  311. const tenderId = ctx.params.id;
  312. const paymentDetail_id = ctx.params.did;
  313. // 支付审批: ctx.params.did 是 zh_payment_detail表的id, 通过这个did在 zh_payment_detail_audit表里找审核信息(td_id = ctx.params.did)
  314. // 没有计量期的概念!!!
  315. const paymentAuditorList = await ctx.service.paymentDetailAudit.getAuditors(paymentDetail_id);
  316. const tender = ctx.paymentTender;
  317. // const treeNodes = await ctx.service.rptTreeNode.getNodesByProjectId([-1, tender.pid]);
  318. const treeNodes = await ctx.service.rptTreeNode.getNodesBySourceType([tender.pid], 101); //
  319. const commonTreeNodes = await ctx.service.rptTreeNode.getNodesByProjectId([-1]); // 这个查通用的
  320. const nodeItems = JSON.parse(commonTreeNodes[0].items);
  321. const commonArrs = [];
  322. nodeItems.forEach(nodeItem => {
  323. if (nodeItem.source_type === 101) {
  324. commonArrs.push(nodeItem);
  325. }
  326. });
  327. // 加一个dummy的通用报表
  328. const dummyCommonRptNode = { id: 1, name: '通用报表', pid: -1, rpt_type: 0, items: JSON.stringify(commonArrs) };
  329. treeNodes.push(dummyCommonRptNode);
  330. // const custTreeNodes = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(this.ctx.session.sessionUser.accountId);
  331. const custCfg = await ctx.service.rptCustomizeCfg.getCustomizeCfgByUserId('Administrator');
  332. // const stageList = await ctx.service.stage.getValidStagesShort(tender.id);
  333. const stageList = [];
  334. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: tender.pid } }); // 找公司章用的
  335. const isAdmin = ctx.session.sessionUser.is_admin;
  336. const lastAuditor = null;
  337. const archiveList = [];
  338. let prjAccList = await ctx.service.projectAccount.getAllAccountByProjectId(tender.pid);
  339. const roleList = await ctx.service.signatureRole.getSignatureRolesByTenderId(tender.id);
  340. const usedList = await ctx.service.signatureUsed.getSignatureUsedByTenderId(tender.id);
  341. // 根据需求,如果开启了‘开启个人「签字」’功能,则只能是本身用户签名,管理员例外
  342. if (!ctx.session.sessionUser.is_admin && pageShow.individualSign === 1) {
  343. // 削减其他account
  344. const newAccList = [];
  345. for (const acc of prjAccList) {
  346. if (acc.id === this.ctx.session.sessionUser.accountId) {
  347. newAccList.push(acc);
  348. }
  349. }
  350. prjAccList = newAccList;
  351. }
  352. // 分类列表
  353. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.session.sessionProject.id);
  354. // 获取用户权限
  355. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  356. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  357. // 获取用户可查看的标段
  358. // ...
  359. const tenderList = [];
  360. const cid = this.ctx.helper._.map(treeNodes, 'id');
  361. const customSelects = null;
  362. const dataSelects = {};
  363. dataSelects.material_select = await ctx.service.rptCustomDefine.getDataSelectByRpt(cid,
  364. reportConst.rptDataType[JV.NODE_CUS_MATERIAL_SELECT]);
  365. const materialList = await ctx.service.material.getValidMaterials();
  366. const stage_id = -100;
  367. let stage_order = -1;
  368. let sorder = -1;
  369. let stage_times = -1;
  370. let stage_status = -1;
  371. const paymentDetailList = await ctx.service.paymentDetail.getDetail(paymentDetail_id);
  372. if (paymentDetailList && paymentDetailList.length > 0) {
  373. const payment = paymentDetailList[0];
  374. stage_status = payment.status;
  375. stage_order = payment.order;
  376. sorder = stage_order;
  377. stage_times = payment.times;
  378. stageList.push({ id: stage_id, user_id: payment.uid, status: payment.status });
  379. }
  380. for (const prjAcc of prjAccList) {
  381. prjAcc.account_group = accountGroup[prjAcc.account_group];
  382. }
  383. const rpt_tpl_items = '{ customize: [], common: [] }';
  384. // if (custTreeNodes.length > 0) {
  385. // rpt_tpl_items = custTreeNodes[0].rpt_tpl_items;
  386. // }
  387. // 获取所有项目参与者
  388. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  389. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  390. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  391. });
  392. const newAccountGroup = accountGroup.map((item, idx) => {
  393. const groupList = accountList.filter(item => item.account_group === idx);
  394. return { groupName: item, groupList };
  395. });
  396. const where = { tid: tenderId };
  397. const advance = await ctx.service.advance.getAllDataByCondition({
  398. columns: ['id', 'order', 'status', 'selected', 'type'],
  399. where,
  400. orders: [['type', 'asc'], ['create_time', 'desc']],
  401. });
  402. advance.forEach(x => {
  403. x.statusStr = auditConst.advance.statusString[x.status];
  404. x.typeStr = advanceConst.typeColMap[x.type].text;
  405. });
  406. const renderData = {
  407. accountGroup: newAccountGroup,
  408. accountList,
  409. unitList: JSON.stringify(unitList),
  410. tender: null,
  411. tenderInfo: null,
  412. rpt_tpl_data: JSON.stringify(treeNodes),
  413. cust_tpl_data: rpt_tpl_items,
  414. cust_cfg: JSON.stringify(custCfg),
  415. project_id: tender.pid,
  416. tender_id: tenderId,
  417. tender_name: tender.name,
  418. detail_id: paymentDetail_id,
  419. stg_id: stage_id,
  420. stg_order: stage_order,
  421. cur_order: sorder,
  422. stg_times: stage_times,
  423. stg_status: stage_status,
  424. stage_list: JSON.stringify(stageList),
  425. prj_account_list: JSON.stringify(prjAccList),
  426. role_list: JSON.stringify(roleList),
  427. used_list: JSON.stringify(usedList),
  428. tenderMenu,
  429. preUrl: '/tender/' + tenderId + '/measure/stage/' + ctx.params.order,
  430. measureType,
  431. categoryData,
  432. tenderList,
  433. auditConst: auditConst.stage,
  434. ledgerAuditConst: auditConst.ledger,
  435. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.report.main),
  436. customSelects,
  437. rptCustomType: reportConst.rptCustomType,
  438. materialList,
  439. stages: stageList,
  440. dataSelects,
  441. pageShow,
  442. authMobile: accountInfo.auth_mobile,
  443. shenpiConst,
  444. archiveList,
  445. lastAuditor,
  446. rpt_id: ctx.query.rpt_id,
  447. isAdmin,
  448. prePay: JSON.stringify(advance),
  449. OSS_PATH: ctx.app.config.fujianOssPath,
  450. viewPmData: PermissionCheck.viewPmData(this.ctx.session.sessionUser.permission),
  451. };
  452. /*
  453. //*/
  454. await this.layout('report/index.ejs', renderData, 'report/rpt_all_popup.ejs');
  455. } catch (err) {
  456. this.log(err);
  457. // ctx.redirect('/tender/' + ctx.tender.id + '/measure/stage');
  458. }
  459. }
  460. async indexForDynamicGrandTotal(ctx) {
  461. // 动态决算(动态投资)入口 (以独立报表为蓝本(index入口))
  462. try {
  463. await this._getStageAuditViewData(ctx);
  464. const pageShow = ctx.session.sessionProject.page_show;
  465. pageShow.showArchive = 1;
  466. // pageShow.closeWatermark = 1;
  467. pageShow.showArchive = 0;
  468. const tenderId = -1;
  469. const paymentDetail_id = -1;
  470. // const tender = ctx.paymentTender;
  471. const pid = ctx.budget.pid;
  472. const treeNodes = await ctx.service.rptTreeNode.getNodesBySourceType([pid], 200); // 这个查定制的
  473. const commonTreeNodes = await ctx.service.rptTreeNode.getNodesByProjectId([-1]); // 这个查通用的
  474. const nodeItems = JSON.parse(commonTreeNodes[0].items);
  475. const commonArrs = [];
  476. nodeItems.forEach(nodeItem => {
  477. if (nodeItem.source_type === 200) {
  478. commonArrs.push(nodeItem);
  479. }
  480. });
  481. // 加一个dummy的通用报表
  482. const dummyCommonRptNode = { id: 1, name: '通用报表', pid: -1, rpt_type: 0, items: JSON.stringify(commonArrs) };
  483. treeNodes.push(dummyCommonRptNode);
  484. // const custTreeNodes = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(this.ctx.session.sessionUser.accountId);
  485. const custCfg = await ctx.service.rptCustomizeCfg.getCustomizeCfgByUserId('Administrator');
  486. const stageList = [];
  487. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid } }); // 找公司章用的
  488. const isAdmin = ctx.session.sessionUser.is_admin;
  489. const lastAuditor = null;
  490. const archiveList = [];
  491. let prjAccList = await ctx.service.projectAccount.getAllAccountByProjectId(pid);
  492. const roleList = [];
  493. const usedList = [];
  494. // 根据需求,如果开启了‘开启个人「签字」’功能,则只能是本身用户签名,管理员例外
  495. if (!ctx.session.sessionUser.is_admin && pageShow.individualSign === 1) {
  496. // 削减其他account
  497. const newAccList = [];
  498. for (const acc of prjAccList) {
  499. if (acc.id === this.ctx.session.sessionUser.accountId) {
  500. newAccList.push(acc);
  501. }
  502. }
  503. prjAccList = newAccList;
  504. }
  505. // 分类列表
  506. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.session.sessionProject.id);
  507. // 获取用户权限
  508. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  509. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  510. // 获取用户可查看的标段
  511. // ...
  512. const tenderList = [];
  513. const cid = this.ctx.helper._.map(treeNodes, 'id');
  514. const customSelects = null;
  515. const dataSelects = {};
  516. dataSelects.material_select = await ctx.service.rptCustomDefine.getDataSelectByRpt(cid,
  517. reportConst.rptDataType[JV.NODE_CUS_MATERIAL_SELECT]);
  518. const materialList = await ctx.service.material.getValidMaterials();
  519. const stage_id = -200;
  520. const stage_order = -1;
  521. const sorder = -1;
  522. const stage_times = -1;
  523. const stage_status = -1;
  524. for (const prjAcc of prjAccList) {
  525. prjAcc.account_group = accountGroup[prjAcc.account_group];
  526. }
  527. const rpt_tpl_items = '{ customize: [], common: [] }';
  528. // 获取所有项目参与者
  529. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  530. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  531. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  532. });
  533. const newAccountGroup = accountGroup.map((item, idx) => {
  534. const groupList = accountList.filter(item => item.account_group === idx);
  535. return { groupName: item, groupList };
  536. });
  537. const where = { tid: tenderId };
  538. const advance = await ctx.service.advance.getAllDataByCondition({
  539. columns: ['id', 'order', 'status', 'selected', 'type'],
  540. where,
  541. orders: [['type', 'asc'], ['create_time', 'desc']],
  542. });
  543. advance.forEach(x => {
  544. x.statusStr = auditConst.advance.statusString[x.status];
  545. x.typeStr = advanceConst.typeColMap[x.type].text;
  546. });
  547. const renderData = {
  548. accountGroup: newAccountGroup,
  549. accountList,
  550. unitList: JSON.stringify(unitList),
  551. tender: null,
  552. tenderInfo: null,
  553. rpt_tpl_data: JSON.stringify(treeNodes),
  554. cust_tpl_data: rpt_tpl_items,
  555. cust_cfg: JSON.stringify(custCfg),
  556. project_id: pid,
  557. tender_id: tenderId,
  558. tender_name: '',
  559. detail_id: paymentDetail_id,
  560. stg_id: stage_id,
  561. stg_order: stage_order,
  562. cur_order: sorder,
  563. stg_times: stage_times,
  564. stg_status: stage_status,
  565. stage_list: JSON.stringify(stageList),
  566. prj_account_list: JSON.stringify(prjAccList),
  567. role_list: JSON.stringify(roleList),
  568. used_list: JSON.stringify(usedList),
  569. tenderMenu,
  570. preUrl: '/tender/' + tenderId + '/measure/stage/' + ctx.params.order,
  571. measureType,
  572. categoryData,
  573. tenderList,
  574. auditConst: auditConst.stage,
  575. ledgerAuditConst: auditConst.ledger,
  576. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.report.main),
  577. customSelects,
  578. rptCustomType: reportConst.rptCustomType,
  579. materialList,
  580. stages: stageList,
  581. dataSelects,
  582. pageShow,
  583. authMobile: accountInfo.auth_mobile,
  584. shenpiConst,
  585. archiveList,
  586. lastAuditor,
  587. rpt_id: ctx.query.rpt_id,
  588. isAdmin,
  589. prePay: JSON.stringify(advance),
  590. OSS_PATH: ctx.app.config.fujianOssPath,
  591. viewPmData: PermissionCheck.viewPmData(this.ctx.session.sessionUser.permission),
  592. };
  593. await this.layout('report/index.ejs', renderData, 'report/rpt_all_popup.ejs');
  594. } catch (err) {
  595. this.log(err);
  596. }
  597. }
  598. /**
  599. * 报表打印页面
  600. *
  601. * @param {Object} ctx - egg全局context
  602. * @return {void}
  603. */
  604. async showPrintPage(ctx) {
  605. try {
  606. // const params = JSON.parse(ctx.request.body.params);
  607. const pageSize = ctx.params.size;
  608. // console.log('pageSize: ' + pageSize);
  609. const renderData = {
  610. size: pageSize,
  611. // orientation: params.orientation,
  612. };
  613. await ctx.render('report/rpt_print.ejs', renderData);
  614. } catch (err) {
  615. this.log(err);
  616. console.log(err);
  617. }
  618. }
  619. async showIndividualPage(ctx) {
  620. try {
  621. // const params = JSON.parse(ctx.request.body.params);
  622. const pageSize = ctx.params.size;
  623. // console.log('pageSize: ' + pageSize);
  624. const renderData = {
  625. size: pageSize,
  626. OSS_PATH: ctx.app.config.fujianOssPath,
  627. // orientation: params.orientation,
  628. };
  629. await ctx.render('report/rpt_individual.ejs', renderData);
  630. } catch (err) {
  631. this.log(err);
  632. console.log(err);
  633. }
  634. }
  635. async _getReport(ctx, params) {
  636. // console.log('in getReport');
  637. // console.log(params);
  638. let rptTpl = await ctx.service.rptTpl.getTplById(params.rpt_tpl_id);
  639. if (!rptTpl || rptTpl.length !== 1) {
  640. throw '获取模板失败';
  641. }
  642. const source_type = rptTpl[0].source_type;
  643. rptTpl = JSON.parse(rptTpl[0].rpt_content);
  644. rptTpl.source_type = source_type || sourceTypeConst.defaultSourceType;
  645. rptTpl.id = params.rpt_tpl_id;
  646. // console.log('get the template!');
  647. const customSelect = rptTpl[JV.NODE_CUSTOM_DEFINE] && rptTpl[JV.NODE_CUSTOM_DEFINE][JV.NODE_CUS_AUDIT_SELECT].enable
  648. ? await ctx.service.rptCustomDefine.getCustomDefine(params.tender_id, params.stage_id, params.rpt_tpl_id)
  649. : await ctx.service.rptCustomDefine.getCustomDefine(params.tender_id, -1, params.rpt_tpl_id);
  650. let copyCustomSelect = this.ctx.helper.clone(customSelect);
  651. if (!params.gather_select && copyCustomSelect) delete copyCustomSelect.gather_select;
  652. if (!params.stage_select && copyCustomSelect) delete copyCustomSelect.stage_select;
  653. if (!params.material_sum_select && copyCustomSelect) delete copyCustomSelect.material_sum_select;
  654. if (params.change_select) {
  655. copyCustomSelect = { tid: params.tender_id, rid: params.rpt_tpl_id, sid: -1, change_select: params.change_select };
  656. }
  657. const pageRst = await ctx.service.jpcReport.getAllPagesCommon(ctx, rptTpl, params, JV.PAGING_OPTION_NORMAL, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir, copyCustomSelect);
  658. for (const page of pageRst.items) {
  659. page[JV.PROP_WATERMARK_CELLS] = [];
  660. }
  661. let waterMarkStr = null;
  662. if (params.stage_status !== 3 && params.closeWatermark === 0) {
  663. // 加水印
  664. fillWaterMark([pageRst]);
  665. if (params.needWaterMark) {
  666. waterMarkStr = await getWatermarkPicData(pageRst, this.app.baseDir);
  667. }
  668. }
  669. // console.log(pageRst);
  670. // const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id)) : [];
  671. const roleRel = await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id, params.stage_id); // 新需求中,允许在非审核状态下设置签名
  672. let stgAudit = null,
  673. stgAuditForOrg = null,
  674. stageFlow = null,
  675. stageList = [];
  676. if (![-100, -200, -300].includes(params.stage_id)) {
  677. stgAudit = await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times);
  678. stgAuditForOrg = await ctx.service.stageAudit.getStageAudit(params.stage_id, 1);
  679. stageList = await ctx.service.stage.getValidStagesShort(params.tender_id);
  680. stageFlow = await ctx.service.stageAudit.getAuditGroupByListWithOwner(params.stage_id, params.stage_times);
  681. } else {
  682. stgAudit = await ctx.service.paymentDetailAudit.getAuditors(params.detail_id, params.stage_times);
  683. stgAuditForOrg = await ctx.service.paymentDetailAudit.getAuditors(params.detail_id, 1);
  684. stageFlow = await ctx.service.paymentDetailAudit.getAuditorsWithOwner(params.detail_id);
  685. const paymentDetailList = await ctx.service.paymentDetail.getDetail(params.detail_id);
  686. if (paymentDetailList && paymentDetailList.length > 0) {
  687. const payment = paymentDetailList[0];
  688. stageList.push({ id: params.stage_id, user_id: payment.uid, status: payment.status });
  689. }
  690. }
  691. stageFlow.forEach(x => {
  692. if (!x) return;
  693. x.visible = (!ctx.session.sessionUser.is_admin && ctx.session.sessionProject.page_show.individualSign === 1)
  694. ? x.aid === ctx.session.sessionUser.accountId : true;
  695. });
  696. mergeStampSignature(ctx, params.stage_status, pageRst, roleRel[0], null, stgAudit, stageList);
  697. mergeTextSignature(params.isTextSignature, params.stage_status, pageRst, roleRel[0], [], stgAudit, stageList);
  698. await encodeSignatureDataUri(roleRel, this.app.baseDir);
  699. if (params.getPicFlag) {
  700. await encodeDummySignatureDataUri(pageRst, this.app.baseDir); // 注意草图数据量问题!
  701. }
  702. // console.log('encodeSignatureDataUri!');
  703. // fsUtil.writeObjToFile(pageRst, 'D:/GitHome/Temp/testBuiltPageResult2.js');
  704. return {
  705. data: pageRst,
  706. signatureRelInfo: roleRel,
  707. stageAudit: stgAudit,
  708. stageAuditOrg: stgAuditForOrg,
  709. debugInfo: ctx.session.sessionUser.loginStatus ? ctx.debugInfo : null,
  710. customDefine: rptTpl[JV.NODE_CUSTOM_DEFINE],
  711. stageFlow,
  712. customSelect,
  713. waterMarkStr,
  714. };
  715. }
  716. /**
  717. * 获取报表数据
  718. *
  719. * @param {Object} ctx - egg全局context
  720. * @return {void}
  721. */
  722. async getReport(ctx) {
  723. try {
  724. // console.log('in getReport');
  725. const params = JSON.parse(ctx.request.body.params);
  726. ctx.body = await this._getReport(ctx, params);
  727. ctx.status = 201;
  728. } catch (ex) {
  729. console.log(ex);
  730. this.setMessage(ex.toString(), this.messageType.ERROR);
  731. }
  732. }
  733. async getTestReport(ctx) {
  734. try {
  735. const file = 'D:/GitHome/Temp/导出PDF_格式样本1.js';
  736. const tplObj = JSON.parse(fs.readFileSync(file, 'utf-8'));
  737. ctx.body = { data: tplObj };
  738. ctx.status = 201;
  739. } catch (ex) {
  740. this.setMessage(ex.toString(), this.messageType.ERROR);
  741. }
  742. }
  743. /**
  744. * 获取多批次报表数据
  745. *
  746. * @param {Object} ctx - egg全局context
  747. * @return {void}
  748. */
  749. async getMultiReportsEx(ctx) {
  750. // 原则说明:把所有报表模板集中获取,统一filter,只读一次数据!
  751. const params = JSON.parse(ctx.request.body.params);
  752. const pageShow = ctx.session.sessionProject.page_show;
  753. if (pageShow !== null
  754. && pageShow !== undefined
  755. && params.outputType
  756. && ((params.outputType === 'PDF' && parseInt(pageShow.closeExportPdf) === 1)
  757. || (params.outputType === 'Excel' && parseInt(pageShow.closeExportExcel) === 1)
  758. )
  759. ) {
  760. ctx.body = { data: [], signatureRelInfo: [], stageAudit: [] };
  761. ctx.status = 201;
  762. return;
  763. }
  764. if (![-100, -200, -300].includes(params.stage_id)) {
  765. await this._saveCustomSelects(params);
  766. }
  767. // const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir) || [];
  768. const pageRstArr = await ctx.service.jpcReport.getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir) || [];
  769. for (const pageRst of pageRstArr) {
  770. for (const page of pageRst.items) {
  771. page[JV.PROP_WATERMARK_CELLS] = [];
  772. }
  773. }
  774. let waterMarkStr = null;
  775. if (params.stage_status !== 3 && params.closeWatermark === 0) {
  776. // 加水印
  777. fillWaterMark(pageRstArr);
  778. if (params.needWaterMark) {
  779. waterMarkStr = await getWatermarkPicData(pageRstArr[0], this.app.baseDir);
  780. }
  781. }
  782. let stgAudit = null,
  783. stageList = [];
  784. if (![-100, -200, -300].includes(params.stage_id)) {
  785. stgAudit = await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times);
  786. stageList = await ctx.service.stage.getValidStagesShort(params.tender_id);
  787. } else {
  788. stgAudit = await ctx.service.paymentDetailAudit.getAuditors(params.detail_id);
  789. const paymentDetailList = await ctx.service.paymentDetail.getDetail(params.detail_id);
  790. if (paymentDetailList && paymentDetailList.length > 0) {
  791. const payment = paymentDetailList[0];
  792. stageList.push({ id: params.stage_id, user_id: payment.uid, status: payment.status });
  793. }
  794. }
  795. // const stgAudit = await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times);
  796. // const stageList = await ctx.service.stage.getValidStagesShort(params.tender_id);
  797. // const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids, params.stage_id)) : [];
  798. const roleRel = await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids, params.stage_id); // 新需求:在审批流程中的签名需要显示
  799. roleRel.sort(function(rr1, rr2) {
  800. return params.rpt_ids.indexOf(parseInt(rr1.rpt_id)) - params.rpt_ids.indexOf(parseInt(rr2.rpt_id));
  801. });
  802. // console.log('roleRel: ');
  803. // console.log(roleRel);
  804. mergeStampSignature(ctx, params.stage_status, pageRstArr, roleRel, params.rpt_ids, stgAudit, stageList);
  805. mergeTextSignature(params.isTextSignature, params.stage_status, pageRstArr, roleRel, params.rpt_ids, stgAudit, stageList);
  806. await encodeSignatureDataUri(roleRel, this.app.baseDir);
  807. if (params.getPicFlag) {
  808. for (const pageRst of pageRstArr) {
  809. await encodeDummySignatureDataUri(pageRst, this.app.baseDir); // 注意草图数据量问题!
  810. }
  811. }
  812. // console.log(pageRstArr);
  813. ctx.body = { data: pageRstArr, signatureRelInfo: roleRel, stageAudit: stgAudit, waterMarkStr };
  814. ctx.status = 201;
  815. }
  816. async _saveCustomSelects(params) {
  817. for (const [i, cs] of params.customSelect.entries()) {
  818. if (!cs) continue;
  819. cs.tender_id = params.tender_id;
  820. cs.stage_id = params.stage_id;
  821. cs.rpt_tpl_id = params.rpt_ids[i];
  822. await this.ctx.service.rptCustomDefine.saveCustomSelect(cs);
  823. }
  824. }
  825. async createExcelFilesEx(ctx) {
  826. const params = JSON.parse(ctx.request.body.params);
  827. const pageShow = ctx.session.sessionProject.page_show;
  828. if (pageShow !== null && pageShow !== undefined && parseInt(pageShow.closeExportExcel) === 1) {
  829. ctx.body = { data: [] };
  830. ctx.status = 201;
  831. return;
  832. }
  833. await this._saveCustomSelects(params);
  834. const baseDir = this.app.baseDir;
  835. function getExcelByPageData(pageRst, rpt_name, innerRoleRel) {
  836. return new Promise(function(resolve, reject) {
  837. rpt_xl_util.exportExcel(pageRst, params.pageSize, rpt_name, params.isOneSheet, null, null, baseDir, innerRoleRel,
  838. (err, uuidName) => {
  839. if (err) return reject(err);
  840. const fileRst = { uuid: uuidName, reportName: rpt_name.replace(new RegExp('%', 'gm'), '_') };
  841. resolve(fileRst);
  842. }
  843. );
  844. });
  845. }
  846. // const stgAudit = (params.stage_status === 3) ? (await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times)) : [];
  847. const stgAudit = await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times);
  848. const stgAuditForOrg = (params.stage_status === 3) ? (await ctx.service.stageAudit.getStageAudit(params.stage_id, 1)) : [];
  849. // const stageList = (params.stage_status === 3) ? (await ctx.service.stage.getValidStagesShort(params.tender_id)) : [];
  850. const stageList = await ctx.service.stage.getValidStagesShort(params.tender_id);
  851. const roleRelArr = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids, params.stage_id)) : [];
  852. // const roleRel = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_tpl_id)) : [];
  853. // const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, baseDir);
  854. const pageRstArr = await ctx.service.jpcReport.getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, baseDir);
  855. // console.log('params.stage_status: ' + params.stage_status);
  856. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/testBuiltPageResult1.js');
  857. for (const pageRst of pageRstArr) {
  858. for (const page of pageRst.items) {
  859. page[JV.PROP_WATERMARK_CELLS] = [];
  860. }
  861. }
  862. let waterMarkStr = null;
  863. if (params.stage_status !== 3 && params.closeWatermark === 0) {
  864. // if (params.stage_status !== 3 && params.closeWatermark === 0 && params.option === JV.PAGING_OPTION_NORMAL) {
  865. // 加水印(注意:还得看用户设置是否需要加水印)
  866. fillWaterMark(pageRstArr);
  867. if (params.needWaterMark) {
  868. waterMarkStr = await getWatermarkPicData(pageRstArr[0], baseDir);
  869. }
  870. }
  871. mergeStampSignature(ctx, params.stage_status, pageRstArr, roleRelArr, params.rpt_ids, stgAudit, stageList);
  872. mergeTextSignature(params.isTextSignature, params.stage_status, pageRstArr, roleRelArr, params.rpt_ids, stgAudit, stageList);
  873. await this.ctx.helper.recursiveMkdirSync(baseDir + '/app/public/download');
  874. const runnableRst = [];
  875. /*
  876. for (let idx = 0; idx < pageRstArr.length; idx++) {
  877. let roleRel = null;
  878. for (const roleR of roleRelArr) {
  879. if (roleR.rpt_id === params.rpt_ids[idx]) {
  880. roleRel = JSON.parse(roleR.rel_content);
  881. break;
  882. }
  883. }
  884. // console.log('roleRel.rel_content: ' + roleRel.rel_content);
  885. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/testBuiltPageResult.jsp');
  886. if (params.stage_status === 3) {
  887. mergeSignAudit(pageRstArr[idx], roleRel, stgAudit);
  888. mergeSignDate(pageRstArr[idx], params.stage_id, roleRel, stgAudit, stgAuditForOrg, stageList);
  889. }
  890. runnableRst.push(getExcelByPageData(pageRstArr[idx], params.rpt_names[idx], roleRel));
  891. }
  892. /*/
  893. // 1. merge签名相关信息
  894. // const pageBrkSize = 6291456; // 6M, 1024*1024*6
  895. const pageBrkSize = 62914560; // 60M, 1024*1024*6*10
  896. for (let idx = 0; idx < pageRstArr.length; idx++) {
  897. let roleRel = null;
  898. for (const roleR of roleRelArr) {
  899. if (roleR.rpt_id === params.rpt_ids[idx]) {
  900. roleRel = JSON.parse(roleR.rel_content);
  901. break;
  902. }
  903. }
  904. if (params.stage_status === 3) {
  905. mergeSignAudit(pageRstArr[idx], roleRel, stgAudit);
  906. mergeSignDate(pageRstArr[idx], params.stage_id, roleRel, stgAudit, stgAuditForOrg, stageList);
  907. }
  908. // 2. 判断草图大小是否需要分页
  909. const picSize = await _chkRawPicSize(pageRstArr[idx], baseDir);
  910. // const picSize = 262914560; // 测试用,强制分页
  911. if (picSize > pageBrkSize) {
  912. // let pageBrkAmt = Math.ceil(picSize / pageBrkSize);
  913. let pageBrkAmt = Math.floor(pageRstArr[idx].items.length / Math.ceil(picSize / pageBrkSize));
  914. if (pageBrkAmt < 10) {
  915. // 不用管
  916. } else if (pageBrkAmt < 100) {
  917. pageBrkAmt = Math.floor(pageBrkAmt / 10) * 10;
  918. } else if (pageBrkAmt < 1000) {
  919. pageBrkAmt = Math.floor(pageBrkAmt / 100) * 100;
  920. } else {
  921. // 暂时不管
  922. }
  923. const newRptNames = []; // 这个为导出控制分页用
  924. const newPageDataRst = _resetPageDataByBreaks([pageRstArr[idx]], pageBrkAmt, [params.rpt_names[idx]], newRptNames);
  925. for (let dtlIdx = 0; dtlIdx < newPageDataRst.length; dtlIdx++) {
  926. runnableRst.push(getExcelByPageData(newPageDataRst[dtlIdx], newRptNames[dtlIdx], roleRel));
  927. }
  928. } else {
  929. runnableRst.push(getExcelByPageData(pageRstArr[idx], params.rpt_names[idx], roleRel));
  930. }
  931. }
  932. // */
  933. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/计量导出pageArr.js');
  934. const uuidRst = await Promise.all(runnableRst);
  935. ctx.body = { data: uuidRst, waterMarkStr };
  936. ctx.status = 201;
  937. }
  938. _getExcelByPageData(pageRst, rpt_name, pageSize, baseDir, innerRoleRel) {
  939. return new Promise(function(resolve, reject) {
  940. rpt_xl_util.exportExcel(pageRst, pageSize, rpt_name, true, null, null, baseDir, innerRoleRel,
  941. (err, uuidName) => {
  942. if (err) return reject(err);
  943. const fileRst = { uuid: uuidName, reportName: rpt_name.replace(new RegExp('%', 'gm'), '_') };
  944. resolve(fileRst);
  945. }
  946. );
  947. });
  948. }
  949. async createExcelFilesFromDirectData(ctx) {
  950. const params = JSON.parse(ctx.request.body.params);
  951. const names = params.rptNames;
  952. const pageDataArr = params.pageData;
  953. const runnableRst = [];
  954. for (let idx = 0; idx < names.length; idx++) {
  955. runnableRst.push(this._getExcelByPageData(pageDataArr[idx], names[idx], 'A4', this.app.baseDir, null));
  956. }
  957. const uuidRst = await Promise.all(runnableRst);
  958. ctx.body = { data: uuidRst, waterMarkStr: null };
  959. ctx.status = 201;
  960. }
  961. async createExcelFilesFromLocal(ctx) {
  962. const baseDir = this.app.baseDir;
  963. function getExcelByPageData(pageRst, rpt_name, innerRoleRel) {
  964. return new Promise(function(resolve, reject) {
  965. rpt_xl_util.exportExcel(pageRst, 'A4', rpt_name, true, null, null, baseDir, innerRoleRel,
  966. (err, uuidName) => {
  967. if (err) return reject(err);
  968. const fileRst = { uuid: uuidName, reportName: rpt_name.replace(new RegExp('%', 'gm'), '_') };
  969. resolve(fileRst);
  970. }
  971. );
  972. });
  973. }
  974. try {
  975. console.log('start:');
  976. const pageRst = JSON.parse(fs.readFileSync('D:/GitHome/temp/计量excel原始数据_A3BZ6_5384_112表(清单).js', 'utf-8'));
  977. console.log('read it');
  978. const runnableRst = [];
  979. const pageBrkSize = 6291456; // 6M, 1024*1024*6
  980. // const pageBrkSize = 1048576; // 1M, 1024*1024*1
  981. console.log('pageBrkSize:' + pageBrkSize);
  982. const picSize = await _chkRawPicSize(pageRst, baseDir);
  983. console.log('picSize:' + picSize);
  984. if (picSize > pageBrkSize) {
  985. let pageBrkAmt = Math.floor(pageRst.items.length / Math.ceil(picSize / pageBrkSize));
  986. console.log('pageBrkAmt:' + pageBrkAmt);
  987. if (pageBrkAmt < 10) {
  988. // 不用管
  989. } else if (pageBrkAmt < 100) {
  990. pageBrkAmt = Math.floor(pageBrkAmt / 10) * 10;
  991. } else if (pageBrkAmt < 1000) {
  992. pageBrkAmt = Math.floor(pageBrkAmt / 100) * 100;
  993. } else {
  994. // 暂时不管
  995. }
  996. const newRptNames = []; // 这个为导出控制分页用
  997. const newPageDataRst = _resetPageDataByBreaks([pageRst], pageBrkAmt, ['工程计量表(计量单元-清单)'], newRptNames);
  998. for (let dtlIdx = 0; dtlIdx < newPageDataRst.length; dtlIdx++) {
  999. runnableRst.push(getExcelByPageData(newPageDataRst[dtlIdx], newRptNames[dtlIdx], null));
  1000. }
  1001. } else {
  1002. runnableRst.push(getExcelByPageData(pageRst, '工程计量表(计量单元-清单)', null));
  1003. }
  1004. const uuidRst = await Promise.all(runnableRst);
  1005. ctx.body = { data: uuidRst, waterMarkStr: null };
  1006. ctx.status = 201;
  1007. } catch (ex) {
  1008. console.log(ex.toString());
  1009. ctx.status = 500;
  1010. }
  1011. }
  1012. async createExcelFilesInOneBookEx(ctx) {
  1013. const params = JSON.parse(ctx.request.body.params);
  1014. const baseDir = this.app.baseDir;
  1015. function getExcelByPageData(pageRstArr, rpt_name, innerRoleRel) {
  1016. return new Promise(function(resolve, reject) {
  1017. rpt_xl_util.exportExcelInOneBook(pageRstArr, params.pageSize, rpt_name, baseDir, innerRoleRel,
  1018. (err, uuidName) => {
  1019. if (err) return reject(err);
  1020. const fileRst = { uuid: uuidName, reportName: rpt_name };
  1021. resolve(fileRst);
  1022. }
  1023. );
  1024. });
  1025. }
  1026. const roleRelArr = (params.stage_status === 3) ? (await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_ids, params.stage_id)) : [];
  1027. // console.log(`baseDir: ${baseDir}`);
  1028. // fsUtil.writeObjToFile(roleRelArr, 'D:/GitHome/temp/签名信息.js');
  1029. const stgAudit = await ctx.service.stageAudit.getStageAudit(params.stage_id, params.stage_times);
  1030. const stgAuditForOrg = await ctx.service.stageAudit.getStageAudit(params.stage_id, 1);
  1031. const stageList = await ctx.service.stage.getValidStagesShort(params.tender_id);
  1032. // const pageRstArr = await getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir);
  1033. const pageRstArr = await ctx.service.jpcReport.getMultiRptsCommon(ctx, params, JV.OUTPUT_TYPE_NORMAL, this.app.baseDir);
  1034. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/pageRstArrForExcel.js');
  1035. for (const pageRst of pageRstArr) {
  1036. for (const page of pageRst.items) {
  1037. page[JV.PROP_WATERMARK_CELLS] = [];
  1038. }
  1039. }
  1040. let waterMarkStr = null;
  1041. if (params.stage_status !== 3 && params.closeWatermark === 0) {
  1042. // if (params.stage_status !== 3 && params.closeWatermark === 0 && params.option === JV.PAGING_OPTION_NORMAL) {
  1043. // 加水印
  1044. fillWaterMark(pageRstArr);
  1045. if (params.needWaterMark) {
  1046. waterMarkStr = await getWatermarkPicData(pageRstArr[0], this.app.baseDir);
  1047. }
  1048. }
  1049. mergeStampSignature(ctx, params.stage_status, pageRstArr, roleRelArr, params.rpt_ids, stgAudit, stageList);
  1050. mergeTextSignature(params.isTextSignature, params.stage_status, pageRstArr, roleRelArr, params.rpt_ids, stgAudit, stageList);
  1051. await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  1052. const runnableRst = [];
  1053. let rptRoleRelArr = [];
  1054. const reAssignSignatureName = function(pageData, roleRel, rpt_name_key) {
  1055. const keyMap = {};
  1056. for (const pageItem of pageData.items) {
  1057. for (const signCell of pageItem[JV.PROP_SIGNATURE_CELLS]) {
  1058. if (!keyMap.hasOwnProperty(signCell.signature_name)) {
  1059. keyMap[signCell.signature_name] = rpt_name_key + '_' + signCell.signature_name;
  1060. }
  1061. }
  1062. }
  1063. for (const pageItem of pageData.items) {
  1064. for (const signCell of pageItem[JV.PROP_SIGNATURE_CELLS]) {
  1065. if (keyMap.hasOwnProperty(signCell.signature_name)) {
  1066. signCell.signature_name = keyMap[signCell.signature_name];
  1067. }
  1068. }
  1069. }
  1070. for (const roleSign of roleRel) {
  1071. if (keyMap.hasOwnProperty(roleSign.signature_name)) {
  1072. roleSign.signature_name = keyMap[roleSign.signature_name];
  1073. }
  1074. }
  1075. // console.log('keyMap of : ' + rpt_name_key);
  1076. // console.log(keyMap);
  1077. };
  1078. for (let idx = 0; idx < pageRstArr.length; idx++) {
  1079. let roleRel = null;
  1080. for (const roleR of roleRelArr) {
  1081. if (roleR.rpt_id === params.rpt_ids[idx]) {
  1082. roleRel = JSON.parse(roleR.rel_content);
  1083. if (params.stage_status === 3) {
  1084. mergeSignAudit(pageRstArr[idx], roleRel, stgAudit);
  1085. mergeSignDate(pageRstArr[idx], params.stage_id, roleRel, stgAudit, stgAuditForOrg, stageList);
  1086. }
  1087. // 这里要做些电子签名的signature_name转换,以防重名
  1088. reAssignSignatureName(pageRstArr[idx], roleRel, params.rpt_names[idx]);
  1089. rptRoleRelArr = rptRoleRelArr.concat(roleRel);
  1090. break;
  1091. }
  1092. }
  1093. }
  1094. // fsUtil.writeObjToFile(pageRstArr, 'D:/GitHome/temp/pageRstArrForExcelAfterReAssign.js');
  1095. // fsUtil.writeObjToFile(rptRoleRelArr, 'D:/GitHome/temp/签名信息_新.js');
  1096. runnableRst.push(getExcelByPageData(pageRstArr, params.rptName, rptRoleRelArr));
  1097. const uuidRst = await Promise.all(runnableRst);
  1098. ctx.body = { data: uuidRst, waterMarkStr };
  1099. ctx.status = 201;
  1100. }
  1101. async getFileByUUID(ctx) {
  1102. // console.log('downloading : ' + ctx.params.uuid);
  1103. const uuid = ctx.params.uuid;
  1104. const rptName = ctx.params.rptName;
  1105. const suffix = '.' + ctx.params.suffix;
  1106. try {
  1107. const rptNameURI = encodeURI(rptName);
  1108. const filePath = this.app.baseDir + '/app/public/download/';
  1109. // console.log('filePath: ' + filePath);
  1110. // await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  1111. ctx.set({
  1112. 'Content-Type': 'application/vnd.openxmlformats',
  1113. 'Content-Disposition': 'attachment; filename="' + rptNameURI + suffix + "\"; filename*=utf-8''" + rptNameURI + suffix,
  1114. });
  1115. ctx.body = await fs.readFileSync(filePath + uuid + suffix);
  1116. } catch (e) {
  1117. console.log(e);
  1118. }
  1119. }
  1120. /**
  1121. * 更新用户通用报表显示配置
  1122. *
  1123. * @param {Object} ctx - egg全局context
  1124. * @return {void}
  1125. */
  1126. async updateCustNode(ctx) {
  1127. const params = JSON.parse(ctx.request.body.params);
  1128. // console.log(params);
  1129. const custId = this.ctx.session.sessionUser.accountId;
  1130. const newNodeItems = JSON.stringify(params.nodeItems);
  1131. const rst = await ctx.service.rptTreeNodeCust.updateCustNode(custId, newNodeItems);
  1132. // console.log(rst);
  1133. ctx.body = { data: rst };
  1134. // ctx.body = { data: { msg: 'test the network' } };
  1135. ctx.status = 201;
  1136. }
  1137. async setCustomDefine(ctx) {
  1138. try {
  1139. const data = JSON.parse(ctx.request.body.data);
  1140. await this.ctx.service.rptCustomDefine.saveCustomSelect(data);
  1141. const result = await this._getReport(ctx, data);
  1142. ctx.body = { err: 0, msg: '', data: result };
  1143. } catch (err) {
  1144. ctx.helper.log(err);
  1145. ctx.body = this.ajaxErrorBody(err, '保存数据出错');
  1146. }
  1147. }
  1148. }
  1149. return ReportController;
  1150. };
  1151. function isFileExisted(file) {
  1152. return new Promise(function(resolve, reject) {
  1153. fs.access(file, err => {
  1154. if (err) {
  1155. reject(false);
  1156. } else {
  1157. resolve(true);
  1158. }
  1159. });
  1160. });
  1161. }
  1162. function fillWaterMark(pageRstArray) {
  1163. const orgWaterMarkWidth = 600;
  1164. const orgWaterMarkHeight = 288;
  1165. const createWaterCell = function(area) {
  1166. const w = area[JV.PROP_RIGHT] - area[JV.PROP_LEFT];
  1167. const h = area[JV.PROP_BOTTOM] - area[JV.PROP_TOP];
  1168. const left = Math.round(area[JV.PROP_LEFT] + w / 2 - orgWaterMarkWidth / 2);
  1169. const right = left + orgWaterMarkWidth;
  1170. const top = Math.round(area[JV.PROP_TOP] + h / 2 - orgWaterMarkHeight / 2);
  1171. const bottom = top + orgWaterMarkHeight;
  1172. return {
  1173. signature_name: JV.SIGNATURE_NAME_DUMMY_WATER_MARK,
  1174. path: '/public/images/not_Approve.png',
  1175. sign_path: '/public/images/not_Approve.png',
  1176. pic: null,
  1177. control: 'Title',
  1178. style: 'Default_None',
  1179. area: {
  1180. Left: left,
  1181. Right: right,
  1182. Top: top,
  1183. Bottom: bottom,
  1184. },
  1185. };
  1186. };
  1187. for (const pageRst of pageRstArray) {
  1188. for (const page of pageRst.items) {
  1189. page[JV.PROP_WATERMARK_CELLS] = [];
  1190. if (page[JV.PROP_PAGE_MERGE_BORDER]) {
  1191. const wmCell = createWaterCell(page[JV.PROP_PAGE_MERGE_BORDER]);
  1192. page[JV.PROP_WATERMARK_CELLS].push(wmCell);
  1193. } else if (page[JV.PAGE_SPECIAL_MERGE_POS]) {
  1194. if (page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_LEFT]) {
  1195. for (let i = 0; i < page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_LEFT].length; i++) {
  1196. const area = {};
  1197. area[JV.PROP_LEFT] = page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_LEFT][i];
  1198. area[JV.PROP_RIGHT] = page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_RIGHT][i];
  1199. area[JV.PROP_TOP] = page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_TOP][i];
  1200. area[JV.PROP_BOTTOM] = page[JV.PAGE_SPECIAL_MERGE_POS][JV.PROP_BOTTOM][i];
  1201. const wmCell = createWaterCell(area);
  1202. page[JV.PROP_WATERMARK_CELLS].push(wmCell);
  1203. }
  1204. }
  1205. }
  1206. }
  1207. }
  1208. }
  1209. async function encodeSignatureDataUri(roleRel, baseDir) {
  1210. if (roleRel) {
  1211. for (const singleRoleRel of roleRel) {
  1212. if (singleRoleRel.rel_content !== null && singleRoleRel.rel_content !== undefined && singleRoleRel.rel_content !== '') {
  1213. const roleRelContent = JSON.parse(singleRoleRel.rel_content);
  1214. for (const role of roleRelContent) {
  1215. // console.log(role);
  1216. if (role.sign_path !== '') {
  1217. const filePath = baseDir + '/app' + role.sign_path;
  1218. try {
  1219. const res = await isFileExisted(filePath);
  1220. if (res) {
  1221. const bData = fs.readFileSync(filePath);
  1222. const base64Str = bData.toString('base64');
  1223. const datauri = 'data:image/png;base64,' + base64Str;
  1224. role.sign_pic = datauri;
  1225. } else {
  1226. console.log('文件不存在:' + filePath);
  1227. }
  1228. } catch (err) {
  1229. console.error(err);
  1230. }
  1231. }
  1232. }
  1233. singleRoleRel.rel_content = JSON.stringify(roleRelContent);
  1234. } else {
  1235. singleRoleRel.rel_content = [];
  1236. }
  1237. }
  1238. }
  1239. }
  1240. async function encodeDummySignatureDataUri(pageRst, baseDir) {
  1241. if (pageRst) {
  1242. for (const page of pageRst.items) {
  1243. for (const signature of page[JV.PROP_SIGNATURE_CELLS]) {
  1244. if (signature.signature_name === JV.SIGNATURE_NAME_DUMMY) {
  1245. if (signature.path !== '') {
  1246. const filePath = baseDir + '/app' + signature.path;
  1247. try {
  1248. const res = await isFileExisted(filePath);
  1249. if (res) {
  1250. const bData = fs.readFileSync(filePath);
  1251. const base64Str = bData.toString('base64');
  1252. const datauri = 'data:image/png;base64,' + base64Str;
  1253. signature.pic = datauri;
  1254. signature.path = '';
  1255. } else {
  1256. console.log('文件不存在:' + filePath);
  1257. }
  1258. } catch (err) {
  1259. console.error(err);
  1260. }
  1261. }
  1262. }
  1263. }
  1264. // 考虑优化,水印只需要一张即可,不需要每页都设置
  1265. // for (const signature of page[JV.PROP_WATERMARK_CELLS]) {
  1266. // if (signature.path !== '') {
  1267. // const filePath = baseDir + '/app' + signature.path;
  1268. // try {
  1269. // const res = await isFileExisted(filePath);
  1270. // if (res) {
  1271. // const bData = fs.readFileSync(filePath);
  1272. // const base64Str = bData.toString('base64');
  1273. // const datauri = 'data:image/png;base64,' + base64Str;
  1274. // signature.pic = datauri;
  1275. // signature.path = '';
  1276. // } else {
  1277. // console.log('文件不存在:' + filePath);
  1278. // }
  1279. // } catch (err) {
  1280. // console.error(err);
  1281. // }
  1282. // }
  1283. // }
  1284. }
  1285. }
  1286. }
  1287. async function getWatermarkPicData(pageRst, baseDir) {
  1288. let rst = '';
  1289. if (pageRst) {
  1290. for (const page of pageRst.items) {
  1291. for (const signature of page[JV.PROP_WATERMARK_CELLS]) {
  1292. if (signature.path !== '') {
  1293. const filePath = baseDir + '/app' + signature.path;
  1294. try {
  1295. const res = await isFileExisted(filePath);
  1296. if (res) {
  1297. const bData = fs.readFileSync(filePath);
  1298. const base64Str = bData.toString('base64');
  1299. rst = 'data:image/png;base64,' + base64Str;
  1300. break;
  1301. } else {
  1302. console.log('文件不存在:' + filePath);
  1303. }
  1304. } catch (err) {
  1305. console.error(err);
  1306. }
  1307. }
  1308. if (rst !== null) break;
  1309. }
  1310. }
  1311. }
  1312. return rst;
  1313. }
  1314. function mergeTextSignature(isTxtSignature, status, pageData, singleRoleRel, rpt_ids, stgAudit, stageList) {
  1315. // 依据需求,有些用户不需要签名图片,那么从源头上把签名从signature_cells转移到cells下,省得后续 预览、打印、导出PDF、导出EXCEL每个地方都改动
  1316. const _mergeSingle = function(_page, _roleRelList, isDeleteSignCell, checkAudit = false) {
  1317. // console.log(_roleRelList);
  1318. let roleRelContent = [];
  1319. if (_roleRelList && _roleRelList.rel_content !== null && _roleRelList.rel_content !== undefined && _roleRelList.rel_content !== '') {
  1320. roleRelContent = JSON.parse(_roleRelList.rel_content);
  1321. }
  1322. for (const page of _page.items) {
  1323. if (page.signature_cells) {
  1324. const deleteSCellsIdx = [];
  1325. for (let scIdx = 0; scIdx < page.signature_cells.length; scIdx++) {
  1326. const sCell = page.signature_cells[scIdx];
  1327. if (sCell.signature_name !== JV.SIGNATURE_NAME_DUMMY) {
  1328. sCell.Value = ''; // 这里要先清除原有信息
  1329. sCell.path = '';
  1330. sCell.pic = '';
  1331. let needTransferText = true;
  1332. for (const role_rel of roleRelContent) {
  1333. if (sCell.signature_name === role_rel.signature_name) {
  1334. if (!checkAudit || _chkIfAudit(role_rel, stgAudit, stageList)) {
  1335. sCell.Value = role_rel.user_name; // 只有审核通过了才需要文本签名内容
  1336. if (role_rel.sign_output && role_rel.sign_output.indexOf(NORMAL_SIGN_STR) < 0) {
  1337. // 用户选择不签名,则不迁移
  1338. needTransferText = false;
  1339. }
  1340. break;
  1341. }
  1342. }
  1343. }
  1344. if (needTransferText) {
  1345. const newCell = {
  1346. font: 'Footer',
  1347. control: sCell.control,
  1348. style: sCell.style,
  1349. Value: sCell.Value,
  1350. area: { Left: sCell.area.Left, Right: sCell.area.Right, Top: sCell.area.Top, Bottom: sCell.area.Bottom },
  1351. };
  1352. page.cells.push(newCell); // 迁移
  1353. deleteSCellsIdx.push(scIdx);
  1354. }
  1355. }
  1356. }
  1357. // 删除 page.signature_cells 签名(草图不能删);
  1358. // console.log('isDeleteSignCell: ' + isDeleteSignCell);
  1359. // console.log(deleteSCellsIdx);
  1360. if (isDeleteSignCell) {
  1361. for (let dIdx = deleteSCellsIdx.length - 1; dIdx >= 0; dIdx--) {
  1362. page.signature_cells.splice(deleteSCellsIdx[dIdx], 1);
  1363. }
  1364. }
  1365. }
  1366. }
  1367. };
  1368. if (isTxtSignature) {
  1369. if (pageData instanceof Array) {
  1370. pageData.forEach((dtlPage, index) => {
  1371. let dftRolRel = [];
  1372. const rpt_id = rpt_ids[index];
  1373. for (const roleRel of singleRoleRel) {
  1374. if (roleRel.rpt_id === rpt_id) {
  1375. dftRolRel = roleRel;
  1376. }
  1377. }
  1378. // console.log(dftRolRel);
  1379. _mergeSingle(dtlPage, dftRolRel, true, status !== 3);
  1380. });
  1381. } else {
  1382. _mergeSingle(pageData, singleRoleRel, false, status !== 3);
  1383. }
  1384. }
  1385. }
  1386. function _chkIfAudit(role_rel, STAGE_AUDIT, STAGE_LIST) {
  1387. let rst = false;
  1388. for (const stg_audit of STAGE_AUDIT) {
  1389. if (role_rel.acc_id === stg_audit.aid) {
  1390. if (stg_audit.status === 3) {
  1391. rst = true;
  1392. } else {
  1393. rst = false;
  1394. }
  1395. // break; //因为实际业务中会有反复,所以就不break了,一直判断,以最后一个为准
  1396. }
  1397. }
  1398. if (!rst) {
  1399. // 还有原报需要检测
  1400. for (const stg_ort_rpt of STAGE_LIST) {
  1401. if (role_rel.acc_id === stg_ort_rpt.user_id) {
  1402. if (stg_ort_rpt.status !== 1 && stg_ort_rpt.status !== 4) {
  1403. // 只要不是待上报(1)、退回审批(4) 状态,都需要显示
  1404. rst = true;
  1405. } else {
  1406. rst = false;
  1407. }
  1408. // break; //因为实际业务中会有反复,所以就不break了,一直判断,以最后一个为准
  1409. }
  1410. }
  1411. }
  1412. // rst = true; //测试,未来将屏蔽
  1413. return rst;
  1414. }
  1415. // 因机制问题,此方法必须在mergeTextSignature之前调用
  1416. async function mergeStampSignature(ctx, status, pageData, singleRoleRel, rpt_ids, stgAudit, stageList) {
  1417. const dupPicPaths = [];
  1418. const _getMaxRect = function(page) {
  1419. const rect = [100000000, 100000000, 0, 0];
  1420. for (const cell of page.cells) {
  1421. if (rect[0] > cell.area.Left) {
  1422. rect[0] = cell.area.Left;
  1423. }
  1424. if (rect[1] > cell.area.Top) {
  1425. rect[1] = cell.area.Top;
  1426. }
  1427. if (rect[2] < cell.area.Right) {
  1428. rect[2] = cell.area.Right;
  1429. }
  1430. if (rect[3] < cell.area.Bottom) {
  1431. rect[3] = cell.area.Bottom;
  1432. }
  1433. }
  1434. return rect;
  1435. };
  1436. const createStampArea = function(orgCell, controls, maxRect) {
  1437. const ctrl = controls[orgCell.control];
  1438. const rst = { Left: orgCell.area.Left, Right: orgCell.area.Right, Top: orgCell.area.Top, Bottom: orgCell.area.Bottom };
  1439. let pLeft = orgCell.area.Left,
  1440. pTop = orgCell.area.Top;
  1441. switch (ctrl[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_HORIZON]]) {
  1442. case JV.OUTPUT_ALIGN.H[JV.H_ALIGN_IDX_LEFT]:
  1443. pLeft = orgCell.area.Left;
  1444. break;
  1445. case JV.OUTPUT_ALIGN.H[JV.H_ALIGN_IDX_CENTER]:
  1446. pLeft = (orgCell.area.Left + orgCell.area.Right - STD_COMP_STAMP_SIZE_WIDTH) / 2;
  1447. break;
  1448. case JV.OUTPUT_ALIGN.H[JV.H_ALIGN_IDX_RIGHT]:
  1449. pLeft = orgCell.area.Right - STD_COMP_STAMP_SIZE_WIDTH;
  1450. break;
  1451. default:break;
  1452. }
  1453. switch (ctrl[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_VERTICAL]]) {
  1454. case JV.OUTPUT_ALIGN.H[JV.V_ALIGN_IDX_TOP]:
  1455. pTop = orgCell.area.Top;
  1456. break;
  1457. case JV.OUTPUT_ALIGN.H[JV.V_ALIGN_IDX_CENTER]:
  1458. pTop = (orgCell.area.Top + orgCell.area.Bottom - STD_COMP_STAMP_SIZE_HEIGHT) / 2;
  1459. break;
  1460. case JV.OUTPUT_ALIGN.H[JV.V_ALIGN_IDX_BOTTOM]:
  1461. pTop = orgCell.area.Bottom - STD_COMP_STAMP_SIZE_HEIGHT;
  1462. break;
  1463. default:break;
  1464. }
  1465. rst.Left = pLeft;
  1466. rst.Top = pTop;
  1467. rst.Right = pLeft + STD_COMP_STAMP_SIZE_WIDTH;
  1468. rst.Bottom = pTop + STD_COMP_STAMP_SIZE_WIDTH;
  1469. // 最后一步,如超过报表范围,则要调整坐标
  1470. // if (rst.Left < maxRect[0]) {
  1471. // const width = maxRect[0] - rst.Left;
  1472. // rst.Left += width;
  1473. // rst.Right += width;
  1474. // }
  1475. // if (rst.Top < maxRect[1]) {
  1476. // const height = maxRect[1] - rst.Top;
  1477. // rst.Top += height;
  1478. // rst.Bottom += height;
  1479. // }
  1480. // if (rst.Right > maxRect[2]) {
  1481. // const width = maxRect[2] - rst.Right; // 负
  1482. // rst.Left += width;
  1483. // rst.Right += width;
  1484. // }
  1485. // if (rst.Bottom > maxRect[3]) {
  1486. // const height = maxRect[3] - rst.Bottom;
  1487. // rst.Top += height;
  1488. // rst.Bottom += height;
  1489. // }
  1490. return rst;
  1491. };
  1492. const _mergeSingleStamp = async function(_page, _roleRelList, checkAudit = false) {
  1493. let roleRelContent = [];
  1494. if (_roleRelList && _roleRelList.rel_content !== null && _roleRelList.rel_content !== undefined && _roleRelList.rel_content !== '') {
  1495. roleRelContent = JSON.parse(_roleRelList.rel_content);
  1496. }
  1497. for (const page of _page.items) {
  1498. const maxRect = _getMaxRect(page);
  1499. if (page.signature_cells) {
  1500. const newStampCells = [];
  1501. for (let scIdx = 0; scIdx < page.signature_cells.length; scIdx++) {
  1502. const sCell = page.signature_cells[scIdx];
  1503. for (const role_rel of roleRelContent) {
  1504. if (sCell.signature_name === role_rel.signature_name) {
  1505. if (!checkAudit || _chkIfAudit(role_rel, stgAudit, stageList)) {
  1506. if (Array.isArray(role_rel.sign_output) && role_rel.sign_output.length > 0) {
  1507. for (const signType of role_rel.sign_output) {
  1508. switch (signType) {
  1509. case COMPANY_SIGN_STR:
  1510. case PRIVATE_SIGN_STR:
  1511. // 创建一个新的cell
  1512. let stampPath = (signType === COMPANY_SIGN_STR) ? role_rel.company_stamp_path : role_rel.private_stamp_path;
  1513. stampPath = ctx.app.config.fujianOssPath + stampPath;
  1514. if (dupPicPaths.indexOf(stampPath) < 0) {
  1515. dupPicPaths.push(stampPath);
  1516. // await _chkRawPicSizeOSS(ctx, stampPath);
  1517. }
  1518. // 保存在签字信息中的签章信息
  1519. let signatureArea;
  1520. if (role_rel.areaData && role_rel.areaData[signType]) {
  1521. signatureArea = role_rel.areaData[signType];
  1522. }
  1523. const newStampCell = {
  1524. signature_name: JV.SIGNATURE_NAME_DUMMY,
  1525. control: sCell.control,
  1526. style: sCell.style,
  1527. path: stampPath,
  1528. isStamp: true,
  1529. maxRect,
  1530. orgArea: sCell.area,
  1531. area: signatureArea || createStampArea(sCell, _page[JV.NODE_CONTROL_COLLECTION], maxRect), // 有签章信息就直接取,没有的话就走原来的逻辑
  1532. signatureName: role_rel.signature_name, // 节点名称
  1533. signType, // 签章信息
  1534. isSaveSignature: !!signatureArea, // 是否有签章位置信息
  1535. };
  1536. newStampCells.push(newStampCell);
  1537. break;
  1538. default: break;
  1539. }
  1540. }
  1541. }
  1542. }
  1543. }
  1544. }
  1545. }
  1546. if (newStampCells.length > 0) {
  1547. page.signature_cells = page.signature_cells.concat(newStampCells);
  1548. }
  1549. }
  1550. }
  1551. };
  1552. if (pageData instanceof Array) {
  1553. pageData.forEach((dtlPage, index) => {
  1554. let dftRolRel = [];
  1555. const rpt_id = rpt_ids[index];
  1556. for (const roleRel of singleRoleRel) {
  1557. if (roleRel.rpt_id === rpt_id) {
  1558. dftRolRel = roleRel;
  1559. }
  1560. }
  1561. _mergeSingleStamp(dtlPage, dftRolRel, status !== 3);
  1562. });
  1563. } else {
  1564. _mergeSingleStamp(pageData, singleRoleRel, status !== 3);
  1565. }
  1566. }
  1567. function mergeSignAudit(pageData, currRoleRelList, currAuditList) {
  1568. for (const page of pageData.items) {
  1569. if (page.signature_audit_cells) {
  1570. for (const sCell of page.signature_audit_cells) {
  1571. sCell.Value = ''; // 这里要先清除原有信息
  1572. if (currRoleRelList && currRoleRelList.length > 0) {
  1573. for (const role_rel of currRoleRelList) {
  1574. if (sCell.signature_name === role_rel.signature_name + '_审核意见') {
  1575. let preDate = '';
  1576. sCell.Value = '同意'; // 只有选择了签名的,才需要初始化一个默认的意见(之前的逻辑在有多个签名,哪怕只选择了一个,其他的意见都会有默认意见)
  1577. if (currAuditList && currAuditList.length > 0) {
  1578. for (const audit_rel of currAuditList) {
  1579. if (role_rel.acc_id === audit_rel.aid) {
  1580. if (audit_rel.end_time > preDate && audit_rel.status === 3) {
  1581. sCell.Value = audit_rel.opinion;
  1582. preDate = audit_rel.end_time;
  1583. }
  1584. // 不能break,实际会有多个审核意见,以最后一个为准
  1585. }
  1586. }
  1587. }
  1588. break;
  1589. }
  1590. }
  1591. }
  1592. }
  1593. }
  1594. }
  1595. }
  1596. function mergeSignDate(pageData, current_stage_id, currRoleRelList, STAGE_AUDIT, STAGE_AUDIT_ORG, STAGE_LIST) {
  1597. if (currRoleRelList && currRoleRelList.length > 0 && STAGE_AUDIT && STAGE_AUDIT.length > 0) {
  1598. for (let rridx = 0; rridx < currRoleRelList.length; rridx++) {
  1599. const role_rel = currRoleRelList[rridx];
  1600. if (role_rel.sign_date === undefined || role_rel.sign_date === null || role_rel.sign_date === '') {
  1601. const dftDate = _getSignDateByAllScenarios(role_rel.acc_id, current_stage_id, STAGE_AUDIT, STAGE_AUDIT_ORG, STAGE_LIST);
  1602. role_rel.sign_date = dftDate;
  1603. }
  1604. }
  1605. }
  1606. if (currRoleRelList && currRoleRelList.length > 0) {
  1607. for (const page of pageData.items) {
  1608. if (page.signature_date_cells) {
  1609. for (const sCell of page.signature_date_cells) {
  1610. sCell.Value = _getSignDateDftName();
  1611. for (const role_rel of currRoleRelList) {
  1612. if (sCell.signature_name === role_rel.signature_name + '_签字日期') {
  1613. if (role_rel.sign_date !== '') {
  1614. if (typeof role_rel.sign_date === 'string') {
  1615. role_rel.sign_date = new Date(role_rel.sign_date);
  1616. }
  1617. sCell.Value = role_rel.sign_date.Format(role_rel.sign_date_format);
  1618. }
  1619. break;
  1620. }
  1621. }
  1622. }
  1623. }
  1624. }
  1625. }
  1626. }
  1627. function _getSignDateByAllScenarios(userAccId, current_stage_id, STAGE_AUDIT, STAGE_AUDIT_ORG, STAGE_LIST) {
  1628. let rst = '';
  1629. let hasAudit = false;
  1630. for (const stg_audit of STAGE_AUDIT) {
  1631. if (stg_audit.aid === userAccId) {
  1632. hasAudit = true;
  1633. if (stg_audit.status === 3) {
  1634. rst = stg_audit.end_time;
  1635. } else {
  1636. rst = '';
  1637. }
  1638. // break; //因为实际业务中会有反复,所以就不break了,一直判断,以最后一个为准
  1639. }
  1640. }
  1641. let isOrgRpt = false;
  1642. for (const stg of STAGE_LIST) {
  1643. if (stg.id === current_stage_id) {
  1644. if (stg.user_id === userAccId) {
  1645. isOrgRpt = true;
  1646. }
  1647. break;
  1648. }
  1649. }
  1650. if (isOrgRpt && !hasAudit && STAGE_AUDIT_ORG && STAGE_AUDIT_ORG.length > 0) {
  1651. if (STAGE_AUDIT_ORG[0].begin_time && STAGE_AUDIT_ORG[0].begin_time !== '' && STAGE_AUDIT_ORG[0].begin_time.length > 20) {
  1652. rst = STAGE_AUDIT_ORG[0].begin_time;
  1653. }
  1654. }
  1655. if (!isOrgRpt && !hasAudit) {
  1656. // 非审批流程人员以及非原报,则显示期截至时间
  1657. for (const stg of STAGE_LIST) {
  1658. if (stg.id === current_stage_id && stg.period) {
  1659. const period = stg.period.split(' ~ ');
  1660. if (period.length === 2) {
  1661. rst = period[1];
  1662. }
  1663. }
  1664. }
  1665. }
  1666. return rst;
  1667. }
  1668. function _getSignDateDftName() {
  1669. return ' 年 月 日';
  1670. }
  1671. async function _chkRawPicSize(pageData, baseDir) {
  1672. let rst = 0;
  1673. const picKeys = [];
  1674. for (let pi = 0; pi < pageData.items.length; pi++) {
  1675. // 每页判断
  1676. for (let ci = 0; ci < pageData.items[pi][JV.PROP_SIGNATURE_CELLS].length; ci++) {
  1677. const picPath = pageData.items[pi][JV.PROP_SIGNATURE_CELLS][ci].path;
  1678. // if (picPath && picPath.indexOf('/sign') < 0 && picKeys.indexOf(picPath) < 0) {
  1679. if (picPath && picPath !== '' && picKeys.indexOf(picPath) < 0) {
  1680. // 签名图片也要计算在内,反正不重复
  1681. picKeys.push(picPath);
  1682. }
  1683. // signature_cells PROP_SIGNATURE_CELLS
  1684. }
  1685. }
  1686. // console.log(picKeys);
  1687. for (const pKey of picKeys) {
  1688. const filePath = `${baseDir}/app${pKey}`;
  1689. console.log('filePath: ' + filePath);
  1690. let isExist = false;
  1691. try {
  1692. isExist = await isFileExisted(filePath);
  1693. } catch (ex) {
  1694. isExist = false;
  1695. }
  1696. console.log('isExist: ' + isExist);
  1697. if (isExist) {
  1698. const obj = fs.statSync(filePath);
  1699. if (obj) {
  1700. rst += obj.size;
  1701. } else {
  1702. console.log('no statSync resutl: ' + filePath);
  1703. }
  1704. // rst += fs.statSync(filePath).size;
  1705. } else {
  1706. console.log(`${pKey} not exists!`);
  1707. }
  1708. }
  1709. return rst;
  1710. }
  1711. async function _chkRawPicSizeOSS(ctx, picPath) {
  1712. // 计量的草图、签章都放在OSS服务器上,需要另外的方式来获取,考虑到效率问题,机制上得有所变化
  1713. const rst = [0, 0];
  1714. try {
  1715. const pf = await ctx.app.signPdfOss.get(picPath);
  1716. console.log('get pic successfully!');
  1717. console.log(pf);
  1718. } catch (ex) {
  1719. console.log(ex);
  1720. } finally {
  1721. console.log('finally!');
  1722. }
  1723. return rst;
  1724. }
  1725. function _resetPageDataByBreaks(pageDataArr, breakAmt, rpt_names, newRptNames) {
  1726. const rst = [];
  1727. for (let pi = 0; pi < pageDataArr.length; pi++) {
  1728. const pageItem = pageDataArr[pi];
  1729. const currentRptName = rpt_names[pi];
  1730. if (pageItem.items.length > breakAmt) {
  1731. let pa = Math.floor(pageItem.items.length / breakAmt);
  1732. if (pageItem.items.length % breakAmt > 0) {
  1733. pa++;
  1734. }
  1735. for (let idx = 0; idx < pa; idx++) {
  1736. const newPageDataObj = { items: [] };
  1737. // newPageDataObj[JV.NODE_PAGE_INFO] = JSON.parse(JSON.stringify(pageItem[JV.NODE_PAGE_INFO]));
  1738. newPageDataObj[JV.NODE_PAGE_INFO] = pageItem[JV.NODE_PAGE_INFO];
  1739. // newPageDataObj[JV.BAND_PROP_MERGE_BAND] = JSON.parse(JSON.stringify(pageItem[JV.BAND_PROP_MERGE_BAND]));
  1740. newPageDataObj[JV.BAND_PROP_MERGE_BAND] = pageItem[JV.BAND_PROP_MERGE_BAND];
  1741. newPageDataObj[JV.NODE_FONT_COLLECTION] = pageItem[JV.NODE_FONT_COLLECTION];
  1742. newPageDataObj[JV.NODE_STYLE_COLLECTION] = pageItem[JV.NODE_STYLE_COLLECTION];
  1743. newPageDataObj[JV.NODE_CONTROL_COLLECTION] = pageItem[JV.NODE_CONTROL_COLLECTION];
  1744. for (let dIdx = idx * breakAmt; dIdx < (Math.min((idx + 1) * breakAmt, pageItem.items.length)); dIdx++) {
  1745. newPageDataObj.items.push(pageItem.items[dIdx]);
  1746. }
  1747. rst.push(newPageDataObj);
  1748. newRptNames.push(currentRptName + '_' + (idx + 1));
  1749. }
  1750. } else {
  1751. rst.push(pageItem);
  1752. newRptNames.push(currentRptName);
  1753. }
  1754. }
  1755. return rst;
  1756. }