participants.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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){
  29. }
  30. /**
  31. * 判断成员是否存在这个讨论组中
  32. * @param sessionId
  33. * @param userId
  34. */
  35. existsUser(sessionId,userId,handler){
  36. let participant_key = super.makeRedisKey(RedisKey.Participants,sessionId);
  37. return redis.zrangeAsync(participant_key,0,-1).then(function(res){
  38. let exists = false
  39. for(var j in res){
  40. var value = res[j];
  41. if(value==userId){
  42. exists = true;
  43. break;
  44. }
  45. }
  46. handler(exists);
  47. })
  48. }
  49. /**
  50. * 根据医生和患者
  51. * @param patient
  52. * @param doctor
  53. */
  54. getSessionIdByParticipants(patient,doctor,handler){
  55. let sql ="select session_id from "+IMTABLE.PARTICIPANTS+" p1 ,participants p2 " +
  56. "where p1.session_id = p2.session_id and " +
  57. "((p1.participaint_id = ? and p2.participaint_id = ?) or (p1.participaint_id = ? and p2.participaint_id = ?))"
  58. imDb.execQuery({
  59. "sql": sql,
  60. "args": [patient,doctor,doctor,patient],
  61. "handler": function (err, res) {
  62. if(err) {
  63. log.error("getSessionIdByParticipants is fail error: "+err);
  64. }
  65. handler(err,res);
  66. }
  67. });
  68. }
  69. /**
  70. * 将成员写入redis
  71. * @param session_id 会话ID
  72. * @param users 用户集合
  73. * @param createDate 创建日期
  74. * @param handler 回调
  75. */
  76. createParticipantsToRedis(session_id,users,createDate,handler){
  77. let participants_key = super.makeRedisKey(RedisKey.Participants,session_id);
  78. for(var j in users){
  79. let user_session_key = super.makeRedisKey(RedisKey.UsersSessions,users[j]);
  80. redis.zaddAsync(participants_key, createDate.getTime(),users[j]).then(function(res){
  81. return redis.zaddAsync(user_session_key,createDate.getTime(),session_id);
  82. }
  83. ).catch(function(err){
  84. log.error("createParticipantsToRedis is fail error:"+err+",session_id:"+session_id+",users:"+users.join(","));
  85. handler(false);
  86. });
  87. }
  88. handler(true);
  89. }
  90. /**
  91. * mysql成员创建
  92. * @param users
  93. */
  94. createParticipantsToMysql(session_id,users){
  95. let sql="insert into "+IMTABLE.PARTICIPANTS +" (session_id,participaint_id,participaint_role,receiving) VALUES "
  96. let args=[];
  97. for(var j in users){
  98. sql+="(?,?,?,?),";
  99. args.push(session_id);
  100. args.push(users[j]);
  101. args.push(0);
  102. args.push(0);
  103. }
  104. sql = sql.substring(0,sql.lastIndexOf(","));
  105. imDb.execQuery({
  106. "sql": sql,
  107. "args": args,
  108. "handler": function (err, res) {
  109. if(err) {
  110. log.error("createParticipantsForMysql is fail error: "+err+",session_id:"+session_id+",users:"+users.join(","));
  111. }else{
  112. return res;
  113. }
  114. }
  115. });
  116. return true;
  117. }
  118. /**
  119. * MUC成员创建
  120. * @param users
  121. */
  122. createMUCParticipants(users){
  123. return true;
  124. }
  125. }
  126. // Expose class
  127. module.exports = Participants;