/** * 成员模型。 */ "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/modelUtil'); let imDb = require('../../repository/mysql/db/im.db'); let log = require('../../util/log.js'); const RedisKey = require('../../include/commons').REDIS_KEYS; const IMTABLE = require('../../include/commons').IM_DB; class Participants extends RedisModel { constructor() { super(); } /** * 根据topicId获取对应的议题的成员信息 * @param topicId */ getParticipantsByTopicId(topicId){ } /** * 根据sessionId获取对应的议题的成员信息 * @param sessionId */ getParticipantsBySessionId(sessionId){ } /** * 判断成员是否存在这个讨论组中 * @param sessionId * @param userId */ existsUser(sessionId,userId,handler){ let participant_key = super.makeRedisKey(RedisKey.Participants,sessionId); return 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); }) } /** * 根据医生和患者 * @param patient * @param doctor */ getSessionIdByParticipants(patient,doctor,handler){ let sql ="select session_id from "+IMTABLE.PARTICIPANTS+" p1 ,participants p2 " + "where p1.session_id = p2.session_id and " + "((p1.participaint_id = ? and p2.participaint_id = ?) or (p1.participaint_id = ? and p2.participaint_id = ?))" imDb.execQuery({ "sql": sql, "args": [patient,doctor,doctor,patient], "handler": function (err, res) { if(err) { log.error("getSessionIdByParticipants is fail error: "+err); } handler(err,res); } }); } /** * 将成员写入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){ let sql="insert into "+IMTABLE.PARTICIPANTS +" (session_id,participaint_id,participaint_role,receiving) VALUES " let args=[]; for(var j in users){ sql+="(?,?,?,?),"; args.push(session_id); args.push(users[j]); args.push(0); args.push(0); } sql = sql.substring(0,sql.lastIndexOf(",")); imDb.execQuery({ "sql": sql, "args": args, "handler": function (err, res) { if(err) { log.error("createParticipantsForMysql is fail error: "+err+",session_id:"+session_id+",users:"+users.join(",")); }else{ return res; } } }); return true; } /** * MUC成员创建 * @param users */ createMUCParticipants(users){ return true; } } // Expose class module.exports = Participants;