participant.repo.js 8.5 KB

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