change_apply_audit_check.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. if (change.sp_group === 0 && shenpiList.length === 0) {
  51. this.tender.info.shenpi.change = shenpiConst.sp_status.sqspr;
  52. } else {
  53. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  54. let sameAudit = auditList.length === shenpiList.length;
  55. if (sameAudit) {
  56. for (const audit of auditList) {
  57. const shenpi = shenpiList.find(x => { return x.audit_id === audit.aid; });
  58. if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
  59. sameAudit = false;
  60. break;
  61. }
  62. }
  63. }
  64. if (!sameAudit) {
  65. yield this.service.changeApplyAudit.updateNewAuditList(change, shenpiList);
  66. yield this.service.changeApply.loadChangeUser(change);
  67. yield this.service.changeApply.doCheckChangeCanCancel(change);
  68. }
  69. }
  70. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  71. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: this.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpi_status });
  72. if (!shenpiInfo || (shenpiInfo && shenpiInfo.audit_id === change.uid)) {
  73. // 不存在终审人 或 存在终审但与原报相同时, 这里恢复为授权审批人
  74. this.tender.info.shenpi.change = shenpiConst.sp_status.sqspr;
  75. } else {
  76. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  77. const lastAuditors = auditList.filter(x => {
  78. return x.order === auditList.length - 1;
  79. });
  80. if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].aid))) {
  81. yield this.service.changeApplyAudit.updateLastAudit(change, auditList, shenpiInfo.audit_id);
  82. yield this.service.changeApply.loadChangeUser(change);
  83. yield this.service.changeApply.doCheckChangeCanCancel(change);
  84. }
  85. }
  86. }
  87. }
  88. yield next;
  89. } catch (err) {
  90. console.log(err);
  91. // 输出错误到日志
  92. if (err.stack) {
  93. this.logger.error(err);
  94. } else {
  95. this.getLogger('fail').info(JSON.stringify({
  96. error: err,
  97. project: this.session.sessionProject,
  98. user: this.session.sessionUser,
  99. body: this.session.body,
  100. }));
  101. }
  102. // 重定向值标段管理
  103. this.redirect(this.request.headers.referer);
  104. }
  105. };
  106. };