Browse Source

会话搜索结果对成员单独分类

Sand 8 years ago
parent
commit
d6d1b1260b

+ 1 - 1
src/server/app.js

@ -104,7 +104,7 @@ let socketHandler = new SocketHandler(io);
socketHandler.start();
if(!server.address()){
    log.error('Starting IM server failed, port is already in use.');
    log.error('Starting IM server failed, port is already in used.');
    process.exit(1);
} else {

+ 5 - 20
src/server/models/search/object.searcher.js

@ -7,7 +7,6 @@ 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;
@ -40,7 +39,6 @@ class ObjectSearcher extends RedisModel {
                            });
                    }
                },
                // 搜索
                function (sessionIdList, keyword, targetRole, entityType, callback) {
                    if (targetRole !== 'doctor' && targetRole !== 'patient') {
                        ModelUtil.emitError(self.eventEmitter, "Unknown target role: " + targetRole);
@ -48,6 +46,7 @@ class ObjectSearcher extends RedisModel {
                    let userTable = targetRole === 'doctor' ? DB_TABLES.Doctors : DB_TABLES.Patients;
                    // 综合搜索与分类搜索
                    if (entityType === 'all') {
                        SearchRepo.searchAll(sessionIdList, keyword, userTable, function (err, res) {
                            if (err) return callback(err, null);
@ -58,33 +57,19 @@ class ObjectSearcher extends RedisModel {
                        SearchRepo.searchUser(sessionIdList, keyword, userTable, page, size, function (err, res) {
                            if (err) return callback(err, null);
                            res.forEach(function (user) {
                                if(!user.avatar) user.avatar = "";
                                user.birthdate = user.birthdate ? ObjectUtil.timestampToLong(user.birthdate) : "";
                            });
                            if (userTable == DB_TABLES.Patients) {
                                callback(null, {patients: res});
                            } else {
                                callback(null, {doctors: res});
                            }
                            callback(null, {users: SearchRepo.userForge(res)});
                        });
                    } else if (entityType === 'session') {
                        SearchRepo.searchSessions(sessionIdList, keyword, userTable, page, size, function (err, res) {
                        SearchRepo.searchSessions(sessionIdList, keyword, page, size, function (err, res) {
                            if (err) return callback(err, null);
                            callback(null, {sessions: res});
                            callback(null, {sessions: SearchRepo.sessionForge(res, keyword)});
                        });
                    } else if (entityType === 'message') {
                        SearchRepo.searchMessages(sessionIdList, keyword, page, size, function (err, res) {
                            if (err) return callback(err, null);
                            res.forEach(function (message) {
                                message.timestamp = ObjectUtil.timestampToLong(message.timestamp);
                            });
                            callback(null, {messages: res});
                            callback(null, {messages: SearchRepo.messageForge(res)});
                        });
                    } else {
                        callback(new Error("Unknown entity type: " + entityType), null);

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

@ -50,16 +50,16 @@ class SearchRepo {
                    if (err) return handler(err, null);
                    let buffer = SearchRepo.userForge(res);
                    userTable == DB_TABLES.Doctors ? data.doctors = buffer : data.patients = buffer;
                    data.users = buffer;
                    callback(null);
                });
            },
            function (callback) {
                SearchRepo.searchSessions(sessionIdList, keyword, userTable, 0, 3, function (err, res) {
                SearchRepo.searchSessions(sessionIdList, keyword, 0, 3, function (err, res) {
                    if (err) return handler(err, null);
                    data.sessions = SearchRepo.sessionForge(res);
                    data.sessions = SearchRepo.sessionForge(res, keyword);
                    callback(null);
                })
@ -103,20 +103,25 @@ class SearchRepo {
    /**
     * 会话搜索。搜索会话名称与会话成员的名称。若有一个符合,就选中。
     */
    static searchSessions(sessionIdList, keyword, userTable, page, size, handler) {
    static searchSessions(sessionIdList, keyword, 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 " +
        let sql = "SELECT * FROM(" +
            "SELECT s.id, s.name, s.type, s.create_date, s.business_type, u.name participant_name " +
            "FROM sessions s, participants p, doctors 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 ?) " +
            " UNION " +
            "SELECT s.id, s.name, s.type, s.create_date, s.business_type, u.name participant_name " +
            "FROM sessions s, participants p, patients 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 ?, ?";
            ") X LIMIT ?, ?";
        keyword = '%' + keyword + '%';
        ImDb.execQuery({
            sql: sql,
            args: [sessionIdList, keyword, keyword, page * size, size],
            args: [sessionIdList, keyword, keyword, sessionIdList, keyword, keyword, page * size, size],
            handler: handler
        });
    }
@ -157,12 +162,30 @@ class SearchRepo {
        return res;
    }
    static sessionForge(res) {
    static sessionForge(res, keyword) {
        let result = [];
        let lastSessionId = null;
        let tempSession = null;
        res.forEach(function (session) {
            session.create_date = ObjectUtil.timestampToLong(session.timestamp);
            if (session.session_id !== lastSessionId) {
                lastSessionId = session.session_id;
                tempSession = {
                    id: session.id,
                    name: session.name,
                    type: session.type,
                    business_type: session.business_type,
                    /*create_date: ObjectUtil.timestampToLong(session.create_date),*/
                    participants: []
                };
                result.push(tempSession);
            }
            if(session.participant_name.indexOf(keyword) >= 0) tempSession.participants.push({name: session.participant_name});
        });
        return res;
        return result;
    }
    static messageForge(res) {
@ -177,7 +200,7 @@ class SearchRepo {
                    session_id: message.session_id,
                    session_name: message.session_name,
                    session_type: message.session_type,
                    session_business_type: message.session_business_type,
                    /*session_business_type: message.session_business_type,*/
                    messages: []
                };

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

@ -29,9 +29,9 @@ describe('API: Doctor Search', function () {
        it('should return 200', function (done) {
            imClient.Sessions.Search.Doctor.searchDoctors(userId, '张', 1, 10,
                function (data) {
                    assert(data.doctors.length > 0, "Search must return at least one data");
                    assert(data.users.length > 0, "Search must return at least one data");
                    console.log(data);
                    console.log(JSON.stringify(data));
                    done();
                },
@ -49,7 +49,7 @@ describe('API: Doctor Search', function () {
                function (data) {
                    assert(data.sessions.length > 0, "Search must return at least one data");
                    console.log(data);
                    console.log(JSON.stringify(data));
                    done();
                },
@ -67,7 +67,7 @@ describe('API: Doctor Search', function () {
                function (data) {
                    assert(data.messages.length > 0, "Search must return at least one data");
                    console.log(data);
                    console.log(JSON.stringify(data));
                    done();
                },

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

@ -29,7 +29,7 @@ describe('API: Patient Search', function () {
        it('return a patient', function (done) {
            imClient.Sessions.Search.Patient.searchPatient(userId, '丽', 1, 10,
                function (data) {
                    assert(data.patients.length > 0, "Search must return at least one data");
                    assert(data.users.length > 0, "Search must return at least one data");
                    console.log(data);