stats.msg.repo.js 10.0 KB

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