| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 | 'use strict';/** * * * @author Mai * @date 2018/6/12 * @version */const audit = require('../const/audit').revise;module.exports = app => {    class LedgerRevise extends app.BaseService {        /**         * 构造函数         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        constructor(ctx) {            super(ctx);            this.tableName = 'ledger_revise';        }        /**         * 获取标段下,修订(分页,且按时间倒序)         * @param {Number}tid - 标段id         * @returns {Promise<*>} - ledger_change下所有数据,并关联 project_account(读取提交人名称、单位、公司)         */        async getReviseList (tid) {            const sql = 'SELECT lc.*, pa.name As user_name, pa.role As user_role, pa.company As user_company' +                '  FROM ' + this.tableName + ' As lc' +                '  INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON lc.uid = pa.id' +                '  WHERE lc.tid = ?' +                '  ORDER BY lc.in_time DESC' +                '  LIMIT ?, ?';            const Len = this.app.config.pageSize;            const sqlParam = [tid, (this.ctx.page - 1) * Len, Len];            return await this.db.query(sql, sqlParam);        }        async getLastestRevise(tid) {            const sql = 'SELECT lc.*, pa.name As user_name, pa.role As user_role, pa.company As user_company' +                '  FROM ' + this.tableName + ' As lc' +                '  INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON lc.uid = pa.id' +                '  WHERE lc.tid = ?' +                '  ORDER BY lc.in_time DESC' +                '  LIMIT 0, 1';            const sqlParam = [tid];            return await this.db.queryOne(sql, sqlParam);            // const revise = await this.db.select(this.tableName, {            //     where: {tid: tid},            //     orders: [['in_time', 'DESC']],            //     limit: 1,            //     offset: 0,            // });            // return revise.length > 0 ? revise[0] : null;        }        /**         * 获取新增修订的序号         * @param {Number}tid - 标段id         * @returns {Promise<number>}         */        async getNewOrder(tid) {            const sql = 'SELECT Max(`corder`) As max_order FROM ' + this.tableName + ' Where `tid` = ? and `valid`';            const sqlParam = [tid];            const result = await this.db.queryOne(sql, sqlParam);            return result.max_order || 0;            // if (result && result.max_order) {            //     return result.max_order;            // } else {            //     return 0;            // }        }        async _initReviseBills(transaction, tid) {            const sql = 'Insert Into ' + this.ctx.service.reviseBills.tableName +                '  (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +                '     quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +                '     sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id)' +                '  Select id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +                '      quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +                '      sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id' +                '  From ' + this.ctx.service.ledger.tableName +                '  Where `tender_id` = ?';            const sqlParam = [tid];            await transaction.query(sql, sqlParam);        }        async _initRevisePos(transaction, tid) {            const sql = 'Insert Into ' + this.ctx.service.revisePos.tableName +                '  (id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +                '     sgfh_qty, sjcl_qty, qtcl_qty, crid)' +                '  Select id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +                '     sgfh_qty, sjcl_qty, qtcl_qty, crid' +                '  From ' + this.ctx.service.pos.tableName +                '  Where `tid` = ?';            const sqlParam = [tid];            await transaction.query(sql, sqlParam);        }        /**         * 新增修订         * @param {Number}tid - 标段id         * @param {Number}uid - 提交人id         * @returns {Promise<void>}         */        async add(tid, uid) {            if (!tid && !uid) {                throw '数据错误';            }            const maxOrder = await this.getNewOrder(tid);            const data = {                id: this.uuid.v4(), tid: tid, uid: uid,                corder: maxOrder + 1, in_time: new Date(), status: audit.status.uncheck,            };            const transaction = await this.db.beginTransaction();            try {                const result = await transaction.insert(this.tableName, data);                if (result.affectedRows !== 1) {                    throw '新增台账修订失败';                }                await transaction.delete(this.ctx.service.reviseBills.tableName, {tender_id: tid});                await transaction.delete(this.ctx.service.revisePos.tableName, {tid: tid});                await this._initReviseBills(transaction, tid, data.id);                await this._initRevisePos(transaction, tid, data.id);                await transaction.commit();                return data;            } catch(err) {                await transaction.rollback();                throw err;            }        }        /**         * 作废修订         * @param id         * @returns {Promise<void>}         */        async cancelRevise(id) {            const result = await this.db.update(this.tableName, {id: id, valid: false});            return result.affectedRows === 1;        }    }    return LedgerRevise;};
 |