search.js 13 KB

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