| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 | 'use strict';/** * * * @author Mai * @date * @version */const validField = ['lid', 'share', 'color', 'comment'];module.exports = app => {    class StageBillsDgn extends app.BaseService {        /**         * 构造函数         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        constructor(ctx) {            super(ctx);            this.tableName = 'ledger_tag';        }        /**         * 获取台账、期所有标段         * @param {Number} tid - 标段id         * @param {Number} sid - 期id(-1时查询台账分解全部标签)         * @returns {Promise<void>}         */        async getDatas(tid, sid = -1) {            // return await this.db.select(this.tableName, {            //     where: {tid: tid, sid: -1},            //     columns: ['id', 'uid', 'lid', 'share', 'color', 'comment'],            //     orders: [['create_time', 'desc']],            // });            const sql = 'SELECT id, uid, lid, share, color, comment FROM ' + this.tableName +                '  WHERE tid = ? and sid = ? and (uid = ? or share) ORDER BY create_time DESC';            return await this.db.query(sql, [tid, sid, this.ctx.session.sessionUser.accountId]);        }        /**         * 过滤无效字段,容错         * @param data         * @private         */        _filterInvalidField(data) {            for (const prop in data) {                if (validField.indexOf(prop) === -1) {                    delete data[prop];                }            }        }        async _addTag(data) {            this._filterInvalidField(data);            data.create_time = new Date();            data.modify_time = data.create_time;            data.uid = this.ctx.session.sessionUser.accountId;            data.tid = this.ctx.tender.id;            data.pid = this.ctx.tender.data.project_id;            if (this.ctx.stage) {                data.sid = this.ctx.stage.id;                data.sorder = this.ctx.stage.order;            }            const result = await this.db.insert(this.tableName, data);            data.id = result.insertId;            return data;        }        async _delTag(id) {            const tag = await this.getDataById(id);            if (tag.uid !== this.ctx.session.sessionUser.accountId) throw '您无权删除该数据';            await this.deleteById(id);            return id;        }        async _updateTag(data) {            const tag = await this.getDataById(data.id);            if (tag.uid !== this.ctx.session.sessionUser.accountId) throw '您无权修改该数据';            this._filterInvalidField(data);            data.modify_time = new Date();            data.id = tag.id;            const result = await this.db.update(this.tableName, data);            if (result.affectedRows === 1) return data;        }        async update(data) {            const result = {};            if (data.add) result.add = await this._addTag(data.add);            if (data.del) result.del = await this._delTag(data.del);            if (data.update) result.update = await this._updateTag(data.update);            return result;        }    }    return StageBillsDgn;};
 |