private.msg.repo.js 6.0 KB

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