Sand 8 年 前
コミット
70079cd5d5

+ 9 - 9
src/client/im.client.js

@ -404,9 +404,9 @@ var imClient = {
        Search: {
            Doctor: {
                // 搜索所有对象:医生,会话及消息
                searchAll: function (userId, keyword, success, failure) {
                searchAll: function (userId, keyword, excludeTopicEndedSessions, success, failure) {
                    httpClient.get(ENDPOINTS.Search.Search,
                        {user_id: userId, target_role: 'doctor', entity_type: 'all', keyword: keyword, exclude_topic_ended_sessions: false},
                        {user_id: userId, target_role: 'doctor', entity_type: 'all', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions},
                        success,
                        failure);
                },
@ -420,9 +420,9 @@ var imClient = {
                },
                // 搜索会话
                searchSessions: function (userId, keyword, page, size, success, failure) {
                searchSessions: function (userId, keyword, excludeTopicEndedSessions, page, size, success, failure) {
                    httpClient.get(ENDPOINTS.Search.Search,
                        {user_id: userId, target_role: 'doctor', entity_type: 'session', keyword: keyword, exclude_topic_ended_sessions: false, page: page, size: size},
                        {user_id: userId, target_role: 'doctor', entity_type: 'session', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size},
                        success,
                        failure);
                },
@ -438,15 +438,15 @@ var imClient = {
            Patient: {
                // 搜索所有对象:患者,会话及消息
                searchAll: function (userId, keyword, success, failure) {
                searchAll: function (userId, keyword, excludeTopicEndedSessions, success, failure) {
                    httpClient.get(ENDPOINTS.Search.Search,
                        {user_id: userId, target_role: 'patient', entity_type: 'all', keyword: keyword, exclude_topic_ended_sessions: false},
                        {user_id: userId, target_role: 'patient', entity_type: 'all', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions},
                        success,
                        failure);
                },
                // 搜索患者
                searchDoctors: function (userId, keyword, page, size, success, failure) {
                searchPatient: function (userId, keyword, page, size, success, failure) {
                    httpClient.get(ENDPOINTS.Search.Search,
                        {user_id: userId, target_role: 'patient', entity_type: 'user', keyword: keyword, exclude_topic_ended_sessions: false, page: page, size: size},
                        success,
@ -454,9 +454,9 @@ var imClient = {
                },
                // 搜索会话
                searchSessions: function (userId, keyword, page, size, success, failure) {
                searchSessions: function (userId, keyword, excludeTopicEndedSessions, page, size, success, failure) {
                    httpClient.get(ENDPOINTS.Search.Search,
                        {user_id: userId, target_role: 'patient', entity_type: 'session', keyword: keyword, exclude_topic_ended_sessions: false, page: page, size: size},
                        {user_id: userId, target_role: 'patient', entity_type: 'session', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size},
                        success,
                        failure);
                },

+ 13 - 4
src/server/models/search/object.searcher.js

@ -7,6 +7,7 @@ let SearchRepo = require('../../repository/mysql/search.repo');
let log = require('../../util/log.js');
let async = require("async");
let ObjectUtil = require("../../util/object.util.js");
let redis = RedisClient.redisClient().connection;
const REDIS_KEYS = require('../../include/commons').REDIS_KEYS;
@ -27,7 +28,7 @@ class ObjectSearcher extends RedisModel {
                        SearchRepo.findTopicEndedSessionIdList(userId, function (err, sessionIdList) {
                            if (err) return callback(err, null);
                            callback(null, sessionIdList);
                            callback(null, sessionIdList, keyword, targetRole, entityType);
                        })
                    } else {
                        redis.zrangeAsync(RedisModel.makeRedisKey(REDIS_KEYS.UserSessions, userId), 0, -1)
@ -57,19 +58,27 @@ class ObjectSearcher extends RedisModel {
                        SearchRepo.searchUser(sessionIdList, keyword, userTable, page, size, function (err, res) {
                            if (err) return callback(err, null);
                            callback(null, res);
                            if (userTable == DB_TABLES.Patients) {
                                callback(null, {patients: res});
                            } else {
                                callback(null, {doctors: res});
                            }
                        });
                    } else if (entityType === 'session') {
                        SearchRepo.searchSessions(sessionIdList, keyword, page, size, function (err, res) {
                            if (err) return callback(err, null);
                            callback(null, res);
                            callback(null, {sessions: res});
                        });
                    } else if (entityType === 'message') {
                        SearchRepo.searchMessages(sessionIdList, keyword, page, size, function (err, res) {
                            if (err) return callback(err, null);
                            callback(null, res);
                            res.forEach(function (message) {
                                message.timestamp = ObjectUtil.timestampToLong(message.timestamp);
                            });
                            callback(null, {messages: res});
                        });
                    } else {
                        callback(new Error("Unknown entity type: " + entityType), null);

+ 29 - 12
src/server/repository/mysql/search.repo.js

@ -2,6 +2,7 @@
let ImDb = require('../mysql/db/im.db');
let async = require("async");
var ObjectUtil = require("../../util/object.util.js");
const DB_TABLES = require('../../include/commons').DB_TABLES;
@ -18,11 +19,12 @@ class SearchRepo {
    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 " +
            "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)";
            "WHERE p.participant_id = ? AND p.session_id = s.id AND s.`type` IN (2, 3)";
        ImDb.execQuery({
            sql: sql,
            args: [userId, userId],
@ -68,6 +70,10 @@ class SearchRepo {
                SearchRepo.searchMessages(sessionIdList, keyword, 0, 3, function (err, res) {
                    if (err) return handler(err, null);
                    res.forEach(function (message) {
                        message.timestamp = ObjectUtil.timestampToLong(message.timestamp);
                    });
                    data.messages = res;
                    handler(null, data);
@ -87,8 +93,8 @@ class SearchRepo {
     * @param handler
     */
    static searchUser(sessionIdList, keyword, userTable, page, size, handler) {
        let sql = "select u.id, u.name, u.sex, u.avatar 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 ?, ?";
        let sql = "SELECT u.id, u.name, u.sex, u.avatar 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 ?, ?";
        keyword = '%' + keyword + '%';
        ImDb.execQuery({
@ -102,7 +108,11 @@ class SearchRepo {
     * 会话搜索
     */
    static searchSessions(sessionIdList, keyword, page, size, handler) {
        let sql = "select s.id, s.name, s.type, s.create_date, s.business_type from sessions s where s.id in (?) and s.name like ? limit ?, ? ";
        if(sessionIdList.length == 0){
            return handler(null, []);
        }
        let sql = "SELECT s.id, s.name, s.type, s.create_date, s.business_type FROM sessions s WHERE s.id in (?) AND s.name LIKE ? LIMIT ?, ? ";
        keyword = '%' + keyword + '%';
        ImDb.execQuery({
@ -116,17 +126,24 @@ class SearchRepo {
     * 消息搜索
     */
    static searchMessages(sessionIdList, keyword, page, size, handler) {
        let sql = "SELECT s.id, s.name, s.type, s.create_date, s.business_type, m.sender_name, m.content " +
        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 ? GROUP BY s.id" +
            " 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` IN (1,2,3) AND m.content_type = 1 AND m.content LIKE ? " +
            "GROUP BY s.id " +
            "ORDER BY s.last_message_time " +
            "LIMIT ?, ?";
            "WHERE s.id IN (?) AND s.id = m.session_id AND s.`type` = 2 AND m.content_type = 1 AND m.content LIKE ? GROUP BY s.id" +
            " 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 ? GROUP BY s.id) X " +
            "ORDER BY X.timestamp";
        keyword = '%' + keyword + '%';
        ImDb.execQuery({
            sql: sql,
            args: [sessionIdList, keyword, page * size, size],
            args: [sessionIdList, keyword, sessionIdList, keyword, sessionIdList, keyword, page * size, size],
            handler: handler
        });
    }

+ 5 - 5
test/client/im.client.doctor.session.search.Test.js

@ -9,7 +9,7 @@ describe('API: Doctor Search', function () {
    // 搜索所有
    describe('search all object types', function () {
        it('should return 200', function (done) {
            imClient.Sessions.Search.Doctor.searchAll(userId, '哦',
            imClient.Sessions.Search.Doctor.searchAll(userId, '哦', false,
            function (data) {
                assert(data.messages.length > 0, "Search must return at least one data");
@ -29,7 +29,7 @@ describe('API: Doctor Search', function () {
        it('should return 200', function (done) {
            imClient.Sessions.Search.Doctor.searchDoctors(userId, '张', 1, 10,
                function (data) {
                    assert(data.length > 0, "Search must return at least one data");
                    assert(data.doctors.length > 0, "Search must return at least one data");
                    console.log(data);
@ -45,9 +45,9 @@ describe('API: Doctor Search', function () {
    // 搜索会话
    describe('search sessions', function () {
        it('should return 200', function (done) {
            imClient.Sessions.Search.Doctor.searchSessions('D2016008240003', '丽', 1, 10,
            imClient.Sessions.Search.Doctor.searchSessions('D2016008240003', '丽', false, 1, 10,
                function (data) {
                    assert(data.length > 0, "Search must return at least one data");
                    assert(data.sessions.length > 0, "Search must return at least one data");
                    console.log(data);
@ -65,7 +65,7 @@ describe('API: Doctor Search', function () {
        it('should return 200', function (done) {
            imClient.Sessions.Search.Doctor.searchMessages(userId, '哦', 1, 10,
                function (data) {
                    assert(data.length > 0, "Search must return at least one data");
                    assert(data.messages.length > 0, "Search must return at least one data");
                    console.log(data);

+ 14 - 13
test/client/im.client.patient.session.search.Test.js

@ -3,15 +3,15 @@
let assert = require('assert');
let imClient = require('../../src/client/im.client');
let userId = 'D2016008240002';
let userId = 'D2016008240003';
describe('API: Patient Search', function () {
    // 搜索所有
    describe('search all object types', function () {
        it('should return 200', function (done) {
            imClient.Sessions.Search.Patient.searchAll(userId, '哦',
        it('return a message', function (done) {
            imClient.Sessions.Search.Patient.searchAll(userId, '哦', false,
            function (data) {
                assert(data.length > 0, "Search must return at least one data");
                assert(data.messages.length > 0, "Search must return at least one data");
                console.log(data);
@ -25,11 +25,11 @@ describe('API: Patient Search', function () {
    });
    // 搜索患者
    describe('search doctors', function () {
        it('should return 200', function (done) {
            imClient.Sessions.Search.Patient.searchPatients(userId, '张', 1, 10,
    describe('search patients', function () {
        it('return a patient', function (done) {
            imClient.Sessions.Search.Patient.searchPatient(userId, '丽', 1, 10,
                function (data) {
                    assert(data.length > 0, "Search must return at least one data");
                    assert(data.patients.length > 0, "Search must return at least one data");
                    console.log(data);
@ -37,6 +37,7 @@ describe('API: Patient Search', function () {
                },
                function (xhr, status, error) {
                    assert.ok(false, xhr.responseJSON.message);
                    done();
                });
        });
@ -44,10 +45,10 @@ describe('API: Patient Search', function () {
    // 搜索会话
    describe('search sessions', function () {
        it('should return 200', function (done) {
            imClient.Sessions.Search.Patient.searchSessions('D2016008240003', '1', 1, 10,
        it('return a session', function (done) {
            imClient.Sessions.Search.Patient.searchSessions(userId, '廖小', false, 1, 10,
                function (data) {
                    assert(data.length > 0, "Search must return at least one data");
                    assert(data.sessions.length > 0, "Search must return at least one data");
                    console.log(data);
@ -62,10 +63,10 @@ describe('API: Patient Search', function () {
    // 搜索会话消息
    describe('search messages', function () {
        it('should return 200', function (done) {
        it('return at least one message', function (done) {
            imClient.Sessions.Search.Patient.searchMessages(userId, '哦', 1, 10,
                function (data) {
                    assert(data.length > 0, "Search must return at least one data");
                    assert(data.messages.length > 0, "Search must return at least one data");
                    console.log(data);