group.msg.repo.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use strict";
  2. var imRepo = require("./database/im.db.js");
  3. exports.save = function (from, groupId, at, contentType, content, handler) {
  4. imRepo.execQuery({
  5. "sql": "INSERT INTO msg_group (to_gid,from_uid,at_uid,type,content) VALUES (?,?,?,?,?)",
  6. "args": [groupId, from, at, contentType, content],
  7. "handler": handler
  8. });
  9. };
  10. exports.findAllMessages = function (groupId, contentType, start, end, count, handler) {
  11. var sql = "SELECT to_gid, msg_id, from_uid, at_uid, type, content, timestamp " +
  12. "FROM msg_group " +
  13. "WHERE to_gid = ? AND type in(" + contentType + ") AND msg_id BETWEEN ? AND ? GROUP BY timestamp DESC LIMIT ?";
  14. imRepo.execQuery({
  15. "sql": sql,
  16. "args": [groupId, end + 1, start - 1, count],
  17. "handler": handler
  18. });
  19. };
  20. /**
  21. * 查找用户参与的组列表,包括行政与求助。
  22. *
  23. * @param userId 指定的用户
  24. * @param handler
  25. */
  26. exports.findAllGroupsWithDoctor = function (userId, handler) {
  27. var sql = "SELECT DISTINCT g.id code, g.name name, ms.last_content_type, ms.last_content, ms.timestamp, ms.new_msg_count, '1' group_type " +
  28. " FROM wlyy.wlyy_admin_team g, wlyy.wlyy_admin_team_member m, wlyy.wlyy_doctor d, msg_statistic ms ," +
  29. " ( SELECT new_msg_count, from_gid FROM msg_statistic WHERE uid =? GROUP BY from_gid ORDER BY `timestamp` DESC ) msgcount " +
  30. " WHERE d.code = ? AND d.code = m.doctor_code AND msgcount.from_gid=ms.from_gid AND m.team_id = g.id AND g.id = ms.from_gid AND ms.last_content_type IN (1,2,3,5,6) GROUP BY g.id , g.name " +
  31. " UNION " +
  32. " SELECT m.group_code, m.group_name, ms.last_content_type, ms.last_content, ms.timestamp, ms.new_msg_count, '2' group_type " +
  33. " FROM wlyy.wlyy_talk_group g, wlyy.wlyy_talk_group_member m, wlyy.wlyy_doctor d, msg_statistic ms, " +
  34. " ( SELECT new_msg_count, from_gid FROM msg_statistic WHERE uid =? GROUP BY from_gid ORDER BY `timestamp` DESC ) msgcount " +
  35. " WHERE d.code = ? AND d.code = m.member_code AND msgcount.from_gid=ms.from_gid AND m.group_code = g.code AND g.type=2 AND g.code = ms.from_gid group by m.group_code, m.group_name";
  36. imRepo.execQuery({
  37. "sql": sql,
  38. "args": [userId, userId,userId,userId],
  39. "handler": handler
  40. });
  41. };
  42. /**
  43. * 查找用户参与的讨论组,且含有患者的讨论组。直接从wlyy_talk_group中查找即可。
  44. *
  45. * @param userId
  46. * @param handler
  47. */
  48. exports.findAllGroupsWithPatient = function (userId, handler) {
  49. var sql = "SELECT g.code, g.name, '', '', '', ms.last_content_type, ms.last_content, ms.timestamp, msgcount.new_msg_count, g.type group_type " +
  50. " FROM msg_statistic ms, (" +
  51. " SELECT g.code code, g.name name, g.type type FROM wlyy.wlyy_talk_group g, wlyy.wlyy_talk_group_member m " +
  52. " WHERE g.code = m.group_code and m.member_code = ?) g, wlyy.wlyy_patient p , " +
  53. " ( SELECT new_msg_count, from_gid FROM msg_statistic WHERE uid =? GROUP BY from_gid ORDER BY `timestamp` DESC ) msgcount " +
  54. " WHERE ((ms.uid = ? and ms.from_uid = p.code) OR (ms.uid = p.code and ms.from_uid = ?)) " +
  55. " and ms.from_gid = g.code and ms.msg_type = 2 AND ms.last_content_type in (1,2,3,5,6) AND msgcount.from_gid=ms.from_gid " +
  56. " UNION " +
  57. " SELECT m.group_code code, m.group_name name, '', '', '', ms.last_content_type, ms.last_content, ms.timestamp, ms.new_msg_count, '2' group_type " +
  58. " FROM wlyy.wlyy_talk_group g, wlyy.wlyy_talk_group_member m, wlyy.wlyy_doctor d, msg_statistic ms, " +
  59. " ( SELECT new_msg_count, from_gid FROM msg_statistic WHERE uid =? GROUP BY from_gid ORDER BY `timestamp` DESC ) msgcount " +
  60. " WHERE d.code = ? AND d.code = m.member_code AND msgcount.from_gid=ms.from_gid AND m.group_code = g.code AND g.type=1 AND g.code = ms.from_gid group by m.group_code, m.group_name";
  61. imRepo.execQuery({
  62. "sql": sql,
  63. "args": [userId, userId, userId,userId,userId,userId],
  64. "handler": handler
  65. });
  66. };
  67. /**
  68. * 查找指定的消息。
  69. *
  70. * @param messageId
  71. * @param handler
  72. */
  73. exports.findOneMessage = function (messageId, handler) {
  74. var sql = "SELECT to_gid, msg_id, from_uid, at_uid, type, content, timestamp " +
  75. "FROM msg_group " +
  76. "WHERE msg_id = ?";
  77. imRepo.execQuery({
  78. "sql": sql,
  79. "args": [messageId],
  80. "handler": handler
  81. });
  82. };
  83. /**
  84. * 查找未读消息。
  85. *
  86. * @param groupId
  87. * @param start
  88. * @param count
  89. * @param handler
  90. */
  91. exports.findUnread = function (groupId, start, count, handler) {
  92. var sql = "SELECT msg_id, to_gid, from_uid, at_uid, type, content, timestamp " +
  93. "FROM msg_group " +
  94. "WHERE to_gid = ? AND msg_id < ? ORDER BY timestamp DESC LIMIT ?";
  95. imRepo.execQuery({
  96. "sql": sql,
  97. "args": [groupId, start, count],
  98. "handler": handler
  99. });
  100. };