'use strict'; /** * Created by Tony on 2021/3/31. */ const path = require('path'); const uuidV1 = require('uuid').v1; const fs = require('fs'); const MAX_ARCHIVE = 3; const tenderMenu = require('../../config/menu').tenderMenu; const measureType = require('../const/tender').measureType; const fsUtil = require('../public/js/fsUtil'); const auditConst = require('../const/audit'); module.exports = app => { class ReportArchiveController extends app.BaseController { async index(ctx) { const tender = ctx.tender; const stage = ctx.stage; let stage_id = -1; let stage_order = -1; let stage_times = -1; let stage_status = -1; const treeNodes = await ctx.service.rptTreeNode.getNodesByProjectId([-1, tender.data.project_id]); const custTreeNodes = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(this.ctx.session.sessionUser.accountId); const stageList = await ctx.service.stage.getValidStagesShort(tender.id); // // 。。。 let archiveList = []; // console.log('tender.data.project_id: ' + tender.data.project_id); if (stage) { // console.log('ctx.stage.id: ' + ctx.stage.id); const archives = await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, ctx.stage.id); stage_id = stage.id; stage_order = stage.order; stage_times = stage.times; stage_status = stage.status; if (archives.length > 0) { archiveList = JSON.parse(archives[0].content); } } else { // console.log('stageList[0].id: ' + stageList[0].id); const archives = await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, stageList[stageList.length - 1].id); // stage_id = stageList[0].id; // stage_order = stageList[0].order; // stage_times = stageList[0].times; // stage_status = stageList[0].status; if (archives && archives.length > 0) { archiveList = JSON.parse(archives[0].content); } } let rpt_tpl_items = '{ customize: [], common: [] }'; if (custTreeNodes.length > 0) { rpt_tpl_items = custTreeNodes[0].rpt_tpl_items; } // const renderData = { tender: tender.data, rpt_tpl_data: JSON.stringify(treeNodes), cust_tpl_data: rpt_tpl_items, project_id: tender.data.project_id, tender_id: tender.id, stg_id: stage_id, stg_order: stage_order, stg_times: stage_times, stg_status: stage_status, stage_list: JSON.stringify(stageList), tenderMenu, measureType, jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.report.main), stages: stageList, auditConst: auditConst.stage, archiveList, }; await this.layout('report/index_archive.ejs', renderData); } async getReportArchive(ctx) { const params = JSON.parse(ctx.request.body.params); // ctx.body = await this._getReport(ctx, params); const archives = await ctx.service.rptArchive.getPrjStgArchive(params.prjId, params.stgId); let archiveList = []; if (archives.length > 0) { archiveList = JSON.parse(archives[0].content); } ctx.body = { data: archiveList, }; } async addReportArchive(ctx) { try { const stream = await ctx.getFileStream(); const prjId = ctx.params.prjId; const stgId = ctx.params.stgId; const rptId = ctx.params.rptId; const newName = uuidV1(); const fileName = newName + '.PDF'; // console.log('adding fileName: ' + fileName); // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', 'public/archive', fileName)); const oss_result = await ctx.oss.put('archive/' + fileName, stream); if (!(oss_result && oss_result.url && oss_result.res.status === 200)) { throw '上传文件失败'; } // const url = await ctx.oss.delete('archive/52d3e7f0-c7fb-11eb-b8c2-51b890b95d23.PDF'); // console.log(url); // const flag = true; // if (flag) { // throw 'ok'; // } const updateDate = new Date(); const montStr = (updateDate.getMonth() + 1) < 10 ? ('0' + (updateDate.getMonth() + 1)) : ((updateDate.getMonth() + 1)); const dateStr = (updateDate.getDate()) < 10 ? ('0' + (updateDate.getDate())) : ((updateDate.getDate())); const dtStr = updateDate.getFullYear() + '-' + montStr + '-' + dateStr; const orgArchiveList = await ctx.service.rptArchive.getPrjStgArchive(prjId, stgId); if (orgArchiveList.length > 0) { const contentArr = JSON.parse(orgArchiveList[0].content); let hasArchive = false; for (const item of contentArr) { if (item.rpt_id === rptId) { hasArchive = true; if (item.items.length >= MAX_ARCHIVE) { // 超出界限,需要删除时间最旧的那个 let rmIdx = 0; for (let idx = 1; idx < item.items.length; idx++) { if (item.items[rmIdx].updateDate_time < item.items[idx].updateDate_time) { rmIdx = idx; } } // 同时删除oss文件 await ctx.oss.delete('archive/' + item.items[rmIdx].uuid + '.PDF'); item.items.splice(rmIdx, 1); } const newItem = { uuid: newName, updateDate_time: dtStr }; item.items.push(newItem); break; } } if (!hasArchive) { // 表示有新的模板需要添加 contentArr.push({ rpt_id: rptId, items: [{ uuid: newName, updateDate_time: dtStr }] }); } const updatedRst = await ctx.service.rptArchive.updateArchive(orgArchiveList[0].id, prjId, stgId, contentArr); // console.log(updatedRst); ctx.body = { err: 0, msg: '', data: { fileName, updateDate, addedRst: contentArr } }; } else { // 需要增加 const archiveArr = []; archiveArr.push({ rpt_id: rptId, items: [{ uuid: newName, updateDate_time: dtStr }] }); const addedRst = await ctx.service.rptArchive.createArchive(prjId, stgId, archiveArr); // console.log(addedRst); ctx.body = { err: 0, msg: '', data: { fileName, updateDate, addedRst: archiveArr } }; } } catch (err) { this.log(err); ctx.body = { err: 1, msg: err.toString(), data: null }; } } async updateReportArchive(ctx) { try { const stream = await ctx.getFileStream(); const prjId = ctx.params.prjId; const stgId = ctx.params.stgId; const rptId = ctx.params.rptId; const orgName = ctx.params.orgName; const fileName = orgName + '.PDF'; console.log('updating fileName: ' + fileName); // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', 'public/archive', fileName)); const oss_result = await ctx.oss.put('archive/' + fileName, stream); if (!(oss_result && oss_result.url && oss_result.res.status === 200)) { throw '上传文件失败'; } const updateDate = new Date(); const montStr = (updateDate.getMonth() + 1) < 10 ? ('0' + (updateDate.getMonth() + 1)) : ((updateDate.getMonth() + 1)); const dateStr = (updateDate.getDate()) < 10 ? ('0' + (updateDate.getDate())) : ((updateDate.getDate())); const dtStr = updateDate.getFullYear() + '-' + montStr + '-' + dateStr; const orgArchiveList = await ctx.service.rptArchive.getPrjStgArchive(prjId, stgId); if (orgArchiveList.length > 0) { const contentArr = JSON.parse(orgArchiveList[0].content); for (const item of contentArr) { if (item.rpt_id === rptId) { if (item.items && item.items.length > 0) { for (const subItem of item.items) { if (subItem.uuid === orgName) { subItem.updateDate_time = dtStr; break; } } } else { item.items = [{ uuid: orgName, updateDate_time: dtStr }]; } break; } } const updatedRst = await ctx.service.rptArchive.updateArchive(orgArchiveList[0].id, prjId, stgId, contentArr); // console.log(updatedRst); ctx.body = { err: 0, msg: '', data: { fileName, updateDate, updatedRst: contentArr } }; } else { // 需要增加 const archiveArr = []; archiveArr.push({ rpt_id: rptId, items: [{ uuid: orgName, updateDate_time: dtStr }] }); const updatedRst = await ctx.service.rptArchive.createArchive(prjId, stgId, archiveArr); // console.log(updatedRst); ctx.body = { err: 0, msg: '', data: { fileName, updateDate, updatedRst: archiveArr } }; } } catch (err) { this.log(err); ctx.body = { err: 1, msg: err.toString(), data: null }; } } async removeReportArchive(ctx) { try { const prjId = ctx.params.prjId; const stgId = ctx.params.stgId; const rptId = ctx.params.rptId; const orgName = ctx.params.orgName; const fileName = orgName + '.PDF'; console.log('removing fileName: ' + fileName); const fullName = path.join(this.app.baseDir, 'app', 'public/archive', fileName); // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', 'public/archive', fileName)); // fs.stat(fullName, function(err, data) { // if (err) { // console.log(err); // } else { // fs.unlink(fullName, function(err) { // if (err) { // console.log(err); // } // }); // } // }); const oss_result = await ctx.oss.delete('archive/' + fileName); if (!(oss_result && oss_result.res.status === 204)) { throw '删除归档文件失败'; } const orgArchiveList = await ctx.service.rptArchive.getPrjStgArchive(prjId, stgId); if (orgArchiveList.length > 0) { const contentArr = JSON.parse(orgArchiveList[0].content); for (const item of contentArr) { if (item.rpt_id === rptId) { if (item.items && item.items.length > 0) { for (const subIdx in item.items) { if (item.items[subIdx].uuid === orgName) { item.items.splice(subIdx, 1); break; } } } break; } } const updatedRst = await ctx.service.rptArchive.updateArchive(prjId, stgId, contentArr); ctx.body = { err: 0, msg: '', data: { fileName, updatedRst } }; } else { ctx.body = { err: 0, msg: '', data: { fileName, updatedRst: null } }; } } catch (err) { this.log(err); ctx.body = { err: 1, msg: err.toString(), data: null }; } } async addMultiReportArchive(ctx, params) { // 暂时不支持 } async getArchivedFileByUUID(ctx) { // console.log('downloading : ' + ctx.params.uuid); const uuid = ctx.params.uuid; const rptName = ctx.params.rptName; const suffix = '.PDF'; try { const rptNameURI = encodeURI(rptName); const filePath = this.app.baseDir + '/app/public/archive/'; // console.log('filePath: ' + filePath); // await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download'); ctx.set({ 'Content-Type': 'application/vnd.openxmlformats', 'Content-Disposition': 'attachment; filename="' + rptNameURI + suffix + "\"; filename*=utf-8''" + rptNameURI + suffix, }); ctx.body = await fs.readFileSync(filePath + uuid + suffix); } catch (e) { console.log(e); } } } return ReportArchiveController; };