<?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('is_ajax')) {
                $mmzl = I('mmzl',0,'int');
                $zhuangtai = I('zhuangtai',0,'int');
                $jiage = I('jiage',0,'int');
                $mianji = I('mianji',0,'int');
                $jrll = I('jrll',0,'int');
                $jiqie = I('jiqie',0,'int');
                $daikan = I('daikan',0,'int');
                $mykeyuan = I('mykeyuan',0,'int');
                $mysike = I('mysike',0,'int');
                $gongke = I('gongke',0,'int');
                $fengpan = I('fengpan',0,'int');
                $dshenhe = I('dshenhe',0,'int');
                $isdel = I('isdel',0,'int');
                $sousuo = sql_check_param(I('sousuo'));
                /*权限控制开始*/
                $condition = "e.gongsiid=".session('gongsiid');
                $x = _rbac_lists_where(66,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";
                }
                /*权限控制结束*/
                $options_arr = get_options('chengjiao_options',0);
                $is_rbacfck = _rbac_node_show(865);
                if (!$is_rbacfck) {
                    $condition .= " and a.fpck = 2";
                }
                if ($mmzl) {//类型
                    $condition .= " and a.mmzl = '$mmzl'";
                }
                if ($zhuangtai) {//状态
                    $condition .= " and a.zhuangtai = '$zhuangtai'";
                }
                if ($fengpan) {//无效
                    $condition .= " and a.zhuangtai in (".$options_arr['kyfengpan_zhuangtai'].")";
                } else {
                    $condition .= " and a.zhuangtai not in (".$options_arr['kyfengpan_zhuangtai'].")";
                }
                if ($dshenhe) {//待审核
                    $condition .= " and a.zhuangtai = 0";
                } else {
                    $condition .= " and a.zhuangtai > 0";
                }
                if ($isdel) {//客源回收站
                    $condition .= " and a.isdel = 1";
                } else {
                    $condition .= " and a.isdel = 2";
                }
                if ($mykeyuan) {//我的客源
                    $user_id = session('uid');
                    $condition .= " and a.weihurenid = '$user_id'";
                }
                if ($mysike) {//我的私客
                    $user_id = session('uid');
                    $condition .= " and a.zhuangtai = '".$options_arr['kysike_zhuangtai']."' and a.weihurenid = '$user_id'";
                }
                if ($gongke) {//公客
                    $condition .= " and a.zhuangtai = '".$options_arr['kygongke_zhuangtai']."'";
                }
                if ($jrll) {//今日浏览
                    $today = "ksj".date("m-d");
                    if (!cookie($today)) {
                        $history = 0;
                    } else {
                        $history = cookie($today);
                    }
                    $condition .= " and a.id in ($history)";
                }
                if ($jiqie) {//急切
                    $condition .= " and a.jiqie = 1";
                }
                if ($daikan) {//带看
                    $condition .= " and a.daikan = 1";
                }
                if ($jiage) {//价格
                    $condition .= " and a.xqjiage1 <= '$jiage' and a.xqjiage2 >= '$jiage'";
                }
                if ($mianji) {//面积
                    $condition .= " and a.xqmianji1 <= '$mianji' and a.xqmianji2 >= '$mianji'";
                }
                if ($sousuo) {//搜索
                    $condition .= " and (a.xqbianhao like '%$sousuo%' or a.khxingming like '%$sousuo%' or a.xqquyu like '%$sousuo%' or a.xqxiaoqu like '%$sousuo%' or a.xqysfy like '%$sousuo%' or e.ygmingcheng like '%$sousuo%' or e.pinyinjs like '%$sousuo%' or e.dianhua like '%$sousuo%')";
                }
                $field = I('field'); //排序字段名
                $order = I('order'); //排序方式
                $limit = I('limit',30,'int'); //每页条数
                $limit = $limit > 150 ? 150 : $limit;
                if ($field && $order) {
                    $kypx = "a.".$field." ".$order;
                } else {
                    $kypx = "a.id DESC";
                }
                $count = M('keyuan')->alias('a')->join('jjrxt_yonghu e on a.weihurenid=e.id')->where($condition)->count();
                $Page = I('p');
                $data = M('keyuan')->alias('a')->join('jjrxt_yonghu e on a.weihurenid=e.id')->field('a.*,e.ygmingcheng,e.dianhua as whrdh')->where($condition)->order($kypx)->page($Page,$limit)->select();
                foreach ($data as $key => $value) {
                    if ($value['mmzl'] == 3) {
                        $xqzy=$value['xqysfy'];
                    } else {
                        $xqzy=$value['xqxiaoqu'];
                    }
                    if ($value['mmzl'] == 2) {
                        $jgdw="元";
                    } else {
                        $jgdw="万";
                    }
                    if ($value['xqmianji1'] && $value['xqmianji2']) {
                        $xqmj=$value['xqmianji1']."m²~".$value['xqmianji2']."m²";
                    } else if ($value['xqmianji1']) {
                        $xqmj=$value['xqmianji1']."m²以上";
                    } else if ($value['xqmianji2']) {
                        $xqmj=$value['xqmianji2']."m²以下";
                    }
                    if ($value['xqjiage1'] && $value['xqjiage2']) {
                        $xqjg=$value['xqjiage1'].$jgdw."~".$value['xqjiage2'].$jgdw;
                    } else if ($value['xqjiage1']) {
                        $xqjg=$value['xqjiage1'].$jgdw."以上";
                    } else if ($value['xqjiage2']) {
                        $xqjg=$value['xqjiage2'].$jgdw."以下";
                    }
                    if ($value['xqhuxing1'] && $value['xqhuxing2']) {
                        $xqhx=$value['xqhuxing1']."室~".$value['xqhuxing2']."室";
                    } else if ($value['xqhuxing1']) {
                        $xqhx=$value['xqhuxing1']."室以上";
                    } else if ($value['xqhuxing2']) {
                        $xqhx=$value['xqhuxing2']."室以下";
                    }
                    $kybh = explode("K", $value['xqbianhao']);
                    $data[$key]['kybh'] = $value['xqbianhao'];
                    $data[$key]['xqbianhao'] = $kybh[1];
                    $data[$key]['xqmj'] = $xqmj ? $xqmj : "——";
                    $data[$key]['xqjg'] = $xqjg ? $xqjg : "——";
                    $data[$key]['xqhx'] = $xqhx ? $xqhx : "——";
                    $data[$key]['xqzy'] = $xqzy;
                    $data[$key]['xqyt'] = get_peizhi('yongtu',$value['xqyongtu'],3);
                    $data[$key]['khly'] = get_peizhi('laiyuan',$value['khlaiyuan'],3);
                    $data[$key]['kyzt'] = get_peizhi('khzhuangtai',$value['zhuangtai'],3);
                    $data[$key]['khxm'] = $value['khxingming'].get_peizhi('yezhulx',$value['kehulx'],3);
                    $data[$key]['gtjd'] = $value['gtjieduan'] ? get_peizhi('gtjieduan',$value['gtjieduan'],3) : "——";
                    $data[$key]['lurusj'] = $value['lurusj'] ? date('Y-m-d H:i:s',$value['lurusj']) : "——";
                    $data[$key]['whr'] = $value['ygmingcheng'];
                    $data[$key]['whrdh'] = $value['whrdh'];
                    $is_rbacxq = _rbac_btn_show(75,$value['zhuangtai'],$value['weihurenid']);
                    if ($value['isdel'] == 1) {
                        $is_rbachy = _rbac_btn_show(771,$value['zhuangtai'],$value['weihurenid']);
                        $is_rbacsc = _rbac_btn_show(772,$value['zhuangtai'],$value['weihurenid']);
                        $data[$key]['rbachy'] = $is_rbachy ? 1 : 0;
                        $data[$key]['rbacsc'] = $is_rbacsc ? 1 : 0;
                    }
                    $data[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
                }
                $return_data = array(
                    'code'=>0,
                    'count'=>$count,
                    'data'=>$data
                );
                $this->ajaxReturn($return_data);
            } else {
                $this->zhuangtai = get_peizhi('khzhuangtai',0,1);
                $this->display();
            }
        }
    	public function addKeyuan(){
            $is_rbac = _rbac_node_show(68);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $mmzl = I('mmzl');
            if (IS_POST) {
                if (session('gongsiid') == "") {
                    $this->error('请重新登录');
                }
                switch ($mmzl) {
                    case 1:
                        $bhao = 'MK';
                        break;
                    case 2:
                        $bhao = 'ZK';
                        break;
                    default:
                        $this->error('异常错误');
                        break;
                }
                $options_arr = get_options('chengjiao_options',0);
                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[0-1,4-9]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-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 == 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≤十万的整数！');
                    }
                } else {
                    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的整数！');
                }
                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 (I('zhuangtai')) {
                    $zhuangtai = $options_arr['kysike_zhuangtai'];
                } else {
                    $zhuangtai = $options_arr['kygongke_zhuangtai'];
                }
                if (I('jiqie')) {
                    $jiqie = 1;
                } else {
                    $jiqie = 0;
                }
                $data['zhuangtai'] = $zhuangtai;
                $data['gongsiid'] = session('gongsiid');
                $data['xqbianhao'] = $bhao.$bh1;
                $data['khxingming'] = I('khxingming');
                $data['kehulx'] = I('kehulx');
                $data['dianhua'] = $dianhua;
                $cx1 = array_keys(I('chaoxiang'));
                $cx2 = implode(',',$cx1);
                $data['chaoxiang'] = $cx2;
                $zx1 = array_keys(I('zhuangxiu'));
                $zx2 = implode(',',$zx1);
                $data['zhuangxiu'] = $zx2;
                $pt1 = array_keys(I('peitao'));
                $pt2 = implode(',',$pt1);
                $data['peitao'] = $pt2;
                $data['jiqie'] = $jiqie;
                $data['fukuan'] = I('fukuan');
                $data['xqyongtu'] = I('xqyongtu');
                $data['xqquyu'] = I('xqquyu');
                $xqxiaoqus = array_unique(array_filter(explode("；",I('xqxiaoqu'))));
                $xqxiaoqu = implode("；",$xqxiaoqus);
                $data['xqxiaoqu'] = $xqxiaoqu;
                $data['xqhuxing1'] = $xqhuxing1;
                $data['xqhuxing2'] = $xqhuxing2;
                $data['xqjiage1'] = $xqjiage1;
                $data['xqjiage2'] = $xqjiage2;
                $data['xqmianji1'] = $xqmianji1;
                $data['xqmianji2'] = $xqmianji2;
                $data['louceng1'] = $louceng1;
                $data['louceng2'] = $louceng2;
                $data['fangling1'] = $fangling1;
                $data['fangling2'] = $fangling2;
                $data['xqyuanyin'] = I('xqyuanyin');
                $data['khlaiyuan'] = I('khlaiyuan');
                $data['sfzheng'] = $sfzheng;
                $data['qqhao'] = $qqhao;
                $data['guoji'] = I('guoji');
                $data['minzu'] = I('minzu');
                $data['youxiang'] = $youxiang;
                $data['weixin'] = I('weixin');
                $data['jtgongju'] = I('jtgongju');
                $data['chexing'] = I('chexing');
                $data['beizhu'] = $beizhu;
                $data['gtjieduan'] = I('gtjieduan');
                $data['xflinian'] = I('xflinian');
                $data['kydengji'] = I('kydengji');
                $data['lururenid'] = session('uid');
                $data['weihurenid'] = session('uid');
                $data['weihuren'] = session('ygmingcheng');
                $data['mmzl'] = $mmzl;
                $data['lurusj'] = time();
                $data['zhgjsj'] = time();
                $data['xiugaisj'] = time();
                $isok = M('Keyuan')->add($data);
                if ($isok) {
                    $data_rizhi['zy_type'] = 2;
                    $data_rizhi['uid'] = session("uid");
                    $data_rizhi['addtime'] = time();
                    $data_rizhi['cz_type'] = 1;
                    $data_rizhi['zy_id'] = $isok;
                    $data_rizhi['zy_bianhao'] = $data['bianhao'];
                    switch ($mmzl) {
                        case 1:
                            $neirong = "新增求购客源";
                            break;
                        case 2:
                            $neirong = "新增求租客源";
                            break;
                    }
                    $data_rizhi['neirong'] = $neirong;
                    M('rizhi')->add($data_rizhi);
                    $this->success('添加成功');
                } else {
                    $this->error('添加失败');
                }
            } else {
                switch ($mmzl) {
                    case 1:
                        $fukuan = get_peizhi('fukuan',0,1);
                        break;
                    case 2:
                        $fukuan = get_peizhi('zlfukuan',0,1);
                        break;
                    default:
                        $this->error('异常错误');
                        break;
                }
                $this->fukuan = $fukuan;
                $this->guoji = get_peizhi('guoji',0,1);
                $this->minzu = get_peizhi('minzu',0,1);
                $this->peitao = get_peizhi('peitao',0,1);
                $this->yongtu = get_peizhi('yongtu',0,1);
                $this->kehulx = get_peizhi('yezhulx',0,1);
                $this->laiyuan = get_peizhi('laiyuan',0,1);
                $this->kydengji = get_peizhi('kydengji',0,1);
                $this->jtgongju = get_peizhi('jtgongju',0,1);
                $this->xflinian = get_peizhi('xflinian',0,1);
                $this->chaoxiang = get_peizhi('chaoxiang',0,1);
                $this->gtjieduan = get_peizhi('gtjieduan',0,1);
                $this->zhuangxiu = get_peizhi('zhuangxiu',0,1);
                $this->khzhuangtai = get_peizhi('khzhuangtai',0,1);
                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(){
            $id = I('id');
            $kybh = I('xqbianhao');
            if ($id) {
                $keyuan=M('keyuan')->where(array('id'=>$id))->find();
            } elseif ($kybh) {
                $keyuan=M('keyuan')->where(array('xqbianhao'=>$kybh))->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=zym($keyuan['lururenid'],14);
            $this->whdh=zym($keyuan['weihurenid'],14);
            $this->fpdh=zym($keyuan['fengpanrenid'],14);
            $qitiannei = time()-604800;
            $this->daikanshu=M('kydaikan')->where('gongsiid ='.session('gongsiid').' and kyid ='.$keyuan['id'].' and dkshijian >'.$qitiannei)->count();
            $this->khlaiyuan=get_peizhi('laiyuan',$keyuan['khlaiyuan'],3);
            $this->kehulx=get_peizhi('yezhulx',$keyuan['kehulx'],3);
            if ($keyuan['mmzl']==1) {
                $this->fukuan=get_peizhi('fukuan',$keyuan['fukuan'],3);
            } elseif ($keyuan['mmzl']==2) {
                $this->fukuan=get_peizhi('zlfukuan',$keyuan['fukuan'],3);
            } elseif ($keyuan['mmzl']==3) {
                $this->fukuan=get_peizhi('ysfukuan',$keyuan['fukuan'],3);
            }
            $this->gtjieduan=get_peizhi('gtjieduan',$keyuan['gtjieduan'],3);
            $this->kydengji=get_peizhi('kydengji',$keyuan['kydengji'],3);
            $this->xflinian=get_peizhi('xflinian',$keyuan['xflinian'],3);
            $this->guoji=get_peizhi('guoji',$keyuan['guoji'],3);
            $this->minzu=get_peizhi('minzu',$keyuan['minzu'],3);
            $this->jtgongju=get_peizhi('jtgongju',$keyuan['jtgongju'],3);
            $this->kygenjin=M('kygenjin')->where(array('kyid'=>$keyuan['id']))->order('gjshijian desc')->select();
            $this->kydaikan=M('kydaikan')->where(array('kyid'=>$keyuan['id']))->order('dkshijian desc')->select();
            $this->kytixing=M('kytixing')->where(array('kyid'=>$keyuan['id']))->order('txshijian desc')->select();
            $this->volist=M('rizhi')->where(array('zy_type'=>2,'zy_id'=>$keyuan['id']))->order('id desc')->select();
            $today="ksj".date("m-d");
            if (!cookie($today)) {
                cookie($today,$keyuan['id'],3600*24);
            }else{
                $value = cookie($today);
                $val=$value.','.$keyuan['id'];
                cookie($today,$val,3600*24);
            }
            $Model = new \Think\Model();
            if ($keyuan['peitao']) {
                $condition1="pzming = 'peitao' and lxid in (".$keyuan['peitao'].")";
                $this->peitao=$Model->query("select * from __PEIZHI__ where {$condition1} order by xsshunxu asc");
            }
            if ($keyuan['chaoxiang']) {
                $condition2="pzming = 'chaoxiang' and lxid in (".$keyuan['chaoxiang'].")";
                $this->chaoxiang=$Model->query("select * from __PEIZHI__ where {$condition2} order by xsshunxu asc");
            }
            if ($keyuan['zhuangxiu']) {
                $condition3="pzming = 'zhuangxiu' and lxid in (".$keyuan['zhuangxiu'].")";
                $this->zhuangxiu=$Model->query("select * from __PEIZHI__ where {$condition3} order by xsshunxu asc");
            }
            $this->display();
        }
        //客源跟进列表
        public function kygenjin(){
            if (I('is_ajax')) {
                /*权限控制开始*/
                $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";
                }
                /*权限控制结束*/
                $bumen = I('bumen');
                $sousuo = I('sousuo');
                $shijian = I('shijian');
                $genjinfs = I('genjinfs');
                if ($bumen) {
                    $string = get_bm_sz($bumen,2);
                    $condition .= " and e.bumen in ($string)";
                }
                if ($genjinfs) {
                    $condition .= " and a.genjinfs = ".$genjinfs;
                }
                if ($shijian) {
                    $time_s = explode(' - ',$shijian);
                    $time_s1 = strtotime($time_s[0]);
                    $time_s2 = strtotime($time_s[1])+86400;
                    $condition .= " and a.gjshijian>=".$time_s1." and a.gjshijian<".$time_s2;
                }
                if ($sousuo) {
                    $condition.=" and ((e.pinyinjs like '%$sousuo%') or (e.ygmingcheng like '%$sousuo%') or (e.dianhua like '%$sousuo%') or (a.kybh like '%$sousuo%'))";
                }
                $field = I('field'); //排序字段名
                $order = I('order'); //排序方式
                $limit = I('limit',30,'int'); //每页条数
                $limit = $limit > 150 ? 150 : $limit;
                if ($field && $order) {
                    $dlpx = "order by a.".$field." ".$order;
                } else {
                    $dlpx = "order by a.id desc";
                }
                $Model = new \Think\Model();
                $count = $Model->query("select count(*) from __KYGENJIN__ a join __YONGHU__ e on a.gjuid=e.id where {$condition}");
                $Page = new \Think\Page($count['0']['count(*)'],$limit);
                $list = "select a.*,e.ygmingcheng,e.bumen from __KYGENJIN__ a join __YONGHU__ e on a.gjuid=e.id where {$condition} ".$dlpx." limit ".$Page->firstRow.','.$Page->listRows;
                $data = $Model->query($list);
                foreach ($data as $key => $value) {
                    $bianhao = explode("-", $value['kybh']);
                    $data[$key]['bianhao'] = $value['kybh'];
                    $data[$key]['kybh'] = $bianhao[1];
                    $data[$key]['gjbm'] = zym($value['bumen'],6);
                    $data[$key]['gjfs'] = get_peizhi('genjinfs',$value['genjinfs'],3);
                    $data[$key]['gjshijian'] = $value['gjshijian'] ? date('Y-m-d H:i:s',$value['gjshijian']) : "——";
                    $gjky = M('keyuan')->where(array('xqbianhao'=>$value['kybh']))->field('zhuangtai,weihurenid,khxingming,kehulx,mmzl')->find();
                    switch ($gjky['mmzl']) {
                        case 1:
                            $kylx = "求购客源";
                            break;
                        case 2:
                            $kylx = "求租客源";
                            break;
                        default:
                            $kylx = "——";
                            break;
                    }
                    $data[$key]['kylx'] = $kylx;
                    $data[$key]['kyxm'] = $gjky['khxingming'].get_peizhi('yezhulx',$value['kehulx'],3);
                    $is_rbacky = _rbac_btn_show(75,$gjky['zhuangtai'],$gjky['weihurenid']);
                    $data[$key]['rbacky'] = $is_rbacky ? 1 : 0;
                }
                $return_data = array(
                    'code'=>0,
                    'count'=>$count['0']['count(*)'],
                    'data'=>$data
                );
                $this->ajaxReturn($return_data);
            } else {
                $this->genjinfs=get_peizhi('genjinfs',0,1);
                $select_categorys = bumen_option( $bumen, 237);
                $this->assign("select_categorys", $select_categorys);
                $this->display();
            }
        }
        //客源带看列表
        public function kydaikan(){
            if (I('is_ajax')) {
                /*权限控制开始*/
                $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";
                }
                /*权限控制结束*/
                $bumen = I('bumen');
                $sousuo = I('sousuo');
                $shijian = I('shijian');
                if ($bumen) {
                    $string = get_bm_sz($bumen,2);
                    $condition .= " and e.bumen in ($string)";
                }
                if ($shijian) {
                    $time_s = explode(' - ',$shijian);
                    $time_s1 = strtotime($time_s[0]);
                    $time_s2 = strtotime($time_s[1])+86400;
                    $condition .= " and a.dkshijian>=".$time_s1." and a.dkshijian<".$time_s2;
                }
                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%'))";
                }
                $field = I('field'); //排序字段名
                $order = I('order'); //排序方式
                $limit = I('limit',30,'int'); //每页条数
                $limit = $limit > 150 ? 150 : $limit;
                if ($field && $order) {
                    $dlpx = "order by a.".$field." ".$order;
                } else {
                    $dlpx = "order by a.id desc";
                }
                $Model = new \Think\Model();
                $count = $Model->query("select count(*) from __KYDAIKAN__ a join __YONGHU__ e on a.dkuid=e.id where {$condition}");
                $Page = new \Think\Page($count['0']['count(*)'],$limit);
                $list = "select a.*,e.ygmingcheng,e.bumen from __KYDAIKAN__ a join __YONGHU__ e on a.dkuid=e.id where {$condition} ".$dlpx." limit ".$Page->firstRow.','.$Page->listRows;
                $data = $Model->query($list);
                foreach ($data as $key => $value) {
                    $fybh = explode("-", $value['fybh']);
                    $data[$key]['bianhao'] = $value['fybh'];
                    $data[$key]['fybh'] = $fybh[1];
                    $kybh = explode("-", $value['kybh']);
                    $data[$key]['xqbianhao'] = $value['kybh'];
                    $data[$key]['kybh'] = $kybh[1];
                    $data[$key]['pkr'] = $value['peikanid'] ? zym($value['peikanid'],5) : "——";
                    $data[$key]['dkbm'] = zym($value['bumen'],6);
                    $data[$key]['dkshijian'] = $value['dkshijian'] ? date('Y-m-d H:i:s',$value['dkshijian']) : "——";
                    $dkky = M('keyuan')->where(array('xqbianhao'=>$value['kybh']))->field('zhuangtai,weihurenid,khxingming,kehulx,mmzl')->find();
                    switch ($dkky['mmzl']) {
                        case 1:
                            $kylx = "求购客源";
                            break;
                        case 2:
                            $kylx = "求租客源";
                            break;
                        default:
                            $kylx = "——";
                            break;
                    }
                    $data[$key]['kylx'] = $kylx;
                    $data[$key]['kyxm'] = $dkky['khxingming'].get_peizhi('yezhulx',$dkky['kehulx'],3);
                    $is_rbacky = _rbac_btn_show(75,$dkky['zhuangtai'],$dkky['weihurenid']);
                    $data[$key]['rbacky'] = $is_rbacky ? 1 : 0;
                    $fyxqlj = '';
                    $is_rbacfy = 0;
                    if (($dkky['mmzl'] == 1) || ($dkky['mmzl'] == 2)) {
                        $fy = M('fangyuan')->where(array('bianhao'=>$value['fybh']))->field('id,zhuangtai,weihurenid')->find();
                        $is_rbacfy = _rbac_btn_show(46,$fy['zhuangtai'],$fy['weihurenid']);
                        if ($is_rbacfy) {
                            $fyxqlj = U('fangyuan/neirong',array('id'=>$fy['id']));
                        }
                    }
                    $data[$key]['rbacfy'] = $is_rbacfy ? 1 : 0;
                    $data[$key]['fyxqlj'] = $fyxqlj;
                }
                $return_data = array(
                    'code'=>0,
                    'count'=>$count['0']['count(*)'],
                    'data'=>$data
                );
                $this->ajaxReturn($return_data);
            } else {
                $select_categorys = bumen_option( $bumen, 238);
                $this->assign("select_categorys", $select_categorys);
                $this->display();
            }
        }
        //客源提醒列表
        public function kytixing(){
            if (I('is_ajax')) {
                /*权限控制开始*/
                $condition = "e.gongsiid=".session('gongsiid');
                $x = _rbac_lists_where(858,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";
                }
                /*权限控制结束*/
                $bumen = I('bumen');
                $sousuo = I('sousuo');
                $shijian1 = I('shijian1');
                $shijian2 = I('shijian2');
                if ($bumen) {
                    $string = get_bm_sz($bumen,2);
                    $condition .= " and e.bumen in ($string)";
                }
                if ($shijian1) {
                    $time_s1 = explode(' - ',$shijian1);
                    $time_s11 = strtotime($time_s1[0]);
                    $time_s12 = strtotime($time_s1[1])+86400;
                    $condition .= " and a.txshijian>=".$time_s11." and a.txshijian<".$time_s12;
                }
                if ($shijian2) {
                    $time_s2 = explode(' - ',$shijian2);
                    $time_s21 = strtotime($time_s2[0]);
                    $time_s22 = strtotime($time_s2[1])+86400;
                    $condition .= " and a.time>=".$time_s21." and a.time<".$time_s22;
                }
                if ($sousuo) {
                    $condition.=" and ((e.pinyinjs like '%$sousuo%') or (e.ygmingcheng like '%$sousuo%') or (e.dianhua like '%$sousuo%') or (a.kybh like '%$sousuo%'))";
                }
                $field = I('field'); //排序字段名
                $order = I('order'); //排序方式
                $limit = I('limit',30,'int'); //每页条数
                $limit = $limit > 150 ? 150 : $limit;
                if ($field && $order) {
                    $dlpx = "order by a.".$field." ".$order;
                } else {
                    $dlpx = "order by a.id desc";
                }
                $Model = new \Think\Model();
                $count = $Model->query("select count(*) from __KYTIXING__ a join __YONGHU__ e on a.txuid=e.id where {$condition}");
                $Page = new \Think\Page($count['0']['count(*)'],$limit);
                $list = "select a.*,e.ygmingcheng,e.bumen,e.dianhua from __KYTIXING__ a join __YONGHU__ e on a.txuid=e.id where {$condition} ".$dlpx." limit ".$Page->firstRow.','.$Page->listRows;
                $data = $Model->query($list);
                foreach ($data as $key => $value) {
                    $bianhao = explode("-", $value['kybh']);
                    $data[$key]['bianhao'] = $value['kybh'];
                    $data[$key]['kybh'] = $bianhao[1];
                    $data[$key]['txbm'] = zym($value['bumen'],6);
                    $data[$key]['tjr'] = zym($value['adduid'],5);
                    $data[$key]['tjdh'] = zym($value['adduid'],14);
                    $data[$key]['txshijian'] = $value['txshijian'] ? date('Y-m-d H:i:s',$value['txshijian']) : "——";
                    $data[$key]['time'] = $value['time'] ? date('Y-m-d H:i:s',$value['time']) : "——";
                    $gjky = M('keyuan')->where(array('xqbianhao'=>$value['kybh']))->field('zhuangtai,weihurenid,khxingming,kehulx,mmzl')->find();
                    switch ($gjky['mmzl']) {
                        case 1:
                            $kylx = "求购客源";
                            break;
                        case 2:
                            $kylx = "求租客源";
                            break;
                        default:
                            $kylx = "——";
                            break;
                    }
                    $data[$key]['kylx'] = $kylx;
                    $data[$key]['kyxm'] = $gjky['khxingming'].get_peizhi('yezhulx',$value['kehulx'],3);
                    $is_rbacky = _rbac_btn_show(75,$gjky['zhuangtai'],$gjky['weihurenid']);
                    $data[$key]['rbacky'] = $is_rbacky ? 1 : 0;
                }
                $return_data = array(
                    'code'=>0,
                    'count'=>$count['0']['count(*)'],
                    'data'=>$data
                );
                $this->ajaxReturn($return_data);
            } else {
                $this->genjinfs=get_peizhi('genjinfs',0,1);
                $select_categorys = bumen_option( $bumen, 858);
                $this->assign("select_categorys", $select_categorys);
                $this->display();
            }
        }
    }
?>