topics.repo.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * 搜索功能。
  3. */
  4. "use strict";
  5. let ImDb = require('../mysql/db/im.db');
  6. let log = require('../../util/log.js');
  7. const DB_TABLES = require('../../include/commons').DB_TABLES;
  8. class TopicRepo {
  9. constructor() {
  10. }
  11. /**
  12. * 查找议题.
  13. *
  14. * @param topicId
  15. * @param handler
  16. */
  17. static findOne(topicId, handler) {
  18. let sql = "select id, session_id, name, create_time, end_by, end_time," +
  19. " start_message_id, end_message_id, description, status from " + DB_TABLES.Topics + " where id = ?";
  20. ImDb.execQuery({
  21. sql: sql,
  22. args: [topicId],
  23. handler: handler
  24. });
  25. }
  26. static findLastTopicStatus(sessionId, handler) {
  27. let sql = "select id from " + DB_TABLES.Topics + " where session_id = ? order by id desc limit 0, 1";
  28. ImDb.execQuery({
  29. sql: sql,
  30. args: [sessionId],
  31. handler: function (err, res) {
  32. if (res.length == 0) {
  33. handler(null, null);
  34. } else {
  35. TopicRepo.findTopicStatus(res[0].id, handler);
  36. }
  37. }
  38. });
  39. }
  40. static findTopicStatus(topicId, handler) {
  41. let sql = "select status from " + DB_TABLES.Topics + " where id = ?";
  42. ImDb.execQuery({
  43. sql: sql,
  44. args: [topicId],
  45. handler: handler
  46. });
  47. }
  48. /**
  49. * 获取会话中的议题。
  50. *
  51. * @param sessionId
  52. * @param handler
  53. */
  54. static findAllBySessionId(sessionId, handler) {
  55. let sql = "select id, session_id, name, create_time, end_by, end_time," +
  56. " start_message_id, end_message_id, description, status from " + DB_TABLES.Topics + " where session_id = ? order by id";
  57. ImDb.execQuery({
  58. sql: sql,
  59. args: [sessionId],
  60. handler: handler
  61. });
  62. }
  63. /**
  64. * 保存议题
  65. *
  66. * @param topicName
  67. * @param topicId
  68. * @param sessionId
  69. * @param messageId
  70. * @param date
  71. * @param description
  72. * @param status
  73. */
  74. static saveTopic(topicName, topicId, sessionId, messageId, date, description, status) {
  75. let sql = "insert into " + DB_TABLES.Topics + " (id,session_id,name,create_time,start_message_id,description,status) VALUES (?,?,?,?,?,?,?)";
  76. ImDb.execQuery({
  77. "sql": sql,
  78. "args": [topicId, sessionId, topicName, date, messageId, description, status],
  79. "handler": function (err, res) {
  80. if (err) {
  81. log.error("saveTopic is fail error: " + err + "messageId:" + messageId);
  82. } else {
  83. log.info("saveTopic is success");
  84. }
  85. }
  86. });
  87. }
  88. /**
  89. * 结束议题
  90. *
  91. * @param topicId
  92. * @param endUser
  93. * @param date
  94. * @param messageId
  95. * @param status
  96. */
  97. static endTopic(topicId, endUser, date, messageId, status) {
  98. let sql = "update " + DB_TABLES.Topics + " set end_by = ?,end_time=?,end_message_id=?,status = ? where id = ?";
  99. ImDb.execQuery({
  100. "sql": sql,
  101. "args": [endUser, date, messageId, status, topicId],
  102. "handler": function (err, res) {
  103. if (err) {
  104. log.error("endTopic is fail error: " + err);
  105. } else {
  106. log.info("endTopic is success");
  107. }
  108. }
  109. });
  110. }
  111. static updateTopics(topicId, jsonValue) {
  112. var values = [];
  113. let sql = "update topics set ";
  114. var key =[];
  115. for(var j in jsonValue){
  116. key.push(j+" = ?");
  117. values.push(jsonValue[j]);
  118. }
  119. sql = sql+key.join(",");
  120. sql = sql + " where id = ?";
  121. values.push(topicId);
  122. ImDb.execQuery({
  123. "sql": sql,
  124. "args": values,
  125. "handler": function (err, res) {
  126. if (err) {
  127. log.error("updateTopis is fail error: " + err);
  128. } else {
  129. log.info("updateTopis is success");
  130. }
  131. }
  132. });
  133. }
  134. }
  135. module.exports = TopicRepo;