file.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 addFiles(filing, fileInfo, user) {
  41. const conn = await this.db.beginTransaction();
  42. const result = {};
  43. try {
  44. const insertData = fileInfo.map(x => {
  45. return {
  46. id: this.uuid.v4(), spid: filing.spid, filing_id: filing.id, filing_type: filing.filing_type,
  47. user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  48. filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath,
  49. };
  50. });
  51. await conn.insert(this.tableName, insertData);
  52. const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
  53. await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
  54. await conn.commit();
  55. result.files = { id: insertData.map(x => { return x.id; })};
  56. result.filing = { id: filing.id, file_count: count };
  57. } catch (err) {
  58. await conn.rollback();
  59. throw err;
  60. }
  61. result.files = await this.getFiles({ where: result.files });
  62. return result;
  63. }
  64. async delFiles(files) {
  65. if (files.length === 0) return;
  66. const fileDatas = await this.getAllDataByCondition({ where: { id: files } });
  67. const filing = await this.ctx.service.filing.getDataById(fileDatas[0].filing_id);
  68. const result = {};
  69. const conn = await this.db.beginTransaction();
  70. try {
  71. const updateData = fileDatas.map(x => { return { id: x.id, is_deleted: 1 }; });
  72. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  73. const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
  74. await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
  75. await conn.commit();
  76. result.del = files;
  77. result.filing = { id: filing.id, file_count: count };
  78. } catch (err) {
  79. await conn.rollback();
  80. throw err;
  81. }
  82. return result;
  83. }
  84. async relaFiles(filing, fileInfo, user) {
  85. const conn = await this.db.beginTransaction();
  86. const result = {};
  87. try {
  88. const insertData = fileInfo.map(x => {
  89. return {
  90. id: this.uuid.v4(), spid: filing.spid, filing_id: filing.id, filing_type: filing.filing_type,
  91. user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  92. filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath,
  93. is_rela: 1, rela_info: x.rela_info,
  94. };
  95. });
  96. await conn.insert(this.tableName, insertData);
  97. const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
  98. await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
  99. await conn.commit();
  100. result.files = { id: insertData.map(x => { return x.id; })};
  101. result.filing = { id: filing.id, file_count: count };
  102. } catch (err) {
  103. await conn.rollback();
  104. throw err;
  105. }
  106. result.files = await this.getFiles({ where: result.files });
  107. return result;
  108. }
  109. async saveFile(id, filename){
  110. const file = await this.getDataById(id);
  111. if (!file) throw '文件不存在';
  112. if (file.user_id !== this.ctx.session.sessionUser.accountId) throw '您无权编辑该文件';
  113. const info = path.parse(filename);
  114. const updateData = { id, filename: info.name, fileext: info.ext};
  115. await this.defaultUpdate(updateData);
  116. return updateData;
  117. }
  118. }
  119. return Filing;
  120. };