123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 'use strict';
- /**
- *
- * @author Mai
- * @date
- * @version
- */
- const status = require('../const/audit').stage.status;
- const reviseStatus = require('../const/audit').revise.status;
- const shenpiConst = require('../const/shenpi');
- const _ = require('lodash');
- module.exports = options => {
- /**
- * 期校验 中间件
- * 1. 读取期数据
- * 2. 检验用户是否参与期(不校验具体权限)
- *
- * 写入ctx.stage数据
- * 其中:
- * stage.auditors: 审批人列表(退回原报时,加载上一流程)
- * stage.curAuditor: 当前审批人(未上报为空,审批通过 or 退回原报时,为空)
- * stage.readonly: 登录人,是否可操作
- * stage.curTimes: 当前登录人,操作、查阅数据times
- * stage.curOrder: 当前登录人,操作、查阅数据order
- *
- * 该方法为通用方法,如需stage其他数据,请在controller中查询
- *
- * @param {function} next - 中间件继续执行的方法
- * @return {void}
- */
- return function* stageCheck(next) {
- try {
- // 读取标段数据
- const stageOrder = parseInt(this.params.order);
- if (stageOrder <= 0) {
- throw '您访问的期不存在';
- }
- const stage = yield this.service.stage.getDataByCondition({
- tid: this.tender.id,
- order: stageOrder,
- });
- if (!stage) {
- throw '期数据错误';
- }
- // 读取原报、审核人数据
- stage.auditors = yield this.service.stageAudit.getAuditors(stage.id, stage.times);
- stage.curAuditor = yield this.service.stageAudit.getCurAuditor(stage.id, stage.times);
- // 历史台账
- if (stage.status === status.checked) {
- stage.ledgerHis = yield this.service.ledgerHistory.getDataById(stage.his_id);
- }
- // 获取最新的期
- stage.highOrder = yield this.service.stage.count({
- tid: this.tender.id,
- });
- const materials = yield this.service.material.getAllDataByCondition({ columns: ['stage_id', 's_order'], where: { tid: this.tender.id } });
- stage.hadMaterial = materials.find(function(item) {
- return item.s_order.split(',').indexOf(stage.highOrder.toString()) !== -1;
- });
- // 权限相关
- // todo 校验权限 (标段参与人、分享、游客)
- const accountId = this.session.sessionUser.accountId,
- auditorIds = _.map(stage.auditors, 'aid');
- let auditAssists = yield this.service.stageAuditAss.getData(stage);
- // 过滤无效的协审人
- auditAssists = auditAssists.filter(x => {
- return x.user_id === stage.user_id || auditorIds.indexOf(x.user_id) >= 0;
- });
- stage.userAssists = auditAssists.filter(x => { return x.user_id === stage.user_id; }); // 原报协同人
- stage.auditAssists = auditAssists.filter(x => { return x.user_id !== stage.user_id; }); // 审批协同人
- const userAssistIds = _.map(stage.userAssists, 'ass_user_id'),
- auditAssistIds = _.map(stage.auditAssists, 'ass_user_id'),
- shareIds = [];
- stage.users = stage.status === status.uncheck ? [stage.user_id, ...userAssistIds] : [stage.user_id, ...userAssistIds, ...auditorIds, ...auditAssistIds];
- stage.relaAssists = auditAssists.filter(x => { return x.user_id === accountId });
- if (stage.status === status.uncheck) {
- stage.readOnly = accountId !== stage.user_id && userAssistIds.indexOf(accountId) < 0;
- if (!stage.readOnly) {
- stage.assist = stage.userAssists.find(x => { return x.ass_user_id === accountId; });
- }
- stage.curTimes = stage.times;
- stage.curOrder = 0;
- } else if (stage.status === status.checkNo) {
- stage.readOnly = accountId !== stage.user_id && userAssistIds.indexOf(accountId) < 0;
- const checkNoAudit = yield this.service.stageAudit.getDataByCondition({
- sid: stage.id, times: stage.times - 1, status: status.checkNo,
- });
- if (!stage.readOnly) {
- stage.assist = stage.userAssists.find(x => { return x.ass_user_id === accountId; });
- stage.curTimes = stage.times;
- stage.curOrder = 0;
- } else {
- stage.curTimes = stage.times - 1;
- stage.curOrder = checkNoAudit.order;
- }
- } else if (stage.status === status.checked) {
- stage.readOnly = true;
- stage.curTimes = stage.times;
- stage.curOrder = _.max(_.map(stage.auditors, 'order'));
- } else {
- const ass = stage.auditAssists.find(x => { return x.user_id === stage.curAuditor.aid && x.ass_user_id === accountId; });
- stage.readOnly = stage.curAuditor.aid !== accountId && !ass;
- stage.curTimes = stage.times;
- if (!stage.readOnly) {
- stage.assist = ass;
- stage.curOrder = stage.curAuditor.order;
- } else {
- stage.curOrder = stage.curAuditor.order - 1;
- }
- }
- if (stage.readOnly) {
- stage.assist = accountId === stage.user_id || auditorIds.indexOf(accountId) >= 0 ? null : auditAssists.find(x => { return x.ass_user_id === accountId});
- }
- const permission = this.session.sessionUser.permission;
- if (stage.users.indexOf(accountId) >= 0) {
- stage.filePermission = true;
- } else {
- if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) {// 分享人
- if (stage.status === status.uncheck) {
- throw '您无权查看该数据';
- }
- stage.filePermission = false;
- } else if (this.tender.isTourist || this.session.sessionUser.is_admin) {
- stage.filePermission = this.tender.touristPermission.file || auditorIds.indexOf(accountId) !== -1;
- } else {
- throw '您无权查看该数据';
- }
- }
- // 获取当前审批人的上一个审批人,判断是否是当前登录人,并赋予撤回功能,(当审批人存在有审批过时,上一人不允许再撤回)
- stage.cancancel = 0;
- if (stage.status !== status.checked && stage.status !== status.uncheck) {
- if (stage.status !== status.checkNo) {
- // 找出当前操作人上一个审批人,包括审批完成的和退回上一个审批人的,同时当前操作人为第一人时,就是则为原报
- const onAuditor = _.find(stage.auditors, function(item) {
- return item.aid === stage.curAuditor.aid && item.status === status.checking;
- });
- const preAudit = onAuditor.order !== 1 ? _.find(stage.auditors, { order: onAuditor.order - 1 }) : false;
- const preAid = preAudit ? (preAudit.status !== status.checkAgain ? preAudit.aid : false) : stage.user_id;// 已发起重审无法撤回
- if ((onAuditor.aid === preAid && preAudit.status === status.checkCancel) || preAudit.is_old === 1) {
- stage.cancancel = 0;// 不可以多次撤回
- } else if (preAid === accountId && preAid !== stage.user_id) {
- if (preAudit.status === status.checked) {
- stage.cancancel = 2;// 审批人撤回审批通过
- } else if (preAudit.status === status.checkNoPre) {
- stage.cancancel = 3;// 审批人撤回审批退回上一人
- }
- stage.preAudit = preAudit;
- } else if (preAid === accountId && preAid === stage.user_id) {
- stage.cancancel = 1;// 原报撤回
- }
- } else {
- const lastAuditors = yield this.service.stageAudit.getAuditors(stage.id, stage.times - 1);
- const onAuditor = _.find(lastAuditors, { status: status.checkNo });
- if (onAuditor.aid === accountId) {
- stage.cancancel = 4;// 审批人撤回退回原报
- }
- }
- }
- const lastRevise = yield this.service.ledgerRevise.getLastestRevise(this.tender.id);
- stage.revising = (lastRevise && lastRevise.status !== reviseStatus.checked) || false;
- this.stage = stage;
- // 根据状态判断是否需要更新审批人列表
- if ((stage.status === status.uncheck || stage.status === status.checkNo) && this.tender.info.shenpi.stage !== shenpiConst.sp_status.sqspr) {
- const shenpi_status = this.tender.info.shenpi.stage;
- // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
- const auditList = yield this.service.stageAudit.getAllDataByCondition({ where: { sid: stage.id, times: stage.times }, orders: [['order', 'asc']] });
- const auditIdList = _.map(auditList, 'aid');
- if (shenpi_status === shenpiConst.sp_status.gdspl) {
- const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: stage.tid, sp_type: shenpiConst.sp_type.stage, sp_status: shenpi_status } });
- const shenpiIdList = _.map(shenpiList, 'audit_id');
- // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
- if (!_.isEqual(auditIdList, shenpiIdList)) {
- yield this.service.stageAudit.updateNewAuditList(stage, shenpiIdList);
- }
- } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
- const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: stage.tid, sp_type: shenpiConst.sp_type.stage, sp_status: shenpi_status });
- // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
- if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {
- yield this.service.stageAudit.updateLastAudit(stage, auditList, shenpiInfo.audit_id);
- } else if (!shenpiInfo) {
- // 不存在终审人的状态下这里恢复为授权审批人
- this.tender.info.shenpi.stage = shenpiConst.sp_status.sqspr;
- }
- }
- }
- yield next;
- } catch (err) {
- console.log(err);
- this.helper.log(err);
- // 输出错误到日志
- if (err.stack) {
- this.logger.error(err);
- } else {
- this.getLogger('fail').info(JSON.stringify({
- error: err,
- project: this.session.sessionProject,
- user: this.session.sessionUser,
- body: this.session.body,
- }));
- }
- // 重定向值标段管理
- this.redirect(this.request.headers.referer);
- }
- };
- };
|