participant.repo.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * 搜索功能。
  3. */
  4. "use strict";
  5. let ImDb = require('../mysql/db/im.db');
  6. let DbUtil = require('../../util/db.util');
  7. let log = require('../../util/log.js');
  8. const DB_TABLES = require('../../include/commons').DB_TABLES;
  9. const SESSION_USER_STATUS = require('../../include/commons').SESSION_USER_STATUS;
  10. const SESSION_TYPES = require('../../include/commons').SESSION_TYPES;
  11. class ParticipantRepo {
  12. constructor() {
  13. }
  14. /**
  15. * 获取会话的成员列表
  16. *
  17. * @param sessionId
  18. * @param handler
  19. */
  20. static findAll(sessionId, handler) {
  21. let sql = "select u.id, u.name, u.sex, u.birthdate, u.avatar, p.participant_role role, false is_patient from sessions s, participants p, doctors u " +
  22. "where s.id = ? and s.id = p.session_id and p.participant_id = u.id union " +
  23. "select u.id, u.name, u.sex, u.birthdate, u.avatar, p.participant_role role, true is_patient from sessions s, participants p, patients u " +
  24. "where s.id = ? and s.id = p.session_id and p.participant_id = u.id";
  25. ImDb.execQuery({
  26. "sql": sql,
  27. "args": [sessionId, sessionId],
  28. "handler": handler
  29. });
  30. }
  31. /**
  32. * 获取会话的成员ID列表
  33. *
  34. * @param sessionId
  35. * @param handler
  36. */
  37. static findIds(sessionId, handler) {
  38. let sql = "select u.id, false is_patient from sessions s, participants p, doctors u " +
  39. "where s.id = ? and s.id = p.session_id and p.participant_id = u.id union " +
  40. "select u.id, true is_patient from sessions s, participants p, patients u " +
  41. "where s.id = ? and s.id = p.session_id and p.participant_id = u.id";
  42. ImDb.execQuery({
  43. "sql": sql,
  44. "args": [sessionId, sessionId],
  45. "handler": handler
  46. });
  47. }
  48. /**
  49. * 获取会话的成员头像列表
  50. *
  51. * @param sessionId
  52. * @param handler
  53. */
  54. static findAllAvatars(sessionId, handler) {
  55. let sql = "select u.id, u.avatar from sessions s, participants p, doctors u " +
  56. "where s.id = ? and s.id = p.session_id and p.participant_id = u.id union " +
  57. "select u.id, u.avatar from sessions s, participants p, patients u " +
  58. "where s.id = ? and s.id = p.session_id and p.participant_id = u.id";
  59. ImDb.execQuery({
  60. "sql": sql,
  61. "args": [sessionId, sessionId],
  62. "handler": handler
  63. });
  64. }
  65. /**
  66. * 获取会话的成员列表
  67. *
  68. * @param sessionId
  69. * @param participantId
  70. * @param role
  71. * @param handler
  72. */
  73. static updateParticipant(sessionId, participantId, role, handler) {
  74. let sql = "update participants set participant_role = ? where session_id = ? and participant_id = ?";
  75. ImDb.execQuery({
  76. "sql": sql,
  77. "args": [role, sessionId, participantId],
  78. "handler": handler
  79. });
  80. }
  81. /**
  82. * 获取P2P成员所在会话。将成员的ID排序后取哈希值即可。
  83. *
  84. * @param userId
  85. * @param anotherUserId
  86. * @param handler
  87. */
  88. static findSessionIdByParticipantIds(userId, anotherUserId, handler) {
  89. let sessionId = DbUtil.stringArrayHash([userId, anotherUserId]);
  90. handler(null, sessionId);
  91. }
  92. static findMucSessionIdByUser(users,handler){
  93. var userTemp = [];//先匹配出在线用户
  94. for(var j in users){
  95. if(users[j]==SESSION_USER_STATUS.ONLINE){
  96. userTemp.push(j);
  97. }
  98. }
  99. let sql ="select s.* from "+DB_TABLES.Participants +" p1,"+DB_TABLES.Participants+ " p2,"+
  100. DB_TABLES.Sessions+" s where p1.session_id = s.id and p2.session_id = s.id and s.type =? " +
  101. "and ((p1.participant_id =? and p2.participant_id = ?) or (p1.participant_id =? and p2.participant_id = ?))"
  102. ImDb.execQuery({
  103. "sql": sql,
  104. "args": [SESSION_TYPES.MUC,userTemp[0], userTemp[1], userTemp[1],userTemp[0]],
  105. "handler": handler
  106. });
  107. }
  108. /**
  109. * 更新最后消息获取时间。
  110. *
  111. * @param lastMessageTime
  112. * @param sessionId
  113. * @param participantId
  114. * @param handler
  115. */
  116. static updateLastFetchTime(lastMessageTime, sessionId, participantId, handler) {
  117. let sql = "update " + DB_TABLES.Participants + " set last_fetch_time=? where session_id = ? and participant_id =?";
  118. ImDb.execQuery({
  119. "sql": sql,
  120. "args": [lastMessageTime, sessionId, participantId],
  121. "handler": handler
  122. });
  123. }
  124. /**
  125. * 用户是否在指定Session中
  126. *
  127. * @param sessionId
  128. * @param userId
  129. * @param handler
  130. */
  131. static existsParticipant(sessionId, userId, handler) {
  132. let sql = "select case when count(*) > 0 then true else false end exist from participants w where w.session_id =? and w.participant_id = ? ";
  133. ImDb.execQuery({
  134. "sql": sql,
  135. "args": [sessionId, userId],
  136. "handler": handler
  137. });
  138. }
  139. /**
  140. * mysql成员创建
  141. *
  142. * @param sessionId
  143. * @param users JSON
  144. * @param handler
  145. */
  146. static saveParticipantsToMysql(sessionId, users, handler) {
  147. let sql = "insert into " + DB_TABLES.Participants + " (session_id,participant_id,participant_role) VALUES "
  148. let args = [];
  149. for (var j in users) {
  150. sql += "(?,?,?)";
  151. let tokens = users[j].split(":");
  152. args.push(sessionId);
  153. args.push(tokens[0]);
  154. args.push(tokens.length > 1 ? tokens[1] : '0');
  155. if (j != users.length - 1) sql += ", ";
  156. }
  157. sql += " ON DUPLICATE KEY UPDATE participant_role = VALUES(participant_role)";
  158. ImDb.execQuery({
  159. "sql": sql,
  160. "args": args,
  161. "handler": handler
  162. });
  163. }
  164. static deleteUserFromMysql(sessionId, userId) {
  165. let sql = "delete from " + DB_TABLES.Participants + " where user_id=? and session_id=? ";
  166. ImDb.execQuery({
  167. "sql": sql,
  168. "args": [userId, sessionId],
  169. "handler": handler
  170. });
  171. }
  172. static getPatientOpenid(code, handler) {
  173. var sql = "select openid from patients where code = ? ";
  174. ImDb.execQuery({
  175. "sql": sql,
  176. "args": [code],
  177. "handler": handler
  178. });
  179. }
  180. }
  181. module.exports = ParticipantRepo;