<?php


/**
 * Class zsjmAction
 */
class zsjmAction extends baseAction
{
    //列表页
    public function index()
    {
        LibRule::Check('ZS01');
        $param = mvc::$URL_PARAMS;
        $page = isset($param['page']) && $param['page'] > 0 ? (int) $param['page'] : 1;
        $pageSize = empty($_COOKIE['perPage'])? 20: (int) $_COOKIE['perPage'];
        $list = $this->searchList($param,$page,$pageSize);
        LibTpl::Set('title','招商加盟列表');
        LibTpl::Set('param',$param);
        LibTpl::Set('page',LibPage::Show($list['count'], $pageSize));
        LibTpl::Set('list',$list['list']);
        LibTpl::Put();
    }

    /**
     * @param $param array 参数
     * @param $page int 页数
     * @param $pageSize int 每页多少条
     * @return array
     */
    public function searchList($param,$page,$pageSize)
    {
        LibRule::Check('ZS01');
        $db = new PjlBase();
        $where = '';
        if($param['status']){
            if($param['status'] == 1){
                $where .= sprintf(" and zsl_etime < %d",time());
            }else{
                $where .= sprintf(" and zsl_etime > %d",time());
            }
        }
        if($param['u_name']){
            $post = [
                'filed'=>['u_id','u_name'],
                'where'=>sprintf(" and u_name like '%%%s%%'", $param['u_name'])
            ];
            $LibYpApi = new LibYpApi();
            $userInfo = $LibYpApi->get('user/getUser', [], $post);
            if(!empty($userInfo['data'])){
                $userInfo = implode(",",array_column($userInfo['data'],'u_id'));
            }else{
                $userInfo = '-1';
            }
            $where .= sprintf(" and zsl_pu_id in (%s)",$userInfo);
        }
        if($param['betime'])
         {
             $temp = explode(' ~ ',$param['betime']);
             $where .= sprintf(" and zsl_atime between %s and %s",$temp['0'],$temp['1']);
         }
        if($param['name']){
             $where .= sprintf(" and zsl_showname like '%%%s%%'",$param['name']);
         }
        $count = $db->get("zs_zhaoshang_list",['count(*) as count'],$where,true);
        $list = $db->get("zs_zhaoshang_list",['zsl_id','zsl_toemail','zsl_atime','zsl_logo','zsl_pu_id','zsl_btime','zsl_etime','zsl_showname', 'zsl_maintel'],sprintf('%s order by zsl_etime desc limit %s,%s',$where,($page-1)*$pageSize,$pageSize));
        $post = [
                'filed'=>['u_id', 'u_name'],
                'where'=>sprintf(" and u_id in (%s)", implode(',',array_column($list['data'],'zsl_pu_id')))
        ];
        $LibYpApi = new LibYpApi();
        $userInfo = $LibYpApi->get('user/getUser', [], $post);
        $userInfo = array_column($userInfo['data'],null,'u_id');
        $zslId = array_column($list['data'],'zsl_id');
        if(!empty($zslId)){
            $zslId = implode(",",$zslId);
            $brandTemp = [];
            $YpcBase = new YpcBase();
            $brand = $YpcBase->Get('yp_brand', ['b_id as id','b_name as name'], ' order by b_initial');
            $brand = array_column($brand['data'],'name','id');
            $brandList = $db->get("zs_zhaoshang_brand",['*'],sprintf(" and zb_zsl_id in (%s)",$zslId));
            if(!empty($brandList['data'])){
                foreach ($brandList['data'] as $value)
                {
                    if($value['zb_brand_id'] == 0){
                        $value['brand_name'] = '所有品牌';
                    }else{
                        $value['brand_name'] = $brand[$value['zb_brand_id']];
                    }
                    $brandTemp[$value['zb_zsl_id']][] = $value;

                    if(count($brandTemp[$value['zb_zsl_id']]) > 3)
                    {
                        array_multisort(array_column($brandTemp[$value['zb_zsl_id']],'zb_brand_id'),SORT_DESC,$brandTemp[$value['zb_zsl_id']]);

                        array_pop($brandTemp[$value['zb_zsl_id']]);
                    }
                }
            }
            $partList = $db->get("zs_zhaoshang_part",['*'],sprintf(" and zp_zsl_id in (%s)",$zslId));
            $zsrShow = $db->get("zs_zhaoshang_report",['zsr_zsl_id','sum(zsr_show) as zsr_show','sum(zsr_tel_count) as zsr_tel_count'],sprintf(" and zsr_zsl_id in (%s) group by zsr_zsl_id",$zslId));
            $applyCount = $db->get("zs_zhaoshang_apply",['count(*) as count','za_zsl_id'],sprintf(" and za_zsl_id in (%s) group by za_zsl_id",$zslId));
            if(!empty($applyCount['data'])){
                $applyCount = array_column($applyCount['data'],'count','za_zsl_id');
            }else{
                $applyCount = [];
            }
            if(!empty($zsrShow['data'])){
                $zsrShow = array_column($zsrShow['data'],null,'zsr_zsl_id');
            }else{
                $zsrShow = [];
            }
            if(!empty($partList['data'])){
                $partTemp = [];
                foreach ($partList['data'] as $value)
                {
                    //todo 全部展示
                    $partTemp[$value['zp_zsl_id']][] = $value['zp_part'];
                }
            }
        }


        foreach ($list['data'] as &$value)
        {
            if(is_array($zsrShow) && isset($zsrShow[$value['zsl_id']])){
                $value['zsr_show'] = $zsrShow[$value['zsl_id']]['zsr_show'];
                $value['zsr_tel_count'] = $zsrShow[$value['zsl_id']]['zsr_tel_count'];
            }else{
                $value['zsr_show'] = '';
                $value['zsr_tel_count'] = '';
            }
            if(is_array($applyCount) && isset($applyCount[$value['zsl_id']])){
                $value['apply_count'] = $applyCount[$value['zsl_id']];
            } else {
                $value['apply_count'] = 0;
            }
            if(isset($partTemp[$value['zsl_id']])){
                $value['part'] = implode(',',$partTemp[$value['zsl_id']]);
            }
            if(isset($brandTemp[$value['zsl_id']])) {
                $value['brand'] = implode(',',array_column($brandTemp[$value['zsl_id']],'brand_name'));
            }
            if(isset($userInfo[$value['zsl_pu_id']]))
            {
                $value['user_info'] = $userInfo[$value['zsl_pu_id']];
                
            }
            $temp = (int) $value['zsl_etime'] - time();
            if($temp < 0){
                $value['time_value'] = '<span class="layui-btn layui-btn-danger layui-btn-xs">已过期'.ceil($temp / (3600*24)).'天</span>';
            }else{
                $value['time_value'] = '<span class="layui-btn layui-btn-normal layui-btn-xs">剩余'.ceil($temp / (3600*24)).'天</span>';
            }
        }
        return [
          'list' => $list['data'],
          'count' => $count['data']['count'] ?: 0
        ];
    }

