| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 | 'use strict';/** * 消息数据模型 * * @author CaiAoLin * @date 2017/11/23 * @version */module.exports = app => {    class Message extends app.BaseService {        /**         * 构造函数         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        constructor(ctx) {            super(ctx);            this.tableName = 'message';        }        /**         * 规则         *         * @return {Object} - 返回规则         */        rule() {            return {                title: { type: 'string', required: true, allowEmpty: false },                content: { type: 'string', required: false, allowEmpty: true },                istop: { type: 'string', required: false, allowEmpty: true },            };        }        /**         * 查询过虑         *         * @param {Object} data - 筛选表单中的get数据         * @return {void}         */        searchFilter(data, project_id) {            this.initSqlBuilder();            // 状态筛选            data.type = parseInt(data.type);            data.type = isNaN(data.type) || data.type <= 0 ? 1 : data.type;            // 获取用户数据            this.sqlBuilder.setAndWhere('type', {                value: data.type,                operate: '=',            });            this.sqlBuilder.setAndWhere('project_id', {                value: project_id,                operate: '=',            });        }        /**         * 获取消息数据         *         * @param {Number} startTime - 时间标记位(获取这个时间点之后的数据)         * @return {Array} - 返回消息数据         */        async getMessage(startTime, project_id) {            this.initSqlBuilder();            this.sqlBuilder.setAndWhere('release_time', {                value: startTime,                operate: '>=',            });            // 获取用户数据            this.sqlBuilder.setAndWhere('project_id', {                value: project_id,                operate: '=',            });            // 获取用户数据            this.sqlBuilder.setAndWhere('type', {                value: 1,                operate: '=',            });            const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);            const result = await this.db.query(sql, sqlParam);            return result;        }        /**         * 修改通知信息         *         * @param {Object} data - post过来的数据         * @return {Boolean} - 返回修改结果         */        async save(id, data, user, projectId, spid = '') {            if (data._csrf_j !== undefined) {                delete data._csrf_j;            }            const transaction = await this.db.beginTransaction();            try {                if (id > 0) {                    // 修改操作时                    data.id = id;                    const msgInfo = await this.getDataById(id);                    data.istop = parseFloat(msgInfo.istop) === 0 && parseInt(data.istop) === 1 ? data.create_time : data.istop === undefined ? 0 : msgInfo.istop;                    delete data.create_time;                } else {                    data.release_time = data.create_time;                    data.project_id = projectId;                    data.spid = spid;                    data.create_uid = user.accountId;                    data.creator = user.name;                    data.istop = parseInt(data.istop) === 1 ? data.create_time : 0;                }                const operate = id === 0 ? await transaction.insert(this.tableName, data) :                    await transaction.update(this.tableName, data);                const result = operate.affectedRows > 0 ? (id === 0 ? operate.insertId : true) : false;                if (id === 0) {                    const attList = await this.ctx.service.messageAtt.getAllDataByCondition({ where: { mid: 0, uid: user.accountId } });                    if (attList.length > 0) {                        const fileUpdateDatas = [];                        for (const att of attList) {                            fileUpdateDatas.push({                                id: att.id,                                mid: result,                            });                        }                        await transaction.updateRows(this.ctx.service.messageAtt.tableName, fileUpdateDatas);                    }                }                await transaction.commit();                return result;            } catch (error) {                console.log(error);                await transaction.rollback();                return false;            }        }        /**         * 修改通知信息         *         * @param {Object} data - post过来的数据         * @return {Boolean} - 返回修改结果         */        async getMsgList(projectId, spid = '', limit = 5, offset = 0, type = 1) {            const sqSql = spid ? ' AND (`spid` = "' + spid + '" OR `spid` = "")' : ' AND `spid` = ""';            if (type === 1) {                const sql = 'SELECT * FROM ?? WHERE `project_id` = ?' + sqSql + ' AND `type` = ? ORDER BY CONCAT(`istop`,`release_time`) DESC LIMIT ?,?';                const sqlParam = [this.tableName, projectId, type, offset, limit];                const result = await this.db.query(sql, sqlParam);                for (const r of result) {                    r.files = await this.ctx.service.messageAtt.getAtt(r.id);                }                return result;            }            const sql = 'SELECT * FROM ?? WHERE `type` = ? AND `status` = ? ORDER BY `release_time` DESC LIMIT ?,?';            const sqlParam = [this.tableName, type, 1, offset, limit];            const result = await this.db.query(sql, sqlParam);            for (const r of result) {                r.files = await this.ctx.service.messageAtt.getAtt(r.id);            }            return result;        }        async deleteMsg(id) {            const transaction = await this.db.beginTransaction();            try {                await transaction.delete(this.tableName, { id });                // 删除附件                const attList = await this.ctx.service.messageAtt.getAllDataByCondition({ where: { mid: id } });                await this.ctx.helper.delFiles(attList);                await transaction.delete(this.ctx.service.messageAtt.tableName, { mid: id });                await transaction.commit();                return true;            } catch (error) {                console.log(error);                await transaction.rollback();                return false;            }        }    }    return Message;};
 |