report_archive_controller.js 19 KB

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