settle_audit.js 5.5 KB

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