advance_check.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 id = parseInt(this.params.order);
  35. if (!id || id <= 0) {
  36. throw '您访问的预付款期不存在';
  37. }
  38. const advance = yield this.service.advance.getDataById(id);
  39. if (!advance) {
  40. throw '预付款数据错误';
  41. }
  42. advance.user = yield this.service.projectAccount.getAccountInfoById(advance.uid);
  43. // 读取审核人列表数据
  44. advance.auditors = yield this.service.advanceAudit.getAuditors(advance.id, advance.times);
  45. advance.curAuditor = yield this.service.advanceAudit.getCurAuditor(advance.id, advance.times);
  46. // 获取最新的期
  47. // advance.highOrder = yield this.service.advance.getLastestAdvance(this.tender.id, type, true)
  48. this.advance = advance;
  49. // 根据状态判断是否需要更新审批人列表
  50. if ((advance.status === status.uncheck || advance.status === status.checkNo) && this.tender.info.shenpi.advance !== shenpiConst.sp_status.sqspr) {
  51. const shenpi_status = this.tender.info.shenpi.advance;
  52. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  53. const auditList = yield this.service.advanceAudit.getAllDataByCondition({ where: { vid: advance.id, times: advance.times } });
  54. const auditIdList = _.map(auditList, 'audit_id');
  55. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  56. const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: advance.tid, sp_type: shenpiConst.sp_type.advance, sp_status: shenpi_status } });
  57. const shenpiIdList = _.map(shenpiList, 'audit_id');
  58. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  59. if (!_.isEqual(auditIdList, shenpiIdList)) {
  60. yield this.service.advanceAudit.updateNewAuditList(advance, shenpiIdList);
  61. }
  62. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  63. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: advance.tid, sp_type: shenpiConst.sp_type.advance, sp_status: shenpi_status });
  64. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  65. if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {
  66. yield this.service.advanceAudit.updateLastAudit(advance, auditList, shenpiInfo.audit_id);
  67. } else if (!shenpiInfo) {
  68. // 不存在终审人的状态下这里恢复为授权审批人
  69. this.tender.info.shenpi.advance = shenpiConst.sp_status.sqspr;
  70. }
  71. }
  72. }
  73. yield next;
  74. } catch (err) {
  75. this.helper.log(err);
  76. // 输出错误到日志
  77. if (err.stack) {
  78. this.logger.error(err);
  79. } else {
  80. this.getLogger('fail').info(
  81. JSON.stringify({
  82. error: err,
  83. project: this.session.sessionProject,
  84. user: this.session.sessionUser,
  85. body: this.session.body,
  86. })
  87. );
  88. }
  89. // 重定向值标段管理
  90. this.redirect(this.request.headers.referer);
  91. }
  92. };
  93. };