search.js 15 KB

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