    //招商添加页面
    public function addPage()
    {
        LibRule::Check('ZS02');
        $param = mvc::$URL_PARAMS;
        $db = new PjlBase();
        if((int) $param['zsl_id'] > 0) {
            $data = $db->get("zs_zhaoshang_list", ['*'], sprintf(sprintf(" and zsl_id=%d", (int)$param['zsl_id'])), true);
            $data = $data['data'];
            $zslId = $data['zsl_id'];
            $isBrand = $db->get("zs_zhaoshang_brand", ['*'], sprintf(" and zb_zsl_id=%d", $zslId));
            if (count($isBrand['data']) == 1 && $isBrand['data']['0']['zb_brand_id'] == 0) {
                $isBrand['data']['0'] = [
                    'zb_zsl_id' => $zslId,
                    'zb_brand_id' => 0,
                    'name' => '所有品牌',
                ];
            } else {
                $brandId = array_column($isBrand['data'], 'zb_brand_id');
                if (!empty($brandId)) {
                    $YpcBase = new YpcBase();
                    $brandList = $YpcBase->Get('yp_brand', ['b_id as id', 'b_name as name'], sprintf(" and b_id in (%s) order by b_initial", implode(",", $brandId)));
                    $brandList = array_column($brandList['data'], null, 'id');
                    foreach ($isBrand['data'] as &$value) {
                        $value['name'] = $brandList[$value['zb_brand_id']]['name'];
                    }
                }
            }
            LibTpl::Set('isBrand', $isBrand['data']);
            $part = $db->get("zs_zhaoshang_part", ['*'], sprintf(" and zp_zsl_id=%d", $zslId));
            if (!empty($part['data'])) {
                $part = implode(',', array_column($part['data'], 'zp_part'));
            } else {
                $part = '';
            }
            $LibYpApi = new LibYpApi();

            $post = [
                'filed' => ['u_id', 'u_name', 'u_mobile'],
                'where' => sprintf(" and u_id=%d", $data['zsl_pu_id'])
            ];
            $userInfo = $LibYpApi->get('user/getUser', [], $post,true);
            LibTpl::Set('user', $userInfo['data']['0'] ?: []);
            LibTpl::Set('part', $part ?: '全车件');
            $data['zsl_web'] = explode("://",$data['zsl_web']);
            $data['zsl_imgs'] = !empty($data['zsl_imgs']) ? explode(',', $data['zsl_imgs']) : '';
            $data['zsl_config'] = json_decode($data['zsl_config']);
            $data['zsl_links'] = json_decode($data['zsl_links'], true);
            $data['zsl_tags'] = explode(',',$data['zsl_tags']);
            LibTpl::Set('title','编辑招商');
        }else{
            $data = array (
                'zsl_id' => '',
                'zsl_pu_id' => '',
                'zsl_btime' => '',
                'zsl_etime' => '',
                'zsl_showname' => '',
                'zsl_logo' => '',
                'zsl_short_des' => '',
                'zsl_des' => '',
                'zsl_imgs' => '',
                'zsl_config' => [],
                'zsl_links' => [],
                'zsl_toemail' => '',
                'zsl_atime' => ''
            );
            LibTpl::Set('title','添加招商');
        }
        LibTpl::Set('data',$data);
        LibTpl::Put();
    }

