payment_shenpi_audit.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. /**
  3. * 版本数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/25
  7. * @version
  8. */
  9. const shenpiConst = require('../const/shenpi');
  10. module.exports = app => {
  11. class PaymentShenpiAudit extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'payment_shenpi_audit';
  21. }
  22. async getShenpiAudit(tr_id, status) {
  23. if (status === shenpiConst.sp_status.gdspl) {
  24. return await this.getAuditList(this.ctx.tender.id, tr_id, status);
  25. } else if (status === shenpiConst.sp_status.gdzs) {
  26. return await this.getAudit(this.ctx.tender.id, tr_id, status);
  27. }
  28. }
  29. async getAudit(tid, tr_id, status) {
  30. const sql = 'SELECT sp.audit_id, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  31. ' WHERE sp.tid = ? AND sp.tr_id = ? AND sp.sp_status = ?';
  32. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, tr_id, status];
  33. return await this.db.queryOne(sql, sqlParam);
  34. }
  35. async getAuditList(tid, tr_id, status) {
  36. const sql = 'SELECT sp.audit_id, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +
  37. ' WHERE sp.tid = ? AND sp.tr_id = ? AND sp.sp_status = ? ORDER BY sp.id ASC';
  38. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, tr_id, status];
  39. return await this.db.query(sql, sqlParam);
  40. }
  41. async addAudit(data) {
  42. const transaction = await this.db.beginTransaction();
  43. try {
  44. const insertData = {
  45. tid: this.ctx.tender.id,
  46. tr_id: data.tr_id,
  47. sp_status: data.status,
  48. audit_id: data.audit_id,
  49. };
  50. await transaction.insert(this.tableName, insertData);
  51. await transaction.commit();
  52. return await this.getShenpiAudit(data.tr_id, data.status);
  53. } catch (err) {
  54. await transaction.rollback();
  55. throw err;
  56. }
  57. }
  58. async removeAudit(data) {
  59. const transaction = await this.db.beginTransaction();
  60. try {
  61. const delData = {
  62. tid: this.ctx.tender.id,
  63. tr_id: data.tr_id,
  64. sp_status: data.status,
  65. audit_id: data.audit_id,
  66. };
  67. await transaction.delete(this.tableName, delData);
  68. await transaction.commit();
  69. return await this.getShenpiAudit(data.tr_id, data.status);
  70. } catch (err) {
  71. await transaction.rollback();
  72. throw err;
  73. }
  74. }
  75. }
  76. return PaymentShenpiAudit;
  77. };