doctor.js 28 KB

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