report_archive_controller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. };
  83. await this.layout('report/index_archive.ejs', renderData);
  84. }
  85. async getReportArchive(ctx) {
  86. const params = JSON.parse(ctx.request.body.params);
  87. // ctx.body = await this._getReport(ctx, params);
  88. const archives = await ctx.service.rptArchive.getPrjStgArchive(params.prjId, params.stgId);
  89. const archiveEncryptions = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(params.prjId, params.stgId);
  90. let archiveList = [], archiveEncryptionList = [];
  91. if (archives.length > 0) {
  92. archiveList = JSON.parse(archives[0].content);
  93. }
  94. if (archiveEncryptions.length > 0) {
  95. archiveEncryptionList = JSON.parse(archiveEncryptions[0].content);
  96. }
  97. ctx.body = {
  98. data: archiveList,
  99. encryptionData: archiveEncryptionList,
  100. };
  101. }
  102. async addReportArchiveEncryption(ctx) {
  103. const params = JSON.parse(ctx.request.body.params);
  104. const prjId = params.prjId;
  105. const stgId = params.stgId;
  106. const rptId = params.rptId;
  107. const ttlPgs = params.ttlPgs;
  108. const content = params.content;
  109. const orgArchiveList = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(prjId, stgId);
  110. if (orgArchiveList.length > 0) {
  111. const contentArr = JSON.parse(orgArchiveList[0].content);
  112. let hasArchive = false;
  113. for (const item of contentArr) {
  114. if (item.rpt_id === rptId) {
  115. // 考虑到报表模板的稳定性,只保留一项来记录位置就足够了,都不考虑用uuid了
  116. item.encryption = content;
  117. hasArchive = true;
  118. break;
  119. }
  120. }
  121. if (!hasArchive) {
  122. // 表示有新的要加
  123. contentArr.push({ rpt_id: rptId, total_page: ttlPgs, encryption: content });
  124. } else {
  125. //
  126. }
  127. const updatedRst = await ctx.service.rptArchiveEncryption.updateArchiveEncryption(orgArchiveList[0].id, prjId, stgId, contentArr);
  128. // console.log(updatedRst);
  129. ctx.body = { err: 0, msg: '', data: { addedRst: contentArr } };
  130. } else {
  131. // 需要增加
  132. const archiveArr = [];
  133. archiveArr.push({ rpt_id: rptId, encryption: content });
  134. const addedRst = await ctx.service.rptArchiveEncryption.createArchiveEncryption(prjId, stgId, archiveArr);
  135. // console.log(addedRst);
  136. ctx.body = { err: 0, msg: '', data: { addedRst: archiveArr } };
  137. }
  138. }
  139. async addReportArchive(ctx) {
  140. try {
  141. const stream = await ctx.getFileStream();
  142. const prjId = ctx.params.prjId;
  143. const stgId = ctx.params.stgId;
  144. const rptId = ctx.params.rptId;
  145. const newName = uuidV1();
  146. const fileName = newName + '.PDF';
  147. // console.log('adding fileName: ' + fileName);
  148. // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', 'public/archive', fileName));
  149. const oss_result = await ctx.oss.put('archive/' + fileName, stream);
  150. if (!(oss_result && oss_result.url && oss_result.res.status === 200)) {
  151. throw '上传文件失败';
  152. }
  153. // const url = await ctx.oss.delete('archive/52d3e7f0-c7fb-11eb-b8c2-51b890b95d23.PDF');
  154. // console.log(url);
  155. // const flag = true;
  156. // if (flag) {
  157. // throw 'ok';
  158. // }
  159. const updateDate = new Date();
  160. const montStr = (updateDate.getMonth() + 1) < 10 ? ('0' + (updateDate.getMonth() + 1)) : ((updateDate.getMonth() + 1));
  161. const dateStr = (updateDate.getDate()) < 10 ? ('0' + (updateDate.getDate())) : ((updateDate.getDate()));
  162. const dtStr = updateDate.getFullYear() + '-' + montStr + '-' + dateStr;
  163. const orgArchiveList = await ctx.service.rptArchive.getPrjStgArchive(prjId, stgId);
  164. if (orgArchiveList.length > 0) {
  165. const contentArr = JSON.parse(orgArchiveList[0].content);
  166. let hasArchive = false;
  167. for (const item of contentArr) {
  168. if (item.rpt_id === rptId) {
  169. hasArchive = true;
  170. if (item.items.length >= MAX_ARCHIVE) {
  171. // 超出界限,需要删除时间最旧的那个
  172. let rmIdx = 0;
  173. for (let idx = 1; idx < item.items.length; idx++) {
  174. if (item.items[rmIdx].updateDate_time < item.items[idx].updateDate_time) {
  175. rmIdx = idx;
  176. }
  177. }
  178. // 同时删除oss文件
  179. await ctx.oss.delete('archive/' + item.items[rmIdx].uuid + '.PDF');
  180. item.items.splice(rmIdx, 1);
  181. }
  182. const newItem = { uuid: newName, updateDate_time: dtStr };
  183. item.items.push(newItem);
  184. break;
  185. }
  186. }
  187. if (!hasArchive) {
  188. // 表示有新的模板需要添加
  189. contentArr.push({ rpt_id: rptId, items: [{ uuid: newName, updateDate_time: dtStr }] });
  190. }
  191. const updatedRst = await ctx.service.rptArchive.updateArchive(orgArchiveList[0].id, prjId, stgId, contentArr);
  192. // console.log(updatedRst);
  193. ctx.body = { err: 0, msg: '', data: { fileName, updateDate, addedRst: contentArr } };
  194. } else {
  195. // 需要增加
  196. const archiveArr = [];
  197. archiveArr.push({ rpt_id: rptId, items: [{ uuid: newName, updateDate_time: dtStr }] });
  198. const addedRst = await ctx.service.rptArchive.createArchive(prjId, stgId, archiveArr);
  199. // console.log(addedRst);
  200. ctx.body = { err: 0, msg: '', data: { fileName, updateDate, addedRst: archiveArr } };
  201. }
  202. } catch (err) {
  203. this.log(err);
  204. ctx.body = { err: 1, msg: err.toString(), data: null };
  205. }
  206. }
  207. async updateReportArchiveEncryption(ctx) {
  208. // 在add方法中已经处理
  209. await this.addReportArchiveEncryption(ctx);
  210. }
  211. async updateReportArchive(ctx) {
  212. try {
  213. const stream = await ctx.getFileStream();
  214. const prjId = ctx.params.prjId;
  215. const stgId = ctx.params.stgId;
  216. const rptId = ctx.params.rptId;
  217. const orgName = ctx.params.orgName;
  218. const fileName = orgName + '.PDF';
  219. console.log('updating fileName: ' + fileName);
  220. // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', 'public/archive', fileName));
  221. const oss_result = await ctx.oss.put('archive/' + fileName, stream);
  222. if (!(oss_result && oss_result.url && oss_result.res.status === 200)) {
  223. throw '上传文件失败';
  224. }
  225. const updateDate = new Date();
  226. const montStr = (updateDate.getMonth() + 1) < 10 ? ('0' + (updateDate.getMonth() + 1)) : ((updateDate.getMonth() + 1));
  227. const dateStr = (updateDate.getDate()) < 10 ? ('0' + (updateDate.getDate())) : ((updateDate.getDate()));
  228. const dtStr = updateDate.getFullYear() + '-' + montStr + '-' + dateStr;
  229. const orgArchiveList = await ctx.service.rptArchive.getPrjStgArchive(prjId, stgId);
  230. if (orgArchiveList.length > 0) {
  231. const contentArr = JSON.parse(orgArchiveList[0].content);
  232. for (const item of contentArr) {
  233. if (item.rpt_id === rptId) {
  234. if (item.items && item.items.length > 0) {
  235. for (const subItem of item.items) {
  236. if (subItem.uuid === orgName) {
  237. subItem.updateDate_time = dtStr;
  238. break;
  239. }
  240. }
  241. } else {
  242. item.items = [{ uuid: orgName, updateDate_time: dtStr }];
  243. }
  244. break;
  245. }
  246. }
  247. const updatedRst = await ctx.service.rptArchive.updateArchive(orgArchiveList[0].id, prjId, stgId, contentArr);
  248. // console.log(updatedRst);
  249. ctx.body = { err: 0, msg: '', data: { fileName, updateDate, updatedRst: contentArr } };
  250. } else {
  251. // 需要增加
  252. const archiveArr = [];
  253. archiveArr.push({ rpt_id: rptId, items: [{ uuid: orgName, updateDate_time: dtStr }] });
  254. const updatedRst = await ctx.service.rptArchive.createArchive(prjId, stgId, archiveArr);
  255. // console.log(updatedRst);
  256. ctx.body = { err: 0, msg: '', data: { fileName, updateDate, updatedRst: archiveArr } };
  257. }
  258. } catch (err) {
  259. this.log(err);
  260. ctx.body = { err: 1, msg: err.toString(), data: null };
  261. }
  262. }
  263. async removeReportArchiveEncryption(ctx) {
  264. try {
  265. const prjId = ctx.params.prjId;
  266. const stgId = ctx.params.stgId;
  267. const rptId = ctx.params.rptId;
  268. const orgArchiveList = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(prjId, stgId);
  269. if (orgArchiveList.length > 0) {
  270. const contentArr = JSON.parse(orgArchiveList[0].content);
  271. for (let idx = 0; idx < contentArr.length; idx++) {
  272. if (contentArr[idx].rpt_id === rptId) {
  273. contentArr.splice(idx, 1);
  274. break;
  275. }
  276. }
  277. const updatedRst = await ctx.service.rptArchive.updateArchive(prjId, stgId, contentArr);
  278. ctx.body = { err: 0, msg: '', data: { updatedRst } };
  279. } else {
  280. ctx.body = { err: 0, msg: '', data: { updatedRst: null } };
  281. }
  282. } catch (err) {
  283. this.log(err);
  284. ctx.body = { err: 1, msg: err.toString(), data: null };
  285. }
  286. }
  287. async removeReportArchive(ctx) {
  288. try {
  289. const prjId = ctx.params.prjId;
  290. const stgId = ctx.params.stgId;
  291. const rptId = ctx.params.rptId;
  292. const orgName = ctx.params.orgName;
  293. const fileName = orgName + '.PDF';
  294. console.log('removing fileName: ' + fileName);
  295. const fullName = path.join(this.app.baseDir, 'app', 'public/archive', fileName);
  296. // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', 'public/archive', fileName));
  297. // fs.stat(fullName, function(err, data) {
  298. // if (err) {
  299. // console.log(err);
  300. // } else {
  301. // fs.unlink(fullName, function(err) {
  302. // if (err) {
  303. // console.log(err);
  304. // }
  305. // });
  306. // }
  307. // });
  308. const oss_result = await ctx.oss.delete('archive/' + fileName);
  309. if (!(oss_result && oss_result.res.status === 204)) {
  310. throw '删除归档文件失败';
  311. }
  312. const orgArchiveList = await ctx.service.rptArchive.getPrjStgArchive(prjId, stgId);
  313. if (orgArchiveList.length > 0) {
  314. const contentArr = JSON.parse(orgArchiveList[0].content);
  315. for (const item of contentArr) {
  316. if (item.rpt_id === rptId) {
  317. if (item.items && item.items.length > 0) {
  318. for (const subIdx in item.items) {
  319. if (item.items[subIdx].uuid === orgName) {
  320. item.items.splice(subIdx, 1);
  321. break;
  322. }
  323. }
  324. }
  325. break;
  326. }
  327. }
  328. const updatedRst = await ctx.service.rptArchive.updateArchive(prjId, stgId, contentArr);
  329. ctx.body = { err: 0, msg: '', data: { fileName, updatedRst } };
  330. } else {
  331. ctx.body = { err: 0, msg: '', data: { fileName, updatedRst: null } };
  332. }
  333. } catch (err) {
  334. this.log(err);
  335. ctx.body = { err: 1, msg: err.toString(), data: null };
  336. }
  337. }
  338. async addMultiReportArchive(ctx, params) {
  339. // 暂时不支持
  340. }
  341. async getArchivedFileByUUID(ctx) {
  342. // console.log('downloading : ' + ctx.params.uuid);
  343. const uuid = ctx.params.uuid;
  344. const rptName = ctx.params.rptName;
  345. const suffix = '.PDF';
  346. try {
  347. const rptNameURI = encodeURI(rptName);
  348. const filePath = this.app.baseDir + '/app/public/archive/';
  349. // console.log('filePath: ' + filePath);
  350. // await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  351. ctx.set({
  352. 'Content-Type': 'application/vnd.openxmlformats',
  353. 'Content-Disposition': 'attachment; filename="' + rptNameURI + suffix + "\"; filename*=utf-8''" + rptNameURI + suffix,
  354. });
  355. ctx.body = await fs.readFileSync(filePath + uuid + suffix);
  356. } catch (e) {
  357. console.log(e);
  358. }
  359. }
  360. async pdfShow(ctx) {
  361. await ctx.render('report/archive_pdf.ejs');
  362. }
  363. }
  364. return ReportArchiveController;
  365. };