participant.repo.js 3.3 KB

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