payment_rpt_audit.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. let sign_time = null;
  31. if ((this.ctx.detail.status !== auditConst.status.uncheck && this.ctx.detail.status !== auditConst.status.checkNo && this.ctx.detail.uid === uid) ||
  32. (this._.findIndex(this.ctx.detail.auditors, { aid: uid }) === -1 && this.ctx.detail.uid !== uid) ||
  33. (this._.findIndex(this.ctx.detail.auditors, { aid: uid }) !== -1 &&
  34. ((this.ctx.detail.status === auditConst.status.checked && !this.ctx.detail.curAuditor) ||
  35. (this.ctx.detail.curAuditor && this.ctx.detail.curAuditor.aid !== uid)))) {
  36. let report_json = JSON.parse(this.ctx.detail.report_json);
  37. report_json = await this.ctx.service.paymentDetail.signOneSignatureData(report_json, rptAudit.signature_name, signature_msg);
  38. // 同步期信息
  39. await transaction.update(this.ctx.service.paymentDetail.tableName, {
  40. id: this.ctx.detail.id,
  41. report_json: JSON.stringify(report_json),
  42. });
  43. sign_time = new Date();
  44. }
  45. await transaction.update(this.tableName, { sign_time, signature_msg: JSON.stringify(signature_msg) }, { where: { td_id, uid } });
  46. await transaction.commit();
  47. return true;
  48. } catch (error) {
  49. await transaction.rollback();
  50. throw error;
  51. }
  52. }
  53. async updateSignTime(transaction, id) {
  54. return await transaction.update(this.tableName, { id, sign_time: new Date() });
  55. }
  56. async clearSignatureMsg(transaction, td_id, uid) {
  57. return await transaction.update(this.tableName, { sign_time: null, signature_msg: null }, { where: { td_id, uid } });
  58. }
  59. async updateAllAuditList(transaction, td_id, newRptList) {
  60. const oldRptList = await this.getAllDataByCondition({ where: { td_id }, orders: [['id', 'asc']] });
  61. const updateData = [];
  62. for (const audit of oldRptList) {
  63. const uData = {
  64. id: audit.id,
  65. signature_msg: null,
  66. sign_time: null,
  67. };
  68. if (audit.uid !== newRptList[audit.signature_index].uid) {
  69. uData.uid = newRptList[audit.signature_index].uid;
  70. }
  71. updateData.push(uData);
  72. }
  73. await transaction.updateRows(this.tableName, updateData);
  74. }
  75. async getListByDetail(td_id) {
  76. const sql = 'SELECT pra.*, pa.name as user_name FROM ?? as pra LEFT JOIN ?? as pa ON pra.`uid` = pa.`id` WHERE td_id = ?';
  77. const params = [this.tableName, this.ctx.service.projectAccount.tableName, td_id];
  78. const list = await this.db.query(sql, params);
  79. if (list.length > 0) {
  80. for (const ra of list) {
  81. ra.signature_msg = ra.signature_msg ? JSON.parse(ra.signature_msg) : null;
  82. }
  83. }
  84. return list;
  85. }
  86. async haveEmptySign(td_id) {
  87. const count = await this.count({ td_id, signature_msg: null });
  88. return count > 0;
  89. }
  90. }
  91. return PaymentRptAudit;
  92. };