Browse Source

Merge branch 'feature-refactor' of http://192.168.1.220:10080/Amoy/im.doctor into feature-refactor

sand 8 years ago
parent
commit
ff2609b95c

+ 4 - 83
src/server/models/messages/messages.js

@ -11,6 +11,7 @@ let modelUtil = require('../../util/model.util');
var imDb = require('../../repository/mysql/db/im.db');
let log = require('../../util/log.js');
let Sessions = require('../sessions/sessions');
let MessageRepo = require('../../repository/mysql/message.repo');
let configFile = require('../../include/commons').CONFIG_FILE;
let config = require('../../resources/config/' + configFile);
const RedisKey = require('../../include/commons').REDIS_KEYS;
@ -46,40 +47,7 @@ class Messages extends RedisModel {
     * @param sessionId
     */
    getMessagesBySession(sessionId,handler){
       let session = new  Sessions();
       session.getSessions(sessionId,function(err,res){
            if(err){
                return;
            }else{
                if(res.length==0){
                    log.warn("session is not found!");
                    return;
                }
                let type = res[0].type;
                let db ="";
                if(type==1){
                    db = IMTABLE.MUCMSG;
                }else if(type==2){
                    db = IMTABLE.P2PMSG;
                }else{
                    db = IMTABLE.GROUPMSG;
                }
                let sql = "select * from "+db+" w where w.session_id = ? limit 0,"+config.sessionConfig.maxMessageCount;
                imDb.execQuery({
                    "sql": sessionsql,
                    "args": [sessionId],
                    "handler": function (err, res) {
                        if(err) {
                            log.error("sql:"+sql+"data:sessionId:"+sessionId);
                        }else{
                            log.info("getMessagesBySession success by sessionId :"+sessionId);
                        }
                        handler(err,res);
                    }
                })
            }
       })
        MessageRepo.getMessagesBySession(sessionId,handler);
    }
    /**
@ -89,42 +57,7 @@ class Messages extends RedisModel {
     * @param pagesize
     */
    getMessageByPage(sessionId,page,pagesize,handler){
        if(page>0){
            page =page*pagesize;
        }
        let session = new  Sessions();
        session.getSessions(sessionId,function(err,res){
            if(err){
                return;
            }else{
                if(res.length==0){
                    log.warn("session is not found!");
                    return;
                }
                let type = res[0].type;
                let db ="";
                if(type==1){
                    db = IMTABLE.MUCMSG;
                }else if(type==2){
                    db = IMTABLE.P2PMSG;
                }else{
                    db = IMTABLE.GROUPMSG;
                }
                let sql = "select * from "+db+" w where w.session_id = ? limit ?,?";
                imDb.execQuery({
                    "sql": sessionsql,
                    "args": [sessionId,page,pagesize],
                    "handler": function (err, res) {
                        if(err) {
                            log.error("sql:"+sql+"data:sessionId:"+sessionId);
                        }else{
                            log.info("getMessagesBySession success by sessionId :"+sessionId);
                        }
                        handler(err,res);
                    }
                })
            }
        })
        MessageRepo.getMessageByPage(sessionId,page,pagesize,handler);
    }
@ -154,19 +87,7 @@ class Messages extends RedisModel {
     * @type type 会话类型,1表示MUC会话,2表示P2P,3表示群会话,4表示临时讨论组
     */
    saveMessageForMysql(messages,type,messageid,sessionId){
        var sql = "INSERT INTO "+(type==1?IMTABLE.MUCMSG:type==2?IMTABLE.P2PMSG:IMTABLE.GROUPMSG)+" (id, session_id, sender_id, sender_name,content_type, content, timestamp) VALUES (?,?,?,?,?,?,?) ";
        imDb.execQuery({
            "sql": sql,
            "args": [messageid,sessionId,messages.senderId,messages.senderName,messages.contentType,messages.content,messages.timestamp],
            "handler": function (err, res) {
                if(err) {
                    log.error("sql:"+sql+",error:"+err+",data:"+JSON.stringify(messages)+",messageid:"+messageid+",sessionId:"+sessionId);
                }else{
                    log.info("save message to mysql is success by session :"+sessionId);
                }
            }
        });
        MessageRepo.saveMessageForMysql(messages,type,messageid,sessionId);
    }
}

+ 5 - 60
src/server/models/sessions/participants.js

@ -8,10 +8,9 @@ let redisClient = RedisClient.redisClient();
let redis = redisClient.connection;
let RedisModel = require('./../redis.model.js');
let modelUtil = require('../../util/model.util');
let imDb = require('../../repository/mysql/db/im.db');
let ParticipantRepo = require('../../repository/mysql/participant.repo');
let log = require('../../util/log.js');
const RedisKey = require('../../include/commons').REDIS_KEYS;
const IMTABLE = require('../../include/commons').IM_DB;
class Participants extends RedisModel {
    constructor() {
@ -40,17 +39,7 @@ class Participants extends RedisModel {
                    handler(res);
                })
            }else{
                let sql ="select w.* from participants w where w.session_id =? ";
                imDb.execQuery({
                    "sql": sql,
                    "args": [sessionId],
                    "handler": function (err, res) {
                        if(err) {
                            log.error("getParticipantsBySessionId is fail error: "+err);
                        }
                        handler(res);
                    }
                });
                ParticipantRepo.getParticipantsBySessionId(sessionId,handler);
            }
        })
    }
