file.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. const result = {};
  74. const conn = await this.db.beginTransaction();
  75. try {
  76. const updateData = fileDatas.map(x => { return { id: x.id, is_deleted: 1 }; });
  77. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  78. const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
  79. await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
  80. await conn.commit();
  81. result.del = files;
  82. result.filing = { id: filing.id, file_count: count };
  83. } catch (err) {
  84. await conn.rollback();
  85. throw err;
  86. }
  87. return result;
  88. }
  89. async relaFiles(filing, fileInfo, user) {
  90. const conn = await this.db.beginTransaction();
  91. const result = {};
  92. try {
  93. const insertData = fileInfo.map(x => {
  94. return {
  95. id: this.uuid.v4(), spid: filing.spid, filing_id: filing.id, filing_type: filing.filing_type,
  96. user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  97. filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath,
  98. is_rela: 1, rela_info: JSON.stringify(x.rela_info),
  99. };
  100. });
  101. await conn.insert(this.tableName, insertData);
  102. const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
  103. await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
  104. await conn.commit();
  105. result.files = { id: insertData.map(x => { return x.id; })};
  106. result.filing = { id: filing.id, file_count: count };
  107. } catch (err) {
  108. await conn.rollback();
  109. throw err;
  110. }
  111. result.files = await this.getFiles({ where: result.files });
  112. return result;
  113. }
  114. async saveFile(id, filename){
  115. const file = await this.getDataById(id);
  116. if (!file) throw '文件不存在';
  117. if (file.user_id !== this.ctx.session.sessionUser.accountId) throw '您无权编辑该文件';
  118. const info = path.parse(filename);
  119. const updateData = { id, filename: info.name, fileext: info.ext};
  120. await this.defaultUpdate(updateData);
  121. return updateData;
  122. }
  123. async search(filing_type, keyword, limit = 1000) {
  124. if (!filing_type || filing_type.length === 0 || !keyword) return [];
  125. const sql = `SELECT * FROM ${this.tableName}` +
  126. ` WHERE spid = ? and is_deleted = 0 and filing_type in (${filing_type.join(',')}) and filename like '%${keyword}%'`+
  127. ` ORDER BY update_time DESC LIMIT 0, ${limit}`;
  128. const result = await this.db.query(sql, [this.ctx.subProject.id]);
  129. this.analysisFiles(result);
  130. return result;
  131. }
  132. }
  133. return Filing;
  134. };