doctor.js 29 KB

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