quality_inspection_att.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 QualityInspectionAtt extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'quality_inspection_attachment';
  22. }
  23. /**
  24. * 获取当前标段(期)所有上传的附件
  25. * @param {Number} tid 标段id
  26. * @param {Number?} mid 期id
  27. * @return {Promise<void>} 数据库查询实例
  28. */
  29. async getAllAtt(tid, qiid) {
  30. const { ctx } = this;
  31. // qiid 如果qiid只有一个就转成数组
  32. if (!tid || !qiid) {
  33. return [];
  34. }
  35. qiid = qiid instanceof Array ? qiid : [qiid];
  36. 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.qiid in (' + ctx.helper.getInArrStrSqlFilter(qiid) + ') ORDER BY upload_time DESC';
  37. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid];
  38. const result = await this.db.query(sql, sqlParam);
  39. return result.map(item => {
  40. item.orginpath = this.ctx.app.config.fujianOssPath + item.filepath;
  41. if (!ctx.helper.canPreview(item.fileext)) {
  42. item.filepath = `/tender/${ctx.tender.id}/change/plan/${item.cpid}/information/file/${item.id}/download`;
  43. } else {
  44. item.filepath = this.ctx.app.config.fujianOssPath + item.filepath;
  45. }
  46. return item;
  47. });
  48. }
  49. /**
  50. * 存储上传的文件信息至数据库
  51. * @param {Array} payload 载荷
  52. * @return {Promise<void>} 数据库插入执行实例
  53. */
  54. async saveFileMsgToDb(payload) {
  55. return await this.db.insert(this.tableName, payload);
  56. }
  57. /**
  58. * 获取单个文件信息
  59. * @param {Number} id 文件id
  60. * @return {Promise<void>} 数据库查询实例
  61. */
  62. async getMaterialFileById(id) {
  63. return await this.getDataByCondition({ id });
  64. }
  65. /**
  66. * 删除附件
  67. * @param {Number} id - 附件id
  68. * @return {void}
  69. */
  70. async delete(id) {
  71. return await this.deleteById(id);
  72. }
  73. }
  74. return QualityInspectionAtt;
  75. };