ledger_audit_check.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Ellisran
  6. * @date 2020/10/15
  7. * @version
  8. */
  9. const status = require('../const/audit').ledger.status;
  10. const shenpiConst = require('../const/shenpi');
  11. const _ = require('lodash');
  12. const checkAuditFlow = async function(ctx) {
  13. const tender = ctx.tender.data;
  14. const info = ctx.tender.info;
  15. if (info.shenpi.ledger === shenpiConst.sp_status.sqspr) return;
  16. if (tender.ledger_status !== status.uncheck && tender.ledger_status !== status.checkNo) return;
  17. const shenpi_status = info.shenpi.ledger;
  18. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  19. const auditList = await ctx.service.ledgerAudit.getAuditors(tender.id, tender.ledger_times);
  20. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  21. const shenpiList = await ctx.service.shenpiAudit.getAllDataByCondition({ where: { tid: tender.id, sp_type: shenpiConst.sp_type.ledger, sp_status: shenpi_status } });
  22. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  23. let sameAudit = auditList.length === shenpiList.length;
  24. console.log(sameAudit);
  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.ledgerAudit.updateNewAuditList(tender, shenpiList);
  35. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  36. const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: tender.id, sp_type: shenpiConst.sp_type.ledger, 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.ledgerAudit.updateLastAudit(tender, auditList, shenpiInfo.audit_id);
  41. } else if (!shenpiInfo) {
  42. // 不存在终审人的状态下这里恢复为授权审批人
  43. info.shenpi.ledger = 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* ledgerAuditCheck(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. };