report_controller.js 81 KB

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