ledger_att.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 ' +
  65. ' LEFT JOIN ?? AS pa ON att.uid = pa.id ' +
  66. ' LEFT JOIN ?? AS leg ON leg.id = att.lid ' +
  67. ' WHERE att.tid = ? and att.revising = 0 and settle_id = -1' +
  68. ' ORDER BY att.id DESC';
  69. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, tid];
  70. return await this.db.query(sql, sqlParam);
  71. }
  72. /**
  73. * 获取单个附件
  74. * @param {int} tid - 标段id
  75. * @param {int} sid - 当前期数
  76. * @return {void}
  77. */
  78. async getDataByFid(id) {
  79. const { ctx } = this;
  80. 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,' +
  81. ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.ledger_id as `ledger_id`,leg.b_code as `b_code`' +
  82. ' FROM ?? AS att,?? AS pa,?? AS leg' +
  83. ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.id = ? ORDER BY att.in_time DESC';
  84. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, id];
  85. const result = await this.db.queryOne(sql, sqlParam);
  86. result.orginpath = this.ctx.app.config.fujianOssPath + result.filepath;
  87. if (!ctx.helper.canPreview(result.fileext)) {
  88. result.filepath = `/tender/${ctx.tender.id}/ledger/download/file/${result.id}`;
  89. } else {
  90. // result.filepath = result.filepath.replace(/^app|\/app/, '');
  91. result.filepath = this.ctx.app.config.fujianOssPath + result.filepath;
  92. }
  93. return result;
  94. }
  95. /**
  96. * 将文件压缩成zip,并返回zip文件的路径
  97. * @param {array} fileIds - 文件数组id
  98. * @param {string} zipPath - 压缩文件存储路径
  99. * @return {string} 压缩后的zip文件路径
  100. */
  101. async compressedFile(fileIds, zipPath) {
  102. this.initSqlBuilder();
  103. this.sqlBuilder.setAndWhere('id', {
  104. value: fileIds,
  105. operate: 'in',
  106. });
  107. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  108. const files = await this.db.query(sql, sqlParam);
  109. return new Promise((resolve, reject) => {
  110. // 每次开一个新的archiver
  111. const ziparchiver = archiver('zip');
  112. const outputPath = fs.createWriteStream(path.resolve(this.app.baseDir, zipPath));
  113. outputPath.on('error', err => {
  114. return reject(err);
  115. });
  116. ziparchiver.pipe(outputPath);
  117. files.forEach(item => {
  118. ziparchiver.file(item.filepath, { name: item.filename + item.fileext });
  119. });
  120. // 存档警告
  121. ziparchiver.on('warning', function(err) {
  122. // if (err.code === 'ENOENT') {
  123. // console.warn('stat故障和其他非阻塞错误');
  124. // }
  125. return reject(err);
  126. });
  127. // 存档出错
  128. ziparchiver.on('error', function(err) {
  129. // console.log(err);
  130. return reject(err);
  131. });
  132. ziparchiver.finalize();
  133. outputPath.on('close', () => {
  134. return resolve(ziparchiver.pointer());
  135. });
  136. });
  137. }
  138. async getViewDataByFid(id) {
  139. 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,' +
  140. ' pa.name as `username`' +
  141. ' FROM ' + this.tableName + ' att Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On pa.id = att.uid' +
  142. ' WHERE att.id = ?';
  143. const result = await this.db.query(sql, [id]);
  144. for (const r of result) {
  145. r.filepath = this.ctx.app.config.fujianOssPath + r.filepath;
  146. if (this.ctx.helper.canPreview(r.fileext)) r.viewpath = r.filepath;
  147. r.in_time = this.ctx.moment(r.in_time * 1000).format('YYYY-MM-DD');
  148. }
  149. return result[0];
  150. }
  151. async getViewData(tid, settle_id = -1) {
  152. 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,' +
  153. ' pa.name as `username`' +
  154. ' FROM ' + this.tableName + ' att Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On pa.id = att.uid' +
  155. ' WHERE att.tid = ? AND att.settle_id = ? ORDER BY att.id DESC';
  156. const result = await this.db.query(sql, [tid, settle_id]);
  157. for (const r of result) {
  158. r.filepath = this.ctx.app.config.fujianOssPath + r.filepath;
  159. if (this.ctx.helper.canPreview(r.fileext)) r.viewpath = r.filepath;
  160. r.in_time = this.ctx.moment(r.in_time * 1000).format('YYYY-MM-DD');
  161. }
  162. return result;
  163. }
  164. async getReviseViewData(tid, rid) {
  165. 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,' +
  166. ' pa.name as `username`' +
  167. ' FROM ' + this.tableName + ' att Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On pa.id = att.uid' +
  168. ' WHERE att.tid = ? AND att.revise_id = ? ORDER BY att.id DESC';
  169. const result = await this.db.query(sql, [tid, rid]);
  170. for (const r of result) {
  171. r.filepath = this.ctx.app.config.fujianOssPath + r.filepath;
  172. if (this.ctx.helper.canPreview(r.fileext)) r.viewpath = r.filepath;
  173. r.in_time = this.ctx.moment(r.in_time * 1000).format('YYYY-MM-DD');
  174. }
  175. return result;
  176. }
  177. async getLedgerViewData(tid) {
  178. 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,' +
  179. ' pa.name as `username`' +
  180. ' FROM ' + this.tableName + ' att Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On pa.id = att.uid' +
  181. ' WHERE att.tid = ? AND att.revising = 0 ORDER BY att.id DESC';
  182. const result = await this.db.query(sql, [tid]);
  183. for (const r of result) {
  184. r.filepath = this.ctx.app.config.fujianOssPath + r.filepath;
  185. if (this.ctx.helper.canPreview(r.fileext)) r.viewpath = r.filepath;
  186. r.in_time = this.ctx.moment(r.in_time * 1000).format('YYYY-MM-DD');
  187. }
  188. return result;
  189. }
  190. }
  191. return LedgerAtt;
  192. };