<?php

namespace app\home\controller;

use think\Lang;

class Glservice extends AdminControl
{
    const EXPORT_SIZE = 5000;

    /**
     * 客服统计
     * @return mixed
     */
    public function index(){
        $srwh = array();
        $srwh['suosgs'] = session('service_pid');
        $gskefz =Model('Admingroup')->getAdminGroupList($srwh,'group_id,group_name');//公司所有客服组
        
        $readok = input('post.kefu_keywords');
        $kefuz = input('post.record_readok');
        $model_service = Model('Service');
        $condition = array();
        if($readok){
            $condition['service_name'] = $readok;
        }
        if($kefuz){
            $condition['service_group_id'] = $kefuz;
        }
        $condition['service_pid'] = session('service_pid');
        $kefulist = $model_service->getServiceList($condition,$field = '',10);
        
        if($kefulist){
            //所属权限组
            for ($i=0; $i < count($kefulist); $i++) { 
                $ner['group_id'] = $kefulist[$i]['service_group_id'];
                $sqxz[] = Model('Admingroup')->getOneAdmingroup($ner,'group_name');
            }
            $this->assign('sqxz',$sqxz);
            //客服满意率
            for ($ie=0; $ie < count($kefulist); $ie++) { 
                $ser['sid'] = $kefulist[$ie]['service_id'];
                $kfrate[] = Model('Chatlog')->chatlogcoRate($ser);
            }
            $this->assign('kfrate',$kfrate);//客服满意率
            //客服挽留率
            for ($i=0; $i < count($kefulist); $i++) { 
                $wer['sid'] = $kefulist[$i]['service_id'];
                $wlrate[] = Model('Chatlog')->chatlogDetainment($wer);
            }
            $this->assign('wlrate',$wlrate);
            //客服转化率
            for ($i=0; $i < count($kefulist); $i++) { 
                $ler['sid'] = $kefulist[$i]['service_id'];
                $zhrate[] = Model('Chatlog')->chatlogConversion($ler);
            }
            $this->assign('zhrate',$zhrate);
        }

        $this->assign('gskefz',$gskefz);
        $this->assign('kefulist',$kefulist);
        $this->assign('show_page', $model_service->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch();
   }

    /**
    * 导出第一步
    */
    public function export_step1()
    {
        $model_service = Model('Service');
        $condition = array();
        $condition['service_pid'] = session('service_pid');
        $kefulist = $model_service->getServiceList($condition,$field = '',10);
        $count = db('Service')->where($condition)->count();

        if (!is_numeric(input('param.curpage'))) {
            $count = db('Service')->where($condition)->count();
            $array = array();
            if ($count > self::EXPORT_SIZE) {    //显示下载链接
                $page = ceil($count / self::EXPORT_SIZE);
                for ($i = 1; $i <= $page; $i++) {
                    $limit1 = ($i - 1) * self::EXPORT_SIZE + 1;
                    $limit2 = $i * self::EXPORT_SIZE > $count ? $count : $i * self::EXPORT_SIZE;
                    $array[$i] = $limit1 . ' ~ ' . $limit2;
                }
                $this->assign('list', $array);
                $this->assign('murl', url('adminlog/list'));
                return $this->fetch('excel');
            }else{    
                //如果数量小，直接下载
                $data = db('Service')->where($condition)->order('service_id desc')->limit(self::EXPORT_SIZE)->select();
                if($kefulist){
                    for ($ie=0; $ie < count($kefulist); $ie++) { 
                        $ser['sid'] = $kefulist[$ie]['service_id'];
                        $kfrate[] = Model('Chatlog')->chatlogcoRate($ser);
                    }//客服满意率
                    for ($i=0; $i < count($kefulist); $i++) { 
                        $wer['sid'] = $kefulist[$i]['service_id'];
                        $wlrate[] = Model('Chatlog')->chatlogDetainment($wer);
                    }//客服挽留率
                    for ($i=0; $i < count($kefulist); $i++) { 
                        $ler['sid'] = $kefulist[$i]['service_id'];
                        $zhrate[] = Model('Chatlog')->chatlogConversion($ler);
                    }//客服转化率
                }
                $this->createExcel($data,$kfrate,$wlrate,$zhrate);
            }
        }else{    
            //下载
            $limit1 = (input('param.curpage') - 1) * self::EXPORT_SIZE;
            $limit2 = self::EXPORT_SIZE;
            $data = db('Service')->where($condition)->order('service_id desc')->limit("{$limit1},{$limit2}")->select();
            $this->createExcel($data);
        }
    }

    /**
     * 生成excel
     *
     * @param array $data
     */
    private function createExcel($data = array(),$kfrate,$wlrate,$zhrate)
    {
        $excel_obj = new \excel\Excel();
        $excel_data = array();
        //设置样式
        $excel_obj->setStyle(array(
            'id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')
        ));
        //header
        $excel_data[0][] = array('styleid' => 's_title', 'data' => "客服名");
        $excel_data[0][] = array('styleid' => 's_title', 'data' => "满意率(百分比)");
        $excel_data[0][] = array('styleid' => 's_title', 'data' => "转化率(百分比)");
        $excel_data[0][] = array('styleid' => 's_title', 'data' => "挽留率(百分比)");
        foreach ((array)$data as $k => $v) {
            $tmp = array();
            $tmp[] = array('data' => $v['service_name']);
            $tmp[] = array('data' => $kfrate[$k] .'%');
            $tmp[] = array('data' => $wlrate[$k] .'%');
            $tmp[] = array('data' => $zhrate[$k] .'%');
            $excel_data[] = $tmp;
        }
        $excel_data = $excel_obj->charset($excel_data, CHARSET);
        $excel_obj->addArray($excel_data);
        $excel_obj->addWorksheet($excel_obj->charset(lang('admin_log'), CHARSET));
        $excel_obj->generateXML($excel_obj->charset(lang('admin_log'), CHARSET) . input('param.curpage') . '-' . date('Y-m-d-H', time()));
    }

}