doctor.js 27 KB

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