| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 | 'use strict';/** * * * @author Mai * @date * @version */const messageType = require('../const/message_type');const status = require('../const/audit').stage.status;const shenpiConst = require('../const/shenpi');const _ = require('lodash');const paymentConst = require('../const/payment');module.exports = options => {    /**     * 标段校验 中间件     * 1. 读取标段数据(包括属性)     * 2. 检验用户是否可见标段(不校验具体权限)     *     * @param {function} next - 中间件继续执行的方法     * @return {void}     */    return function* paymentDetailCheck(next) {        try {            const id = parseInt(this.params.did);            if (!id) throw '参数错误';            const detail = yield this.service.paymentDetail.getDataById(id);            if (!detail) {                throw '支付审批表单不存在';            }            detail.decimal = detail.bills_decimal ? JSON.parse(detail.bills_decimal) : { qty: 3, tp: 2, up: 2};            const trInfo = yield this.service.paymentTenderRpt.getDataById(detail.tr_id);            if (!trInfo) {                throw '支付审批报表不存在';            }            // 读取原报、审核人数据            detail.auditors = yield this.service.paymentDetailAudit.getAuditors(detail.id, detail.times);            detail.curAuditor = yield this.service.paymentDetailAudit.getCurAuditor(detail.id, detail.times);            detail.rptAudits = yield this.service.paymentRptAudit.getListByDetail(detail.id);            const accountId = this.session.sessionUser.accountId,                auditorIds = _.map(detail.auditors, 'aid'),                rptAuditIds = _.map(detail.rptAudits, 'uid');            if (accountId === detail.uid) { // 原报                detail.curTimes = detail.times;                if (detail.status === status.uncheck || detail.status === status.checkNo) {                    detail.curOrder = 0;                } else if (detail.status === status.checked) {                    detail.curOrder = _.max(_.map(detail.auditors, 'order'));                } else {                    detail.curOrder = detail.curAuditor.aid === accountId ? detail.curAuditor.order : detail.curAuditor.order - 1;                }                detail.filePermission = true;            } else if (auditorIds.indexOf(accountId) !== -1 || rptAuditIds.indexOf(accountId) !== -1 || this.payment.auditPermission.view_all) { // 审批人及签署人及查看所有权人                if (detail.status === status.uncheck) {                    throw '您无权查看该数据';                }                // detail.readOnly = detail.status !== status.checking || accountId !== detail.curAuditor.aid;                detail.curTimes = detail.status === status.checkNo ? detail.times - 1 : detail.times;                if (detail.status === status.checked) {                    detail.curOrder = _.max(_.map(detail.auditors, 'order'));                } else if (detail.status === status.checkNo) {                    const audit = this.service.paymentDetailAudit.getDataByCondition({                        td_id: detail.id, times: detail.times, status: status.checkNo,                    });                    detail.curOrder = audit.order;                } else {                    detail.curOrder = accountId === detail.curAuditor.aid ? detail.curAuditor.order : detail.curAuditor.order - 1;                }                detail.filePermission = auditorIds.indexOf(accountId) !== -1 || rptAuditIds.indexOf(accountId) !== -1;            } else { // 其他不可见                throw '您无权查看该数据';            }            // 获取最新的期            detail.highOrder = yield this.service.paymentDetail.count({                tr_id: detail.tr_id,            });            detail.readOnly = !((detail.status === status.uncheck || detail.status === status.checkNo) && accountId === detail.uid);            if (detail.readOnly && detail.type === paymentConst.modes_value_object.safe) {                if ((detail.status === status.checking || detail.status === status.checkNoPre) && detail.curAuditor && detail.curAuditor.aid === accountId) {                    detail.readOnly = false;                }            }            this.detail = detail;            this.trInfo = trInfo;            // 根据状态判断是否需要更新审批人列表            if ((detail.status === status.uncheck || detail.status === status.checkNo) && trInfo.sp_status !== shenpiConst.sp_status.sqspr) {                const shenpi_status = trInfo.sp_status;                // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审                const auditList = yield this.service.paymentDetailAudit.getAllDataByCondition({ where: { td_id: detail.id, times: detail.times }, orders: [['order', 'asc']] });                const auditIdList = _.map(auditList, 'aid');                if (shenpi_status === shenpiConst.sp_status.gdspl) {                    const shenpiList = yield this.service.paymentShenpiAudit.getAllDataByCondition({ where: { tr_id: trInfo.id, sp_status: shenpi_status } });                    const shenpiIdList = _.map(shenpiList, 'audit_id');                    // 判断2个id数组是否相同,不同则删除原审批流,切换成固定的审批流                    if (!_.isEqual(auditIdList, shenpiIdList)) {                        yield this.service.paymentDetailAudit.updateNewAuditList(detail, shenpiIdList);                    }                } else if (shenpi_status === shenpiConst.sp_status.gdzs) {                    const shenpiInfo = yield this.service.paymentShenpiAudit.getDataByCondition({ tr_id: trInfo.id, sp_status: shenpi_status });                    // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审                    if (shenpiInfo && shenpiInfo.audit_id !== _.last(auditIdList)) {                        yield this.service.paymentDetailAudit.updateLastAudit(detail, auditList, shenpiInfo.audit_id);                    } else if (!shenpiInfo) {                        // 不存在终审人的状态下这里恢复为授权审批人                        this.trInfo.sp_status = shenpiConst.sp_status.sqspr;                    }                }            }            yield next;        } catch (err) {            // 输出错误到日志            if (err.stack) {                this.logger.error(err);            } else {                this.session.message = {                    type: messageType.ERROR,                    icon: 'exclamation-circle',                    message: err,                };                this.getLogger('fail').info(JSON.stringify({                    error: err,                    project: this.session.sessionProject,                    user: this.session.sessionUser,                    body: this.session.body,                }));            }            if (this.helper.isAjax(this.request)) {                if (err.stack) {                    this.body = {err: 4, msg: '标段数据未知错误', data: null};                } else {                    this.body = {err: 3, msg: err.toString(), data: null};                }            } else {                if (this.helper.isWap(this.request)) {                    this.redirect('/wap/subproj');                } else {                    err === '您无权查看该内容' ? this.redirect(this.request.headers.referer) : this.redirect('/sp/' + this.subProject.id + '/payment');                }            }        }    };};
 |