'use strict'; /** * 质量管理 - 工程质量 * * @author Mai * @date 2024/7/22 * @version */ const path = require('path'); module.exports = app => { class QualityFile extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'quality_file'; } analysisFiles(files) { const helper = this.ctx.helper; const userId = this.ctx.session.sessionUser.accountId; const ossPath = this.ctx.app.config.ossUrl; 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 getFiles(condition) { condition.orders = [['create_time', 'asc']]; const result = await this.getAllDataByCondition(condition); this.analysisFiles(result); return result; } async getBlockFiles(quality, blockType, blockId) { if (!quality) throw '参数错误'; // const where = { tid: quality.tid, quality_id: quality.id }; const where = { quality_id: quality.id, is_deleted: 0 }; if (blockType) where.block_type = blockType; if (blockId) where.block_id = blockId; return this.getFiles({ where }); } async addFiles(user, quality, fileInfo, blockType, blockId = '', specType = '') { const insertData = fileInfo.map(x => { return { id: this.uuid.v4(), tid: quality.tid, quality_id: quality.id, rela_type: quality.rela_type, rela_id: quality.rela_id, block_type: blockType, block_id: blockId, spec_type: specType, 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 this.db.insert(this.tableName, insertData); return await this.getFiles({ where: { id: insertData.map(x => { return x.id; })} }); } async delFiles(files) { if (files.length === 0) return; const fileDatas = await this.getAllDataByCondition({ where: { id: files } }); const updateData = fileDatas.map(x => { return { id: x.id, is_deleted: 1 }; }); if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData); return files; } async saveFile(id, filename){ const file = await this.getDataById(id); if (!file) throw '文件不存在'; if (file.user_id !== this.ctx.session.sessionUser.accountId) throw '您无权编辑该文件'; const info = path.parse(filename); const updateData = { id, filename: info.name, fileext: info.ext}; await this.defaultUpdate(updateData); return updateData; } } return QualityFile; };