/** * 成员模型。 */ "use strict"; let RedisClient = require('../../repository/redis/redis.client.js'); let redisClient = RedisClient.redisClient(); let redis = redisClient.connection; let RedisModel = require('./../redis.model.js'); let modelUtil = require('../../util/model.util'); let ParticipantRepo = require('../../repository/mysql/participant.repo'); let log = require('../../util/log.js'); const RedisKey = require('../../include/commons').REDIS_KEYS; class Participants extends RedisModel { constructor() { super(); } /** * 根据topicId获取对应的议题的成员信息 * @param topicId */ getParticipantsByTopicId(topicId){ } /** * 根据sessionId获取对应的议题的成员信息 * @param sessionId */ getParticipantsBySessionId(sessionId,handler){ let participant_key = super.makeRedisKey(RedisKey.Participants,sessionId); redis.existsAsync(participant_key).then(function(res){ if(res){ redis.zrangeAsync(participant_key,0,-1).then(function(res){ handler(res); }) }else{ ParticipantRepo.getParticipantsBySessionId(sessionId,handler); } }) } /** * 判断成员是否存在这个讨论组中 * @param sessionId * @param userId */ existsUser(sessionId,userId,handler){ let participant_key = super.makeRedisKey(RedisKey.Participants,sessionId); redis.existsAsync(participant_key).then(function(res){ if(res){//存在redis中直接从redis校验 redis.zrangeAsync(participant_key,0,-1).then(function(res){ let exists = false for(var j in res){ var value = res[j]; if(value==userId){ exists = true; break; } } handler(exists); }) }else{//不存在从数据库中获取 ParticipantRepo.existsUser(sessionId,userId,handler); } }) } /** * 根据医生和患者 * @param patient * @param doctor */ getSessionIdByParticipants(patient,doctor,handler){ ParticipantRepo.getSessionIdByParticipants(patient,doctor,handler); } /** * 将成员写入redis * @param session_id 会话ID * @param users 用户集合 * @param createDate 创建日期 * @param handler 回调 */ createParticipantsToRedis(session_id,users,createDate,handler){ let participants_key = super.makeRedisKey(RedisKey.Participants,session_id); for(var j in users){ let user_session_key = super.makeRedisKey(RedisKey.UsersSessions,users[j]); redis.zaddAsync(participants_key, createDate.getTime(),users[j]).then(function(res){ return redis.zaddAsync(user_session_key,createDate.getTime(),session_id); } ).catch(function(err){ log.error("createParticipantsToRedis is fail error:"+err+",session_id:"+session_id+",users:"+users.join(",")); handler(false); }); } handler(true); } /** * mysql成员创建 * @param users */ createParticipantsToMysql(session_id,users){ return ParticipantRepo.createParticipantsToMysql(session_id,users); } /** * MUC成员创建 * @param users */ createMUCParticipants(users){ return true; } /** * 移除讨论组成员 * @param sessionId * @param user */ deleteUser(sessionId,user){ let self = this; let participants_key = super.makeRedisKey(RedisKeys.Participants,sessionId); let user_session_key = super.makeRedisKey(RedisKeys.UsersSessions,user); //1.移除SESSION成员表中的成员信息 redis.zremAsync(participants_key,user).then(function(res){ log.info("remove participants:"+res); //2.删除对应人员的Session列表 redis.zremAsync(user_session_key,sessionId).then(function(res){ log.info("remove user_session:"+res); //3.移除数据库中的人员记录 self.deleteUserFromMysql(sessionId,user); modelUtil.emitData(self.eventEmitter,{"status":200,"msg":"人员删除成功!"}); }).catch(function(err){ modelUtil.emitData(self.eventEmitter,{"status":-1,"msg":"人员删除失败!"+err}); }) }).catch(function(err){ modelUtil.emitData(self.eventEmitter,{"status":-1,"msg":"人员删除失败!"+err}); }) } /** * 添加讨论组成员 * @param sessionId * @param user */ pushUser(sessionId,user){ let self = this; let users = []; users.push(user); self.createParticipantsToRedis(sessionId,users,new Date(),function(res){ if(res){ self.createParticipantsToMysql(sessionId,users); }else{ modelUtil.emitData(self.eventEmitter,{"status":-1,"msg":"人员添加失败!"}); } }) } /** * user从数据库中删除 * @param sessionId 会话 * @param user 用户 */ deleteUserFromMysql(sessionId,user){ ParticipantRepo.deleteUserFromMysql(sessionId,user); } } // Expose class module.exports = Participants;