stage_shoufang.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. /**
  3. * 收方单-数据模型
  4. *
  5. * @author ellisran
  6. * @date 2021/09/23
  7. * @version
  8. */
  9. const accountGroup = require('../const/account_group').group;
  10. const fs = require('fs');
  11. const path = require('path');
  12. const qr = require('qr-image');
  13. const sendToWormhole = require('stream-wormhole');
  14. const auditConst = require('../const/audit').stage;
  15. module.exports = app => {
  16. class stageShoufang extends app.BaseService {
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'stage_shoufang';
  20. }
  21. async add(datas) {
  22. const transaction = await this.db.beginTransaction();
  23. try {
  24. const data = {
  25. tid: this.ctx.tender.id,
  26. order: this.ctx.stage.order,
  27. sid: this.ctx.stage.id,
  28. lid: datas.lid,
  29. pid: datas.pid ? datas.pid : null,
  30. extra_upload: this.ctx.stage.status === auditConst.status.checked ? 1 : 0,
  31. create_time: new Date(),
  32. };
  33. const result = await transaction.insert(this.tableName, data);
  34. // 生成二维码并保存
  35. const size = 5;
  36. const margin = 1;
  37. const text = this.ctx.protocol + '://' + this.ctx.host + '/wap/shoufang/upload' +
  38. '?tid=' + this.ctx.tender.id + '&order=' + this.ctx.stage.order + '&sfid=' + result.insertId;
  39. // 大小默认5,二维码周围间距默认1
  40. const qr_png = await qr.image(text || '', { type: 'png', size: size || 5, margin: margin || 1 });
  41. const qrcodePath = 'app/public/upload/' + this.ctx.tender.id + '/stage/shoufang/qrcode/' + result.insertId + '.png';
  42. // await this.ctx.helper.saveStreamFile(qr_png, path.resolve(this.app.baseDir, 'app/' + qrcodePath));
  43. await this.ctx.app.fujianOss.put(this.ctx.app.config.fujianOssFolder + qrcodePath, qr_png);
  44. await transaction.update(this.tableName, { id: result.insertId, qrcode: qrcodePath });
  45. await transaction.commit();
  46. return result;
  47. } catch (err) {
  48. await transaction.rollback();
  49. throw err;
  50. }
  51. }
  52. async del(sfid) {
  53. const transaction = await this.db.beginTransaction();
  54. try {
  55. // 删除附件再删除收方单
  56. const sfInfo = await this.getDataById(sfid);
  57. const sfAttList = await this.ctx.service.stageShoufangAtt.getAllDataByCondition({ where: { sfid } });
  58. await this.ctx.helper.delFiles(sfAttList);
  59. // if (sfAttList) {
  60. // if (sfAttList.length !== 0) {
  61. // for (const att of sfAttList) {
  62. // if (fs.existsSync(path.join(this.app.baseDir, att.filepath))) {
  63. // await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  64. // }
  65. // }
  66. // }
  67. // }
  68. // 删除二维码
  69. // if (fs.existsSync(path.join(this.app.baseDir, 'app/' + sfInfo.qrcode))) {
  70. // await fs.unlinkSync(path.join(this.app.baseDir, 'app/' + sfInfo.qrcode));
  71. // }
  72. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + sfInfo.qrcode);
  73. await transaction.delete(this.ctx.service.stageShoufangAtt.tableName, { sfid });
  74. await transaction.delete(this.tableName, { id: sfid });
  75. await transaction.commit();
  76. return true;
  77. } catch (err) {
  78. await transaction.rollback();
  79. throw err;
  80. }
  81. }
  82. }
  83. return stageShoufang;
  84. };