'use strict'; /** * * 附件 * @author Ellisran * @date 2019/1/11 * @version */ module.exports = app => { class SubProjFile extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'sub_project_file'; } _analysisData(files) { const helper = this.ctx.helper; const userId = this.ctx.session.sessionUser.accountId; const ossPath = this.ctx.app.config.fujianOssPath; files.forEach(x => { x.viewpath = helper.canPreview(x.fileext) ? ossPath + x.filepath : ''; x.filepath = ossPath + x.filepath; x.fileext_str = helper.fileExtStr(x.fileext); x.canEdit = x.user_id === userId; }); } async getData(spid, type) { const data = await this.getAllDataByCondition({ where: { type, spid, is_deleted: 0 }, orders: [['create_time', 'desc']], }); this._analysisData(data); return data; } async getFiles(condition) { condition.orders = [['create_time', 'desc']]; const result = await this.getAllDataByCondition(condition); this._analysisData(result); return result; } async addFiles(spid, type, fileInfo, user) { const conn = await this.db.beginTransaction(); const result = {}; try { const insertData = fileInfo.map(x => { return { id: this.uuid.v4(), spid, type, rela_id: x.rela_id, user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role, filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath, }; }); await conn.insert(this.tableName, insertData); await conn.commit(); result.files = { id: insertData.map(x => { return x.id; })}; } catch (err) { await conn.rollback(); throw err; } return await this.getFiles({ where: result.files }); } async delFiles(files) { if (files.length === 0) return; const fileDatas = await this.getAllDataByCondition({ where: { id: files } }); const result = {}; const conn = await this.db.beginTransaction(); try { const updateData = fileDatas.map(x => { return { id: x.id, is_deleted: 1 }; }); if (updateData.length > 0) await conn.updateRows(this.tableName, updateData); await conn.commit(); result.del = files; } catch (err) { await conn.rollback(); throw err; } return result; } } return SubProjFile; };