| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 | 'use strict';/** * * * @author Mai * @date 2018/8/14 * @version */const auditConst = require('../const/audit').changeProject;const pushType = require('../const/audit').pushType;module.exports = app => {    class ChangeProjectXsAudit extends app.BaseService {        /**         * 构造函数         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        constructor(ctx) {            super(ctx);            this.tableName = 'change_project_xs_audit';        }        /**         * 获取协审人列表         *         * @param auditorId         * @return {Promise<*>}         */        async getAuditList(changeId) {            const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`cpid`, la.`aid` ' +                '  FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +                '  WHERE la.`cpid` = ? ORDER BY la.`id`';            const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, changeId];            return await this.db.query(sql, sqlParam);        }        /**         * 获取单个协审人         *         * @param auditorId         * @return {Promise<*>}         */        async getOneAudit(cpid, aid) {            const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`cpid`, la.`aid` ' +                '  FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +                '  WHERE la.`cpid` = ? AND la.`aid` = ?';            const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpid, aid];            return await this.db.queryOne(sql, sqlParam);        }        /**         * 新增协审人         *         * @param {Number} cpId - 立项书id         * @param {Number} auditorId - 审核人id         * @param {Number} times - 第几次审批         * @return {Promise<number>}         */        async addAuditor(cpId, auditorId) {            const transaction = await this.db.beginTransaction();            let flag = false;            try {                const data = {                    tid: this.ctx.tender.id,                    cpid: cpId,                    aid: auditorId,                    for_aid: this.ctx.session.sessionUser.accountId,                    in_time: new Date(),                };                const result = await transaction.insert(this.tableName, data);                await transaction.commit();                flag = result.effectRows = 1;            } catch (err) {                await transaction.rollback();                throw err;            }            return flag;        }        /**         * 移除协审人         *         * @param {Number} cpId - 变更立项书id         * @param {Number} auditorId - 审核人id         * @param {Number} times - 第几次审批         * @return {Promise<boolean>}         */        async deleteAuditor(cpId, auditorId) {            const transaction = await this.db.beginTransaction();            try {                const condition = { cpid: cpId, aid: auditorId };                const auditor = await this.getDataByCondition(condition);                if (!auditor) {                    throw '该审核人不存在';                }                await transaction.delete(this.tableName, condition);                await transaction.commit();            } catch (err) {                await transaction.rollback();                throw err;            }            return true;        }        /**         * 获取审核人需要审核的期列表         *         * @param auditorId         * @return {Promise<*>}         */        async getAuditChangeProject(auditorId) {            const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`cpid`,' +                '    m.`status` As `mstatus`, m.`code` As `mcode`,' +                '    t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +                '  FROM ?? AS ma, ?? AS m, ?? As t ' +                '  WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +                '    and ma.`cpid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`begin_time` DESC';            const sqlParam = [this.tableName, this.ctx.service.changeProject.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.back, auditConst.status.back];            return await this.db.query(sql, sqlParam);        }        /**         * 复制上一期的协审人列表给最新一期         *         * @param transaction - 新增一期的事务         * @param {Object} preMaterial - 上一期         * @param {Object} newaMaterial - 最新一期         * @return {Promise<*>}         */        async copyPreChangeProjectXsAuditors(transaction, preChange, newChange) {            const auditors = await this.getAuditList(preChange.id);            const newAuditors = [];            for (const a of auditors) {                const na = {                    tid: preChange.tid,                    cpid: newChange.id,                    aid: a.aid,                    for_aid: preChange.for_aid,                    in_time: new Date(),                };                newAuditors.push(na);            }            const result = await transaction.insert(this.tableName, newAuditors);            return result.affectedRows === auditors.length;        }        async getAllAuditors(tenderId) {            const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +                '  LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +                '  WHERE t.id = ?' +                '  GROUP BY  ma.aid';            const sqlParam = [tenderId];            return this.db.query(sql, sqlParam);        }    }    return ChangeProjectXsAudit;};
 |