material_check.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 'use strict';
  2. /**
  3. *
  4. * @author EllisRan
  5. * @date
  6. * @version
  7. */
  8. const status = require('../const/audit').material.status;
  9. const shenpiConst = require('../const/shenpi');
  10. const _ = require('lodash');
  11. module.exports = options => {
  12. /**
  13. * 材料调差校验 中间件
  14. * 1. 读取材料调差数据
  15. * 2. 检验用户是否参与材料调差(不校验具体权限)
  16. *
  17. * 写入ctx.material数据
  18. * 其中:
  19. * material.auditors: 审批人列表(退回原报时,加载上一流程)
  20. * material.curAuditor: 当前审批人(未上报为空,审批通过 or 退回原报时,为空)
  21. * material.readonly: 登录人,是否可操作
  22. * material.curTimes: 当前登录人,操作、查阅数据times
  23. * material.curOrder: 当前登录人,操作、查阅数据order
  24. *
  25. * 该方法为通用方法,如需material其他数据,请在controller中查询
  26. *
  27. * @param {function} next - 中间件继续执行的方法
  28. * @return {void}
  29. */
  30. return function* materialCheck(next) {
  31. try {
  32. // 读取标段数据
  33. const materialOrder = parseInt(this.params.order);
  34. if (materialOrder <= 0) {
  35. throw '您访问的期不存在';
  36. }
  37. const material = yield this.service.material.getDataByCondition({
  38. tid: this.tender.id,
  39. order: materialOrder,
  40. });
  41. if (!material) {
  42. throw '材料调差期数据错误';
  43. }
  44. // 读取原报、审核人数据
  45. material.auditors = yield this.service.materialAudit.getAuditors(material.id, material.times);
  46. // if (material.status > 1) {
  47. // material.auditors2 === 2 ? material.auditors : (yield this.service.materialAudit.getAuditors(material.id, material.times - 1));
  48. // }
  49. material.curAuditor = yield this.service.materialAudit.getCurAuditor(material.id, material.times);
  50. // 权限相关
  51. // todo 校验权限 (标段参与人、分享)
  52. const accountId = this.session.sessionUser.accountId,
  53. auditorIds = _.map(material.auditors, 'aid'),
  54. shareIds = [];
  55. if (accountId === material.user_id) { // 原报
  56. // if (material.curAuditor) {
  57. // material.readOnly = material.status === status.checking && material.curAuditor.user_id === accountId;
  58. // } else {
  59. // material.readOnly = material.status !== status.uncheck && material.status !== status.checkNo;
  60. // }
  61. material.curTimes = material.times;
  62. if (material.status === status.uncheck || material.status === status.checkNo) {
  63. material.curOrder = 0;
  64. } else if (material.status === status.checked) {
  65. material.curOrder = _.max(_.map(material.auditors, 'order'));
  66. } else {
  67. material.curOrder = material.curAuditor.aid === accountId ? material.curAuditor.order : material.curAuditor.order - 1;
  68. }
  69. material.filePermission = true;
  70. } else if (this.tender.isTourist) {
  71. material.curTimes = material.times;
  72. if (material.status === status.uncheck || material.status === status.checkNo) {
  73. material.curOrder = 0;
  74. } else if (material.status === status.checked) {
  75. material.curOrder = _.max(_.map(material.auditors, 'order'));
  76. } else {
  77. material.curOrder = material.curAuditor.order;
  78. }
  79. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  80. if (material.status === status.uncheck) {
  81. throw '您无权查看该数据';
  82. }
  83. // material.readOnly = material.status !== status.checking || accountId !== material.curAuditor.aid;
  84. material.curTimes = material.status === status.checkNo ? material.times - 1 : material.times;
  85. if (material.status === status.checked) {
  86. material.curOrder = _.max(_.map(material.auditors, 'order'));
  87. } else if (material.status === status.checkNo) {
  88. const audit = this.service.materialAudit.getDataByCondition({
  89. mid: material.id, times: material.times, status: status.checkNo,
  90. });
  91. material.curOrder = audit.order;
  92. } else {
  93. material.curOrder = accountId === material.curAuditor.aid ? material.curAuditor.order : material.curAuditor.order - 1;
  94. }
  95. material.filePermission = true;
  96. } else if (shareIds.indexOf(accountId) !== -1) { // 分享人
  97. if (material.status === status.uncheck) {
  98. throw '您无权查看该数据';
  99. }
  100. // material.readOnly = true;
  101. material.curTimes = material.status === status.checkNo ? material.times - 1 : material.times;
  102. material.curOrder = material.status === status.checked ? _.max(_.map(material.auditors, 'order')) : material.curAuditor.order - 1;
  103. material.filePermission = false;
  104. } else { // 其他不可见
  105. throw '您无权查看该数据';
  106. }
  107. // 获取最新的期
  108. material.highOrder = yield this.service.material.count({
  109. tid: this.tender.id,
  110. });
  111. // 调差的readOnly 指表格和页面只能看不能改,和审批无关
  112. material.readOnly = !((material.status === status.uncheck || material.status === status.checkNo) && accountId === material.user_id);
  113. this.material = material;
  114. // 根据状态判断是否需要更新审批人列表
  115. if ((material.status === status.uncheck || material.status === status.checkNo) && this.tender.info.shenpi.material !== shenpiConst.sp_status.sqspr) {
  116. const shenpi_status = this.tender.info.shenpi.material;
  117. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  118. const auditList = yield this.service.materialAudit.getAllDataByCondition({ where: { mid: material.id, times: material.times } });
  119. const auditIdList = _.map(auditList, 'aid');
  120. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  121. const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status } });
  122. const shenpiIdList = _.map(shenpiList, 'audit_id');
  123. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  124. if (!_.isEqual(auditIdList, shenpiIdList)) {
  125. yield this.service.materialAudit.updateNewAuditList(material, shenpiIdList);
  126. }
  127. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  128. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status });
  129. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  130. if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {
  131. yield this.service.materialAudit.updateLastAudit(material, auditList, shenpiInfo.audit_id);
  132. } else if (!shenpiInfo) {
  133. // 不存在终审人的状态下这里恢复为授权审批人
  134. this.tender.info.shenpi.material = shenpiConst.sp_status.sqspr;
  135. }
  136. }
  137. }
  138. yield next;
  139. } catch (err) {
  140. console.log(err);
  141. // 输出错误到日志
  142. if (err.stack) {
  143. this.logger.error(err);
  144. } else {
  145. this.getLogger('fail').info(JSON.stringify({
  146. error: err,
  147. project: this.session.sessionProject,
  148. user: this.session.sessionUser,
  149. body: this.session.body,
  150. }));
  151. }
  152. // 重定向值标段管理
  153. this.redirect(this.request.headers.referer);
  154. }
  155. };
  156. };