|  | @ -1,15 +1,17 @@
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * 搜索功能。
 | 
	
		
			
				|  |  |  * 会话成员。
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | "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;
 | 
	
		
			
				|  |  | const SESSION_BUSINESS_TYPE = require('../../include/commons').SESSION_BUSINESS_TYPE;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | class ParticipantRepo {
 | 
	
		
			
				|  |  |     constructor() {
 | 
	
		
			
				|  |  |     }
 | 
	
	
		
			
				|  | @ -21,10 +23,10 @@ class ParticipantRepo {
 | 
	
		
			
				|  |  |      * @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";
 | 
	
		
			
				|  |  |         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,
 | 
	
	
		
			
				|  | @ -40,10 +42,10 @@ class ParticipantRepo {
 | 
	
		
			
				|  |  |      * @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";
 | 
	
		
			
				|  |  |         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,
 | 
	
	
		
			
				|  | @ -59,10 +61,10 @@ class ParticipantRepo {
 | 
	
		
			
				|  |  |      * @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";
 | 
	
		
			
				|  |  |         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,
 | 
	
	
		
			
				|  | @ -80,7 +82,7 @@ class ParticipantRepo {
 | 
	
		
			
				|  |  |      * @param handler
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     static updateParticipant(sessionId, participantId, role, handler) {
 | 
	
		
			
				|  |  |         let sql = "update participants set participant_role = ? where session_id = ? and participant_id = ?";
 | 
	
		
			
				|  |  |         let sql = "update participants set participant_role = ? WHERE session_id = ? AND participant_id = ?";
 | 
	
		
			
				|  |  |         ImDb.execQuery({
 | 
	
		
			
				|  |  |             "sql": sql,
 | 
	
		
			
				|  |  |             "args": [role, sessionId, participantId],
 | 
	
	
		
			
				|  | @ -100,19 +102,19 @@ class ParticipantRepo {
 | 
	
		
			
				|  |  |         handler(null, sessionId);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     static getBusinessType(users,handler){
 | 
	
		
			
				|  |  |         let sql ="select count(1) as count from patients p where p.id in ('"+users+"')";
 | 
	
		
			
				|  |  |     static getBusinessType(users, handler) {
 | 
	
		
			
				|  |  |         let sql = "SELECT count(1) as count FROM patients p WHERE p.id in ('" + users + "')";
 | 
	
		
			
				|  |  |         ImDb.execQuery({
 | 
	
		
			
				|  |  |             "sql": sql,
 | 
	
		
			
				|  |  |             "args": [],
 | 
	
		
			
				|  |  |             "handler": function(err,res){
 | 
	
		
			
				|  |  |                 if(err){
 | 
	
		
			
				|  |  |                     console.log("err businessType  : "+err);
 | 
	
		
			
				|  |  |                 }else{
 | 
	
		
			
				|  |  |                     if(res[0].count){
 | 
	
		
			
				|  |  |                         handler(err,SESSION_BUSINESS_TYPE.PATIENT);
 | 
	
		
			
				|  |  |                     }else{
 | 
	
		
			
				|  |  |                         handler(err,SESSION_BUSINESS_TYPE.DOCTOR);
 | 
	
		
			
				|  |  |             "handler": function (err, res) {
 | 
	
		
			
				|  |  |                 if (err) {
 | 
	
		
			
				|  |  |                     console.log("err businessType  : " + err);
 | 
	
		
			
				|  |  |                 } else {
 | 
	
		
			
				|  |  |                     if (res[0].count) {
 | 
	
		
			
				|  |  |                         handler(err, SESSION_BUSINESS_TYPE.PATIENT);
 | 
	
		
			
				|  |  |                     } else {
 | 
	
		
			
				|  |  |                         handler(err, SESSION_BUSINESS_TYPE.DOCTOR);
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
	
		
			
				|  | @ -121,35 +123,37 @@ class ParticipantRepo {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     static findNameById(userId,handler){
 | 
	
		
			
				|  |  |         let sql ="select p.name  from patients p where p.id =? union select d.name  from doctors d where d.id =?";
 | 
	
		
			
				|  |  |     static findNameById(userId, handler) {
 | 
	
		
			
				|  |  |         let sql = "SELECT p.name FROM patients p WHERE p.id =? union SELECT d.name FROM doctors d WHERE d.id =?";
 | 
	
		
			
				|  |  |         ImDb.execQuery({
 | 
	
		
			
				|  |  |             "sql": sql,
 | 
	
		
			
				|  |  |             "args": [userId,userId],
 | 
	
		
			
				|  |  |             "handler": function(err,res){
 | 
	
		
			
				|  |  |                 if(err){
 | 
	
		
			
				|  |  |                     console.log("err businessType  : "+err);
 | 
	
		
			
				|  |  |                 }else{
 | 
	
		
			
				|  |  |                     handler(null,res);
 | 
	
		
			
				|  |  |             "args": [userId, userId],
 | 
	
		
			
				|  |  |             "handler": function (err, res) {
 | 
	
		
			
				|  |  |                 if (err) {
 | 
	
		
			
				|  |  |                     console.log("err businessType  : " + err);
 | 
	
		
			
				|  |  |                 } else {
 | 
	
		
			
				|  |  |                     handler(null, res);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         });
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     static findMucSessionIdByUser(users,handler){
 | 
	
		
			
				|  |  |         var userTemp = [];//先匹配出在线用户
 | 
	
		
			
				|  |  |         for(var j in users){
 | 
	
		
			
				|  |  |             if(users[j]==SESSION_USER_STATUS.ONLINE){
 | 
	
		
			
				|  |  |                 userTemp.push(j);
 | 
	
		
			
				|  |  |     static findMucSessionIdByUser(users, handler) {
 | 
	
		
			
				|  |  |         //先匹配出在线用户
 | 
	
		
			
				|  |  |         let userTemp = [];
 | 
	
		
			
				|  |  |         users.forEach(function (user) {
 | 
	
		
			
				|  |  |             if (user == SESSION_USER_STATUS.ONLINE) {
 | 
	
		
			
				|  |  |                 userTemp.push(user);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         let sql ="select DISTINCT 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 = ?))"
 | 
	
		
			
				|  |  |         });
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         let sql = "SELECT DISTINCT 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]],
 | 
	
		
			
				|  |  |             "args": [SESSION_TYPES.MUC, userTemp[0], userTemp[1], userTemp[1], userTemp[0]],
 | 
	
		
			
				|  |  |             "handler": handler
 | 
	
		
			
				|  |  |         });
 | 
	
		
			
				|  |  |     }
 | 
	
	
		
			
				|  | @ -163,7 +167,7 @@ class ParticipantRepo {
 | 
	
		
			
				|  |  |      * @param handler
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     static updateLastFetchTime(lastMessageTime, sessionId, participantId, handler) {
 | 
	
		
			
				|  |  |         let sql = "update " + DB_TABLES.Participants + " set last_fetch_time=? where session_id = ? and participant_id =?";
 | 
	
		
			
				|  |  |         let sql = "update " + DB_TABLES.Participants + " set last_fetch_time=? WHERE session_id = ? AND participant_id =?";
 | 
	
		
			
				|  |  |         ImDb.execQuery({
 | 
	
		
			
				|  |  |             "sql": sql,
 | 
	
		
			
				|  |  |             "args": [lastMessageTime, sessionId, participantId],
 | 
	
	
		
			
				|  | @ -179,7 +183,7 @@ class ParticipantRepo {
 | 
	
		
			
				|  |  |      * @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 = ? ";
 | 
	
		
			
				|  |  |         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],
 | 
	
	
		
			
				|  | @ -195,9 +199,9 @@ class ParticipantRepo {
 | 
	
		
			
				|  |  |      * @param handler
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     static saveParticipantsToMysql(sessionId, users, handler) {
 | 
	
		
			
				|  |  |         let sql = "insert into " + DB_TABLES.Participants + " (session_id,participant_id,participant_role) VALUES "
 | 
	
		
			
				|  |  |         let sql = "INSERT INTO " + DB_TABLES.Participants + " (session_id,participant_id,participant_role) VALUES ";
 | 
	
		
			
				|  |  |         let args = [];
 | 
	
		
			
				|  |  |         for (var j in users) {
 | 
	
		
			
				|  |  |         for (let j in users) {
 | 
	
		
			
				|  |  |             sql += "(?,?,?)";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             let tokens = users[j].split(":");
 | 
	
	
		
			
				|  | @ -217,26 +221,16 @@ class ParticipantRepo {
 | 
	
		
			
				|  |  |         });
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     static deleteUserFromMysql(sessionId, userId,handler) {
 | 
	
		
			
				|  |  |         let sql = "delete from " + DB_TABLES.Participants + " where user_id=? and session_id=? ";
 | 
	
		
			
				|  |  |     static deleteUserFromMysql(sessionId, userId, handler) {
 | 
	
		
			
				|  |  |         let sql = "DELETE FROM " + DB_TABLES.Participants + " WHERE user_id = ? AND session_id = ? ";
 | 
	
		
			
				|  |  |         ImDb.execQuery({
 | 
	
		
			
				|  |  |             "sql": sql,
 | 
	
		
			
				|  |  |             "args": [userId, sessionId],
 | 
	
		
			
				|  |  |             "handler": handler||function(){
 | 
	
		
			
				|  |  |             "handler": handler || function () {
 | 
	
		
			
				|  |  |                 log.info("deleteUserFromMysql");
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         });
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     static getPatientOpenid(code, handler) {
 | 
	
		
			
				|  |  |         var sql = "select openid from patients where code = ? ";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         ImDb.execQuery({
 | 
	
		
			
				|  |  |             "sql": sql,
 | 
	
		
			
				|  |  |             "args": [code],
 | 
	
		
			
				|  |  |             "handler": handler
 | 
	
		
			
				|  |  |         });
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | module.exports = ParticipantRepo;
 |