report_controller.js 91 KB

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