msg_statistic.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. "use strict";
  2. var log = require('../util/log');
  3. var mysql_wlyy = require("../db/mysql_wlyy");
  4. var mysql_im = require("../db/mysql_im");
  5. var config = require("../config");
  6. var http = require('http');
  7. var qs = require('querystring');
  8. var async = require('async');
  9. function updateGroupChatInfo(user_id, group_id, from_uid, at_me, type, content, msg_count_plus_one, handler) {
  10. var uuid = user_id + '_' + group_id;
  11. if (msg_count_plus_one) {
  12. mysql_im.execQuery({
  13. "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",
  14. "args": [user_id, uuid, from_uid, group_id, at_me, 2, type, content, 1, from_uid, at_me, type, content],
  15. "handler": handler
  16. });
  17. } else {
  18. mysql_im.execQuery({
  19. "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=?",
  20. "args": [user_id, uuid, from_uid, group_id, at_me, 2, type, content, 0, from_uid, at_me, type, content],
  21. "handler": handler
  22. });
  23. }
  24. }
  25. function updateP2PChatInfo(user_id, peer_uid, from_uid, type, content, handler) {
  26. var uuid = user_id + '_' + peer_uid;
  27. if (user_id == from_uid) {
  28. // 更新自身的统计信息
  29. mysql_im.execQuery({
  30. "sql": "INSERT INTO msg_statistic (uid,uuid,from_uid,peer_uid,msg_type,last_content_type,last_content,new_msg_count) VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE peer_uid=?,last_content_type=?,last_content=?",
  31. "args": [user_id, uuid, from_uid, peer_uid, 1, type, content, 0, peer_uid, type, content],
  32. "handler": handler
  33. });
  34. } else {
  35. // 更新对端的统计信息
  36. mysql_im.execQuery({
  37. "sql": "INSERT INTO msg_statistic (uid,uuid,from_uid,peer_uid,msg_type,last_content_type,last_content,new_msg_count) VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE peer_uid=?,last_content_type=?,last_content=?,new_msg_count=new_msg_count+1",
  38. "args": [user_id, uuid, from_uid, peer_uid, 1, type, content, 1, peer_uid, type, content],
  39. "handler": handler
  40. });
  41. }
  42. }
  43. function clearGroupChatInfo(user_id, group_id, handler) {
  44. var uuid = user_id + '_' + group_id;
  45. mysql_im.execQuery({
  46. "sql": "UPDATE msg_statistic SET new_msg_count='0' WHERE uuid=?",
  47. "args": [uuid],
  48. "handler": handler
  49. });
  50. }
  51. function clearP2PChatInfo(user_id, peer_uid, handler) {
  52. var uuid = user_id + '_' + peer_uid;
  53. mysql_im.execQuery({
  54. "sql": "UPDATE msg_statistic SET new_msg_count='0' WHERE uuid=?",
  55. "args": [uuid],
  56. "handler": handler
  57. });
  58. }
  59. function getGroupChatInfo(user_id, group_id, handler) {
  60. var uuid = user_id + '_' + group_id;
  61. mysql_im.execQuery({
  62. "sql": "SELECT uid,from_uid,from_gid,at_me,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uuid = ?",
  63. "args": [uuid],
  64. "handler": handler
  65. });
  66. }
  67. function getP2PChatInfo(user_id, peer_uid, handler) {
  68. var uuid = user_id + '_' + peer_uid;
  69. mysql_im.execQuery({
  70. "sql": "SELECT uid,from_uid,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uuid = ?",
  71. "args": [uuid],
  72. "handler": handler
  73. });
  74. }
  75. function getChatList(user_id, handler) {
  76. mysql_im.execQuery({
  77. "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 = ?",
  78. "args": [user_id],
  79. "handler": handler
  80. });
  81. }
  82. function getGroupChatAllUnRead(user_id, handler) {
  83. mysql_im.execQuery({
  84. "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND msg_type=2 AND new_msg_count>0",
  85. "args": [user_id],
  86. "handler": handler
  87. });
  88. }
  89. function getP2PChatAllUnRead(user_id, handler) {
  90. mysql_im.execQuery({
  91. "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND msg_type=1 AND new_msg_count>0",
  92. "args": [user_id],
  93. "handler": handler
  94. });
  95. }
  96. function getChatAllUnRead(user_id, handler) {
  97. mysql_im.execQuery({
  98. "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND new_msg_count>0",
  99. "args": [user_id],
  100. "handler": handler
  101. });
  102. }
  103. function getAppMsgAmount(user_id, handler) {
  104. mysql_wlyy.execQuery({
  105. "sql": "SELECT imei,token from wlyy_token WHERE user=?",
  106. "args": [user_id],
  107. "handler": function(err, result) {
  108. if (err) {
  109. handler(null, 0);
  110. return;
  111. }
  112. if (result.length == 0) {
  113. handler(null, 0);
  114. return;
  115. }
  116. var options = {
  117. hostname: config.app_serer_cfg.hostname,
  118. port: config.app_serer_cfg.port,
  119. path: config.app_serer_cfg.path,
  120. method: config.app_serer_cfg.method,
  121. headers: {
  122. 'userAgent': '{"token":"'+ result[0].token +'","uid":"'+ user_id +'","imei":"'+ result[0].imei +'"}'
  123. }
  124. };
  125. var req = http.request(options, function (res) {
  126. res.setEncoding('utf8');
  127. res.on('data', function (chunk) {
  128. console.log('BODY: ' + chunk);
  129. var data = JSON.parse(chunk);
  130. handler(null, data);
  131. });
  132. });
  133. req.on('error', function (e) {
  134. console.log('problem with request: ' + e.message);
  135. handler(e, null);
  136. });
  137. req.end();
  138. }
  139. });
  140. }
  141. function getBadgeNumber(user_id, handler) {
  142. async.parallel([
  143. function(callback) {
  144. // 此版本app界面未显示im的未读条数
  145. callback(null, 0);
  146. /*
  147. getChatAllUnRead(user_id, function(err, result) {
  148. if (err) {
  149. callback(null, 0);
  150. return;
  151. }
  152. if (result.length == 0) {
  153. callback(null, 0);
  154. return;
  155. }
  156. var count = 0;
  157. var index = 0;
  158. var length = result.length;
  159. for (; index < length; index++) {
  160. count += result[index].new_msg_count;
  161. }
  162. callback(null, count);
  163. });
  164. */
  165. },
  166. function(callback) {
  167. getAppMsgAmount(user_id, function(err, result) {
  168. if (err) {
  169. callback(null, 0);
  170. } else {
  171. var count = 0;
  172. try {
  173. count += result.data.healthIndex;
  174. count += result.data.sign;
  175. count += result.data.consultTeam;
  176. callback(null, count);
  177. } catch (e) {
  178. callback(null, 0);
  179. }
  180. }
  181. });
  182. }],
  183. function(err, results) {
  184. var badge = 0;
  185. for (var index = 0; index < results.length; index++) {
  186. badge += results[index];
  187. }
  188. handler(null, badge);
  189. });
  190. }
  191. exports.updateGroupChatInfo = updateGroupChatInfo;
  192. exports.updateP2PChatInfo = updateP2PChatInfo;
  193. exports.clearGroupChatInfo = clearGroupChatInfo;
  194. exports.clearP2PChatInfo = clearP2PChatInfo;
  195. exports.getGroupChatInfo = getGroupChatInfo;
  196. exports.getP2PChatInfo = getP2PChatInfo;
  197. exports.getChatList = getChatList;
  198. exports.getGroupChatAllUnRead = getGroupChatAllUnRead;
  199. exports.getP2PChatAllUnRead = getP2PChatAllUnRead;
  200. exports.getChatAllUnRead = getChatAllUnRead;
  201. exports.getAppMsgAmount = getAppMsgAmount;
  202. exports.getBadgeNumber = getBadgeNumber;