Browse Source

搜索会话时增加用户信息;搜索消息时返回所有相关的消息

Sand 8 years ago
parent
commit
e86c46c002

+ 7 - 1
src/server/models/search/object.searcher.js

@ -58,6 +58,12 @@ class ObjectSearcher extends RedisModel {
                        SearchRepo.searchUser(sessionIdList, keyword, userTable, page, size, function (err, res) {
                        SearchRepo.searchUser(sessionIdList, keyword, userTable, page, size, function (err, res) {
                            if (err) return callback(err, null);
                            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) {
                            if (userTable == DB_TABLES.Patients) {
                                callback(null, {patients: res});
                                callback(null, {patients: res});
                            } else {
                            } else {
@ -65,7 +71,7 @@ class ObjectSearcher extends RedisModel {
                            }
                            }
                        });
                        });
                    } else if (entityType === 'session') {
                    } else if (entityType === 'session') {
                        SearchRepo.searchSessions(sessionIdList, keyword, page, size, function (err, res) {
                        SearchRepo.searchSessions(sessionIdList, keyword, userTable, page, size, function (err, res) {
                            if (err) return callback(err, null);
                            if (err) return callback(err, null);
                            callback(null, {sessions: res});
                            callback(null, {sessions: res});

+ 0 - 1
src/server/package.json

@ -26,7 +26,6 @@
    "request": "^2.79.0",
    "request": "^2.79.0",
    "serve-favicon": "~2.2.1",
    "serve-favicon": "~2.2.1",
    "socket.io": "~1.5.1",
    "socket.io": "~1.5.1",
    "solr-client": "^0.6.0",
    "sprintf-js": "^1.0.3",
    "sprintf-js": "^1.0.3",
    "underscore": "~1.8.3"
    "underscore": "~1.8.3"
  }
  }

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

@ -3,6 +3,7 @@
let ImDb = require('../mysql/db/im.db');
let ImDb = require('../mysql/db/im.db');
let async = require("async");
let async = require("async");
let ObjectUtil = require("../../util/object.util.js");
let ObjectUtil = require("../../util/object.util.js");
let vsprintf = require("sprintf-js").vsprintf;
const DB_TABLES = require('../../include/commons').DB_TABLES;
const DB_TABLES = require('../../include/commons').DB_TABLES;
@ -30,7 +31,6 @@ class SearchRepo {
            args: [userId, userId],
            args: [userId, userId],
            handler: handler
            handler: handler
        });
        });
    }
    }
    /**
    /**
@ -42,39 +42,33 @@ class SearchRepo {
     * @param handler
     * @param handler
     */
     */
    static searchAll(sessionIdList, keyword, userTable, handler) {
    static searchAll(sessionIdList, keyword, userTable, handler) {
        let data = {};
        async.waterfall([
        async.waterfall([
            function (callback) {
            function (callback) {
                SearchRepo.searchUser(sessionIdList, keyword, userTable, 0, 3, function (err, res) {
                SearchRepo.searchUser(sessionIdList, keyword, userTable, 0, 3, function (err, res) {
                    if (err) return handler(err, null);
                    if (err) return handler(err, null);
                    let data = {};
                    if (userTable == DB_TABLES.Doctors) {
                        data.doctors = res;
                    } else {
                        data.patients = res;
                    }
                    let buffer = SearchRepo.userForge(res);
                    userTable == DB_TABLES.Doctors ? data.doctors = buffer : data.patients = buffer;
                    callback(null, data);
                    callback(null);
                });
                });
            },
            },
            function (data, callback) {
                SearchRepo.searchSessions(sessionIdList, keyword, 0, 3, function (err, res) {
            function (callback) {
                SearchRepo.searchSessions(sessionIdList, keyword, userTable, 0, 3, function (err, res) {
                    if (err) return handler(err, null);
                    if (err) return handler(err, null);
                    data.sessions = res;
                    data.sessions = SearchRepo.sessionForge(res);
                    callback(null, data);
                    callback(null);
                })
                })
            },
            },
            function (data, callback) {
            function (callback) {
                SearchRepo.searchMessages(sessionIdList, keyword, 0, 3, function (err, res) {
                SearchRepo.searchMessages(sessionIdList, keyword, 0, 3, function (err, res) {
                    if (err) return handler(err, null);
                    if (err) return handler(err, null);
                    res.forEach(function (message) {
                        message.timestamp = ObjectUtil.timestampToLong(message.timestamp);
                    });
                    data.messages = res;
                    data.messages = SearchRepo.messageForge(res);
                    handler(null, data);
                    handler(null, data);
                })
                })
@ -93,9 +87,11 @@ class SearchRepo {
     * @param handler
     * @param handler
     */
     */
    static searchUser(sessionIdList, keyword, userTable, page, size, 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 +
        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 ?, ?";
            " 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 + '%';
        keyword = '%' + keyword + '%';
        ImDb.execQuery({
        ImDb.execQuery({
            sql: sql,
            sql: sql,
@ -105,19 +101,22 @@ class SearchRepo {
    }
    }
    /**
    /**
     * 会话搜索
     * 会话搜索。搜索会话名称与会话成员的名称。若有一个符合,就选中。
     */
     */
    static searchSessions(sessionIdList, keyword, page, size, handler) {
        if(sessionIdList.length == 0){
    static searchSessions(sessionIdList, keyword, userTable, page, size, handler) {
        if (sessionIdList.length == 0) {
            return handler(null, []);
            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 ?, ? ";
        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 + '%';
        keyword = '%' + keyword + '%';
        ImDb.execQuery({
        ImDb.execQuery({
            sql: sql,
            sql: sql,
            args: [sessionIdList, keyword, page * size, size],
            args: [sessionIdList, keyword, keyword, page * size, size],
            handler: handler
            handler: handler
        });
        });
    }
    }
@ -129,16 +128,16 @@ class SearchRepo {
        let sql = "SELECT * FROM(" +
        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 " +
            "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 " +
            "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" +
            "WHERE s.id IN (?) AND s.id = m.session_id AND s.`type` = 1 AND m.content_type = 1 AND m.content LIKE ? " +
            " UNION " +
            " 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 " +
            "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 " +
            "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 ? GROUP BY s.id" +
            "WHERE s.id IN (?) AND s.id = m.session_id AND s.`type` = 2 AND m.content_type = 1 AND m.content LIKE ? " +
            " UNION " +
            " 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 " +
            "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 " +
            "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";
            "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 + '%';
        keyword = '%' + keyword + '%';
        ImDb.execQuery({
        ImDb.execQuery({
@ -147,6 +146,55 @@ class SearchRepo {
            handler: handler
            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;
module.exports = SearchRepo;

+ 1 - 1
src/server/resources/schema/ichat_1.2.8_table_schema.sql

@ -181,7 +181,7 @@ CREATE TABLE `sessions`
	`last_sender_id` VARCHAR(50) COMMENT '消息最后发送人ID',
	`last_sender_id` VARCHAR(50) COMMENT '消息最后发送人ID',
	`last_sender_name` VARCHAR(50) COMMENT '消息最后发送人姓名',
	`last_sender_name` VARCHAR(50) COMMENT '消息最后发送人姓名',
	`last_content_type` VARCHAR(50) COMMENT '消息最后内容类型',
	`last_content_type` VARCHAR(50) COMMENT '消息最后内容类型',
	`last_content` VARCHAR(1024) COMMENT '消息最后内容',
	`last_content` VARCHAR(5120) COMMENT '消息最后内容',
	`last_message_time` TIMESTAMP(0) COMMENT '消息最后时间',
	`last_message_time` TIMESTAMP(0) COMMENT '消息最后时间',
	CONSTRAINT `PK_sessions` PRIMARY KEY (`id`)
	CONSTRAINT `PK_sessions` PRIMARY KEY (`id`)
) COMMENT='会话'
) COMMENT='会话'

+ 1 - 1
src/server/resources/schema/ichat_1.2.8_view_schema.sql

@ -1,6 +1,6 @@
/* 用户视图:医生、患者、用户微信状态*/
/* 用户视图:医生、患者、用户微信状态*/
create or replace view doctors as 
create or replace view doctors as 
select code id, name, sex, birthday birthdate, photo avatar, level
select code id, name, sex, birthday birthdate, photo avatar, hospital_name, level
from wlyy.wlyy_doctor;
from wlyy.wlyy_doctor;
create or replace view patients as 
create or replace view patients as 

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

@ -13,7 +13,7 @@ describe('API: Doctor Search', function () {
            function (data) {
            function (data) {
                assert(data.messages.length > 0, "Search must return at least one data");
                assert(data.messages.length > 0, "Search must return at least one data");
                console.log(data);
                console.log(JSON.stringify(data));
                done();
                done();
            },
            },