revise_audit_check.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Ellisran
  6. * @date 2020/10/15
  7. * @version
  8. */
  9. const status = require('../const/audit').revise.status;
  10. const shenpiConst = require('../const/shenpi');
  11. const _ = require('lodash');
  12. const checkAuditFlow = async function(ctx) {
  13. const info = ctx.tender.info;
  14. const revise = await ctx.service.ledgerRevise.getLastestRevise(ctx.tender.id);
  15. if (!revise) throw '台账修订数据有误';
  16. if (info.shenpi.revise === shenpiConst.sp_status.sqspr) return;
  17. if (revise.status !== status.uncheck && revise.status !== status.checkNo) return;
  18. const shenpi_status = info.shenpi.revise;
  19. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  20. const auditList = await ctx.service.reviseAudit.getAuditors(revise.id, revise.times);
  21. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  22. const shenpiList = await ctx.service.shenpiAudit.getAllDataByCondition({ where: { tid: revise.tid, sp_type: shenpiConst.sp_type.revise, sp_status: shenpi_status } });
  23. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  24. let sameAudit = auditList.length === shenpiList.length;
  25. if (sameAudit) {
  26. for (const audit of auditList) {
  27. const shenpi = shenpiList.find(x => { return x.audit_id === audit.audit_id; });
  28. if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type || shenpi.audit_ledger_id !== audit.audit_ledger_id) {
  29. sameAudit = false;
  30. break;
  31. }
  32. }
  33. }
  34. if (!sameAudit) await ctx.service.reviseAudit.updateNewAuditList(revise, shenpiList);
  35. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  36. const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: revise.tid, sp_type: shenpiConst.sp_type.revise, sp_status: shenpi_status });
  37. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  38. const lastAuditors = auditList.filter(x => { x.audit_order === auditList[auditList.length - 1].audit_order; });
  39. if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].audit_id))) {
  40. await ctx.service.reviseAudit.updateLastAudit(revise, auditList, shenpiInfo.audit_id);
  41. } else if (!shenpiInfo) {
  42. // 不存在终审人的状态下这里恢复为授权审批人
  43. info.shenpi.revise = shenpiConst.sp_status.sqspr;
  44. }
  45. }
  46. };
  47. module.exports = options => {
  48. /**
  49. * 标段校验 中间件
  50. * 1. 读取标段数据(包括属性)
  51. * 2. 检验用户是否可见标段(不校验具体权限)
  52. *
  53. * @param {function} next - 中间件继续执行的方法
  54. * @return {void}
  55. */
  56. return function* reviseAuditCheck(next) {
  57. try {
  58. yield checkAuditFlow(this);
  59. yield next
  60. } catch (err) {
  61. this.log(err);
  62. // 重定向值标段管理
  63. this.redirect(this.request.headers.referer);
  64. }
  65. };
  66. };