stage_att.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. /**
  3. *
  4. * 附件
  5. * @author Ellisran
  6. * @date 2019/1/11
  7. * @version
  8. */
  9. module.exports = app => {
  10. class StageAtt extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'stage_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. lid: postData.lid,
  31. uid,
  32. remark: '',
  33. };
  34. Object.assign(data, fileData);
  35. const result = await this.db.insert(this.tableName, data);
  36. return result;
  37. }
  38. /**
  39. * 添加附件
  40. * @param {Object} postData - 表单信息
  41. * @param {Object} fileData - 文件信息
  42. * @param {int} uid - 上传者id
  43. * @return {void}
  44. */
  45. async updateByID(postData, fileData) {
  46. delete postData.size;
  47. const data = {};
  48. Object.assign(data, fileData);
  49. Object.assign(data, postData);
  50. const result = await this.db.update(this.tableName, data);
  51. return result.affectedRows === 1;
  52. }
  53. /**
  54. * 获取所有附件
  55. * @param {int} tid - 标段id
  56. * @param {int} sid - 当前期数
  57. * @return {void}
  58. */
  59. async getDataByTenderIdAndStageId(tid, sid) {
  60. const { ctx } = this;
  61. const sql = 'SELECT att.id, att.lid, att.uid, att.filename, att.fileext, att.filesize, att.re_upload, att.remark, att.in_time,' +
  62. ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`, leg.b_code as `b_code`' +
  63. ' FROM ?? AS att,?? AS pa,?? AS leg' +
  64. ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.tid = ? AND att.sid = ? ORDER BY att.id DESC';
  65. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, tid, sid];
  66. return await this.db.query(sql, sqlParam);
  67. }
  68. /**
  69. * 获取单个附件
  70. * @param {int} tid - 标段id
  71. * @param {int} sid - 当前期数
  72. * @return {void}
  73. */
  74. async getDataByFid(id) {
  75. const { ctx } = this;
  76. const sql = 'SELECT att.id, att.lid, att.uid, att.filepath, att.filename, att.re_upload, att.fileext, att.filesize, att.remark, att.in_time,' +
  77. ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`,leg.b_code as `b_code`' +
  78. ' FROM ?? AS att,?? AS pa,?? AS leg' +
  79. ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.id = ? ORDER BY att.in_time DESC';
  80. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, id];
  81. const result = await this.db.queryOne(sql, sqlParam);
  82. if (!ctx.helper.canPreview(result.fileext)) result.filepath = `/tender/${ctx.tender.id}/measure/stage/${ctx.params.order}/download/file/${result.id}`;
  83. else result.filepath = result.filepath.replace(/^app|\/app/, '');
  84. return result;
  85. }
  86. }
  87. return StageAtt;
  88. };