    /**
     * 添加招商信息接口
     * @throws Exception
     */
    public function add()
    {
		LibRule::Check('ZS02');
            if($_POST['brand_all']){
                $brand = ['0'];
            }else{
                if(!empty($_POST['zs_brand'])){
                    $brand = array_map(function($value){
                        return (int) trim($value);
                    }, $_POST['zs_brand']);
                }else{
                    $brand = [];
                }
            }
            $part = '';
            if($_POST['zs_part_type'] == 2) {
                if(!empty($_POST['zs_part'])) {
                    $part = str_replace("，", ",", $_POST['zs_part']);
                    $part = str_replace(";",",",$part);
                    $part = str_replace("；",",",$part);
                    $part = explode(",", $part);
                    $part = array_map("trim", $part);
                }
            }else if($_POST['zs_part_type'] == 1){
                $part = ['全车件'];
            }
        $tags = '';
        if(!empty($_POST['zsl_tags'])){
            $tags = $_POST['zsl_tags'];
            if(count($tags) > 6){
                LibFc::ajaxJsonEncode(['status' => false,'data' => '最多支持6个标签']);
            }
            foreach ($tags as $key => &$tag){
                if(empty($tag)){
                    unset($tags[$key]);
                    continue;
                }
                $tag = trim($tag);
                if(mb_strlen($tag) > 6){
                    LibFc::ajaxJsonEncode(['status' => false,'data' => '标签长度超过了限制6']);
                }
            }
            if(!empty($tags)){
                $tags = implode(',',$tags);
            }else{
                $tags = '';
            }
        }
            $city = $_POST['city'];
            $arr = [];
            if(!empty($_POST['link'])){
                $link = $_POST['link'];
                foreach ($link['type'] as $key => $value)
                {
                    if($value == 'url'){
                        if(substr($link['key'][$key],0,4) !== 'http'){
                            LibFc::ajaxJsonEncode(['status' => false,'data' => '附加信息中的网址:'.$link['key'][$key].'错误,请以http或者https开头填写网址']);
                        }
                    }
                    if(!empty($link['value'][$key])){
                        $arr[] = [
                            'key' => $link['key'][$key] ?: '',
                            'title' => $link['title'][$key],
                            'value' => $link['value'][$key],
                            'sort' => $link['sort'][$key],
                            'type' => $value
                        ];
                    }
                }
                //排序
                array_multisort(array_column($arr,'sort'),SORT_ASC,$arr);
            }
            if(!empty($_POST['zsl_imgs'])){
                $tempImg = [];
                foreach ($_POST['zsl_imgs']['src'] as $key => $value){
                    $tempImg[] = [
                        'src' => $value,
                        'sort' => $_POST['zsl_imgs']['sort'][$key]
                    ];
                };
                array_multisort($_POST['zsl_imgs']['sort'],SORT_ASC,$tempImg);
                $tempImg = array_column($tempImg,'src');
                $tempImg = implode(",",$tempImg);
            }else{
                $tempImg = '';
            }
            $_POST['zsl_web'] =  !empty($_POST['zsl_web']) ? $_POST['zsl_web_ss']."://".$_POST['zsl_web'] : '';
            if(empty($_POST['zsl_btime'])) LibFc::ajaxJsonEncode(['status' => false,'data' => '时间不可为空']);
            $temp = array_map('trim',explode(" - ", $_POST['zsl_btime']));
            $_POST['zsl_btime'] = $temp['0'];
            $_POST['zsl_etime'] = $temp['1'];
            if((int) $_POST['ads']['ads_u_id'] <= 0)  LibFc::ajaxJsonEncode(['status' => false,'data' => '所属用户不可为空']);
            //if(empty($_POST['imgs']['0'])) LibFc::ajaxJsonEncode(['status' => false,'data' => '列表图不可为空']);
            if(empty($_POST['zsl_showname'])) LibFc::ajaxJsonEncode(['status' => false,'data' => '显示名称不可为空']);
            if(mb_strlen($_POST['zsl_showname']) > 64) LibFc::ajaxJsonEncode(['status' => false,'data' => '显示名称长度超过了64']);
            if(mb_strlen($_POST['zsl_short_des']) > 256) LibFc::ajaxJsonEncode(['status' => false,'data' => '概要长度超过了64']);
            if(mb_strlen($tempImg) > 128) LibFc::ajaxJsonEncode(['status' => false,'data' => '轮播图长度超过了128']);
            if(mb_strlen($_POST['zsl_config']) > 128) LibFc::ajaxJsonEncode(['status' => false,'data' => '配置颜色长度超过了128']);
            if(mb_strlen($_POST['zsl_links']) > 1024) LibFc::ajaxJsonEncode(['status' => false,'data' => '联系方式长度超过了128']);
            if(mb_strlen($_POST['zsl_toemail']) > 128) LibFc::ajaxJsonEncode(['status' => false,'data' => '邮箱长度超过了128']);
            if(mb_strlen(json_encode($arr)) > 4000) LibFc::ajaxJsonEncode(['status' => false,'data' => '联系方式长度超过了4000']);
            if(mb_strlen($_POST['zsl_hot']) > 64) LibFc::ajaxJsonEncode(['status' => false,'data' => '招商热点长度超过了限制64']);
            if(mb_strlen($_POST['zsl_zhuying']) > 256) LibFc::ajaxJsonEncode(['status' => false,'data' => '主营项目长度超过了限制256']);
            if(mb_strlen($_POST['zsl_email']) > 128) LibFc::ajaxJsonEncode(['status' => false,'data' => '联系邮箱长度超过了限制128']);
            if(mb_strlen($_POST['zsl_web']) > 128) LibFc::ajaxJsonEncode(['status' => false,'data' => '公司官网长度超过了限制128']);
            if(mb_strlen($_POST['zsl_addr']) > 256) LibFc::ajaxJsonEncode(['status' => false,'data' => '公司地址长度超过了限制256']);
            $model = new PjlZsjm();

            //zsl_id 主键ID
            $postData = [
                'zsl_addr' => trim($_POST['zsl_addr']),
                'zsl_web' => trim($_POST['zsl_web']),
                'zsl_email' => trim($_POST['zsl_email']),
                'zsl_zhuying' => trim($_POST['zsl_zhuying']) ?: '',
                'zsl_tags' => $tags,
                'zsl_hot' => trim($_POST['zsl_hot']),
                'zsl_pu_id' => (int) $_POST['ads']['ads_u_id'],
                'zsl_btime' => strtotime($_POST['zsl_btime']),  // '开始时间',
                'zsl_etime' => strtotime($_POST['zsl_etime']),// 结束时间,
                'zsl_showname' => trim($_POST['zsl_showname']) ?: '', //'显示名称',
                'zsl_logo' => $_POST['imgs']['0'] ?: '', //v显示logo
                'zsl_short_des' => $_POST['zsl_short_des'] ?: '',//'概要',
                'zsl_maintel' => $_POST['zsl_maintel'] ?: '',
                'zsl_des' => $_POST['zsl_des'] ?: '', // '详情介绍',
                'zsl_youshi' => $_POST['zsl_youshi'] ?: '',//加盟优势
                'zsl_tiaojian' => $_POST['zsl_tiaojian'],//加盟条件
                'zsl_imgs' =>  $tempImg, //轮播图 多张逗号分隔',
                'zsl_config' => $_POST['zsl_config'] ? json_encode(['color' => $_POST['zsl_config']]) : '{}', //'json配置 颜色...{color:#eee}',
                'zsl_links' => json_encode($arr), //varchar(1024) COLLATE utf8_unicode_ci DEFAULT '{}' COMMENT '联系方式  参考广告',
                'zsl_toemail' => $_POST['zsl_toemail'] ?: '', //varchar(128) COLLATE utf8_unicode_ci DEFAULT '' COMMENT '用于接收申请的邮箱',
                'zsl_atime' => time() //int(11) DEFAULT NULL COMMENT '添加时间',
            ];
            $result = $model->adds($postData,(int) $_POST['zsl_id'] ?: 0,$part,$brand,$city);
            LibFc::ajaxJsonEncode($result);
    }

