123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- /**
- * 消息统计。方便前端获取聊天的状态。
- */
- "use strict";
- var http = require('http');
- var async = require('async');
- var configFile = require('../include/commons').CONFIG_FILE;
- var config = require('../resources/config/' + configFile);
- var log = require('../util/log');
- var wlyyRepo = require("./database/wlyy.db.js");
- var imRepo = require("./database/im.db.js");
- let doctorRepo = require('../repository/doctor.repo.js');
- let gmRepo = require('../repository/group.msg.repo');
- let pmRepo = require('../repository/private.msg.repo');
- var WLYY_ENPOINTS = require('../include/endpoints').WLYY_ENPOINTS;
- //--------------------About all chats--------------------
- /**
- * 所有聊天列表。
- *
- * @param userId
- * @param handler
- */
- exports.getChatList = function (userId, handler) {
- imRepo.execQuery({
- "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 = ?",
- "args": [userId],
- "handler": handler
- });
- };
- /**
- * 所有未读聊天记录数。
- *
- * @param userId
- * @param handler
- */
- exports.getChatAllUnReadCount = function (userId, handler) {
- imRepo.execQuery({
- "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND new_msg_count>0",
- "args": [userId],
- "handler": handler
- });
- };
- //--------------------About private chat summary--------------------
- exports.updatePrivateChatSummary = function (userId, peerId, from, type, content, handler) {
- var uuid = userId + '_' + peerId;
- if (userId == from) {
- //userId = from ,peerId = to from = from
- // 更新自身的统计信息
- var 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=?";
- imRepo.execQuery({
- "sql": sql,
- "args": [userId, uuid, from, peerId, 1, type, content, 0, peerId, type, content],
- "handler": handler
- });
- } else {
- var sql ="";
- if(type==7){//结束的咨询
- //userId = to ,peerId = from, from = from
- 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=?";
- // 更新对端的统计信息
- imRepo.execQuery({
- "sql": sql,
- "args": [userId, uuid, from, peerId, 1, type, content,0, peerId, type, content],
- "handler": handler
- });
- }else{
- //userId = to ,peerId = from, from = from
- 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";
- // 更新对端的统计信息
- imRepo.execQuery({
- "sql": sql,
- "args": [userId, uuid, from, peerId, 1, type, content, 1, peerId, type, content],
- "handler": handler
- });
- }
- }
- };
- exports.clearPrivateChatSummary = function (userId, peerId, handler) {
- var uuid = userId + '_' + peerId;
- imRepo.execQuery({
- "sql": "UPDATE msg_statistic SET new_msg_count='0' WHERE uuid=?",
- "args": [uuid],
- "handler": handler
- });
- };
- exports.getPrivateChatSummary = function (userId, peerId, handler) {
- var uuid = userId + '_' + peerId;
- imRepo.execQuery({
- "sql": "SELECT uid,from_uid,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uuid = ?",
- "args": [uuid],
- "handler": handler
- });
- };
- exports.getPrivateChatAllUnReadCount = function (userId, handler) {
- imRepo.execQuery({
- "sql": "SELECT new_msg_count from msg_statistic WHERE uid = ? AND msg_type = 1 AND new_msg_count > 0",
- "args": [userId],
- "handler": handler
- });
- };
- /**
- * 最近聊天对象,如患者,医生与群等基本信息。
- *
- * @param userId
- * @param days
- * @param handler
- */
- exports.getRecentChats = function (userId, days, handler) {
- var timespan = 60 * 60 * 24 * days; // 多少天内的联系对象
- var sql = "SELECT * FROM(" +
- "SELECT DISTINCT p.id code, p.name name, p.birthday birthday, p.sex sex, p.photo photo, ms.timestamp timestamp, 'patient' type " +
- "FROM msg_statistic ms, ydf_patient p " +
- "WHERE ms.uid = ? AND ms.uid = p.id AND " +
- "UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(ms.timestamp) < ? AND msg_type = 1" +
- " UNION " +
- "SELECT DISTINCT d.id code, d.name name, d.birthday birthday, d.sex sex, d.photo photo, ms.timestamp timestamp,'doctor' type " +
- "FROM msg_statistic ms, ydf_doctor d, (SELECT CASE WHEN ms1.timestamp > ms2.timestamp THEN ms1.id ELSE ms2.id END id " +
- " FROM msg_statistic ms1, msg_statistic ms2 " +
- " 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 " +
- "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) < ?" +
- " UNION " +
- "SELECT g.id code, g.name name, '' birthday, '' sex, '' photo, max(ms.timestamp) timestamp, 'type' ':group' " +
- "FROM msg_statistic ms, ydf_doctor_team g, ydf_doctor_team_member m, ydf_doctor d " +
- "WHERE d.code = ? AND d.code = m.doctor_code AND m.team_id = g.id AND g.id = ms.from_gid " +
- " 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 " +
- ") x ORDER BY timestamp DESC";
- imRepo.execQuery({
- "sql": sql,
- "args": [userId, timespan, userId, userId, timespan, userId, timespan],
- "handler": handler
- });
- };
- //--------------------About group chat summary--------------------
- /**
- * 更新群聊统计摘要。
- *
- * @param userId
- * @param groupId
- * @param from
- * @param atMe
- * @param type
- * @param content
- * @param msgCountPlusOne
- * @param handler
- */
- exports.updateGroupChatSummary = function (userId, groupId, from, atMe, type, content, msgCountPlusOne, handler) {
- var uuid = userId + '_' + groupId;
- if (msgCountPlusOne) {
- imRepo.execQuery({
- "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",
- "args": [userId, uuid, from, groupId, atMe, 2, type, content, 1, from, atMe, type, content],
- "handler": handler
- });
- } else {
- imRepo.execQuery({
- "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=?",
- "args": [userId, uuid, from, groupId, atMe, 2, type, content, 0, from, atMe, type, content],
- "handler": handler
- });
- }
- };
- exports.getGroupChatAllUnReadCount = function (userId, handler) {
- imRepo.execQuery({
- "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND msg_type=2 AND new_msg_count>0",
- "args": [userId],
- "handler": handler
- });
- };
- exports.getGroupChatSummary = function (userId, groupId, handler) {
- var uuid = userId + '_' + groupId;
- imRepo.execQuery({
- "sql": "SELECT uid,from_uid,from_gid,at_me,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uuid = ?",
- "args": [uuid],
- "handler": handler
- });
- };
- exports.clearGroupChatSummary = function clearGroupChatInfo(userId, groupId, handler) {
- var uuid = userId + '_' + groupId;
- imRepo.execQuery({
- "sql": "UPDATE msg_statistic SET new_msg_count='0' WHERE uuid=?",
- "args": [uuid],
- "handler": handler
- });
- };
- //--------------------Others--------------------
- exports.getAppMsgAmount = function (userId, handler) {
- let new_msg_count =0;
- // 先获取医生间的私聊
- pmRepo.findAllP2PWithDoctor(userId, function (err, doctors) {
- for (let i = 0; i < doctors.length; i++) {
- let doctor = doctors[i];
- new_msg_count = doctor.new_msg_count+new_msg_count;
- }
- // 再获取医生间的组
- gmRepo.findAllGroupsWithDoctor(userId, function (err, groups) {
- for (let i = 0; i < groups.length; i++) {
- let group = groups[i];
- new_msg_count = group.new_msg_count+new_msg_count;
- }
- //获取患者记录数量
- pmRepo.findAllP2PWithPatient(userId, function (err, patients) {
- for (let i = 0; i < patients.length; i++) {
- let patient = patients[i];
- new_msg_count =new_msg_count+ patient.new_msg_count;
- }
- pmRepo.countAppo(userId,function(err, res){
- new_msg_count = new_msg_count+res[0].cou;
- return new_msg_count;
- })
- });
- });
- });
- };
- exports.getBadgeNumber = function (userId, handler) {
- var self = this;
- async.parallel([
- function (callback) {
- callback(null, 0);
- },
- function (callback) {
- self.getAppMsgAmount(userId, function (err, result) {
- if (err) {
- callback(null, 0);
- } else {
- var count = result;
- callback(null, count);
- }
- });
- }],
- function (err, results) {
- var badge = 0;
- for (var index = 0; index < results.length; index++) {
- badge += results[index];
- }
- handler(null, badge);
- });
- };
|