participant.repo.js 7.6 KB

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