ledger_audit_check.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. if (sameAudit) {
  25. for (const audit of auditList) {
  26. const shenpi = shenpiList.find(x => { return x.audit_id === audit.audit_id; });
  27. if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type || shenpi.audit_ledger_id !== audit.audit_ledger_id) {
  28. sameAudit = false;
  29. break;
  30. }
  31. }
  32. }
  33. if (!sameAudit) await ctx.service.ledgerAudit.updateNewAuditList(tender, shenpiList);
  34. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  35. const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: tender.id, sp_type: shenpiConst.sp_type.ledger, sp_status: shenpi_status });
  36. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  37. const lastAuditors = auditList.filter(x => { x.audit_order === auditList[auditList.length - 1].audit_order; });
  38. if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].audit_id))) {
  39. await ctx.service.ledgerAudit.updateLastAudit(tender, auditList, shenpiInfo.audit_id);
  40. } else if (!shenpiInfo) {
  41. // 不存在终审人的状态下这里恢复为授权审批人
  42. info.shenpi.ledger = shenpiConst.sp_status.sqspr;
  43. }
  44. }
  45. };
  46. module.exports = options => {
  47. /**
  48. * 标段校验 中间件
  49. * 1. 读取标段数据(包括属性)
  50. * 2. 检验用户是否可见标段(不校验具体权限)
  51. *
  52. * @param {function} next - 中间件继续执行的方法
  53. * @return {void}
  54. */
  55. return function* ledgerAuditCheck(next) {
  56. try {
  57. yield checkAuditFlow(this);
  58. yield next;
  59. } catch (err) {
  60. this.log(err);
  61. // 重定向值标段管理
  62. this.redirect(this.request.headers.referer);
  63. }
  64. };
  65. };