| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 | 'use strict';/** * 版本数据模型 * * @author CaiAoLin * @date 2017/10/25 * @version */const shenpiConst = require('../const/shenpi');module.exports = app => {    class ShenpiAudit extends app.BaseService {        /**         * 构造函数         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        constructor(ctx) {            super(ctx);            this.tableName = 'shenpi_audit';        }        async getAudit(tid, type, status) {            const sql = 'SELECT sp.audit_id, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +                ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ?';            const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status];            return await this.db.queryOne(sql, sqlParam);        }        async getAuditList(tid, type, status) {            const sql = 'SELECT sp.audit_id, pa.name FROM ?? AS sp LEFT JOIN ?? AS pa ON sp.audit_id = pa.id' +                ' WHERE sp.tid = ? AND sp.sp_type = ? AND sp.sp_status = ? ORDER BY sp.id ASC';            const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid, type, status];            return await this.db.query(sql, sqlParam);        }        async addAudit(data) {            const transaction = await this.db.beginTransaction();            try {                if (parseInt(data.code) === shenpiConst.sp_type.stage && parseInt(data.status) === shenpiConst.sp_status.gdspl) {                    const options = {                        where: {                            tid: this.ctx.tender.id,                            user_id: data.audit_id,                        },                    };                    const updateData = {                        status: 1,                    };                    await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);                }                const insertData = {                    tid: this.ctx.tender.id,                    sp_type: data.code,                    sp_status: data.status,                    audit_id: data.audit_id,                };                const result = await transaction.insert(this.tableName, insertData);                await transaction.commit();                return result.effectRows === 1;            } catch (err) {                await transaction.rollback();                throw err;            }        }        async removeAudit(data) {            const transaction = await this.db.beginTransaction();            try {                const delData = {                    tid: this.ctx.tender.id,                    sp_type: data.code,                    sp_status: data.status,                    audit_id: data.audit_id,                };                await transaction.delete(this.tableName, delData);                if (parseInt(data.code) === shenpiConst.sp_type.stage && parseInt(data.status) === shenpiConst.sp_status.gdspl) {                    const options = {                        where: {                            tid: this.ctx.tender.id,                            user_id: data.audit_id,                        },                    };                    const updateData = {                        status: 0,                    };                    await transaction.update(this.ctx.service.ledgerCooperation.tableName, updateData, options);                }                await transaction.commit();                return true;            } catch (err) {                await transaction.rollback();                throw err;            }        }        async copyAudit2otherTender(data) {            const transaction = await this.db.beginTransaction();            try {                const shenpi_status = parseInt(data.status);                // 1.复制修改当前审批到其他的tender_info里                // 2.删除其他的shenpiAudit                // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)                const tenderInfoUpdateList = [];                const tenders = [];                for (const tid of data.tidList.split(',')) {                    // 获取原报                    const tender = await this.ctx.service.tender.getDataById(tid);                    if (tender) {                        tenders.push({ id: parseInt(tid), user_id: tender.user_id });                        const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(tid);                        // 把当前期状态复制到其他标段里                        if (shenpiInfo[data.code] !== shenpi_status) {                            shenpiInfo[data.code] = shenpi_status;                            tenderInfoUpdateList.push({ row: { shenpi: JSON.stringify(shenpiInfo) }, where: { tid: parseInt(tid) } });                        }                    }                }                if (tenderInfoUpdateList.length > 0) await transaction.updateRows(this.ctx.service.tenderInfo.tableName, tenderInfoUpdateList);                const insertList = [];                const needYB = ['ledger', 'revise', 'change'];                const canYB = needYB.indexOf(data.code) !== -1;                for (const t of tenders) {                    if (shenpi_status !== shenpiConst.sp_status.sqspr) {                        await transaction.delete(this.tableName, { tid: t.id, sp_type: shenpiConst.sp_type[data.code], sp_status: shenpi_status });                        for (const aid of data.aidList.split(',')) {                            if (aid && parseInt(aid) !== t.user_id || (parseInt(aid) === t.user_id && canYB)) {                                const insertData = {                                    tid: t.id,                                    sp_type: shenpiConst.sp_type[data.code],                                    sp_status: shenpi_status,                                    audit_id: parseInt(aid),                                };                                insertList.push(insertData);                            }                        }                    }                }                // console.log(tenderInfoUpdateList, insertList);                if (insertList.length > 0) await transaction.insert(this.tableName, insertList);                await transaction.commit();                return true;            } catch (err) {                await transaction.rollback();                throw err;            }        }        async copyAudit2otherShenpi(data) {            const transaction = await this.db.beginTransaction();            try {                const shenpi_status = parseInt(data.status);                // 1.修改当前审批到它的tender_info里                // 2.删除相同审批状态的shenpiAudit                // 3.添加新的shenpiAudit(还要针对该标段是否为原报进行处理)                const insertList = [];                const needYB = ['ledger', 'revise', 'change'];                const shenpiInfo = await this.ctx.service.tenderInfo.getTenderShenpiInfo(this.ctx.tender.id);                for (const code of data.shenpiList.split(',')) {                    // 把当前审批状态复制到其他标段里                    if (shenpiInfo[code] !== shenpi_status) {                        shenpiInfo[code] = shenpi_status;                    }                    if (shenpiInfo[code] !== shenpiConst.sp_status.sqspr) {                        await transaction.delete(this.tableName, { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type[code], sp_status: shenpiInfo[code] });                        for (const aid of data.aidList.split(',')) {                            if (aid && parseInt(aid) !== this.ctx.tender.data.user_id || (parseInt(aid) === this.ctx.tender.data.user_id && needYB.indexOf(code) !== -1)) {                                const insertData = {                                    tid: this.ctx.tender.id,                                    sp_type: shenpiConst.sp_type[code],                                    sp_status: shenpi_status,                                    audit_id: parseInt(aid),                                };                                insertList.push(insertData);                            }                        }                    }                }                await transaction.update(this.ctx.service.tenderInfo.tableName,                    {                        shenpi: JSON.stringify(shenpiInfo),                    },                    {                        where: {                            tid: this.ctx.tender.id,                        },                    });                if (insertList.length > 0) await transaction.insert(this.tableName, insertList);                await transaction.commit();                return true;            } catch (err) {                await transaction.rollback();                throw err;            }        }    }    return ShenpiAudit;};
 |