<?php

    /***************************************************************\
    |                                                               |
    |   开单大师 - 专业的房产ERP管理                                  |
    |                                                               |
    |   © 2019 http://www.kaidandashi.com All rights reserved.      |
    |                                                               |
    |   开单大师由淮南市银泰软件科技有限公司提供技术                    |
    |                                                               |
    |   QQ:984784483                                                |
    |                                                               |
    \***************************************************************/

    namespace Admin\Controller;
    use Think\Controller;
    class Keyuan1Controller extends CommonController {
        public function index(){
            if (I('isget')) {
                $mmzl=I('mmzl');
                $mysike=I('mysike');
                $sousuo=I('sousuo');
                $fengpan=I('fengpan');
                $dshenhe=I('dshenhe');
                $jiqie=I('jiqie');
                $daikan=I('daikan');
                $jrll=I('jrll');
                $gongke=I('gongke');
                $mykeyuan=I('mykeyuan');
                $jiage=I('jiage');
                $mianji=I('mianji');
                $isdel=I('isdel');
                $condition="gongsiid=".session('gongsiid');
                $options_arr = get_options('chengjiao_options',0);
                if ($mmzl == 1 || $mmzl == 2) {
                    $condition.=" and mmzl=$mmzl";
                } else {
                    $condition.=" and (mmzl=1 or mmzl=2)";
                }
                if ($isdel) {
                    $condition.=" and isdel=1";
                }else{
                    $condition.=" and isdel=2";
                }
                $is_rbacfck = _rbac_node_show(865);
                if (!$is_rbacfck) {
                    $condition.=" and fpck=2";
                }
                if ($mysike) {//我的私客
                    $user_id=session('uid');
                    $condition.=" and zhuangtai=".$options_arr['kysike_zhuangtai']." and weihurenid=$user_id";
                }
                if ($sousuo) {//搜索
                    $condition.=" and (xqbianhao like '%$sousuo%' or khxingming like '%$sousuo%' or xqquyu like '%$sousuo%' or xqxiaoqu like '%$sousuo%' or dianhua like '%$sousuo%' or weihuren like '%$sousuo%')";
                }
                if ($fengpan) {//封盘
                    $condition.=" and zhuangtai in (".$options_arr['kyfengpan_zhuangtai'].")";
                }else{
                    $condition.=" and zhuangtai not in (".$options_arr['kyfengpan_zhuangtai'].")";
                }
                if ($dshenhe) {//待审核
                    $condition.=" and zhuangtai=0";
                }else{
                    $condition.=" and zhuangtai>0";
                }
                if ($jiqie) {//急切
                    $condition.=" and jiqie=1";
                }
                if ($daikan) {//带看
                    $condition.=" and daikan=1";
                }
                if ($jrll) {//今日浏览
                    $today="ksj".date("m-d");
                    if (!cookie($today)) {
                        $history=0;
                    }else{
                        $history = cookie($today);
                    }
                    $condition.=" and id in ($history)";
                }
                if ($gongke) {//公客
                    $condition.=" and zhuangtai=".$options_arr['kygongke_zhuangtai'];
                }
                if ($mykeyuan) {//我的客源
                    $user_id=session('uid');
                    $condition.=" and weihurenid=$user_id";
                }
                if ($jiage) {//价格
                    $condition.=" and $jiage>=xqjiage1 and $jiage<=xqjiage2";
                }
                if ($mianji) {//面积
                    $condition.=" and $mianji>=xqmianji1 and $mianji<=xqmianji2";
                }
                $map = "gongsiid=".session('gongsiid');
                $x = _rbac_lists_where(66,0);
                if ($x && $x != "all" && $x != "me") {
                    $map .= " and bumen in ({$x})";
                }elseif ($x == "me") {
                    $map .= " and id=".session('uid');
                }elseif ($x == false) {
                    $map .= " and id=0";
                }
                $count=M('keyuan')->query("select count(*) from __KEYUAN__ a join (select id as e_id,ygmingcheng from __YONGHU__ where {$map}) e on a.weihurenid=e.e_id where {$condition}");
                $Page=new \Think\Page($count['0']['count(*)'],30);
                $show=$Page->show();
                $list="select * from __KEYUAN__ a join (select id as e_id,ygmingcheng from __YONGHU__ where {$map}) e on a.weihurenid=e.e_id where {$condition} order by id DESC limit ".$Page->firstRow.','.$Page->listRows;
                $keyuan=M('keyuan')->query($list);
                $this->assign('keyuan',$keyuan);// 赋值数据集
                $this->assign('count',$count);// 赋值分页输出
                $this->assign('page',$show);
                $this->gtjieduan=M('Peizhi')->where(array('pzming'=>'gtjieduan'))->select();
                $this->laiyuan=M('Peizhi')->where(array('pzming'=>'laiyuan'))->select();
                $this->yongtu=M('Peizhi')->where(array('pzming'=>'yongtu'))->select();
                $this->kehulx=M('Peizhi')->where(array('pzming'=>'yezhulx'))->select();
                $this->display('keyuan1list');
            } else {
                $this->display();
            }
    	}
    	public function addKeyuan(){
            $is_rbac = _rbac_btn_show(68,0);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $mmzl=I('mmzl');
            if (IS_POST) {
                if (session('gongsiid')=="") {
                    $this->error('请重新登录');
                }
                $chengjiao_options=M('options')->where(array('option_name'=>'chengjiao_options'))->find();
                $options_arr = json_decode($chengjiao_options['option_value'],true);
                if (C('IS_KEYUAN_SIKEXZ')==1) {
                    $keyuanshu=M('keyuan')->where(array('weihurenid'=>session('uid'),'zhuangtai'=>$options_arr['kysike_zhuangtai']))->count();
                    $sikexzs=C('IS_KEYUAN_SIKES')-1;
                    if ((C('IS_KEYUAN_SIKEXZ')==1) && (I('zhuangtai')==$options_arr['kysike_zhuangtai']) && ($keyuanshu>$sikexzs)) {
                        $this->error('您的私客已达上限！无法再添加私客！');
                    }
                }
                $dianhua=I('dianhua');
                $xqhuxing1=I('xqhuxing1');
                $xqhuxing2=I('xqhuxing2');
                $xqjiage1=I('xqjiage1');
                $xqjiage2=I('xqjiage2');
                $xqmianji1=I('xqmianji1');
                $xqmianji2=I('xqmianji2');
                $louceng1=I('louceng1');
                $louceng2=I('louceng2');
                $fangling1=I('fangling1');
                $fangling2=I('fangling2');
                $qqhao=I('qqhao');
                $youxiang=I('youxiang');
                $sfzheng=I('sfzheng');
                $beizhu=preg_replace ( "/\s(?=\s)/","\\1", I('beizhu') );
                if(!preg_match("/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[8-9])\d{8}$|^0\d{2,3}-?\d{7,8}$/",$dianhua)){
                    $this->error('手机请输入正确的手机号码或电话号码！');
                }
                if(!preg_match("/^([1-9]{1})$|^(1[0]{1})$/",$xqhuxing1) || !preg_match("/^([1-9]{1})$|^(1[0]{1})$/",$xqhuxing2)){
                    $this->error('户型请输入＞0≤10的整数！');
                }if ($mmzl==1) {
                    if(!preg_match("/^([1-9][0-9]{0,4})$|^(1[0]{5})$/",$xqjiage1) || !preg_match("/^([1-9][0-9]{0,4})$|^(1[0]{5})$/",$xqjiage2)){
                        $this->error('价格请输入＞0≤十亿的整数！');
                    }
                } elseif ($mmzl==2) {
                    if(!preg_match("/^([1-9][0-9]{0,4})$|^(1[0]{5})$/",$xqjiage1) || !preg_match("/^([1-9][0-9]{0,4})$|^(1[0]{5})$/",$xqjiage2)){
                        $this->error('价格请输入＞0≤十万的整数！');
                    }
                }
                if(!preg_match("/^([1-9][0-9]{0,3})$|^(1[0]{4})$/",$xqmianji1) || !preg_match("/^([1-9][0-9]{0,3})$|^(1[0]{4})$/",$xqmianji2)){
                    $this->error('面积请输入＞0≤一万的整数！');
                }
                $cx1=array_keys(I('chaoxiang'));
                $cx2=implode(',',$cx1);
                if (!$cx2) {
                    $this->error('请选择朝向！');
                }
                $zx1=array_keys(I('zhuangxiu'));
                $zx2=implode(',',$zx1);
                if (!$zx2) {
                    $this->error('请选择装修！');
                }
                $pt1=array_keys(I('peitao'));
                $pt2=implode(',',$pt1);
                if (!$pt2) {
                    $this->error('请选择配套！');
                }
                if(!preg_match("/^$|^([1-9]{1})$|^([1-9][0-9]{1})$|^(1[0-9]{2})$|^(2[0]{2})$/",$louceng1) || !preg_match("/^$|^([1-9]{1})$|^([1-9][0-9]{1})$|^(1[0-9]{2})$|^(2[0]{2})$/",$louceng2)){
                    $this->error('楼层请输入＞0≤200的整数！');
                }
                if(!preg_match("/^$|^([1-9][0-9]{0,2})$|^(1[0]{3})$/",$fangling1) || !preg_match("/^$|^([1-9][0-9]{0,2})$|^(1[0]{3})$/",$fangling2)){
                    $this->error('房龄请输入＞0≤1000的整数！');
                }
                if(!preg_match("/^$|^[1-9][0-9]{4,}$/",$qqhao)){
                    $this->error('QQ请输入正确的QQ号！');
                }
                if(!preg_match("/^$|^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/",$youxiang)){
                    $this->error('邮箱请输入正确的邮箱！');
                }
                if(!preg_match("/^$|^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$|^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/",$sfzheng)){
                    $this->error('身份证请输入正确的身份证号！');
                }
                if(!preg_match("/^$|^[\s\S]{0,300}$/",$beizhu)){
                    $this->error('备注只能输入300个字符！');
                }
                $bh=M('bianhao')->where(array('id'=>2))->setInc('bianhao');
                $bh1=M('bianhao')->where(array('id'=>2))->getField('bianhao');
                if ($mmzl==1) {
                    $bhao='MK';
                } elseif ($mmzl==2) {
                    $bhao='ZK';
                } else {
                    $this->error('异常错误');
                }
                if(I('zhuangtai')){
                    $zhuangtai=$options_arr['kysike_zhuangtai'];
                }else{
                    $zhuangtai=$options_arr['kygongke_zhuangtai'];
                }
                if(I('jiqie')){
                    $jiqie=1;
                }else{
                    $jiqie=0;
                }
                $data['jiqie']=$jiqie;
                $data['zhuangtai']=$zhuangtai;
                $data['leixing']=$mmzl;
                $data['fukuan']=I('fukuan');
                $data['gongsiid']=session('gongsiid');
                $data['xqbianhao']=$bhao.$bh1;
                $data['khxingming']=I('khxingming');
                $data['kehulx']=I('kehulx');
                $data['dianhua']=$dianhua;
                $data['xqhuxing1']=$xqhuxing1;
                $data['xqhuxing2']=$xqhuxing2;
                $data['xqjiage1']=$xqjiage1;
                $data['xqjiage2']=$xqjiage2;
                $data['xqmianji1']=$xqmianji1;
                $data['xqmianji2']=$xqmianji2;
                $data['xflinian']=I('xflinian');
                $data['louceng1']=$louceng1;
                $data['louceng2']=$louceng2;
                $data['xqyongtu']=I('xqyongtu');
                $data['fangling1']=$fangling1;
                $data['fangling2']=$fangling2;
                $data['xqquyu']=I('xqquyu');
                $xqxiaoqus=array_unique(array_filter(explode("；",I('xqxiaoqu'))));
                $xqxiaoqu=implode("；",$xqxiaoqus);
                $data['xqxiaoqu']=$xqxiaoqu;
                $data['chaoxiang']=$cx2;
                $data['zhuangxiu']=$zx2;
                $data['peitao']=$pt2;
                $data['xqyuanyin']=I('xqyuanyin');
                $data['khlaiyuan']=I('khlaiyuan');
                $data['kydengji']=I('kydengji');
                $data['gtjieduan']=I('gtjieduan');
                $data['guoji']=I('guoji');
                $data['minzu']=I('minzu');
                $data['sfzheng']=$sfzheng;
                $data['qqhao']=$qqhao;
                $data['youxiang']=$youxiang;
                $data['weixin']=I('weixin');
                $data['jtgongju']=I('jtgongju');
                $data['chexing']=I('chexing');
                $data['beizhu']=$beizhu;
                $data['lururenid']=session('uid');
                $data['weihurenid']=session('uid');
                $data['weihuren']=session('ygmingcheng');
                $data['mmzl']=$mmzl;
                $data['lurusj']=time();
                $data['zhgjsj']=time();
                $data['xiugaisj']=time();
                $data['zhuangtai']=$zhuangtai;
                $isok=M('Keyuan')->add($data);
                if ($isok) {
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }else{
                if ($mmzl==1) {
                    $this->fukuan=M('Peizhi')->where(array('pzming'=>'fukuan','zhuangtai'=>1))->order('xsshunxu asc')->select();
                } elseif ($mmzl==2){
                    $this->fukuan=M('Peizhi')->where(array('pzming'=>'zlfukuan','zhuangtai'=>1))->order('xsshunxu asc')->select();
                } else {
                    $this->error('异常错误');
                }
                $this->yongtu=M('Peizhi')->where(array('pzming'=>'yongtu','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->chaoxiang=M('Peizhi')->where(array('pzming'=>'chaoxiang','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->kehulx=M('Peizhi')->where(array('pzming'=>'yezhulx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->peitao=M('Peizhi')->where(array('pzming'=>'peitao','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->laiyuan=M('Peizhi')->where(array('pzming'=>'laiyuan','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->zhuangxiu=M('Peizhi')->where(array('pzming'=>'zhuangxiu','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->xflinian=M('Peizhi')->where(array('pzming'=>'xflinian','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->guoji=M('Peizhi')->where(array('pzming'=>'guoji','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->minzu=M('Peizhi')->where(array('pzming'=>'minzu','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->kydengji=M('Peizhi')->where(array('pzming'=>'kydengji','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khzhuangtai=M('Peizhi')->where(array('pzming'=>'khzhuangtai','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->jtgongju=M('Peizhi')->where(array('pzming'=>'jtgongju','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->gtjieduan=M('Peizhi')->where(array('pzming'=>'gtjieduan','zhuangtai'=>1))->order('xsshunxu asc')->select();
                if (C('IS_KEYUAN_SIKEXZ')==1) {
                    $this->keyuanshu=M('keyuan')->where(array('weihurenid'=>session('uid'),'zhuangtai'=>$options_arr['kysike_zhuangtai']))->count();
                    $this->sikexzs=C('IS_KEYUAN_SIKES')-1;
                }
                $this->display();
            }
    	}
        public function neirong1(){
            $keyuan=M('Keyuan')->where(array('id'=>I('id'),'gongsiid'=>session('gongsiid')))->find();
            $is_rbac = _rbac_btn_show(75,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $is_rbacfck = _rbac_node_show(865);
            if (!$is_rbacfck && ($keyuan['fpck'] != 2)) {
                $this->redirect('Index/error');
            }
            $is_rbacfdh = _rbac_node_show(864);
            if ($is_rbacfdh || ($keyuan['fpdh'] == 2)) {
                $this->is_rbacfdh=1;
            }
            $chengjiao_options=M('options')->where(array('option_name'=>'chengjiao_options'))->find();
            $options_arr = json_decode($chengjiao_options['option_value'],true);
            $this->options_arr=$options_arr;
            $this->kyfengpan_zhuangtai=explode(',',$options_arr['kyfengpan_zhuangtai']);
            $this->assign('keyuan',$keyuan);
            $this->lrdh=M('Yonghu')->where(array('id'=>$keyuan['lururenid']))->getField('dianhua');
            $this->whdh=M('Yonghu')->where(array('id'=>$keyuan['weihurenid']))->getField('dianhua');
            $this->fpdh=M('Yonghu')->where(array('id'=>$keyuan['fengpanrenid']))->getField('dianhua');
            $id=I('id');
            $qitiannei = time()-604800;
            $this->daikanshu=M('kydaikan')->where('gongsiid ='.session('gongsiid').' and kyid ='.$id.' and dkshijian >'.$qitiannei)->count();
            $this->genjinfs=M('Peizhi')->where(array('pzming'=>'genjinfs'))->select();
            $this->khlaiyuan=M('Peizhi')->where(array('pzming'=>'laiyuan','lxid'=>$keyuan['khlaiyuan']))->getField('lxming');
            $this->kehulx=M('Peizhi')->where(array('pzming'=>'yezhulx','lxid'=>$keyuan['kehulx']))->getField('lxming');
            if ($keyuan['mmzl']==1) {
                $this->fukuan=M('Peizhi')->where(array('pzming'=>'fukuan','lxid'=>$keyuan['fukuan']))->getField('lxming');
            } elseif ($keyuan['mmzl']==2) {
                $this->fukuan=M('Peizhi')->where(array('pzming'=>'zlfukuan','lxid'=>$keyuan['fukuan']))->getField('lxming');
            }
            $this->gtjieduan=M('Peizhi')->where(array('pzming'=>'gtjieduan','lxid'=>$keyuan['gtjieduan']))->getField('lxming');
            $this->kydengji=M('Peizhi')->where(array('pzming'=>'kydengji','lxid'=>$keyuan['kydengji']))->getField('lxming');
            $this->xflinian=M('Peizhi')->where(array('pzming'=>'xflinian','lxid'=>$keyuan['xflinian']))->getField('lxming');
            $this->guoji=M('Peizhi')->where(array('pzming'=>'guoji','lxid'=>$keyuan['guoji']))->getField('lxming');
            $this->minzu=M('Peizhi')->where(array('pzming'=>'minzu','lxid'=>$keyuan['minzu']))->getField('lxming');
            $this->jtgongju=M('Peizhi')->where(array('pzming'=>'jtgongju','lxid'=>$keyuan['jtgongju']))->getField('lxming');
            $Model = new \Think\Model;
            $this->luru=$Model->table(array('jjrxt_keyuan'=>'a','jjrxt_yonghu'=>'b','jjrxt_bumen'=>'c'))->field('c.bmming,b.ygmingcheng')->where('a.lururenid=b.id and b.bumen=c.id and a.id='.$id)->find();
            $this->weihu=$Model->table(array('jjrxt_keyuan'=>'a','jjrxt_yonghu'=>'b','jjrxt_bumen'=>'c'))->field('c.bmming,b.ygmingcheng')->where('a.weihurenid=b.id and b.bumen=c.id and a.id='.$id)->find();
            $this->fengpan=$Model->table(array('jjrxt_keyuan'=>'a','jjrxt_yonghu'=>'b','jjrxt_bumen'=>'c'))->field('c.bmming,b.ygmingcheng')->where('a.fengpanrenid=b.id and b.bumen=c.id and a.id='.$id)->find();
            $this->kygenjin=$Model->table(array('jjrxt_kygenjin'=>'a','jjrxt_yonghu'=>'b','jjrxt_bumen'=>'c'))->field('a.gjneirong,a.gjshijian,c.bmming,b.ygmingcheng,a.genjinfs,a.zhuangtai,a.id,a.gjvoice')->where('a.gjuid=b.id and b.bumen=c.id and kyid='.$id)->order('a.gjshijian desc')->select();
            $this->kydaikan=$Model->table(array('jjrxt_kydaikan'=>'a','jjrxt_yonghu'=>'b','jjrxt_bumen'=>'c'))->field('a.kehupj,a.dkshijian,c.bmming,b.ygmingcheng,a.fybh')->where('a.dkuid=b.id and b.bumen=c.id and kyid='.$id)->order('a.dkshijian desc')->select();
            $this->kytixing=$Model->table(array('jjrxt_kytixing'=>'a','jjrxt_yonghu'=>'b','jjrxt_bumen'=>'c'))->field('a.txneirong,a.txshijian,c.bmming,b.ygmingcheng')->where('a.txuid=b.id and b.bumen=c.id and kyid='.$id)->order('a.txshijian desc')->select();
            $this->volist = $Model->table(array('jjrxt_rizhi'=>'a','jjrxt_yonghu'=>'b'))->field('a.*,b.ygmingcheng')->where('a.uid=b.id and zy_type=2 and zy_id='.$id)->order('a.id desc')->select();
            $peitao1=M('keyuan')->where(array('id'=>$id))->getField('peitao');
            $today="ksj".date("m-d");
            if (!cookie($today)) {
                cookie($today,$id,3600*24);
            }else{
                $value = cookie($today);
                $val=$value.','.$id;
                cookie($today,$val,3600*24);
            }
            if ($peitao1) {
                $condition="pzming = 'peitao'";
                $condition.=" and lxid in ($peitao1)";
                $this->peitao=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
            }
            $chaoxiang1=M('keyuan')->where(array('id'=>$id))->getField('chaoxiang');
            if ($chaoxiang1) {
                $condition="pzming = 'chaoxiang'";
                $condition.=" and lxid in ($chaoxiang1)";
                $this->chaoxiang=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
            }
            $zhuangxiu1=M('keyuan')->where(array('id'=>$id))->getField('zhuangxiu');
            if ($zhuangxiu1) {
                $condition="pzming = 'zhuangxiu'";
                $condition.=" and lxid in ($zhuangxiu1)";
                $this->zhuangxiu=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
            }
            $this->display();
        }
        //客源跟进列表
        public function kygenjin(){
            $map = "gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(237,0);
            if ($x && $x != "all" && $x != "me") {
                $map .= " and bumen in ({$x})";
            }elseif ($x == "me") {
                $map .= " and id=".session('uid');
            }elseif ($x == false) {
                $map .= " and id=0";
            }
            $condition = "a.gongsiid=".session('gongsiid');
            $sousuo=I('sousuo');
            if ($sousuo) {//搜索
                $condition.=" and ((e.pinyinjs like '%$sousuo%') or (e.ygmingcheng like '%$sousuo%') or (e.dianhua like '%$sousuo%') or (a.kybh like '%$sousuo%'))";
            }
            $bumen = I("get.bumen",0,'intval');
            $result = M('bumen')->order(array("sort" => "ASC"))->select();
            if ($bumen) {
                $arr_string = _rbac_bumen_array($result,$bumen);
                $arr_string[] = $bumen;
                $string = implode(',',$arr_string);
                $condition .= " and e.bumen in ($string)";
            }
            $shijian1=I('shijian1');
            $shijian2=I('shijian2');
            if ($shijian1) {
                $this->shijian1=$shijian1;
                $shijian3=strtotime($shijian1);
            }
            if ($shijian2) {
                $this->shijian2=$shijian2;
                $shijian4=strtotime($shijian2)+86400;
            }
            if ($shijian3 and $shijian4){
                if ($shijian3 > $shijian4){
                    $condition.=" and a.gjshijian>".$shijian4." and a.gjshijian<".$shijian3;
                    $this->shijian1=$shijian2;
                    $this->shijian2=$shijian1;
                } else {
                    $condition.=" and a.gjshijian>".$shijian3." and a.gjshijian<".$shijian4;
                }
            } elseif ($shijian3) {
                $condition.=" and a.gjshijian>".$shijian3;
            } elseif ($shijian4) {
                $condition.=" and a.gjshijian<".$shijian4;
            }
            $count=M('kygenjin')->query("select count(*) from __KYGENJIN__ a join (select id as e_id,ygmingcheng,pinyinjs,bumen,dianhua from __YONGHU__ where {$map}) e on a.gjuid=e.e_id where {$condition}");
            $Page = new \Think\Page($count['0']['count(*)'],20);
            $show = $Page->show();
            $list="select b.*,c.khxingming,c.mmzl,d.khlxm from (select a.*,e.ygmingcheng,e.bumen from (select * from __KYGENJIN__ ) a join (select id as e_id,ygmingcheng,pinyinjs,bumen,dianhua from __YONGHU__ where {$map}) e on a.gjuid=e.e_id where {$condition} order by a.gjshijian DESC limit ".$Page->firstRow.','.$Page->listRows.") b left join (select id as c_id,khxingming,kehulx,mmzl from __KEYUAN__) c on b.kyid = c.c_id left join (select lxid as d_id,lxming as khlxm from __PEIZHI__ where pzming = 'yezhulx') d on c.kehulx = d.d_id ";
            $genjin=M('kygenjin')->query($list);
            $this->assign('firstRow',$Page->firstRow);
            $this->assign('genjin',$genjin);
            $this->assign('count',$count);
            $this->assign('page',$show);
            $select_categorys = bumen_option( $bumen, 237);
            $this->assign("select_categorys", $select_categorys);
            $this->display();
        }
        //客源带看列表
        public function kydaikan(){
            $map = "gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(238,0);
            if ($x && $x != "all" && $x != "me") {
                $map .= " and bumen in ({$x})";
            }elseif ($x == "me") {
                $map .= " and id=".session('uid');
            }elseif ($x == false) {
                $map .= " and id=0";
            }
            $condition = "a.gongsiid=".session('gongsiid');
            $sousuo=I('sousuo');
            if ($sousuo) {//搜索
                $condition.=" and ((e.pinyinjs like '%$sousuo%') or (e.ygmingcheng like '%$sousuo%') or (e.dianhua like '%$sousuo%') or (a.kybh like '%$sousuo%') or (a.fybh like '%$sousuo%'))";
            }
            $bumen = I("get.bumen",0,'intval');
            $result = M('bumen')->order(array("sort" => "ASC"))->select();
            if ($bumen) {
                $arr_string = _rbac_bumen_array($result,$bumen);
                $arr_string[] = $bumen;
                $string = implode(',',$arr_string);
                $condition .= " and e.bumen in ($string)";
            }
            $shijian1=I('shijian1');
            $shijian2=I('shijian2');
            if ($shijian1) {
                $this->shijian1=$shijian1;
                $shijian3=strtotime($shijian1);
            }
            if ($shijian2) {
                $this->shijian2=$shijian2;
                $shijian4=strtotime($shijian2)+86400;
            }
            if ($shijian3 and $shijian4){
                if ($shijian3 > $shijian4){
                    $condition.=" and a.dkshijian>".$shijian4." and a.dkshijian<".$shijian3;
                    $this->shijian1=$shijian2;
                    $this->shijian2=$shijian1;
                } else {
                    $condition.=" and a.dkshijian>".$shijian3." and a.dkshijian<".$shijian4;
                }
            } elseif ($shijian3) {
                $condition.=" and a.dkshijian>".$shijian3;
            } elseif ($shijian4) {
                $condition.=" and a.dkshijian<".$shijian4;
            }
            $count=M('kydaikan')->query("select count(*) from __KYDAIKAN__ a join (select id as e_id,ygmingcheng,pinyinjs,bumen,dianhua from __YONGHU__ where {$map}) e on a.dkuid=e.e_id where {$condition}");
            $Page=new \Think\Page($count['0']['count(*)'],20);
            $show  = $Page->show();
            $list="select b.*,c.khxingming,c.mmzl,d.khlxm from (select a.*,e.ygmingcheng,e.bumen from (select * from __KYDAIKAN__ ) a join (select id as e_id,ygmingcheng,pinyinjs,bumen,dianhua from __YONGHU__ where {$map}) e on a.dkuid=e.e_id where {$condition} order by a.dkshijian DESC limit ".$Page->firstRow.','.$Page->listRows.") b left join (select id as c_id,khxingming,kehulx,mmzl from __KEYUAN__) c on b.kyid = c.c_id left join (select lxid as d_id,lxming as khlxm from __PEIZHI__ where pzming = 'yezhulx') d on c.kehulx = d.d_id ";
            $daikan=M('kydaikan')->query($list);
            $this->assign('firstRow',$Page->firstRow);
            $this->assign('daikan',$daikan);
            $this->assign('count',$count);
            $this->assign('page',$show);
            $select_categorys = bumen_option( $bumen, 238);
            $this->assign("select_categorys", $select_categorys);
            $this->display();
        }
        //客源提醒列表
        public function kytixing(){
            $map = "gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(858,0);
            if ($x && $x != "all" && $x != "me") {
                $map .= " and bumen in ({$x})";
            }elseif ($x == "me") {
                $map .= " and id=".session('uid');
            }elseif ($x == false) {
                $map .= " and id=0";
            }
            $condition = "a.gongsiid=".session('gongsiid');
            $sousuo=I('sousuo');
            if ($sousuo) {//搜索
                $condition.=" and ((e.pinyinjs like '%$sousuo%') or (e.ygmingcheng like '%$sousuo%') or (e.dianhua like '%$sousuo%') or (a.kybh like '%$sousuo%'))";
            }
            $bumen = I("get.bumen",0,'intval');
            $result = M('bumen')->order(array("sort" => "ASC"))->select();
            if ($bumen) {
                $arr_string = _rbac_bumen_array($result,$bumen);
                $arr_string[] = $bumen;
                $string = implode(',',$arr_string);
                $condition .= " and e.bumen in ($string)";
            }
            $shijian1=I('shijian1');
            $shijian2=I('shijian2');
            if ($shijian1) {
                $this->shijian1=$shijian1;
                $shijian3=strtotime($shijian1);
            }
            if ($shijian2) {
                $this->shijian2=$shijian2;
                $shijian4=strtotime($shijian2)+86400;
            }
            if ($shijian3 and $shijian4){
                if ($shijian3 > $shijian4){
                    $condition.=" and a.time>".$shijian4." and a.time<".$shijian3;
                    $this->shijian1=$shijian2;
                    $this->shijian2=$shijian1;
                } else {
                    $condition.=" and a.time>".$shijian3." and a.time<".$shijian4;
                }
            } elseif ($shijian3) {
                $condition.=" and a.time>".$shijian3;
            } elseif ($shijian4) {
                $condition.=" and a.time<".$shijian4;
            }
            $count=M('kytixing')->query("select count(*) from __KYTIXING__ a join (select id as e_id,ygmingcheng,pinyinjs,bumen,dianhua from __YONGHU__ where {$map}) e on a.txuid=e.e_id where {$condition}");
            $Page=new \Think\Page($count['0']['count(*)'],20);
            $show  = $Page->show();
            $list="select b.*,c.khxingming,c.mmzl,d.khlxm,f.txtjr,f.txtjdh from (select a.*,e.ygmingcheng,e.bumen,e.dianhua from (select * from __KYTIXING__ ) a join (select id as e_id,ygmingcheng,pinyinjs,bumen,dianhua from __YONGHU__ where {$map}) e on a.txuid=e.e_id where {$condition} order by a.time DESC limit ".$Page->firstRow.','.$Page->listRows.") b left join (select id as c_id,khxingming,kehulx,mmzl from __KEYUAN__) c on b.kyid = c.c_id left join (select lxid as d_id,lxming as khlxm from __PEIZHI__ where pzming = 'yezhulx') d on c.kehulx = d.d_id left join (select id as f_id,ygmingcheng as txtjr,dianhua as txtjdh from __YONGHU__) f on b.adduid=f.f_id ";
            $tixing=M('kytixing')->query($list);
            $this->assign('firstRow',$Page->firstRow);
            $this->assign('tixing',$tixing);
            $this->assign('count',$count);
            $this->assign('page',$show);
            $select_categorys = bumen_option( $bumen, 858);
            $this->assign("select_categorys", $select_categorys);
            $this->display();
        }
    }
?>