|
|
@@ -0,0 +1,96 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * 临时文件
|
|
|
+ * @author Mai
|
|
|
+ * @date 2026/4/21
|
|
|
+ * @version
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+module.exports = app => {
|
|
|
+ class TempFile extends app.BaseService {
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ *
|
|
|
+ * @param {Object} ctx - egg全局变量
|
|
|
+ * @return {void}
|
|
|
+ */
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx);
|
|
|
+ this.tableName = 'temp_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 getFiles(condition) {
|
|
|
+ condition.orders = [['create_time', 'desc']];
|
|
|
+ const result = await this.getAllDataByCondition(condition);
|
|
|
+ this._analysisData(result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ async addFiles(tenderId, fileInfo, user) {
|
|
|
+ const conn = await this.db.beginTransaction();
|
|
|
+ const result = {};
|
|
|
+ try {
|
|
|
+ const insertData = fileInfo.map(x => {
|
|
|
+ return {
|
|
|
+ id: this.uuid.v4(), tid: tenderId, rela_type: x.rela_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) {
|
|
|
+ const fileDatas = await this.getAllDataByCondition({ where: { id: files } });
|
|
|
+ const result = {};
|
|
|
+
|
|
|
+ const conn = await this.db.beginTransaction();
|
|
|
+ try {
|
|
|
+ for (const f of fileDatas) {
|
|
|
+ await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + f.filepath);
|
|
|
+ }
|
|
|
+ await conn.delete(this.tableName, { id: files });
|
|
|
+ await conn.commit();
|
|
|
+ result.del = files;
|
|
|
+ } catch (err) {
|
|
|
+ await conn.rollback();
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ async clearFiles(days) {
|
|
|
+ const fileDatas = await this.db.query(`SELECT * FROM ${this.tableName} WHERE create_time < DATE_SUB(CURDATE(), INTERVAL ${days} day)`);
|
|
|
+ if (fileDatas.length === 0) return;
|
|
|
+
|
|
|
+ for (const f of fileDatas) {
|
|
|
+ await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + f.filepath);
|
|
|
+ await this.deleteById(f.id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return TempFile;
|
|
|
+};
|