@ -76,17 +65,7 @@ class Participants extends RedisModel {
                  handler(exists);
              })
          }else{//不存在从数据库中获取
              let sql ="select count(1) as count from participants w where w.session_id =? and w.participaint_id = ? ";
              imDb.execQuery({
                  "sql": sql,
                  "args": [sessionId,userId],
                  "handler": function (err, res) {
                      if(err) {
                          log.error("existsUser is fail error: "+err);
                      }
                      handler(res[0].count);
                  }
              });
              ParticipantRepo.existsUser(sessionId,userId,handler);
          }
      })
    }
@ -98,20 +77,7 @@ class Participants extends RedisModel {
     * @param doctor
     */
    getSessionIdByParticipants(patient,doctor,handler){
        let sql ="select session_id from "+IMTABLE.PARTICIPANTS+" p1 ,participants p2 " +
            "where p1.session_id = p2.session_id and " +
            "((p1.participaint_id = ? and p2.participaint_id = ?) or (p1.participaint_id = ? and p2.participaint_id = ?))"
        imDb.execQuery({
            "sql": sql,
            "args": [patient,doctor,doctor,patient],
            "handler": function (err, res) {
                if(err) {
                    log.error("getSessionIdByParticipants is fail error: "+err);
                }
                handler(err,res);
            }
        });
        ParticipantRepo.getSessionIdByParticipants(patient,doctor,handler);
    }
    /**
@ -140,28 +106,7 @@ class Participants extends RedisModel {
     * @param users
     */
    createParticipantsToMysql(session_id,users){
        let sql="insert into "+IMTABLE.PARTICIPANTS +" (session_id,participaint_id,participaint_role,receiving) VALUES "
        let args=[];
        for(var j in users){
            sql+="(?,?,?,?),";
            args.push(session_id);
            args.push(users[j]);
            args.push(0);
            args.push(0);
        }
        sql = sql.substring(0,sql.lastIndexOf(","));
        imDb.execQuery({
            "sql": sql,
            "args": args,
            "handler": function (err, res) {
                if(err) {
                    log.error("createParticipantsForMysql is fail error: "+err+",session_id:"+session_id+",users:"+users.join(","));
                }else{
                   return res;
                }
            }
        });
        return true;
        return createParticipantsToMysql(session_id,users);
    }
    /**

+ 121 - 0
src/server/repository/mysql/message.repo.js

@ -0,0 +1,121 @@
/**
 * 搜索功能。
 */
"use strict";
let ImDb = require('../mysql/db/im.db');
let log = require('../../util/log.js');
const IMTABLE = require('../../include/commons').IM_DB;
class MessageRepo {
    constructor() {
    }
    /**
     * 根据sessionId获取对应的会话的信息列表mysql
     * @param sessionId
     */
    static getMessagesBySession(sessionId,handler){
        let session = new  Sessions();
        session.getSessions(sessionId,function(err,res){
            if(err){
                return;
            }else{
                if(res.length==0){
                    log.warn("session is not found!");
                    return;
                }
                let type = res[0].type;
                let db ="";
                if(type==1){
                    db = IMTABLE.MUCMSG;
                }else if(type==2){
                    db = IMTABLE.P2PMSG;
                }else{
                    db = IMTABLE.GROUPMSG;
                }
                let sql = "select * from "+db+" w where w.session_id = ? limit 0,"+config.sessionConfig.maxMessageCount;
                imDb.execQuery({
                    "sql": sessionsql,
                    "args": [sessionId],
                    "handler": function (err, res) {
                        if(err) {
                            log.error("sql:"+sql+"data:sessionId:"+sessionId);
                        }else{
                            log.info("getMessagesBySession success by sessionId :"+sessionId);
                        }
                        handler(err,res);
                    }
                })
            }
        })
    }
    /**
     * 分页获取消息MySQL
     * @param sessionId
     * @param page
     * @param pagesize
     */
    static getMessageByPage(sessionId,page,pagesize,handler){
        if(page>0){
            page =page*pagesize;
        }
        let session = new  Sessions();
        session.getSessions(sessionId,function(err,res){
            if(err){
                return;
            }else{
                if(res.length==0){
                    log.warn("session is not found!");
                    return;
                }
                let type = res[0].type;
                let db ="";
                if(type==1){
                    db = IMTABLE.MUCMSG;
                }else if(type==2){
                    db = IMTABLE.P2PMSG;
                }else{
                    db = IMTABLE.GROUPMSG;
                }
                let sql = "select * from "+db+" w where w.session_id = ? limit ?,?";
                imDb.execQuery({
                    "sql": sessionsql,
                    "args": [sessionId,page,pagesize],
                    "handler": function (err, res) {
                        if(err) {
                            log.error("sql:"+sql+"data:sessionId:"+sessionId);
                        }else{
                            log.info("getMessagesBySession success by sessionId :"+sessionId);
                        }
                        handler(err,res);
                    }
                })
            }
        })
    }
    /**
     * 保存Message 到mysql
     * @param messages 消息对象
     * @type type 会话类型,1表示MUC会话,2表示P2P,3表示群会话,4表示临时讨论组
     */
    static saveMessageForMysql(messages,type,messageid,sessionId){
        var sql = "INSERT INTO "+(type==1?IMTABLE.MUCMSG:type==2?IMTABLE.P2PMSG:IMTABLE.GROUPMSG)+" (id, session_id, sender_id, sender_name,content_type, content, timestamp) VALUES (?,?,?,?,?,?,?) ";
        imDb.execQuery({
            "sql": sql,
            "args": [messageid,sessionId,messages.senderId,messages.senderName,messages.contentType,messages.content,messages.timestamp],
            "handler": function (err, res) {
                if(err) {
                    log.error("sql:"+sql+",error:"+err+",data:"+JSON.stringify(messages)+",messageid:"+messageid+",sessionId:"+sessionId);
                }else{
                    log.info("save message to mysql is success by session :"+sessionId);
                }
            }
        });
    }
}
module.exports = MessageRepo;

+ 104 - 0
src/server/repository/mysql/participant.repo.js

@ -0,0 +1,104 @@
/**
 * 搜索功能。
 */
"use strict";
let ImDb = require('../mysql/db/im.db');
let log = require('../../util/log.js');
const IMTABLE = require('../../include/commons').IM_DB;
class ParticipantRepo {
    constructor() {
    }
    /**
     * 获取某个会话中的成员信息
     * @param sessionId
     * @param handler
     */
    static getParticipantsBySessionId(sessionId,handler){
        let sql ="select w.* from participants w where w.session_id =? ";
        ImDb.execQuery({
            "sql": sql,
            "args": [sessionId],
            "handler": function (err, res) {
                if(err) {
                    log.error("getParticipantsBySessionId is fail error: "+err);
                }
                handler(res);
            }
        });
    }
    /**
     * 判断用户是否存在session中
     * @param sessionId
     * @param userId
     * @param handler
     */
    static existsUser(sessionId,userId,handler){
        let sql ="select count(1) as count from participants w where w.session_id =? and w.participaint_id = ? ";
        ImDb.execQuery({
            "sql": sql,
            "args": [sessionId,userId],
            "handler": function (err, res) {
                if(err) {
                    log.error("existsUser is fail error: "+err);
                }
                handler(res[0].count);
            }
        });
    }
     /**
     * 根据医生和患者
     * @param patient
     * @param doctor
     */
     static getSessionIdByParticipants(patient,doctor,handler){
        let sql ="select session_id from "+IMTABLE.PARTICIPANTS+" p1 ,participants p2 " +
            "where p1.session_id = p2.session_id and " +
            "((p1.participaint_id = ? and p2.participaint_id = ?) or (p1.participaint_id = ? and p2.participaint_id = ?))"
         ImDb.execQuery({
            "sql": sql,
            "args": [patient,doctor,doctor,patient],
            "handler": function (err, res) {
                if(err) {
                    log.error("getSessionIdByParticipants is fail error: "+err);
                }
                handler(err,res);
            }
        });
    }
    /**
     * mysql成员创建
     * @param users
     */
    createParticipantsToMysql(session_id,users){
        let sql="insert into "+IMTABLE.PARTICIPANTS +" (session_id,participaint_id,participaint_role,receiving) VALUES "
        let args=[];
        for(var j in users){
            sql+="(?,?,?,?),";
            args.push(session_id);
            args.push(users[j]);
            args.push(0);
            args.push(0);
        }
        sql = sql.substring(0,sql.lastIndexOf(","));
        ImDb.execQuery({
            "sql": sql,
            "args": args,
            "handler": function (err, res) {
                if(err) {
                    log.error("createParticipantsForMysql is fail error: "+err+",session_id:"+session_id+",users:"+users.join(","));
                }else{
                    return res;
                }
            }
        });
        return true;
    }
}
module.exports = ParticipantRepo;