"use strict"; var http = require('http'); var qs = require('querystring'); var async = require('async'); var commons = require('./include/commons'); var config = require(commons.CONFIG_FILE); var log = require('../util/log'); var mysql_wlyy = require("../repository/mysql_wlyy"); var mysql_im = require("../repository/mysql_im"); function updateGroupChatInfo(user_id, group_id, from_uid, at_me, type, content, msg_count_plus_one, handler) { var uuid = user_id + '_' + group_id; if (msg_count_plus_one) { mysql_im.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": [user_id, uuid, from_uid, group_id, at_me, 2, type, content, 1, from_uid, at_me, type, content], "handler": handler }); } else { mysql_im.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": [user_id, uuid, from_uid, group_id, at_me, 2, type, content, 0, from_uid, at_me, type, content], "handler": handler }); } } function updateP2PChatInfo(user_id, peer_uid, from_uid, type, content, handler) { var uuid = user_id + '_' + peer_uid; if (user_id == from_uid) { // 更新自身的统计信息 mysql_im.execQuery({ "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=?", "args": [user_id, uuid, from_uid, peer_uid, 1, type, content, 0, peer_uid, type, content], "handler": handler }); } else { // 更新对端的统计信息 mysql_im.execQuery({ "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", "args": [user_id, uuid, from_uid, peer_uid, 1, type, content, 1, peer_uid, type, content], "handler": handler }); } } function clearGroupChatInfo(user_id, group_id, handler) { var uuid = user_id + '_' + group_id; mysql_im.execQuery({ "sql": "UPDATE msg_statistic SET new_msg_count='0' WHERE uuid=?", "args": [uuid], "handler": handler }); } function clearP2PChatInfo(user_id, peer_uid, handler) { var uuid = user_id + '_' + peer_uid; mysql_im.execQuery({ "sql": "UPDATE msg_statistic SET new_msg_count='0' WHERE uuid=?", "args": [uuid], "handler": handler }); } function getGroupChatInfo(user_id, group_id, handler) { var uuid = user_id + '_' + group_id; mysql_im.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 }); } function getP2PChatInfo(user_id, peer_uid, handler) { var uuid = user_id + '_' + peer_uid; mysql_im.execQuery({ "sql": "SELECT uid,from_uid,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uuid = ?", "args": [uuid], "handler": handler }); } function getChatList(user_id, handler) { mysql_im.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": [user_id], "handler": handler }); } function getGroupChatAllUnRead(user_id, handler) { mysql_im.execQuery({ "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND msg_type=2 AND new_msg_count>0", "args": [user_id], "handler": handler }); } function getP2PChatAllUnRead(user_id, handler) { mysql_im.execQuery({ "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND msg_type=1 AND new_msg_count>0", "args": [user_id], "handler": handler }); } function getChatAllUnRead(user_id, handler) { mysql_im.execQuery({ "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND new_msg_count>0", "args": [user_id], "handler": handler }); } function getAppMsgAmount(user_id, handler) { mysql_wlyy.execQuery({ "sql": "SELECT imei,token from wlyy_token WHERE user=?", "args": [user_id], "handler": function(err, result) { if (err) { handler(null, 0); return; } if (result.length == 0) { handler(null, 0); return; } var options = { hostname: config.app_serer_cfg.hostname, port: config.app_serer_cfg.port, path: config.app_serer_cfg.path, method: config.app_serer_cfg.method, headers: { 'userAgent': '{"token":"'+ result[0].token +'","uid":"'+ user_id +'","imei":"'+ result[0].imei +'"}' } }; var req = http.request(options, function (res) { res.setEncoding('utf8'); res.on('data', function (chunk) { console.log('BODY: ' + chunk); var data = JSON.parse(chunk); handler(null, data); }); }); req.on('error', function (e) { console.log('problem with request: ' + e.message); handler(e, null); }); req.end(); } }); } function getBadgeNumber(user_id, handler) { async.parallel([ function(callback) { // 此版本app界面未显示im的未读条数 callback(null, 0); /* getChatAllUnRead(user_id, function(err, result) { if (err) { callback(null, 0); return; } if (result.length == 0) { callback(null, 0); return; } var count = 0; var index = 0; var length = result.length; for (; index < length; index++) { count += result[index].new_msg_count; } callback(null, count); }); */ }, function(callback) { getAppMsgAmount(user_id, function(err, result) { if (err) { callback(null, 0); } else { var count = 0; try { count += result.data.healthIndex; count += result.data.sign; count += result.data.consultTeam; callback(null, count); } catch (e) { callback(null, 0); } } }); }], function(err, results) { var badge = 0; for (var index = 0; index < results.length; index++) { badge += results[index]; } handler(null, badge); }); } exports.updateGroupChatInfo = updateGroupChatInfo; exports.updateP2PChatInfo = updateP2PChatInfo; exports.clearGroupChatInfo = clearGroupChatInfo; exports.clearP2PChatInfo = clearP2PChatInfo; exports.getGroupChatInfo = getGroupChatInfo; exports.getP2PChatInfo = getP2PChatInfo; exports.getChatList = getChatList; exports.getGroupChatAllUnRead = getGroupChatAllUnRead; exports.getP2PChatAllUnRead = getP2PChatAllUnRead; exports.getChatAllUnRead = getChatAllUnRead; exports.getAppMsgAmount = getAppMsgAmount; exports.getBadgeNumber = getBadgeNumber;