123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const path = require('path');
- module.exports = app => {
- class Filing extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @param {String} tableName - 表名
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'file';
- }
- analysisFiles(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.analysisFiles(result);
- return result;
- }
- async checkFiles(filing_id, files) {
- const existFiles = await this.getAllDataByCondition({ columns: ['filename', 'fileext'], where: { filing_id, is_deleted: 0 } });
- const existFilesName = existFiles.map(x => { return x.filename + x.fileext });
- return files.filter(x => { return existFilesName.indexOf(x) >= 0; });
- }
- async addFiles(filing, fileInfo, user) {
- const conn = await this.db.beginTransaction();
- const result = {};
- try {
- const insertData = fileInfo.map(x => {
- return {
- id: this.uuid.v4(), spid: filing.spid, filing_id: filing.id, filing_type: filing.filing_type,
- 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);
- const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
- await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
- await conn.commit();
- result.files = { id: insertData.map(x => { return x.id; })};
- result.filing = { id: filing.id, file_count: count };
- } catch (err) {
- await conn.rollback();
- throw err;
- }
- result.files = await this.getFiles({ where: result.files });
- return result;
- }
- async delFiles(files) {
- if (files.length === 0) return;
- const fileDatas = await this.getAllDataByCondition({ where: { id: files } });
- const filing = await this.ctx.service.filing.getDataById(fileDatas[0].filing_id);
- 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);
- const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
- await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
- await conn.commit();
- result.del = files;
- result.filing = { id: filing.id, file_count: count };
- } catch (err) {
- await conn.rollback();
- throw err;
- }
- return result;
- }
- async relaFiles(filing, fileInfo, user) {
- const conn = await this.db.beginTransaction();
- const result = {};
- try {
- const insertData = fileInfo.map(x => {
- return {
- id: this.uuid.v4(), spid: filing.spid, filing_id: filing.id, filing_type: filing.filing_type,
- 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,
- is_rela: 1, rela_info: JSON.stringify(x.rela_info),
- };
- });
- await conn.insert(this.tableName, insertData);
- const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
- await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
- await conn.commit();
- result.files = { id: insertData.map(x => { return x.id; })};
- result.filing = { id: filing.id, file_count: count };
- } catch (err) {
- await conn.rollback();
- throw err;
- }
- result.files = await this.getFiles({ where: result.files });
- return result;
- }
- 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;
- }
- async search(filing_type, keyword, limit = 1000) {
- if (!filing_type || filing_type.length === 0 || !keyword) return [];
- const sql = `SELECT * FROM ${this.tableName}` +
- ` WHERE spid = ? and is_deleted = 0 and filing_type in (${filing_type.join(',')}) and filename like '%${keyword}%'`+
- ` ORDER BY update_time DESC LIMIT 0, ${limit}`;
- const result = await this.db.query(sql, [this.ctx.subProject.id]);
- this.analysisFiles(result);
- return result;
- }
- }
- return Filing;
- };
|