change_audit_check.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Ellisran
  6. * @date 2020/10/15
  7. * @version
  8. */
  9. const status = require('../const/audit').change.status;
  10. const shenpiConst = require('../const/shenpi');
  11. const _ = require('lodash');
  12. module.exports = options => {
  13. /**
  14. * 标段校验 中间件
  15. * 1. 读取标段数据(包括属性)
  16. * 2. 检验用户是否可见标段(不校验具体权限)
  17. *
  18. * @param {function} next - 中间件继续执行的方法
  19. * @return {void}
  20. */
  21. return function* changeAuditCheck(next) {
  22. try {
  23. // 获取revise
  24. const cid = this.params.cid || this.request.body.cid;
  25. if (!cid) {
  26. throw '您访问的变更令不存在';
  27. }
  28. // const change = yield this.service.change.getDataByCondition({ cid });
  29. if (!this.change) {
  30. const change = yield this.service.change.getDataByCondition({ cid });
  31. if (!change) throw '变更令数据有误';
  32. yield this.service.change.loadChangeUser(change);
  33. this.change = change;
  34. }
  35. const change = this.change;
  36. if ((change.status === status.uncheck || change.status === status.checkNo || change.status === status.revise) && this.tender.info.shenpi.change !== shenpiConst.sp_status.sqspr) {
  37. const shenpi_status = this.tender.info.shenpi.change;
  38. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  39. const auditList = yield this.service.changeAudit.getAllDataByCondition({ where: { cid: change.cid, times: change.times }, orders: [['usite', 'asc']] });
  40. auditList.shift();
  41. const auditIdList = _.map(auditList, 'uid');
  42. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  43. // 判断并获取审批组
  44. const group = yield this.service.shenpiGroup.getSelectGroupByChangeType(this.tender.id, shenpiConst.sp_type.change, 'change', change.sp_group);
  45. if ((group && change.sp_group !== group.id) || (!group && change.sp_group !== 0)) {
  46. change.sp_group = group ? group.id : 0;
  47. yield this.service.change.defaultUpdate({ sp_group: change.sp_group }, { where: { cid: change.cid } });
  48. }
  49. const condition = { tid: this.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpi_status, sp_group: change.sp_group };
  50. const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: condition, orders: [['audit_order', 'asc']] });
  51. if (change.sp_group === 0 && shenpiList.length === 0) {
  52. this.tender.info.shenpi.change = shenpiConst.sp_status.sqspr;
  53. } else {
  54. // const shenpiIdList = _.map(shenpiList, 'audit_id');
  55. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  56. let sameAudit = auditList.length === shenpiList.length;
  57. if (sameAudit) {
  58. for (const audit of auditList) {
  59. const shenpi = shenpiList.find(x => { return x.audit_id === audit.uid; });
  60. if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
  61. sameAudit = false;
  62. break;
  63. }
  64. }
  65. }
  66. if (!sameAudit) {
  67. yield this.service.changeAudit.updateNewAuditList(change, shenpiList);
  68. yield this.service.change.loadChangeUser(change);
  69. yield this.service.change.doCheckChangeCanCancel(change);
  70. }
  71. }
  72. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  73. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: this.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpi_status });
  74. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  75. const lastAuditors = auditList.filter(x => { return x.usite === auditList.length - 1; });
  76. if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].uid))) {
  77. yield this.service.changeAudit.updateLastAudit(change, auditList, shenpiInfo.audit_id);
  78. yield this.service.change.loadChangeUser(change);
  79. yield this.service.change.doCheckChangeCanCancel(change);
  80. } else if (!shenpiInfo) {
  81. // 不存在终审人的状态下这里恢复为授权审批人
  82. this.tender.info.shenpi.change = shenpiConst.sp_status.sqspr;
  83. }
  84. }
  85. }
  86. yield next;
  87. } catch (err) {
  88. console.log(err);
  89. // 输出错误到日志
  90. if (err.stack) {
  91. this.logger.error(err);
  92. } else {
  93. this.getLogger('fail').info(JSON.stringify({
  94. error: err,
  95. project: this.session.sessionProject,
  96. user: this.session.sessionUser,
  97. body: this.session.body,
  98. }));
  99. }
  100. // 重定向值标段管理
  101. this.redirect(this.request.headers.referer);
  102. }
  103. };
  104. };