| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/7/6
- * Time: 16:59
- */
- namespace App\common;
- use app\admin\model\Kfonline as KfonlineModel;
- use app\admin\model\Users;
- use think\Db;
- /**
- * [
- * 用户名:
- * 会话量:今日客服累计的的会话量 hyl
- * 接待量:当前客服正在接待的会话数 jdl
- * 平均会话时长:今日客服的平均会话时长,即访客进入到会话结束时时间间隔的平均值 pjhusc
- * 参评率:今日客服的累计参评率,即参加评价的数量和接入会话数量的比值 cpl
- * 满意度率:今日客服的累计满意度率,即满意的数量和评价会话数量的比值 mydl
- * 平均响应时长:今日客服的平均响应时长,即访客访问和客服回复时间间隔的平均值。 pjxysc
- * 今日休息时长:今日客服的休息时长的累计值 jrxxsc
- * ]
- */
- class KfjkLogic
- {
- public function getAllData($kfuidarray)
- {
- if (empty($kfuidarray)) {
- return false;
- }
- $todayall = $this->getTodayServiceData($kfuidarray);
- if (!$todayall) {
- return false;
- }
- $kfuidarray_index = kftoKey($kfuidarray, 1);
- $uids = $kfuidarray;
- $uidsname = $this->uidName($kfuidarray);
- $hyl = $this->hyl($kfuidarray, $todayall);
- $jdl = $this->jdl($kfuidarray, $todayall);
- $pjhysc = $this->pjhusc($kfuidarray, $todayall);
- $cpl = $this->cpl($kfuidarray, $todayall);
- $mydl = $this->mydl($kfuidarray, $todayall);
- $pjxysc = $this->pjxysc($kfuidarray, $todayall);
- $jrxxsc = $this->jrxxsc($kfuidarray, $todayall);
- $kfip = $this->getKfinlineip();
- foreach ($kfuidarray_index as $uid => $val) {
- $kfuidarray_index[$uid]['ip'] = $kfip['KF' . $uid];
- $kfuidarray_index[$uid]['uid'] = $uids[$uid];
- $kfuidarray_index[$uid]['uidname'] = $uidsname[$uid];
- $kfuidarray_index[$uid]['hhl'] = $hyl[$uid];
- $kfuidarray_index[$uid]['jdl'] = $jdl[$uid];
- $kfuidarray_index[$uid]['pjhysc'] = $pjhysc[$uid];
- $kfuidarray_index[$uid]['cpl'] = $cpl[$uid];
- $kfuidarray_index[$uid]['mydl'] = $mydl[$uid];
- $kfuidarray_index[$uid]['pjxysc'] = $pjxysc[$uid];
- $kfuidarray_index[$uid]['jrxxsc'] = $jrxxsc[$uid];
- $kfuidarray_index[$uid]['fxx'] = $this->makeBtn($uid);
- }
- $last = [];
- foreach ($kfuidarray_index as $val) {
- $last[] = $val;
- }
- return $last;
- }
- // 生成按钮
- private function makeBtn($id)
- {
- $operate = '<a href="javascript:msg(' . $id . ')"><button type="button" class="btn btn-info btn-sm">';
- $operate .= '发消息</button></a> ';
- return $operate;
- }
- //按条件获取用户数组 组ID, 在线状态 0为全部
- public function getValiKF($groupid, $onlinestatus)
- {
- $groupusers = (new Users)->getByGroup($groupid);
- $groupuserArray = [];
- if ($groupusers) {
- foreach ($groupusers as $val) {
- $groupuserArray[$val['id']] = $val['id'];
- }
- }
- $statusArr = (new KfonlineModel())->getOnlineAll($onlinestatus);
- $return = array_intersect($groupuserArray, $statusArr);
- return $return;
- }
- public function uidName($uidArray)
- {
- $users = (new Users)->where(['id' => ['IN', $uidArray]])->select();
- $return = kftoKey($uidArray);
- foreach ($return as $uid => $ttttt) {
- foreach ($users as $nowuser) {
- if ($uid == $nowuser['id']) {
- $return[$uid] = $nowuser['user_name'];
- }
- }
- }
- return $return;
- }
- //会话量 统计
- public function hyl($uidArray, $alllogs)
- {
- $rets = kftoKey($uidArray);
- foreach ($rets as $uid => $aaaa) {
- foreach ($alllogs as $log) {
- if ($uid == $log['kf_id'] && ($log['status'] == 1 || $log['status'] == 3)) {
- $rets[$uid]++;
- }
- }
- }
- return $rets;
- }
- //接待量 统计
- public function jdl($uidArray, $alllogs)
- {
- $rets = kftoKey($uidArray);
- foreach ($rets as $uid => $aaa) {
- foreach ($alllogs as $log) {
- if ($uid == $log['kf_id']) {
- $rets[$uid]++;
- }
- }
- }
- return $rets;
- }
- //平均会话时长 统计
- public function pjhusc($uidArray, $alllogs)
- {
- $rets = kftoKey($uidArray, 1);
- foreach ($rets as $uid => $aaa) {
- foreach ($alllogs as $log) {
- if ($uid == $log['kf_id']) {
- (!isset($rets[$uid]['count'])) ? ($rets[$uid]['count'] = 1) : $rets[$uid]['count']++;
- (!isset($rets[$uid]['times'])) ? ($rets[$uid]['times'] = 0) : ($rets[$uid]['times'] += (($log['end_time'] ? $log['end_time'] : $log['start_time']) - $log['start_time']));
- }
- }
- }
- $return = [];
- foreach ($rets as $uid => $val) {
- if (isset($rets[$uid]['count'])) {
- $return[$uid] = intval($rets[$uid]['times'] / $rets[$uid]['count']);
- } else {
- $return[$uid] = 0;
- }
- }
- return $return;
- }
- //参评率
- public function cpl($uidArray, $alllogs)
- {
- $rets = kftoKey($uidArray, 1);
- foreach ($rets as $uid => $aaa) {
- foreach ($alllogs as $log) {
- if ($uid == $log['kf_id']) {
- !(isset($rets[$uid]['count'])) ? 1 : $rets[$uid]['count']++;
- isset($rets[$uid]['pl']) ? ($rets[$uid]['pl'] = ($log['evaluate_id'] > 0 ? 1 : 0)) : 0;
- }
- }
- }
- $return = [];
- foreach ($rets as $uid => $val) {
- if (isset($rets[$uid]['count'])) {
- $return[$uid] = $return[$uid]['pl'] / $return[$uid]['cocountunt'];
- } else {
- $return[$uid] = 0;
- }
- }
- return $return;
- }
- //满意度率
- public function mydl($uidArray, $alllogs)
- {
- $rets = kftoKey($uidArray, 1);
- foreach ($rets as $uid => $aaa) {
- foreach ($alllogs as $log) {
- if ($uid == $log['kf_id'] && $log['evaluate_id'] > 0) {
- (!isset($rets[$uid]['count'])) ? ($rets[$uid]['count'] = 1) : $rets[$uid]['count']++;
- (!isset($rets[$uid]['times'])) ? (($log['evaluate_id'] == 1) ? 1 : 0) : (($log['evaluate_id'] == 1) ? $rets[$uid]['times']++ : '');
- }
- }
- }
- $return = [];
- foreach ($rets as $uid => $val) {
- if (isset($rets[$uid]['count']) && isset($rets[$uid]['times'])) {
- $return[$uid] = $rets[$uid]['times'] / $rets[$uid]['count'];
- } else {
- $return[$uid] = 0;
- }
- }
- return $return;
- }
- //平均响应时长
- public function pjxysc($uidArray, $alllogs)
- {
- $rets = kftoKey($uidArray);
- if (empty($alllogs)) {
- return $rets;
- }
- $serids = [];
- foreach ($alllogs as $val) {
- $serids[] = $val['servicelog_id'];
- }
- $times = Db::name('alarm')->field('alarm_corresponding,servicelog_id')->where(['alarm_respond' => 2, 'servicelog_id' => ['IN', $serids]])->select();
- if ($times) {
- foreach ($alllogs as $key => $val) {
- foreach ($times as $sval) {
- if ($val['servicelog_id'] == $sval['servicelog_id']) {
- $alllogs[$key]['corresponding'] = $sval['alarm_corresponding'];
- }
- }
- }
- }
- $rets = kftoKey($uidArray, 1);
- foreach ($rets as $uid => $aaa) {
- foreach ($alllogs as $log) {
- if ($uid == $log['kf_id'] && isset($log['corresponding'])) {
- (!isset($rets[$uid]['count'])) ? ($rets[$uid]['count'] = 1) : $rets[$uid]['count']++;
- (!isset($rets[$uid]['times'])) ? ($rets[$uid]['times'] = intval($log['corresponding'])) : ($rets[$uid]['times'] += intval($log['corresponding']));
- }
- }
- }
- $return = [];
- foreach ($rets as $uid => $val) {
- if (isset($rets[$uid]['count']) && isset($rets[$uid]['times'])) {
- $return[$uid] = intval($rets[$uid]['times'] / $rets[$uid]['count']);
- } else {
- $return[$uid] = 0;
- }
- }
- return $return;
- }
- //今日休息时长统计
- public function jrxxsc($uidArray, $alllogs)
- {
- $kfuidarray = array_map(function ($i) {
- return 'KF' . $i;
- }, $uidArray);
- $return = kftoKey($uidArray);
- $rets = kftoKey($uidArray, 1);
- $today = date("Y-m-d");
- $ret = Db::name('kfstatetimes')->field('kfuid,stime')->where(['sday' => $today, 'kfuid' => ['IN', $kfuidarray], 'kstatus' => 3])->select();
- if (!$ret) {
- return $return;
- }
- foreach ($return as $uid => $ttttt) {
- foreach ($ret as $val) {
- $ruid = trim($val['kfuid'], 'KF');
- if ($uid == $ruid) {
- $return[$uid] = $val['stime'];
- }
- }
- }
- return $return;
- }
- public function getTodayServiceData($kfuidArray = [])
- {
- $today_begin = strtotime(date("Y-m-d"));
- $today_end = $today_begin + 86400;
- $filds = "servicelog_id,group_id,kf_id,user_id,status,start_time,intime,end_time,evaluate_id";
- if ($kfuidArray) {
- $ret = Db::name('service_log')->field($filds)->where(['start_time' => ['>=', $today_begin], 'kf_id' => ['IN', $kfuidArray]])->where(['start_time' => ['<', $today_end]])->select();
- } else {
- $ret = Db::name('service_log')->field($filds)->where(['start_time' => ['>=', $today_begin]])->where(['start_time' => ['<', $today_end]])->select();
- }
- return $ret;
- }
- //获取在线客服ip地址
- public function getKfinlineip()
- {
- $ret = Db::name('kfonline')->select();
- $return = [];
- if ($ret) {
- foreach ($ret as $item) {
- //'KF22'=>'192.168.1.1';
- $return[$item['uid']] = $item['ip'];
- }
- }
- return $return;
- }
- }
|