stage_check.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. 'use strict';
  2. /**
  3. *
  4. * @author Mai
  5. * @date
  6. * @version
  7. */
  8. const status = require('../const/audit').stage.status;
  9. const reviseStatus = require('../const/audit').revise.status;
  10. const shenpiConst = require('../const/shenpi');
  11. const _ = require('lodash');
  12. module.exports = options => {
  13. /**
  14. * 期校验 中间件
  15. * 1. 读取期数据
  16. * 2. 检验用户是否参与期(不校验具体权限)
  17. *
  18. * 写入ctx.stage数据
  19. * 其中:
  20. * stage.auditors: 审批人列表(退回原报时,加载上一流程)
  21. * stage.curAuditors: 当前审批人(未上报为空,审批通过 or 退回原报时,为空)
  22. * stage.readonly: 登录人,是否可操作
  23. * stage.curTimes: 当前登录人,操作、查阅数据times
  24. * stage.curOrder: 当前登录人,操作、查阅数据order
  25. *
  26. * 该方法为通用方法,如需stage其他数据,请在controller中查询
  27. *
  28. * @param {function} next - 中间件继续执行的方法
  29. * @return {void}
  30. */
  31. return function* stageCheck(next) {
  32. try {
  33. // 读取标段数据
  34. const stageOrder = parseInt(this.params.order);
  35. if (stageOrder <= 0) {
  36. throw '您访问的期不存在';
  37. }
  38. const stage = yield this.service.stage.getDataByCondition({
  39. tid: this.tender.id,
  40. order: stageOrder,
  41. });
  42. if (!stage) {
  43. throw '期数据错误';
  44. }
  45. // 读取原报、审核人数据
  46. yield this.service.stage.loadStageUser(stage);
  47. // 历史台账
  48. if (stage.status === status.checked) {
  49. stage.ledgerHis = yield this.service.ledgerHistory.getDataById(stage.his_id);
  50. }
  51. // 获取最新的期
  52. stage.highOrder = yield this.service.stage.count({
  53. tid: this.tender.id,
  54. });
  55. const materials = yield this.service.material.getAllDataByCondition({ columns: ['stage_id', 's_order'], where: { tid: this.tender.id } });
  56. stage.hadMaterial = materials.find(function(item) {
  57. return item.s_order.split(',').indexOf(stage.highOrder.toString()) !== -1;
  58. });
  59. // 权限相关
  60. // todo 校验权限 (标段参与人、分享、游客)
  61. const accountId = this.session.sessionUser.accountId;
  62. const shareIds = [];
  63. if (stage.status === status.uncheck) {
  64. stage.readOnly = accountId !== stage.user_id && stage.userAssistIds.indexOf(accountId) < 0;
  65. if (!stage.readOnly) {
  66. stage.assist = stage.userAssists.find(x => { return x.ass_user_id === accountId; });
  67. }
  68. stage.curTimes = stage.times;
  69. stage.curOrder = 0;
  70. } else if (stage.status === status.checkNo) {
  71. stage.readOnly = accountId !== stage.user_id && stage.userAssistIds.indexOf(accountId) < 0;
  72. const checkNoAudit = yield this.service.stageAudit.getDataByCondition({
  73. sid: stage.id, times: stage.times - 1, status: status.checkNo,
  74. });
  75. if (!stage.readOnly) {
  76. stage.assist = stage.userAssists.find(x => { return x.ass_user_id === accountId; });
  77. stage.curTimes = stage.times;
  78. stage.curOrder = 0;
  79. } else {
  80. stage.curTimes = stage.times - 1;
  81. stage.curOrder = checkNoAudit.order;
  82. }
  83. } else if (stage.status === status.checked) {
  84. stage.readOnly = true;
  85. stage.curTimes = stage.times;
  86. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  87. } else {
  88. const ass = stage.auditAssists.find(x => { return stage.curAuditorIds.indexOf(x.user_id) >= 0 && x.ass_user_id === accountId; });
  89. stage.readOnly = stage.curAuditorIds.indexOf(accountId) < 0 && !ass;
  90. stage.curTimes = stage.times;
  91. if (!stage.readOnly) {
  92. stage.assist = ass;
  93. stage.curOrder = stage.curAuditors[0].order;
  94. } else {
  95. stage.curOrder = stage.curAuditors[0].order - 1;
  96. }
  97. stage.readOnly = stage.readOnly && this._.isEqual(stage.flowAuditorIds, stage.curAuditorIds);
  98. }
  99. if (stage.readOnly) {
  100. stage.assist = accountId === stage.user_id || stage.auditorIds.indexOf(accountId) >= 0 ? null : stage.auditAssists.find(x => { return x.ass_user_id === accountId});
  101. }
  102. const permission = this.session.sessionUser.permission;
  103. if (stage.userIds.indexOf(accountId) >= 0 || this.session.sessionUser.is_admin) {
  104. stage.filePermission = true;
  105. } else {
  106. if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) {// 分享人
  107. if (stage.status === status.uncheck) {
  108. throw '您无权查看该数据';
  109. }
  110. stage.filePermission = false;
  111. } else if (this.tender.isTourist || this.session.sessionUser.is_admin) {
  112. stage.filePermission = this.tender.touristPermission.file || stage.auditorIds.indexOf(accountId) !== -1;
  113. } else {
  114. throw '您无权查看该数据';
  115. }
  116. }
  117. // 判断stage流程可否撤回,是哪一种撤回
  118. yield this.service.stage.doCheckStageCanCancel(stage);
  119. // 期是否台账修订中
  120. const lastRevise = yield this.service.ledgerRevise.getLastestRevise(this.tender.id);
  121. stage.revising = (lastRevise && lastRevise.status !== reviseStatus.checked) || false;
  122. // 根据状态判断是否需要更新审批人列表
  123. yield this.service.stage.doCheckStageCanCancel(stage);
  124. this.stage = stage;
  125. if ((stage.status === status.uncheck || stage.status === status.checkNo) && this.tender.info.shenpi.stage !== shenpiConst.sp_status.sqspr) {
  126. const shenpi_status = this.tender.info.shenpi.stage;
  127. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  128. const auditList = yield this.service.stageAudit.getAllDataByCondition({ where: { sid: stage.id, times: stage.times }, orders: [['order', 'asc']] });
  129. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  130. const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: stage.tid, sp_type: shenpiConst.sp_type.stage, sp_status: shenpi_status } });
  131. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  132. let sameAudit = auditList.length === shenpiList.length;
  133. if (sameAudit) {
  134. for (const audit of auditList) {
  135. const shenpi = shenpiList.find(x => { return x.audit_id === audit.aid; });
  136. if (shenpi.audit_order !== audit.audit_order) sameAudit = false;
  137. }
  138. }
  139. if (!sameAudit) {
  140. yield this.service.stageAudit.updateNewAuditList(stage, shenpiList);
  141. }
  142. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  143. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: stage.tid, sp_type: shenpiConst.sp_type.stage, sp_status: shenpi_status });
  144. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  145. const lastAuditors = auditList.filter(x => { x.order === auditList.order; });
  146. if (shenpiInfo && (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].aid)) {
  147. yield this.service.stageAudit.updateLastAudit(stage, auditList, shenpiInfo.audit_id);
  148. } else if (!shenpiInfo) {
  149. // 不存在终审人的状态下这里恢复为授权审批人
  150. this.tender.info.shenpi.stage = shenpiConst.sp_status.sqspr;
  151. }
  152. }
  153. }
  154. yield next;
  155. } catch (err) {
  156. console.log(err);
  157. this.helper.log(err);
  158. // 输出错误到日志
  159. if (err.stack) {
  160. this.logger.error(err);
  161. } else {
  162. this.getLogger('fail').info(JSON.stringify({
  163. error: err,
  164. project: this.session.sessionProject,
  165. user: this.session.sessionUser,
  166. body: this.session.body,
  167. }));
  168. }
  169. // 重定向值标段管理
  170. this.redirect(this.request.headers.referer);
  171. }
  172. };
  173. };