stage_att.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 sql = 'SELECT att.id, att.lid, att.uid, att.filename, att.fileext, att.filesize, att.extra_upload, att.remark, att.in_time,' +
  64. ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`, leg.b_code as `b_code`' +
  65. ' FROM ?? AS att,?? AS pa,?? AS leg' +
  66. ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.tid = ? AND att.sid = ? ORDER BY att.id DESC';
  67. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, tid, sid];
  68. return await this.db.query(sql, sqlParam);
  69. }
  70. /**
  71. * 获取单个附件
  72. * @param {int} tid - 标段id
  73. * @param {int} sid - 当前期数
  74. * @return {void}
  75. */
  76. async getDataByFid(id) {
  77. const { ctx } = this;
  78. const sql = 'SELECT att.id, att.lid, att.uid, att.filepath, att.filename, att.extra_upload, att.fileext, att.filesize, att.remark, att.in_time,' +
  79. ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`,leg.b_code as `b_code`' +
  80. ' FROM ?? AS att,?? AS pa,?? AS leg' +
  81. ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.id = ? ORDER BY att.in_time DESC';
  82. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, id];
  83. const result = await this.db.queryOne(sql, sqlParam);
  84. if (!ctx.helper.canPreview(result.fileext)) result.filepath = `/tender/${ctx.tender.id}/measure/stage/${ctx.params.order}/download/file/${result.id}`;
  85. else result.filepath = result.filepath.replace(/^app|\/app/, '');
  86. return result;
  87. }
  88. /**
  89. * 将文件压缩成zip,并返回zip文件的路径
  90. * @param {array} fileIds - 文件数组id
  91. * @param {string} zipPath - 压缩文件存储路径
  92. * @return {string} 压缩后的zip文件路径
  93. */
  94. async compressedFile(fileIds, zipPath) {
  95. this.initSqlBuilder();
  96. this.sqlBuilder.setAndWhere('id', {
  97. value: fileIds,
  98. operate: 'in',
  99. });
  100. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  101. const files = await this.db.query(sql, sqlParam);
  102. // const paths = files.map(item => {
  103. // return { name: item.filename + item.fileext, path: item.filepath }
  104. // })
  105. return new Promise((resolve, reject) => {
  106. // 每次开一个新的archiver
  107. const ziparchiver = archiver('zip');
  108. const outputPath = fs.createWriteStream(path.resolve(this.app.baseDir, zipPath));
  109. outputPath.on('error', err => {
  110. reject(err);
  111. });
  112. ziparchiver.pipe(outputPath);
  113. files.forEach(item => {
  114. ziparchiver.file(item.filepath, { name: item.filename + item.fileext });
  115. });
  116. // 存档警告
  117. ziparchiver.on('warning', function(err) {
  118. if (err.code === 'ENOENT') {
  119. console.warn('stat故障和其他非阻塞错误');
  120. }
  121. reject(err);
  122. });
  123. // 存档出错
  124. ziparchiver.on('error', function(err) {
  125. console.log(err);
  126. reject(err);
  127. });
  128. ziparchiver.finalize();
  129. outputPath.on('close', () => {
  130. resolve(ziparchiver.pointer());
  131. });
  132. });
  133. }
  134. }
  135. return StageAtt;
  136. };