construction_att.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. module.exports = app => {
  7. class ConstructionAtt extends BaseService {
  8. /**
  9. * 构造函数
  10. *
  11. * @param {Object} ctx - egg全局变量
  12. * @return {void}
  13. */
  14. constructor(ctx) {
  15. super(ctx);
  16. this.tableName = 'construction_attachment';
  17. }
  18. /**
  19. * 添加附件
  20. * @param {Object} postData - 表单信息
  21. * @param {Object} fileData - 文件信息
  22. * @param {int} uid - 上传者id
  23. * @return {void}
  24. */
  25. async save(postData, fileData, uid) {
  26. const data = {
  27. tid: postData.tid,
  28. log_id: postData.log_id,
  29. uid,
  30. };
  31. Object.assign(data, fileData);
  32. const result = await this.db.insert(this.tableName, data);
  33. return result;
  34. }
  35. /**
  36. * 获取 详情 所有附件
  37. * @param {uuid} td_id - 详情id
  38. * @return {Promise<void>}
  39. */
  40. async getConstructionAttachment(log_id, sort = 'asc') {
  41. const sql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
  42. ' FROM ?? As ca ' +
  43. ' Left Join ?? As pa ' +
  44. ' On ca.uid = pa.id ' +
  45. ' Where ca.log_id = ? order by id ' + sort;
  46. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, log_id];
  47. const result = await this.db.query(sql, sqlParam);
  48. return result.map(item => {
  49. item.orginpath = this.ctx.app.config.fujianOssPath + item.filepath;
  50. if (!this.ctx.helper.canPreview(item.fileext)) {
  51. item.filepath = `/construction/${item.tid}/log/${item.log_id}/file/${item.id}/download`;
  52. } else {
  53. item.filepath = this.ctx.app.config.fujianOssPath + item.filepath;
  54. item.viewpath = item.filepath;
  55. }
  56. return item;
  57. });
  58. }
  59. }
  60. return ConstructionAtt;
  61. };