    //申请记录
    public function applyList()
    {
        LibRule::Check('ZS03');
        $param = mvc::$URL_PARAMS;
        $page = isset($param['page']) && $param['page'] > 0 ? (int) $param['page'] : 1;
        $pageSize = empty($_COOKIE['perPage'])? 20: (int) $_COOKIE['perPage'];
        $list = $this->searchApplyList($param,$page,$pageSize);
        LibTpl::Set("title");
        LibTpl::Set('page',LibPage::Show($list['count'], $pageSize));
        LibTpl::Set('list',$list['list']);
        LibTpl::Set('statusName',$list['status_name']);
        LibTpl::Set('param',$param);
        LibTpl::Put();
    }

    /**
     * @param $param array 参数
     * @param $page int 页数
     * @param $pageSize int 每页多少调
     * @return array
     */
    public function searchApplyList($param,$page,$pageSize)
    {
        $statusName = [
            '-1' =>  '无效',
            '0' => '处理中',
            '1' => '有效'
        ];
        $where = '';
        if(is_numeric($param['status'])){
            $where .= sprintf(" and za_status='%s'",$param['status']);
        }
        $db = new PjlBase();

        $count = $db->get("zs_zhaoshang_apply left join zs_zhaoshang_list on zsl_id=za_zsl_id",['count(*) as count'],$where,true);
        $list = $db->get("zs_zhaoshang_apply left join zs_zhaoshang_list on zsl_id=za_zsl_id",['*'],sprintf(" %s order by za_atime desc limit %s,%s",$where,($page-1)*$pageSize,$pageSize));
        foreach ($list['data'] as &$value)
        {
            $value['status_name'] = $statusName[$value['za_status']];
        }

        return [
            'list' => $list['data'] ?: [],
            'count' => $count['data']['count'] ?: 0,
            'status_name' => $statusName
        ];
    }

