<?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;
        }
        $kfuidarray_index = kftoKey($kfuidarray, 1);
        $uidsname = $this->uidName($kfuidarray);
        $kfip = $this->getKfinline();

        foreach ($kfuidarray as $uid => $val) {
            $kfuidarray_index[$uid]['ip'] = $kfip['KF' . $uid]['ip'];
            $kfuidarray_index[$uid]['status'] = $this->getStatusName($kfip['KF' . $uid]['status']);
            $kfuidarray_index[$uid]['uid'] = $uid;
            $kfuidarray_index[$uid]['uidname'] = $uidsname[$uid];
            $kfuidarray_index[$uid]['hhl'] = 0;
            $kfuidarray_index[$uid]['jdl'] = 0;
            $kfuidarray_index[$uid]['pjhysc'] = 0;
            $kfuidarray_index[$uid]['cpl'] = 0;
            $kfuidarray_index[$uid]['mydl'] = 0;
            $kfuidarray_index[$uid]['pjxysc'] = 0;
            $kfuidarray_index[$uid]['jrxxsc'] = 0;
            $kfuidarray_index[$uid]['fxx'] = $this->makeBtn($uid);;
        }

        $todayall = $this->getTodayServiceData($kfuidarray);
        if (!$todayall) {
            GOTO DOLAST;
        }

        $uids = $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);

        foreach ($kfuidarray_index as $uid => $val) {
            $kfuidarray_index[$uid]['ip'] = $kfip['KF' . $uid]['ip'];
            $kfuidarray_index[$uid]['status'] = $this->getStatusName($kfip['KF' . $uid]['status']);
            $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);
        }

        DOLAST:
        $last = [];
        foreach ($kfuidarray_index as $val) {
            $last[] = $val;
        }

        return $last;
    }

    public function getStatusName($status)
    {
        $r = ['0' => '离线', '1' => '在线', '2' => '隐身', '3' => '休息'];
        return isset($r[$status]) ? $r[$status] : 'Error';
    }

    // 生成按钮
    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 getKfinline()
    {
        $ret = Db::name('kfonline')->select();
        $return = [];
        if ($ret) {
            foreach ($ret as $item) {
                //'KF22'=>'192.168.1.1';
                $return[$item['uid']] = $item;
            }
        }
        return $return;
    }

}