msg.stat.js 9.9 KB

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