participant.repo.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. class ParticipantRepo {
  10. constructor() {
  11. }
  12. /**
  13. * 获取会话的成员列表
  14. *
  15. * @param sessionId
  16. * @param handler
  17. */
  18. static findAll(sessionId, handler) {
  19. 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 " +
  20. "where s.id = ? and s.id = p.session_id and p.participant_id = u.id union " +
  21. "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 " +
  22. "where s.id = ? and s.id = p.session_id and p.participant_id = u.id";
  23. ImDb.execQuery({
  24. "sql": sql,
  25. "args": [sessionId, sessionId],
  26. "handler": handler
  27. });
  28. }
  29. /**
  30. * 获取会话的成员头像列表
  31. *
  32. * @param sessionId
  33. * @param handler
  34. */
  35. static findAllAvatars(sessionId, handler) {
  36. let sql = "select u.id, u.avatar from sessions s, participants p, doctors u " +
  37. "where s.id = ? and s.id = p.session_id and p.participant_id = u.id union " +
  38. "select u.id, u.avatar from sessions s, participants p, patients u " +
  39. "where s.id = ? and s.id = p.session_id and p.participant_id = u.id";
  40. ImDb.execQuery({
  41. "sql": sql,
  42. "args": [sessionId, sessionId],
  43. "handler": handler
  44. });
  45. }
  46. /**
  47. * 获取会话的成员列表
  48. *
  49. * @param sessionId
  50. * @param participantId
  51. * @param role
  52. * @param handler
  53. */
  54. static updateParticipant(sessionId, participantId, role, handler) {
  55. let sql = "update participants set participant_role = ? where session_id = ? and participant_id = ?";
  56. ImDb.execQuery({
  57. "sql": sql,
  58. "args": [role, sessionId, participantId],
  59. "handler": handler
  60. });
  61. }
  62. /**
  63. * 获取P2P成员所在会话。将成员的ID排序后取哈希值即可。
  64. *
  65. * @param userId
  66. * @param anotherUserId
  67. * @param handler
  68. */
  69. static findSessionIdByParticipantIds(userId, anotherUserId, handler) {
  70. let sessionId = DbUtil.stringArrayHash([userId, anotherUserId]);
  71. handler(null, sessionId);
  72. }
  73. static updateLastTime(lastMessageTime,sessionId,participantId,handler){
  74. let sql ="update "+DB_TABLES.Participants+" set last_fetch_time=? where session_id = ? and participant_id =?";
  75. ImDb.execQuery({
  76. "sql": sql,
  77. "args": [lastMessageTime,sessionId,participantId],
  78. "handler": handler||function(err,res){
  79. log.info(res);
  80. }
  81. });
  82. }
  83. /**
  84. * 用户是否在指定Session中
  85. *
  86. * @param sessionId
  87. * @param userId
  88. * @param handler
  89. */
  90. static existsParticipant(sessionId, userId, handler) {
  91. let sql = "select case when count(*) > 0 then true else false end exist from participants w where w.session_id =? and w.participant_id = ? ";
  92. ImDb.execQuery({
  93. "sql": sql,
  94. "args": [sessionId, userId],
  95. "handler": handler
  96. });
  97. }
  98. /**
  99. * mysql成员创建
  100. *
  101. * @param sessionId
  102. * @param users JSON
  103. * @param handler
  104. */
  105. static saveParticipantsToMysql(sessionId, users, handler) {
  106. let sql = "insert into " + DB_TABLES.Participants + " (session_id,participant_id,participant_role) VALUES "
  107. let args = [];
  108. for (var j in users) {
  109. sql += "(?,?,?)";
  110. let tokens = users[j].split(":");
  111. args.push(sessionId);
  112. args.push(tokens[0]);
  113. args.push(tokens.length > 1 ? tokens[1] : '0');
  114. if (j != users.length - 1) sql += ", ";
  115. }
  116. sql += " ON DUPLICATE KEY UPDATE participant_role = VALUES(participant_role)";
  117. ImDb.execQuery({
  118. "sql": sql,
  119. "args": args,
  120. "handler": handler
  121. });
  122. }
  123. static deleteUserFromMysql(sessionId, userId) {
  124. let sql = "delete from " + DB_TABLES.Participants + " where user_id=? and session_id=? ";
  125. ImDb.execQuery({
  126. "sql": sql,
  127. "args": [userId, sessionId],
  128. "handler": handler
  129. });
  130. }
  131. static getPatientOpenid(code, handler) {
  132. var sql = "select openid from patients where code = ? ";
  133. ImDb.execQuery({
  134. "sql": sql,
  135. "args": [code],
  136. "handler": handler
  137. });
  138. }
  139. }
  140. module.exports = ParticipantRepo;