| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 | 'use strict';/** * * @author EllisRan * @date * @version */const status = require('../const/audit').material.status;const shenpiConst = require('../const/shenpi');const materialConst = require('../const/material');const _ = require('lodash');module.exports = options => {    /**     * 材料调差校验 中间件     * 1. 读取材料调差数据     * 2. 检验用户是否参与材料调差(不校验具体权限)     *     * 写入ctx.material数据     * 其中:     * material.auditors: 审批人列表(退回原报时,加载上一流程)     * material.curAuditor: 当前审批人(未上报为空,审批通过 or 退回原报时,为空)     * material.readonly: 登录人,是否可操作     * material.curTimes: 当前登录人,操作、查阅数据times     * material.curOrder: 当前登录人,操作、查阅数据order     *     * 该方法为通用方法,如需material其他数据,请在controller中查询     *     * @param {function} next - 中间件继续执行的方法     * @return {void}     */    return function* materialCheck(next) {        try {            if (!this.subProject.page_show.openMaterial) {                throw '该功能已关闭';            }            // 读取标段数据            const materialOrder = parseInt(this.params.order);            if (materialOrder <= 0) {                throw '您访问的期不存在';            }            const material = yield this.service.material.getDataByCondition({                tid: this.tender.id,                order: materialOrder,            });            if (!material) {                throw '材料调差期数据错误';            }            // 读取原报、审核人数据            yield this.service.material.loadMaterialUser(material);            const openMaterialTax = this.subProject.page_show.openMaterialTax;            if ((material.status === status.uncheck || material.status === status.checkNo) && material.material_tax !== openMaterialTax) {                yield this.service.material.updateMaterialTax(material.id, openMaterialTax);                material.material_tax = openMaterialTax;            }            // 读取原报、审核人数据            // material.auditors = yield this.service.materialAudit.getAuditors(material.id, material.times);            // if (material.status > 1) {            //     material.auditors2 === 2 ? material.auditors : (yield this.service.materialAudit.getAuditors(material.id, material.times - 1));            // }            // material.curAuditor = yield this.service.materialAudit.getCurAuditor(material.id, material.times);            // 权限相关            // todo 校验权限 (标段参与人、分享)            const accountId = this.session.sessionUser.accountId,                auditorIds = _.map(material.auditors, 'aid'),                shareIds = [];            if (accountId === material.user_id) { // 原报                material.filePermission = true;            } else if (this.tender.isTourist) {                material.filePermission = this.tender.touristPermission.file || auditorIds.indexOf(accountId) !== -1;            } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人                if (material.status === status.uncheck) {                    throw '您无权查看该数据';                }                // material.readOnly = material.status !== status.checking || accountId !== material.curAuditor.aid;                material.filePermission = true;            } else if (shareIds.indexOf(accountId) !== -1) { // 分享人                if (material.status === status.uncheck) {                    throw '您无权查看该数据';                }                material.filePermission = false;            } else if (this.session.sessionUser.is_admin) {                material.filePermission = true;            } else { // 其他不可见                throw '您无权查看该数据';            }            // 获取最新的期            material.highOrder = yield this.service.material.count({                tid: this.tender.id,            });            // 调差的readOnly 指表格和页面只能看不能改,和审批无关            material.readOnly = !(((material.status === status.uncheck || material.status === status.checkNo) && accountId === material.user_id)                || (this.subProject.page_show.openMaterialEditForAudit && (material.status === status.checking || material.status === status.checkNoPre) && material.curAuditorIds.indexOf(accountId) !== -1));            material.editForAudit = this.subProject.page_show.openMaterialEditForAudit && (material.status === status.checking || material.status === status.checkNoPre) && material.curAuditorIds.indexOf(accountId) !== -1;            material.decimal = material.decimal ? JSON.parse(material.decimal) : materialConst.decimal;            // 判断stage流程可否撤回,是哪一种撤回            // yield this.service.material.doCheckMaterialCanCancel(material);            this.material = material;            // 根据状态判断是否需要更新审批人列表            if ((material.status === status.uncheck || material.status === status.checkNo) && this.tender.info.shenpi.material !== shenpiConst.sp_status.sqspr) {                const shenpi_status = this.tender.info.shenpi.material;                // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审                const auditList = yield this.service.materialAudit.getAllDataByCondition({ where: { mid: material.id, times: material.times }, orders: [['order', 'asc']] });                if (shenpi_status === shenpiConst.sp_status.gdspl) {                    const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status } });                    // 判断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.materialAudit.updateNewAuditList(material, shenpiList);                        yield this.service.material.loadMaterialUser(material);                    }                } else if (shenpi_status === shenpiConst.sp_status.gdzs) {                    const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status });                    // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审                    const lastAuditors = auditList.filter(x => { x.order === auditList[auditList.length - 1].order; });                    if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].aid))) {                        yield this.service.materialAudit.updateLastAudit(material, auditList, shenpiInfo.audit_id);                        yield this.service.material.loadMaterialUser(material);                    } else if (!shenpiInfo) {                        // 不存在终审人的状态下这里恢复为授权审批人                        this.tender.info.shenpi.material = shenpiConst.sp_status.sqspr;                    }                }                // const auditIdList = _.map(auditList, 'aid');                // if (shenpi_status === shenpiConst.sp_status.gdspl) {                //     const shenpiList = yield this.service.shenpiAudit.getAllDataByCondition({ where: { tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status } });                //     const shenpiIdList = _.map(shenpiList, 'audit_id');                //     // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流                //     if (!_.isEqual(auditIdList, shenpiIdList)) {                //         yield this.service.materialAudit.updateNewAuditList(material, shenpiIdList);                //     }                // } else if (shenpi_status === shenpiConst.sp_status.gdzs) {                //     const shenpiInfo = yield this.service.shenpiAudit.getDataByCondition({ tid: material.tid, sp_type: shenpiConst.sp_type.material, sp_status: shenpi_status });                //     // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审                //     if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {                //         yield this.service.materialAudit.updateLastAudit(material, auditList, shenpiInfo.audit_id);                //     } else if (!shenpiInfo) {                //         // 不存在终审人的状态下这里恢复为授权审批人                //         this.tender.info.shenpi.material = 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,                }));            }            // 重定向值标段管理            if (err === '您无权查看该数据') {                this.tender ? this.redirect('/tender/' + this.tender.id + '/measure/material') : this.redirect(`/sp/${ctx.subProject.id}/list`);            }            this.redirect(this.request.headers.referer);        }    };};
 |