<?php

    /*************************************************************************\
    |                                                                         |
    | 房佳（专业的新房分销ERP管理系统）                                       |
    |                                                                         |
    | Copyright (c) 2008-2019 http://fj.ytsofts.cn/ All rights reserved. |
    |                                                                         |
    | 本系统由淮南市银泰软件科技有限公司提供技术支持                          |
    |                                                                         |
    | QQ号：984784483                                                         |
    |                                                                         |
    \*************************************************************************/

    namespace Admin\Controller;
    use Think\Controller;
    class Keyuan1Controller extends CommonController {
        public function index(){
            if (I('is_ajax')) {
                $yxky=I('yxky');
                $bumen=I('bumen');
                $kydengji=I('kydengji');
                $sousuo=I('sousuo');
                $sousuo1=I('sousuo1');
                $sousuo2=I('sousuo2');
                $daikan=I('daikan');
                $genjin=I('genjin');
                $wdaikan=I('wdaikan');
                $wgenjin=I('wgenjin');
                $jingjiren=I('jingjiren');
                $shijian1=I('shijian1');
                $shijian2=I('shijian2');
                $condition="a.gongsiid=".session('gongsiid')." and a.isdel=2";
                if ($shijian1) {
                    $time_s1 = explode(' - ',$shijian1);
                    $time_s11 = strtotime($time_s1[0]);
                    $time_s12 = strtotime($time_s1[1])+86400;
                    $condition .= " and a.lurusj>=".$time_s11." and a.lurusj<".$time_s12;
                }
                if ($shijian2) {
                    $time_s2 = explode(' - ',$shijian2);
                    $time_s21 = strtotime($time_s2[0]);
                    $time_s22 = strtotime($time_s2[1])+86400;
                    if ($daikan) {
                        $condition .= " and a.daikan=1 and a.zhdkshijian>=".$time_s21." and a.zhdkshijian<".$time_s22;
                    }
                    if ($genjin) {
                        $condition .= " and a.genjin=1 and a.zhgjsj>=".$time_s21." and a.zhgjsj<".$time_s22;
                    }
                    if ($wdaikan) {
                        $condition .= " and (a.daikan=0 or a.zhdkshijian<".$time_s21.")";
                    }
                    if ($wgenjin) {
                        $condition .= " and (a.genjin=0 or a.zhgjsj<".$time_s21.")";
                    }
                } else {
                    if ($daikan) {
                        $condition.=" and a.daikan=1";
                    }
                    if ($genjin) {
                        $condition.=" and a.genjin=1";
                    }
                    if ($wdaikan) {
                        $condition.=" and a.daikan=0";
                    }
                    if ($wgenjin) {
                        $condition.=" and a.genjin=0";
                    }
                }
                if ($kydengji) {
                    $condition.=" and a.kydengji=".$kydengji;
                }
                if ($sousuo) {
                    $condition.=" and (a.xqbianhao like '%$sousuo%' or a.khxingming like '%$sousuo%' or a.xqxiaoqu like '%$sousuo%' or a.dianhua like '%$sousuo%' or a.dianhua2 like '%$sousuo%')";
                }
                if ($sousuo1) {
                    if ($yxky) {
                        $condition .= " and (d.ygmingcheng like '%$sousuo1%')";
                    } else {
                        $condition .= " and (a.weihuren like '%$sousuo1%')";
                    }
                }
                if ($jingjiren) {
                    $condition.=" and a.jingjiren=''";
                }
                if ($sousuo2) {
                    $condition .= " and (a.lururen like '%$sousuo2%')";
                }
                $map = "e.gongsiid=".session('gongsiid');
                $x = _rbac_lists_where(66,0);
                if ($x && $x != "all" && $x != "me") {
                    $map .= " and e.bumen in ({$x})";
                }elseif ($x == "me") {
                    $map .= " and e.id=".session('uid');
                }elseif ($x == false) {
                    $map .= " and e.id=0";
                }
                if ($bumen) {
                    $map .= " and e.bumen =".$bumen;
                }
                $field = I('field'); //排序字段名
                $order = I('order'); //排序方式
                $limit = I('limit',30,'int'); //每页条数
                $limit = $limit > 150 ? 150 : $limit;
                if ($field && $order) {
                    $kypx = "order by a.".$field." ".$order;
                } else {
                    $kypx = "order by a.id desc";
                }
                $Model = new \Think\Model();
                $qitianqian = time()-3600*24*7;
                if ($yxky) {
                    if((session('username') != C('RBAC_SUPERADMIN')) && ($x != "all")){
                        $yhqx = 1;
                    } else {
                        if ($sousuo1) {
                            $yhqx = 1;
                        }
                    }
                    if ($yhqx == 1) {
                        $count=M('keyuan')->query("select count(*) from (select b.kyid,count(*),e.ygmingcheng from __FZKY__ b join __YONGHU__ e on b.weihurenid=e.id where {$map} group by b.kyid) d join __KEYUAN__ a on d.kyid=a.id where {$condition}");
                        $Page = new \Think\Page($count['0']['count(*)'],$limit);
                        if (!$field || !$order) {
                            $listzd="select a.*,d.* from (select b.kyid as fzky,b.weihurenid as fzxs,e.ygmingcheng from __FZKY__ b join __YONGHU__ e on b.weihurenid=e.id where {$map} group by b.kyid) d join __KEYUAN__ a on d.fzky=a.id where a.zdshijian > {$qitianqian} and {$condition} order by a.zdshijian desc limit 10";
                        }
                        $listky="select a.*,d.* from (select b.kyid as fzky,b.weihurenid as fzxs,e.ygmingcheng from __FZKY__ b join __YONGHU__ e on b.weihurenid=e.id where {$map} group by b.kyid) d join __KEYUAN__ a on d.fzky=a.id where {$condition} ".$kypx." limit ".$Page->firstRow.','.$Page->listRows;
                    } else {
                        $yhqx = 2;
                    }
                } else {
                    $count=M('keyuan')->query("select count(*) from __KEYUAN__ a join __YONGHU__ e on a.weihurenid=e.id where {$condition} and {$map}");
                    $Page = new \Think\Page($count['0']['count(*)'],$limit);
                    if (!$field || !$order) {
                        $listzd="select a.* from __KEYUAN__ a join __YONGHU__ e on a.weihurenid=e.id where a.zdshijian > {$qitianqian} and {$condition} and {$map} order by a.zdshijian desc limit 10";
                    }
                    $listky="select a.* from __KEYUAN__ a join __YONGHU__ e on a.weihurenid=e.id where {$condition} and {$map} ".$kypx." limit ".$Page->firstRow.','.$Page->listRows;
                }
                if ($yhqx != 2) {
                    if (!$field || !$order) {
                        $datazd = $Model->query($listzd);
                        foreach ($datazd as $key => $value) {
                            if ($value['fzxs']) {
                                $kywhr=$value['fzxs'];
                            } else {
                                $kywhr=$value['weihurenid'];
                            }
                            if ($shijian2) {
                                $kydks=M('kydaikan')->where(array('kyid'=>$value['id'],'time'=>array('egt',$time_s21),'time'=>array('lt',$time_s22)))->count();
                                $kygjs=M('kygenjin')->where(array('kyid'=>$value['id'],'time'=>array('egt',$time_s21),'time'=>array('lt',$time_s22)))->count();
                            } else {
                                $kydks=M('kydaikan')->where(array('kyid'=>$value['id']))->count();
                                $kygjs=M('kygenjin')->where(array('kyid'=>$value['id']))->count();
                            }
                            $khdj="";
                            if ($value['kydengji'] > 0) {
                                $kydj=get_peizhi('kydengji',$value['kydengji']);
                                if ((($value['kydengji']+4) % 4) == 0) {
                                    $khdj="<span style='color: green;'>".$kydj."</span>";
                                } elseif ((($value['kydengji']+4) % 4) == 1) {
                                    $khdj="<span style='color: red;'>".$kydj."</span>";
                                } elseif ((($value['kydengji']+4) % 4) == 2) {
                                    $khdj="<span style='color: orange;'>".$kydj."</span>";
                                } elseif ((($value['kydengji']+4) % 4) == 3) {
                                    $khdj="<span style='color: blue;'>".$kydj."</span>";
                                }
                            }
                            $kylrs=M('kydclr')->where(array('kyid'=>$value['id']))->count();
                            $zxdknr=M('kydaikan')->where(array('kyid'=>$value['id']))->order('time desc')->limit(1)->getField('kehupj');
                            $zxgjnr=M('kygenjin')->where(array('kyid'=>$value['id']))->order('time desc')->limit(1)->getField('gjneirong');
                            $weihuren=M('yonghu')->where(array('id'=>$kywhr))->getField('ygmingcheng');
                            $lururen=M('yonghu')->where(array('id'=>$value['lururenid']))->getField('ygmingcheng');
                            $datazd[$key]['khxm'] = $value['khxingming'].get_peizhi('yezhulx',$value['kehulx']);
                            $datazd[$key]['khzt'] = $value['zhuangtai'] ? get_peizhi('khzhuangtai',$value['zhuangtai']) : "——";
                            $datazd[$key]['lurusj'] = $value['lurusj'] ? date('Y-m-d H:i:s',$value['lurusj']) : "——";
                            $datazd[$key]['zhgjsj'] = $value['zhgjsj'] ? date('Y-m-d H:i:s',$value['zhgjsj']) : "——";
                            $datazd[$key]['zhdkshijian'] = $value['zhdkshijian'] ? date('Y-m-d H:i:s',$value['zhdkshijian']) : "——";
                            $datazd[$key]['xqxiaoqu'] = $value['xqxiaoqu'] ? $value['xqxiaoqu'] : "——";
                            $datazd[$key]['jingjiren'] = $value['jingjiren'] ? $value['jingjiren'] : "——";
                            $datazd[$key]['dianhua2'] = $value['dianhua2'] ? $value['dianhua2'] : "——";
                            $datazd[$key]['zxdknr'] = $zxdknr ? $zxdknr : "——";
                            $datazd[$key]['zxgjnr'] = $zxgjnr ? $zxgjnr : "——";
                            $datazd[$key]['beizhu'] = $value['beizhu'] ? $value['beizhu'] : "——";
                            $datazd[$key]['weihuren'] = $weihuren ? $weihuren : "——";
                            $datazd[$key]['lururen'] = $lururen ? $lururen : "——";
                            $datazd[$key]['kywhr'] = $kywhr ? $kywhr : "——";
                            $datazd[$key]['khdj'] = $khdj ? $khdj : "——";
                            $datazd[$key]['kydks'] = $kydks;
                            $datazd[$key]['kygjs'] = $kygjs;
                            $datazd[$key]['kylrs'] = $kylrs;
                            $is_rbacxq = _rbac_btn_show(75,0,$kywhr);
                            $datazd[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
                            $datazd[$key]['kyiszd'] = 1;
                        }
                    }
                    $dataky = $Model->query($listky);
                    foreach ($dataky as $key => $value) {
                        if ($value['fzxs']) {
                            $kywhr=$value['fzxs'];
                        } else {
                            $kywhr=$value['weihurenid'];
                        }
                        if ($shijian2) {
                            $kydks=M('kydaikan')->where(array('kyid'=>$value['id'],'time'=>array('egt',$time_s21),'time'=>array('lt',$time_s22)))->count();
                            $kygjs=M('kygenjin')->where(array('kyid'=>$value['id'],'time'=>array('egt',$time_s21),'time'=>array('lt',$time_s22)))->count();
                        } else {
                            $kydks=M('kydaikan')->where(array('kyid'=>$value['id']))->count();
                            $kygjs=M('kygenjin')->where(array('kyid'=>$value['id']))->count();
                        }
                        $khdj="";
                        if ($value['kydengji'] > 0) {
                            $kydj=get_peizhi('kydengji',$value['kydengji']);
                            if ((($value['kydengji']+4) % 4) == 0) {
                                $khdj="<span style='color: green;'>".$kydj."</span>";
                            } elseif ((($value['kydengji']+4) % 4) == 1) {
                                $khdj="<span style='color: red;'>".$kydj."</span>";
                            } elseif ((($value['kydengji']+4) % 4) == 2) {
                                $khdj="<span style='color: orange;'>".$kydj."</span>";
                            } elseif ((($value['kydengji']+4) % 4) == 3) {
                                $khdj="<span style='color: blue;'>".$kydj."</span>";
                            }
                        }
                        $kylrs=M('kydclr')->where(array('kyid'=>$value['id']))->count();
                        $zxdknr=M('kydaikan')->where(array('kyid'=>$value['id']))->order('time desc')->limit(1)->getField('kehupj');
                        $zxgjnr=M('kygenjin')->where(array('kyid'=>$value['id']))->order('time desc')->limit(1)->getField('gjneirong');
                        $weihuren=M('yonghu')->where(array('id'=>$kywhr))->getField('ygmingcheng');
                        $lururen=M('yonghu')->where(array('id'=>$value['lururenid']))->getField('ygmingcheng');
                        $dataky[$key]['khxm'] = $value['khxingming'].get_peizhi('yezhulx',$value['kehulx']);
                        $dataky[$key]['khzt'] = $value['zhuangtai'] ? get_peizhi('khzhuangtai',$value['zhuangtai']) : "——";
                        $dataky[$key]['lurusj'] = $value['lurusj'] ? date('Y-m-d H:i:s',$value['lurusj']) : "——";
                        $dataky[$key]['zhgjsj'] = $value['zhgjsj'] ? date('Y-m-d H:i:s',$value['zhgjsj']) : "——";
                        $dataky[$key]['zhdkshijian'] = $value['zhdkshijian'] ? date('Y-m-d H:i:s',$value['zhdkshijian']) : "——";
                        $dataky[$key]['xqxiaoqu'] = $value['xqxiaoqu'] ? $value['xqxiaoqu'] : "——";
                        $dataky[$key]['jingjiren'] = $value['jingjiren'] ? $value['jingjiren'] : "——";
                        $dataky[$key]['dianhua2'] = $value['dianhua2'] ? $value['dianhua2'] : "——";
                        $dataky[$key]['zxdknr'] = $zxdknr ? $zxdknr : "——";
                        $dataky[$key]['zxgjnr'] = $zxgjnr ? $zxgjnr : "——";
                        $dataky[$key]['beizhu'] = $value['beizhu'] ? $value['beizhu'] : "——";
                        $dataky[$key]['weihuren'] = $weihuren ? $weihuren : "——";
                        $dataky[$key]['lururen'] = $lururen ? $lururen : "——";
                        $dataky[$key]['kywhr'] = $kywhr ? $kywhr : "——";
                        $dataky[$key]['khdj'] = $khdj ? $khdj : "——";
                        $dataky[$key]['kydks'] = $kydks;
                        $dataky[$key]['kygjs'] = $kygjs;
                        $dataky[$key]['kylrs'] = $kylrs;
                        $is_rbacxq = _rbac_btn_show(75,0,$kywhr);
                        $dataky[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
                    }
                    $data = array();
                    if (!$field || !$order) {
                        foreach ($datazd as $key => $value) {
                            $data[] = $value;
                        }
                    }
                    foreach ($dataky as $key => $value) {
                        $data[] = $value;
                    }
                } else {
                    $count['0']['count(*)'] = 0;
                    $data = "";
                }
                $return_data = array(
                    'code'=>0,
                    'count'=>$count['0']['count(*)'],
                    'data'=>$data
                );
                $this->ajaxReturn($return_data);
            } else {
                $this->kydengji = get_peizhi('kydengji',0);
                $select_categorys = bumen_option( 0, 66);
                $this->assign("select_categorys", $select_categorys);
                $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]{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的整数！');
                }
                $xq1 = array_keys(I('xiaoqu'));
                if (!$xq1) {
                    $this->error('请选择需求小区！');
                }
                $xqxiaoquid = implode(',',$xq1);
                $xqxiaoquids = array_unique(array_filter(explode(",",$xqxiaoquid)));
                $xqxiaoqu2 = array();
                foreach ($xqxiaoquids as $k => $val) {
                    $xiaoqum = M('xiaoqu')->where(array('id'=>$val))->getField('xiaoqum');
                    $xqxiaoqu2[$k] = $xiaoqum;
                }
                $xqxiaoqu = implode('；',$xqxiaoqu2);
                if ($dianhua && $dianhua2) {
                    $dhiscf = $dianhua.",".$dianhua2;
                } elseif ($dianhua) {
                    $dhiscf = $dianhua;
                } elseif ($dianhua2) {
                    $dhiscf = $dianhua2;
                }
                $iscfky1 = M('keyuan')->where(array('dianhua'=>array('in',$dhiscf),'isdel'=>2))->field('id,xqbianhao,weihurenid,weihuren,dianhua2,jingjiren,xqhuxing1,xqhuxing2,xqjiage1,xqjiage2,xqmianji1,xqmianji2,louceng1,louceng2,xqxiaoqu,xqxiaoquid,beizhu,lurusj')->order('lurusj asc')->find();
                $iscfky2 = M('keyuan')->where(array('dianhua2'=>array('in',$dhiscf),'isdel'=>2))->field('id,xqbianhao,weihurenid,weihuren,dianhua2,jingjiren,xqhuxing1,xqhuxing2,xqjiage1,xqjiage2,xqmianji1,xqmianji2,louceng1,louceng2,xqxiaoqu,xqxiaoquid,beizhu,lurusj')->order('lurusj asc')->find();
                if ($iscfky1 && $iscfky2) {
                    if ($iscfky1['lurusj'] >= $iscfky2['lurusj']) {
                        $iscfky = $iscfky2;
                    } else {
                        $iscfky = $iscfky1;
                    }
                } elseif ($iscfky1) {
                    $iscfky = $iscfky1;
                } elseif ($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.'的指定销售！');
                        }
                    }
                }
                $khxingming = I('khxingming');
                $kehulx = I('kehulx');
                $jingjiren = I('jingjiren');
                $beizhu = preg_replace ( "/\s(?=\s)/","\\1", I('beizhu') );
                if ($iscfky) {
                    $ygmingcheng = M('yonghu')->where(array('id'=>session('uid')))->getField('ygmingcheng');
                    $data_kydclr['kyid'] = $iscfky['id'];
                    $data_kydclr['kybh'] = $iscfky['xqbianhao'];
                    $data_kydclr['lururenid'] = session('uid');
                    $data_kydclr['lururen'] = $ygmingcheng;
                    $data_kydclr['lurusj'] = time();
                    $data_kydclr['khxingming'] = $khxingming;
                    $data_kydclr['kehulx'] = $kehulx;
                    $data_kydclr['dianhua'] = $dianhua;
                    if ($dianhua2) {
                        $data_kydclr['dianhua2'] = $dianhua2;
                    }
                    if ($jingjiren) {
                        $data_kydclr['jingjiren'] = $jingjiren;
                    }
                    if ($xqhuxing1) {
                        $data_kydclr['xqhuxing1'] = $xqhuxing1;
                    }
                    if ($xqhuxing2) {
                        $data_kydclr['xqhuxing2'] = $xqhuxing2;
                    }
                    if ($xqjiage1) {
                        $data_kydclr['xqjiage1'] = $xqjiage1;
                    }
                    if ($xqjiage2) {
                        $data_kydclr['xqjiage2'] = $xqjiage2;
                    }
                    if ($xqmianji1) {
                        $data_kydclr['xqmianji1'] = $xqmianji1;
                    }
                    if ($xqmianji2) {
                        $data_kydclr['xqmianji2'] = $xqmianji2;
                    }
                    if ($louceng1) {
                        $data_kydclr['louceng1'] = $louceng1;
                    }
                    if ($louceng2) {
                        $data_kydclr['louceng2'] = $louceng2;
                    }
                    $data_kydclr['xqxiaoqu'] = $xqxiaoqu;
                    $data_kydclr['xqxiaoquid'] = $xqxiaoquid;
                    if ($beizhu) {
                        $data_kydclr['beizhu'] = $beizhu;
                    }
                    if ($iscfky['weihurenid'] == session('uid')) {
                        $data_cskybd['zdshijian'] = time();
                        $cskybd = array();
                        if ($dianhua2 && !$iscfky['dianhua2']) {
                            $cskybd[]=1;
                            $data_cskybd['dianhua2'] = $dianhua2;
                        }
                        if ($jingjiren && !$iscfky['jingjiren']) {
                            $cskybd[]=2;
                            $data_cskybd['jingjiren'] = $jingjiren;
                        }
                        if (($xqhuxing1 || $xqhuxing2) && !$iscfky['xqhuxing1'] && !$iscfky['xqhuxing2']) {
                            $cskybd[]=3;
                            $data_cskybd['xqhuxing1'] = $xqhuxing1;
                            $data_cskybd['xqhuxing2'] = $xqhuxing2;
                        }
                        if (($xqjiage1 || $xqjiage2) && !$iscfky['xqjiage1'] && !$iscfky['xqjiage2']) {
                            $cskybd[]=4;
                            $data_cskybd['xqjiage1'] = $xqjiage1;
                            $data_cskybd['xqjiage2'] = $xqjiage2;
                        }
                        if (($xqmianji1 || $xqmianji2) && !$iscfky['xqmianji1'] && !$iscfky['xqmianji2']) {
                            $cskybd[]=5;
                            $data_cskybd['xqmianji1'] = $xqmianji1;
                            $data_cskybd['xqmianji2'] = $xqmianji2;
                        }
                        if (($louceng1 || $louceng2) && !$iscfky['louceng1'] && !$iscfky['louceng2']) {
                            $cskybd[]=6;
                            $data_cskybd['louceng1'] = $louceng1;
                            $data_cskybd['louceng2'] = $louceng2;
                        }
                        if ($xqxiaoqu != $iscfky['xqxiaoqu']) {
                            $cskybd[] = 7;
                            $data_cskybd['xqxiaoqu'] = $xqxiaoqu;
                            $data_cskybd['xqxiaoquid'] = $xqxiaoquid;
                        }
                        if ($beizhu && !$iscfky['beizhu']) {
                            $cskybd[]=8;
                            $data_cskybd['beizhu'] = $beizhu;
                        }
                        $kybdzd = implode(',',$cskybd);
                        M('keyuan')->where(array('id'=>$iscfky['id']))->save($data_cskybd);
                    } else {
                        M('keyuan')->where(array('id'=>$iscfky['id']))->setField('zdshijian',time());
                    }
                    if ($kybdzd) {
                        $data_kydclr['kybdzd'] = $kybdzd;
                    }
                    $isok = M('kydclr')->add($data_kydclr);
                } else {
                    $bh = M('bianhao')->where(array('id'=>2))->setInc('bianhao');
                    $bh1 = M('bianhao')->where(array('id'=>2))->getField('bianhao');
                    $data['gongsiid'] = session('gongsiid');
                    $data['xqbianhao'] = 'KH-'.$bh1;
                    $data['khxingming'] = $khxingming;
                    $data['kehulx'] = $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;
                    $data['beizhu'] = $beizhu;
                    $data['lururenid'] = session('uid');
                    $data['jingjiren'] = $jingjiren;
                    $data['lurusj'] = time();
                    $data['zhgjsj'] = time();
                    $data['xiugaisj'] = time();
                    $data['zhdkshijian'] = time();
                    $data['zhuangtai'] = 1;
                    $isok = M('Keyuan')->add($data);
                }
                if ($isok) {
                    if ($iscfky) {
                        $this->success('添加成功（此客户为老客户，由'.$iscfky['weihuren'].'接待）');
                    } else {
                        $ygmingcheng = M('yonghu')->where(array('id'=>session('uid')))->getField('ygmingcheng');
                        $data_kydclr['kyid'] = $isok;
                        $data_kydclr['kybh'] = 'KH-'.$bh1;
                        $data_kydclr['lururenid'] = session('uid');
                        $data_kydclr['lururen'] = $ygmingcheng;
                        $data_kydclr['lurusj'] = time();
                        $data_kydclr['khxingming'] = $khxingming;
                        $data_kydclr['kehulx'] = $kehulx;
                        $data_kydclr['dianhua'] = $dianhua;
                        if($dianhua2){
                            $data_kydclr['dianhua2'] = $dianhua2;
                        }
                        if($jingjiren){
                            $data_kydclr['jingjiren'] = $jingjiren;
                        }
                        if($xqhuxing1){
                            $data_kydclr['xqhuxing1'] = $xqhuxing1;
                        }
                        if($xqhuxing2){
                            $data_kydclr['xqhuxing2'] = $xqhuxing2;
                        }
                        if($xqjiage1){
                            $data_kydclr['xqjiage1'] = $xqjiage1;
                        }
                        if($xqjiage2){
                            $data_kydclr['xqjiage2'] = $xqjiage2;
                        }
                        if($xqmianji1){
                            $data_kydclr['xqmianji1'] = $xqmianji1;
                        }
                        if($xqmianji2){
                            $data_kydclr['xqmianji2'] = $xqmianji2;
                        }
                        if($louceng1){
                            $data_kydclr['louceng1'] = $louceng1;
                        }
                        if($louceng2){
                            $data_kydclr['louceng2'] = $louceng2;
                        }
                        $data_kydclr['xqxiaoqu'] = $xqxiaoqu;
                        $data_kydclr['xqxiaoquid'] = $xqxiaoquid;
                        if($beizhu){
                            $data_kydclr['beizhu'] = $beizhu;
                        }
                        M('kydclr')->add($data_kydclr);
                        M('fzky')->where(array('lzxqxq'=>1,'kyid'=>$isok))->delete();
                        foreach ($xqxiaoquids as $key => $value) {
                            $xsid=M('xqbdxs')->where(array('uid'=>session('uid'),'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);
                                }
                            }
                        }
                        $this->success('添加成功（此客户为新客户）');
                    }
                } else {
                    $this->error('添加失败');
                }
            } else {
                if (session(C('ADMIN_AUTH_KEY')) == 1) {
                    $this->xiaoqu = M('xiaoqu')->field('id,xiaoqum')->select();
                } else {
                    $user = M('yonghu')->where(array('id'=>session('uid')))->field('bumen,leixing')->find();
                    if ($user['leixing'] == 1) {
                        $qybm = M('bumen')->where(array('id'=>$user['bumen']))->field('id,level')->find();
                        if ($qybm['level'] == 2) {
                            $this->xiaoqu = M('xiaoqu')->where(array('bumen'=>$qybm['id']))->field('id,xiaoqum')->select();
                        }
                    }
                    if ($user['leixing'] == 2) {
                        $xqidsz = M('xqbdxs')->where(array('uid'=>session('uid')))->field('xqid')->select();
                        $xqids = array();
                        foreach ($xqidsz as $key => $value) {
                            $xqids[] = $value['xqid'];
                        }
                        $xqids[] = M('xiaoqu')->where(array('bdbumen'=>$user['bumen']))->getField('id');
                        $xqids = implode(',',$xqids);
                        if ($xqids) {
                            $this->xiaoqu = M('xiaoqu')->where(array('id'=>array('in',$xqids)))->field('id,xiaoqum')->select();
                        }
                    }
                    if ($user['leixing'] == 3) {
                        $xqidsz = M('xqbdxs')->where(array('uid'=>session('uid')))->field('xqid')->select();
                        $xqids = array();
                        foreach ($xqidsz as $key => $value) {
                            $xqids[] = $value['xqid'];
                        }
                        $xqids = implode(',',$xqids);
                        if ($xqids) {
                            $this->xiaoqu = M('xiaoqu')->where(array('id'=>array('in',$xqids)))->field('id,xiaoqum')->select();
                        }
                    }
                }
                $this->kehulx = M('Peizhi')->where(array('pzming'=>'yezhulx','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_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_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_rbacxq) {
                $this->is_rbacxq=1;
            }
            if ($is_rbacck) {
                $this->is_rbacck=1;
            }
            if ($is_rbacsc) {
                $this->is_rbacsc=1;
            }
            $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');
            $this->kydengji=M('Peizhi')->where(array('pzming'=>'kydengji','lxid'=>$keyuan['kydengji']))->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->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,a.time')->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,a.time')->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);
            }
            $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=$Model->query($xqlist);
            $this->assign('xqxiaoqu',$xqxiaoqu);
            $lrlist="select * from __KYDCLR__ where kyid=".I('id')." order by lurusj asc ";
            $kydclr=$Model->query($lrlist);
            foreach ($kydclr as $key => $value) {
                $kydclr[$key]['lurusj'] = $value['lurusj'] ? date('Y-m-d H:i:s',$value['lurusj']) : "——";
                $kydclr[$key]['khxm'] = $value['khxingming'].get_peizhi('yezhulx',$value['kehulx']);
                $kydclr[$key]['dianhua2'] = $value['dianhua2'] ? $value['dianhua2'] : "——";
                $kydclr[$key]['jingjiren'] = $value['jingjiren'] ? $value['jingjiren'] : "——";
                if ($value['xqhuxing1'] && $value['xqhuxing2']) {
                    $kydclr[$key]['huxing'] = $value['xqhuxing1'].'-'.$value['xqhuxing2'].'室';
                } elseif ($value['xqhuxing1']) {
                    $kydclr[$key]['huxing'] = $value['xqhuxing1'].'室以上';
                } elseif ($value['xqhuxing2']) {
                    $kydclr[$key]['huxing'] = $value['xqhuxing2'].'室以下';
                } else {
                    $kydclr[$key]['huxing'] = "——";
                }
                if ($value['xqjiage1'] && $value['xqjiage2']) {
                    $kydclr[$key]['jiage'] = $value['xqjiage1'].'-'.$value['xqjiage2'].'万';
                } elseif ($value['xqjiage1']) {
                    $kydclr[$key]['jiage'] = $value['xqjiage1'].'万以上';
                } elseif ($value['xqjiage2']) {
                    $kydclr[$key]['jiage'] = $value['xqjiage2'].'万以下';
                } else {
                    $kydclr[$key]['jiage'] = "——";
                }
                if ($value['xqmianji1'] && $value['xqmianji2']) {
                    $kydclr[$key]['mianji'] = $value['xqmianji1'].'~'.$value['xqmianji2'].'m²';
                } elseif ($value['xqmianji1']) {
                    $kydclr[$key]['mianji'] = $value['xqmianji1'].'m²以上';
                } elseif ($value['xqmianji2']) {
                    $kydclr[$key]['mianji'] = $value['xqmianji2'].'m²以下';
                } else {
                    $kydclr[$key]['mianji'] = "——";
                }
                if ($value['louceng1'] && $value['louceng2']) {
                    $kydclr[$key]['louceng'] = $value['louceng1'].'-'.$value['louceng2'].'层';
                } elseif ($value['louceng1']) {
                    $kydclr[$key]['louceng'] = $value['louceng1'].'层以上';
                } elseif ($value['louceng2']) {
                    $kydclr[$key]['louceng'] = $value['louceng2'].'层以下';
                } else {
                    $kydclr[$key]['louceng'] = "——";
                }
                $kydclr[$key]['xqxiaoqu'] = $value['xqxiaoqu'] ? $value['xqxiaoqu'] : "——";
                $kydclr[$key]['beizhu'] = $value['beizhu'] ? $value['beizhu'] : "——";
                $kybdzd = explode(",",$value['kybdzd']);
                if (in_array(1,$kybdzd)) {
                    $kydclr[$key]['zd1ys'] = "color:red;";
                }
                if (in_array(2,$kybdzd)) {
                    $kydclr[$key]['zd2ys'] = "color:red;";
                }
                if (in_array(3,$kybdzd)) {
                    $kydclr[$key]['zd3ys'] = "color:red;";
                }
                if (in_array(4,$kybdzd)) {
                    $kydclr[$key]['zd4ys'] = "color:red;";
                }
                if (in_array(5,$kybdzd)) {
                    $kydclr[$key]['zd5ys'] = "color:red;";
                }
                if (in_array(6,$kybdzd)) {
                    $kydclr[$key]['zd6ys'] = "color:red;";
                }
                if (in_array(7,$kybdzd)) {
                    $kydclr[$key]['zd7ys'] = "color:red;";
                }
                if (in_array(8,$kybdzd)) {
                    $kydclr[$key]['zd8ys'] = "color:red;";
                }
            }
            $this->assign('kydclr',$kydclr);
            $this->display();
        }
        public function kygenjin(){
            $condition = "e.gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(237,0);
            if ($x && $x != "all" && $x != "me") {
                $condition .= " and e.bumen in ({$x})";
            }elseif ($x == "me") {
                $condition .= " and e.id=".session('uid');
            }elseif ($x == false) {
                $condition .= " and e.id=0";
            }
            $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 a.gjshijian>".strtotime(I('shijian1'))." and a.gjshijian<".strtotime(I('shijian2'));
            } elseif (I('shijian1')) {
                $condition.=" and a.gjshijian>".strtotime(I('shijian1'));
            } elseif (I('shijian2')) {
                $condition.=" and a.gjshijian<".strtotime(I('shijian2'));
            }
            $count=M('kygenjin')->query("select count(*) from __KYGENJIN__ a join __YONGHU__ e on a.gjuid=e.id where {$condition}");
            $Page = new \Think\Page($count['0']['count(*)'],20);
            $show = $Page->show();
            $list="select a.*,e.ygmingcheng,e.bumen from __KYGENJIN__ a join __YONGHU__ e on a.gjuid=e.id where {$condition} order by a.gjshijian DESC limit ".$Page->firstRow.','.$Page->listRows;
            $genjin=M('kygenjin')->query($list);
            foreach ($genjin as $key => $value) {
                $genjin[$key]['khxingming']=M('keyuan')->where(array('id'=>$value['kyid']))->getField('khxingming');
            }
            $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(){
            $condition = "e.gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(238,0);
            if ($x && $x != "all" && $x != "me") {
                $condition .= " and e.bumen in ({$x})";
            }elseif ($x == "me") {
                $condition .= " and e.id=".session('uid');
            }elseif ($x == false) {
                $condition .= " and e.id=0";
            }
            $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 a.dkshijian>".strtotime(I('shijian1'))." and a.dkshijian<".strtotime(I('shijian2'));
            } elseif (I('shijian1')) {
                $condition.=" and a.dkshijian>".strtotime(I('shijian1'));
            } elseif (I('shijian2')) {
                $condition.=" and a.dkshijian<".strtotime(I('shijian2'));
            }
            $count=M('kydaikan')->query("select count(*) from __KYDAIKAN__ a join __YONGHU__ e on a.dkuid=e.id where {$condition}");
            $Page=new \Think\Page($count['0']['count(*)'],20);
            $show  = $Page->show();
            $list="select a.*,e.ygmingcheng,e.bumen from __KYDAIKAN__ a join __YONGHU__ e on a.dkuid=e.id where {$condition} order by a.dkshijian DESC limit ".$Page->firstRow.','.$Page->listRows;
            $daikan=M('kydaikan')->query($list);
            foreach ($daikan as $key => $value) {
                $daikan[$key]['khxingming']=M('keyuan')->where(array('id'=>$value['kyid']))->getField('khxingming');
            }
            $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(){
            $condition = "e.gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(918,0);
            if ($x && $x != "all" && $x != "me") {
                $condition .= " and e.bumen in ({$x})";
            }elseif ($x == "me") {
                $condition .= " and e.id=".session('uid');
            }elseif ($x == false) {
                $condition .= " and e.id=0";
            }
            $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 a.time>".strtotime(I('shijian1'))." and a.time<".strtotime(I('shijian2'));
            } elseif (I('shijian1')) {
                $condition.=" and a.time>".strtotime(I('shijian1'));
            } elseif (I('shijian2')) {
                $condition.=" and a.time<".strtotime(I('shijian2'));
            }
            $count=M('kytixing')->query("select count(*) from __KYTIXING__ a join __YONGHU__ e on a.txuid=e.id where {$condition}");
            $Page=new \Think\Page($count['0']['count(*)'],20);
            $show = $Page->show();
            $list="select a.*,e.ygmingcheng,e.dianhua,e.bumen from __KYTIXING__ a join __YONGHU__ e on a.txuid=e.id where {$condition} order by a.time DESC limit ".$Page->firstRow.','.$Page->listRows;
            $tixing=M('kytixing')->query($list);
            foreach ($tixing as $key => $value) {
                $tixing[$key]['khxingming']=M('keyuan')->where(array('id'=>$value['kyid']))->getField('khxingming');
                $tjrxx[$key]=M('yonghu')->where(array('id'=>$value['adduid']))->field('ygmingcheng,dianhua')->find();
                $tixing[$key]['txtjr']=$tjrxx[$key]['ygmingcheng'];
                $tixing[$key]['txtjdh']=$tjrxx[$key]['dianhua'];
            }
            $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();
        }
        public function kydaochu() {
            $is_rbac = _rbac_node_show(931);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $this->display();
        }
    }
?>