msg.private.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. "use strict";
  2. var imRepo = require("../repository/im.repo");
  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. /**
  27. * 查找所有消息。
  28. *
  29. * @param to
  30. * @param from
  31. * @param contentType
  32. * @param start
  33. * @param end
  34. * @param count
  35. * @param closedInterval
  36. * @param handler
  37. */
  38. exports.findAllMessages = function (to, from, contentType, start, end, count, closedInterval, handler) {
  39. var sql = "SELECT msg_id, to_uid, from_uid, type, content, timestamp from msg_p2p " +
  40. "WHERE ((to_uid=? AND from_uid=?) OR (to_uid=? AND from_uid=?)) " +
  41. " AND type in (" + contentType + ") AND msg_id between ? and ? ORDER BY msg_id DESC LIMIT ?";
  42. imRepo.execQuery({
  43. "sql": sql,
  44. "args": [to, from, from, to, closedInterval ? end : end + 1, closedInterval ? start : start - 1, count],
  45. "handler": handler
  46. });
  47. };
  48. /**
  49. * 查找用户聊天过的医生列表。
  50. *
  51. * @param userId 指定的用户
  52. * @param handler
  53. */
  54. exports.findAllP2PWithDoctor = function (userId, handler) {
  55. 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 " +
  56. "FROM (SELECT DISTINCT CASE WHEN ms1.timestamp > ms2.timestamp THEN ms1.id ELSE ms2.id END id " +
  57. " FROM msg_statistic ms1, msg_statistic ms2 " +
  58. " WHERE ms1.from_gid IS NULL AND ms2.from_gid IS NULL " +
  59. " AND ms1.uid = ms2.peer_uid AND ms1.peer_uid = ms2.uid) x, msg_statistic ms3, wlyy.wlyy_doctor d " +
  60. "WHERE x.id = ms3.id AND ms3.last_content_type in (1,2,3,5,6) AND " +
  61. "((ms3.uid = d.code AND ms3.peer_uid = ?) OR (ms3.uid = ? AND ms3.peer_uid = d.code)) GROUP BY d.code, d.name ORDER BY d.code";
  62. imRepo.execQuery({
  63. "sql": sql,
  64. "args": [userId, userId],
  65. "handler": handler
  66. });
  67. };
  68. /**
  69. * 查找用户聊天过的患者列表。
  70. *
  71. * @param userId
  72. * @param handler
  73. */
  74. exports.findAllP2PWithPatient = function (userId, handler) {
  75. 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 " +
  76. "FROM msg_statistic ms, wlyy.wlyy_patient p " +
  77. "WHERE ms.msg_type = 1 AND ms.last_content_type in (1,2,3,5,6) AND (ms.from_uid = ? AND ms.uid = p.code) OR (ms.uid = ? AND ms.from_uid = p.code)";
  78. imRepo.execQuery({
  79. "sql": sql,
  80. "args": [userId, userId],
  81. "handler": handler
  82. });
  83. };
  84. /**
  85. * 查找未读消息。
  86. *
  87. * @param from
  88. * @param to
  89. * @param start
  90. * @param count
  91. * @param handler
  92. */
  93. exports.findUnread = function(from, to, start, count, handler) {
  94. var sql = "SELECT msg_id, to_uid, from_uid, type, content, timestamp from msg_p2p " +
  95. "WHERE from_uid = ? AND to_uid = ? AND msg_id < ? ORDER BY timestamp DESC LIMIT ?";
  96. imRepo.execQuery({
  97. "sql": sql,
  98. "args": [from, to, start, count],
  99. "handler": handler
  100. });
  101. };
  102. exports.isCurrentSessionFinished = function (userId, peerId, handler) {
  103. var sql = "SELECT c.consult consult_id, case when c.end_msg_id is not null then 1 else 0 end finished " +
  104. "FROM msg_p2p pm, wlyy.wlyy_consult_team c " +
  105. "WHERE ((pm.from_uid = ? AND pm.to_uid = ?) OR (pm.from_uid = ? AND pm.to_uid = ?)) " +
  106. "AND c.start_msg_id = pm.msg_id ORDER BY start_msg_id desc limit 1";
  107. imRepo.execQuery({
  108. "sql": sql,
  109. "args": [userId, peerId, peerId, userId],
  110. "handler": handler
  111. });
  112. };