| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 | 'use strict';/** * * * @author Ellisran * @date 2020/10/15 * @version */const status = require('../const/audit').ledger.status;const shenpiConst = require('../const/shenpi');const _ = require('lodash');const checkAuditFlow = async function(ctx) {    const tender = ctx.tender.data;    const info = ctx.tender.info;    if (info.shenpi.ledger === shenpiConst.sp_status.sqspr) return;    if (tender.ledger_status !== status.uncheck && tender.ledger_status !== status.checkNo) return;    const shenpi_status = info.shenpi.ledger;    // 进一步比较审批流是否与审批流程设置的相同,不同则替换为固定审批流或固定的终审    const auditList = await ctx.service.ledgerAudit.getAuditors(tender.id, tender.ledger_times);    if (shenpi_status === shenpiConst.sp_status.gdspl) {        const shenpiList = await ctx.service.shenpiAudit.getAllDataByCondition({ where: { tid: tender.id, sp_type: shenpiConst.sp_type.ledger, 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.audit_id; });                if (!shenpi || shenpi.audit_order !== audit.audit_order || shenpi.audit_type !== audit.audit_type || shenpi.audit_ledger_id !== audit.audit_ledger_id) {                    sameAudit = false;                    break;                }            }        }        if (!sameAudit) await ctx.service.ledgerAudit.updateNewAuditList(tender, shenpiList);    } else if (shenpi_status === shenpiConst.sp_status.gdzs) {        const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: tender.id, sp_type: shenpiConst.sp_type.ledger, sp_status: shenpi_status });        // 判断最后一个id是否与固定终审id相同,不同则删除原审批流中如果存在的id和添加终审        const lastAuditors = auditList.filter(x => { x.audit_order === auditList[auditList.length - 1].audit_order; });        if (shenpiInfo && (lastAuditors.length === 0 || (lastAuditors.length > 1 || shenpiInfo.audit_id !== lastAuditors[0].audit_id))) {            await ctx.service.ledgerAudit.updateLastAudit(tender, auditList, shenpiInfo.audit_id);        } else if (!shenpiInfo) {            // 不存在终审人的状态下这里恢复为授权审批人            info.shenpi.ledger = shenpiConst.sp_status.sqspr;        }    }};module.exports = options => {    /**     * 标段校验 中间件     * 1. 读取标段数据(包括属性)     * 2. 检验用户是否可见标段(不校验具体权限)     *     * @param {function} next - 中间件继续执行的方法     * @return {void}     */    return function* ledgerAuditCheck(next) {        try {            yield checkAuditFlow(this);            yield next;        } catch (err) {            this.log(err);            // 重定向值标段管理            this.redirect(this.request.headers.referer);        }    };};
 |