123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- /**
- * 成员模型。
- */
- "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,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{
- let sql ="select w.* from participants w where w.session_id =? ";
- imDb.execQuery({
- "sql": sql,
- "args": [sessionId],
- "handler": function (err, res) {
- if(err) {
- log.error("getParticipantsBySessionId is fail error: "+err);
- }
- handler(res);
- }
- });
- }
- })
- }
- /**
- * 判断成员是否存在这个讨论组中
- * @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{//不存在从数据库中获取
- let sql ="select count(1) as count from participants w where w.session_id =? and w.participaint_id = ? ";
- imDb.execQuery({
- "sql": sql,
- "args": [sessionId,userId],
- "handler": function (err, res) {
- if(err) {
- log.error("existsUser is fail error: "+err);
- }
- handler(res[0].count);
- }
- });
- }
- })
- }
- /**
- * 根据医生和患者
- * @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;
|