stats.msg.repo.js 11 KB

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