stats.msg.repo.js 10 KB

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