quality_file.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. /**
  3. * 质量管理 - 工程质量
  4. *
  5. * @author Mai
  6. * @date 2024/7/22
  7. * @version
  8. */
  9. const path = require('path');
  10. module.exports = app => {
  11. class QualityFile extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'quality_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.ossUrl;
  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', 'asc']];
  35. const result = await this.getAllDataByCondition(condition);
  36. this.analysisFiles(result);
  37. return result;
  38. }
  39. async getBlockFiles(quality, blockType, blockId) {
  40. if (!quality) throw '参数错误';
  41. // const where = { tid: quality.tid, quality_id: quality.id };
  42. const where = { quality_id: quality.id, is_deleted: 0 };
  43. if (blockType) where.block_type = blockType;
  44. if (blockId) where.block_id = blockId;
  45. return this.getFiles({ where });
  46. }
  47. async addFiles(user, quality, fileInfo, blockType, blockId = '', specType = '') {
  48. const insertData = fileInfo.map(x => {
  49. return {
  50. id: this.uuid.v4(), tid: quality.tid, quality_id: quality.id,
  51. rela_type: quality.rela_type, rela_id: quality.rela_id,
  52. block_type: blockType, block_id: blockId, spec_type: specType,
  53. user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  54. filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath,
  55. };
  56. });
  57. await this.db.insert(this.tableName, insertData);
  58. return await this.getFiles({ where: { id: insertData.map(x => { return x.id; })} });
  59. }
  60. async delFiles(files) {
  61. if (files.length === 0) return;
  62. const fileDatas = await this.getAllDataByCondition({ where: { id: files } });
  63. const updateData = fileDatas.map(x => { return { id: x.id, is_deleted: 1 }; });
  64. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  65. return files;
  66. }
  67. async saveFile(id, filename){
  68. const file = await this.getDataById(id);
  69. if (!file) throw '文件不存在';
  70. if (file.user_id !== this.ctx.session.sessionUser.accountId) throw '您无权编辑该文件';
  71. const info = path.parse(filename);
  72. const updateData = { id, filename: info.name, fileext: info.ext};
  73. await this.defaultUpdate(updateData);
  74. return updateData;
  75. }
  76. }
  77. return QualityFile;
  78. };