participant.repo.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. const SESSION_BUSINESS_TYPE = require('../../include/commons').SESSION_BUSINESS_TYPE;
  12. class ParticipantRepo {
  13. constructor() {
  14. }
  15. /**
  16. * 获取会话的成员列表
  17. *
  18. * @param sessionId
  19. * @param handler
  20. */
  21. static findAll(sessionId, handler) {
  22. let sql = "SELECT u.id, u.name, u.sex, u.birthdate, u.avatar, p.participant_role role, false is_patient,p.last_fetch_time FROM sessions s, participants p, doctors u " +
  23. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id union " +
  24. "SELECT u.id, u.name, u.sex, u.birthdate, u.avatar, p.participant_role role, true is_patient,p.last_fetch_time FROM sessions s, participants p, patients u " +
  25. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id";
  26. ImDb.execQuery({
  27. "sql": sql,
  28. "args": [sessionId, sessionId],
  29. "handler": function(err,res){
  30. if(res&&res.length>0){
  31. for(var j in res){
  32. res[j].last_fetch_time = res[j].last_fetch_time.getTime();
  33. }
  34. }
  35. handler(err,res);
  36. }
  37. });
  38. }
  39. /**
  40. * 获取会话的成员ID列表
  41. *
  42. * @param sessionId
  43. * @param handler
  44. */
  45. static findIds(sessionId, handler) {
  46. let sql =
  47. "SELECT u.id, u.name, false is_patient, p.participant_role,u.avatar FROM sessions s, participants p, doctors u " +
  48. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id " +
  49. "UNION " +
  50. "SELECT u.id, u.name, true is_patient, p.participant_role,u.avatar FROM sessions s, participants p, patients u " +
  51. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id";
  52. ImDb.execQuery({
  53. "sql": sql,
  54. "args": [sessionId, sessionId],
  55. "handler": handler
  56. });
  57. }
  58. /**
  59. * 获取会话的成员头像列表
  60. *
  61. * @param sessionId
  62. * @param handler
  63. */
  64. static findAllAvatars(sessionId, handler) {
  65. let sql = "SELECT u.id, u.avatar FROM sessions s, participants p, doctors u " +
  66. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id union " +
  67. "SELECT u.id, u.avatar FROM sessions s, participants p, patients u " +
  68. "WHERE s.id = ? AND s.id = p.session_id AND p.participant_id = u.id";
  69. ImDb.execQuery({
  70. "sql": sql,
  71. "args": [sessionId, sessionId],
  72. "handler": handler
  73. });
  74. }
  75. /**
  76. * 获取会话的成员列表
  77. *
  78. * @param sessionId
  79. * @param participantId
  80. * @param role
  81. * @param handler
  82. */
  83. static updateParticipant(sessionId, participantId, role, handler) {
  84. let sql = "update participants set participant_role = ? WHERE session_id = ? AND participant_id = ?";
  85. ImDb.execQuery({
  86. "sql": sql,
  87. "args": [role, sessionId, participantId],
  88. "handler": handler
  89. });
  90. }
  91. /**
  92. * 获取P2P成员所在会话。将成员的ID排序后取哈希值即可。
  93. *
  94. * @param userId
  95. * @param anotherUserId
  96. * @param handler
  97. */
  98. static findSessionIdByParticipantIds(userId, anotherUserId, handler) {
  99. let sessionId = DbUtil.stringArrayHash([userId, anotherUserId]);
  100. handler(null, sessionId);
  101. }
  102. static getBusinessType(users, handler) {
  103. let sql = "SELECT count(1) as count FROM patients p WHERE p.id in (?)";
  104. ImDb.execQuery({
  105. "sql": sql,
  106. "args": [users],
  107. "handler": function (err, res) {
  108. if (err) {
  109. console.log("err businessType : " + err);
  110. } else {
  111. if (res[0].count > 0) {
  112. handler(err, SESSION_BUSINESS_TYPE.PATIENT);
  113. } else {
  114. handler(err, SESSION_BUSINESS_TYPE.DOCTOR);
  115. }
  116. }
  117. }
  118. });
  119. }
  120. static findNameById(userId, handler) {
  121. let sql = "SELECT p.name,p.sex,p.birthdate FROM patients p WHERE p.id =? union SELECT d.name,d.sex,d.birthdate FROM doctors d WHERE d.id =?";
  122. ImDb.execQuery({
  123. "sql": sql,
  124. "args": [userId, userId],
  125. "handler": function (err, res) {
  126. if (err) {
  127. console.log("err businessType : " + err);
  128. } else {
  129. handler(null, res);
  130. }
  131. }
  132. });
  133. }
  134. static findMucSessionIdByUser(users, handler) {
  135. //先匹配出在线用户
  136. let userTemp = [];
  137. users.forEach(function (user) {
  138. if (user == SESSION_USER_STATUS.ONLINE) {
  139. userTemp.push(user);
  140. }
  141. });
  142. let sql = "SELECT DISTINCT s.* FROM " + DB_TABLES.Participants + " p1," + DB_TABLES.Participants + " p2," +
  143. DB_TABLES.Sessions + " s WHERE p1.session_id = s.id AND p2.session_id = s.id AND s.type =? " +
  144. "AND ((p1.participant_id =? AND p2.participant_id = ?) or (p1.participant_id =? AND p2.participant_id = ?))";
  145. ImDb.execQuery({
  146. "sql": sql,
  147. "args": [SESSION_TYPES.MUC, userTemp[0], userTemp[1], userTemp[1], userTemp[0]],
  148. "handler": handler
  149. });
  150. }
  151. /**
  152. * 更新最后消息获取时间。
  153. *
  154. * @param lastMessageTime
  155. * @param sessionId
  156. * @param participantId
  157. * @param handler
  158. */
  159. static updateLastFetchTime(lastMessageTime, sessionId, participantId, handler) {
  160. let sql = "update " + DB_TABLES.Participants + " set last_fetch_time=? WHERE session_id = ? AND participant_id =?";
  161. ImDb.execQuery({
  162. "sql": sql,
  163. "args": [lastMessageTime, sessionId, participantId],
  164. "handler": handler
  165. });
  166. }
  167. /**
  168. * 用户是否在指定Session中
  169. *
  170. * @param sessionId
  171. * @param userId
  172. * @param handler
  173. */
  174. static existsParticipant(sessionId, userId, handler) {
  175. let sql = "SELECT case when count(*) > 0 then true else false end exist FROM participants w WHERE w.session_id =? AND w.participant_id = ? ";
  176. ImDb.execQuery({
  177. "sql": sql,
  178. "args": [sessionId, userId],
  179. "handler": handler
  180. });
  181. }
  182. /**
  183. * mysql成员创建
  184. *
  185. * @param sessionId
  186. * @param users JSON
  187. * @param handler
  188. */
  189. static saveParticipantsToMysql(sessionId, users, handler) {
  190. let sql = "INSERT INTO " + DB_TABLES.Participants + " (session_id,participant_id,participant_role) VALUES ";
  191. let args = [];
  192. for (let j in users) {
  193. sql += "(?,?,?)";
  194. let tokens = users[j].split(":");
  195. args.push(sessionId);
  196. args.push(tokens[0]);
  197. args.push(tokens.length > 1 ? tokens[1] : '0');
  198. if (j != users.length - 1) sql += ", ";
  199. }
  200. sql += " ON DUPLICATE KEY UPDATE participant_role = VALUES(participant_role)";
  201. ImDb.execQuery({
  202. "sql": sql,
  203. "args": args,
  204. "handler": handler
  205. });
  206. }
  207. static deleteUserFromMysql(sessionId, userId, handler) {
  208. let sql = "delete from " + DB_TABLES.Participants + " where participant_id=? and session_id=? ";
  209. ImDb.execQuery({
  210. "sql": sql,
  211. "args": [userId, sessionId],
  212. "handler": handler || function (err, res) {
  213. log.info("deleteUserFromMysql");
  214. }
  215. });
  216. }
  217. static deleteAllUser(sessionId, handler) {
  218. let sql = "delete from " + DB_TABLES.Participants + " where session_id=? ";
  219. ImDb.execQuery({
  220. "sql": sql,
  221. "args": [sessionId],
  222. "handler": handler || function (err, res) {
  223. log.info("deleteUserFromMysql");
  224. }
  225. });
  226. }
  227. }
  228. module.exports = ParticipantRepo;