doctor.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /**
  2. * 医生模型。
  3. */
  4. "use strict";
  5. let log = require("../../util/log.js");
  6. let getui = require('getui');
  7. let BaseModel = require('./../base.model');
  8. let Schedule = require("./../schedule/schedule.js");
  9. let doctorRepo = require('../../repository/mysql/doctor.repo.js');
  10. let gmRepo = require('../../repository/mysql/group.msg.repo');
  11. let pmRepo = require('../../repository/mysql/private.msg.repo');
  12. let nmRepo = require("../../repository/mysql/notify.msg.repo");
  13. let smRepo = require("../../repository/mysql/system.msg.repo.js");
  14. let statsRepo = require("../../repository/mysql/stats.msg.repo");
  15. let objectUtil = require("../../util/objectUtil.js");
  16. let modelUtil = require('../../util/modelUtil');
  17. const CONTENT_TYPES = require('../../include/commons').CONTENT_TYPE;
  18. const PLATFORMS = require('../../include/commons').PLATFORM;
  19. const MAX_INT = require('../../include/commons').MAX_INT;
  20. class Doctor extends BaseModel {
  21. constructor() {
  22. super();
  23. this.$id = 'doctor';
  24. }
  25. /**
  26. * 向医生发送消息。
  27. *
  28. * @param message
  29. */
  30. sendMessage(message) {
  31. let self = this;
  32. let tempContent = message.contentType === CONTENT_TYPES.Article ? JSON.stringify(message.content) : message.content;
  33. pmRepo.save(message.to, message.from, message.contentType, tempContent, function (err, result) {
  34. if (err) {
  35. modelUtil.emitDbError(self.eventEmitter, 'Save private message failed', err);
  36. return;
  37. }
  38. // 返回新插入的消息数据,并推送
  39. pmRepo.findOneMessage(result.insertId, function (err, msg) {
  40. if (err) {
  41. modelUtil.emitDbError(self.eventEmitter, 'Save private message success, but return last message failed', err);
  42. return;
  43. }
  44. // 先结束网络连接,再推送给客户端
  45. modelUtil.emitData(self.eventEmitter, Doctor.fillMessages(msg));
  46. Doctor.pushMessage(message, 'p2p_msg');
  47. });
  48. // 更新自身的聊天统计信息
  49. statsRepo.updatePrivateChatSummary(message.from, message.to, message.from, message.contentType, message.content, function (err, result) {
  50. if (err) log.error(err);
  51. });
  52. // 更新对端的聊天统计信息
  53. statsRepo.updatePrivateChatSummary(message.to, message.from, message.from, message.contentType, message.content, function (err, result) {
  54. if (err) log.error(err);
  55. });
  56. });
  57. }
  58. /**
  59. * 向医生发送系统消息。
  60. *
  61. * @param message
  62. */
  63. sendSystemMessage(message) {
  64. let self = this;
  65. smRepo.save(message.to,
  66. message.contentType,
  67. message.title,
  68. message.summary,
  69. message.content,
  70. function (err, result) {
  71. if (err) {
  72. modelUtil.emitDbError(self.eventEmitter, "Save system message failed", err);
  73. return;
  74. }
  75. // 先结束网络连接,再推送给客户端
  76. modelUtil.emitData(self.eventEmitter, {});
  77. Doctor.pushMessage(message, 'system_msg');
  78. });
  79. }
  80. /**
  81. * 推送消息。
  82. *
  83. * @param message
  84. * @param channel
  85. */
  86. static pushMessage(message, channel){
  87. doctorRepo.getUserStatus(message.to, function (err, result) {
  88. if (err) {
  89. log.error('Lookup notify message receiver failed: ' + message.to);
  90. return;
  91. }
  92. if (result.length == 0) {
  93. log.warn('Notify message receiver is not found: ', message.to);
  94. return;
  95. }
  96. let userStatus = result[0];
  97. let isOnline = result.length > 0 && userStatus.is_online === 1;
  98. let delay = null;
  99. // 构建通知消息
  100. let notifyMessage = {type: channel, data: message.content};
  101. if (message.from) notifyMessage.from_uid = message.from;
  102. if (message.gid) notifyMessage.gid = message.gid;
  103. if (message.delay && message.delay !== "null") delay = new Date(Date.parse(message.delay));
  104. let title = '新消息';
  105. let content = message.content;
  106. if (message.contentType === CONTENT_TYPES.Image) {
  107. content = '[图片]';
  108. } else if (message.contentType === CONTENT_TYPES.Audio) {
  109. content = '[语音]';
  110. } else if (message.contentType > 3) {
  111. content = '您有一条新消息';
  112. }
  113. // 保存通知消息到数据库中,并根据用户在线状态推送此消息
  114. nmRepo.save(message.to, message.contentType, title, content, JSON.stringify(notifyMessage), isOnline, delay, function (err, result) {
  115. if (err) {
  116. log.error('Save notify message failed, ', err);
  117. return;
  118. }
  119. if (delay) {
  120. Schedule.dateSchedule(delay, function (message, client_id, status, token, title, content, notifyMessage, platform) {
  121. Doctor.pushToClient(message.to, client_id, status, token, message.contentType,
  122. title, content, notifyMessage, platform, function (err, result) {
  123. if (err != null) {
  124. log.error(err);
  125. } else {
  126. log.info(result);
  127. }
  128. });
  129. }.bind(null, message, userStatus.client_id, userStatus.status, userStatus.token, title, content, notifyMessage, userStatus.platform));
  130. } else if (isOnline) {
  131. Doctor.pushToClient(message.to, userStatus.client_id, userStatus.status, userStatus.token, message.contentType,
  132. title, content, notifyMessage, userStatus.platform, function (err, result) {
  133. if (err != null) {
  134. log.error(err);
  135. } else {
  136. log.info(result);
  137. }
  138. });
  139. }
  140. });
  141. });
  142. }
  143. /**
  144. * 推送消息给医生客户端。
  145. *
  146. * @param userId 用户ID
  147. * @param clientId 客户端设备ID
  148. * @param appStatus 客户端App状态
  149. * @param token
  150. * @param contentType
  151. * @param title
  152. * @param content
  153. * @param notifyMessage
  154. * @param platform
  155. * @param handler
  156. */
  157. static pushToClient(userId, clientId, appStatus, token, contentType, title, content, notifyMessage, platform, handler) {
  158. if (platform === PLATFORMS.iOS) {
  159. getui.pushAPN(userId, token, contentType, title, content, notifyMessage, handler);
  160. } else if (platform === PLATFORMS.Android) {
  161. getui.pushAndroid(clientId, contentType, title, content, notifyMessage, appStatus, handler);
  162. }
  163. }
  164. /**
  165. * 获取最近聊天的用户,组。
  166. */
  167. getRecentChatList(userId, days) {
  168. let self = this;
  169. statsRepo.getRecentChats(userId, days, function (err, rows) {
  170. if (err) {
  171. modelUtil.emitDbError(self.eventEmitter, 'Get recent chat objects failed', err);
  172. return;
  173. }
  174. let data = {patients: [], doctors: [], groups: []};
  175. if (rows.length > 0) {
  176. for (let i = 0; i < rows.length; ++i) {
  177. let row = rows[i];
  178. if (row.type.indexOf('patient') > -1) {
  179. data.patients.push({
  180. code: row.code,
  181. name: row.name,
  182. birthday: row.birthday === null ? "" : row.birthday,
  183. sex: row.sex,
  184. avatar: row.photo === null ? "" : row.photo
  185. });
  186. } else if (row.type.indexOf('doctor') > -1) {
  187. data.doctors.push({
  188. code: row.code,
  189. name: row.name,
  190. birthday: row.birthday === null ? "" : row.birthday,
  191. sex: row.sex,
  192. avatar: row.photo === null ? "" : row.photo
  193. });
  194. } else if (row.type.indexOf('group') > -1) {
  195. data.groups.push({
  196. code: row.code,
  197. name: row.name
  198. });
  199. }
  200. }
  201. }
  202. modelUtil.emitData(self.eventEmitter, data);
  203. });
  204. }
  205. /**
  206. * 获取参与的聊天列表,包括:点对点,参与的讨论组,系统消息等。
  207. *
  208. * @param userId
  209. */
  210. getChatList(userId) {
  211. let self = this;
  212. // 与患者的私信
  213. pmRepo.findAllP2PWithPatient(userId, function (err, patients) {
  214. if (err) {
  215. modelUtil.emitDbError(self.eventEmitter, 'Get chat list with patient failed', err);
  216. return;
  217. }
  218. let chats = {patients: [], doctors: [], groups: []};
  219. for (let i = 0; i < patients.length; i++) {
  220. let patient = patients[i];
  221. chats.patients.push({
  222. code: patient.code,
  223. name: patient.name,
  224. birthday: patient.birthday,
  225. sex: patient.sex,
  226. avatar: patient.photo == null ? "" : patient.photo,
  227. newMessageCount: patient.new_msg_count,
  228. lastContentType: patient.last_content_type,
  229. lastContent: patient.last_content,
  230. timestamp: objectUtil.timestampToLong(patient.timestamp)
  231. });
  232. }
  233. // 含有患者的群
  234. gmRepo.findAllGroupsWithPatient(userId, function (err, groups) {
  235. if (err) {
  236. modelUtil.emitDbError(self.eventEmitter, 'Get group list with patient failed', err);
  237. return;
  238. }
  239. for (let i = 0; i < groups.length; i++) {
  240. let group = groups[i];
  241. // 过滤掉医生间的求助团队
  242. if (group.group_type === 2) continue;
  243. chats.groups.push({
  244. code: group.code,
  245. name: group.name,
  246. groupType: group.msg_type,
  247. newMessageCount: group.new_msg_count,
  248. lastContentType: group.last_content_type,
  249. lastContent: group.last_content,
  250. timestamp: objectUtil.timestampToLong(group.timestamp)
  251. });
  252. }
  253. // 医生间的私聊
  254. pmRepo.findAllP2PWithDoctor(userId, function (err, doctors) {
  255. if (err) {
  256. modelUtil.emitDbError(self.eventEmitter, 'Get chat list with doctor failed', err);
  257. return;
  258. }
  259. for (let i = 0; i < doctors.length; i++) {
  260. let doctor = doctors[i];
  261. chats.doctors.push({
  262. code: doctor.code,
  263. name: doctor.name,
  264. sex: doctor.sex,
  265. avatar: doctor.photo === null ? "" : doctor.photo,
  266. newMessageCount: doctor.new_msg_count,
  267. lastContentType: doctor.last_content_type,
  268. lastContent: doctor.last_content,
  269. timestamp: objectUtil.timestampToLong(doctor.timestamp)
  270. });
  271. }
  272. // 获取医生间的组
  273. gmRepo.findAllGroupsWithDoctor(userId, function (err, groups) {
  274. if (err) {
  275. modelUtil.emitDbError(self.eventEmitter, 'Get group list with doctor failed', err);
  276. return;
  277. }
  278. for (let i = 0; i < groups.length; i++) {
  279. let group = groups[i];
  280. chats.groups.push({
  281. code: group.code,
  282. name: group.name,
  283. groupType: group.group_type, // 行政团队 or 求助
  284. newMessageCount: group.new_msg_count,
  285. lastContentType: group.last_content_type,
  286. lastContent: group.last_content,
  287. timestamp: objectUtil.timestampToLong(group.timestamp)
  288. });
  289. }
  290. modelUtil.emitData(self.eventEmitter, chats);
  291. });
  292. });
  293. })
  294. });
  295. }
  296. /**
  297. * 获取与患者的聊天列表。
  298. */
  299. getChatsListWithPatient(userId) {
  300. let self = this;
  301. pmRepo.findAllP2PWithPatient(userId, function (err, patients) {
  302. if (err) {
  303. modelUtil.emitDbError(self.eventEmitter, 'Get chat list with patient failed', err);
  304. return;
  305. }
  306. let chats = {patients: [], groups: []};
  307. for (let i = 0; i < patients.length; i++) {
  308. let patient = patients[i];
  309. chats.patients.push({
  310. code: patient.code,
  311. name: patient.name,
  312. birthday: patient.birthday,
  313. sex: patient.sex,
  314. avatar: patient.photo == null ? "" : patient.photo,
  315. newMessageCount: patient.new_msg_count,
  316. lastContentType: patient.last_content_type,
  317. lastContent: patient.last_content,
  318. timestamp: objectUtil.timestampToLong(patient.timestamp)
  319. });
  320. }
  321. gmRepo.findAllGroupsWithPatient(userId, function (err, groups) {
  322. if (err) {
  323. modelUtil.emitDbError(self.eventEmitter, 'Get group list with patient failed', err);
  324. return;
  325. }
  326. for (let i = 0; i < groups.length; i++) {
  327. let group = groups[i];
  328. // 过滤掉医生间的求助团队
  329. if (group.group_type === 2) continue;
  330. chats.groups.push({
  331. code: group.code,
  332. name: group.name,
  333. groupType: group.msg_type,
  334. newMessageCount: group.new_msg_count,
  335. lastContentType: group.last_content_type,
  336. lastContent: group.last_content,
  337. timestamp: objectUtil.timestampToLong(group.timestamp)
  338. });
  339. }
  340. modelUtil.emitData(self.eventEmitter, chats);
  341. })
  342. });
  343. }
  344. /**
  345. * 获取与医生的聊天列表,包括:点对点,参与的讨论组。
  346. *
  347. * @param userId
  348. */
  349. getChatListWithDoctor(userId) {
  350. let self = this;
  351. // 先获取医生间的私聊
  352. pmRepo.findAllP2PWithDoctor(userId, function (err, doctors) {
  353. if (err) {
  354. modelUtil.emitDbError(self.eventEmitter, 'Get chat list with doctor failed', err);
  355. return;
  356. }
  357. let chats = {doctors: [], groups: []};
  358. for (let i = 0; i < doctors.length; i++) {
  359. let doctor = doctors[i];
  360. chats.doctors.push({
  361. code: doctor.code,
  362. name: doctor.name,
  363. sex: doctor.sex,
  364. avatar: doctor.photo === null ? "" : doctor.photo,
  365. newMessageCount: doctor.new_msg_count,
  366. lastContentType: doctor.last_content_type,
  367. lastContent: doctor.last_content,
  368. timestamp: objectUtil.timestampToLong(doctor.timestamp)
  369. });
  370. }
  371. // 再获取医生间的组
  372. gmRepo.findAllGroupsWithDoctor(userId, function (err, groups) {
  373. if (err) {
  374. modelUtil.emitDbError(self.eventEmitter, 'Get group list with doctor failed', err);
  375. return;
  376. }
  377. for (let i = 0; i < groups.length; i++) {
  378. let group = groups[i];
  379. chats.groups.push({
  380. code: group.code,
  381. name: group.name,
  382. groupType: group.group_type, // 行政团队 or 求助
  383. newMessageCount: group.new_msg_count,
  384. lastContentType: group.last_content_type,
  385. lastContent: group.last_content,
  386. timestamp: objectUtil.timestampToLong(group.timestamp)
  387. });
  388. }
  389. modelUtil.emitData(self.eventEmitter, chats);
  390. });
  391. });
  392. }
  393. /**
  394. * 获取与医生,患者的聊天列表,包括:点对点,参与的讨论组。消息数量
  395. *
  396. * @param userId
  397. */
  398. getChatListMsgAmount(userId) {
  399. let self = this;
  400. let chats = {doctor: {}, patient: {}};
  401. // 先获取医生间的私聊
  402. pmRepo.findAllP2PWithDoctor(userId, function (err, doctors) {
  403. if (err) {
  404. modelUtil.emitDbError(self.eventEmitter, 'Get chat list with doctor failed', err);
  405. return;
  406. }
  407. var amount = 0;
  408. for (let i = 0; i < doctors.length; i++) {
  409. let doctor = doctors[i];
  410. //过滤结束的咨询
  411. //if(doctor.last_content_type==7)continue;
  412. amount = doctor.new_msg_count+amount;
  413. }
  414. // 再获取医生间的组
  415. gmRepo.findAllGroupsWithDoctor(userId, function (err, groups) {
  416. if (err) {
  417. modelUtil.emitDbError(self.eventEmitter, 'Get group list with doctor failed', err);
  418. return;
  419. }
  420. for (let i = 0; i < groups.length; i++) {
  421. let group = groups[i];
  422. //过滤结束的咨询
  423. //if(group.last_content_type==7)continue;
  424. amount = group.new_msg_count+amount;
  425. }
  426. chats.doctor = amount;
  427. var patientAmount =0;
  428. //获取患者记录数量
  429. pmRepo.findAllP2PWithPatient(userId, function (err, patients) {
  430. if (err) {
  431. modelUtil.emitDbError(self.eventEmitter, 'Get chat list with patient failed', err);
  432. return;
  433. }
  434. for (let i = 0; i < patients.length; i++) {
  435. let patient = patients[i];
  436. //过滤结束的咨询
  437. // if(patient.last_content_type==7)continue;
  438. patientAmount =patientAmount+ patient.new_msg_count;
  439. }
  440. //获取患者记录数量
  441. gmRepo.findAllGroupsWithPatient(userId, function (err, groups) {
  442. if (err) {
  443. modelUtil.emitDbError(self.eventEmitter, 'Get group list with patient failed', err);
  444. return;
  445. }
  446. for (let i = 0; i < groups.length; i++) {
  447. let group = groups[i];
  448. // 过滤掉医生间的求助团队
  449. if (group.group_type === 2) continue;
  450. //过滤结束的咨询
  451. //if(group.last_content_type==7)continue;
  452. patientAmount = patientAmount+ group.new_msg_count;
  453. }
  454. chats.patient = patientAmount;
  455. modelUtil.emitData(self.eventEmitter, chats);
  456. });
  457. });
  458. });
  459. });
  460. }
  461. /**
  462. * 获取与指定用户的聊天记录。
  463. *
  464. * @param userId
  465. * @param peerId
  466. * @param contentType
  467. * @param msgStartId
  468. * @param msgEndId
  469. * @param count
  470. * @param closedInterval
  471. */
  472. getPrivateMessages(userId, peerId, contentType, msgStartId, msgEndId, count, closedInterval) {
  473. let self = this;
  474. pmRepo.findAllMessages(userId, peerId, contentType === undefined ? "0,1,2,3,4,5,6" : contentType, msgStartId, msgEndId, count, closedInterval, function (err, rows) {
  475. if (err) {
  476. modelUtil.emitDbError(self.eventEmitter, 'Get private message failed', err);
  477. return;
  478. }
  479. let messages = Doctor.fillMessages(rows);
  480. modelUtil.emitData(self.eventEmitter, messages);
  481. // 清空统计信息
  482. statsRepo.clearPrivateChatSummary(userId, peerId, function (err, result) {
  483. if (err) log.error(err);
  484. });
  485. });
  486. }
  487. /**
  488. * 获取与某人聊天的未读消息数。
  489. *
  490. * @param userId
  491. * @param peerId
  492. */
  493. getUnreadMessageCount(userId, peerId) {
  494. let self = this;
  495. statsRepo.getPrivateChatAllUnReadCount(userId, function (err, result) {
  496. if (err) {
  497. modelUtil.emitDbError(self.eventEmitter, "Get unread private message count failed", err);
  498. return;
  499. }
  500. let data = {userId: userId, messageType: 1, newMessageCount: 0};
  501. for (let i = 0; i < result.length; i++) {
  502. data.newMessageCount += result[i].new_msg_count;
  503. }
  504. modelUtil.emitData(self.eventEmitter, data);
  505. });
  506. }
  507. /**
  508. * 获取所有未读的消息数,包括群。
  509. *
  510. * @param userId
  511. */
  512. getAllUnreadMessageCount(userId) {
  513. let self = this;
  514. statsRepo.getChatAllUnReadCount(userId, function (err, result) {
  515. if (err) {
  516. modelUtil.emitDbError(self.eventEmitter, "Get all unread message count failed", err);
  517. return;
  518. }
  519. let data = {userId: userId, messageType: 0, newMessageCount: 0};
  520. for (let index = 0; index < result.length; index++) {
  521. data.newMessageCount += result[index].new_msg_count;
  522. }
  523. modelUtil.emitData(self.eventEmitter, data);
  524. });
  525. }
  526. /**
  527. * 获取与指定用户的未读聊天记录。
  528. *
  529. * @param userId
  530. * @param peerId
  531. */
  532. getUnreadPrivateMessages(userId, peerId) {
  533. let self = this;
  534. statsRepo.getPrivateChatSummary(userId, peerId, function (err, summary) {
  535. if (err) {
  536. modelUtil.emitDbError(self.eventEmitter, 'Get unread private messages failed', err);
  537. return;
  538. }
  539. // 没有未读消息,直接返回
  540. if (summary.length == 0 || summary[0].new_msg_count === 0) {
  541. modelUtil.emitData(self.eventEmitter, {startId: 0, count: 0, records: []});
  542. return;
  543. }
  544. pmRepo.findUnread(peerId, userId, MAX_INT, summary[0].new_msg_count, function (err, rows) {
  545. if (err) {
  546. modelUtil.emitDbError(self.eventEmitter, "Get unread private messages failed", err);
  547. return;
  548. }
  549. let messages = Doctor.fillMessages(rows);
  550. modelUtil.emitData(self.eventEmitter, messages);
  551. });
  552. });
  553. }
  554. /**
  555. * 获取聊天统计摘要。
  556. *
  557. * @param userId
  558. * @param peerId
  559. */
  560. getChatSummary(userId, peerId) {
  561. let self = this;
  562. statsRepo.getPrivateChatSummary(userId, peerId, function (err, result) {
  563. if (err) {
  564. modelUtil.emitDbError(self.eventEmitter, "Get private messages statistic failed", err);
  565. return;
  566. }
  567. let data = {
  568. userId: userId,
  569. peerId: peerId,
  570. lastCContentType: 1,
  571. lastContent: "",
  572. newMessageCount: 0,
  573. timestamp: 0
  574. };
  575. if (result.length > 0) {
  576. let row = result[0];
  577. data.userId = row.uid;
  578. data.peerId = row.from_uid;
  579. data.lastContentType = row.last_content_type;
  580. data.lastContent = row.last_content;
  581. data.newMessageCount = row.new_msg_count;
  582. data.timestamp = objectUtil.timestampToLong(row.timestamp)
  583. }
  584. modelUtil.emitData(self.eventEmitter, data);
  585. });
  586. }
  587. getMessage(messageId, messageType) {
  588. let self = this;
  589. if (messageType == 1) {
  590. // 私信
  591. pmRepo.findOneMessage(messageId, function (err, result) {
  592. if (err) {
  593. modelUtil.emitDbError(self.eventEmitter, "Get message failed", err);
  594. return;
  595. }
  596. if (result.length == 0) {
  597. modelUtil.emitDataNotFound(self.eventEmitter, "Message not found.");
  598. return;
  599. }
  600. modelUtil.emitData(self.eventEmitter, {
  601. id: result[0].msg_id,
  602. from: result[0].from_uid,
  603. to: result[0].to_uid,
  604. contentType: result[0].type,
  605. content: result[0].content,
  606. timestamp: objectUtil.timestampToLong(result[0].timestamp)
  607. });
  608. })
  609. } else {
  610. // 群信
  611. gmRepo.findOneMessage(messageId, function (err, result) {
  612. if (err) {
  613. modelUtil.emitDbError(self.eventEmitter, "Get message failed", err);
  614. return;
  615. }
  616. if (result.length == 0) {
  617. modelUtil.emitDataNotFound(self.eventEmitter, "Message not found.");
  618. return;
  619. }
  620. modelUtil.emitData(self.eventEmitter, {
  621. id: result[0].msg_id,
  622. from: result[0].from_uid,
  623. at: result[0].at_uid,
  624. groupId: result[0].to_gid,
  625. contentType: result[0].type,
  626. content: result[0].content,
  627. timestamp: objectUtil.timestampToLong(result[0].timestamp)
  628. });
  629. });
  630. }
  631. }
  632. /**
  633. * 判断与患者的最新咨询会话是否已经结束。
  634. */
  635. isConsultFinished(doctorId, patientId) {
  636. let self = this;
  637. pmRepo.isCurrentSessionFinished(doctorId, patientId, function (err, result) {
  638. if (err) {
  639. modelUtil.emitDbError(self.eventEmitter, "Get session finish status failed: ", err);
  640. return;
  641. }
  642. let data = {finished: true, consultId: ''};
  643. if (result.length > 0) {
  644. let finishRow = result[0];
  645. data.finished = finishRow.finished === 1;
  646. if (!data.finished) {
  647. data.consultId = finishRow.consult_id;
  648. }
  649. }
  650. modelUtil.emitData(self.eventEmitter, data);
  651. })
  652. }
  653. /**
  654. * 将消息的返回结果合并成JSON。
  655. *
  656. * @param rows
  657. *
  658. * @returns {startId: 0, count: 0, records: []}
  659. */
  660. static fillMessages(rows) {
  661. let messages = {startId: rows.length > 0 ? rows[0].msg_id : '', count: rows.length, records: []};
  662. for (let i = 0; i < rows.length; i++) {
  663. let row = rows[i];
  664. let record = {
  665. id: row.msg_id,
  666. from: row.from_uid,
  667. contentType: row.type,
  668. content: row.content,
  669. timestamp: objectUtil.timestampToLong(row.timestamp)
  670. };
  671. if (row.to_uid !== undefined) record.to = row.to_uid;
  672. if (row.at_uid !== undefined) record.at = row.at_uid;
  673. messages.records.push(record);
  674. }
  675. return messages;
  676. }
  677. }
  678. // Expose class
  679. module.exports = Doctor;