search.repo.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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,2) " +
  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. * 查询正常会话及议题已结束的会话(P2P, MUC)
  32. *
  33. * @param userId
  34. * @param handler
  35. */
  36. static findTopicActiveSessionIdList(userId, handler) {
  37. let sql = "SELECT s.id " +
  38. "FROM sessions s, topics t, participants p " +
  39. "WHERE p.participant_id = ? AND p.session_id = s.id AND s.id = t.session_id AND t.end_message_id IS NULL AND s.`type` IN (1,2) " +
  40. " UNION " +
  41. "SELECT s.id " +
  42. "FROM sessions s, participants p " +
  43. "WHERE p.participant_id = ? AND p.session_id = s.id AND s.`type` IN (2,3) and s.id not in(" +
  44. " select DISTINCT p1.session_id from participants p1 ,topics t where p1.participant_id = ? and t.session_id = p1.session_id " +
  45. ") ";
  46. ImDb.execQuery({
  47. sql: sql,
  48. args: [userId, userId,userId],
  49. handler: handler
  50. });
  51. }
  52. /**
  53. * 全部搜索
  54. *
  55. * @param sessionIdList
  56. * @param keyword
  57. * @param userTable
  58. * @param handler
  59. */
  60. static searchAll(sessionIdList,userId, keyword, userTable, handler) {
  61. let data = {};
  62. async.waterfall([
  63. function (callback) {
  64. SearchRepo.searchUser(sessionIdList,userId, keyword, userTable, 0, 4, function (err, res) {
  65. if (err) return handler(err, null);
  66. let buffer = SearchRepo.userForge(res);
  67. data.users = buffer;
  68. callback(null);
  69. });
  70. },
  71. function (callback) {
  72. SearchRepo.searchSessions(sessionIdList,userId, keyword, 0, 4, function (err, res) {
  73. if (err) return handler(err, null);
  74. data.sessions = SearchRepo.sessionForge(res, keyword);
  75. callback(null);
  76. })
  77. },
  78. function (callback) {
  79. SearchRepo.searchMessages(sessionIdList,userId, keyword, 0, 4, function (err, res) {
  80. if (err) return handler(err, null);
  81. data.messages = SearchRepo.messageForge(res);
  82. handler(null, data);
  83. })
  84. }
  85. ]);
  86. }
  87. /**
  88. * 用户搜索
  89. *
  90. * @param sessionIdList
  91. * @param keyword
  92. * @param userTable
  93. * @param page
  94. * @param size
  95. * @param handler
  96. */
  97. static searchUser(sessionIdList,userId, keyword, userTable, page, size, handler) {
  98. let sql = "SELECT DISTINCT u.name user_name,s.id session_id, s.name session_name, s.type session_type, s.business_type, u.id user_id,u.sex, u.birthdate, u.avatar,u.idcard %s " +
  99. " FROM participants p, " + userTable +
  100. " u,sessions s WHERE s.id in (?) AND s.id = p.session_id AND p.participant_id = u.id and p.participant_id<>? AND (u.name like ? or u.idcard like ?) ";
  101. if (userTable === DB_TABLES.Doctors) {
  102. sql += " AND s.type in (2) and s.business_type = 1 ";
  103. }else{
  104. sql += " AND s.type in (1,2,8) and s.business_type = 2 ";
  105. }
  106. sql += " limit ?, ? ";
  107. sql = vsprintf(sql, [userTable == DB_TABLES.Doctors ? ', hospital_name' : '']);
  108. keyword = '%' + keyword + '%';
  109. ImDb.execQuery({
  110. sql: sql,
  111. args: [sessionIdList,userId, keyword,keyword, page * size, size],
  112. handler: handler
  113. });
  114. }
  115. /**
  116. * 会话搜索。搜索会话名称与会话成员的名称。若有一个符合,就选中。
  117. */
  118. static searchSessions(sessionIdList,userId, keyword, page, size, handler) {
  119. if (sessionIdList.length == 0) {
  120. return handler(null, []);
  121. }
  122. let sql = "SELECT * FROM(" +
  123. "SELECT s.id, s.name, s.type, s.create_date, s.business_type,GROUP_CONCAT(u. NAME) as participant_name " +
  124. "FROM sessions s, doctors u ,participants p " +
  125. "WHERE s.id IN (?) and s.type = 3 AND s.id = p.session_id AND p.participant_id = u.id and p.participant_id<>? AND (u.name like ? or s.name like ?) group by s.id " +
  126. ") X LIMIT ?, ?";
  127. keyword = '%' + keyword + '%';
  128. ImDb.execQuery({
  129. sql: sql,
  130. args: [sessionIdList,userId, keyword,keyword, page * size, size],
  131. handler: handler
  132. });
  133. }
  134. /**
  135. * 消息搜索
  136. */
  137. static searchMessages(sessionIdList,userId, keyword, page, size, handler) {
  138. let sql = "SELECT * FROM(" +
  139. "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 " +
  140. "FROM sessions s, muc_messages m " +
  141. "WHERE s.id IN (?) AND s.id = m.session_id AND s.`type` = 1 AND m.content_type = 1 AND m.content LIKE ? " +
  142. " UNION " +
  143. "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 " +
  144. "FROM sessions s, p2p_messages m " +
  145. "WHERE s.id IN (?) AND s.id = m.session_id AND s.`type` = 2 AND m.content_type = 1 AND m.content LIKE ? " +
  146. " UNION " +
  147. "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 " +
  148. "FROM sessions s, group_messages m " +
  149. "WHERE s.id IN (?) AND s.id = m.session_id AND s.`type` = 3 AND m.content_type = 1 AND m.content LIKE ? ) X " +
  150. "ORDER BY X.session_id, X.message_id LIMIT ?, ?";
  151. keyword = '%' + keyword + '%';
  152. ImDb.execQuery({
  153. sql: sql,
  154. args: [sessionIdList, keyword, sessionIdList, keyword, sessionIdList, keyword, page * size, size],
  155. handler: handler
  156. });
  157. }
  158. static userForge(res) {
  159. res.forEach(function (user) {
  160. if (!user.avatar) user.avatar = "";
  161. user.birthdate = user.birthdate ? user.birthdate.getTime():"";
  162. });
  163. return res;
  164. }
  165. static sessionForge(res, keyword) {
  166. let result = [];
  167. let lastSessionId = null;
  168. let tempSession = null;
  169. res.forEach(function (session) {
  170. if (session.id !== lastSessionId) {
  171. lastSessionId = session.id;
  172. tempSession = {
  173. id: session.id,
  174. name: session.name,
  175. type: session.type,
  176. business_type: session.business_type,
  177. /*create_date: ObjectUtil.timestampToLong(session.create_date),*/
  178. members: []
  179. };
  180. result.push(tempSession);
  181. }
  182. if (session.participant_name.indexOf(keyword) >= 0) tempSession.members.push({name: session.participant_name});
  183. });
  184. return result;
  185. }
  186. static messageForge(res) {
  187. let result = [];
  188. let lastSessionId = null;
  189. let session = null;
  190. res.forEach(function (message) {
  191. if (message.session_id !== lastSessionId) {
  192. lastSessionId = message.session_id;
  193. session = {
  194. session_id: message.session_id,
  195. session_name: message.session_name,
  196. session_type: message.session_type,
  197. /*session_business_type: message.session_business_type,*/
  198. messages: []
  199. };
  200. result.push(session);
  201. }
  202. session.messages.push({
  203. id: message.message_id,
  204. sender_id: message.sender_id,
  205. sender_name: message.sender_name,
  206. timestamp: ObjectUtil.timestampToLong(message.timestamp),
  207. content: message.content
  208. });
  209. });
  210. return result;
  211. }
  212. }
  213. module.exports = SearchRepo;