advance_check.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. /**
  3. * 预付款中间件
  4. * @author lanjianrong
  5. * @date 2020/8/10
  6. * @version
  7. */
  8. const status = require('../const/audit').advance.status;
  9. const shenpiConst = require('../const/shenpi');
  10. const _ = require('lodash');
  11. // const _ = require('lodash')
  12. module.exports = () => {
  13. /**
  14. * 预付款 中间件
  15. * 1. 读取期数据
  16. * 2. 检验用户是否参与期(不校验具体权限)
  17. *
  18. * 写入ctx.advance数据
  19. * 其中:
  20. * advance.auditors: 审批人列表(退回原报时,加载上一流程)
  21. * advance.curAuditor: 当前审批人(未上报为空,审批通过 or 退回原报时,为空)
  22. * advance.readonly: 登录人,是否可操作
  23. * advance.curTimes: 当前登录人,操作、查阅数据times
  24. * advance.curOrder: 当前登录人,操作、查阅数据order
  25. *
  26. * 该方法为通用方法,如需advance其他数据,请在controller中查询
  27. *
  28. * @param {function} next - 中间件继续执行的方法
  29. * @return {void}
  30. */
  31. return function* advanceCheck(next) {
  32. try {
  33. // 读取预付款id
  34. const cid = this.params.cid || this.request.body.cid;
  35. const id = parseInt(this.params.order);
  36. if (!id || id <= 0) {
  37. throw '您访问的预付款期不存在';
  38. }
  39. const advance = yield this.service.advance.getDataById(id);
  40. if (!advance) {
  41. throw '预付款数据错误';
  42. }
  43. advance.user = yield this.service.projectAccount.getAccountInfoById(advance.uid);
  44. // 读取审核人列表数据
  45. advance.auditors = yield this.service.advanceAudit.getAuditors(advance.id, advance.times);
  46. advance.curAuditor = yield this.service.advanceAudit.getCurAuditor(advance.id, advance.times);
  47. // 获取最新的期
  48. // advance.highOrder = yield this.service.advance.getLastestAdvance(this.tender.id, type, true)
  49. this.advance = advance;
  50. // 根据状态判断是否需要更新审批人列表
  51. if ((advance.status === status.uncheck || advance.status === status.checkNo) && this.tender.info.shenpi.advance !== shenpiConst.sp_status.sqspr) {
  52. const shenpi_status = this.tender.info.shenpi.advance;
  53. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  54. const auditList = yield this.service.advanceAudit.getAllDataByCondition({ where: { vid: advance.id, times: advance.times }, orders: [['order', 'asc']] });
  55. const auditIdList = _.map(auditList, 'audit_id');
  56. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  57. const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: advance.tid, sp_type: shenpiConst.sp_type.advance, sp_status: shenpi_status } });
  58. const shenpiIdList = _.map(shenpiList, 'audit_id');
  59. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  60. if (!_.isEqual(auditIdList, shenpiIdList)) {
  61. yield this.service.advanceAudit.updateNewAuditList(advance, shenpiIdList);
  62. }
  63. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  64. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: advance.tid, sp_type: shenpiConst.sp_type.advance, sp_status: shenpi_status });
  65. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  66. if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {
  67. yield this.service.advanceAudit.updateLastAudit(advance, auditList, shenpiInfo.audit_id);
  68. } else if (!shenpiInfo) {
  69. // 不存在终审人的状态下这里恢复为授权审批人
  70. this.tender.info.shenpi.advance = shenpiConst.sp_status.sqspr;
  71. }
  72. }
  73. }
  74. yield next;
  75. } catch (err) {
  76. this.helper.log(err);
  77. // 输出错误到日志
  78. if (err.stack) {
  79. this.logger.error(err);
  80. } else {
  81. this.getLogger('fail').info(
  82. JSON.stringify({
  83. error: err,
  84. project: this.session.sessionProject,
  85. user: this.session.sessionUser,
  86. body: this.session.body,
  87. })
  88. );
  89. }
  90. // 重定向值标段管理
  91. this.redirect(this.request.headers.referer);
  92. }
  93. };
  94. };