stage_check.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.curAuditor: 当前审批人(未上报为空,审批通过 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. stage.auditors = yield this.service.stageAudit.getAuditors(stage.id, stage.times);
  47. stage.curAuditor = yield this.service.stageAudit.getCurAuditor(stage.id, stage.times);
  48. // 历史台账
  49. if (stage.status === status.checked) {
  50. stage.ledgerHis = yield this.service.ledgerHistory.getDataById(stage.his_id);
  51. }
  52. // 获取最新的期
  53. stage.highOrder = yield this.service.stage.count({
  54. tid: this.tender.id,
  55. });
  56. const materials = yield this.service.material.getAllDataByCondition({ columns: ['stage_id', 's_order'], where: { tid: this.tender.id } });
  57. stage.hadMaterial = materials.find(function(item) {
  58. return item.s_order.split(',').indexOf(stage.highOrder.toString()) !== -1;
  59. });
  60. // 权限相关
  61. // todo 校验权限 (标段参与人、分享、游客)
  62. const accountId = this.session.sessionUser.accountId,
  63. auditorIds = _.map(stage.auditors, 'aid');
  64. let auditAssists = yield this.service.stageAuditAss.getData(stage);
  65. // 过滤无效的协审人
  66. auditAssists = auditAssists.filter(x => {
  67. return x.user_id === stage.user_id || auditorIds.indexOf(x.user_id) >= 0;
  68. });
  69. stage.userAssists = auditAssists.filter(x => { return x.user_id === stage.user_id; }); // 原报协同人
  70. stage.auditAssists = auditAssists.filter(x => { return x.user_id !== stage.user_id; }); // 审批协同人
  71. const userAssistIds = _.map(stage.userAssists, 'ass_user_id'),
  72. auditAssistIds = _.map(stage.auditAssists, 'ass_user_id'),
  73. shareIds = [];
  74. stage.users = stage.status === status.uncheck ? [stage.user_id, ...userAssistIds] : [stage.user_id, ...userAssistIds, ...auditorIds, ...auditAssistIds];
  75. stage.relaAssists = auditAssists.filter(x => { return x.user_id === accountId });
  76. if (stage.status === status.uncheck) {
  77. stage.readOnly = accountId !== stage.user_id && userAssistIds.indexOf(accountId) < 0;
  78. if (!stage.readOnly) {
  79. stage.assist = stage.userAssists.find(x => { return x.ass_user_id === accountId; });
  80. }
  81. stage.curTimes = stage.times;
  82. stage.curOrder = 0;
  83. } else if (stage.status === status.checkNo) {
  84. stage.readOnly = accountId !== stage.user_id && userAssistIds.indexOf(accountId) < 0;
  85. const checkNoAudit = yield this.service.stageAudit.getDataByCondition({
  86. sid: stage.id, times: stage.times - 1, status: status.checkNo,
  87. });
  88. if (!stage.readOnly) {
  89. stage.assist = stage.userAssists.find(x => { return x.ass_user_id === accountId; });
  90. stage.curTimes = stage.times;
  91. stage.curOrder = 0;
  92. } else {
  93. stage.curTimes = stage.times - 1;
  94. stage.curOrder = checkNoAudit.order;
  95. }
  96. } else if (stage.status === status.checked) {
  97. stage.readOnly = true;
  98. stage.curTimes = stage.times;
  99. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  100. } else {
  101. const ass = stage.auditAssists.find(x => { return x.user_id === stage.curAuditor.aid && x.ass_user_id === accountId; });
  102. stage.readOnly = stage.curAuditor.aid !== accountId && !ass;
  103. stage.curTimes = stage.times;
  104. if (!stage.readOnly) {
  105. stage.assist = ass;
  106. stage.curOrder = stage.curAuditor.order;
  107. } else {
  108. stage.curOrder = stage.curAuditor.order - 1;
  109. }
  110. }
  111. if (stage.readOnly) {
  112. stage.assist = accountId === stage.user_id || auditorIds.indexOf(accountId) >= 0 ? null : auditAssists.find(x => { return x.ass_user_id === accountId});
  113. }
  114. const permission = this.session.sessionUser.permission;
  115. if (stage.users.indexOf(accountId) >= 0) {
  116. stage.filePermission = true;
  117. } else {
  118. if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) {// 分享人
  119. if (stage.status === status.uncheck) {
  120. throw '您无权查看该数据';
  121. }
  122. stage.filePermission = false;
  123. } else if (this.tender.isTourist || this.session.sessionUser.is_admin) {
  124. stage.filePermission = this.tender.touristPermission.file || auditorIds.indexOf(accountId) !== -1;
  125. } else {
  126. throw '您无权查看该数据';
  127. }
  128. }
  129. // 获取当前审批人的上一个审批人,判断是否是当前登录人,并赋予撤回功能,(当审批人存在有审批过时,上一人不允许再撤回)
  130. stage.cancancel = 0;
  131. if (stage.status !== status.checked && stage.status !== status.uncheck) {
  132. if (stage.status !== status.checkNo) {
  133. // 找出当前操作人上一个审批人,包括审批完成的和退回上一个审批人的,同时当前操作人为第一人时,就是则为原报
  134. const onAuditor = _.find(stage.auditors, function(item) {
  135. return item.aid === stage.curAuditor.aid && item.status === status.checking;
  136. });
  137. const preAudit = onAuditor.order !== 1 ? _.find(stage.auditors, { order: onAuditor.order - 1 }) : false;
  138. const preAid = preAudit ? (preAudit.status !== status.checkAgain ? preAudit.aid : false) : stage.user_id;// 已发起重审无法撤回
  139. if ((onAuditor.aid === preAid && preAudit.status === status.checkCancel) || preAudit.is_old === 1) {
  140. stage.cancancel = 0;// 不可以多次撤回
  141. } else if (preAid === accountId && preAid !== stage.user_id) {
  142. if (preAudit.status === status.checked) {
  143. stage.cancancel = 2;// 审批人撤回审批通过
  144. } else if (preAudit.status === status.checkNoPre) {
  145. stage.cancancel = 3;// 审批人撤回审批退回上一人
  146. }
  147. stage.preAudit = preAudit;
  148. } else if (preAid === accountId && preAid === stage.user_id) {
  149. stage.cancancel = 1;// 原报撤回
  150. }
  151. } else {
  152. const lastAuditors = yield this.service.stageAudit.getAuditors(stage.id, stage.times - 1);
  153. const onAuditor = _.find(lastAuditors, { status: status.checkNo });
  154. if (onAuditor.aid === accountId) {
  155. stage.cancancel = 4;// 审批人撤回退回原报
  156. }
  157. }
  158. }
  159. const lastRevise = yield this.service.ledgerRevise.getLastestRevise(this.tender.id);
  160. stage.revising = (lastRevise && lastRevise.status !== reviseStatus.checked) || false;
  161. this.stage = stage;
  162. // 根据状态判断是否需要更新审批人列表
  163. if ((stage.status === status.uncheck || stage.status === status.checkNo) && this.tender.info.shenpi.stage !== shenpiConst.sp_status.sqspr) {
  164. const shenpi_status = this.tender.info.shenpi.stage;
  165. // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
  166. const auditList = yield this.service.stageAudit.getAllDataByCondition({ where: { sid: stage.id, times: stage.times }, orders: [['order', 'asc']] });
  167. const auditIdList = _.map(auditList, 'aid');
  168. if (shenpi_status === shenpiConst.sp_status.gdspl) {
  169. const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: stage.tid, sp_type: shenpiConst.sp_type.stage, sp_status: shenpi_status } });
  170. const shenpiIdList = _.map(shenpiList, 'audit_id');
  171. // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
  172. if (!_.isEqual(auditIdList, shenpiIdList)) {
  173. yield this.service.stageAudit.updateNewAuditList(stage, shenpiIdList);
  174. }
  175. } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
  176. const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: stage.tid, sp_type: shenpiConst.sp_type.stage, sp_status: shenpi_status });
  177. // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
  178. if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {
  179. yield this.service.stageAudit.updateLastAudit(stage, auditList, shenpiInfo.audit_id);
  180. } else if (!shenpiInfo) {
  181. // 不存在终审人的状态下这里恢复为授权审批人
  182. this.tender.info.shenpi.stage = shenpiConst.sp_status.sqspr;
  183. }
  184. }
  185. }
  186. yield next;
  187. } catch (err) {
  188. console.log(err);
  189. this.helper.log(err);
  190. // 输出错误到日志
  191. if (err.stack) {
  192. this.logger.error(err);
  193. } else {
  194. this.getLogger('fail').info(JSON.stringify({
  195. error: err,
  196. project: this.session.sessionProject,
  197. user: this.session.sessionUser,
  198. body: this.session.body,
  199. }));
  200. }
  201. // 重定向值标段管理
  202. this.redirect(this.request.headers.referer);
  203. }
  204. };
  205. };