123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- '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,2,8) " +
- " 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
- });
- }
- /**
- * 查询正常会话及议题已结束的会话(P2P, MUC)
- *
- * @param userId
- * @param handler
- */
- static findTopicActiveSessionIdList(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 NULL AND s.`type` IN (1,2,8) " +
- " 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) and s.id not in(" +
- " select DISTINCT p1.session_id from participants p1 ,topics t where p1.participant_id = ? and t.session_id = p1.session_id " +
- ") ";
- ImDb.execQuery({
- sql: sql,
- args: [userId, userId,userId],
- handler: handler
- });
- }
- /**
- * 全部搜索
- *
- * @param sessionIdList
- * @param keyword
- * @param userTable
- * @param handler
- */
- static searchAll(sessionIdList,userId, keyword, userTable, handler) {
- let data = {};
- async.waterfall([
- function (callback) {
- SearchRepo.searchUser(sessionIdList,userId, keyword, userTable, 0, 4, function (err, res) {
- if (err) return handler(err, null);
- let buffer = SearchRepo.userForge(res);
- data.users = buffer;
- callback(null);
- });
- },
- function (callback) {
- SearchRepo.searchSessions(sessionIdList,userId, keyword, 0, 4, function (err, res) {
- if (err) return handler(err, null);
- data.sessions = SearchRepo.sessionForge(res, keyword);
- callback(null);
- })
- },
- function (callback) {
- SearchRepo.searchMessages(sessionIdList,userId, keyword, 0, 4, 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,userId, keyword, userTable, page, size, handler) {
- 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 " +
- " FROM participants p, " + userTable +
- " 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 ?) ";
- if (userTable === DB_TABLES.Doctors) {
- sql += " AND s.type = 2 and s.business_type = 1 ";
- }else{
- sql += " AND s.type in (1,2,8) and s.business_type = 2 ";
- }
- sql += " limit ?, ? ";
- sql = vsprintf(sql, [userTable == DB_TABLES.Doctors ? ', hospital_name' : '']);
- keyword = '%' + keyword + '%';
- ImDb.execQuery({
- sql: sql,
- args: [sessionIdList,userId, keyword,keyword, page * size, size],
- handler: handler
- });
- }
- /**
- * 会话搜索。搜索会话名称与会话成员的名称。若有一个符合,就选中。
- */
- static searchSessions(sessionIdList,userId, keyword, page, size, handler) {
- if (sessionIdList.length == 0) {
- return handler(null, []);
- }
- let sql = "SELECT * FROM(" +
- "SELECT s.id, s.name, s.type, s.create_date, s.business_type,GROUP_CONCAT(u. NAME) as participant_name " +
- "FROM sessions s, doctors u ,participants p " +
- "WHERE s.id IN (?) and s.type in (3,5) 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 " +
- ") X LIMIT ?, ?";
- keyword = '%' + keyword + '%';
- ImDb.execQuery({
- sql: sql,
- args: [sessionIdList,userId, keyword,keyword, page * size, size],
- handler: handler
- });
- }
- /**
- * 消息搜索
- */
- static searchMessages(sessionIdList,userId, 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` in (3,5) 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 ? user.birthdate.getTime():"";
- });
- return res;
- }
- static sessionForge(res, keyword) {
- let result = [];
- let lastSessionId = null;
- let tempSession = null;
- res.forEach(function (session) {
- if (session.id !== lastSessionId) {
- lastSessionId = session.id;
- tempSession = {
- id: session.id,
- name: session.name,
- type: session.type,
- business_type: session.business_type,
- /*create_date: ObjectUtil.timestampToLong(session.create_date),*/
- members: []
- };
- result.push(tempSession);
- }
- if (session.participant_name.indexOf(keyword) >= 0) tempSession.members.push({name: session.participant_name});
- });
- return result;
- }
- 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;
|