'use strict'; /** * * 附件 * @author LanJianRong * @date 2021/7/30 * @version */ const archiver = require('archiver'); const path = require('path'); const fs = require('fs'); module.exports = app => { class LedgerAtt extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'ledger_attachment'; } /** * 添加附件 * @param {Object} postData - 表单信息 * @param {Object} fileData - 文件信息 * @param {int} uid - 上传者id * @return {void} */ async save(postData, fileData, uid) { const data = { lid: postData.lid, uid, remark: '', }; Object.assign(data, fileData); const result = await this.db.insert(this.tableName, data); return result; } /** * 更新附件 * @param {Object} postData - 表单信息 * @param {Object} fileData - 文件信息 * @param {int} uid - 上传者id * @return {void} */ async updateByID(postData, fileData) { delete postData.size; const data = {}; Object.assign(data, fileData); Object.assign(data, postData); const result = await this.db.update(this.tableName, data); return result.affectedRows === 1; } /** * 获取所有附件 * @param {int} tid - 标段id * @return {void} */ async getDataByTenderId(tid) { const sql = 'SELECT att.id, att.lid, att.uid, att.filepath, att.filename, att.fileext, att.filesize, att.extra_upload, att.remark, att.in_time,' + ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`, leg.b_code as `b_code`' + ' FROM ?? AS att,?? AS pa,?? AS leg' + ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.tid = ? ORDER BY att.id DESC'; const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, tid]; return await this.db.query(sql, sqlParam); } /** * 获取单个附件 * @param {int} tid - 标段id * @param {int} sid - 当前期数 * @return {void} */ async getDataByFid(id) { const { ctx } = this; const sql = 'SELECT att.id, att.lid, att.uid, att.filepath, att.filename, att.extra_upload, att.fileext, att.filesize, att.remark, att.in_time,' + ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`,leg.b_code as `b_code`' + ' FROM ?? AS att,?? AS pa,?? AS leg' + ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.id = ? ORDER BY att.in_time DESC'; const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, id]; const result = await this.db.queryOne(sql, sqlParam); result.orginpath = this.ctx.app.config.fujianOssPath + result.filepath; if (!ctx.helper.canPreview(result.fileext)) { result.filepath = `/tender/${ctx.tender.id}/ledger/download/file/${result.id}`; } else { // result.filepath = result.filepath.replace(/^app|\/app/, ''); result.filepath = this.ctx.app.config.fujianOssPath + result.filepath; } return result; } /** * 将文件压缩成zip,并返回zip文件的路径 * @param {array} fileIds - 文件数组id * @param {string} zipPath - 压缩文件存储路径 * @return {string} 压缩后的zip文件路径 */ async compressedFile(fileIds, zipPath) { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('id', { value: fileIds, operate: 'in', }); const [sql, sqlParam] = this.sqlBuilder.build(this.tableName); const files = await this.db.query(sql, sqlParam); return new Promise((resolve, reject) => { // 每次开一个新的archiver const ziparchiver = archiver('zip'); const outputPath = fs.createWriteStream(path.resolve(this.app.baseDir, zipPath)); outputPath.on('error', err => { return reject(err); }); ziparchiver.pipe(outputPath); files.forEach(item => { ziparchiver.file(item.filepath, { name: item.filename + item.fileext }); }); // 存档警告 ziparchiver.on('warning', function(err) { // if (err.code === 'ENOENT') { // console.warn('stat故障和其他非阻塞错误'); // } return reject(err); }); // 存档出错 ziparchiver.on('error', function(err) { // console.log(err); return reject(err); }); ziparchiver.finalize(); outputPath.on('close', () => { return resolve(ziparchiver.pointer()); }); }); } async getViewDataByFid(id) { const sql = 'SELECT att.id, att.lid, att.uid, att.filepath, att.filename, att.fileext, att.filesize, att.extra_upload, att.remark, att.in_time,' + ' pa.name as `username`' + ' FROM ' + this.tableName + ' att Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On pa.id = att.uid' + ' WHERE att.id = ?'; const result = await this.db.query(sql, [id]); for (const r of result) { r.filepath = this.ctx.app.config.fujianOssPath + r.filepath; if (this.ctx.helper.canPreview(r.fileext)) r.viewpath = r.filepath; r.in_time = this.ctx.moment(r.in_time * 1000).format('YYYY-MM-DD'); } return result[0]; } async getViewData(tid, settle_id = -1) { const sql = 'SELECT att.id, att.lid, att.uid, att.filepath, att.filename, att.fileext, att.filesize, att.extra_upload, att.remark, att.in_time,' + ' pa.name as `username`' + ' FROM ' + this.tableName + ' att Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On pa.id = att.uid' + ' WHERE att.tid = ? AND att.settle_id = ? ORDER BY att.id DESC'; const result = await this.db.query(sql, [tid, settle_id]); for (const r of result) { r.filepath = this.ctx.app.config.fujianOssPath + r.filepath; if (this.ctx.helper.canPreview(r.fileext)) r.viewpath = r.filepath; r.in_time = this.ctx.moment(r.in_time * 1000).format('YYYY-MM-DD'); } return result; } } return LedgerAtt; };