payment_detail_check.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const messageType = require('../const/message_type');
  10. const status = require('../const/audit').stage.status;
  11. const shenpiConst = require('../const/shenpi');
  12. const _ = require('lodash');
  13. module.exports = options => {
  14. /**
  15. * 标段校验 中间件
  16. * 1. 读取标段数据(包括属性)
  17. * 2. 检验用户是否可见标段(不校验具体权限)
  18. *
  19. * @param {function} next - 中间件继续执行的方法
  20. * @return {void}
  21. */
  22. return function* paymentDetailCheck(next) {
  23. try {
  24. const id = parseInt(this.params.did);
  25. if (!id) throw '参数错误';
  26. const detail = yield this.service.paymentDetail.getDataById(id);
  27. if (!detail) {
  28. throw '支付审批表单不存在';
  29. }
  30. const trInfo = yield this.service.paymentTenderRpt.getDataById(detail.tr_id);
  31. if (!trInfo) {
  32. throw '支付审批报表不存在';
  33. }
  34. // 读取原报、审核人数据
  35. detail.auditors = yield this.service.paymentDetailAudit.getAuditors(detail.id, detail.times);
  36. detail.curAuditor = yield this.service.paymentDetailAudit.getCurAuditor(detail.id, detail.times);
  37. const accountId = this.session.sessionUser.accountId,
  38. auditorIds = _.map(detail.auditors, 'aid');
  39. if (accountId === detail.uid) { // 原报
  40. detail.curTimes = detail.times;
  41. if (detail.status === status.uncheck || detail.status === status.checkNo) {
  42. detail.curOrder = 0;
  43. } else if (detail.status === status.checked) {
  44. detail.curOrder = _.max(_.map(detail.auditors, 'order'));
  45. } else {
  46. detail.curOrder = detail.curAuditor.aid === accountId ? detail.curAuditor.order : detail.curAuditor.order - 1;
  47. }
  48. } else if (this.tender.isTourist) {
  49. detail.curTimes = detail.times;
  50. if (detail.status === status.uncheck || detail.status === status.checkNo) {
  51. detail.curOrder = 0;
  52. } else if (detail.status === status.checked) {
  53. detail.curOrder = _.max(_.map(detail.auditors, 'order'));
  54. } else {
  55. detail.curOrder = detail.curAuditor.order;
  56. }
  57. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  58. if (detail.status === status.uncheck) {
  59. throw '您无权查看该数据';
  60. }
  61. // detail.readOnly = detail.status !== status.checking || accountId !== detail.curAuditor.aid;
  62. detail.curTimes = detail.status === status.checkNo ? detail.times - 1 : detail.times;
  63. if (detail.status === status.checked) {
  64. detail.curOrder = _.max(_.map(detail.auditors, 'order'));
  65. } else if (detail.status === status.checkNo) {
  66. const audit = this.service.paymentDetailAudit.getDataByCondition({
  67. td_id: detail.id, times: detail.times, status: status.checkNo,
  68. });
  69. detail.curOrder = audit.order;
  70. } else {
  71. detail.curOrder = accountId === detail.curAuditor.aid ? detail.curAuditor.order : detail.curAuditor.order - 1;
  72. }
  73. } else { // 其他不可见
  74. throw '您无权查看该数据';
  75. }
  76. // 获取最新的期
  77. detail.highOrder = yield this.service.paymentDetail.count({
  78. tr_id: detail.tr_id,
  79. });
  80. detail.readOnly = !((detail.status === status.uncheck || detail.status === status.checkNo) && accountId === detail.uid);
  81. this.detail = detail;
  82. this.trInfo = trInfo;
  83. // 根据状态判断是否需要更新审批人列表
  84. if ((detail.status === status.uncheck || detail.status === status.checkNo) && trInfo.sp_status !== shenpiConst.sp_status.sqspr) {
  85. const shenpi_status = trInfo.sp_status;
  86. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  87. const auditList = yield this.service.paymentDetailAudit.getAllDataByCondition({ where: { td_id: detail.id, times: detail.times }, orders: [['order', 'asc']] });
  88. const auditIdList = _.map(auditList, 'aid');
  89. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  90. const shenpiList = yield this.service.paymentShenpiAudit.getAllDataByCondition({ where: { tr_id: trInfo.id, sp_status: shenpi_status } });
  91. const shenpiIdList = _.map(shenpiList, 'audit_id');
  92. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  93. if (!_.isEqual(auditIdList, shenpiIdList)) {
  94. yield this.service.paymentDetailAudit.updateNewAuditList(detail, shenpiIdList);
  95. }
  96. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  97. const shenpiInfo = yield this.service.paymentShenpiAudit.getDataByCondition({ tr_id: trInfo.id, sp_status: shenpi_status });
  98. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  99. if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {
  100. yield this.service.paymentDetailAudit.updateLastAudit(detail, auditList, shenpiInfo.audit_id);
  101. } else if (!shenpiInfo) {
  102. // 不存在终审人的状态下这里恢复为授权审批人
  103. this.detail.sp_status = shenpiConst.sp_status.sqspr;
  104. }
  105. }
  106. }
  107. yield next;
  108. } catch (err) {
  109. // 输出错误到日志
  110. if (err.stack) {
  111. this.logger.error(err);
  112. } else {
  113. this.session.message = {
  114. type: messageType.ERROR,
  115. icon: 'exclamation-circle',
  116. message: err,
  117. };
  118. this.getLogger('fail').info(JSON.stringify({
  119. error: err,
  120. project: this.session.sessionProject,
  121. user: this.session.sessionUser,
  122. body: this.session.body,
  123. }));
  124. }
  125. if (this.helper.isAjax(this.request)) {
  126. if (err.stack) {
  127. this.body = {err: 4, msg: '标段数据未知错误', data: null};
  128. } else {
  129. this.body = {err: 3, msg: err.toString(), data: null};
  130. }
  131. } else {
  132. if (this.helper.isWap(this.request)) {
  133. this.redirect('/wap/list');
  134. } else {
  135. err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/payment');
  136. }
  137. }
  138. }
  139. };
  140. };