settle_audit.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. /**
  3. * 与期不同,含原报
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. const auditType = auditConst.auditType;
  11. module.exports = app => {
  12. class SettleAudit extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'settle_audit';
  22. }
  23. async getAuditors(settleId, times) {
  24. return await this.getAllDataByCondition({ where: { settle_id: settleId, audit_times: times } }); // 全部参与的审批人
  25. }
  26. async getAuditorGroup(settleId, times) {
  27. const auditors = await this.getAuditors(settleId, times); // 全部参与的审批人
  28. return this.ctx.helper.groupAuditors(auditors, 'active_order');
  29. }
  30. async getUniqAuditors(settle) {
  31. const auditors = await this.getAuditors(settle.id, settle.audit_times); // 全部参与的审批人
  32. const result = [];
  33. auditors.forEach(x => {
  34. if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === x.audit_order; }) < 0) {
  35. result.push(x);
  36. }
  37. });
  38. return result;
  39. }
  40. // 去重
  41. async getUniqAuditorsGroup(settleId, times) {
  42. const group = await this.getAuditorGroup(settleId, times);
  43. return this.ctx.helper.groupAuditorsUniq(group);
  44. }
  45. async getAuditorsByStatus(settleId, status, times) {
  46. const cur = await this.db.queryOne(`SELECT * From ${this.tableName} where settle_id = ? AND audit_times = ? AND audit_status = ? ORDER By audit_times DESC, audit_order DESC `, [settleId, times, status]);
  47. if (!cur) return [];
  48. return await this.getAllDataByCondition({ where: { settle_id: settleId, audit_times: times, audit_order: cur.audit_order}});
  49. }
  50. async getAuditorHistory(settleId, times, reverse = false) {
  51. const history = [];
  52. if (times >= 1) {
  53. for (let i = 1; i <= times; i++) {
  54. const auditors = await this.getAuditors(settleId, i);
  55. const group = this.ctx.helper.groupAuditors(auditors);
  56. const historyGroup = [];
  57. const max_order = group.length > 0 && group[group.length - 1].length > 0 ? group[group.length - 1][0].audit_order : -1;
  58. for (const g of group) {
  59. const his = {
  60. auditYear: '', auditDate: '', auditTime: '', audit_time: null,
  61. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  62. auditors: g
  63. };
  64. if (his.audit_type === auditType.key.common) {
  65. his.name = g[0].name;
  66. } else {
  67. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  68. }
  69. his.is_final = his.audit_order === max_order;
  70. let audit_time;
  71. g.forEach(x => {
  72. if (x.status === auditConst.settle.status.checkSkip) return;
  73. if (!his.status || x.status === auditConst.settle.status.checking) his.audit_status = x.audit_status;
  74. if (x.audit_time && (!audit_time || x.audit_time > audit_time)) {
  75. audit_time = x.audit_time;
  76. if (his.status !== auditConst.settle.status.checking) his.audit_status = x.audit_status;
  77. }
  78. });
  79. if (audit_time) {
  80. his.audit_time = audit_time;
  81. const auditTime = this.ctx.moment(audit_time);
  82. his.auditYear = auditTime.format('YYYY');
  83. his.auditDate = auditTime.format('MM-DD');
  84. his.auditTime = auditTime.format('HH:mm:ss');
  85. }
  86. historyGroup.push(his);
  87. }
  88. if (reverse) {
  89. history.push(historyGroup.reverse());
  90. } else {
  91. history.push(historyGroup);
  92. }
  93. }
  94. }
  95. return history;
  96. }
  97. async copyPreAuditors(transaction, preSettle, newSettle) {
  98. const auditors = preSettle ? await this.getUniqAuditors(preSettle) : [];
  99. const newAuditors = [];
  100. // 添加原报
  101. const user = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  102. newAuditors.push({
  103. tid: newSettle.tid, settle_id: newSettle.id,
  104. audit_id: this.ctx.session.sessionUser.accountId,
  105. audit_times: 1, audit_order: 0, audit_type: auditConst.auditType.key.common,
  106. active_order: 0, audit_status: auditConst.settle.status.uncheck,
  107. name: user.name, company: user.company, role: user.role, mobile: user.mobile,
  108. });
  109. // 添加其他参与人
  110. for (const a of auditors) {
  111. newAuditors.push({
  112. tid: newSettle.tid, settle_id: newSettle.id,
  113. audit_id: a.id,
  114. audit_times: 1, audit_order: a.audit_order, audit_type: a.audit_type,
  115. active_order: a.audit_order, audit_status: auditConst.auditConst.settle.status.uncheck,
  116. name: a.name, company: a.company, role: a.role, mobile: a.mobile,
  117. });
  118. }
  119. const result = await transaction.insert(this.tableName, newAuditors);
  120. if (result.affectedRows !== newAuditors.length) throw '初始化审批流程错误';
  121. }
  122. }
  123. return SettleAudit;
  124. };