    /**
     * 添加城市接口
     * @throws Exception
     */
    public function addCityPage()
    {
            $param = mvc::$URL_PARAMS;
            $YpcBase = new YpcBase();
            $db = new PjlBase();
            $treeCity = $db->get("yp_province_city",['pc_id as id','pc_pid as pId','pc_name as name'],sprintf(" and (pc_pid in(select pc_id from yp_province_city where pc_pid=0) or pc_pid=0)"));
            $isCity = $db->get("zs_zhaoshang_city",['*'],sprintf(" and zc_zsl_id=%d",(int) $param['zsl_id']));
            if(count($isCity['data']) == 1 && $isCity['data']['0']['zc_pc_id'] == 0)
            {
                array_walk($treeCity['data'], function(&$v, $k,$arr) {
                    $v = array_merge($v, $arr);
                },['checked' => true ,'open' => true]);
                $treeCity['is_all'] = true;
            }
            else
            {
                $isCity = array_column($isCity['data'],'zc_zsl_id','zc_pc_id');
                foreach ($treeCity['data'] as &$value){
                    $value['checked'] = false;
                    if(isset($isCity[$value['id']])){
                        $value['checked'] = true;
                       // $value['open'] = true;
                    }
                }
                $treeCity['is_all'] = false;
            }
            LibFc::ajaxJsonEncode($treeCity);
    }

