report_controller.js 82 KB

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