change_audit_check.js 4.8 KB

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