'use strict'; /** * Created by EllisRan on 2020/3/3. */ const BaseService = require('../base/base_service'); const contractConst = require('../const/contract'); module.exports = app => { class ContractAtt extends BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'contract_attachment'; this.dataId = 'id'; } async getAtt(cid) { const sql = 'SELECT a.*, b.name as username FROM ?? as a LEFT JOIN ?? as b ON a.`uid` = b.`id` WHERE a.`cid` = ? ORDER BY `upload_time` DESC'; const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid]; const result = await this.db.query(sql, sqlParam); return result.map(item => { item.orginpath = this.ctx.app.config.fujianOssPath + item.filepath; if (!this.ctx.helper.canPreview(item.fileext)) { item.filepath = `/sp/${this.ctx.subProject.id}/contract${item.tid ? '/tender/' + item.tid : ''}/detail/${contractConst.typeMap[item.contract_type]}/${item.cid}/file/${item.id}/download`; } else { item.filepath = this.ctx.app.config.fujianOssPath + item.filepath; item.viewpath = item.filepath; } return item; }); } /** * 存储上传的文件信息至数据库 * @param {Array} payload 载荷 * @return {Promise} 数据库插入执行实例 */ async saveFileMsgToDb(payload) { return await this.db.insert(this.tableName, payload); } /** * 删除附件 * @param {Number} id - 附件id * @return {void} */ async delete(id) { return await this.deleteById(id); } } return ContractAtt; };