change_att.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. /**
  3. *
  4. * 附件
  5. * @author Ellisran
  6. * @date 2019/1/11
  7. * @version
  8. */
  9. module.exports = app => {
  10. class ChangeAtt extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'change_attachment';
  20. }
  21. /**
  22. * 添加附件
  23. * @param {Object} postData - 表单信息
  24. * @param {Object} fileData - 文件信息
  25. * @param {int} uid - 上传者id
  26. * @return {void}
  27. */
  28. async save(postData, fileData, uid) {
  29. const data = {
  30. tid: postData.tid,
  31. cid: postData.cid,
  32. uid,
  33. };
  34. Object.assign(data, fileData);
  35. const result = await this.db.insert(this.tableName, data);
  36. return result;
  37. }
  38. /**
  39. * 获取 变更令 所有附件
  40. * @param {uuid} cid - 变更令id
  41. * @return {Promise<void>}
  42. */
  43. async getChangeAttachment(cid) {
  44. const sql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
  45. ' FROM ?? As ca ' +
  46. ' Left Join ?? As pa ' +
  47. ' On ca.uid = pa.id ' +
  48. ' Where ca.cid = ?';
  49. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid];
  50. return await this.db.query(sql, sqlParam);
  51. }
  52. /**
  53. * 获取所有附件
  54. * @param {String} cid 变更令id
  55. */
  56. async getAllChangeFiles(cid) {
  57. const { ctx } = this;
  58. const result = await this.db.select(this.tableName, { where: { cid } });
  59. return result.map(item => {
  60. if (!ctx.helper.canPreview(item.fileext)) {
  61. item.filepath = `/change/download/file/${item.id}`;
  62. } else {
  63. item.filepath = '/' + item.filepath;
  64. }
  65. return item;
  66. });
  67. }
  68. }
  69. return ChangeAtt;
  70. };