Browse Source

未读数改造

wangzhinan 2 years ago
parent
commit
da968374bb

+ 18 - 2
src/server/endpoints/v2/session.endpoint.js

@ -686,11 +686,11 @@ router.get(APIv2.Sessions.SessionUnreadMessageCount, function (req, res) {
/**
 * 获取所有会话未读消息数。
 * 根据businessTypeandStatus获取所有会话未读消息数。
 *
 * 请求URL:/sessions/unread_message_count?user_id=xyz&type=1
 */
router.get(APIv2.Sessions.SessionsUnreadMessageCountByBussinessType, function (req, res) {
router.get(APIv2.Sessions.SessionsUnreadMessageCountByBussinessTypeAndStatus, function (req, res) {
    let userId = req.query.user_id;
    let businessType = req.query.business_type;
    let status= req.query.status
@ -702,4 +702,20 @@ router.get(APIv2.Sessions.SessionsUnreadMessageCountByBussinessType, function (r
});
/**
 * 根据businessTypeandStatus获取所有会话未读消息数。
 *
 * 请求URL:/sessions/unread_message_count?user_id=xyz&type=1
 */
router.get(APIv2.Sessions.SessionsUnreadMessageCountByBussinessType, function (req, res) {
    let userId = req.query.user_id;
    let businessType = req.query.business_type;
    let sessions = new Sessions();
    ControllerUtil.regModelEventHandler(sessions, res);
    sessions.getAllSessionsUnreadMessageCountByBusinessType(userId,businessType);
});
module.exports = router;

+ 2 - 1
src/server/include/endpoints.js

@ -64,7 +64,8 @@ const APIv2 = {
        MessageUpdate: '/:session_id/messages/:message_id/update',      // 更新消息内容(消息里有i健康业务状态时)
        SessionsUnreadMessageCount: '/unread_message_count',            // 所有会话的未读消息数
        SessionsUnreadMessageCountByBussinessType: '/unread_message_count_business_type',            // 所有会话的未读消息数
        SessionsUnreadMessageCountByBussinessTypeAndStatus: '/unread_message_count_business_type_status',// 带status所有会话的未读消息数
        SessionsUnreadMessageCountByBussinessType: '/unread_message_count_business_type',// 带status所有会话的未读消息数
        SessionUnreadMessageCount: '/:session_id/unread_message_count', // 指定会话的未读消息数
        SessionUnreadMessages: '/:session_id/messages/unread',          // 会话未读消息

+ 125 - 0
src/server/models/sessions/sessions.js

@ -2077,6 +2077,131 @@ class Sessions extends RedisModel {
        }
    }
    /**
     * 获取所有会话的未读消息数。
     */
    getAllSessionsUnreadMessageCountByBusinessType(userId,businessType,handler) {
        let self = this;
        let count = 0;
        let patientCount = 0;
        let doctorCount = 0;
        let patientEndCount = 0;
        SessionRepo.findAllByTypeAndBusinessTypeAndNoPage(userId,businessType,function (err, res) {
            // SessionRepo.findUnEndAll(userId, function (err, res) {
            if (err) {
                if (handler) {
                    handler(err, res);
                    return;
                }
                ModelUtil.logError("getAllSessionsUnreadMessageCount is failed", err);
                return;
            }
            if (res && res.length == 0) {
                if (handler) {
                    handler(err, count);
                    return;
                }
                ModelUtil.emitOK(self.eventEmitter, {count: count});
                return;
            }
            /*let has = false;*/
            for (let j in res) {
                /* has = false;
                 //是否过滤指定类型
                 if( type != null){
                     let typeString = type.split(",");
                     var str = res[j].type;
                     var str1 = str.toString();
                     if(typeString.indexOf(str1)<0){
                         continue;
                     }
                 }*/
                //logger.info("type==="+res[j].type);
                if (res[j].type == SESSION_TYPES.SYSTEM) {
                    if (j == res.length - 1) {
                        if (handler) {
                            handler(err, count);
                            return;
                        }
                        ModelUtil.emitOK(self.eventEmitter, {count: count, patient: patientCount, doctor: doctorCount});
                    }
                    continue;
                }
                callback(res, j, res[j]);
                /*      has = true*/
            }
            /* if(!has){
                 ModelUtil.emitOK(self.eventEmitter, {count: count, patient: patientCount,patientEnd: 0, doctor: doctorCount});
             }*/
        });
        /*function callback(res, j, session) {
            self.getSessionUnreadMessageCount(res[j].id, userId, function (err, con) {
                if (err) {
                    if(handler)
                    {
                        handler(err,count);
                        return;
                    }
                    ModelUtil.logError("getAllSessionsUnreadMessageCount is failed", err);
                }
                count = count + con;
                if (session.business_type == SESSION_BUSINESS_TYPE.PATIENT) {
                    if(session.status == SESSION_STATUS.ENDED){//新增判断是否咨询结束
                        patientEndCount = patientEndCount + con;
                    }else{
                        patientCount = patientCount + con;
                    }
                } else {
                    doctorCount = doctorCount + con;
                }
                if (j == res.length - 1) {
                    if(handler)
                    {
                        handler(err,count)
                        return;
                    }
                    ModelUtil.emitOK(self.eventEmitter, {count: count, patient: patientCount,patientEnd: patientEndCount, doctor: doctorCount});
                }
            })
        }*/
        function callback(res, j, session) {
            self.getSessionUnreadMessageCount(res[j].id, userId, function (err, con) {
                if (err) {
                    if(handler)
                    {
                        handler(err,count);
                        return;
                    }
                    ModelUtil.logError("getAllSessionsUnreadMessageCount is failed", err);
                }
                count = count + con;
                if (session.business_type == SESSION_BUSINESS_TYPE.PATIENT) {
                    if(session.status == SESSION_STATUS.ENDED){//新增判断是否咨询结束
                        patientEndCount = patientEndCount + con;
                    }else{
                        patientCount = patientCount + con;
                    }
                } else {
                    doctorCount = doctorCount + con;
                }
                if (j == res.length - 1) {
                    if(handler)
                    {
                        handler(err,count)
                        return;
                    }
                    ModelUtil.emitOK(self.eventEmitter, {count: count, patient: patientCount,patientEnd: patientEndCount, doctor: doctorCount});
                }
            })
        }
    }
    /**
     * 获取会话的未读消息数。根据成员最后一次获取消息的时候与当前时间。
     *

+ 57 - 1
src/server/repository/mysql/session.repo.js

@ -125,7 +125,7 @@ class SessionRepo {
    static findAllByTypes(userId, type, handler) {
        let sql = "select session_id from " + DB_TABLES.Participants + " w where w.participant_id = ?  group by w.session_id";
        let sessionSQL = "select id, name, type, create_date, last_sender_id, last_sender_name, last_content_type, last_content, last_message_time from "
            + DB_TABLES.Sessions + " s where s.id in(" + sql + ")  AND S.STATUS = 0 ";
            + DB_TABLES.Sessions + " s where s.id in(" + sql + ")  AND s.STATUS = 0 ";
        log.info("获取用户全部会话: sql :" + sessionSQL);
        if(type){
            sessionSQL +=" and type in("+type+") ";
@ -373,6 +373,62 @@ class SessionRepo {
    }
    static findAllByTypeAndBusinessTypeAndNoPage(userId, businessType,handler) {
        let sessionSQL ="";
        let sql ="";
        if(status == SESSION_STATUS.ENDED){
            if(businessType == SESSION_BUSINESS_TYPE.PATIENT){//区分居民,有未读消息的置顶排列
                //找出已经结束的咨询
                sql = "select session_id from " + DB_TABLES.Participants + " w where w.participant_id = ?  group by w.session_id";
                //找出角色讨论组中为旁听且未结束的咨询
                let sql1 = ("select session_id from " + DB_TABLES.Participants + " w where w.participant_id = ? and participant_role ="+PARTICIPANT_ROLES.REGULAR+" group by w.session_id")
                sessionSQL =  "select s.* from " + DB_TABLES.Sessions + " s, " + DB_TABLES.Participants + " p " +
                    " where ((s.id in(" + sql + ") and s.business_type = ? and s.status = 1) or (s.id in(" + sql1 + ") and s.business_type = ? and s.status = 0)) " +
                    // " and s.id = p.session_id and p.participant_id = ? ORDER BY (p.last_fetch_time - s.last_message_time+1)>0,s.create_date desc limit "+page+","+pagesize;
                    " and s.id = p.session_id and p.participant_id = ? ORDER BY s.last_message_time desc ";
                ImDb.execQuery({
                    "sql": sessionSQL,
                    "args": [userId, businessType,userId,businessType,userId],
                    "handler": handler || function (err, res) {
                        if(err) log.error(err);
                    }
                });
            }else{
                //找出已经结束的咨询
                sql = "select session_id from " + DB_TABLES.Participants + " w where w.participant_id = ?  group by w.session_id";
                //找出角色讨论组中为旁听且未结束的咨询
                let sql1 = ("select session_id from " + DB_TABLES.Participants + " w where w.participant_id = ? and participant_role ="+PARTICIPANT_ROLES.REGULAR+" group by w.session_id")
                sessionSQL =  "select * from "
                    + DB_TABLES.Sessions + " s where (s.id in(" + sql + ") and s.business_type = ? and s.status = 1) or (s.id in(" + sql1 + ") and s.business_type = ? and s.status = 0)  ";
                log.info("findAllByTypeAndStatus: sql " + sessionSQL);
                log.info("findAllByTypeAndStatus: args " + [userId, businessType,userId,businessType]);
                ImDb.execQuery({
                    "sql": sessionSQL,
                    "args": [userId, businessType,userId,businessType],
                    "handler": handler || function (err, res) {
                        if(err) log.error(err);
                    }
                });
            }
        }else{
            sql = "select session_id from " + DB_TABLES.Participants + " w where w.participant_id = ? and participant_role ="+PARTICIPANT_ROLES.HOST+" group by w.session_id";
            sessionSQL =  "select * from "
                + DB_TABLES.Sessions + " s where s.id in(" + sql + ") and s.business_type in (?) ";
            log.info("findAllByTypeAndStatus: sql : "+sessionSQL);
            log.info("findAllByTypeAndStatus: args : "+[userId, businessType]);
            ImDb.execQuery({
                "sql": sessionSQL,
                "args": [userId, businessType.split(",")],
                "handler": handler || function (err, res) {
                    if(err) log.error(err);
                }
            });
        }
    }
    static findAllByType(userId, businessType,page,pagesize, handler) {
        if (page > 0) {
            if (page == 1) {