participants.js 5.6 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/modelUtil');
  10. let imDb = require('../../repository/mysql/db/im.db');
  11. let log = require('../../util/log.js');
  12. const RedisKey = require('../../include/commons').REDIS_KEYS;
  13. const IMTABLE = require('../../include/commons').IM_DB;
  14. class Participants extends RedisModel {
  15. constructor() {
  16. super();
  17. }
  18. /**
  19. * 根据topicId获取对应的议题的成员信息
  20. * @param topicId
  21. */
  22. getParticipantsByTopicId(topicId){
  23. }
  24. /**
  25. * 根据sessionId获取对应的议题的成员信息
  26. * @param sessionId
  27. */
  28. getParticipantsBySessionId(sessionId,handler){
  29. let participant_key = super.makeRedisKey(RedisKey.Participants,sessionId);
  30. redis.existsAsync(participant_key).then(function(res){
  31. if(res){
  32. redis.zrangeAsync(participant_key,0,-1).then(function(res){
  33. handler(res);
  34. })
  35. }else{
  36. let sql ="select w.* from participants w where w.session_id =? ";
  37. imDb.execQuery({
  38. "sql": sql,
  39. "args": [sessionId],
  40. "handler": function (err, res) {
  41. if(err) {
  42. log.error("getParticipantsBySessionId is fail error: "+err);
  43. }
  44. handler(res);
  45. }
  46. });
  47. }
  48. })
  49. }
  50. /**
  51. * 判断成员是否存在这个讨论组中
  52. * @param sessionId
  53. * @param userId
  54. */
  55. existsUser(sessionId,userId,handler){
  56. let participant_key = super.makeRedisKey(RedisKey.Participants,sessionId);
  57. redis.existsAsync(participant_key).then(function(res){
  58. if(res){//存在redis中直接从redis校验
  59. redis.zrangeAsync(participant_key,0,-1).then(function(res){
  60. let exists = false
  61. for(var j in res){
  62. var value = res[j];
  63. if(value==userId){
  64. exists = true;
  65. break;
  66. }
  67. }
  68. handler(exists);
  69. })
  70. }else{//不存在从数据库中获取
  71. let sql ="select count(1) as count from participants w where w.session_id =? and w.participaint_id = ? ";
  72. imDb.execQuery({
  73. "sql": sql,
  74. "args": [sessionId,userId],
  75. "handler": function (err, res) {
  76. if(err) {
  77. log.error("existsUser is fail error: "+err);
  78. }
  79. handler(res[0].count);
  80. }
  81. });
  82. }
  83. })
  84. }
  85. /**
  86. * 根据医生和患者
  87. * @param patient
  88. * @param doctor
  89. */
  90. getSessionIdByParticipants(patient,doctor,handler){
  91. let sql ="select session_id from "+IMTABLE.PARTICIPANTS+" p1 ,participants p2 " +
  92. "where p1.session_id = p2.session_id and " +
  93. "((p1.participaint_id = ? and p2.participaint_id = ?) or (p1.participaint_id = ? and p2.participaint_id = ?))"
  94. imDb.execQuery({
  95. "sql": sql,
  96. "args": [patient,doctor,doctor,patient],
  97. "handler": function (err, res) {
  98. if(err) {
  99. log.error("getSessionIdByParticipants is fail error: "+err);
  100. }
  101. handler(err,res);
  102. }
  103. });
  104. }
  105. /**
  106. * 将成员写入redis
  107. * @param session_id 会话ID
  108. * @param users 用户集合
  109. * @param createDate 创建日期
  110. * @param handler 回调
  111. */
  112. createParticipantsToRedis(session_id,users,createDate,handler){
  113. let participants_key = super.makeRedisKey(RedisKey.Participants,session_id);
  114. for(var j in users){
  115. let user_session_key = super.makeRedisKey(RedisKey.UsersSessions,users[j]);
  116. redis.zaddAsync(participants_key, createDate.getTime(),users[j]).then(function(res){
  117. return redis.zaddAsync(user_session_key,createDate.getTime(),session_id);
  118. }
  119. ).catch(function(err){
  120. log.error("createParticipantsToRedis is fail error:"+err+",session_id:"+session_id+",users:"+users.join(","));
  121. handler(false);
  122. });
  123. }
  124. handler(true);
  125. }
  126. /**
  127. * mysql成员创建
  128. * @param users
  129. */
  130. createParticipantsToMysql(session_id,users){
  131. let sql="insert into "+IMTABLE.PARTICIPANTS +" (session_id,participaint_id,participaint_role,receiving) VALUES "
  132. let args=[];
  133. for(var j in users){
  134. sql+="(?,?,?,?),";
  135. args.push(session_id);
  136. args.push(users[j]);
  137. args.push(0);
  138. args.push(0);
  139. }
  140. sql = sql.substring(0,sql.lastIndexOf(","));
  141. imDb.execQuery({
  142. "sql": sql,
  143. "args": args,
  144. "handler": function (err, res) {
  145. if(err) {
  146. log.error("createParticipantsForMysql is fail error: "+err+",session_id:"+session_id+",users:"+users.join(","));
  147. }else{
  148. return res;
  149. }
  150. }
  151. });
  152. return true;
  153. }
  154. /**
  155. * MUC成员创建
  156. * @param users
  157. */
  158. createMUCParticipants(users){
  159. return true;
  160. }
  161. }
  162. // Expose class
  163. module.exports = Participants;