stage_att.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';
  2. /**
  3. *
  4. * 附件
  5. * @author Ellisran
  6. * @date 2019/1/11
  7. * @version
  8. */
  9. const archiver = require('archiver');
  10. const path = require('path');
  11. const fs = require('fs');
  12. module.exports = app => {
  13. class StageAtt extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'stage_attachment';
  23. }
  24. /**
  25. * 添加附件
  26. * @param {Object} postData - 表单信息
  27. * @param {Object} fileData - 文件信息
  28. * @param {int} uid - 上传者id
  29. * @return {void}
  30. */
  31. async save(postData, fileData, uid) {
  32. const data = {
  33. lid: postData.lid,
  34. uid,
  35. remark: '',
  36. };
  37. Object.assign(data, fileData);
  38. const result = await this.db.insert(this.tableName, data);
  39. return result;
  40. }
  41. /**
  42. * 添加附件
  43. * @param {Object} postData - 表单信息
  44. * @param {Object} fileData - 文件信息
  45. * @param {int} uid - 上传者id
  46. * @return {void}
  47. */
  48. async updateByID(postData, fileData) {
  49. delete postData.size;
  50. const data = {};
  51. Object.assign(data, fileData);
  52. Object.assign(data, postData);
  53. const result = await this.db.update(this.tableName, data);
  54. return result.affectedRows === 1;
  55. }
  56. /**
  57. * 获取所有附件
  58. * @param {int} tid - 标段id
  59. * @param {int} sid - 当前期数
  60. * @return {void}
  61. */
  62. async getDataByTenderIdAndStageId(tid, sid) {
  63. const { ctx } = this;
  64. const sql = 'SELECT att.id, att.lid, att.uid, att.filename, att.fileext, att.filesize, att.re_upload, att.remark, att.in_time,' +
  65. ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`, leg.b_code as `b_code`' +
  66. ' FROM ?? AS att,?? AS pa,?? AS leg' +
  67. ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.tid = ? AND att.sid = ? ORDER BY att.id DESC';
  68. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, tid, sid];
  69. return await this.db.query(sql, sqlParam);
  70. }
  71. /**
  72. * 获取单个附件
  73. * @param {int} tid - 标段id
  74. * @param {int} sid - 当前期数
  75. * @return {void}
  76. */
  77. async getDataByFid(id) {
  78. const { ctx } = this;
  79. 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,' +
  80. ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`,leg.b_code as `b_code`' +
  81. ' FROM ?? AS att,?? AS pa,?? AS leg' +
  82. ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.id = ? ORDER BY att.in_time DESC';
  83. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, id];
  84. const result = await this.db.queryOne(sql, sqlParam);
  85. if (!ctx.helper.canPreview(result.fileext)) result.filepath = `/tender/${ctx.tender.id}/measure/stage/${ctx.params.order}/download/file/${result.id}`;
  86. else result.filepath = result.filepath.replace(/^app|\/app/, '');
  87. return result;
  88. }
  89. /**
  90. * 将文件压缩成zip,并返回zip文件的路径
  91. * @param {array} fileIds - 文件数组id
  92. * @param {string} zipPath - 压缩文件存储路径
  93. * @return {string} 压缩后的zip文件路径
  94. */
  95. async compressedFile(fileIds, zipPath) {
  96. this.initSqlBuilder();
  97. this.sqlBuilder.setAndWhere('id', {
  98. value: fileIds,
  99. operate: 'in',
  100. });
  101. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  102. const files = await this.db.query(sql, sqlParam);
  103. // const paths = files.map(item => {
  104. // return { name: item.filename + item.fileext, path: item.filepath }
  105. // })
  106. return new Promise(resolve => {
  107. // 每次开一个新的archiver
  108. const ziparchiver = archiver('zip');
  109. const outputPath = fs.createWriteStream(path.resolve(this.app.baseDir, zipPath));
  110. ziparchiver.pipe(outputPath);
  111. files.forEach(item => {
  112. ziparchiver.file(item.filepath, { name: item.filename + item.fileext });
  113. });
  114. // 存档警告
  115. ziparchiver.on('warning', function(err) {
  116. if (err.code === 'ENOENT') {
  117. console.warn('stat故障和其他非阻塞错误');
  118. } else {
  119. throw err;
  120. }
  121. });
  122. // 存档出错
  123. ziparchiver.on('error', function(err) {
  124. console.log(err);
  125. throw err;
  126. });
  127. ziparchiver.finalize();
  128. outputPath.on('close', () => {
  129. resolve(ziparchiver.pointer());
  130. });
  131. });
  132. }
  133. }
  134. return StageAtt;
  135. };