123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 'use strict';
- /**
- *
- *
- * @author Ellisran
- * @date 2020/10/15
- * @version
- */
- const status = require('../const/audit').change.status;
- const shenpiConst = require('../const/shenpi');
- const _ = require('lodash');
- module.exports = options => {
- /**
- * 标段校验 中间件
- * 1. 读取标段数据(包括属性)
- * 2. 检验用户是否可见标段(不校验具体权限)
- *
- * @param {function} next - 中间件继续执行的方法
- * @return {void}
- */
- return function* changeAuditCheck(next) {
- try {
- // 获取revise
- const cid = this.params.cid || this.request.body.cid;
- if (!cid) {
- throw '您访问的变更令不存在';
- }
- // const change = yield this.service.change.getDataByCondition({ cid });
- if (!this.change) {
- const change = yield this.service.change.getDataByCondition({ cid });
- if (!change) throw '变更令数据有误';
- yield this.service.change.loadChangeUser(change);
- this.change = change;
- }
- const change = this.change;
- if ((change.status === status.uncheck || change.status === status.checkNo || change.status === status.revise) && this.tender.info.shenpi.change !== shenpiConst.sp_status.sqspr) {
- const shenpi_status = this.tender.info.shenpi.change;
- // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审
- const auditList = yield this.service.changeAudit.getAllDataByCondition({ where: { cid: change.cid, times: change.times }, orders: [['usite', 'asc']] });
- auditList.shift();
- const auditIdList = _.map(auditList, 'uid');
- if (shenpi_status === shenpiConst.sp_status.gdspl) {
- // 判断并获取审批组
- const group = yield this.service.shenpiGroup.getSelectGroupByChangeType(this.tender.id, shenpiConst.sp_type.change, 'change', change.sp_group);
- if ((group && change.sp_group !== group.id) || (!group && change.sp_group !== 0)) {
- change.sp_group = group ? group.id : 0;
- yield this.service.change.defaultUpdate({ sp_group: change.sp_group }, { where: { cid: change.cid } });
- }
- const condition = { tid: this.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpi_status, sp_group: change.sp_group };
- const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: condition, orders: [['audit_order', 'asc']] });
- if (change.sp_group === 0 && shenpiList.length === 0) {
- this.tender.info.shenpi.change = shenpiConst.sp_status.sqspr;
- } else {
- // const shenpiIdList = _.map(shenpiList, 'audit_id');
- // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流
- let sameAudit = auditList.length === shenpiList.length;
- if (sameAudit) {
- for (const audit of auditList) {
- const shenpi = shenpiList.find(x => { return x.audit_id === audit.uid; });
- if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {
- sameAudit = false;
- break;
- }
- }
- }
- if (!sameAudit) {
- yield this.service.changeAudit.updateNewAuditList(change, shenpiList);
- yield this.service.change.loadChangeUser(change);
- yield this.service.change.doCheckChangeCanCancel(change);
- }
- }
- } else if (shenpi_status === shenpiConst.sp_status.gdzs) {
- const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: this.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpi_status });
- // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审
- const lastAuditors = auditList.filter(x => { return x.usite === auditList.length - 1; });
- if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].uid))) {
- yield this.service.changeAudit.updateLastAudit(change, auditList, shenpiInfo.audit_id);
- yield this.service.change.loadChangeUser(change);
- yield this.service.change.doCheckChangeCanCancel(change);
- } else if (!shenpiInfo) {
- // 不存在终审人的状态下这里恢复为授权审批人
- this.tender.info.shenpi.change = shenpiConst.sp_status.sqspr;
- }
- }
- }
- yield next;
- } catch (err) {
- console.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);
- }
- };
- };
|