<?php

	/***************************************************************\
	|																|
	| 开单大师（专业的房产ERP管理系统）								|
	|																|
	| © 2018 http://www.kaidandashi.com All rights reserved.		|
	|																|
	| 本系统由淮南市银泰科技软件有限公司提供技术支持				|
	|																|
	| QQ号：1264399051												|
	|																|
	\***************************************************************/

	namespace Admin\Controller;
    use Think\Controller;
    class Keyuan1Controller extends CommonController {
    	public function index(){
            if (I('isget')) {
                $sousuo=I('sousuo');
                $sousuo1=I('sousuo1');
                $jiqie=I('jiqie');
                $daikan=I('daikan');
                $genjin=I('genjin');
                $jiage=I('jiage');
                $mianji=I('mianji');
                $shijian1=I('shijian1');
                $shijian2=I('shijian2');
                $condition="gongsiid=".session('gongsiid');
                $condition.=" and isdel=2";
                if ($sousuo) {
                    $condition.=" and (xqbianhao like '%$sousuo%' or khxingming like '%$sousuo%' or xqxiaoqu like '%$sousuo%' or dianhua like '%$sousuo%' or dianhua2 like '%$sousuo%')";
                }
                if ($sousuo1) {
                    $condition .= " and (weihuren like '%$sousuo1%')";
                }
                if ($jiqie) {
                    $condition.=" and jiqie=1";
                }
                if ($daikan) {
                    $condition.=" and daikan=1";
                }
                if ($genjin) {
                    $condition.=" and genjin=1";
                }
                if ($jiage) {
                    $condition.=" and $jiage>=xqjiage1 and $jiage<=xqjiage2";
                }
                if ($mianji) {
                    $condition.=" and $mianji>=xqmianji1 and $mianji<=xqmianji2";
                }
                if ($shijian1 and $shijian2){
                    if ($shijian1 > $shijian2){
                        $this->error('开始日大于结束日!');
                    }
                    $condition.=" and lurusj>".strtotime($shijian1)." and lurusj<".strtotime($shijian2);
                } elseif ($shijian1) {
                    $condition.=" and lurusj>".strtotime($shijian1);
                } elseif ($shijian2) {
                    $condition.=" and lurusj<".strtotime($shijian2);
                }
                $map = "gongsiid=".session('gongsiid');
                $map .= " and lock1=0";
                $map .= " and status=1";
                $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";
                }
                if((session('username') != C('RBAC_SUPERADMIN')) && ($x != "all")){
                    $yxky=I('yxky');
                }
                if ($yxky) {
                    $count=M('keyuan')->query("select count(*) from (select a.fzky,count(*) from (select kyid as fzky,weihurenid as fzxs from __FZKY__ ) a join (select id as e_id from __YONGHU__ where {$map}) e on a.fzxs=e.e_id group by a.fzky) d join (select id,weihurenid from __KEYUAN__ where {$condition}) b on d.fzky=b.id");
                    $Page=new \Think\Page($count['0']['count(*)'],20);
                    $show=$Page->show();
                    $list="select * from (select * from (select a.fzky,a.fzxs,count(*) from (select kyid as fzky,weihurenid as fzxs from __FZKY__ ) a join (select id as e_id from __YONGHU__ where {$map}) e on a.fzxs=e.e_id group by a.fzky) d join (select * from __KEYUAN__ where {$condition}) b on d.fzky=b.id order by b.id DESC limit ".$Page->firstRow.','.$Page->listRows.") as f left join (select id as c_id,ygmingcheng from __YONGHU__) c on f.weihurenid=c.c_id";
                } else {
                    $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->display('keyuan1list');
            } else {
                $x = _rbac_lists_where(66,0);
                if((session('username') != C('RBAC_SUPERADMIN')) && ($x != "all")){
                    $this->yxky=1;
                }
                $this->display();
            }
    	}
    	public function addKeyuan(){
            $is_rbac = _rbac_node_show(68);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                if (session('gongsiid')=="") {
                    $this->error('请重新登录');
                }
                $dianhua=I('dianhua');
                $dianhua2=I('dianhua2');
                $xqhuxing1=I('xqhuxing1');
                $xqhuxing2=I('xqhuxing2');
                $xqjiage1=I('xqjiage1');
                $xqjiage2=I('xqjiage2');
                $xqmianji1=I('xqmianji1');
                $xqmianji2=I('xqmianji2');
                $louceng1=I('louceng1');
                $louceng2=I('louceng2');
                $xqxiaoqu=I('xqxiaoqu');
                $xqxiaoquid=I('xqxiaoquid');
                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("/^$|^(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}$/",$dianhua2)){
                    $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(!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≤一万的整数！');
                }
                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的整数！');
                }
                $xqxiaoqus=array_unique(array_filter(explode("；",$xqxiaoqu)));
                $xqxiaoqums=implode(",",$xqxiaoqus);
                $xqxiaoquids=array_unique(array_filter(explode(",",$xqxiaoquid)));
                $xqxiaoqu=implode("；",$xqxiaoqus);
                $xqxiaoquid=implode(",",$xqxiaoquids);
                foreach ($xqxiaoqus as $key => $value) {
                    $xqxqid=M('xiaoqu')->where(array('xiaoqum'=>$value))->getField('id');
                    if (!in_array($xqxqid, $xqxiaoquids)) {
                        $this->error('需求小区异常错误！');
                    }
                }
                foreach ($xqxiaoquids as $key => $value) {
                    $xqxqm=M('xiaoqu')->where(array('id'=>$value))->getField('xiaoqum');
                    if (!in_array($xqxqm, $xqxiaoqus)) {
                        $this->error('需求小区异常错误！');
                    }
                }
                $iscfky1 = M('keyuan')->where(array('dianhua'=>I('dianhua'),'zhuangtai'=>2))->field('weihurenid,weihuren')->order('id desc')->find();
                $iscfky2 = M('keyuan')->where(array('dianhua'=>I('dianhua')))->field('weihurenid,weihuren')->order('id asc')->find();
                if ($iscfky1) {
                    $iscfky=$iscfky1;
                } else if ($iscfky2) {
                    $iscfky=$iscfky2;
                }
                if ($iscfky) {
                    $data['weihurenid']=$iscfky['weihurenid'];
                    $data['weihuren']=$iscfky['weihuren'];
                } else {
                    $data['weihurenid']=session('uid');
                    $data['weihuren']=session('ygmingcheng');
                }
                $user=M('yonghu')->where(array('id'=>$data['weihurenid']))->field('bumen,leixing')->find();
                if ($user['leixing'] != 2 and $user['leixing'] != 3) {
                    $this->error('客源经办人不是销售或者销售扩展！找不到指定销售！');
                }
                $xqid1=M('xiaoqu')->where(array('bdbumen'=>$user['bumen']))->getField('id');
                foreach ($xqxiaoquids as $key => $value) {
                    $xiaoqum=M('xiaoqu')->where(array('id'=>$value))->getField('xiaoqum');
                    $xsid=M('xqbdxs')->where(array('uid'=>$data['weihurenid'],'xqid'=>$value))->getField('xsid');
                    if ((($user['leixing'] == 2) && ($value == $xqid1)) || $xsid) {
                    } else {
                        if ($iscfky) {
                            $this->error('未找到'.$iscfky['weihuren'].'在'.$xiaoqum.'的指定销售！');
                        } else {
                            $this->error('未找到'.$xiaoqum.'的指定销售！');
                        }
                    }
                }
                $bh=M('bianhao')->where(array('id'=>2))->setInc('bianhao');
                $bh1=M('bianhao')->where(array('id'=>2))->getField('bianhao');
                if(I('jiqie')){
                    $jiqie=1;
                }else{
                    $jiqie=0;
                }
                $data['jiqie']=$jiqie;
                $data['gongsiid']=session('gongsiid');
                $data['xqbianhao']='KH-'.$bh1;
                $data['khxingming']=I('khxingming');
                $data['kehulx']=I('kehulx');
                $data['dianhua']=$dianhua;
                if($dianhua2){
                    $data['dianhua2']=$dianhua2;
                }
                if($xqhuxing1){
                    $data['xqhuxing1']=$xqhuxing1;
                }
                if($xqhuxing2){
                    $data['xqhuxing2']=$xqhuxing2;
                }
                if($xqjiage1){
                    $data['xqjiage1']=$xqjiage1;
                }
                if($xqjiage2){
                    $data['xqjiage2']=$xqjiage2;
                }
                if($xqmianji1){
                    $data['xqmianji1']=$xqmianji1;
                }
                if($xqmianji2){
                    $data['xqmianji2']=$xqmianji2;
                }
                if($louceng1){
                    $data['louceng1']=$louceng1;
                }
                if($louceng2){
                    $data['louceng2']=$louceng2;
                }
                $data['xqxiaoqu']=$xqxiaoqu;
                $data['xqxiaoquid']=$xqxiaoquid;
                $zx1=array_keys(I('zhuangxiu'));
                $zx2=implode(',',$zx1);
                $data['zhuangxiu']=$zx2;
                $beizhu=preg_replace ( "/\s(?=\s)/","\\1", I('beizhu') );
                $data['beizhu']=$beizhu;
                $data['lururenid']=session('uid');
                $data['jingjiren']=I('jingjiren');
                $data['lurusj']=time();
                $data['zhgjsj']=time();
                $data['xiugaisj']=time();
                $data['zhuangtai']=1;
                $isok=M('Keyuan')->add($data);
                if ($isok) {
                    foreach ($xqxiaoquids as $key => $value) {
                        $xqid1=M('xiaoqu')->where(array('bdbumen'=>$user['bumen']))->getField('id');
                        $xsid=M('xqbdxs')->where(array('uid'=>$data['weihurenid'],'xqid'=>$value))->getField('xsid');
                        $fzkyid=M('fzky')->where(array('weihurenid'=>$xsid,'kyid'=>$isok))->getField('id');
                        if (!$fzkyid) {
                            if (($user['leixing'] == 3 && $xsid) || (($user['leixing'] == 2) && ($value != $xqid1) && $xsid)) {
                                $data_keyuan['lzxqxq']=1;
                                $data_keyuan['kyid']=$isok;
                                $data_keyuan['xqid']=$value;
                                $data_keyuan['weihurenid']=$xsid;
                                $data_keyuan['addtime']=time();
                                $data_keyuan['adduid']=session('uid');
                                $data_keyuan['gongsiid']=session('gongsiid');
                                M('fzky')->add($data_keyuan);
                            }
                        }
                    }
                    if ($iscfky) {
                        $this->success('添加成功（此客户为老客户，由'.$iscfky['weihuren'].'接待）');
                    } else {
                        $this->success('添加成功（此客户为新客户）');
                    }
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->kehulx=M('Peizhi')->where(array('pzming'=>'yezhulx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->zhuangxiu=M('Peizhi')->where(array('pzming'=>'zhuangxiu','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->display();
            }
    	}
        public function neirong1(){
            $keyuan=M('Keyuan')->where(array('id'=>I('id'),'gongsiid'=>session('gongsiid')))->find();
            $kywhr=I('kywhr');
            $this->kywhr=$kywhr;
            if ($kywhr) {
                $is_rbac = _rbac_btn_show(75,0,$kywhr);
                $is_rbacgx = _rbac_btn_show(79,0,$kywhr);
                $is_rbacwh = _rbac_btn_show(89,0,$kywhr);
                $is_rbachf = _rbac_btn_show(85,0,$kywhr);
                $is_rbaclf = _rbac_btn_show(87,0,$kywhr);
                $is_rbactx = _rbac_btn_show(302,0,$kywhr);
                $is_rbacyx = _rbac_btn_show(863,0,$kywhr);
                $is_rbacxq = _rbac_btn_show(438,0,$kywhr);
                $is_rbacck = _rbac_btn_show(440,0,$kywhr);
                $is_rbacsc = _rbac_btn_show(501,0,$kywhr);
            } else {
                $is_rbac = _rbac_btn_show(75,0,$keyuan['weihurenid']);
                $is_rbacgx = _rbac_btn_show(79,0,$keyuan['weihurenid']);
                $is_rbacwh = _rbac_btn_show(89,0,$keyuan['weihurenid']);
                $is_rbachf = _rbac_btn_show(85,0,$keyuan['weihurenid']);
                $is_rbaclf = _rbac_btn_show(87,0,$keyuan['weihurenid']);
                $is_rbactx = _rbac_btn_show(302,0,$keyuan['weihurenid']);
                $is_rbacyx = _rbac_btn_show(863,0,$keyuan['weihurenid']);
                $is_rbacxq = _rbac_btn_show(438,0,$keyuan['weihurenid']);
                $is_rbacck = _rbac_btn_show(440,0,$keyuan['weihurenid']);
                $is_rbacsc = _rbac_btn_show(501,0,$keyuan['weihurenid']);
            }
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if ($is_rbacgx) {
                $this->is_rbacgx=1;
            }
            if ($is_rbacwh) {
                $this->is_rbacwh=1;
            }
            if ($is_rbachf) {
                $this->is_rbachf=1;
            }
            if ($is_rbaclf) {
                $this->is_rbaclf=1;
            }
            if ($is_rbactx) {
                $this->is_rbactx=1;
            }
            if ($is_rbacyx) {
                $this->is_rbacyx=1;
            }
            if ($is_rbacxq) {
                $this->is_rbacxq=1;
            }
            if ($is_rbacck) {
                $this->is_rbacck=1;
            }
            if ($is_rbacsc) {
                $this->is_rbacsc=1;
            }
            if ($keyuan['zhuangtai']==3) {
                $kycjxq=M('chengjiao')->where(array('kybianhao'=>$keyuan['xqbianhao']))->field('cjuid,cjbianhao,fybianhao,id')->find();
                $is_rbaccj = _rbac_btn_show(202,0,$kycjxq['cjuid']);
                if ($is_rbaccj) {
                    $this->cjxqck=1;
                }
                $this->kycjxq=$kycjxq;
            }
            $this->assign('keyuan',$keyuan);
            $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->kehulx=M('Peizhi')->where(array('pzming'=>'yezhulx','lxid'=>$keyuan['kehulx']))->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,b.dianhua')->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,b.dianhua')->where('a.weihurenid=b.id and b.bumen=c.id and a.id='.$id)->find();
            $this->kyyixiang=$Model->table(array('jjrxt_kyyxfw'=>'a','jjrxt_fjxinxi'=>'b','jjrxt_lcxinxi'=>'c','jjrxt_dyxinxi'=>'d','jjrxt_zdxinxi'=>'e','jjrxt_xiaoqu'=>'f','jjrxt_yonghu'=>'g'))->field('b.fanghao,b.bianhao,b.xqid,b.zdid,b.dyid,b.lcid,c.lcming,d.dyming,e.zdming,f.xiaoqum,g.ygmingcheng')->where('a.fwid=b.id and b.lcid=c.id and b.dyid=d.id and b.zdid=e.id and b.xqid=f.id and a.adduid=g.id and a.kyid='.$id)->order('a.addtime desc')->select();
            $this->kygenjin=$Model->table(array('jjrxt_kygenjin'=>'a','jjrxt_yonghu'=>'b','jjrxt_bumen'=>'c'))->field('a.gjneirong,a.gjuid,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,a.adduid,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();
            $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);
            }
            $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}");
            }
            $xqxiaoquids=array_unique(array_filter(explode(",",$keyuan['xqxiaoquid'])));
            foreach ($xqxiaoquids as $key => $value) {
                $xqid=M('fzky')->where(array('lzxqxq'=>1,'kyid'=>$keyuan['id'],'xqid'=>$value))->getField('xqid');
                if (!$xqid) {
                    $bxqid=$value;
                }
            }
            if ($bxqid) {
                $this->bxq=1;
                $bxq=M('xiaoqu')->where(array('id'=>$bxqid))->field('xiaoqum,adduid')->find();
                $this->bxqm=$bxq['xiaoqum'];
                $this->bxqjlm=M('yonghu')->where(array('id'=>$bxq['adduid']))->getField('ygmingcheng');
            }
            $xqlist="select * from (select a.fzky,a.fzxs,a.xqid1,e.xqzdxs,f.xqtjr from (select kyid as fzky,weihurenid as fzxs,adduid,xqid as xqid1 from __FZKY__ where kyid = ".$keyuan['id']." and lzxqxq = 1 ) a join (select id as e_id,ygmingcheng as xqzdxs from __YONGHU__ ) e on a.fzxs=e.e_id left join (select id as f_id,ygmingcheng as xqtjr from __YONGHU__ ) f on a.adduid=f.f_id ) d left join (select id as xqid2,xiaoqum,adduid as xqfzr from __XIAOQU__ ) b on d.xqid1=b.xqid2 left join (select id as c_id,ygmingcheng as xmjlm from __YONGHU__) c on b.xqfzr=c.c_id ";
            $xqxiaoqu=M('fzky')->query($xqlist);
            $this->assign('xqxiaoqu',$xqxiaoqu);
            $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 = "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)";
            }
            if (I('shijian1') and I('shijian2')){
                if (I('shijian1') > I('shijian2')){
                    $this->error('开始日大于结束日!');
                }
                $condition.=" and gjshijian>".strtotime(I('shijian1'))." and gjshijian<".strtotime(I('shijian2'));
            } elseif (I('shijian1')) {
                $condition.=" and gjshijian>".strtotime(I('shijian1'));
            } elseif (I('shijian2')) {
                $condition.=" and gjshijian<".strtotime(I('shijian2'));
            }
            $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 a.*,b.khxingming,e.ygmingcheng,e.bumen from __KYGENJIN__ a join (select id as e_id,ygmingcheng,pinyinjs,bumen,dianhua from __YONGHU__ where {$map}) e on a.gjuid=e.e_id left join (select id as b_id,khxingming from __KEYUAN__) b on a.kyid=b.b_id where {$condition} order by gjshijian DESC limit ".$Page->firstRow.','.$Page->listRows;
            $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 = "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)";
            }
            if (I('shijian1') and I('shijian2')){
                if (I('shijian1') > I('shijian2')){
                    $this->error('开始日大于结束日!');
                }
                $condition.=" and dkshijian>".strtotime(I('shijian1'))." and dkshijian<".strtotime(I('shijian2'));
            } elseif (I('shijian1')) {
                $condition.=" and dkshijian>".strtotime(I('shijian1'));
            } elseif (I('shijian2')) {
                $condition.=" and dkshijian<".strtotime(I('shijian2'));
            }
            $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 a.*,b.khxingming,e.ygmingcheng,e.bumen from __KYDAIKAN__ a join (select id as e_id,ygmingcheng,pinyinjs,bumen,dianhua from __YONGHU__ where {$map}) e on a.dkuid=e.e_id left join (select id as b_id,khxingming from __KEYUAN__) b on a.kyid=b.b_id where {$condition} order by dkshijian DESC limit ".$Page->firstRow.','.$Page->listRows;
            $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(918,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 = "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)";
            }
            if (I('shijian1') and I('shijian2')){
                if (I('shijian1') > I('shijian2')){
                    $this->error('开始日大于结束日!');
                }
                $condition.=" and time>".strtotime(I('shijian1'))." and time<".strtotime(I('shijian2'));
            } elseif (I('shijian1')) {
                $condition.=" and time>".strtotime(I('shijian1'));
            } elseif (I('shijian2')) {
                $condition.=" and time<".strtotime(I('shijian2'));
            }
            $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 a.*,b.khxingming,e.ygmingcheng,e.dianhua,e.bumen,c.txtjr,c.txtjdh from __KYTIXING__ a join (select id as e_id,ygmingcheng,pinyinjs,bumen,dianhua from __YONGHU__ where {$map}) e on a.txuid=e.e_id left join (select id as b_id,khxingming from __KEYUAN__) b on a.kyid=b.b_id left join (select id as c_id,ygmingcheng as txtjr,dianhua as txtjdh from __YONGHU__) c on a.adduid=c.c_id where {$condition} order by time DESC limit ".$Page->firstRow.','.$Page->listRows;
            $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, 238);
            $this->assign("select_categorys", $select_categorys);
            $this->display();
        }
    }
?>