report_controller.js 83 KB

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