stats.msg.repo.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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("./mysql/wlyy.db.js");
  11. var imRepo = require("./mysql/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. 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) " +
  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, 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.code code, p.name name, p.birthday birthday, p.sex sex, p.photo photo, ms.timestamp timestamp, 'patient' type " +
  112. "FROM msg_statistic ms, wlyy.wlyy_patient p " +
  113. "WHERE ms.uid = ? AND ms.uid = p.code AND " +
  114. "UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(ms.timestamp) < ? AND msg_type = 1" +
  115. " UNION " +
  116. "SELECT DISTINCT d.code code, d.name name, d.birthday birthday, d.sex sex, d.photo photo, ms.timestamp timestamp,'doctor' type " +
  117. "FROM msg_statistic ms, wlyy.wlyy_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, wlyy.wlyy_admin_team g, wlyy.wlyy_admin_team_member m, wlyy.wlyy_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. wlyyRepo.execQuery({
  188. "sql": "SELECT imei,token from wlyy_token WHERE user=?",
  189. "args": [userId],
  190. "handler": function (err, result) {
  191. if (err || result.length == 0) {
  192. handler(null, 0);
  193. return;
  194. }
  195. var options = {
  196. hostname: config.wlyyServerConfig.host,
  197. port: config.wlyyServerConfig.port,
  198. path: WLYY_ENPOINTS.Doctor.MessageCount.Path,
  199. method: WLYY_ENPOINTS.Doctor.MessageCount.Method,
  200. headers: {
  201. 'userAgent': '{"token":"' + result[0].token + '","uid":"' + userId + '","imei":"' + result[0].imei + '"}'
  202. }
  203. };
  204. var req = http.request(options, function (res) {
  205. res.setEncoding('utf8');
  206. log.info('请求家庭医生平台: http://', options.hostname + ":" + options.port + options.path);
  207. res.on('data', function (chunk) {
  208. log.info('家庭医生平台返回: ', chunk);
  209. handler(null, JSON.parse(chunk));
  210. });
  211. });
  212. req.on('error', function (e) {
  213. log.error('家庭医生平台接口调用出错: ', e.message);
  214. handler(e, null);
  215. });
  216. req.end();
  217. }
  218. });
  219. };
  220. exports.getBadgeNumber = function (userId, handler) {
  221. var self = this;
  222. async.parallel([
  223. function (callback) {
  224. callback(null, 0);
  225. },
  226. function (callback) {
  227. self.getAppMsgAmount(userId, function (err, result) {
  228. if (err) {
  229. callback(null, 0);
  230. } else {
  231. var count = 0;
  232. try {
  233. count += parseInt(result.data.healthIndex.amount);
  234. count += parseInt(result.data.sign.amount);
  235. count += parseInt(result.data.system.amount);
  236. var immsg = JSON.parse(result.data.imMsgCount);
  237. count+=parseInt(immsg.patient);
  238. count+=parseInt(immsg.doctor);
  239. callback(null, count);
  240. } catch (e) {
  241. callback(null, 0);
  242. }
  243. }
  244. });
  245. }],
  246. function (err, results) {
  247. var badge = 0;
  248. for (var index = 0; index < results.length; index++) {
  249. badge += results[index];
  250. }
  251. handler(null, badge);
  252. });
  253. };