payment_rpt_audit.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. /**
  3. * 版本数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/25
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. module.exports = app => {
  11. class PaymentRptAudit 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_rpt_audit';
  21. }
  22. async updateSignatureMsg(td_id, uid, signature_msg) {
  23. const transaction = await this.db.beginTransaction();
  24. try {
  25. // 判断是否是审批人或非它审批阶段签字签章,如果不是则需要更新到detail中
  26. const rptAudit = await this.getDataByCondition({ td_id, uid });
  27. if (!rptAudit) {
  28. throw '当前人不存在报表角色中';
  29. }
  30. if ((this.ctx.detail.status !== auditConst.status.uncheck && this.ctx.detail.status !== auditConst.status.checkNo && this.ctx.detail.uid === uid) ||
  31. (this._.findIndex(this.ctx.detail.auditors, { aid: uid }) === -1 && this.ctx.detail.uid !== uid) ||
  32. (this._.findIndex(this.ctx.detail.auditors, { aid: uid }) !== -1 && this.ctx.detail.curAuditor && this.ctx.detail.curAuditor.aid !== uid)) {
  33. let report_json = JSON.parse(this.ctx.detail.report_json);
  34. report_json = await this.ctx.service.paymentDetail.signOneSignatureData(report_json, rptAudit.signature_name, signature_msg);
  35. // 同步期信息
  36. await transaction.update(this.ctx.service.paymentDetail.tableName, {
  37. id: this.ctx.detail.id,
  38. report_json: JSON.stringify(report_json),
  39. });
  40. }
  41. await transaction.update(this.tableName, { sign_time: new Date(), signature_msg: JSON.stringify(signature_msg) }, { where: { td_id, uid } });
  42. await transaction.commit();
  43. return true;
  44. } catch (error) {
  45. await transaction.rollback();
  46. throw error;
  47. }
  48. }
  49. async clearSignatureMsg(transaction, td_id, uid) {
  50. return await transaction.update(this.tableName, { sign_time: null, signature_msg: null }, { where: { td_id, uid } });
  51. }
  52. async updateAllAuditList(transaction, td_id, newRptList) {
  53. const oldRptList = await this.getAllDataByCondition({ where: { td_id }, orders: [['id', 'asc']] });
  54. const updateData = [];
  55. for (const audit of oldRptList) {
  56. const uData = {
  57. id: audit.id,
  58. signature_msg: null,
  59. sign_time: null,
  60. };
  61. if (audit.uid !== newRptList[audit.signature_index].uid) {
  62. uData.uid = newRptList[audit.signature_index].uid;
  63. }
  64. updateData.push(uData);
  65. }
  66. await transaction.updateRows(this.tableName, updateData);
  67. }
  68. async getListByDetail(td_id) {
  69. const sql = 'SELECT pra.*, pa.name as user_name FROM ?? as pra LEFT JOIN ?? as pa ON pra.`uid` = pa.`id` WHERE td_id = ?';
  70. const params = [this.tableName, this.ctx.service.projectAccount.tableName, td_id];
  71. const list = await this.db.query(sql, params);
  72. if (list.length > 0) {
  73. for (const ra of list) {
  74. ra.signature_msg = ra.signature_msg ? JSON.parse(ra.signature_msg) : null;
  75. }
  76. }
  77. return list;
  78. }
  79. async haveEmptySign(td_id) {
  80. const count = await this.count({ td_id, signature_msg: null });
  81. return count > 0;
  82. }
  83. }
  84. return PaymentRptAudit;
  85. };