search.repo.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. 'use strict';
  2. let ImDb = require('../mysql/db/im.db');
  3. let async = require("async");
  4. let ObjectUtil = require("../../util/object.util.js");
  5. let vsprintf = require("sprintf-js").vsprintf;
  6. const DB_TABLES = require('../../include/commons').DB_TABLES;
  7. class SearchRepo {
  8. constructor() {
  9. }
  10. /**
  11. * 查询正常会话及议题已结束的会话(P2P, MUC)
  12. *
  13. * @param userId
  14. * @param handler
  15. */
  16. static findTopicEndedSessionIdList(userId, handler) {
  17. let sql = "SELECT s.id " +
  18. "FROM sessions s, topics t, participants p " +
  19. "WHERE p.participant_id = ? AND p.session_id = s.id AND s.id = t.session_id AND t.end_message_id IS NOT NULL AND s.`type` IN (1) " +
  20. " UNION " +
  21. "SELECT s.id " +
  22. "FROM sessions s, participants p " +
  23. "WHERE p.participant_id = ? AND p.session_id = s.id AND s.`type` IN (2, 3)";
  24. ImDb.execQuery({
  25. sql: sql,
  26. args: [userId, userId],
  27. handler: handler
  28. });
  29. }
  30. /**
  31. * 全部搜索
  32. *
  33. * @param sessionIdList
  34. * @param keyword
  35. * @param userTable
  36. * @param handler
  37. */
  38. static searchAll(sessionIdList, keyword, userTable, handler) {
  39. let data = {};
  40. async.waterfall([
  41. function (callback) {
  42. SearchRepo.searchUser(sessionIdList, keyword, userTable, 0, 3, function (err, res) {
  43. if (err) return handler(err, null);
  44. let buffer = SearchRepo.userForge(res);
  45. data.users = buffer;
  46. callback(null);
  47. });
  48. },
  49. function (callback) {
  50. SearchRepo.searchSessions(sessionIdList, keyword, 0, 3, function (err, res) {
  51. if (err) return handler(err, null);
  52. data.sessions = SearchRepo.sessionForge(res, keyword);
  53. callback(null);
  54. })
  55. },
  56. function (callback) {
  57. SearchRepo.searchMessages(sessionIdList, keyword, 0, 3, function (err, res) {
  58. if (err) return handler(err, null);
  59. data.messages = SearchRepo.messageForge(res);
  60. handler(null, data);
  61. })
  62. }
  63. ]);
  64. }
  65. /**
  66. * 用户搜索
  67. *
  68. * @param sessionIdList
  69. * @param keyword
  70. * @param userTable
  71. * @param page
  72. * @param size
  73. * @param handler
  74. */
  75. static searchUser(sessionIdList, keyword, userTable, page, size, handler) {
  76. let sql = "SELECT DISTINCT s.id session_id, s.name session_name, s.type session_type, s.business_type, u.id user_id, u.name user_name, u.sex, u.birthdate, u.avatar %s" +
  77. " FROM sessions s, participants p, " + userTable +
  78. " u WHERE s.id in (?) AND s.id = p.session_id AND p.participant_id = u.id AND u.name like ? ";
  79. if (userTable === DB_TABLES.Doctors) {
  80. sql += " AND s.type = 2 ";
  81. }
  82. sql += " limit ?, ? ";
  83. sql = vsprintf(sql, [userTable == DB_TABLES.Doctors ? ', hospital_name' : '']);
  84. keyword = '%' + keyword + '%';
  85. ImDb.execQuery({
  86. sql: sql,
  87. args: [sessionIdList, keyword, page * size, size],
  88. handler: handler
  89. });
  90. }
  91. /**
  92. * 会话搜索。搜索会话名称与会话成员的名称。若有一个符合,就选中。
  93. */
  94. static searchSessions(sessionIdList, keyword, page, size, handler) {
  95. if (sessionIdList.length == 0) {
  96. return handler(null, []);
  97. }
  98. let sql = "SELECT * FROM(" +
  99. "SELECT s.id, s.name, s.type, s.create_date, s.business_type, u.name participant_name " +
  100. "FROM sessions s, participants p, doctors u " +
  101. "WHERE s.id IN (?) AND s.id = p.session_id AND p.participant_id = u.id AND (s.name LIKE ? or u.name like ?) " +
  102. " UNION " +
  103. "SELECT s.id, s.name, s.type, s.create_date, s.business_type, u.name participant_name " +
  104. "FROM sessions s, participants p, patients u " +
  105. "WHERE s.id IN (?) AND s.id = p.session_id AND p.participant_id = u.id AND (s.name LIKE ? or u.name like ?) " +
  106. ") X LIMIT ?, ?";
  107. keyword = '%' + keyword + '%';
  108. ImDb.execQuery({
  109. sql: sql,
  110. args: [sessionIdList, keyword, keyword, sessionIdList, keyword, keyword, page * size, size],
  111. handler: handler
  112. });
  113. }
  114. /**
  115. * 消息搜索
  116. */
  117. static searchMessages(sessionIdList, keyword, page, size, handler) {
  118. let sql = "SELECT * FROM(" +
  119. "SELECT s.id session_id, s.name session_name, s.type session_type, s.business_type session_business_type, m.id message_id, m.sender_id, m.sender_name, m.timestamp, m.content " +
  120. "FROM sessions s, muc_messages m " +
  121. "WHERE s.id IN (?) AND s.id = m.session_id AND s.`type` = 1 AND m.content_type = 1 AND m.content LIKE ? " +
  122. " UNION " +
  123. "SELECT s.id session_id, s.name session_name, s.type session_type, s.business_type session_business_type, m.id message_id, m.sender_id, m.sender_name, m.timestamp, m.content " +
  124. "FROM sessions s, p2p_messages m " +
  125. "WHERE s.id IN (?) AND s.id = m.session_id AND s.`type` = 2 AND m.content_type = 1 AND m.content LIKE ? " +
  126. " UNION " +
  127. "SELECT s.id session_id, s.name session_name, s.type session_type, s.business_type session_business_type, m.id message_id, m.sender_id, m.sender_name, m.timestamp, m.content " +
  128. "FROM sessions s, group_messages m " +
  129. "WHERE s.id IN (?) AND s.id = m.session_id AND s.`type` = 3 AND m.content_type = 1 AND m.content LIKE ? ) X " +
  130. "ORDER BY X.session_id, X.message_id LIMIT ?, ?";
  131. keyword = '%' + keyword + '%';
  132. ImDb.execQuery({
  133. sql: sql,
  134. args: [sessionIdList, keyword, sessionIdList, keyword, sessionIdList, keyword, page * size, size],
  135. handler: handler
  136. });
  137. }
  138. static userForge(res) {
  139. res.forEach(function (user) {
  140. if (!user.avatar) user.avatar = "";
  141. user.birthdate = user.birthdate ? ObjectUtil.timestampToLong(user.birthdate) : "";
  142. });
  143. return res;
  144. }
  145. static sessionForge(res, keyword) {
  146. let result = [];
  147. let lastSessionId = null;
  148. let tempSession = null;
  149. res.forEach(function (session) {
  150. if (session.session_id !== lastSessionId) {
  151. lastSessionId = session.session_id;
  152. tempSession = {
  153. id: session.id,
  154. name: session.name,
  155. type: session.type,
  156. business_type: session.business_type,
  157. /*create_date: ObjectUtil.timestampToLong(session.create_date),*/
  158. participants: []
  159. };
  160. result.push(tempSession);
  161. }
  162. if (session.participant_name.indexOf(keyword) >= 0) tempSession.participants.push({name: session.participant_name});
  163. });
  164. return result;
  165. }
  166. static messageForge(res) {
  167. let result = [];
  168. let lastSessionId = null;
  169. let session = null;
  170. res.forEach(function (message) {
  171. if (message.session_id !== lastSessionId) {
  172. lastSessionId = message.session_id;
  173. session = {
  174. session_id: message.session_id,
  175. session_name: message.session_name,
  176. session_type: message.session_type,
  177. /*session_business_type: message.session_business_type,*/
  178. messages: []
  179. };
  180. result.push(session);
  181. }
  182. session.messages.push({
  183. id: message.message_id,
  184. sender_id: message.sender_id,
  185. sender_name: message.sender_name,
  186. timestamp: ObjectUtil.timestampToLong(message.timestamp),
  187. content: message.content
  188. });
  189. });
  190. return result;
  191. }
  192. }
  193. module.exports = SearchRepo;