search.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /**
  2. * 患者、讨论组搜索。
  3. *
  4. * 注意:此模型效率堪忧,但为了实现先这样做。更稳妥的方案是使用Solr或Elastic Search
  5. * 为数据提供索引功能,JS使用搜索接口搜索之后再取得对象的ID进行获取,提高效率。
  6. * 后续开发都希望看到这段注释,实现此方案。
  7. *
  8. * author: Sand
  9. * since: 2016.11.20
  10. */
  11. "use strict";
  12. let BaseModel = require('./base.model');
  13. let searchRepo = require('../repository/search.repo');
  14. let modelUtil = require("../util/modelUtil");
  15. let objectUtil = require('../util/objectUtil');
  16. const GROUP_TYPE = require('../include/commons').GROUP_TYPE;
  17. class Search extends BaseModel{
  18. constructor() {
  19. super();
  20. }
  21. /**
  22. * 搜索患者相关的数据,包括患者信息与相关的私信记录。关键词不支持空格拆分。
  23. */
  24. searchAboutPatient(userId, userRole, keyword) {
  25. let self = this;
  26. searchRepo.searchPatients(userId, userRole, keyword, function (err, patients) {
  27. if (err) {
  28. log.error("Search patient on basic information failed: ", err);
  29. res.status(500).send({message: "Search patient on basic information failed."});
  30. return;
  31. }
  32. var data = {patients: [],group:[], chats: []};
  33. for (var i = 0; i < patients.length; ++i) {
  34. var patient = patients[i];
  35. var p ={};
  36. console.log(patient.code);
  37. p= {
  38. code: patient.code,
  39. name: patient.name,
  40. sex: patient.sex,
  41. birthday: objectUtil.timestampToLong(patient.birthday),
  42. avatar: patient.photo === null ? "" : patient.photo
  43. }
  44. data.patients.push(p);
  45. }
  46. searchRepo.searchGroupPatients(userId, keyword, function (err, groups) {
  47. if (err) {
  48. modelUtil.emitDbError(self.eventEmitter, "Search talk group failed", err);
  49. return;
  50. }
  51. var group = null;
  52. for (var i = 0; i < groups.length; ++i) {
  53. var t = groups[i];
  54. group = {
  55. code: t.code,
  56. name: t.name,
  57. members: t.con,
  58. };
  59. data.group.push(group);
  60. }
  61. searchRepo.searchPatientPM(userId, keyword, function (err, chats) {
  62. if (err) {
  63. log.error("Search patient on private messages failed: ", err);
  64. res.status(500).send({message: "Search patient on private messages failed."});
  65. return;
  66. }
  67. for (var i = 0; i < chats.length; ++i) {
  68. var lastPatient = {code: '', name: '', sex: '', avatar: '',amount:'',content:'',chat:'',type:''};
  69. var chat = chats[i];
  70. console.log(JSON.stringify(chat));
  71. lastPatient.code = chat.code;
  72. lastPatient.name = chat.name;
  73. lastPatient.sex = chat.sex;
  74. lastPatient.birthday = objectUtil.timestampToLong(chat.birthday);
  75. lastPatient.avatar = chat.photo === null ? "" : chat.photo;
  76. lastPatient.amount = chat.amount;
  77. lastPatient.chat = chat.chat;
  78. lastPatient.content = chat.content;
  79. lastPatient.type = chat.type;
  80. lastPatient.msg_id =chat.msg_id;
  81. data.chats.push(lastPatient);
  82. }
  83. modelUtil.emitData(self.eventEmitter, data);
  84. });
  85. });
  86. });
  87. }
  88. /**
  89. * 患者查询查看更多1.患者 2.内容 3.群组
  90. * @param userId
  91. * @param userRole
  92. * @param keyword
  93. * @param type
  94. */
  95. searchAboutPatientAll(userId, userRole, keyword,type) {
  96. let self = this;
  97. var data = [];
  98. if(type==1){
  99. searchRepo.searchPatients(userId, userRole, keyword, function (err, patients) {
  100. if (err) {
  101. log.error("Search patient on basic information failed: ", err);
  102. res.status(500).send({message: "Search patient on basic information failed."});
  103. return;
  104. }
  105. for (var i = 0; i < patients.length; ++i) {
  106. var patient = patients[i];
  107. data.push({
  108. code: patient.code,
  109. name: patient.name,
  110. sex: patient.sex,
  111. birthday: objectUtil.timestampToLong(patient.birthday),
  112. avatar: patient.photo === null ? "" : patient.photo
  113. });
  114. }
  115. modelUtil.emitData(self.eventEmitter, data);
  116. });
  117. }
  118. if(type==3){
  119. searchRepo.searchPatientPM(userId, keyword, function (err, chats) {
  120. if (err) {
  121. log.error("Search patient on private messages failed: ", err);
  122. res.status(500).send({message: "Search patient on private messages failed."});
  123. return;
  124. }
  125. for (var i = 0; i < chats.length; ++i) {
  126. var lastPatient = {code: '', name: '', sex: '', avatar: '',amount:'',content:'',chat:'',type:''};
  127. var chat = chats[i];
  128. lastPatient.code = chat.code;
  129. lastPatient.name = chat.name;
  130. lastPatient.sex = chat.sex;
  131. lastPatient.birthday = objectUtil.timestampToLong(chat.birthday);
  132. lastPatient.avatar = chat.photo === null ? "" : chat.photo;
  133. lastPatient.amount = chat.amount;
  134. lastPatient.chat = chat.chat;
  135. lastPatient.content = chat.content;
  136. lastPatient.type = chat.type;
  137. data.push(lastPatient);
  138. }
  139. modelUtil.emitData(self.eventEmitter, data);
  140. });
  141. }
  142. if(type==2){
  143. searchRepo.searchGroupPatients(userId, keyword, function (err, groups) {
  144. if (err) {
  145. modelUtil.emitDbError(self.eventEmitter, "Search talk group failed", err);
  146. return;
  147. }
  148. var group = null;
  149. for (var i = 0; i < groups.length; ++i) {
  150. var t = groups[i];
  151. group = {
  152. code: t.code,
  153. name: t.name,
  154. members: t.con,
  155. };
  156. data.push(group);
  157. }
  158. modelUtil.emitData(self.eventEmitter, data);
  159. });
  160. }
  161. }
  162. /**
  163. * 过滤某个聊天组的详细信息
  164. * @param userId
  165. * @param keyword
  166. * @param groupId
  167. * @param type
  168. */
  169. searchAboutPatientList(userId, keyword,groupId,type){
  170. let self = this;
  171. searchRepo.searchPatientPMList(userId, keyword,groupId,type, function (err, chats) {
  172. var data = [];
  173. if (err) {
  174. log.error("Search searchPatientPMList on private messages failed: ", err);
  175. res.status(500).send({message: "Search patient on private messages failed."});
  176. return;
  177. }
  178. for (var i = 0; i < chats.length; ++i) {
  179. var lastPatient = {code: '', name: '', sex: '', avatar: '',chat:'',content:''};
  180. var chat = chats[i];
  181. lastPatient.code = chat.code;
  182. lastPatient.name = chat.name;
  183. lastPatient.sex = chat.sex;
  184. lastPatient.birthday = objectUtil.timestampToLong(chat.birthday);
  185. lastPatient.avatar = chat.photo === null ? "" : chat.photo;
  186. lastPatient.chat = chat.chat;
  187. lastPatient.content=chat.content;
  188. lastPatient.msg_id=chat.msg_id;
  189. data.push(lastPatient);
  190. }
  191. modelUtil.emitData(self.eventEmitter, data);
  192. })
  193. }
  194. /**
  195. * 搜索医生相关的数据,包括医生信息与相关的聊天记录,包括私信与群信。
  196. */
  197. searchAboutDoctor(userId, keyword) {
  198. let self = this;
  199. //搜索单对单医生聊天
  200. searchRepo.searchP2Pdoctors(userId, keyword, function (err, doctors) {
  201. if (err) {
  202. modelUtil.emitDbError(self.eventEmitter, "Search doctor on basic information failed", err);
  203. return;
  204. }
  205. var data = {doctors: [], groups: [],content:[]};
  206. for (var i = 0; i < doctors.length; ++i) {
  207. var doctor = doctors[i];
  208. data.doctors.push({
  209. code: doctor.code,
  210. name: doctor.name,
  211. hospitalName:doctor.hospital_name,
  212. sex: doctor.sex,
  213. avatar: doctor.photo === null ? "" : doctor.photo
  214. });
  215. }
  216. // 搜索讨论组名称及成员名称(讨论组)
  217. searchRepo.searchGroupDoctors(userId, keyword, function (err, groups) {
  218. if (err) {
  219. modelUtil.emitDbError(self.eventEmitter, "Search talk group failed", err);
  220. return;
  221. }
  222. var group = null;
  223. for (var i = 0; i < groups.length; ++i) {
  224. var t = groups[i];
  225. group = {
  226. code: t.code,
  227. name: t.name,
  228. members: t.con,
  229. groupType:t.group_type
  230. };
  231. data.groups.push(group);
  232. }
  233. // 搜索医生间的私信
  234. searchRepo.searchDoctorsContent(userId, keyword, function (err, messages) {
  235. if (err) {
  236. modelUtil.emitDbError(self.eventEmitter, "Search doctor private messages failed", err);
  237. return;
  238. }
  239. var message = null;
  240. for (var i = 0; i < messages.length; ++i) {
  241. var t = messages[i];
  242. message = {
  243. code: t.code,
  244. name: t.name,
  245. amount: t.amount,
  246. content:t.content,
  247. type:t.type,
  248. msg_id:t.msg_id,
  249. groupType:t.group_type
  250. };
  251. data.content.push(message);
  252. }
  253. modelUtil.emitData(self.eventEmitter, data);
  254. });
  255. });
  256. });
  257. }
  258. searchDoctorMore(userId,keyword,type){
  259. let self = this;
  260. var data = [];
  261. if(type==1){
  262. searchRepo.searchP2Pdoctors(userId, keyword, function (err, doctors) {
  263. if (err) {
  264. modelUtil.emitDbError(self.eventEmitter, "Search doctor on basic information failed", err);
  265. return;
  266. }
  267. for (var i = 0; i < doctors.length; ++i) {
  268. var doctor = doctors[i];
  269. data.push({
  270. code: doctor.code,
  271. name: doctor.name,
  272. hospitalName:doctor.name,
  273. sex: doctor.sex,
  274. avatar: doctor.photo === null ? "" : doctor.photo
  275. });
  276. }
  277. modelUtil.emitData(self.eventEmitter, data);
  278. });
  279. }else if(type==2){
  280. searchRepo.searchGroupDoctors(userId, keyword, function (err, groups) {
  281. if (err) {
  282. modelUtil.emitDbError(self.eventEmitter, "Search talk group failed", err);
  283. return;
  284. }
  285. var group = null;
  286. for (var i = 0; i < groups.length; ++i) {
  287. var t = groups[i];
  288. group = {
  289. code: t.code,
  290. name: t.name,
  291. members: t.con,
  292. groupType:t.group_type
  293. };
  294. data.push(group);
  295. }
  296. modelUtil.emitData(self.eventEmitter, data);
  297. });
  298. }else if(type==3){
  299. searchRepo.searchDoctorsContent(userId, keyword, function (err, messages) {
  300. if (err) {
  301. modelUtil.emitDbError(self.eventEmitter, "Search doctor private messages failed", err);
  302. return;
  303. }
  304. var message = null;
  305. for (var i = 0; i < messages.length; ++i) {
  306. var t = messages[i];
  307. message = {
  308. code: t.code,
  309. name: t.name,
  310. amount: t.amount,
  311. content:t.content,
  312. type:t.type,
  313. msg_id:t.msg_id,
  314. groupType:t.group_type
  315. };
  316. data.push(message);
  317. }
  318. modelUtil.emitData(self.eventEmitter, data);
  319. });
  320. }
  321. }
  322. /**
  323. * 搜索医生聊天详情
  324. * @param userId 当前医生ID
  325. * @param keyword 关键字
  326. * @param groupcode 群组code
  327. * @param type type =1 p2p type = 2群组
  328. */
  329. searchDoctorContentDetail(userId,keyword,groupcode,type){
  330. let self = this;
  331. var data = [];
  332. searchRepo.searchDoctorsContentDetail(userId, keyword,groupcode,type, function (err, doctors) {
  333. if (err) {
  334. modelUtil.emitDbError(self.eventEmitter, "Search doctor on basic information failed", err);
  335. return;
  336. }
  337. for (var i = 0; i < doctors.length; ++i) {
  338. var doctor = doctors[i];
  339. data.push({
  340. code: doctor.code,
  341. name: doctor.name,
  342. content:doctor.content,
  343. msg_id:t.msg_id,
  344. groupType:t.group_type
  345. });
  346. }
  347. modelUtil.emitData(self.eventEmitter, data);
  348. });
  349. }
  350. }
  351. module.exports = Search;