stats.msg.repo.js 12 KB

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