report_controller.js 83 KB

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