    //todo 添加品牌页面
    public function addBrandPage()
    {
         LibFc::ajaxJsonEncode((new YpcBase)->Get('yp_brand', ['b_id as id','b_name as name'], ' order by b_initial'));
    }

    /**
     * 处理申请记录
     * @return array
     * @throws Exception 不是ajax请求
     */
    public function withApply()
    {
		LibRule::Check('ZS04');
		$zaId = (int) $_POST['za_id'];
		if(!$zaId) LibFc::ajaxJsonEncode(['status' => false,'data' => '参数错误']);
		//if(empty($_POST['za_check_remark'])) LibFc::ajaxJsonEncode(['status' => false,'data' => '备注不可为空']);
		if(mb_strlen($_POST['za_check_remark']) > 255) LibFc::ajaxJsonEncode(['status' => false,'data' => '备注超过了长度']);
		$updateArr = [
			'za_etime' => time(),
			'za_check_remark' => $_POST['za_check_remark'],
			'za_status' => $_POST['za_status']
		];
		$result = (new PjlBase)->set("zs_zhaoshang_apply",$updateArr,sprintf(" and za_id=%d",$zaId));
		return LibFc::ajaxJsonEncode($result);
    }

    /**
     * 删除
     * @throws Exception
     */
    public function deleteWithApply()
    {
        $zaId = (int) $_POST['za_id'];
        if($zaId <= 0)
        {
            throw new \Exception("Request is error");
        }
        $result = (new PjlBase)->del("zs_zhaoshang_apply",sprintf(" and za_id=%d",$zaId));
        LibFc::ajaxJsonEncode($result);
    }

