|  | @ -128,8 +128,10 @@ class Sessions extends RedisModel {
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         function callBusinessType(sessionId) {
 | 
	
		
			
				|  |  |             if(type == SESSION_TYPES.MUC||type == SESSION_TYPES.PRESCRIPTION){
 | 
	
		
			
				|  |  |             if(type == SESSION_TYPES.MUC||type == SESSION_TYPES.PRESCRIPTION || SESSION_TYPES.PRESCRIPTION_HOSPITAL || SESSION_TYPES.ONDOOR_NURSING){
 | 
	
		
			
				|  |  |                 callCreate(sessionId, SESSION_BUSINESS_TYPE.PATIENT);
 | 
	
		
			
				|  |  |             }else if(type==SESSION_TYPES.DISCUSSION||type==SESSION_TYPES.GROUP){
 | 
	
		
			
				|  |  |                 callCreate(sessionId, SESSION_BUSINESS_TYPE.DOCTOR);
 | 
	
		
			
				|  |  |             }else {
 | 
	
		
			
				|  |  |                 ParticipantRepo.getBusinessType(participantIdArray, function (err, businessType) {
 | 
	
		
			
				|  |  |                     callCreate(sessionId, businessType);
 | 
	
	
		
			
				|  | @ -141,7 +143,7 @@ class Sessions extends RedisModel {
 | 
	
		
			
				|  |  |             let createDate = new Date();
 | 
	
		
			
				|  |  |             Participants.saveParticipantsToRedis(sessionId, participantArray, createDate, function (res) {
 | 
	
		
			
				|  |  |                 let sessionKey = RedisModel.makeRedisKey(REDIS_KEYS.Session, sessionId);
 | 
	
		
			
				|  |  |                 if (type == SESSION_TYPES.MUC||type == SESSION_TYPES.PRESCRIPTION) {
 | 
	
		
			
				|  |  |                 if (type == SESSION_TYPES.MUC||type == SESSION_TYPES.PRESCRIPTION || SESSION_TYPES.PRESCRIPTION_HOSPITAL || SESSION_TYPES.ONDOOR_NURSING) {
 | 
	
		
			
				|  |  |                     businessType = 2;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 let session = {
 | 
	
	
		
			
				|  | @ -663,6 +665,149 @@ class Sessions extends RedisModel {
 | 
	
		
			
				|  |  |         ]);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 查找会话数
 | 
	
		
			
				|  |  |      * @param userId
 | 
	
		
			
				|  |  |      * @param type
 | 
	
		
			
				|  |  |      * @param handler
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     getSessionCountByType(userId,type,handler){
 | 
	
		
			
				|  |  |         let self = this;
 | 
	
		
			
				|  |  |         SessionRepo.findSessionCountByType(userId,type,function (err,res) {
 | 
	
		
			
				|  |  |             if(res){
 | 
	
		
			
				|  |  |                 ModelUtil.emitOK(self.eventEmitter,{status:200,count:res[0].count});
 | 
	
		
			
				|  |  |                 return;
 | 
	
		
			
				|  |  |             }else {
 | 
	
		
			
				|  |  |                 ModelUtil.emitOK(self.eventEmitter,{status:200,count:0});
 | 
	
		
			
				|  |  |                 return;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         })
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 根据用户类型获取用户的session列表
 | 
	
		
			
				|  |  |      * @param userId
 | 
	
		
			
				|  |  |      * @param page
 | 
	
		
			
				|  |  |      * @param size
 | 
	
		
			
				|  |  |      * @param businessType
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     getUserSessionsByType(userId,type,page, size) {
 | 
	
		
			
				|  |  |         let userSessionKey = RedisModel.makeRedisKey(REDIS_KEYS.UserSessions, userId);
 | 
	
		
			
				|  |  |         let self = this;
 | 
	
		
			
				|  |  |         async.waterfall([
 | 
	
		
			
				|  |  |             // 获取会话ID列表
 | 
	
		
			
				|  |  |             function (callback) {
 | 
	
		
			
				|  |  |                 SessionRepo.findListByType(userId,type,page,size,function(err,res){
 | 
	
		
			
				|  |  |                     if (res.length == 0) {
 | 
	
		
			
				|  |  |                         ModelUtil.emitOK(self.eventEmitter, []);
 | 
	
		
			
				|  |  |                         return;
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     callback(null,res);
 | 
	
		
			
				|  |  |                 })
 | 
	
		
			
				|  |  |             },
 | 
	
		
			
				|  |  |             // 遍历会话
 | 
	
		
			
				|  |  |             function (sessionIds) {
 | 
	
		
			
				|  |  |                 let sessionList = [];
 | 
	
		
			
				|  |  |                 let functionList = [];
 | 
	
		
			
				|  |  |                 for (let j = 0; j < sessionIds.length; j++) {
 | 
	
		
			
				|  |  |                     let fun = function (index, callback) {
 | 
	
		
			
				|  |  |                         if (!callback) {
 | 
	
		
			
				|  |  |                             callback = index, index = 0
 | 
	
		
			
				|  |  |                         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                         let mysqlSession = sessionIds[index];
 | 
	
		
			
				|  |  |                         let sessionId = mysqlSession.id;
 | 
	
		
			
				|  |  |                         let sessionKey = RedisModel.makeRedisKey(REDIS_KEYS.Session, sessionId);
 | 
	
		
			
				|  |  |                         let sessionParticipantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
 | 
	
		
			
				|  |  |                         redis.multi()
 | 
	
		
			
				|  |  |                             .hgetall(sessionKey)                       // 会话实体
 | 
	
		
			
				|  |  |                             .zscore(sessionParticipantsKey, userId)    // 用户在此会话中最后一次获取未读消息的时间
 | 
	
		
			
				|  |  |                             .zrange(sessionParticipantsKey, 0, -1)
 | 
	
		
			
				|  |  |                             .zrange(sessionParticipantsKey, 0,-1,'withscores')  // 所有用户在此会话中最后一次获取未读消息的时间
 | 
	
		
			
				|  |  |                             .execAsync()
 | 
	
		
			
				|  |  |                             .then(function (res) {
 | 
	
		
			
				|  |  |                                 let session = res[0];
 | 
	
		
			
				|  |  |                                 let lastFetchTime = res[1];
 | 
	
		
			
				|  |  |                                 let users = res[2];
 | 
	
		
			
				|  |  |                                 let participantsTimeArray = res[3];
 | 
	
		
			
				|  |  |                                 let participantsTime = [];
 | 
	
		
			
				|  |  |                                 for(var j = 0 ;j<participantsTimeArray.length;j++){
 | 
	
		
			
				|  |  |                                     if(j%2!=0)continue;
 | 
	
		
			
				|  |  |                                     let participantsTimeJson = {};
 | 
	
		
			
				|  |  |                                     participantsTimeJson[participantsTimeArray[j]] = participantsTimeArray[j+1];
 | 
	
		
			
				|  |  |                                     participantsTime.push(participantsTimeJson);
 | 
	
		
			
				|  |  |                                 }
 | 
	
		
			
				|  |  |                                 let sessionName = "";
 | 
	
		
			
				|  |  |                                 let otherUserId = "";
 | 
	
		
			
				|  |  |                                 if (session.type == SESSION_TYPES.P2P) {
 | 
	
		
			
				|  |  |                                     for (let j in users) {
 | 
	
		
			
				|  |  |                                         if (users[j] != userId) {
 | 
	
		
			
				|  |  |                                             otherUserId = users[j];
 | 
	
		
			
				|  |  |                                         }
 | 
	
		
			
				|  |  |                                     }
 | 
	
		
			
				|  |  |                                 }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                                 if (!lastFetchTime) lastFetchTime = new Date().getTime();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                                 // 计算未读消息数
 | 
	
		
			
				|  |  |                                 let messagesByTimestampKey = RedisModel.makeRedisKey(REDIS_KEYS.MessagesByTimestamp, sessionId);
 | 
	
		
			
				|  |  |                                 redis.zcountAsync(messagesByTimestampKey, parseInt(lastFetchTime)+1, new Date().getTime())
 | 
	
		
			
				|  |  |                                     .then(function (count) {
 | 
	
		
			
				|  |  |                                         if (!otherUserId) otherUserId = userId;
 | 
	
		
			
				|  |  |                                         ParticipantRepo.findNameById(otherUserId, function (err, res) {
 | 
	
		
			
				|  |  |                                             if ((res && res.length == 0) || session.type != SESSION_TYPES.P2P) {
 | 
	
		
			
				|  |  |                                                 sessionName = session.name;
 | 
	
		
			
				|  |  |                                             } else {
 | 
	
		
			
				|  |  |                                                 sessionName = res[0].name;
 | 
	
		
			
				|  |  |                                             }
 | 
	
		
			
				|  |  |                                             var bir = new Date().getTime();
 | 
	
		
			
				|  |  |                                             if (res.length != 0 && res[0].birthdate) {
 | 
	
		
			
				|  |  |                                                 bir = res[0].birthdate.getTime();
 | 
	
		
			
				|  |  |                                             }
 | 
	
		
			
				|  |  |                                             var sex = 1;
 | 
	
		
			
				|  |  |                                             if (res.length != 0 && res[0].sex) {
 | 
	
		
			
				|  |  |                                                 sex = res[0].sex;
 | 
	
		
			
				|  |  |                                             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                                             sessionList.push({
 | 
	
		
			
				|  |  |                                                 id: sessionId,
 | 
	
		
			
				|  |  |                                                 name: sessionName,
 | 
	
		
			
				|  |  |                                                 create_date: new Date(mysqlSession.last_message_time).getTime(),
 | 
	
		
			
				|  |  |                                                 last_content_type: session.last_content_type,
 | 
	
		
			
				|  |  |                                                 last_content: session.last_content,
 | 
	
		
			
				|  |  |                                                 sender_id: session.last_sender_id,
 | 
	
		
			
				|  |  |                                                 type: session.type,
 | 
	
		
			
				|  |  |                                                 sender_name: session.last_sender_name,
 | 
	
		
			
				|  |  |                                                 unread_count: count,
 | 
	
		
			
				|  |  |                                                 business_type: session.business_type,
 | 
	
		
			
				|  |  |                                                 sender_sex: sex,
 | 
	
		
			
				|  |  |                                                 sender_birthday: bir,
 | 
	
		
			
				|  |  |                                                 participantsTimeArray:participantsTime,
 | 
	
		
			
				|  |  |                                                 status:session.status,
 | 
	
		
			
				|  |  |                                             });
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                                             index = (parseInt(index) + 1);
 | 
	
		
			
				|  |  |                                             if (index == sessionIds.length) {
 | 
	
		
			
				|  |  |                                                 ModelUtil.emitOK(self.eventEmitter, sessionList);
 | 
	
		
			
				|  |  |                                             } else {
 | 
	
		
			
				|  |  |                                                 callback(null, index);
 | 
	
		
			
				|  |  |                                             }
 | 
	
		
			
				|  |  |                                         })
 | 
	
		
			
				|  |  |                                     })
 | 
	
		
			
				|  |  |                             })
 | 
	
		
			
				|  |  |                             .catch(function (err) {
 | 
	
		
			
				|  |  |                                 logger.error("Get sessions:"+sessionId+" failed: ", err);
 | 
	
		
			
				|  |  |                             });
 | 
	
		
			
				|  |  |                     };
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                     functionList.push(fun);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 async.waterfall(functionList);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         ]);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 根据用户ID获取用户已经结束咨询的session列表
 | 
	
		
			
				|  |  |      * @param userId
 | 
	
	
		
			
				|  | @ -827,6 +972,7 @@ class Sessions extends RedisModel {
 | 
	
		
			
				|  |  |         let message_timestamp_key = RedisModel.makeRedisKey(REDIS_KEYS.MessagesByTimestamp, sessionId);
 | 
	
		
			
				|  |  |         if (!start_msg_id && !end_msg_id) {
 | 
	
		
			
				|  |  |             redis.zrevrangeAsync(message_timestamp_key, 0, 0).then(function (res) {
 | 
	
		
			
				|  |  |                 logger.info("redis return res-----"+res);
 | 
	
		
			
				|  |  |                 log.info("session.js--830---getMessages  res.length: " + res.length);
 | 
	
		
			
				|  |  |                 if (res.length == 0) {
 | 
	
		
			
				|  |  |                     //修复应redis没有缓冲聊天记录导致会话列表加载不出来
 | 
	
	
		
			
				|  | @ -1033,7 +1179,7 @@ class Sessions extends RedisModel {
 | 
	
		
			
				|  |  |                                                 if(users[j]==userId)continue;
 | 
	
		
			
				|  |  |                                                  WechatClient.sendAllRead(users[j],sessionId);
 | 
	
		
			
				|  |  |                                             }
 | 
	
		
			
				|  |  |                                         }else if(session.type == SESSION_TYPES.MUC || session.type == SESSION_TYPES.PRESCRIPTION){
 | 
	
		
			
				|  |  |                                         }else if(session.type == SESSION_TYPES.MUC || session.type == SESSION_TYPES.PRESCRIPTION || SESSION_TYPES.PRESCRIPTION_HOSPITAL || SESSION_TYPES.ONDOOR_NURSING){
 | 
	
		
			
				|  |  |                                             for(var j in users){
 | 
	
		
			
				|  |  |                                                 if(users[j]==userId)continue;
 | 
	
		
			
				|  |  |                                                 WechatClient.sendMucAllRead(users[j],userId,sessionId);
 | 
	
	
		
			
				|  | @ -1066,7 +1212,7 @@ class Sessions extends RedisModel {
 | 
	
		
			
				|  |  |                                                 //通知对方自己已经读取数据
 | 
	
		
			
				|  |  |                                                 WechatClient.sendAllRead(users[j],sessionId);
 | 
	
		
			
				|  |  |                                             }
 | 
	
		
			
				|  |  |                                         }else if(session.type == SESSION_TYPES.MUC || session.type == SESSION_TYPES.PRESCRIPTION){
 | 
	
		
			
				|  |  |                                         }else if(session.type == SESSION_TYPES.MUC || session.type == SESSION_TYPES.PRESCRIPTION || SESSION_TYPES.PRESCRIPTION_HOSPITAL || SESSION_TYPES.ONDOOR_NURSING){
 | 
	
		
			
				|  |  |                                             for(var j in users){
 | 
	
		
			
				|  |  |                                                 if(users[j]==userId)continue;
 | 
	
		
			
				|  |  |                                                 //如果是患者拉取数据告诉在线的医生患者已经读取数据
 | 
	
	
		
			
				|  | @ -1283,7 +1429,7 @@ class Sessions extends RedisModel {
 | 
	
		
			
				|  |  |                 return;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             logger.info("session.js--1285--res[0].exist" + res[0].exist);
 | 
	
		
			
				|  |  |             if ( res[0].exist || userId == "system") {
 | 
	
		
			
				|  |  |             if ( res[0].exist || message.sender_id == "system") {
 | 
	
		
			
				|  |  |                 redis.hmgetAsync(sessionKey, ["type", "name"]).then(function (res) {
 | 
	
		
			
				|  |  |                     sessionType = res[0];
 | 
	
		
			
				|  |  |                     let sessionName = res[1];
 | 
	
	
		
			
				|  | @ -1291,7 +1437,7 @@ class Sessions extends RedisModel {
 | 
	
		
			
				|  |  |                         ModelUtil.emitError(self.eventEmitter, "Session " + sessionId + " is not found.");
 | 
	
		
			
				|  |  |                         return;
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     if(sessionType == SESSION_TYPES.MUC || sessionType == SESSION_TYPES.PRESCRIPTION){
 | 
	
		
			
				|  |  |                     if(sessionType == SESSION_TYPES.MUC || sessionType == SESSION_TYPES.PRESCRIPTION || SESSION_TYPES.PRESCRIPTION_HOSPITAL || SESSION_TYPES.ONDOOR_NURSING){
 | 
	
		
			
				|  |  |                         if(message.content_type == CONTENT_TYPES.PlainText ||
 | 
	
		
			
				|  |  |                            message.content_type == CONTENT_TYPES.Image ||
 | 
	
		
			
				|  |  |                            message.content_type == CONTENT_TYPES.Audio||
 | 
	
	
		
			
				|  | @ -1395,7 +1541,7 @@ class Sessions extends RedisModel {
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         // 发送成员必须处于会话中
 | 
	
		
			
				|  |  |         participants.existsParticipant(sessionId, message.sender_id, function (err, res) {
 | 
	
		
			
				|  |  |             if ( res[0].exist || userId == "system") {
 | 
	
		
			
				|  |  |             if ( res[0].exist || message.sender_id == "system") {
 | 
	
		
			
				|  |  |                 redis.hmgetAsync(session_key, ["type", "name"]).then(function (res) {
 | 
	
		
			
				|  |  |                     sessionType = res[0];
 | 
	
		
			
				|  |  |                     sessionName = res[1];
 | 
	
	
		
			
				|  | @ -1475,7 +1621,7 @@ class Sessions extends RedisModel {
 | 
	
		
			
				|  |  |         // 发送成员必须处于会话中
 | 
	
		
			
				|  |  |         participants.existsParticipant(sessionId, message.sender_id, function (err, res) {
 | 
	
		
			
				|  |  |             log.info("session.js--1477--res[0].exist" + res[0].exist);
 | 
	
		
			
				|  |  |             if (res[0].exist || userId == "system") {
 | 
	
		
			
				|  |  |             if (res[0].exist || message.sender_id == "system") {
 | 
	
		
			
				|  |  |                 redis.hmgetAsync(session_key, ["type", "name"]).then(function (res) {
 | 
	
		
			
				|  |  |                     sessionType = res[0];
 | 
	
		
			
				|  |  |                     sessionName = res[1];
 |