ledger_att.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. this._analysisAtt(result);
  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. return new Promise((resolve, reject) => {
  104. // 每次开一个新的archiver
  105. const ziparchiver = archiver('zip');
  106. const outputPath = fs.createWriteStream(path.resolve(this.app.baseDir, zipPath));
  107. outputPath.on('error', err => {
  108. return reject(err);
  109. });
  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. // }
  119. return reject(err);
  120. });
  121. // 存档出错
  122. ziparchiver.on('error', function(err) {
  123. // console.log(err);
  124. return reject(err);
  125. });
  126. ziparchiver.finalize();
  127. outputPath.on('close', () => {
  128. return resolve(ziparchiver.pointer());
  129. });
  130. });
  131. }
  132. _analysisAtt(data) {
  133. const datas = data instanceof Array ? data : [data];
  134. for (const r of datas) {
  135. r.orginpath = this.ctx.app.config.fujianOssPath + r.filepath;
  136. r.filepath = this.ctx.app.config.fujianOssPath + r.filepath;
  137. r.viewpath = this.ctx.helper.getPreviewPath(r.fileext, r.filepath);
  138. r.in_time = this.ctx.moment(r.in_time * 1000).format('YYYY-MM-DD');
  139. }
  140. }
  141. async getViewDataByFid(id) {
  142. 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,' +
  143. ' pa.name as `username`' +
  144. ' FROM ' + this.tableName + ' att Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On pa.id = att.uid' +
  145. ' WHERE att.id = ?';
  146. const result = await this.db.query(sql, [id]);
  147. this._analysisAtt(result);
  148. return result[0];
  149. }
  150. async getViewData(tid, settle_id = -1) {
  151. 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,' +
  152. ' pa.name as `username`' +
  153. ' FROM ' + this.tableName + ' att Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On pa.id = att.uid' +
  154. ' WHERE att.tid = ? AND att.settle_id = ? ORDER BY att.id DESC';
  155. const result = await this.db.query(sql, [tid, settle_id]);
  156. this._analysisAtt(result);
  157. return result;
  158. }
  159. async getReviseViewData(tid, rid) {
  160. 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,' +
  161. ' pa.name as `username`' +
  162. ' FROM ' + this.tableName + ' att Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On pa.id = att.uid' +
  163. ' WHERE att.tid = ? AND att.revise_id = ? ORDER BY att.id DESC';
  164. const result = await this.db.query(sql, [tid, rid]);
  165. this._analysisAtt(result);
  166. return result;
  167. }
  168. async getLedgerViewData(tid) {
  169. 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,' +
  170. ' pa.name as `username`' +
  171. ' FROM ' + this.tableName + ' att Left Join ' + this.ctx.service.projectAccount.tableName + ' pa On pa.id = att.uid' +
  172. ' WHERE att.tid = ? AND att.revising = 0 AND att.settle_id = -1 ORDER BY att.id DESC';
  173. const result = await this.db.query(sql, [tid]);
  174. this._analysisAtt(result);
  175. return result;
  176. }
  177. }
  178. return LedgerAtt;
  179. };