wangzhinan преди 2 години
родител
ревизия
b5926f4c53

+ 17 - 0
src/server/endpoints/v2/session.endpoint.js

@ -685,4 +685,21 @@ router.get(APIv2.Sessions.SessionUnreadMessageCount, function (req, res) {
});
/**
 * 获取所有会话未读消息数。
 *
 * 请求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 status= req.query.status
    let sessions = new Sessions();
    ControllerUtil.regModelEventHandler(sessions, res);
    sessions.getAllSessionsUnreadMessageCountByStatus(userId,businessType,status);
});
module.exports = router;

+ 1 - 0
src/server/include/commons.js

@ -202,6 +202,7 @@ const CONTENT_TYPES = {
    KnowSymptomsDiseaseQ:3006,//知识库-疾病症状问题
    KnowSymptomsDiseaseA:3007,//知识库-疾病症状回答
    KnowCommonCustomer:3008,//知识库-客服欢迎
    ForwardChat:3009,//转发聊天
    typeToDescription: function (type, defaultDescription) {
        if (CONTENT_TYPES.Image == type) {
            return '[图片]';

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

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

+ 2 - 1
src/server/models/client/wechat.client.js

@ -97,7 +97,8 @@ class WechatClient extends RedisModel {
            message.content_type==CONTENT_TYPES.KnowSymptomsDisease||
            message.content_type==CONTENT_TYPES.KnowSymptomsDiseaseQ||
            message.content_type==CONTENT_TYPES.KnowSymptomsDiseaseA||
            message.content_type==CONTENT_TYPES.KnowCommonCustomer
            message.content_type==CONTENT_TYPES.KnowCommonCustomer||
            message.content_type==CONTENT_TYPES.ForwardChat
            )) {
            let patientClient = clientCache.findById(targetUserId);
            let pc_patientClient = clientCache.findById("pcpatient_"+targetUserId);

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

@ -1952,6 +1952,131 @@ class Sessions extends RedisModel {
        }
    }
    /**
     * 获取所有会话的未读消息数。
     */
    getAllSessionsUnreadMessageCountByStatus(userId,businessType,status,handler) {
        let self = this;
        let count = 0;
        let patientCount = 0;
        let doctorCount = 0;
        let patientEndCount = 0;
        SessionRepo.findAllByTypeAndStatusAndNoPage(userId,businessType, status,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});
                }
            })
        }
    }
    /**
     * 获取会话的未读消息数。根据成员最后一次获取消息的时候与当前时间。
     *

+ 56 - 0
src/server/repository/mysql/session.repo.js

@ -317,6 +317,62 @@ class SessionRepo {
    }
    static findAllByTypeAndStatusAndNoPage(userId, businessType,status,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 (?) and s.status = ? ";
            log.info("findAllByTypeAndStatus: sql : "+sessionSQL);
            log.info("findAllByTypeAndStatus: args : "+[userId, businessType,status]);
            ImDb.execQuery({
                "sql": sessionSQL,
                "args": [userId, businessType.split(","),status],
                "handler": handler || function (err, res) {
                    if(err) log.error(err);
                }
            });
        }
    }
    static findAllByType(userId, businessType,page,pagesize, handler) {
        if (page > 0) {
            if (page == 1) {