/** * 议题模型。 */ "use strict"; let RedisClient = require('../../repository/redis/redis.client.js'); let redisClient = RedisClient.redisClient(); let redis = redisClient.connection; let RedisModel = require('./../redis.model.js'); let modelUtil = require('../../util/model.util'); let Participants = require("./participants"); let Sessions = require("./sessions"); let log = require('../../util/log.js'); let TopicsRepo = require('../../repository/mysql/topics.repo'); let configFile = require('../../include/commons').CONFIG_FILE; let config = require('../../resources/config/' + configFile); const RedisKey = require('../../include/commons').REDIS_KEYS; const UserStatus = require('../../include/commons').SESSION_USER_STATUS; class Topics extends RedisModel { constructor() { super(); } /** * 根据topicId获取对应的消息 * @param topicId */ getTopicMessages(topicId,page,pagesize) { let self = this; let topic_key = super.makeRedisKey(RedisKey.Topic, topicId); let _super = super.makeRedisKey; redis.hgetallAsync(topic_key).then(function (topic) { let message_time_key = _super(RedisKey.MessagesByTimestamp, topic.session_id); let message_key = _super(RedisKey.Messages, topic.session_id); //倒序取出所有的消息ID let create_time =topic.create_time; let end_time =topic.end_time; if(!end_time){ end_time = new Date().getTime(); } redis.zrevrangebyscoreAsync(message_time_key,end_time,create_time).then(function (messages) { //取出消息实例 redis.hmgetAsync(message_key,messages).then(function(res){ modelUtil.emitData(self.eventEmitter,res); }) }) }) } /** * * @param topicName 发起议题的名称 * @param patient 发起议题的患者 * @param doctor 参与的医生 * @param messages 发送的消息对象{description:"",title:"",img:"",patient:"",patientName:""}图片多个用逗号隔开 */ createTopics(topicName,topicId,patient,healthDoctor,doctor,messages){ let self = this; //MUC模式中sessionID就是患者ID let topics_key = super.makeRedisKey(RedisKey.Topics,patient); let topic_key = super.makeRedisKey(RedisKey.Topic,topicId); let sessions = new Sessions(); let participants = new Participants(); //从数据库中获取sessionId let date = new Date(); redis.zaddAsync(topics_key, date.getTime(), topicId).then(function(res){ redis.hmsetAsync(topic_key,"name",topicName,"end_by","","session_id",patient,"create_time",date.getTime(),"end_time","","description",messages.description).then(function(res){ sessions.getSessions(patient,function(err,res){ //已经存在对应的会话更新全科为旁听 if(res&&res.length>0){ participants.updateUser(patient,doctor,UserStatus.OTHER); callbegin(); }else{//不存在创建SESSION var users={}; users[patient]=UserStatus.ONLINE; users[healthDoctor]=UserStatus.ONLINE; users[doctor]=UserStatus.OTHER; sessions.createSession(patient,messages.patientName,config.sessionConfig.MUC,JSON.stringify(users),function(res){ if(res){ callbegin(); } }); } }) }) }) /** * 开始消息发送 */ function callbegin(){ let msg ={}; msg.senderId = messages.patient; msg.senderName = messages.patientName; msg.contentType = 6; msg.content ="开始咨询" msg.timestamp=date; sessions.saveMessageByTopic(msg,patient,function(err,msgId){ if(err){ modelUtil.emitData(self.eventEmitter,err); }else{ self.saveTopicsToSql(topicName,topicId,patient,msgId,date); callBeginMsg(); } }) } /** * 发送求助内容 */ function callBeginMsg(){ let msg ={}; msg.senderId = messages.patient; msg.senderName = messages.patientName; msg.contentType = 1; msg.content =messages.description; msg.timestamp = new Date(); sessions.saveMessageByTopic(msg,patient,function(err,msgId){ log.info("begin send"+messages.description); }) if(messages.img){ let imgs = messages.img.split(","); for(var j in imgs){ let msgimg ={}; msgimg.senderId = messages.patient; msgimg.senderName = messages.patientName; msgimg.contentType =2; msgimg.content =imgs[j]; msgimg.timestamp = new Date(); sessions.saveMessageByTopic(msgimg,patient,function(err,msgId){ log.info("begin send"+imgs[j]); }) } } modelUtil.emitData(self.eventEmitter,"创建成功!"); } } saveTopicsToSql(topicName,topicId,sessionId,messageId,date){ TopicsRepo.saveTopic(topicName,topicId,sessionId,messageId,date); } /** * 结束议题 * @param topicId * @param endUser */ endTopic(topicId,endUser,endUserName){ let endDate = new Date(); let self = this; let topic_key = super.makeRedisKey(RedisKey.Topic,topicId); redis.hmsetAsync(topic_key,"end_time",endDate.getTime(),"end_by",endUser).then(function (res) { redis.hgetallAsync(topic_key).then(function(topic){ callEnd(topic.session_id); }) }) /** * 结束消息发送 */ function callEnd(sessionId){ let msg ={}; msg.senderId = endUser; msg.senderName = endUserName; msg.contentType = 7; msg.content =endUserName+"结束了咨询" msg.timestamp = new Date(); let sessions = new Sessions(); sessions.saveMessageByTopic(msg,sessionId,function(err,msgId){ if(err){ modelUtil.emitData(self.eventEmitter,err); }else{ modelUtil.emitData(self.eventEmitter,"结束成功!"); TopicsRepo.endTopic(topicId,endUser,msg.date,msgId); } }) } } } // Expose class module.exports = Topics;