'use strict'; /** * 收方单-数据模型 * * @author ellisran * @date 2021/09/23 * @version */ const accountGroup = require('../const/account_group').group; const fs = require('fs'); const path = require('path'); const qr = require('qr-image'); const sendToWormhole = require('stream-wormhole'); const auditConst = require('../const/audit').stage; module.exports = app => { class stageShoufang extends app.BaseService { constructor(ctx) { super(ctx); this.tableName = 'stage_shoufang'; } async add(datas) { const transaction = await this.db.beginTransaction(); try { const data = { tid: this.ctx.tender.id, order: this.ctx.stage.order, sid: this.ctx.stage.id, lid: datas.lid, pid: datas.pid ? datas.pid : null, extra_upload: this.ctx.stage.status === auditConst.status.checked ? 1 : 0, create_time: new Date(), }; const result = await transaction.insert(this.tableName, data); // 生成二维码并保存 const size = 5; const margin = 1; const text = this.ctx.protocol + '://' + this.ctx.host + '/wap/shoufang/upload' + '?tid=' + this.ctx.tender.id + '&order=' + this.ctx.stage.order + '&sfid=' + result.insertId; // 大小默认5,二维码周围间距默认1 const qr_png = await qr.image(text || '', { type: 'png', size: size || 5, margin: margin || 1 }); const qrcodePath = 'app/public/upload/' + this.ctx.tender.id + '/stage/shoufang/qrcode/' + result.insertId + '.png'; // await this.ctx.helper.saveStreamFile(qr_png, path.resolve(this.app.baseDir, 'app/' + qrcodePath)); await this.ctx.app.fujianOss.put(this.ctx.app.config.fujianOssFolder + qrcodePath, qr_png); await transaction.update(this.tableName, { id: result.insertId, qrcode: qrcodePath }); await transaction.commit(); return result; } catch (err) { await transaction.rollback(); throw err; } } async del(sfid) { const transaction = await this.db.beginTransaction(); try { // 删除附件再删除收方单 const sfInfo = await this.getDataById(sfid); const sfAttList = await this.ctx.service.stageShoufangAtt.getAllDataByCondition({ where: { sfid } }); await this.ctx.helper.delFiles(sfAttList); // if (sfAttList) { // if (sfAttList.length !== 0) { // for (const att of sfAttList) { // if (fs.existsSync(path.join(this.app.baseDir, att.filepath))) { // await fs.unlinkSync(path.join(this.app.baseDir, att.filepath)); // } // } // } // } // 删除二维码 // if (fs.existsSync(path.join(this.app.baseDir, 'app/' + sfInfo.qrcode))) { // await fs.unlinkSync(path.join(this.app.baseDir, 'app/' + sfInfo.qrcode)); // } await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + sfInfo.qrcode); await transaction.delete(this.ctx.service.stageShoufangAtt.tableName, { sfid }); await transaction.delete(this.tableName, { id: sfid }); await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } } return stageShoufang; };