payment_shenpi_audit.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.paymentTender.id, tr_id, status);
  25. } else if (status === shenpiConst.sp_status.gdzs) {
  26. return await this.getAudit(this.ctx.paymentTender.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.paymentTender.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.paymentTender.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. async delDataFromtrids(transaction, tr_ids) {
  76. return await transaction.delete(this.tableName, { tr_id: tr_ids });
  77. }
  78. async removeAuditByReport(transaction, tr_id, audit_id) {
  79. return await transaction.delete(this.tableName, { tr_id, audit_id });
  80. }
  81. }
  82. return PaymentShenpiAudit;
  83. };