report_controller.js 83 KB

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