private.msg.repo.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * P2P消息库。
  3. */
  4. "use strict";
  5. var ImDb = require("../mysql/db/im.db.js");
  6. class PrivateMsgRepo {
  7. constructor() {
  8. }
  9. /**
  10. * 保存消息。
  11. *
  12. * @param to
  13. * @param from
  14. * @param type
  15. * @param content
  16. * @param handler
  17. */
  18. static save(to, from, type, content, handler) {
  19. ImDb.execQuery({
  20. "sql": "INSERT INTO msg_p2p (to_uid,from_uid,type,content) VALUES (?,?,?,?)",
  21. "args": [to, from, type, content],
  22. "handler": handler
  23. });
  24. };
  25. static findOneMessage(messageId, handler) {
  26. ImDb.execQuery({
  27. "sql": "SELECT msg_id, to_uid, from_uid, type, content, timestamp from msg_p2p where msg_id = ?",
  28. "args": [messageId],
  29. "handler": handler
  30. });
  31. };
  32. static findOnePatientMessage(messageId, handler) {
  33. ImDb.execQuery({
  34. "sql": "SELECT m.*,d.name,d.photo FROM msg_p2p m, wlyy.wlyy_doctor d, wlyy.wlyy_patient p WHERE m.from_uid = d. CODE AND m.to_uid = p. CODE AND m.msg_id =?",
  35. "args": [messageId],
  36. "handler": handler
  37. });
  38. };
  39. /**
  40. * 查找所有消息。
  41. *
  42. * @param to
  43. * @param from
  44. * @param contentType
  45. * @param start
  46. * @param end
  47. * @param count
  48. * @param closedInterval
  49. * @param handler
  50. */
  51. static findAllMessages(to, from, contentType, start, end, count, closedInterval, handler) {
  52. var sql = "SELECT msg_id, to_uid, from_uid, type, content, timestamp from msg_p2p " +
  53. "WHERE ((to_uid=? AND from_uid=?) OR (to_uid=? AND from_uid=?)) " +
  54. " AND type in (" + contentType + ") AND msg_id between ? and ? ORDER BY msg_id DESC LIMIT ?";
  55. ImDb.execQuery({
  56. "sql": sql,
  57. "args": [to, from, from, to, closedInterval ? end : end + 1, closedInterval ? start : start - 1, count],
  58. "handler": handler
  59. });
  60. };
  61. /**
  62. * 查找用户聊天过的医生列表。
  63. *
  64. * @param userId 指定的用户
  65. * @param handler
  66. */
  67. static findAllP2PWithDoctor(userId, handler) {
  68. //var sql = "SELECT DISTINCT d.code, d.name, d.sex, d.photo, ms3.last_content_type, ms3.last_content, ms3.timestamp, ms3.new_msg_count " +
  69. // "FROM (SELECT DISTINCT CASE WHEN ms1.timestamp > ms2.timestamp THEN ms1.id ELSE ms2.id END id " +
  70. // " FROM msg_statistic ms1, msg_statistic ms2 " +
  71. // " WHERE ms1.from_gid IS NULL AND ms2.from_gid IS NULL " +
  72. // " AND ms1.uid = ms2.peer_uid AND ms1.peer_uid = ms2.uid) x, msg_statistic ms3, wlyy.wlyy_doctor d " +
  73. // "WHERE x.id = ms3.id AND ms3.last_content_type in (1,2,3,5,6) AND " +
  74. // "(ms3.uid = ? AND ms3.peer_uid = d.code) GROUP BY d.code, d.name ORDER BY ms3.timestamp DESC";
  75. var sql = "SELECT d. CODE as code, d. NAME as name, d.sex, d.photo, s.last_content_type, s.last_content, s. TIMESTAMP as timestamp, s.new_msg_count " +
  76. "FROM msg_p2p p, wlyy.wlyy_doctor d, msg_statistic s " +
  77. "WHERE (( p.from_uid = d. CODE AND p.to_uid = ? ) OR ( p.to_uid = d. CODE AND p.from_uid = ? )) " +
  78. "AND s.from_gid IS NULL AND s.uid = ? " +
  79. "AND s.peer_uid = d. CODE " +
  80. "GROUP BY d. NAME, d. CODE, d.hospital_name, d.job_name, d.sex, d.photo ORDER BY p.msg_id DESC"
  81. ImDb.execQuery({
  82. "sql": sql,
  83. "args": [userId, userId, userId],
  84. "handler": handler
  85. });
  86. };
  87. /**
  88. * 查找用户聊天过的患者列表。
  89. *
  90. * @param userId
  91. * @param handler
  92. */
  93. static findAllP2PWithPatient(userId, handler) {
  94. //var sql = "SELECT p.code, p.name, p.birthday, p.sex, p.photo, ms.last_content_type, ms.last_content, ms.timestamp, ms.new_msg_count " +
  95. // "FROM msg_statistic ms, wlyy.wlyy_patient p " +
  96. // "WHERE ms.msg_type = 1 AND ms.last_content_type in (1,2,3,5,6) " +
  97. // "AND ((ms.from_uid = ? AND ms.uid = p.code) OR (ms.uid = ? AND ms.from_uid = p.code)) ORDER BY ms.timestamp";
  98. var sql = "SELECT p1. CODE as code , p1. NAME as name, p1.birthday, p1.sex, p1.photo, w.last_content, w.last_content_type, w. TIMESTAMP as timestamp, w.new_msg_count " +
  99. "FROM msg_p2p p, wlyy.wlyy_patient p1, msg_statistic w " +
  100. "WHERE (( p.to_uid = p1.`code` AND p.from_uid = ? ) OR ( p.from_uid = p1.`code` AND p.to_uid = ? )) " +
  101. "AND w.last_content_type IN (1, 2, 3, 5, 6) AND w.uid = ? AND w.peer_uid = p1.`code` AND w.from_gid IS NULL " +
  102. "GROUP BY p1. CODE, p1. NAME, p1.birthday, p1.sex, p1.photo " +
  103. "union all " +
  104. "SELECT p1. CODE AS code, p1. NAME AS name, p1.birthday, p1.sex, p1.photo, w.last_content, t.type, w. TIMESTAMP AS timestamp, w.new_msg_count " +
  105. "FROM msg_p2p p, wlyy.wlyy_patient p1, msg_statistic w, wlyy.wlyy_consult_team t " +
  106. "WHERE (( p.to_uid = p1.`code` AND p.from_uid = ? ) OR ( p.from_uid = p1.`code` AND p.to_uid = ? )) " +
  107. "AND w.last_content_type = 7 AND t.type = 6 " +
  108. "AND (( t.patient = p.from_uid AND t.doctor = p.to_uid ) OR ( t.patient = p.to_uid AND t.doctor = p.from_uid )) " +
  109. "AND w.uid = ? AND w.peer_uid = p1.`code` AND w.from_gid IS NULL GROUP BY p1. CODE, p1. NAME, p1.birthday, p1.sex, p1.photo;";
  110. ImDb.execQuery({
  111. "sql": sql,
  112. "args": [userId, userId, userId, userId, userId, userId],
  113. "handler": handler
  114. });
  115. };
  116. /**
  117. * 查找未读消息。
  118. *
  119. * @param from
  120. * @param to
  121. * @param start
  122. * @param count
  123. * @param handler
  124. */
  125. static findUnread (from, to, start, count, handler) {
  126. var sql = "SELECT msg_id, to_uid, from_uid, type, content, timestamp from msg_p2p " +
  127. "WHERE from_uid = ? AND to_uid = ? AND msg_id < ? ORDER BY timestamp DESC LIMIT ?";
  128. ImDb.execQuery({
  129. "sql": sql,
  130. "args": [from, to, start, count],
  131. "handler": handler
  132. });
  133. };
  134. static isCurrentSessionFinished(doctorId, patientId, handler) {
  135. var sql = "SELECT c.consult consult_id, case when c.end_msg_id is not null then 1 else 0 end finished " +
  136. "FROM wlyy.wlyy_consult_team c where c.doctor=? and c.patient = ? ORDER BY id DESC LIMIT 1";
  137. ImDb.execQuery({
  138. "sql": sql,
  139. "args": [doctorId, patientId],
  140. "handler": handler
  141. });
  142. };
  143. }
  144. module.exports = PrivateMsgRepo;