12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- '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');
- 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,
- 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 = '/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 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 } });
- 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 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;
- };
|