|
@ -3,26 +3,24 @@
|
|
*/
|
|
*/
|
|
"use strict";
|
|
"use strict";
|
|
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
|
|
|
|
var log = require("../util/log.js");
|
|
|
|
var modelUtil = require('../util/modelUtil');
|
|
|
|
var getui = require('getui');
|
|
|
|
|
|
|
|
var userRepo = require('../repository/doctor.repo.js');
|
|
|
|
var gmRepo = require('../repository/group.msg.repo');
|
|
|
|
var pmRepo = require('../repository/private.msg.repo');
|
|
|
|
var nmRepo = require("../repository/notify.msg.repo");
|
|
|
|
var statsRepo = require("../repository/stats.msg.repo");
|
|
|
|
var objectUtil = require("../util/objectUtil.js");
|
|
|
|
|
|
|
|
var MODEL_EVENTS = require('../include/commons').MODEL_EVENTS;
|
|
|
|
var CONTENT_TYPES = require('../include/commons').CONTENT_TYPE;
|
|
|
|
var PLATFORMS = require('../include/commons').PLATFORM;
|
|
|
|
|
|
|
|
class Doctor{
|
|
|
|
constructor(){
|
|
|
|
this._eventEmitter = new EventEmitter();
|
|
|
|
|
|
let log = require("../util/log.js");
|
|
|
|
let modelUtil = require('../util/modelUtil');
|
|
|
|
let getui = require('getui');
|
|
|
|
|
|
|
|
let BaseModel = require('./base.model');
|
|
|
|
let userRepo = require('../repository/doctor.repo.js');
|
|
|
|
let gmRepo = require('../repository/group.msg.repo');
|
|
|
|
let pmRepo = require('../repository/private.msg.repo');
|
|
|
|
let nmRepo = require("../repository/notify.msg.repo");
|
|
|
|
let statsRepo = require("../repository/stats.msg.repo");
|
|
|
|
let objectUtil = require("../util/objectUtil.js");
|
|
|
|
|
|
|
|
let CONTENT_TYPES = require('../include/commons').CONTENT_TYPE;
|
|
|
|
let PLATFORMS = require('../include/commons').PLATFORM;
|
|
|
|
|
|
|
|
class Doctor extends BaseModel{
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@ -30,56 +28,56 @@ class Doctor{
|
|
*
|
|
*
|
|
* @param message
|
|
* @param message
|
|
*/
|
|
*/
|
|
sendMessage(message){
|
|
|
|
var self = this;
|
|
|
|
|
|
sendMessage(message) {
|
|
|
|
let self = this;
|
|
userRepo.isExist(message.to, function (err, rows) {
|
|
userRepo.isExist(message.to, function (err, rows) {
|
|
if (err) {
|
|
if (err) {
|
|
modelUtil.emitDbError(self, 'Lookup receiving users failed', err);
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Lookup receiving users failed', err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
if (rows.length == 0) {
|
|
if (rows.length == 0) {
|
|
self.emit(MODEL_EVENTS.DataNotFound, {});
|
|
|
|
|
|
modelUtil.emitDataNotFound(self.eventEmitter, {});
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
// 保存消息
|
|
// 保存消息
|
|
var tempContent = message.contentType === CONTENT_TYPES.Article ? JSON.stringify(message.content) : message.content;
|
|
|
|
|
|
let tempContent = message.contentType === CONTENT_TYPES.Article ? JSON.stringify(message.content) : message.content;
|
|
pmRepo.save(message.to, message.from, message.contentType, tempContent, function (err, result) {
|
|
pmRepo.save(message.to, message.from, message.contentType, tempContent, function (err, result) {
|
|
if (err) {
|
|
if (err) {
|
|
modelUtil.emitDbError(self, 'Save private message failed.', err);
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Save private message failed.', err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
// 返回发送成功后的消息记录,控制器可选择结束网络连接
|
|
// 返回发送成功后的消息记录,控制器可选择结束网络连接
|
|
pmRepo.findOneMessage(result.insertId, function (err, msg) {
|
|
|
|
|
|
pmRepo.findOneMessage(result.insertId, function (err, rows) {
|
|
if (err) {
|
|
if (err) {
|
|
modelUtil.emitDbError(self, 'Save private message success, but return last message failed.', err);
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Save private message success, but return last message failed', err);
|
|
} else {
|
|
} else {
|
|
self.emit(MODEL_EVENTS.OK, fillMessages(msg));
|
|
|
|
|
|
modelUtil.emitData(self._eventEmitter, fillMessages(rows));
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
|
|
// 更新自身的聊天统计信息
|
|
// 更新自身的聊天统计信息
|
|
statsRepo.updatePrivateChatSummary(message.from, message.to, message.from, message.contentType, message.content, function (err, result) {
|
|
statsRepo.updatePrivateChatSummary(message.from, message.to, message.from, message.contentType, message.content, function (err, result) {
|
|
if (err) modelUtil.logDbError(null, err);
|
|
|
|
|
|
if (err) modelUtil.logError(null, err);
|
|
});
|
|
});
|
|
|
|
|
|
// 更新对端的聊天统计信息
|
|
// 更新对端的聊天统计信息
|
|
statsRepo.updatePrivateChatSummary(message.to, message.from, message.from, message.contentType, message.content, function (err, result) {
|
|
statsRepo.updatePrivateChatSummary(message.to, message.from, message.from, message.contentType, message.content, function (err, result) {
|
|
if (err) modelUtil.logDbError(null, err);
|
|
|
|
|
|
if (err) modelUtil.logError(null, err);
|
|
});
|
|
});
|
|
|
|
|
|
// 获取对方状态,即对端的系统平台,token等信息,并推送通知消息给对端
|
|
// 获取对方状态,即对端的系统平台,token等信息,并推送通知消息给对端
|
|
userRepo.getUserStatus(message.to, function (err, result) {
|
|
userRepo.getUserStatus(message.to, function (err, result) {
|
|
if (err) {
|
|
if (err) {
|
|
modelUtil.logDbError('Get target user status for private message failed: ' + message.to, err);
|
|
|
|
|
|
modelUtil.logError('Get target user status for private message failed: ' + message.to, err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
// 构建通知消息
|
|
// 构建通知消息
|
|
var title = '新消息';
|
|
|
|
var content = '';
|
|
|
|
|
|
let title = '新消息';
|
|
|
|
let content = '';
|
|
if (message.contentType === CONTENT_TYPES.PlainText) {
|
|
if (message.contentType === CONTENT_TYPES.PlainText) {
|
|
content = message.content;
|
|
content = message.content;
|
|
} else if (message.contentType === CONTENT_TYPES.Image) {
|
|
} else if (message.contentType === CONTENT_TYPES.Image) {
|
|
@ -90,8 +88,8 @@ class Doctor{
|
|
content = '接收到一条新消息';
|
|
content = '接收到一条新消息';
|
|
}
|
|
}
|
|
|
|
|
|
var isOnline = false;
|
|
|
|
var target;
|
|
|
|
|
|
let isOnline = false;
|
|
|
|
let target;
|
|
if (result.length > 0) {
|
|
if (result.length > 0) {
|
|
target = result[0];
|
|
target = result[0];
|
|
if (target.is_online) {
|
|
if (target.is_online) {
|
|
@ -99,7 +97,7 @@ class Doctor{
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
var notifyMessage = JSON.stringify({type: 'p2p_msg', from_uid: message.from});
|
|
|
|
|
|
let notifyMessage = JSON.stringify({type: 'p2p_msg', from_uid: message.from});
|
|
|
|
|
|
// 保存通知消息到数据库中并根据用户在线状态推送此消息
|
|
// 保存通知消息到数据库中并根据用户在线状态推送此消息
|
|
nmRepo.save(message.to, message.contentType, title, content, notifyMessage, isOnline, function (err, result) {
|
|
nmRepo.save(message.to, message.contentType, title, content, notifyMessage, isOnline, function (err, result) {
|
|
@ -147,48 +145,44 @@ class Doctor{
|
|
/**
|
|
/**
|
|
* 获取最近聊天的用户,组。
|
|
* 获取最近聊天的用户,组。
|
|
*/
|
|
*/
|
|
getRecentChats(){
|
|
|
|
statsRepo.getRecentChats(userId, days, function (err, result) {
|
|
|
|
|
|
getRecentChatList() {
|
|
|
|
let self = this;
|
|
|
|
statsRepo.getRecentChats(userId, days, function (err, rows) {
|
|
if (err) {
|
|
if (err) {
|
|
log.error('Get recent chat objects failed: ', err);
|
|
|
|
|
|
|
|
res.status(500).send({message: 'Get recent chat objects failed.'});
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Get recent chat objects failed', err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
var data = {patients: [], doctors: [], groups: []};
|
|
|
|
if (result.length === 0) {
|
|
|
|
res.status(200).send(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < result.length; ++i) {
|
|
|
|
var row = result[i];
|
|
|
|
if (row.type.indexOf('patient') > -1) {
|
|
|
|
data.patients.push({
|
|
|
|
code: row.code,
|
|
|
|
name: row.name,
|
|
|
|
birthday: row.birthday === null ? "" : row.birthday,
|
|
|
|
sex: row.sex,
|
|
|
|
avatar: row.photo === null ? "" : row.photo
|
|
|
|
});
|
|
|
|
} else if (row.type.indexOf('doctor') > -1) {
|
|
|
|
data.doctors.push({
|
|
|
|
code: row.code,
|
|
|
|
name: row.name,
|
|
|
|
birthday: row.birthday === null ? "" : row.birthday,
|
|
|
|
sex: row.sex,
|
|
|
|
avatar: row.photo === null ? "" : row.photo
|
|
|
|
});
|
|
|
|
} else if (row.type.indexOf('group') > -1) {
|
|
|
|
data.groups.push({
|
|
|
|
code: row.code,
|
|
|
|
name: row.name
|
|
|
|
});
|
|
|
|
|
|
let data = {patients: [], doctors: [], groups: []};
|
|
|
|
if (rows.length > 0) {
|
|
|
|
for (let i = 0; i < rows.length; ++i) {
|
|
|
|
let row = rows[i];
|
|
|
|
if (row.type.indexOf('patient') > -1) {
|
|
|
|
data.patients.push({
|
|
|
|
code: row.code,
|
|
|
|
name: row.name,
|
|
|
|
birthday: row.birthday === null ? "" : row.birthday,
|
|
|
|
sex: row.sex,
|
|
|
|
avatar: row.photo === null ? "" : row.photo
|
|
|
|
});
|
|
|
|
} else if (row.type.indexOf('doctor') > -1) {
|
|
|
|
data.doctors.push({
|
|
|
|
code: row.code,
|
|
|
|
name: row.name,
|
|
|
|
birthday: row.birthday === null ? "" : row.birthday,
|
|
|
|
sex: row.sex,
|
|
|
|
avatar: row.photo === null ? "" : row.photo
|
|
|
|
});
|
|
|
|
} else if (row.type.indexOf('group') > -1) {
|
|
|
|
data.groups.push({
|
|
|
|
code: row.code,
|
|
|
|
name: row.name
|
|
|
|
});
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
res.status(200).send(data);
|
|
|
|
|
|
modelUtil.emitData(self.eventEmitter, data);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
@ -198,18 +192,18 @@ class Doctor{
|
|
* @param userId
|
|
* @param userId
|
|
*/
|
|
*/
|
|
getChatList(userId) {
|
|
getChatList(userId) {
|
|
var self = this;
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
// 与患者的私信
|
|
// 与患者的私信
|
|
pmRepo.findAllP2PWithPatient(userId, function (err, patients) {
|
|
pmRepo.findAllP2PWithPatient(userId, function (err, patients) {
|
|
if (err) {
|
|
if (err) {
|
|
modelUtil.emitDbError(self, 'Get chat list with patient failed', err);
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Get chat list with patient failed', err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
var chats = {patients: [], doctors: [], groups: []};
|
|
|
|
for (var i = 0; i < patients.length; i++) {
|
|
|
|
var patient = patients[i];
|
|
|
|
|
|
let chats = {patients: [], doctors: [], groups: []};
|
|
|
|
for (let i = 0; i < patients.length; i++) {
|
|
|
|
let patient = patients[i];
|
|
chats.patients.push({
|
|
chats.patients.push({
|
|
code: patient.code,
|
|
code: patient.code,
|
|
name: patient.name,
|
|
name: patient.name,
|
|
@ -226,15 +220,15 @@ class Doctor{
|
|
// 含有患者的群
|
|
// 含有患者的群
|
|
gmRepo.findAllGroupsWithPatient(userId, function (err, groups) {
|
|
gmRepo.findAllGroupsWithPatient(userId, function (err, groups) {
|
|
if (err) {
|
|
if (err) {
|
|
modelUtil.emitDbError(self, 'Get group list with patient failed', err);
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Get group list with patient failed', err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
for (var i = 0; i < groups.length; i++) {
|
|
|
|
var group = groups[i];
|
|
|
|
|
|
for (let i = 0; i < groups.length; i++) {
|
|
|
|
let group = groups[i];
|
|
|
|
|
|
// 过滤掉医生间的求助团队
|
|
// 过滤掉医生间的求助团队
|
|
if(group.group_type === 2) continue;
|
|
|
|
|
|
if (group.group_type === 2) continue;
|
|
|
|
|
|
chats.groups.push({
|
|
chats.groups.push({
|
|
code: group.code,
|
|
code: group.code,
|
|
@ -250,12 +244,12 @@ class Doctor{
|
|
// 医生间的私聊
|
|
// 医生间的私聊
|
|
pmRepo.findAllP2PWithDoctor(userId, function (err, doctors) {
|
|
pmRepo.findAllP2PWithDoctor(userId, function (err, doctors) {
|
|
if (err) {
|
|
if (err) {
|
|
modelUtil.emitDbError(self, 'Get chat list with doctor failed', err);
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Get chat list with doctor failed', err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
for (var i = 0; i < doctors.length; i++) {
|
|
|
|
var doctor = doctors[i];
|
|
|
|
|
|
for (let i = 0; i < doctors.length; i++) {
|
|
|
|
let doctor = doctors[i];
|
|
chats.doctors.push({
|
|
chats.doctors.push({
|
|
code: doctor.code,
|
|
code: doctor.code,
|
|
name: doctor.name,
|
|
name: doctor.name,
|
|
@ -271,12 +265,12 @@ class Doctor{
|
|
// 获取医生间的组
|
|
// 获取医生间的组
|
|
gmRepo.findAllGroupsWithDoctor(userId, function (err, groups) {
|
|
gmRepo.findAllGroupsWithDoctor(userId, function (err, groups) {
|
|
if (err) {
|
|
if (err) {
|
|
modelUtil.emitDbError(self, 'Get group list with doctor failed', err);
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Get group list with doctor failed', err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
for (var i = 0; i < groups.length; i++) {
|
|
|
|
var group = groups[i];
|
|
|
|
|
|
for (let i = 0; i < groups.length; i++) {
|
|
|
|
let group = groups[i];
|
|
chats.groups.push({
|
|
chats.groups.push({
|
|
code: group.code,
|
|
code: group.code,
|
|
name: group.name,
|
|
name: group.name,
|
|
@ -288,7 +282,7 @@ class Doctor{
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
self.emit(MODEL_EVENTS.OK, chats);
|
|
|
|
|
|
modelUtil.emitData(self.eventEmitter, chats);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
})
|
|
})
|
|
@ -300,19 +294,19 @@ class Doctor{
|
|
*
|
|
*
|
|
* @param userId
|
|
* @param userId
|
|
*/
|
|
*/
|
|
getChatListWithDoctor(userId){
|
|
|
|
|
|
getChatListWithDoctor(userId) {
|
|
|
|
let self = this;
|
|
|
|
|
|
// 先获取医生间的私聊
|
|
// 先获取医生间的私聊
|
|
pmRepo.findAllP2PWithDoctor(userId, function (err, doctors) {
|
|
pmRepo.findAllP2PWithDoctor(userId, function (err, doctors) {
|
|
if (err) {
|
|
if (err) {
|
|
log.error('Get chat list with doctor failed: ', err);
|
|
|
|
|
|
|
|
res.status(500).send({message: 'Get chat list with doctor failed.'});
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Get chat list with doctor failed', err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
var chats = {doctors: [], groups: []};
|
|
|
|
for (var i = 0; i < doctors.length; i++) {
|
|
|
|
var doctor = doctors[i];
|
|
|
|
|
|
let chats = {doctors: [], groups: []};
|
|
|
|
for (let i = 0; i < doctors.length; i++) {
|
|
|
|
let doctor = doctors[i];
|
|
chats.doctors.push({
|
|
chats.doctors.push({
|
|
code: doctor.code,
|
|
code: doctor.code,
|
|
name: doctor.name,
|
|
name: doctor.name,
|
|
@ -328,14 +322,12 @@ class Doctor{
|
|
// 再获取医生间的组
|
|
// 再获取医生间的组
|
|
gmRepo.findAllGroupsWithDoctor(userId, function (err, groups) {
|
|
gmRepo.findAllGroupsWithDoctor(userId, function (err, groups) {
|
|
if (err) {
|
|
if (err) {
|
|
log.error('Get group list with doctor failed: ', err);
|
|
|
|
|
|
|
|
res.status(500).send({message: 'Get group list with doctor failed.'});
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Get group list with doctor failed', err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
for (var i = 0; i < groups.length; i++) {
|
|
|
|
var group = groups[i];
|
|
|
|
|
|
for (let i = 0; i < groups.length; i++) {
|
|
|
|
let group = groups[i];
|
|
chats.groups.push({
|
|
chats.groups.push({
|
|
code: group.code,
|
|
code: group.code,
|
|
name: group.name,
|
|
name: group.name,
|
|
@ -347,34 +339,141 @@ class Doctor{
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
res.status(200).send(chats);
|
|
|
|
|
|
modelUtil.emitData(self.eventEmitter, chats);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取与患者的聊天列表。
|
|
|
|
*/
|
|
|
|
getChatsListWithPatient(userId) {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
pmRepo.findAllP2PWithPatient(userId, function (err, patients) {
|
|
|
|
if (err) {
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Get chat list with patient failed', err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let chats = {patients: [], groups: []};
|
|
|
|
for (let i = 0; i < patients.length; i++) {
|
|
|
|
let patient = patients[i];
|
|
|
|
chats.patients.push({
|
|
|
|
code: patient.code,
|
|
|
|
name: patient.name,
|
|
|
|
birthday: patient.birthday,
|
|
|
|
sex: patient.sex,
|
|
|
|
avatar: patient.photo == null ? "" : patient.photo,
|
|
|
|
newMessageCount: patient.new_msg_count,
|
|
|
|
lastContentType: patient.last_content_type,
|
|
|
|
lastContent: patient.last_content,
|
|
|
|
timestamp: objectUtil.timestampToLong(patient.timestamp)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
gmRepo.findAllGroupsWithPatient(userId, function (err, groups) {
|
|
|
|
if (err) {
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Get group list with patient failed', err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < groups.length; i++) {
|
|
|
|
let group = groups[i];
|
|
|
|
|
|
|
|
// 过滤掉医生间的求助团队
|
|
|
|
if (group.group_type === 2) continue;
|
|
|
|
|
|
|
|
chats.groups.push({
|
|
|
|
code: group.code,
|
|
|
|
name: group.name,
|
|
|
|
groupType: group.msg_type,
|
|
|
|
newMessageCount: group.new_msg_count,
|
|
|
|
lastContentType: group.last_content_type,
|
|
|
|
lastContent: group.last_content,
|
|
|
|
timestamp: objectUtil.timestampToLong(group.timestamp)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
modelUtil.emitData(self.eventEmitter, chats);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取与指定用户的聊天记录。
|
|
* 获取与指定用户的聊天记录。
|
|
*
|
|
*
|
|
* @param userId
|
|
* @param userId
|
|
* @param peerId
|
|
* @param peerId
|
|
|
|
* @param contentType
|
|
|
|
* @param msgStartId
|
|
|
|
* @param msgEndId
|
|
|
|
* @param count
|
|
|
|
* @param closedInterval
|
|
*/
|
|
*/
|
|
getPrivateMessages(userId, peerId){
|
|
|
|
|
|
getPrivateMessages(userId, peerId, contentType, msgStartId, msgEndId, count, closedInterval) {
|
|
|
|
let self = this;
|
|
|
|
|
|
pmRepo.findAllMessages(userId, peerId, contentType === undefined ? "1,2,3,5,6" : contentType, msgStartId, msgEndId, count, closedInterval, function (err, rows) {
|
|
pmRepo.findAllMessages(userId, peerId, contentType === undefined ? "1,2,3,5,6" : contentType, msgStartId, msgEndId, count, closedInterval, function (err, rows) {
|
|
if (err) {
|
|
if (err) {
|
|
log.error("Get private message failed, ", err);
|
|
|
|
|
|
|
|
res.status(500).send({message: "Get private messages failed."});
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Get private message failed', err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
var messages = fillMessages(rows);
|
|
|
|
|
|
let messages = self.fillMessages(rows);
|
|
|
|
modelUtil.emitData(self.eventEmitter, messages);
|
|
|
|
|
|
// 清空统计信息
|
|
// 清空统计信息
|
|
statsRepo.clearPrivateChatSummary(userId, peerId, function (err, result) {
|
|
statsRepo.clearPrivateChatSummary(userId, peerId, function (err, result) {
|
|
if (err) console.log(err);
|
|
if (err) console.log(err);
|
|
});
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取与某人聊天的未读消息数。
|
|
|
|
*
|
|
|
|
* @param userId
|
|
|
|
* @param peerId
|
|
|
|
*/
|
|
|
|
getUnreadMessageCount(userId, peerId) {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
statsRepo.getPrivateChatAllUnReadCount(userId, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, "Get unread private message count failed", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = {userId: userId, messageType: 1, newMessageCount: 0};
|
|
|
|
for (let i = 0; i < result.length; i++) {
|
|
|
|
data.newMessageCount += result[i].new_msg_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
modelUtil.emitData(self.eventEmitter, data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取所有未读的消息数,包括群。
|
|
|
|
*
|
|
|
|
* @param userId
|
|
|
|
*/
|
|
|
|
getAllUnreadMessageCount(userId) {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
statsRepo.getChatAllUnReadCount(userId, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, "Get all unread message count failed", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = {userId: userId, messageType: 0, newMessageCount: 0};
|
|
|
|
for (let index = 0; index < result.length; index++) {
|
|
|
|
data.newMessageCount += result[index].new_msg_count;
|
|
|
|
}
|
|
|
|
|
|
res.status(200).send(messages);
|
|
|
|
|
|
modelUtil.emitData(self.eventEmitter, data);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
@ -385,37 +484,144 @@ class Doctor{
|
|
* @param peerId
|
|
* @param peerId
|
|
*/
|
|
*/
|
|
getUnreadPrivateMessages(userId, peerId) {
|
|
getUnreadPrivateMessages(userId, peerId) {
|
|
|
|
let self = this;
|
|
statsRepo.getPrivateChatSummary(userId, peerId, function (err, summary) {
|
|
statsRepo.getPrivateChatSummary(userId, peerId, function (err, summary) {
|
|
if (err) {
|
|
if (err) {
|
|
log.error("Get unread private messages failed: ", err);
|
|
|
|
|
|
|
|
res.status(500).send({message: "Get unread private messages failed."});
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, 'Get unread private messages failed', err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
// 没有未读消息,直接返回
|
|
// 没有未读消息,直接返回
|
|
if (summary.length == 0 || summary[0].new_msg_count === 0) {
|
|
if (summary.length == 0 || summary[0].new_msg_count === 0) {
|
|
res.status(200).send({startId: 0, count: 0, records: []});
|
|
|
|
|
|
modelUtil.emitData(self.eventEmitter, {startId: 0, count: 0, records: []});
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
pmRepo.findUnread(peerId, userId, MAX_INT, summary[0].new_msg_count, function (err, rows) {
|
|
pmRepo.findUnread(peerId, userId, MAX_INT, summary[0].new_msg_count, function (err, rows) {
|
|
if (err) {
|
|
if (err) {
|
|
log.error("Get unread private messages failed: ", err);
|
|
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, "Get unread private messages failed", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let messages = self.fillMessages(rows);
|
|
|
|
modelUtil.emitData(self.eventEmitter, messages);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取聊天统计摘要。
|
|
|
|
*
|
|
|
|
* @param userId
|
|
|
|
* @param peerId
|
|
|
|
*/
|
|
|
|
getChatSummary(userId, peerId) {
|
|
|
|
let self = this;
|
|
|
|
statsRepo.getPrivateChatSummary(userId, peerId, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, "Get private messages stats failed", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = {
|
|
|
|
userId: userId,
|
|
|
|
peerId: peerId,
|
|
|
|
lastCContentType: 1,
|
|
|
|
lastContent: "",
|
|
|
|
newMessageCount: 0,
|
|
|
|
timestamp: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
if (result.length > 0) {
|
|
|
|
let row = result[0];
|
|
|
|
|
|
|
|
data.userId = row.uid;
|
|
|
|
data.peerId = row.from_uid;
|
|
|
|
data.lastContentType = row.last_content_type;
|
|
|
|
data.lastContent = row.last_content;
|
|
|
|
data.newMessageCount = row.new_msg_count;
|
|
|
|
data.timestamp = objectUtil.timestampToLong(row.timestamp)
|
|
|
|
}
|
|
|
|
|
|
|
|
modelUtil.emitData(self.eventEmitter, data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getMessage(messageId, messageType) {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
// 私信
|
|
|
|
if (messageType == 1) {
|
|
|
|
pmRepo.findOneMessage(messageId, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, "Get message failed", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
res.status(500).send({message: "Get unread private messages failed."});
|
|
|
|
|
|
if (result.length == 0) {
|
|
|
|
modelUtil.emitDataNotFound(self.eventEmitter, "Message not found.");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
var messages = fillMessages(rows);
|
|
|
|
|
|
modelUtil.emitData(self.eventEmitter, {
|
|
|
|
id: result[0].msg_id,
|
|
|
|
from: result[0].from_uid,
|
|
|
|
to: result[0].to_uid,
|
|
|
|
contentType: result[0].type,
|
|
|
|
content: result[0].content,
|
|
|
|
timestamp: objectUtil.timestampToLong(result[0].timestamp)
|
|
|
|
});
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
gmRepo.findOneMessage(messageId, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, "Get message failed", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
res.status(200).send(messages);
|
|
|
|
|
|
if (result.length == 0) {
|
|
|
|
modelUtil.emitDataNotFound(self.eventEmitter, "Message not found.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
modelUtil.emitData(self.eventEmitter, {
|
|
|
|
id: result[0].msg_id,
|
|
|
|
from: result[0].from_uid,
|
|
|
|
at: result[0].at_uid,
|
|
|
|
groupId: result[0].to_gid,
|
|
|
|
contentType: result[0].type,
|
|
|
|
content: result[0].content,
|
|
|
|
timestamp: objectUtil.timestampToLong(result[0].timestamp)
|
|
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
on(message, handler){
|
|
|
|
this._eventEmitter.on(message, handler);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 判断与患者的最新咨询会话是否已经结束。
|
|
|
|
*/
|
|
|
|
isConsultFinished(doctorId, patientId) {
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
pmRepo.isCurrentSessionFinished(doctorId, patientId, function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
modelUtil.emitDbError(self.eventEmitter, "Get session finish status failed: ", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = {finished: true, consultId: ''};
|
|
|
|
if (result.length > 0) {
|
|
|
|
let finishRow = result[0];
|
|
|
|
|
|
|
|
data.finished = finishRow.finished === 1;
|
|
|
|
if (!data.finished) {
|
|
|
|
data.consultId = finishRow.consult_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
modelUtil.emitData(self.eventEmitter, data);
|
|
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@ -426,10 +632,10 @@ class Doctor{
|
|
* @returns {startId: 0, count: 0, records: []}
|
|
* @returns {startId: 0, count: 0, records: []}
|
|
*/
|
|
*/
|
|
static fillMessages(rows) {
|
|
static fillMessages(rows) {
|
|
var messages = {startId: rows.length > 0 ? rows[0].msg_id : '', count: rows.length, records: []};
|
|
|
|
for (var i = 0; i < rows.length; i++) {
|
|
|
|
var row = rows[i];
|
|
|
|
var record = {
|
|
|
|
|
|
let messages = {startId: rows.length > 0 ? rows[0].msg_id : '', count: rows.length, records: []};
|
|
|
|
for (let i = 0; i < rows.length; i++) {
|
|
|
|
let row = rows[i];
|
|
|
|
let record = {
|
|
id: row.msg_id,
|
|
id: row.msg_id,
|
|
from: row.from_uid,
|
|
from: row.from_uid,
|
|
contentType: row.type,
|
|
contentType: row.type,
|