participants.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * 成员模型。
  3. */
  4. "use strict";
  5. let RedisClient = require('../../repository/redis/redis.client.js');
  6. let redisClient = RedisClient.redisClient();
  7. let redis = redisClient.connection;
  8. let RedisModel = require('./../redis.model.js');
  9. let modelUtil = require('../../util/model.util');
  10. let ParticipantRepo = require('../../repository/mysql/participant.repo');
  11. let log = require('../../util/log.js');
  12. const RedisKey = require('../../include/commons').REDIS_KEYS;
  13. class Participants extends RedisModel {
  14. constructor() {
  15. super();
  16. }
  17. /**
  18. * 根据topicId获取对应的议题的成员信息
  19. * @param topicId
  20. */
  21. getParticipantsByTopicId(topicId){
  22. }
  23. /**
  24. * 根据sessionId获取对应的议题的成员信息
  25. * @param sessionId
  26. */
  27. getParticipantsBySessionId(sessionId,handler){
  28. let participant_key = super.makeRedisKey(RedisKey.Participants,sessionId);
  29. redis.existsAsync(participant_key).then(function(res){
  30. if(res){
  31. redis.zrangeAsync(participant_key,0,-1).then(function(res){
  32. handler(res);
  33. })
  34. }else{
  35. ParticipantRepo.getParticipantsBySessionId(sessionId,handler);
  36. }
  37. })
  38. }
  39. /**
  40. * 判断成员是否存在这个讨论组中
  41. * @param sessionId
  42. * @param userId
  43. */
  44. existsUser(sessionId,userId,handler){
  45. let participant_key = super.makeRedisKey(RedisKey.Participants,sessionId);
  46. redis.existsAsync(participant_key).then(function(res){
  47. if(res){//存在redis中直接从redis校验
  48. redis.zrangeAsync(participant_key,0,-1).then(function(res){
  49. let exists = false
  50. for(var j in res){
  51. var value = res[j];
  52. if(value==userId){
  53. exists = true;
  54. break;
  55. }
  56. }
  57. handler(exists);
  58. })
  59. }else{//不存在从数据库中获取
  60. ParticipantRepo.existsUser(sessionId,userId,handler);
  61. }
  62. })
  63. }
  64. /**
  65. * 根据医生和患者
  66. * @param patient
  67. * @param doctor
  68. */
  69. getSessionIdByParticipants(patient,doctor,handler){
  70. ParticipantRepo.getSessionIdByParticipants(patient,doctor,handler);
  71. }
  72. /**
  73. * 将成员写入redis
  74. * @param session_id 会话ID
  75. * @param users 用户集合
  76. * @param createDate 创建日期
  77. * @param handler 回调
  78. */
  79. createParticipantsToRedis(session_id,users,createDate,handler){
  80. let participants_key = super.makeRedisKey(RedisKey.Participants,session_id);
  81. for(var j in users){
  82. let user_session_key = super.makeRedisKey(RedisKey.UsersSessions,users[j]);
  83. redis.zaddAsync(participants_key, createDate.getTime(),users[j]).then(function(res){
  84. return redis.zaddAsync(user_session_key,createDate.getTime(),session_id);
  85. }
  86. ).catch(function(err){
  87. log.error("createParticipantsToRedis is fail error:"+err+",session_id:"+session_id+",users:"+users.join(","));
  88. handler(false);
  89. });
  90. }
  91. handler(true);
  92. }
  93. /**
  94. * mysql成员创建
  95. * @param users
  96. */
  97. createParticipantsToMysql(session_id,users){
  98. return ParticipantRepo.createParticipantsToMysql(session_id,users);
  99. }
  100. /**
  101. * MUC成员创建
  102. * @param users
  103. */
  104. createMUCParticipants(users){
  105. return true;
  106. }
  107. /**
  108. * 移除讨论组成员
  109. * @param sessionId
  110. * @param user
  111. */
  112. deleteUser(sessionId,user){
  113. let self = this;
  114. let participants_key = super.makeRedisKey(RedisKeys.Participants,sessionId);
  115. let user_session_key = super.makeRedisKey(RedisKeys.UsersSessions,user);
  116. //1.移除SESSION成员表中的成员信息
  117. redis.zremAsync(participants_key,user).then(function(res){
  118. log.info("remove participants:"+res);
  119. //2.删除对应人员的Session列表
  120. redis.zremAsync(user_session_key,sessionId).then(function(res){
  121. log.info("remove user_session:"+res);
  122. //3.移除数据库中的人员记录
  123. self.deleteUserFromMysql(sessionId,user);
  124. modelUtil.emitData(self.eventEmitter,{"status":200,"msg":"人员删除成功!"});
  125. }).catch(function(err){
  126. modelUtil.emitData(self.eventEmitter,{"status":-1,"msg":"人员删除失败!"+err});
  127. })
  128. }).catch(function(err){
  129. modelUtil.emitData(self.eventEmitter,{"status":-1,"msg":"人员删除失败!"+err});
  130. })
  131. }
  132. /**
  133. * 添加讨论组成员
  134. * @param sessionId
  135. * @param user
  136. */
  137. pushUser(sessionId,user){
  138. let self = this;
  139. let users = [];
  140. users.push(user);
  141. self.createParticipantsToRedis(sessionId,users,new Date(),function(res){
  142. if(res){
  143. self.createParticipantsToMysql(sessionId,users);
  144. }else{
  145. modelUtil.emitData(self.eventEmitter,{"status":-1,"msg":"人员添加失败!"});
  146. }
  147. })
  148. }
  149. /**
  150. * user从数据库中删除
  151. * @param sessionId 会话
  152. * @param user 用户
  153. */
  154. deleteUserFromMysql(sessionId,user){
  155. ParticipantRepo.deleteUserFromMysql(sessionId,user);
  156. }
  157. }
  158. // Expose class
  159. module.exports = Participants;