stage_att.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. const result = await this.db.query(sql, sqlParam);
  67. return result.map(item => {
  68. if (!ctx.helper.canPreview(item.fileext)) {
  69. item.filepath = `/tender/${ctx.tender.id}/measure/stage/${ctx.params.order}/download/file/${item.id}`;
  70. } else {
  71. item.filepath = '/' + item.filepath;
  72. }
  73. return item;
  74. });
  75. }
  76. /**
  77. * 获取单个附件
  78. * @param {int} tid - 标段id
  79. * @param {int} sid - 当前期数
  80. * @return {void}
  81. */
  82. async getDataByFid(id) {
  83. const { ctx } = this;
  84. 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,' +
  85. ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`,leg.b_code as `b_code`' +
  86. ' FROM ?? AS att,?? AS pa,?? AS leg' +
  87. ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.id = ? ORDER BY att.in_time DESC';
  88. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, id];
  89. const result = await this.db.queryOne(sql, sqlParam);
  90. if (!ctx.helper.canPreview(result.fileext)) result.filepath = `/tender/${ctx.tender.id}/measure/stage/${ctx.params.order}/download/file/${result.id}`;
  91. else result.filepath = '/' + result.filepath;
  92. return result;
  93. }
  94. }
  95. return StageAtt;
  96. };