stats.msg.repo.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/wlyy.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. // 更新自身的统计信息
  45. var sql = "INSERT INTO msg_statistic (uid,uuid,from_uid,peer_uid,msg_type,last_content_type,last_content,new_msg_count) " +
  46. "VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE peer_uid=?,last_content_type=?,last_content=?";
  47. imRepo.execQuery({
  48. "sql": sql,
  49. "args": [userId, uuid, from, peerId, 1, type, content, 0, peerId, type, content],
  50. "handler": handler
  51. });
  52. } else {
  53. var sql = "INSERT INTO msg_statistic (uid,uuid,from_uid,peer_uid,msg_type,last_content_type,last_content,new_msg_count) " +
  54. "VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE peer_uid=?,last_content_type=?,last_content=?,new_msg_count=new_msg_count+1";
  55. // 更新对端的统计信息
  56. imRepo.execQuery({
  57. "sql": sql,
  58. "args": [userId, uuid, from, peerId, 1, type, content, 1, peerId, type, content],
  59. "handler": handler
  60. });
  61. }
  62. };
  63. exports.clearPrivateChatSummary = function (userId, peerId, handler) {
  64. var uuid = userId + '_' + peerId;
  65. imRepo.execQuery({
  66. "sql": "UPDATE msg_statistic SET new_msg_count='0' WHERE uuid=?",
  67. "args": [uuid],
  68. "handler": handler
  69. });
  70. };
  71. exports.getPrivateChatSummary = function (userId, peerId, handler) {
  72. var uuid = userId + '_' + peerId;
  73. imRepo.execQuery({
  74. "sql": "SELECT uid,from_uid,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uuid = ?",
  75. "args": [uuid],
  76. "handler": handler
  77. });
  78. };
  79. exports.getPrivateChatAllUnReadCount = function (userId, handler) {
  80. imRepo.execQuery({
  81. "sql": "SELECT new_msg_count from msg_statistic WHERE uid = ? AND msg_type = 1 AND new_msg_count > 0",
  82. "args": [userId],
  83. "handler": handler
  84. });
  85. };
  86. /**
  87. * 最近聊天对象,如患者,医生与群等基本信息。
  88. *
  89. * @param userId
  90. * @param days
  91. * @param handler
  92. */
  93. exports.getRecentChats = function (userId, days, handler) {
  94. var timespan = 60 * 60 * 24 * days; // 多少天内的联系对象
  95. var sql = "SELECT * FROM(" +
  96. "SELECT DISTINCT p.code code, p.name name, p.birthday birthday, p.sex sex, p.photo photo, ms.timestamp timestamp, 'patient' type " +
  97. "FROM msg_statistic ms, wlyy.wlyy_patient p " +
  98. "WHERE ms.uid = ? AND ms.uid = p.code AND " +
  99. "UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(ms.timestamp) < ? AND msg_type = 1" +
  100. " UNION " +
  101. "SELECT DISTINCT d.code code, d.name name, d.birthday birthday, d.sex sex, d.photo photo, ms.timestamp timestamp,'doctor' type " +
  102. "FROM msg_statistic ms, wlyy.wlyy_doctor d, (SELECT CASE WHEN ms1.timestamp > ms2.timestamp THEN ms1.id ELSE ms2.id END id " +
  103. " FROM msg_statistic ms1, msg_statistic ms2 " +
  104. " 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 " +
  105. "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) < ?" +
  106. " UNION " +
  107. "SELECT g.id code, g.name name, '' birthday, '' sex, '' photo, max(ms.timestamp) timestamp, 'type' ':group' " +
  108. "FROM msg_statistic ms, wlyy.wlyy_admin_team g, wlyy.wlyy_admin_team_member m, wlyy.wlyy_doctor d " +
  109. "WHERE d.code = ? AND d.code = m.doctor_code AND m.team_id = g.id AND g.id = ms.from_gid " +
  110. " 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 " +
  111. ") x ORDER BY timestamp DESC";
  112. imRepo.execQuery({
  113. "sql": sql,
  114. "args": [userId, timespan, userId, userId, timespan, userId, timespan],
  115. "handler": handler
  116. });
  117. };
  118. //--------------------About group chat summary--------------------
  119. /**
  120. * 更新群聊统计摘要。
  121. *
  122. * @param userId
  123. * @param groupId
  124. * @param from
  125. * @param atMe
  126. * @param type
  127. * @param content
  128. * @param msgCountPlusOne
  129. * @param handler
  130. */
  131. exports.updateGroupChatSummary = function (userId, groupId, from, atMe, type, content, msgCountPlusOne, handler) {
  132. var uuid = userId + '_' + groupId;
  133. if (msgCountPlusOne) {
  134. imRepo.execQuery({
  135. "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",
  136. "args": [userId, uuid, from, groupId, atMe, 2, type, content, 1, from, atMe, type, content],
  137. "handler": handler
  138. });
  139. } else {
  140. imRepo.execQuery({
  141. "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=?",
  142. "args": [userId, uuid, from, groupId, atMe, 2, type, content, 0, from, atMe, type, content],
  143. "handler": handler
  144. });
  145. }
  146. };
  147. exports.getGroupChatAllUnReadCount = function (userId, handler) {
  148. imRepo.execQuery({
  149. "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND msg_type=2 AND new_msg_count>0",
  150. "args": [userId],
  151. "handler": handler
  152. });
  153. };
  154. exports.getGroupChatSummary = function (userId, groupId, handler) {
  155. var uuid = userId + '_' + groupId;
  156. imRepo.execQuery({
  157. "sql": "SELECT uid,from_uid,from_gid,at_me,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uuid = ?",
  158. "args": [uuid],
  159. "handler": handler
  160. });
  161. };
  162. exports.clearGroupChatSummary = function clearGroupChatInfo(userId, groupId, handler) {
  163. var uuid = userId + '_' + groupId;
  164. imRepo.execQuery({
  165. "sql": "UPDATE msg_statistic SET new_msg_count='0' WHERE uuid=?",
  166. "args": [uuid],
  167. "handler": handler
  168. });
  169. };
  170. //--------------------Others--------------------
  171. exports.getAppMsgAmount = function (userId, handler) {
  172. wlyyRepo.execQuery({
  173. "sql": "SELECT imei,token from wlyy_token WHERE user=?",
  174. "args": [userId],
  175. "handler": function (err, result) {
  176. if (err || result.length == 0) {
  177. handler(null, 0);
  178. return;
  179. }
  180. var options = {
  181. hostname: config.wlyyServerConfig.host,
  182. port: config.wlyyServerConfig.port,
  183. path: WLYY_ENPOINTS.Doctor.MessageCount.Path,
  184. method: WLYY_ENPOINTS.Doctor.MessageCount.Method,
  185. headers: {
  186. 'userAgent': '{"token":"' + result[0].token + '","uid":"' + userId + '","imei":"' + result[0].imei + '"}'
  187. }
  188. };
  189. var req = http.request(options, function (res) {
  190. res.setEncoding('utf8');
  191. log.info('请求家庭医生平台: http://', options.hostname + ":" + options.port + options.path);
  192. res.on('data', function (chunk) {
  193. log.info('家庭医生平台返回: ', chunk);
  194. handler(null, JSON.parse(chunk));
  195. });
  196. });
  197. req.on('error', function (e) {
  198. log.error('家庭医生平台接口调用出错: ', e.message);
  199. handler(e, null);
  200. });
  201. req.end();
  202. }
  203. });
  204. };
  205. exports.getBadgeNumber = function (userId, handler) {
  206. var self = this;
  207. async.parallel([
  208. function (callback) {
  209. callback(null, 0);
  210. },
  211. function (callback) {
  212. self.getAppMsgAmount(userId, function (err, result) {
  213. if (err) {
  214. callback(null, 0);
  215. } else {
  216. var count = 0;
  217. try {
  218. count += result.data.healthIndex;
  219. count += result.data.sign;
  220. count += result.data.consultTeam;
  221. callback(null, count);
  222. } catch (e) {
  223. callback(null, 0);
  224. }
  225. }
  226. });
  227. }],
  228. function (err, results) {
  229. var badge = 0;
  230. for (var index = 0; index < results.length; index++) {
  231. badge += results[index];
  232. }
  233. handler(null, badge);
  234. });
  235. };