'use strict'; /** * 版本数据模型 * * @author CaiAoLin * @date 2017/10/25 * @version */ const shenpiConst = require('../const/shenpi'); module.exports = app => { class PaymentShenpiAudit extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'payment_shenpi_audit'; } async getShenpiAudit(tr_id, status) { if (status === shenpiConst.sp_status.gdspl) { return await this.getAuditList(this.ctx.paymentTender.id, tr_id, status); } else if (status === shenpiConst.sp_status.gdzs) { return await this.getAudit(this.ctx.paymentTender.id, tr_id, status); } } async getAudit(tid, tr_id, status) { const sql = 'SELECT sp.audit_id, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' + ' WHERE sp.tid = ? AND sp.tr_id = ? AND sp.sp_status = ?'; const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, tr_id, status]; return await this.db.queryOne(sql, sqlParam); } async getAuditList(tid, tr_id, status) { const sql = 'SELECT sp.audit_id, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' + ' WHERE sp.tid = ? AND sp.tr_id = ? AND sp.sp_status = ? ORDER BY sp.id ASC'; const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, tr_id, status]; return await this.db.query(sql, sqlParam); } async addAudit(data) { const transaction = await this.db.beginTransaction(); try { const insertData = { tid: this.ctx.paymentTender.id, tr_id: data.tr_id, sp_status: data.status, audit_id: data.audit_id, }; await transaction.insert(this.tableName, insertData); await transaction.commit(); return await this.getShenpiAudit(data.tr_id, data.status); } catch (err) { await transaction.rollback(); throw err; } } async removeAudit(data) { const transaction = await this.db.beginTransaction(); try { const delData = { tid: this.ctx.paymentTender.id, tr_id: data.tr_id, sp_status: data.status, audit_id: data.audit_id, }; await transaction.delete(this.tableName, delData); await transaction.commit(); return await this.getShenpiAudit(data.tr_id, data.status); } catch (err) { await transaction.rollback(); throw err; } } async delDataFromtrids(transaction, tr_ids) { return await transaction.delete(this.tableName, { tr_id: tr_ids }); } async removeAuditByReport(transaction, tr_id, audit_id) { return await transaction.delete(this.tableName, { tr_id, audit_id }); } } return PaymentShenpiAudit; };