file.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class Filing extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @param {String} tableName - 表名
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'file';
  21. }
  22. analysisFiles(files) {
  23. const helper = this.ctx.helper;
  24. const userId = this.ctx.session.sessionUser.accountId;
  25. const ossPath = this.ctx.app.config.fujianOssPath;
  26. files.forEach(x => {
  27. x.viewpath = helper.canPreview(x.fileext) ? ossPath + x.filepath : '';
  28. x.filepath = ossPath + x.filepath;
  29. x.fileext_str = helper.fileExtStr(x.fileext);
  30. x.canEdit = x.user_id === userId;
  31. });
  32. }
  33. async getFiles(condition) {
  34. condition.orders = [['create_time', 'desc']];
  35. const result = await this.getAllDataByCondition(condition);
  36. this.analysisFiles(result);
  37. return result;
  38. }
  39. async addFiles(filing, fileInfo, user) {
  40. const conn = await this.db.beginTransaction();
  41. const result = {};
  42. try {
  43. const insertData = fileInfo.map(x => {
  44. return {
  45. id: this.uuid.v4(), spid: filing.spid, filing_id: filing.id, filing_type: filing.filing_type,
  46. user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  47. filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath,
  48. };
  49. });
  50. await conn.insert(this.tableName, insertData);
  51. const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
  52. await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
  53. await conn.commit();
  54. result.files = { id: insertData.map(x => { return x.id; })};
  55. result.filing = { id: filing.id, file_count: count };
  56. } catch (err) {
  57. await conn.rollback();
  58. throw err;
  59. }
  60. result.files = await this.getFiles({ where: result.files });
  61. return result;
  62. }
  63. async delFiles(files) {
  64. if (files.length === 0) return;
  65. const fileDatas = await this.getAllDataByCondition({ where: { id: files } });
  66. const filing = await this.ctx.service.filing.getDataById(fileDatas[0].filing_id);
  67. const result = {};
  68. const conn = await this.db.beginTransaction();
  69. try {
  70. const updateData = fileDatas.map(x => { return { id: x.id, is_deleted: 1 }; });
  71. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  72. const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
  73. await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
  74. await conn.commit();
  75. result.del = files;
  76. result.filing = { id: filing.id, file_count: count };
  77. } catch (err) {
  78. await conn.rollback();
  79. throw err;
  80. }
  81. return result;
  82. }
  83. async relaFiles(filing, fileInfo, user) {
  84. const conn = await this.db.beginTransaction();
  85. const result = {};
  86. try {
  87. const insertData = fileInfo.map(x => {
  88. return {
  89. id: this.uuid.v4(), spid: filing.spid, filing_id: filing.id, filing_type: filing.filing_type,
  90. user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  91. filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath,
  92. is_rela: 1, rela_info: x.rela_info,
  93. };
  94. });
  95. await conn.insert(this.tableName, insertData);
  96. const count = await conn.count(this.tableName, { filing_id: filing.id, is_deleted: 0 });
  97. await conn.update(this.ctx.service.filing.tableName, { id: filing.id, file_count: count });
  98. await conn.commit();
  99. result.files = { id: insertData.map(x => { return x.id; })};
  100. result.filing = { id: filing.id, file_count: count };
  101. } catch (err) {
  102. await conn.rollback();
  103. throw err;
  104. }
  105. result.files = await this.getFiles({ where: result.files });
  106. return result;
  107. }
  108. }
  109. return Filing;
  110. };