/** * 搜索功能。 */ "use strict"; let ImDb = require('../mysql/db/im.db'); let DbUtil = require('../../util/db.util'); let log = require('../../util/log.js'); const DB_TABLES = require('../../include/commons').DB_TABLES; const SESSION_USER_STATUS = require('../../include/commons').SESSION_USER_STATUS; const SESSION_TYPES = require('../../include/commons').SESSION_TYPES; class ParticipantRepo { constructor() { } /** * 获取会话的成员列表 * * @param sessionId * @param handler */ static findAll(sessionId, handler) { let sql = "select u.id, u.name, u.sex, u.birthdate, u.avatar, p.participant_role role, false is_patient from sessions s, participants p, doctors u " + "where s.id = ? and s.id = p.session_id and p.participant_id = u.id union " + "select u.id, u.name, u.sex, u.birthdate, u.avatar, p.participant_role role, true is_patient from sessions s, participants p, patients u " + "where s.id = ? and s.id = p.session_id and p.participant_id = u.id"; ImDb.execQuery({ "sql": sql, "args": [sessionId, sessionId], "handler": handler }); } /** * 获取会话的成员ID列表 * * @param sessionId * @param handler */ static findIds(sessionId, handler) { let sql = "select u.id, false is_patient from sessions s, participants p, doctors u " + "where s.id = ? and s.id = p.session_id and p.participant_id = u.id union " + "select u.id, true is_patient from sessions s, participants p, patients u " + "where s.id = ? and s.id = p.session_id and p.participant_id = u.id"; ImDb.execQuery({ "sql": sql, "args": [sessionId, sessionId], "handler": handler }); } /** * 获取会话的成员头像列表 * * @param sessionId * @param handler */ static findAllAvatars(sessionId, handler) { let sql = "select u.id, u.avatar from sessions s, participants p, doctors u " + "where s.id = ? and s.id = p.session_id and p.participant_id = u.id union " + "select u.id, u.avatar from sessions s, participants p, patients u " + "where s.id = ? and s.id = p.session_id and p.participant_id = u.id"; ImDb.execQuery({ "sql": sql, "args": [sessionId, sessionId], "handler": handler }); } /** * 获取会话的成员列表 * * @param sessionId * @param participantId * @param role * @param handler */ static updateParticipant(sessionId, participantId, role, handler) { let sql = "update participants set participant_role = ? where session_id = ? and participant_id = ?"; ImDb.execQuery({ "sql": sql, "args": [role, sessionId, participantId], "handler": handler }); } /** * 获取P2P成员所在会话。将成员的ID排序后取哈希值即可。 * * @param userId * @param anotherUserId * @param handler */ static findSessionIdByParticipantIds(userId, anotherUserId, handler) { let sessionId = DbUtil.stringArrayHash([userId, anotherUserId]); handler(null, sessionId); } static findMucSessionIdByUser(users,handler){ var userTemp = [];//先匹配出在线用户 for(var j in users){ if(users[j]==SESSION_USER_STATUS.ONLINE){ userTemp.push(j); } } let sql ="select s.* from "+DB_TABLES.Participants +" p1,"+DB_TABLES.Participants+ " p2,"+ DB_TABLES.Sessions+" s where p1.session_id = s.id and p2.session_id = s.id and s.type =? " + "and ((p1.participant_id =? and p2.participant_id = ?) or (p1.participant_id =? and p2.participant_id = ?))" ImDb.execQuery({ "sql": sql, "args": [SESSION_TYPES.MUC,userTemp[0], userTemp[1], userTemp[1],userTemp[0]], "handler": handler }); } /** * 更新最后消息获取时间。 * * @param lastMessageTime * @param sessionId * @param participantId * @param handler */ static updateLastFetchTime(lastMessageTime, sessionId, participantId, handler) { let sql = "update " + DB_TABLES.Participants + " set last_fetch_time=? where session_id = ? and participant_id =?"; ImDb.execQuery({ "sql": sql, "args": [lastMessageTime, sessionId, participantId], "handler": handler }); } /** * 用户是否在指定Session中 * * @param sessionId * @param userId * @param handler */ static existsParticipant(sessionId, userId, handler) { let sql = "select case when count(*) > 0 then true else false end exist from participants w where w.session_id =? and w.participant_id = ? "; ImDb.execQuery({ "sql": sql, "args": [sessionId, userId], "handler": handler }); } /** * mysql成员创建 * * @param sessionId * @param users JSON * @param handler */ static saveParticipantsToMysql(sessionId, users, handler) { let sql = "insert into " + DB_TABLES.Participants + " (session_id,participant_id,participant_role) VALUES " let args = []; for (var j in users) { sql += "(?,?,?)"; let tokens = users[j].split(":"); args.push(sessionId); args.push(tokens[0]); args.push(tokens.length > 1 ? tokens[1] : '0'); if (j != users.length - 1) sql += ", "; } sql += " ON DUPLICATE KEY UPDATE participant_role = VALUES(participant_role)"; ImDb.execQuery({ "sql": sql, "args": args, "handler": handler }); } static deleteUserFromMysql(sessionId, userId) { let sql = "delete from " + DB_TABLES.Participants + " where user_id=? and session_id=? "; ImDb.execQuery({ "sql": sql, "args": [userId, sessionId], "handler": handler }); } static getPatientOpenid(code, handler) { var sql = "select openid from patients where code = ? "; ImDb.execQuery({ "sql": sql, "args": [code], "handler": handler }); } } module.exports = ParticipantRepo;