stats.msg.repo.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * 消息统计。方便前端获取聊天的状态。
  3. */
  4. "use strict";
  5. var http = require('http');
  6. var async = require('async');
  7. var configFile = require('../include/commons').CONFIG_FILE;
  8. var config = require('../resources/config/' + configFile);
  9. var log = require('../util/log');
  10. var wlyyRepo = require("./database/wlyy.db.js");
  11. var imRepo = require("./database/im.db.js");
  12. var WLYY_ENPOINTS = require('../include/endpoints').WLYY_ENPOINTS;
  13. //--------------------About all chats--------------------
  14. /**
  15. * 所有聊天列表。
  16. *
  17. * @param userId
  18. * @param handler
  19. */
  20. exports.getChatList = function (userId, handler) {
  21. imRepo.execQuery({
  22. "sql": "SELECT uid,from_uid,from_gid,peer_uid,at_me,msg_type,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uid = ?",
  23. "args": [userId],
  24. "handler": handler
  25. });
  26. };
  27. /**
  28. * 所有未读聊天记录数。
  29. *
  30. * @param userId
  31. * @param handler
  32. */
  33. exports.getChatAllUnReadCount = function (userId, handler) {
  34. imRepo.execQuery({
  35. "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND new_msg_count>0",
  36. "args": [userId],
  37. "handler": handler
  38. });
  39. };
  40. //--------------------About private chat summary--------------------
  41. exports.updatePrivateChatSummary = function (userId, peerId, from, type, content, handler) {
  42. var uuid = userId + '_' + peerId;
  43. if (userId == from) {
  44. //userId = from ,peerId = to from = from
  45. // 更新自身的统计信息
  46. var sql = "INSERT INTO msg_statistic (uid,uuid,from_uid,peer_uid,msg_type,last_content_type,last_content,new_msg_count) " +
  47. "VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE peer_uid=?,last_content_type=?,last_content=?";
  48. imRepo.execQuery({
  49. "sql": sql,
  50. "args": [userId, uuid, from, peerId, 1, type, content, 0, peerId, type, content],
  51. "handler": handler
  52. });
  53. } else {
  54. var sql ="";
  55. if(type==7){//结束的咨询
  56. //userId = to ,peerId = from, from = from
  57. sql = "INSERT INTO msg_statistic (uid,uuid,from_uid,peer_uid,msg_type,last_content_type,last_content,new_msg_count) " +
  58. "VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE peer_uid=?,last_content_type=?,last_content=?";
  59. // 更新对端的统计信息
  60. imRepo.execQuery({
  61. "sql": sql,
  62. "args": [userId, uuid, from, peerId, 1, type, content,0, peerId, type, content],
  63. "handler": handler
  64. });
  65. }else{
  66. //userId = to ,peerId = from, from = from
  67. sql = "INSERT INTO msg_statistic (uid,uuid,from_uid,peer_uid,msg_type,last_content_type,last_content,new_msg_count) " +
  68. "VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE peer_uid=?,last_content_type=?,last_content=?,new_msg_count=new_msg_count+1";
  69. // 更新对端的统计信息
  70. imRepo.execQuery({
  71. "sql": sql,
  72. "args": [userId, uuid, from, peerId, 1, type, content, 1, peerId, type, content],
  73. "handler": handler
  74. });
  75. }
  76. }
  77. };
  78. exports.clearPrivateChatSummary = function (userId, peerId, handler) {
  79. var uuid = userId + '_' + peerId;
  80. imRepo.execQuery({
  81. "sql": "UPDATE msg_statistic SET new_msg_count='0' WHERE uuid=?",
  82. "args": [uuid],
  83. "handler": handler
  84. });
  85. };
  86. exports.getPrivateChatSummary = function (userId, peerId, handler) {
  87. var uuid = userId + '_' + peerId;
  88. imRepo.execQuery({
  89. "sql": "SELECT uid,from_uid,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uuid = ?",
  90. "args": [uuid],
  91. "handler": handler
  92. });
  93. };
  94. exports.getPrivateChatAllUnReadCount = function (userId, handler) {
  95. imRepo.execQuery({
  96. "sql": "SELECT new_msg_count from msg_statistic WHERE uid = ? AND msg_type = 1 AND new_msg_count > 0",
  97. "args": [userId],
  98. "handler": handler
  99. });
  100. };
  101. /**
  102. * 最近聊天对象,如患者,医生与群等基本信息。
  103. *
  104. * @param userId
  105. * @param days
  106. * @param handler
  107. */
  108. exports.getRecentChats = function (userId, days, handler) {
  109. var timespan = 60 * 60 * 24 * days; // 多少天内的联系对象
  110. var sql = "SELECT * FROM(" +
  111. "SELECT DISTINCT p.id code, p.name name, p.birthday birthday, p.sex sex, p.photo photo, ms.timestamp timestamp, 'patient' type " +
  112. "FROM msg_statistic ms, ydf_patient p " +
  113. "WHERE ms.uid = ? AND ms.uid = p.id AND " +
  114. "UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(ms.timestamp) < ? AND msg_type = 1" +
  115. " UNION " +
  116. "SELECT DISTINCT d.id code, d.name name, d.birthday birthday, d.sex sex, d.photo photo, ms.timestamp timestamp,'doctor' type " +
  117. "FROM msg_statistic ms, ydf_doctor d, (SELECT CASE WHEN ms1.timestamp > ms2.timestamp THEN ms1.id ELSE ms2.id END id " +
  118. " FROM msg_statistic ms1, msg_statistic ms2 " +
  119. " WHERE ms1.from_gid IS NULL AND ms2.from_gid IS NULL AND ms1.uid = ms2.peer_uid AND ms1.peer_uid = ms2.uid) x " +
  120. "WHERE x.id = ms.id AND ((ms.uid = ? AND ms.peer_uid = d.code) OR (ms.uid = d.code AND ms.peer_uid = ?)) AND UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(ms.timestamp) < ?" +
  121. " UNION " +
  122. "SELECT g.id code, g.name name, '' birthday, '' sex, '' photo, max(ms.timestamp) timestamp, 'type' ':group' " +
  123. "FROM msg_statistic ms, ydf_doctor_team g, ydf_doctor_team_member m, ydf_doctor d " +
  124. "WHERE d.code = ? AND d.code = m.doctor_code AND m.team_id = g.id AND g.id = ms.from_gid " +
  125. " AND (ms.uid = d.code or ms.from_uid = d.code) AND UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(ms.timestamp) < ? group by g.id, g.name " +
  126. ") x ORDER BY timestamp DESC";
  127. imRepo.execQuery({
  128. "sql": sql,
  129. "args": [userId, timespan, userId, userId, timespan, userId, timespan],
  130. "handler": handler
  131. });
  132. };
  133. //--------------------About group chat summary--------------------
  134. /**
  135. * 更新群聊统计摘要。
  136. *
  137. * @param userId
  138. * @param groupId
  139. * @param from
  140. * @param atMe
  141. * @param type
  142. * @param content
  143. * @param msgCountPlusOne
  144. * @param handler
  145. */
  146. exports.updateGroupChatSummary = function (userId, groupId, from, atMe, type, content, msgCountPlusOne, handler) {
  147. var uuid = userId + '_' + groupId;
  148. if (msgCountPlusOne) {
  149. imRepo.execQuery({
  150. "sql": "INSERT INTO msg_statistic (uid,uuid,from_uid,from_gid,at_me,msg_type,last_content_type,last_content,new_msg_count) VALUES (?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE from_uid=?,at_me=?,last_content_type=?,last_content=?,new_msg_count=new_msg_count+1",
  151. "args": [userId, uuid, from, groupId, atMe, 2, type, content, 1, from, atMe, type, content],
  152. "handler": handler
  153. });
  154. } else {
  155. imRepo.execQuery({
  156. "sql": "INSERT INTO msg_statistic (uid,uuid,from_uid,from_gid,at_me,msg_type,last_content_type,last_content,new_msg_count) VALUES (?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE from_uid=?,at_me=?,last_content_type=?,last_content=?",
  157. "args": [userId, uuid, from, groupId, atMe, 2, type, content, 0, from, atMe, type, content],
  158. "handler": handler
  159. });
  160. }
  161. };
  162. exports.getGroupChatAllUnReadCount = function (userId, handler) {
  163. imRepo.execQuery({
  164. "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND msg_type=2 AND new_msg_count>0",
  165. "args": [userId],
  166. "handler": handler
  167. });
  168. };
  169. exports.getGroupChatSummary = function (userId, groupId, handler) {
  170. var uuid = userId + '_' + groupId;
  171. imRepo.execQuery({
  172. "sql": "SELECT uid,from_uid,from_gid,at_me,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uuid = ?",
  173. "args": [uuid],
  174. "handler": handler
  175. });
  176. };
  177. exports.clearGroupChatSummary = function clearGroupChatInfo(userId, groupId, handler) {
  178. var uuid = userId + '_' + groupId;
  179. imRepo.execQuery({
  180. "sql": "UPDATE msg_statistic SET new_msg_count='0' WHERE uuid=?",
  181. "args": [uuid],
  182. "handler": handler
  183. });
  184. };
  185. //--------------------Others--------------------
  186. exports.getAppMsgAmount = function (userId, handler) {
  187. };
  188. exports.getBadgeNumber = function (userId, handler) {
  189. var self = this;
  190. async.parallel([
  191. function (callback) {
  192. callback(null, 0);
  193. },
  194. function (callback) {
  195. self.getAppMsgAmount(userId, function (err, result) {
  196. if (err) {
  197. callback(null, 0);
  198. } else {
  199. var count = 0;
  200. try {
  201. count += parseInt(result.data.healthIndex.amount);
  202. count += parseInt(result.data.sign.amount);
  203. count += parseInt(result.data.system.amount);
  204. var immsg = JSON.parse(result.data.imMsgCount);
  205. count+=parseInt(immsg.patient);
  206. count+=parseInt(immsg.doctor);
  207. callback(null, count);
  208. } catch (e) {
  209. callback(null, 0);
  210. }
  211. }
  212. });
  213. }],
  214. function (err, results) {
  215. var badge = 0;
  216. for (var index = 0; index < results.length; index++) {
  217. badge += results[index];
  218. }
  219. handler(null, badge);
  220. });
  221. };