    public function dayStatistics()
    {
        $param = mvc::$URL_PARAMS;
        $fastTime = [
            '本月'=>date('Y-m-01').' ~ '.date('Y-m-d'),   
            '上个月'=>date('Y-m-01', strtotime('-1 month', strtotime(date('Y-m')))).' ~ '.date('Y-m-d', strtotime(date('Y-m-01'))-1),
            '本年'=>date('Y-01-01').' ~ '.date('Y-m-d')
        ];

        $where = '';
        $groupByArr = [
            'day'=>['日统计', '%Y-%m-%d'], 
            'month'=>['月统计', '%Y-%m'],  
            'year'=>['年统计', '%Y']
        ];
        if((int) $param['zsl_id'] > 0){
            $where .= sprintf(" and zsr_zsl_id=%d",(int) $param['zsl_id']);
        }
        if(!empty($param['betime'])){
            $temp = explode('~', $param['betime']);
            $func = function($value){ return date('Ymd',(int) strtotime(trim($value)));};
            $temp = array_map($func, $temp);
            $where .= sprintf(" and zsr_date >= '%s' and zsr_date <= '%s'",$temp['0'],$temp['1']);
            unset($temp);
        }
        switch ($param['group']) {
            case 'month':
                  $list = (new PjlBase)->get("zs_zhaoshang_report",['left(zsr_date,6) as zsr_date','sum(zsr_show) as zsr_show','sum(zsr_tel_count) as zsr_tel_count'],sprintf(" %s group by left(zsr_date,6) order by zsr_date asc",$where));
                break;
            case 'day':
                $list = (new PjlBase)->get("zs_zhaoshang_report",['zsr_date','sum(zsr_show) as zsr_show','sum(zsr_tel_count) as zsr_tel_count'],sprintf(" %s group by zsr_date order by zsr_date asc",$where));
                break;
            default:
                  $list = (new PjlBase)->get("zs_zhaoshang_report",['left(zsr_date,4) as zsr_date','sum(zsr_show) as zsr_show','sum(zsr_tel_count) as zsr_tel_count'],sprintf(" %s group by left(zsr_date,4) order by zsr_date asc",$where));
                break;
        }
        $lists = [];
        if(!empty($list['data'])){
            foreach ($list['data'] as $key => $value) {
                $lists['key'][] = $value['zsr_date'];
                $lists['value'][] = $value['zsr_show'];
                $lists['tel_count'][] = $value['zsr_tel_count'];
            }
        }
        $lists = json_encode($lists);
        $areaList = (new PjlBase)->get("zs_zhaoshang_report join yp_province_city on pc_id=zsr_pid and pc_pid=0",['pc_name','sum(zsr_show) as zsr_show','sum(zsr_tel_count) as zsr_tel_count'],sprintf(" %s group by zsr_pid",$where));
        $jsCity = array(
           '西藏' => 0, '青海' => 0, '宁夏' => 0, '海南' => 0, '甘肃' => 0, '贵州' => 0,
           '新疆' => 0, '云南' => 0, '重庆' => 0, '吉林' => 0, '山西' => 0, '天津' => 0,
           '江西' => 0, '广西' => 0, '陕西' => 0, '黑龙江' => 0, '内蒙古' => 0, '安徽' => 0,
           '北京' => 0, '福建' => 0, '上海' => 0, '湖北' => 0, '湖南' => 0, '四川' => 0,
           '辽宁' => 0, '河北' => 0, '河南' => 0, '浙江' => 0, '山东' => 0, '江苏' => 0,
           '广东' => 0
        );
       if(!empty($areaList['data']))
       {
           foreach ($areaList['data'] as $value)
           {
               $name = trim(str_replace("省",'',$value['pc_name']));
               if(isset($jsCity[$name])){
                   $jsCity[$name] = [
                       'zsr_show' => (int) $value['zsr_show'],
                       'zsr_tel_count' => (int) $value['zsr_tel_count'],
                   ];
               }
           }
           unset($value);
       }
       unset($areaList,$db,$zslId);
       $jsCitys = [];
       $max = 0;
       foreach ($jsCity as $key => $value)
       {
           if($max < $value['zsr_show']) $max = $value['zsr_show'];
           $temp = [];
           $temp['name'] = $key;
           $temp['value'] = $value['zsr_show'] ?: 0;
           $temp['tel_count'] = $value['zsr_tel_count'] ?: 0;
           if($value['zsr_show'] > 1000)  $temp['selected'] = true; //阈值
           $jsCitys[] = $temp;
           unset($temp);
       }

        unset($isCity,$key,$value);
        array_multisort(array_column($jsCitys,'value'), SORT_DESC, $jsCitys);
        LibTpl::Set('areaList',json_encode(['data' => $jsCitys,'max' => $max]));
        LibTpl::Set('list',$lists);
        LibTpl::Set('groupByArr',$groupByArr);
        LibTpl::Set('fastTime', $fastTime);
        LibTpl::Set('title',$param['zsl_showname'].'汇总统计报表');
        LibTpl::Set('Params',$param);
        LibTpl::Put();
    }
}

