file.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const path = require('path');
  10. module.exports = app => {
  11. class Filing extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @param {String} tableName - 表名
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'file';
  22. }
  23. analysisFiles(files) {
  24. const helper = this.ctx.helper;
  25. const userId = this.ctx.session.sessionUser.accountId;
  26. const ossPath = this.ctx.app.config.fujianOssPath;
  27. files.forEach(x => {
  28. x.viewpath = helper.canPreview(x.fileext) ? ossPath + x.filepath : '';
  29. x.filepath = ossPath + x.filepath;
  30. x.fileext_str = helper.fileExtStr(x.fileext);
  31. x.canEdit = x.user_id === userId;
  32. });
  33. }
  34. async getFiles(condition) {
  35. // condition.orders = [['create_time', 'desc']];
  36. const result = await this.getAllDataByCondition(condition);
  37. this.analysisFiles(result);
  38. return result;
  39. }
  40. async checkFiles(filing_id, files) {
  41. const existFiles = await this.getAllDataByCondition({ columns: ['filename', 'fileext'], where: { filing_id, is_deleted: 0 } });
  42. const existFilesName = existFiles.map(x => { return x.filename + x.fileext });
  43. return files.filter(x => { return existFilesName.indexOf(x) >= 0; });
  44. }
  45. async addFiles(filing, fileInfo, user) {
  46. const conn = await this.db.beginTransaction();
  47. const result = {};
  48. try {
  49. const insertData = fileInfo.map(x => {
  50. return {
  51. id: this.uuid.v4(), spid: filing.spid, filing_id: filing.id, filing_type: filing.filing_type,
  52. user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  53. filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath,
  54. };
  55. });
  56. await conn.insert(this.tableName, insertData);
  57. const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
  58. await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
  59. await conn.commit();
  60. result.files = { id: insertData.map(x => { return x.id; })};
  61. result.filing = { id: filing.id, file_count: count };
  62. } catch (err) {
  63. await conn.rollback();
  64. throw err;
  65. }
  66. result.files = await this.getFiles({ where: result.files });
  67. return result;
  68. }
  69. async delFiles(files) {
  70. if (files.length === 0) return;
  71. const fileDatas = await this.getAllDataByCondition({ where: { id: files } });
  72. const filing = await this.ctx.service.filing.getDataById(fileDatas[0].filing_id);
  73. if (this.ctx.subProject.permission.file_permission.indexOf(this.ctx.service.subProjPermission.PermissionConst.file.delete.value) < 0) {
  74. for (const file of fileDatas) {
  75. if (file.user_id !== this.ctx.session.sessionUser.accountId) throw '无权删除文件';
  76. }
  77. }
  78. const result = {};
  79. const conn = await this.db.beginTransaction();
  80. try {
  81. const updateData = fileDatas.map(x => { return { id: x.id, is_deleted: 1 }; });
  82. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  83. const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
  84. await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
  85. await conn.commit();
  86. result.del = files;
  87. result.filing = { id: filing.id, file_count: count };
  88. } catch (err) {
  89. await conn.rollback();
  90. throw err;
  91. }
  92. return result;
  93. }
  94. async relaFiles(filing, fileInfo, user) {
  95. const conn = await this.db.beginTransaction();
  96. const result = {};
  97. try {
  98. const insertData = fileInfo.map(x => {
  99. return {
  100. id: this.uuid.v4(), spid: filing.spid, filing_id: filing.id, filing_type: filing.filing_type,
  101. user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  102. filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath,
  103. is_rela: 1, rela_info: JSON.stringify(x.rela_info),
  104. };
  105. });
  106. await conn.insert(this.tableName, insertData);
  107. const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
  108. await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
  109. await conn.commit();
  110. result.files = { id: insertData.map(x => { return x.id; })};
  111. result.filing = { id: filing.id, file_count: count };
  112. } catch (err) {
  113. await conn.rollback();
  114. throw err;
  115. }
  116. result.files = await this.getFiles({ where: result.files });
  117. return result;
  118. }
  119. async saveFile(id, filename){
  120. const file = await this.getDataById(id);
  121. if (!file) throw '文件不存在';
  122. if (file.user_id !== this.ctx.session.sessionUser.accountId) throw '您无权编辑该文件';
  123. const info = path.parse(filename);
  124. const updateData = { id, filename: info.name, fileext: info.ext};
  125. await this.defaultUpdate(updateData);
  126. return updateData;
  127. }
  128. async moveFile(id, filing_id) {
  129. const file = await this.getDataById(id);
  130. if (!file) throw '文件不存在';
  131. if (file.user_id !== this.ctx.session.sessionUser.accountId) throw '您无权编辑该文件';
  132. const orgFiling = await this.ctx.service.filing.getDataById(file.filing_id);
  133. const filing = await this.ctx.service.filing.getDataById(filing_id);
  134. if (!filing) throw '目标分类不存在';
  135. const conn = await this.db.beginTransaction();
  136. try {
  137. const updateFile = { id: file.id, filing_type: filing.filing_type, filing_id: filing.id };
  138. await conn.update(this.tableName, updateFile);
  139. const updateFiling = [{id: orgFiling.id, file_count: orgFiling.file_count - 1}, {id: filing.id, file_count: filing.file_count + 1}];
  140. await conn.updateRows(this.ctx.service.filing.tableName, updateFiling);
  141. await conn.commit();
  142. return {file: updateFile, filing: updateFiling};
  143. } catch(err) {
  144. await conn.rollback();
  145. throw err;
  146. }
  147. }
  148. async search(filing_type, keyword, limit = 1000) {
  149. if (!filing_type || filing_type.length === 0 || !keyword) return [];
  150. const sql = `SELECT * FROM ${this.tableName}` +
  151. ` WHERE spid = ? and is_deleted = 0 and filing_type in (${filing_type.join(',')}) and filename like '%${keyword}%'`+
  152. ` ORDER BY update_time DESC LIMIT 0, ${limit}`;
  153. const result = await this.db.query(sql, [this.ctx.subProject.id]);
  154. this.analysisFiles(result);
  155. return result;
  156. }
  157. }
  158. return Filing;
  159. };