ledger_att.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. /**
  3. *
  4. * 附件
  5. * @author LanJianRong
  6. * @date 2021/7/30
  7. * @version
  8. */
  9. const archiver = require('archiver');
  10. const path = require('path');
  11. const fs = require('fs');
  12. module.exports = app => {
  13. class LedgerAtt extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'ledger_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. * @return {void}
  60. */
  61. async getDataByTenderId(tid) {
  62. const sql = 'SELECT att.id, att.lid, att.uid, att.filepath, att.filename, att.fileext, att.filesize, att.extra_upload, att.remark, att.in_time,' +
  63. ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`, leg.b_code as `b_code`' +
  64. ' FROM ?? AS att,?? AS pa,?? AS leg' +
  65. ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.tid = ? ORDER BY att.id DESC';
  66. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, tid];
  67. return await this.db.query(sql, sqlParam);
  68. }
  69. /**
  70. * 获取单个附件
  71. * @param {int} tid - 标段id
  72. * @param {int} sid - 当前期数
  73. * @return {void}
  74. */
  75. async getDataByFid(id) {
  76. const { ctx } = this;
  77. 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,' +
  78. ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`,leg.b_code as `b_code`' +
  79. ' FROM ?? AS att,?? AS pa,?? AS leg' +
  80. ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.id = ? ORDER BY att.in_time DESC';
  81. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, id];
  82. const result = await this.db.queryOne(sql, sqlParam);
  83. result.orginpath = this.ctx.app.config.fujianOssPath + result.filepath;
  84. if (!ctx.helper.canPreview(result.fileext)) {
  85. result.filepath = `/tender/${ctx.tender.id}/ledger/download/file/${result.id}`;
  86. } else {
  87. // result.filepath = result.filepath.replace(/^app|\/app/, '');
  88. result.filepath = this.ctx.app.config.fujianOssPath + result.filepath;
  89. }
  90. return result;
  91. }
  92. /**
  93. * 将文件压缩成zip,并返回zip文件的路径
  94. * @param {array} fileIds - 文件数组id
  95. * @param {string} zipPath - 压缩文件存储路径
  96. * @return {string} 压缩后的zip文件路径
  97. */
  98. async compressedFile(fileIds, zipPath) {
  99. this.initSqlBuilder();
  100. this.sqlBuilder.setAndWhere('id', {
  101. value: fileIds,
  102. operate: 'in',
  103. });
  104. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  105. const files = await this.db.query(sql, sqlParam);
  106. return new Promise((resolve, reject) => {
  107. // 每次开一个新的archiver
  108. const ziparchiver = archiver('zip');
  109. const outputPath = fs.createWriteStream(path.resolve(this.app.baseDir, zipPath));
  110. outputPath.on('error', err => {
  111. return reject(err);
  112. });
  113. ziparchiver.pipe(outputPath);
  114. files.forEach(item => {
  115. ziparchiver.file(item.filepath, { name: item.filename + item.fileext });
  116. });
  117. // 存档警告
  118. ziparchiver.on('warning', function(err) {
  119. // if (err.code === 'ENOENT') {
  120. // console.warn('stat故障和其他非阻塞错误');
  121. // }
  122. return reject(err);
  123. });
  124. // 存档出错
  125. ziparchiver.on('error', function(err) {
  126. // console.log(err);
  127. return reject(err);
  128. });
  129. ziparchiver.finalize();
  130. outputPath.on('close', () => {
  131. return resolve(ziparchiver.pointer());
  132. });
  133. });
  134. }
  135. async getViewDataByFid(id) {
  136. const sql = 'SELECT att.id, att.lid, att.uid, att.filepath, att.filename, att.fileext, att.filesize, att.extra_upload, att.remark, att.in_time,' +
  137. ' pa.name as `username`' +
  138. ' FROM ' + this.tableName + ' att Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On pa.id = att.uid' +
  139. ' WHERE att.id = ?';
  140. const result = await this.db.query(sql, [id]);
  141. for (const r of result) {
  142. r.filepath = this.ctx.app.config.fujianOssPath + r.filepath;
  143. if (this.ctx.helper.canPreview(r.fileext)) r.viewpath = r.filepath;
  144. r.in_time = this.ctx.moment(r.in_time * 1000).format('YYYY-MM-DD');
  145. }
  146. return result[0];
  147. }
  148. async getViewData(tid, settle_id = -1) {
  149. const sql = 'SELECT att.id, att.lid, att.uid, att.filepath, att.filename, att.fileext, att.filesize, att.extra_upload, att.remark, att.in_time,' +
  150. ' pa.name as `username`' +
  151. ' FROM ' + this.tableName + ' att Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On pa.id = att.uid' +
  152. ' WHERE att.tid = ? AND att.settle_id = ? ORDER BY att.id DESC';
  153. const result = await this.db.query(sql, [tid, settle_id]);
  154. for (const r of result) {
  155. r.filepath = this.ctx.app.config.fujianOssPath + r.filepath;
  156. if (this.ctx.helper.canPreview(r.fileext)) r.viewpath = r.filepath;
  157. r.in_time = this.ctx.moment(r.in_time * 1000).format('YYYY-MM-DD');
  158. }
  159. return result;
  160. }
  161. }
  162. return LedgerAtt;
  163. };