report_archive_controller.js 18 KB

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