group.msg.repo.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 ? ORDER BY timestamp,msg_id 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 * FROM (" +
  28. // " SELECT DISTINCT g.id code, g.name name, ms.last_content_type, ms.last_content, ms.timestamp, ms.new_msg_count, '1' group_type " +
  29. // " FROM ydf_doctor_team g, ydf_doctor_team_member m, wlyy_doctor d, msg_statistic ms ," +
  30. // " ( SELECT new_msg_count, from_gid FROM msg_statistic WHERE uid =? GROUP BY from_gid ORDER BY `timestamp` DESC ) msgcount " +
  31. // " 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 " +
  32. // " UNION " +
  33. // " SELECT m.group_code, m.group_name, ms.last_content_type, ms.last_content, ms.timestamp, ms.new_msg_count, '2' group_type " +
  34. // " FROM wlyy_talk_group g, wlyy_talk_group_member m, wlyy_doctor d, msg_statistic ms, " +
  35. // " ( SELECT new_msg_count, from_gid FROM msg_statistic WHERE uid =? GROUP BY from_gid ORDER BY `timestamp` DESC ) msgcount " +
  36. // " 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" +
  37. // ") X ORDER BY timestamp DESC";
  38. var sql="SELECT * FROM ( select g.id code, g. NAME name, ms.last_content_type, ms.last_content, ms.TIMESTAMP AS timestamp, ms.new_msg_count, '1' as group_type FROM ydf_doctor_team g, ydf_doctor_team_member m, ydf_doctor d, msg_statistic ms WHERE d. id = ? AND d. id = m.doctor_code AND m.team_id = g.id AND g.id = ms.from_gid AND ms.uid = ? AND ms.last_content_type IN (1, 2, 3, 5, 6,7) GROUP BY g.id, g. NAME ) X ORDER BY TIMESTAMP DESC;";
  39. imRepo.execQuery({
  40. "sql": sql,
  41. "args": [userId, userId,userId, userId],
  42. "handler": handler
  43. });
  44. };
  45. /**
  46. * 查找指定的消息。
  47. *
  48. * @param messageId
  49. * @param handler
  50. */
  51. exports.findOneMessage = function (messageId, handler) {
  52. var sql = "SELECT to_gid, msg_id, from_uid, at_uid, type, content, timestamp " +
  53. "FROM msg_group " +
  54. "WHERE msg_id = ?";
  55. imRepo.execQuery({
  56. "sql": sql,
  57. "args": [messageId],
  58. "handler": handler
  59. });
  60. };
  61. /**
  62. * 查找未读消息。
  63. *
  64. * @param groupId
  65. * @param start
  66. * @param count
  67. * @param handler
  68. */
  69. exports.findUnread = function (groupId, start, count, handler) {
  70. var sql = "SELECT msg_id, to_gid, from_uid, at_uid, type, content, timestamp " +
  71. "FROM msg_group " +
  72. "WHERE to_gid = ? AND msg_id < ? ORDER BY timestamp DESC LIMIT ?";
  73. imRepo.execQuery({
  74. "sql": sql,
  75. "args": [groupId, start, count],
  76. "handler": handler
  77. });
  78. };