report_controller.js 83 KB

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