315 lines
11 KiB
PHP
315 lines
11 KiB
PHP
<?php
|
|
namespace server\model;
|
|
use server\components\Db;
|
|
use server\components\Redis;
|
|
|
|
class UserModel{
|
|
|
|
private static $_db;
|
|
public function __construct($dbInstance){
|
|
self::$_db = $dbInstance;
|
|
|
|
}
|
|
/**
|
|
* 客户端数据转数据库格式
|
|
* @param $data
|
|
* @param $kf
|
|
* @return array
|
|
*/
|
|
public static function dataToDb($data,$kf){
|
|
$log = [
|
|
'user_id'=>$data['uid'],
|
|
'client_id'=>$data['client_id'],
|
|
'user_name'=>$data['name'],
|
|
'user_avatar'=>$data['avatar'],
|
|
'data'=>empty($data['data'])?'':$data['data'],
|
|
'user_ip'=>$data['ip'],
|
|
'kf_id'=>$kf['id'],
|
|
'group_id'=>$data['group_id'],
|
|
'kf_type'=>$data['kf_type'],
|
|
'start_time'=> time(),
|
|
'end_time'=>0
|
|
];
|
|
return $log;
|
|
}
|
|
|
|
/**
|
|
* 数据库数据转客户端格式
|
|
* @param $data
|
|
* @return array
|
|
*/
|
|
public static function dbToData($data){
|
|
|
|
$log = [
|
|
'id'=>$data['user_id'],
|
|
'client_id'=>$data['client_id'],
|
|
'name'=>$data['user_name'],
|
|
'avatar'=>$data['user_avatar'],
|
|
'ip'=>$data['user_ip'],
|
|
'group_id'=>$data['group_id'],
|
|
'kf_type'=>$data['kf_type'],
|
|
];
|
|
return $log;
|
|
}
|
|
|
|
|
|
/**
|
|
* 根据用户编号获取当前连接列表
|
|
* @param $user_id
|
|
* @return array
|
|
*/
|
|
public function getUidLinks($user_id,$type,$groupId,$kfType=''){
|
|
|
|
//发送方为用户时取客服的信息
|
|
if($type === 'user'){
|
|
if(!(strpos($user_id,'KF')===0)){
|
|
$user_id = 'KF'.$user_id;
|
|
}
|
|
|
|
$kfInfo = json_decode(Redis::instance()->hget('kfList',$user_id),true);
|
|
|
|
if($kfInfo) return self::dbToData($kfInfo);
|
|
}else{
|
|
$link_list = self::$_db->select('client_id,kf_type')
|
|
->from('ws_service_log')
|
|
->where(" end_time=0 and kf_type=:kf_type and group_id=:group_id and user_id=:user_id ")
|
|
->bindValues(['kf_type' => $kfType,'group_id' => $groupId,'user_id' => $user_id])
|
|
->row();
|
|
|
|
return $link_list;
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 根据用户编号获取用户fd
|
|
* @param $user_id
|
|
* @return int
|
|
*/
|
|
public function getUserFd($user_id)
|
|
{
|
|
$fd = self::$_db->select('client_id')
|
|
->from('ws_service_log')
|
|
->where(" end_time=0 and user_id='{$user_id}' ")
|
|
->single();
|
|
|
|
return $fd ? $fd : false;
|
|
}
|
|
|
|
/**
|
|
* 根据客服id获取客服
|
|
* @param $kf_id
|
|
* @return array
|
|
*/
|
|
public function getkfById($kfId,$groupId,$kfType)
|
|
{
|
|
if(strpos($kfId,'KF') === 0) $kfId = str_replace('KF', '', $kfId);
|
|
|
|
$kf = self::$_db->select('id,user_name,group_id,online,kf_type,user_avatar')
|
|
->from('ws_users')
|
|
->where('id = :kf_id AND group_id = :group_id AND kf_type = :kf_type')
|
|
->bindValues(['kf_id'=>$kfId, 'group_id' => $groupId, 'kf_type' => $kfType])
|
|
->row();
|
|
|
|
return $kf;
|
|
}
|
|
|
|
/**
|
|
* 更新当前统计信息
|
|
* @param int $flag
|
|
*/
|
|
public function saveNowData($flag = 1){
|
|
|
|
//获取当天日期
|
|
$now_date = date('Y-m-d');
|
|
|
|
// 上午 8点 到 22 点开始统计
|
|
// if (date('H') < 8 || date('H') > 22) {
|
|
// return;
|
|
// }
|
|
|
|
// 当前正在接入的人 和 在线客服数
|
|
$onlineKf = Redis::instance()->hgetall('kfList');
|
|
$on_line_kf_list = [];
|
|
foreach ($onlineKf as $vo) {
|
|
$on_line_kf_list[] = json_decode($vo, true);
|
|
}
|
|
$on_line_num_arr = self::dealGroup($on_line_kf_list);
|
|
// 给每个平台增加统计记录
|
|
$platform = self::getPlatformList();
|
|
foreach ($platform as $vo) {
|
|
|
|
$onlineKf_num = isset($on_line_num_arr[$vo['id']]) ? $on_line_num_arr[$vo['id']] : 0;
|
|
|
|
$nowTalking = self::getNowCliNum($vo['id']);
|
|
|
|
// 接入的会话连接数
|
|
$success_in = self::getTodayCliNum($vo['id']);
|
|
//当前客户端会话链接总数量
|
|
// $cli_all = count(Redis::instance()->hgetall('fd'));
|
|
// 在队列中的用户 当前客服端链接总量与客服最大限制接待量取差
|
|
// $max_num = self::$_db->select('max_service')
|
|
// ->from('ws_kf_config')
|
|
// ->where("id= '1' ")
|
|
// ->row();
|
|
// $inQueue = $cli_all-$max_num['max_service']*$onlineKf;
|
|
$inQueue = 0;
|
|
// $inQueue = $inQueue>0?$inQueue:0;
|
|
|
|
//当日会话总量
|
|
$total_in = $inQueue > 0 ? ($success_in + $inQueue) : $success_in;
|
|
|
|
//查询是否有当日记录 若有执行更新 无则新增
|
|
$data = self::$_db->select('*')->from('ws_now_data')->where("now_date='$now_date' AND group_id = {$vo['id']}")->row();
|
|
// 获取订单数量和总金额
|
|
$redis = Redis::instance();
|
|
$date = date("Ymd");
|
|
$hash_money_key = "statistics_recharge_money_day_{$date}_{$vo['id']}";
|
|
$hash_order_key = "statistics_order_day_{$date}_{$vo['id']}";
|
|
$order_num = $redis->get($hash_order_key);
|
|
$money_total = $redis->get($hash_money_key);
|
|
// var_dump($order_num, $money_total);return;
|
|
// $order_num = self::$_db->select('order_num')->from('ws_service_data')->where("add_date='$now_date' AND group_id = {$vo['id']}")->column();
|
|
// $money_total = self::$_db->select('money_total')->from('ws_service_data')->where("add_date='$now_date' AND group_id = {$vo['id']}")->column();
|
|
$order_num = !empty($order_num) ? $order_num : 0;
|
|
$money_total = !empty($money_total) ? $money_total : 0;
|
|
$param = [
|
|
'is_talking' => $nowTalking,
|
|
'in_queue' => $inQueue,
|
|
'online_kf' => $onlineKf_num,
|
|
'success_in' => $success_in,
|
|
'total_in' => $total_in,
|
|
'order_num' => $order_num,
|
|
'money_total' => $money_total,
|
|
'now_date' => $now_date,
|
|
'group_id' => $vo['id'],
|
|
];
|
|
if($data){
|
|
self::$_db->update('ws_now_data')
|
|
->cols($param)
|
|
->where("now_date='$now_date' AND group_id = {$vo['id']}")
|
|
->query();
|
|
}else{
|
|
self::$_db->insert('ws_now_data')
|
|
->cols($param)
|
|
->query();
|
|
}
|
|
|
|
|
|
if (2 == $flag) {
|
|
// 检测是否已经有记录
|
|
$now_h = date('H');
|
|
$service_data = self::$_db->select('*')->from('ws_service_data')->where("add_hour='$now_h' AND group_id = {$vo['id']}")->row();
|
|
$param = [
|
|
'is_talking' => $nowTalking,
|
|
'in_queue' => $inQueue,
|
|
'online_kf' => $onlineKf_num,
|
|
'success_in' => $success_in,
|
|
'total_in' => $total_in,
|
|
'order_num' => isset($service_data['order_num']) ? $service_data['order_num'] : 0,
|
|
'money_total' => isset($service_data['money_total']) ? $service_data['money_total'] : 0,
|
|
'add_date' => $now_date,
|
|
'add_hour' => date('H'),
|
|
'add_minute' => date('i'),
|
|
'group_id' => $vo['id'],
|
|
];
|
|
if ($service_data) {
|
|
self::$_db->update('ws_service_data')
|
|
->cols($param)
|
|
->where("add_hour='$now_h' AND group_id = {$vo['id']}")
|
|
->query();
|
|
} else {
|
|
self::$_db->insert('ws_service_data')
|
|
->cols($param)
|
|
->query();
|
|
}
|
|
}
|
|
unset($nowTalking, $inQueue, $onlineKf, $param);
|
|
}
|
|
}
|
|
|
|
public static function getDayOrderNum($log_list)
|
|
{
|
|
if (!$log_list) return 0;
|
|
// 遍历统计数量
|
|
$num = 0;
|
|
foreach ($log_list as $k => $v) {
|
|
$num += $v['order_num'];
|
|
}
|
|
return $num;
|
|
}
|
|
|
|
/**
|
|
* 获取当前咨询正在咨询的连接数
|
|
* @return int
|
|
*/
|
|
public static function getNowCliNum($group_id){
|
|
|
|
// $row = self::$_db->select('id')
|
|
// ->from('ws_service_log')
|
|
// ->where("end_time=0 AND group_id = {$group_id}")
|
|
// ->query();
|
|
$row = 0;
|
|
$keys = Redis::instance()->keys('user_set_'.$group_id.'_*');
|
|
if(!empty($keys)){
|
|
foreach ($keys as $item) {
|
|
$row += Redis::instance()->scard($item);
|
|
}
|
|
}
|
|
return $row;
|
|
}
|
|
|
|
/**
|
|
* 处理为分组
|
|
*/
|
|
public static function dealGroup($row)
|
|
{
|
|
$group = [];
|
|
// 分组查询
|
|
foreach ($row as $vo) {
|
|
if (isset($group[$vo['group_id']])) {
|
|
$group[$vo['group_id']] += 1;
|
|
} else {
|
|
$group[$vo['group_id']] = 1;
|
|
}
|
|
}
|
|
return $group;
|
|
}
|
|
|
|
/**
|
|
* 获取平台管理员列表
|
|
*/
|
|
public static function getPlatformList()
|
|
{
|
|
// 管理员
|
|
// $role = self::$_db->from('ws_role')->select('id')->where("name = '平台管理员' AND status = 1")->row();
|
|
// if (!$role) return [];
|
|
// // 管理员角色记录
|
|
// $role_list = self::$_db->from('ws_admin_role')->select('id, admin_id')->where("role_id = {$role['id']}")->query();
|
|
// $admin_ids = [];
|
|
// foreach($role_list as $vo) {
|
|
// $admin_ids[] = $vo['admin_id'];
|
|
// }
|
|
// $admin_ids = implode(',', $admin_ids);
|
|
// $admin = self::$_db->from('ws_admins')->select('id')->where("id in ({$admin_ids})")->query();
|
|
$group = self::$_db->from('ws_groups')->select('id')->where("status = 1")->query();
|
|
|
|
return $group;
|
|
}
|
|
|
|
/**
|
|
* 获取今日链接数
|
|
* @return int
|
|
*/
|
|
public static function getTodayCliNum($group_id){
|
|
// $toDay = strtotime(date('Y-m-d'));
|
|
// $row = self::$_db->select('id')
|
|
// ->from('ws_service_log')
|
|
// ->where("start_time >= '{$toDay}' AND group_id = {$group_id}")
|
|
// ->query();
|
|
$row = Redis::instance()->get('statistics_cli_'.date("Ymd").'_'.$group_id);
|
|
return intval($row);
|
|
}
|
|
|
|
}
|