12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- '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 = 'phase_pay_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(phase_id, type) {
- const data = await this.getAllDataByCondition({
- where: { type, phase_id, 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(phasePay, type, fileInfo, user) {
- const conn = await this.db.beginTransaction();
- const result = {};
- try {
- const insertData = fileInfo.map(x => {
- return {
- id: this.uuid.v4(), tid: phasePay.tid, phase_id: phasePay.id, 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;
- };
|