| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | 'use strict';/** * * * @author Ellisran * @date 2020/10/15 * @version */const status = require('../const/audit').changeProject.status;const shenpiConst = require('../const/shenpi');const _ = require('lodash');module.exports = options => {    /**     * 标段校验 中间件     * 1. 读取标段数据(包括属性)     * 2. 检验用户是否可见标段(不校验具体权限)     *     * @param {function} next - 中间件继续执行的方法     * @return {void}     */    return function* changeProjectAuditCheck(next) {        try {            // 获取revise            const id = this.params.cpid || this.request.body.cpid;            if (!id) {                throw '您访问的变更立项不存在';            }            // const change = yield this.service.change.getDataByCondition({ cid });            if (!this.change) {                const change = yield this.service.changeProject.getDataById(id);                if (!change) throw '变更立项数据有误';                yield this.service.changeProject.loadChangeUser(change);                this.change = change;            }            const change = this.change;            if ((change.status === status.uncheck || change.status === status.back || 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.changeProjectAudit.getAllDataByCondition({ where: { cpid: change.id, times: change.times }, orders: [['order', 'asc']] });                if (shenpi_status === shenpiConst.sp_status.gdspl) {                    // 判断并获取审批组                    const group = yield this.service.shenpiGroup.getSelectGroupByChangeType(this.tender.id, shenpiConst.sp_type.change, 'changeProject', 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.changeProject.defaultUpdate({ id: change.id, sp_group: change.sp_group });                    }                    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']] });                    yield this.service.shenpiAudit.noYbShenpiList(change.uid, shenpiList);                    if (change.sp_group === 0 && shenpiList.length === 0) {                        this.tender.info.shenpi.change = shenpiConst.sp_status.sqspr;                    } else {                        // 判断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.aid;                                });                                if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type) {                                    sameAudit = false;                                    break;                                }                            }                        }                        if (!sameAudit) {                            yield this.service.changeProjectAudit.updateNewAuditList(change, shenpiList);                            yield this.service.changeProject.loadChangeUser(change);                            yield this.service.changeProject.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 });                    if (!shenpiInfo || (shenpiInfo && shenpiInfo.audit_id === change.uid)) {                        // 不存在终审人 或 存在终审但与原报相同时, 这里恢复为授权审批人                        this.tender.info.shenpi.change = shenpiConst.sp_status.sqspr;                    } else {                        // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审                        const lastAuditors = auditList.filter(x => { return x.order === auditList.length - 1; });                        if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].aid))) {                            yield this.service.changeProjectAudit.updateLastAudit(change, auditList, shenpiInfo.audit_id);                            yield this.service.changeProject.loadChangeUser(change);                            yield this.service.changeProject.doCheckChangeCanCancel(change);                        }                    }                }            }            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);        }    };};
 |