123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- 'use strict';
- let ImDb = require('../mysql/db/im.db');
- let async = require("async");
- let ObjectUtil = require("../../util/object.util.js");
- let vsprintf = require("sprintf-js").vsprintf;
- const DB_TABLES = require('../../include/commons').DB_TABLES;
- class SearchRepo {
- constructor() {
- }
- /**
- * 查询正常会话及议题已结束的会话(P2P, MUC)
- *
- * @param userId
- * @param handler
- */
- static findTopicEndedSessionIdList(userId, handler) {
- let sql = "SELECT s.id " +
- "FROM sessions s, topics t, participants p " +
- "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) " +
- " UNION " +
- "SELECT s.id " +
- "FROM sessions s, participants p " +
- "WHERE p.participant_id = ? AND p.session_id = s.id AND s.`type` IN (2, 3)";
- ImDb.execQuery({
- sql: sql,
- args: [userId, userId],
- handler: handler
- });
- }
- /**
- * 全部搜索
- *
- * @param sessionIdList
- * @param keyword
- * @param userTable
- * @param handler
- */
- static searchAll(sessionIdList, keyword, userTable, handler) {
- let data = {};
- async.waterfall([
- function (callback) {
- SearchRepo.searchUser(sessionIdList, keyword, userTable, 0, 3, function (err, res) {
- if (err) return handler(err, null);
- let buffer = SearchRepo.userForge(res);
- userTable == DB_TABLES.Doctors ? data.doctors = buffer : data.patients = buffer;
- callback(null);
- });
- },
- function (callback) {
- SearchRepo.searchSessions(sessionIdList, keyword, userTable, 0, 3, function (err, res) {
- if (err) return handler(err, null);
- data.sessions = SearchRepo.sessionForge(res);
- callback(null);
- })
- },
- function (callback) {
- SearchRepo.searchMessages(sessionIdList, keyword, 0, 3, function (err, res) {
- if (err) return handler(err, null);
- data.messages = SearchRepo.messageForge(res);
- handler(null, data);
- })
- }
- ]);
- }
- /**
- * 用户搜索
- *
- * @param sessionIdList
- * @param keyword
- * @param userTable
- * @param page
- * @param size
- * @param handler
- */
- static searchUser(sessionIdList, keyword, userTable, page, size, handler) {
- let sql = "SELECT DISTINCT u.id, u.name, u.sex, u.birthdate, u.avatar %s FROM sessions s, participants p, " + userTable +
- " u WHERE s.id in (?) AND s.id = p.session_id AND p.participant_id = u.id AND u.name like ? limit ?, ?";
- sql = vsprintf(sql, [userTable == DB_TABLES.Doctors ? ', hospital_name' : '']);
- keyword = '%' + keyword + '%';
- ImDb.execQuery({
- sql: sql,
- args: [sessionIdList, keyword, page * size, size],
- handler: handler
- });
- }
- /**
- * 会话搜索。搜索会话名称与会话成员的名称。若有一个符合,就选中。
- */
- static searchSessions(sessionIdList, keyword, userTable, page, size, handler) {
- if (sessionIdList.length == 0) {
- return handler(null, []);
- }
- let sql = "SELECT s.id, s.name, s.type, s.create_date, s.business_type, u.name participant_name " +
- "FROM sessions s, participants p, " + userTable + " u " +
- "WHERE s.id IN (?) AND s.id = p.session_id AND p.participant_id = u.id AND (s.name LIKE ? or u.name like ?) " +
- "GROUP by s.id LIMIT ?, ?";
- keyword = '%' + keyword + '%';
- ImDb.execQuery({
- sql: sql,
- args: [sessionIdList, keyword, keyword, page * size, size],
- handler: handler
- });
- }
- /**
- * 消息搜索
- */
- static searchMessages(sessionIdList, keyword, page, size, handler) {
- let sql = "SELECT * FROM(" +
- "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 " +
- "FROM sessions s, muc_messages m " +
- "WHERE s.id IN (?) AND s.id = m.session_id AND s.`type` = 1 AND m.content_type = 1 AND m.content LIKE ? " +
- " UNION " +
- "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 " +
- "FROM sessions s, p2p_messages m " +
- "WHERE s.id IN (?) AND s.id = m.session_id AND s.`type` = 2 AND m.content_type = 1 AND m.content LIKE ? " +
- " UNION " +
- "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 " +
- "FROM sessions s, group_messages m " +
- "WHERE s.id IN (?) AND s.id = m.session_id AND s.`type` = 3 AND m.content_type = 1 AND m.content LIKE ? ) X " +
- "ORDER BY X.session_id, X.message_id LIMIT ?, ?";
- keyword = '%' + keyword + '%';
- ImDb.execQuery({
- sql: sql,
- args: [sessionIdList, keyword, sessionIdList, keyword, sessionIdList, keyword, page * size, size],
- handler: handler
- });
- }
- static userForge(res) {
- res.forEach(function (user) {
- if (!user.avatar) user.avatar = "";
- user.birthdate = user.birthdate ? ObjectUtil.timestampToLong(user.birthdate) : "";
- });
- return res;
- }
- static sessionForge(res) {
- res.forEach(function (session) {
- session.create_date = ObjectUtil.timestampToLong(session.timestamp);
- });
- return res;
- }
- static messageForge(res) {
- let result = [];
- let lastSessionId = null;
- let session = null;
- res.forEach(function (message) {
- if (message.session_id !== lastSessionId) {
- lastSessionId = message.session_id;
- session = {
- session_id: message.session_id,
- session_name: message.session_name,
- session_type: message.session_type,
- session_business_type: message.session_business_type,
- messages: []
- };
- result.push(session);
- }
- session.messages.push({
- id: message.message_id,
- sender_id: message.sender_id,
- sender_name: message.sender_name,
- timestamp: ObjectUtil.timestampToLong(message.timestamp),
- content: message.content
- });
- });
- return result;
- }
- }
- module.exports = SearchRepo;
|