change_project_att.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. const archiver = require('archiver');
  3. const path = require('path');
  4. const fs = require('fs');
  5. /**
  6. * 附件表 数据模型
  7. * @author LanJianRong
  8. * @date 2020/6/30
  9. * @version
  10. */
  11. module.exports = app => {
  12. class ChangeProjectFile extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'change_project_attachment';
  22. }
  23. /**
  24. * 获取当前标段(期)所有上传的附件
  25. * @param {Number} tid 标段id
  26. * @param {Number?} mid 期id
  27. * @return {Promise<void>} 数据库查询实例
  28. */
  29. async getAllChangeProjectAtt(tid, cpid) {
  30. const { ctx } = this;
  31. // const where = { tid };
  32. // if (cpid) where.cpid = cpid;
  33. const sql = 'SELECT a.*,b.name as username FROM ?? as a LEFT JOIN ?? as b ON a.uid = b.id WHERE a.tid = ? AND a.cpid = ? ORDER BY upload_time DESC';
  34. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, cpid];
  35. const result = await this.db.query(sql, sqlParam);
  36. // const result = await this.db.select(this.tableName, {
  37. // where,
  38. // orders: [['upload_time', 'desc']],
  39. // });
  40. // for (const r of result) {
  41. // const userInfo = await this.ctx.service.projectAccount.getDataById(r.uid);
  42. // r.username = userInfo ? userInfo.name : '';
  43. // }
  44. return result.map(item => {
  45. item.orginpath = this.ctx.app.config.fujianOssPath + item.filepath;
  46. if (!ctx.helper.canPreview(item.fileext)) {
  47. item.filepath = `/tender/${ctx.tender.id}/change/project/${item.cpid}/information/file/${item.id}/download`;
  48. } else {
  49. item.filepath = this.ctx.app.config.fujianOssPath + item.filepath;
  50. }
  51. return item;
  52. });
  53. }
  54. /**
  55. * 存储上传的文件信息至数据库
  56. * @param {Array} payload 载荷
  57. * @return {Promise<void>} 数据库插入执行实例
  58. */
  59. async saveFileMsgToDb(payload) {
  60. return await this.db.insert(this.tableName, payload);
  61. }
  62. /**
  63. * 获取单个文件信息
  64. * @param {Number} id 文件id
  65. * @return {Promise<void>} 数据库查询实例
  66. */
  67. async getMaterialFileById(id) {
  68. return await this.getDataByCondition({ id });
  69. }
  70. /**
  71. * 删除附件
  72. * @param {Number} id - 附件id
  73. * @return {void}
  74. */
  75. async delete(id) {
  76. return await this.deleteById(id);
  77. }
  78. /**
  79. * 将文件压缩成zip,并返回zip文件的路径
  80. * @param {array} fileIds - 文件数组id
  81. * @param {string} zipPath - 压缩文件存储路径
  82. * @return {string} 压缩后的zip文件路径
  83. */
  84. async compressedFile(fileIds, zipPath) {
  85. this.initSqlBuilder();
  86. this.sqlBuilder.setAndWhere('id', {
  87. value: fileIds,
  88. operate: 'in',
  89. });
  90. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  91. const files = await this.db.query(sql, sqlParam);
  92. // const paths = files.map(item => {
  93. // return { name: item.filename + item.fileext, path: item.filepath }
  94. // })
  95. return new Promise((resolve, reject) => {
  96. // 每次开一个新的archiver
  97. const ziparchiver = archiver('zip');
  98. const outputPath = fs.createWriteStream(path.resolve(this.app.baseDir, zipPath));
  99. outputPath.on('error', err => {
  100. return reject(err);
  101. });
  102. ziparchiver.pipe(outputPath);
  103. files.forEach(item => {
  104. ziparchiver.file(path.resolve(this.app.baseDir, 'app', item.filepath), { name: item.file_name });
  105. });
  106. // 存档警告
  107. ziparchiver.on('warning', function(err) {
  108. // if (err.code === 'ENOENT') {
  109. // console.warn('stat故障和其他非阻塞错误');
  110. // }
  111. return reject(err);
  112. });
  113. // 存档出错
  114. ziparchiver.on('error', function(err) {
  115. // console.log(err);
  116. return reject(err);
  117. });
  118. ziparchiver.finalize();
  119. outputPath.on('close', () => {
  120. return resolve(ziparchiver.pointer());
  121. });
  122. });
  123. }
  124. }
  125. return ChangeProjectFile;
  126. };