contract_att.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. /**
  3. * Created by EllisRan on 2020/3/3.
  4. */
  5. const BaseService = require('../base/base_service');
  6. const contractConst = require('../const/contract');
  7. module.exports = app => {
  8. class ContractAtt extends BaseService {
  9. /**
  10. * 构造函数
  11. *
  12. * @param {Object} ctx - egg全局变量
  13. * @return {void}
  14. */
  15. constructor(ctx) {
  16. super(ctx);
  17. this.tableName = 'contract_attachment';
  18. this.dataId = 'id';
  19. }
  20. async getAtt(cid) {
  21. const sql = 'SELECT a.*, b.name as username FROM ?? as a LEFT JOIN ?? as b ON a.`uid` = b.`id` WHERE a.`cid` = ? ORDER BY `upload_time` DESC';
  22. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid];
  23. const result = await this.db.query(sql, sqlParam);
  24. return result.map(item => {
  25. item.orginpath = this.ctx.app.config.fujianOssPath + item.filepath;
  26. if (!this.ctx.helper.canPreview(item.fileext)) {
  27. item.filepath = `/contract/${item.spid || item.tid}/detail/${contractConst.typeMap[item.contract_type]}/${item.cid}/file/${item.id}/download`;
  28. } else {
  29. item.filepath = this.ctx.app.config.fujianOssPath + item.filepath;
  30. item.viewpath = item.filepath;
  31. }
  32. return item;
  33. });
  34. }
  35. /**
  36. * 存储上传的文件信息至数据库
  37. * @param {Array} payload 载荷
  38. * @return {Promise<void>} 数据库插入执行实例
  39. */
  40. async saveFileMsgToDb(payload) {
  41. return await this.db.insert(this.tableName, payload);
  42. }
  43. /**
  44. * 删除附件
  45. * @param {Number} id - 附件id
  46. * @return {void}
  47. */
  48. async delete(id) {
  49. return await this.deleteById(id);
  50. }
  51. }
  52. return ContractAtt;
  53. };