/** * 患者、讨论组搜索。 * * 注意:此模型效率堪忧,但为了实现先这样做。更稳妥的方案是使用Solr或Elastic Search * 为数据提供索引功能,JS使用搜索接口搜索之后再取得对象的ID进行获取,提高效率。 * 后续开发都希望看到这段注释,实现此方案。 * * author: Sand * since: 2016.11.20 */ "use strict"; let BaseModel = require('./base.model'); let searchRepo = require('../repository/search.repo'); let modelUtil = require("../util/modelUtil"); let objectUtil = require('../util/objectUtil'); class Search extends BaseModel { constructor() { super(); } /** * 搜索患者相关的数据,包括患者信息与相关的私信记录。关键词不支持空格拆分。 */ searchAboutPatient(userId, userRole, keyword) { let self = this; searchRepo.searchPatients(userId, userRole, keyword, function (err, patients) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search patient on basic information failed", err); return; } var data = {patients: [], group: [], chats: []}; for (var i = 0; i < patients.length; ++i) { var patient = patients[i]; var p = {}; console.log(patient.code); p = { code: patient.code, name: patient.name, sex: patient.sex, birthday: objectUtil.timestampToLong(patient.birthday), avatar: patient.photo === null ? "" : patient.photo }; data.patients.push(p); } searchRepo.searchGroupPatients(userId, keyword, function (err, groups) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search talk group failed", err); return; } var group = null; for (var i = 0; i < groups.length; ++i) { var t = groups[i]; group = { code: t.code, name: t.name, members: t.con, }; data.group.push(group); } searchRepo.searchPatientPM(userId, keyword, function (err, chats) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search patient on private messages failed", err); return; } for (var i = 0; i < chats.length; ++i) { var lastPatient = { code: '', name: '', sex: '', avatar: '', amount: '', content: '', chat: '', type: '' }; var chat = chats[i]; console.log(JSON.stringify(chat)); lastPatient.code = chat.code; lastPatient.name = chat.name; lastPatient.sex = chat.sex; lastPatient.birthday = objectUtil.timestampToLong(chat.birthday); lastPatient.avatar = chat.photo === null ? "" : chat.photo; lastPatient.amount = chat.amount; lastPatient.chat = chat.chat; lastPatient.content = chat.content; lastPatient.type = chat.type; lastPatient.msg_id = chat.msg_id; data.chats.push(lastPatient); } modelUtil.emitData(self.eventEmitter, data); }); }); }); } /** * 患者查询查看更多1.患者 2.内容 3.群组 * @param userId * @param userRole * @param keyword * @param type */ searchAboutPatientAll(userId, userRole, keyword, type) { let self = this; var data = []; if (type == 1) { searchRepo.searchPatients(userId, userRole, keyword, function (err, patients) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search patient on basic information failed", err); return; } for (var i = 0; i < patients.length; ++i) { var patient = patients[i]; data.push({ code: patient.code, name: patient.name, sex: patient.sex, birthday: objectUtil.timestampToLong(patient.birthday), avatar: patient.photo === null ? "" : patient.photo }); } modelUtil.emitData(self.eventEmitter, data); }); } if (type == 3) { searchRepo.searchPatientPM(userId, keyword, function (err, chats) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search patient on private messages failed", err); return; } for (var i = 0; i < chats.length; ++i) { var lastPatient = { code: '', name: '', sex: '', avatar: '', amount: '', content: '', chat: '', type: '' }; var chat = chats[i]; lastPatient.code = chat.code; lastPatient.name = chat.name; lastPatient.sex = chat.sex; lastPatient.birthday = objectUtil.timestampToLong(chat.birthday); lastPatient.avatar = chat.photo === null ? "" : chat.photo; lastPatient.amount = chat.amount; lastPatient.chat = chat.chat; lastPatient.content = chat.content; lastPatient.type = chat.type; data.push(lastPatient); } modelUtil.emitData(self.eventEmitter, data); }); } if (type == 2) { searchRepo.searchGroupPatients(userId, keyword, function (err, groups) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search talk group failed", err); return; } var group = null; for (var i = 0; i < groups.length; ++i) { var t = groups[i]; group = { code: t.code, name: t.name, members: t.con, }; data.push(group); } modelUtil.emitData(self.eventEmitter, data); }); } } /** * 过滤某个聊天组的详细信息 * @param userId * @param keyword * @param groupId * @param type */ searchAboutPatientList(userId, keyword, groupId, type) { let self = this; searchRepo.searchPatientPMList(userId, keyword, groupId, type, function (err, chats) { var data = []; if (err) { modelUtil.emitDbError(self.eventEmitter, "Search searchPatientPMList on private messages failed", err); return; } for (var i = 0; i < chats.length; ++i) { var lastPatient = {code: '', name: '', sex: '', avatar: '', chat: '', content: ''}; var chat = chats[i]; lastPatient.code = chat.code; lastPatient.name = chat.name; lastPatient.sex = chat.sex; lastPatient.birthday = objectUtil.timestampToLong(chat.birthday); lastPatient.avatar = chat.photo === null ? "" : chat.photo; lastPatient.chat = chat.chat; lastPatient.content = chat.content; lastPatient.msg_id = chat.msg_id; data.push(lastPatient); } modelUtil.emitData(self.eventEmitter, data); }) } /** * 搜索医生相关的数据,包括医生信息与相关的聊天记录,包括私信与群信。 */ searchAboutDoctor(userId, keyword) { let self = this; //搜索单对单医生聊天 searchRepo.searchP2Pdoctors(userId, keyword, function (err, doctors) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search doctor on basic information failed", err); return; } var data = {doctors: [], groups: [], content: []}; for (var i = 0; i < doctors.length; ++i) { var doctor = doctors[i]; data.doctors.push({ code: doctor.code, name: doctor.name, hospitalName: doctor.hospital_name, sex: doctor.sex, avatar: doctor.photo === null ? "" : doctor.photo }); } // 搜索讨论组名称及成员名称(讨论组) searchRepo.searchGroupDoctors(userId, keyword, function (err, groups) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search talk group failed", err); return; } var group = null; for (var i = 0; i < groups.length; ++i) { var t = groups[i]; group = { code: t.code, name: t.name, members: t.con, groupType: t.group_type }; data.groups.push(group); } // 搜索医生间的私信 searchRepo.searchDoctorsContent(userId, keyword, function (err, messages) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search doctor private messages failed", err); return; } var message = null; for (var i = 0; i < messages.length; ++i) { var t = messages[i]; message = { code: t.code, name: t.name, amount: t.amount, content: t.content, type: t.type, msg_id: t.msg_id, groupType: t.group_type, avatar: t.photo }; data.content.push(message); } modelUtil.emitData(self.eventEmitter, data); }); }); }); } searchDoctorMore(userId, keyword, type) { let self = this; var data = []; if (type == 1) { searchRepo.searchP2Pdoctors(userId, keyword, function (err, doctors) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search doctor on basic information failed", err); return; } for (var i = 0; i < doctors.length; ++i) { var doctor = doctors[i]; data.push({ code: doctor.code, name: doctor.name, hospitalName: doctor.name, sex: doctor.sex, avatar: doctor.photo === null ? "" : doctor.photo }); } modelUtil.emitData(self.eventEmitter, data); }); } else if (type == 2) { searchRepo.searchGroupDoctors(userId, keyword, function (err, groups) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search talk group failed", err); return; } var group = null; for (var i = 0; i < groups.length; ++i) { var t = groups[i]; group = { code: t.code, name: t.name, members: t.con, groupType: t.group_type }; data.push(group); } modelUtil.emitData(self.eventEmitter, data); }); } else if (type == 3) { searchRepo.searchDoctorsContent(userId, keyword, function (err, messages) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search doctor private messages failed", err); return; } var message = null; for (var i = 0; i < messages.length; ++i) { var t = messages[i]; message = { code: t.code, name: t.name, amount: t.amount, content: t.content, type: t.type, msg_id: t.msg_id, groupType: t.group_type, avatar:t.photo }; data.push(message); } modelUtil.emitData(self.eventEmitter, data); }); } } /** * 搜索医生聊天详情 * @param userId 当前医生ID * @param keyword 关键字 * @param groupcode 群组code * @param type type =1 p2p type = 2群组 */ searchDoctorContentDetail(userId, keyword, groupcode, type) { let self = this; var data = []; searchRepo.searchDoctorsContentDetail(userId, keyword, groupcode, type, function (err, doctors) { if (err) { modelUtil.emitDbError(self.eventEmitter, "Search doctor on basic information failed", err); return; } for (var i = 0; i < doctors.length; ++i) { var doctor = doctors[i]; data.push({ code: doctor.code, name: doctor.name, content: doctor.content, msg_id: doctor.msg_id, avatar: doctor.photo, groupType: doctor.group_type }); } modelUtil.emitData(self.eventEmitter, data); }); } } module.exports = Search;