participant.repo.js 4.0 KB

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