change_apply_audit_check.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Ellisran
  6. * @date 2020/10/15
  7. * @version
  8. */
  9. const status = require('../const/audit').changeApply.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* changeApplyAuditCheck(next) {
  22. try {
  23. // 获取revise
  24. const id = this.params.caid || this.request.body.caid;
  25. if (!id) {
  26. throw '您访问的变更申请不存在';
  27. }
  28. // const change = yield this.service.change.getDataByCondition({ cid });
  29. if (!this.change) {
  30. const change = yield this.service.changeApply.getDataById(id);
  31. if (!change) throw '变更申请数据有误';
  32. yield this.service.changeApply.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.changeApplyAudit.getAllDataByCondition({ where: { caid: change.id, times: change.times }, orders: [['order', 'asc']] });
  40. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  41. // 判断并获取审批组
  42. const group = yield this.service.shenpiGroup.getSelectGroupByChangeType(this.tender.id, shenpiConst.sp_type.change, 'changeApply', change.sp_group);
  43. if ((group && change.sp_group !== group.id) || (!group && change.sp_group !== 0)) {
  44. change.sp_group = group ? group.id : 0;
  45. yield this.service.changeApply.defaultUpdate({ id: change.id, sp_group: change.sp_group });
  46. }
  47. const condition = { tid: this.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpi_status, sp_group: change.sp_group };
  48. const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: condition, orders: [['audit_order', 'asc']] });
  49. yield this.service.shenpiAudit.noYbShenpiList(change.uid, shenpiList);
  50. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  51. let sameAudit = auditList.length === shenpiList.length;
  52. if (sameAudit) {
  53. for (const audit of auditList) {
  54. const shenpi = shenpiList.find(x => { return x.audit_id === audit.aid; });
  55. if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
  56. sameAudit = false;
  57. break;
  58. }
  59. }
  60. }
  61. if (!sameAudit) {
  62. yield this.service.changeApplyAudit.updateNewAuditList(change, shenpiList);
  63. yield this.service.changeApply.loadChangeUser(change);
  64. yield this.service.changeApply.doCheckChangeCanCancel(change);
  65. }
  66. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  67. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: this.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpi_status });
  68. if (!shenpiInfo || (shenpiInfo && shenpiInfo.audit_id === change.uid)) {
  69. // 不存在终审人 或 存在终审但与原报相同时, 这里恢复为授权审批人
  70. this.tender.info.shenpi.change = shenpiConst.sp_status.sqspr;
  71. } else {
  72. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  73. const lastAuditors = auditList.filter(x => {
  74. return x.order === auditList.length - 1;
  75. });
  76. if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].aid))) {
  77. yield this.service.changeApplyAudit.updateLastAudit(change, auditList, shenpiInfo.audit_id);
  78. yield this.service.changeApply.loadChangeUser(change);
  79. yield this.service.changeApply.doCheckChangeCanCancel(change);
  80. }
  81. }
  82. }
  83. }
  84. yield next;
  85. } catch (err) {
  86. console.log(err);
  87. // 输出错误到日志
  88. if (err.stack) {
  89. this.logger.error(err);
  90. } else {
  91. this.getLogger('fail').info(JSON.stringify({
  92. error: err,
  93. project: this.session.sessionProject,
  94. user: this.session.sessionUser,
  95. body: this.session.body,
  96. }));
  97. }
  98. // 重定向值标段管理
  99. this.redirect(this.request.headers.referer);
  100. }
  101. };
  102. };