report_archive_controller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. 'use strict';
  2. /**
  3. * Created by Tony on 2021/3/31.
  4. */
  5. const path = require('path');
  6. const uuidV1 = require('uuid').v1;
  7. const fs = require('fs');
  8. const MAX_ARCHIVE = 3;
  9. const tenderMenu = require('../../config/menu').tenderMenu;
  10. const measureType = require('../const/tender').measureType;
  11. const fsUtil = require('../public/js/fsUtil');
  12. const auditConst = require('../const/audit');
  13. module.exports = app => {
  14. class ReportArchiveController extends app.BaseController {
  15. async index(ctx) {
  16. const tender = ctx.tender;
  17. const stage = ctx.stage;
  18. let stage_id = -1;
  19. let stage_order = -1;
  20. let stage_times = -1;
  21. let stage_status = -1;
  22. const treeNodes = await ctx.service.rptTreeNode.getNodesByProjectId([-1, tender.data.project_id]);
  23. const custTreeNodes = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(this.ctx.session.sessionUser.accountId);
  24. const stageList = await ctx.service.stage.getValidStagesShort(tender.id);
  25. //
  26. // 。。。
  27. let archiveList = [];
  28. let archiveEncryptionList = [];
  29. // console.log('tender.data.project_id: ' + tender.data.project_id);
  30. if (stage) {
  31. // console.log('ctx.stage.id: ' + ctx.stage.id);
  32. const archives = await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, ctx.stage.id);
  33. const archiveEncryptions = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(tender.data.project_id, ctx.stage.id);
  34. stage_id = stage.id;
  35. stage_order = stage.order;
  36. stage_times = stage.times;
  37. stage_status = stage.status;
  38. if (archives.length > 0) {
  39. archiveList = JSON.parse(archives[0].content);
  40. }
  41. if (archiveEncryptions.length > 0) {
  42. archiveEncryptionList = JSON.parse(archiveEncryptions[0].content);
  43. }
  44. } else {
  45. // console.log('stageList[0].id: ' + stageList[0].id);
  46. const archives = await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, stageList[stageList.length - 1].id);
  47. const archiveEncryptions = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(tender.data.project_id, stageList[stageList.length - 1].id);
  48. // stage_id = stageList[0].id;
  49. // stage_order = stageList[0].order;
  50. // stage_times = stageList[0].times;
  51. // stage_status = stageList[0].status;
  52. if (archives && archives.length > 0) {
  53. archiveList = JSON.parse(archives[0].content);
  54. }
  55. if (archiveEncryptions && archiveEncryptions.length > 0) {
  56. archiveEncryptionList = JSON.parse(archiveEncryptions[0].content);
  57. }
  58. }
  59. let rpt_tpl_items = '{ customize: [], common: [] }';
  60. if (custTreeNodes.length > 0) {
  61. rpt_tpl_items = custTreeNodes[0].rpt_tpl_items;
  62. }
  63. //
  64. const renderData = {
  65. tender: tender.data,
  66. rpt_tpl_data: JSON.stringify(treeNodes),
  67. cust_tpl_data: rpt_tpl_items,
  68. project_id: tender.data.project_id,
  69. tender_id: tender.id,
  70. stg_id: stage_id,
  71. stg_order: stage_order,
  72. stg_times: stage_times,
  73. stg_status: stage_status,
  74. stage_list: JSON.stringify(stageList),
  75. tenderMenu,
  76. measureType,
  77. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.report.main),
  78. stages: stageList,
  79. auditConst: auditConst.stage,
  80. archiveList,
  81. archiveEncryptionList,
  82. can_netcasign: ctx.session.sessionProject.page_show.openNetCaSign === 1,
  83. };
  84. await this.layout('report/index_archive.ejs', renderData, 'report/index_archive_modal.ejs');
  85. }
  86. async getReportArchive(ctx) {
  87. const params = JSON.parse(ctx.request.body.params);
  88. // ctx.body = await this._getReport(ctx, params);
  89. const archives = await ctx.service.rptArchive.getPrjStgArchive(params.prjId, params.stgId);
  90. const archiveEncryptions = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(params.prjId, params.stgId);
  91. let archiveList = [];
  92. let archiveEncryptionList = [];
  93. if (archives.length > 0) {
  94. archiveList = JSON.parse(archives[0].content);
  95. }
  96. if (archiveEncryptions.length > 0) {
  97. archiveEncryptionList = JSON.parse(archiveEncryptions[0].content);
  98. }
  99. ctx.body = {
  100. data: archiveList,
  101. encryptionData: archiveEncryptionList,
  102. };
  103. }
  104. async addReportArchiveEncryption(ctx) {
  105. const params = JSON.parse(ctx.request.body.params);
  106. const prjId = params.prjId;
  107. const stgId = params.stgId;
  108. const rptId = params.rptId;
  109. const ttlPgs = params.ttlPgs;
  110. const content = params.content;
  111. const orgArchiveList = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(prjId, stgId);
  112. if (orgArchiveList.length > 0) {
  113. const contentArr = JSON.parse(orgArchiveList[0].content);
  114. let hasArchive = false;
  115. for (const item of contentArr) {
  116. if (item.rpt_id === rptId) {
  117. // 考虑到报表模板的稳定性,只保留一项来记录位置就足够了,都不考虑用uuid了
  118. item.encryption = content;
  119. hasArchive = true;
  120. break;
  121. }
  122. }
  123. if (!hasArchive) {
  124. // 表示有新的要加
  125. contentArr.push({ rpt_id: rptId, total_page: ttlPgs, encryption: content });
  126. } else {
  127. //
  128. }
  129. const updatedRst = await ctx.service.rptArchiveEncryption.updateArchiveEncryption(orgArchiveList[0].id, prjId, stgId, contentArr);
  130. // console.log(updatedRst);
  131. ctx.body = { err: 0, msg: '', data: { addedRst: contentArr } };
  132. } else {
  133. // 需要增加
  134. const archiveArr = [];
  135. archiveArr.push({ rpt_id: rptId, encryption: content });
  136. const addedRst = await ctx.service.rptArchiveEncryption.createArchiveEncryption(prjId, stgId, archiveArr);
  137. // console.log(addedRst);
  138. ctx.body = { err: 0, msg: '', data: { addedRst: archiveArr } };
  139. }
  140. }
  141. async addReportArchive(ctx) {
  142. try {
  143. const stream = await ctx.getFileStream();
  144. const prjId = ctx.params.prjId;
  145. const stgId = ctx.params.stgId;
  146. const rptId = ctx.params.rptId;
  147. const newName = uuidV1();
  148. const fileName = newName + '.PDF';
  149. // console.log('adding fileName: ' + fileName);
  150. // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', 'public/archive', fileName));
  151. const oss_result = await ctx.oss.put('archive/' + fileName, stream);
  152. if (!(oss_result && oss_result.url && oss_result.res.status === 200)) {
  153. throw '上传文件失败';
  154. }
  155. // const url = await ctx.oss.delete('archive/52d3e7f0-c7fb-11eb-b8c2-51b890b95d23.PDF');
  156. // console.log(url);
  157. // const flag = true;
  158. // if (flag) {
  159. // throw 'ok';
  160. // }
  161. const updateDate = new Date();
  162. const montStr = (updateDate.getMonth() + 1) < 10 ? ('0' + (updateDate.getMonth() + 1)) : ((updateDate.getMonth() + 1));
  163. const dateStr = (updateDate.getDate()) < 10 ? ('0' + (updateDate.getDate())) : ((updateDate.getDate()));
  164. const dtStr = updateDate.getFullYear() + '-' + montStr + '-' + dateStr;
  165. const orgArchiveList = await ctx.service.rptArchive.getPrjStgArchive(prjId, stgId);
  166. if (orgArchiveList.length > 0) {
  167. const contentArr = JSON.parse(orgArchiveList[0].content);
  168. let hasArchive = false;
  169. for (const item of contentArr) {
  170. if (item.rpt_id === rptId) {
  171. hasArchive = true;
  172. if (item.items.length >= MAX_ARCHIVE) {
  173. // 超出界限,需要删除时间最旧的那个
  174. let rmIdx = 0;
  175. for (let idx = 1; idx < item.items.length; idx++) {
  176. if (item.items[rmIdx].updateDate_time < item.items[idx].updateDate_time) {
  177. rmIdx = idx;
  178. }
  179. }
  180. // 同时删除oss文件
  181. await ctx.oss.delete('archive/' + item.items[rmIdx].uuid + '.PDF');
  182. item.items.splice(rmIdx, 1);
  183. }
  184. const newItem = { uuid: newName, updateDate_time: dtStr };
  185. item.items.push(newItem);
  186. break;
  187. }
  188. }
  189. if (!hasArchive) {
  190. // 表示有新的模板需要添加
  191. contentArr.push({ rpt_id: rptId, items: [{ uuid: newName, updateDate_time: dtStr }] });
  192. }
  193. const updatedRst = await ctx.service.rptArchive.updateArchive(orgArchiveList[0].id, prjId, stgId, contentArr);
  194. // console.log(updatedRst);
  195. ctx.body = { err: 0, msg: '', data: { fileName, updateDate, addedRst: contentArr } };
  196. } else {
  197. // 需要增加
  198. const archiveArr = [];
  199. archiveArr.push({ rpt_id: rptId, items: [{ uuid: newName, updateDate_time: dtStr }] });
  200. const addedRst = await ctx.service.rptArchive.createArchive(prjId, stgId, archiveArr);
  201. // console.log(addedRst);
  202. ctx.body = { err: 0, msg: '', data: { fileName, updateDate, addedRst: archiveArr } };
  203. }
  204. } catch (err) {
  205. this.log(err);
  206. ctx.body = { err: 1, msg: err.toString(), data: null };
  207. }
  208. }
  209. async updateReportArchiveEncryption(ctx) {
  210. // 在add方法中已经处理
  211. await this.addReportArchiveEncryption(ctx);
  212. }
  213. async updateReportArchive(ctx) {
  214. try {
  215. const stream = await ctx.getFileStream();
  216. const prjId = ctx.params.prjId;
  217. const stgId = ctx.params.stgId;
  218. const rptId = ctx.params.rptId;
  219. const orgName = ctx.params.orgName;
  220. const fileName = orgName + '.PDF';
  221. console.log('updating fileName: ' + fileName);
  222. // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', 'public/archive', fileName));
  223. const oss_result = await ctx.oss.put('archive/' + fileName, stream);
  224. if (!(oss_result && oss_result.url && oss_result.res.status === 200)) {
  225. throw '上传文件失败';
  226. }
  227. const updateDate = new Date();
  228. const montStr = (updateDate.getMonth() + 1) < 10 ? ('0' + (updateDate.getMonth() + 1)) : ((updateDate.getMonth() + 1));
  229. const dateStr = (updateDate.getDate()) < 10 ? ('0' + (updateDate.getDate())) : ((updateDate.getDate()));
  230. const dtStr = updateDate.getFullYear() + '-' + montStr + '-' + dateStr;
  231. const orgArchiveList = await ctx.service.rptArchive.getPrjStgArchive(prjId, stgId);
  232. if (orgArchiveList.length > 0) {
  233. const contentArr = JSON.parse(orgArchiveList[0].content);
  234. for (const item of contentArr) {
  235. if (item.rpt_id === rptId) {
  236. if (item.items && item.items.length > 0) {
  237. for (const subItem of item.items) {
  238. if (subItem.uuid === orgName) {
  239. subItem.updateDate_time = dtStr;
  240. break;
  241. }
  242. }
  243. } else {
  244. item.items = [{ uuid: orgName, updateDate_time: dtStr }];
  245. }
  246. break;
  247. }
  248. }
  249. const updatedRst = await ctx.service.rptArchive.updateArchive(orgArchiveList[0].id, prjId, stgId, contentArr);
  250. // console.log(updatedRst);
  251. ctx.body = { err: 0, msg: '', data: { fileName, updateDate, updatedRst: contentArr } };
  252. } else {
  253. // 需要增加
  254. const archiveArr = [];
  255. archiveArr.push({ rpt_id: rptId, items: [{ uuid: orgName, updateDate_time: dtStr }] });
  256. const updatedRst = await ctx.service.rptArchive.createArchive(prjId, stgId, archiveArr);
  257. // console.log(updatedRst);
  258. ctx.body = { err: 0, msg: '', data: { fileName, updateDate, updatedRst: archiveArr } };
  259. }
  260. } catch (err) {
  261. this.log(err);
  262. ctx.body = { err: 1, msg: err.toString(), data: null };
  263. }
  264. }
  265. async removeReportArchiveEncryption(ctx) {
  266. try {
  267. const prjId = ctx.params.prjId;
  268. const stgId = ctx.params.stgId;
  269. const rptId = ctx.params.rptId;
  270. const orgArchiveList = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(prjId, stgId);
  271. if (orgArchiveList.length > 0) {
  272. const contentArr = JSON.parse(orgArchiveList[0].content);
  273. for (let idx = 0; idx < contentArr.length; idx++) {
  274. if (contentArr[idx].rpt_id === rptId) {
  275. contentArr.splice(idx, 1);
  276. break;
  277. }
  278. }
  279. const updatedRst = await ctx.service.rptArchive.updateArchive(prjId, stgId, contentArr);
  280. ctx.body = { err: 0, msg: '', data: { updatedRst } };
  281. } else {
  282. ctx.body = { err: 0, msg: '', data: { updatedRst: null } };
  283. }
  284. } catch (err) {
  285. this.log(err);
  286. ctx.body = { err: 1, msg: err.toString(), data: null };
  287. }
  288. }
  289. async removeReportArchive(ctx) {
  290. try {
  291. const prjId = ctx.params.prjId;
  292. const stgId = ctx.params.stgId;
  293. const rptId = ctx.params.rptId;
  294. const orgName = ctx.params.orgName;
  295. const fileName = orgName + '.PDF';
  296. console.log('removing fileName: ' + fileName);
  297. const fullName = path.join(this.app.baseDir, 'app', 'public/archive', fileName);
  298. // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', 'public/archive', fileName));
  299. // fs.stat(fullName, function(err, data) {
  300. // if (err) {
  301. // console.log(err);
  302. // } else {
  303. // fs.unlink(fullName, function(err) {
  304. // if (err) {
  305. // console.log(err);
  306. // }
  307. // });
  308. // }
  309. // });
  310. const oss_result = await ctx.oss.delete('archive/' + fileName);
  311. if (!(oss_result && oss_result.res.status === 204)) {
  312. throw '删除归档文件失败';
  313. }
  314. const orgArchiveList = await ctx.service.rptArchive.getPrjStgArchive(prjId, stgId);
  315. if (orgArchiveList.length > 0) {
  316. const contentArr = JSON.parse(orgArchiveList[0].content);
  317. for (const item of contentArr) {
  318. if (item.rpt_id === rptId) {
  319. if (item.items && item.items.length > 0) {
  320. for (const subIdx in item.items) {
  321. if (item.items[subIdx].uuid === orgName) {
  322. item.items.splice(subIdx, 1);
  323. break;
  324. }
  325. }
  326. }
  327. break;
  328. }
  329. }
  330. const updatedRst = await ctx.service.rptArchive.updateArchive(prjId, stgId, contentArr);
  331. ctx.body = { err: 0, msg: '', data: { fileName, updatedRst } };
  332. } else {
  333. ctx.body = { err: 0, msg: '', data: { fileName, updatedRst: null } };
  334. }
  335. } catch (err) {
  336. this.log(err);
  337. ctx.body = { err: 1, msg: err.toString(), data: null };
  338. }
  339. }
  340. async addMultiReportArchive(ctx, params) {
  341. // 暂时不支持
  342. }
  343. async getArchivedFileByUUID(ctx) {
  344. // console.log('downloading : ' + ctx.params.uuid);
  345. const uuid = ctx.params.uuid;
  346. const rptName = ctx.params.rptName;
  347. const suffix = '.PDF';
  348. try {
  349. const rptNameURI = encodeURI(rptName);
  350. const filePath = this.app.baseDir + '/app/public/archive/';
  351. // console.log('filePath: ' + filePath);
  352. // await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  353. ctx.set({
  354. 'Content-Type': 'application/vnd.openxmlformats',
  355. 'Content-Disposition': 'attachment; filename="' + rptNameURI + suffix + "\"; filename*=utf-8''" + rptNameURI + suffix,
  356. });
  357. ctx.body = await fs.readFileSync(filePath + uuid + suffix);
  358. } catch (e) {
  359. console.log(e);
  360. }
  361. }
  362. async pdfShow(ctx) {
  363. const renderData = {
  364. can_netcasign: ctx.session.sessionProject.page_show.openNetCaSign === 1,
  365. };
  366. await ctx.render('report/archive_pdf.ejs', renderData);
  367. }
  368. }
  369. return ReportArchiveController;
  370. };