<?php

    /*******************************************************************\
    |                                                                   |
    |   房开源（综合房产ERP管理系统）                                   |
    |                                                                   |
    |   © 2019-2023 http://www.kaidandashi.com/ All rights reserved.    |
    |                                                                   |
    |   本系统由淮南市银泰软件科技有限公司提供技术支持                  |
    |                                                                   |
    |   QQ号：984784483                                                 |
    |                                                                   |
    \*******************************************************************/

    namespace Admin\Controller;
    use Think\Controller;
    use QL\QueryList;
    class AjaxController extends CommonController {

        //提醒列表
        public function tixing(){
            $condition = "gongsiid=".session('gongsiid');
            $sousuo=I('sousuo');
            if ($sousuo == 0) {//搜索
                $condition.=" and txuid=".session('uid')." and txshijian<".time()." and isyidu=0";
            } elseif ($sousuo == 1) {
                $condition.=" and txuid=".session('uid')." and txshijian<".time()." and isyidu=1";
            } elseif ($sousuo == 2) {
                $condition.=" and txuid=".session('uid')." and isyidu=0";
            } elseif ($sousuo == 3) {
                $condition.=" and txuid=".session('uid')." and isyidu=1";
            } elseif ($sousuo == 4) {
                $condition.=" and txuid=".session('uid');
            }
            $shijian1=I('shijian1');
            $shijian2=I('shijian2');
            if ($shijian1) {
                $this->shijian1=$shijian1;
                $shijian3=strtotime($shijian1);
            }
            if ($shijian2) {
                $this->shijian2=$shijian2;
                $shijian4=strtotime($shijian2)+86400;
            }
            if ($shijian3 and $shijian4){
                if ($shijian3 > $shijian4){
                    $condition.=" and txshijian>".$shijian4." and txshijian<".$shijian3;
                    $this->shijian1=$shijian2;
                    $this->shijian2=$shijian1;
                } else {
                    $condition.=" and txshijian>".$shijian3." and txshijian<".$shijian4;
                }
            } elseif ($shijian3) {
                $condition.=" and txshijian>".$shijian3;
            } elseif ($shijian4) {
                $condition.=" and txshijian<".$shijian4;
            }
            $Model = new \Think\Model;
            $count=$Model->query("select count(*) from __KYTIXING__ where {$condition}");
            $Page=new \Think\Page($count['0']['count(*)'],20);
            $show  = $Page->show();
            $list="select * from __KYTIXING__ where {$condition} order by txshijian DESC limit ".$Page->firstRow.','.$Page->listRows;
            $tixing=$Model->query($list);
            $this->assign('firstRow',$Page->firstRow);
            $this->assign('tixing',$tixing);
            $this->assign('count',$count);
            $this->assign('page',$show);
            $this->display();
        }

        //提醒详情
        public function tixingxq(){
            $id=I('id');
            $tixing=M('kytixing')->where(array('id'=>$id))->find();
            $this->tixing=$tixing;
            $this->txren=zym($tixing['txuid'],5);
            $this->tjren=zym($tixing['adduid'],5);
            if ($tixing['txshijian'] < time()) {
                $isok=M('kytixing')->where(array('id'=>$id))->setField('isyidu',1);
            }
            $this->display();
        }

        //错误提示
        public function error(){
            $this->error = I('error');
            $this->display();
        }

        //获取拼音检索
        public function get_pinyin(){
            $txt=I('txt');
            $py = new \Lib\Pinyin\Pinyin;
            $s = $py->qupinyin($txt ,true);
            $q = $py->qupinyin($txt);
            $h = $s.' '.$q.' '.$txt;
            $data=array('status'=>1,'pyjs'=>$h);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        //获取部门
        public function get_bumen(){
            $select_categorys = bumen_option( 0, I('jiedian'));
            $data=array('status'=>1,'sousuo'=>$select_categorys);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        //获取部门用户
        public function get_bumen_yonghu(){
            $bmid=I('bmid');
            $map['status'] = 1;
            $map['bumen'] = $bmid;
            $list=M("yonghu")->where($map)->field('id,ygmingcheng')->order('pinyinjs asc')->select(); 
            $data=array('status'=>1,'sousuo'=>$list);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        //获取在职用户
        public function get_yonghu(){
            $node=I('node');
            $txt=I('txt');
            $map['status'] = 1;
            $map['lock1'] = 0;
            $map['_string']='(pinyinjs like "%'.$txt.'%") OR (ygmingcheng like "%'.$txt.'%") OR (dianhua like "%'.$txt.'%")';
            if ($node) {
                $x = _rbac_lists_where($node,0);
                if ($x && $x != "all" && $x != "me") {
                    $map['bumen'] = array('in',$x);
                }elseif ($x == "me") {
                    $map['id'] = session('uid');
                }elseif ($x == false) {
                    $map['id'] = 0;
                }
            }
            $list=M("yonghu")->where($map)->field('id,ygmingcheng')->limit(10)->order('pinyinjs asc')->select(); 
            $data=array('status'=>1,'district'=>$list);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        //获取未删除用户
        public function get_rsyonghu(){
            $txt=I('txt');
            $node=I('node');
            $x = _rbac_lists_where($node,0);
            if ($x && $x != "all" && $x != "me") {
                $map['bumen'] = array('in',$x);
            }elseif ($x == "me") {
                $map['id'] = session('uid');
            }elseif ($x == false) {
                $map['id'] = 0;
            }
            $map['status'] = 1;
            $map['gongsiid'] = session('gongsiid');
            $map['_string']='(pinyinjs like "%'.$txt.'%") or (ygmingcheng like "%'.$txt.'%") or (ygbianhao like "%'.$txt.'%") or (dianhua like "%'.$txt.'%")';
            $list=M('yonghu')->where($map)->field('id,ygmingcheng')->limit(10)->order('pinyinjs asc')->select();
            $data=array('status'=>1,'district'=>$list);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        // 房源打印模板管理图片上传（异步）
        public function upload(){
            if(!empty($_FILES)){
                $upload=new \Think\Upload();
                $upload->maxSize=3145728; //文件上传的大小限制
                $upload->exts=array('jpg','gif','png','jpeg'); //上传文件后缀的样式
                $upload->rootPath='./';   //上传文件根目录
                $upload->savePath='Upload/'.session('gongsiid').'/dayinmoban/'; //上传文件的子目录
                $upload->autoSub = false;
                $upload->saveName=date("Ymdhis",time()).rand(1,9);//上传文件的名称
                $info=$upload->uploadOne($_FILES['imageload']); //上传单文件
                if(!$info){
                    $data['status']=0;
                    $data['content']="上传失败";
                    $this->ajaxReturn($data);
                }else{
                    $data['status']=1;
                    $data['src']=__ROOT__.'/'.$info['savepath'].$info['savename'];
                    $data['upfilename']=$info['savename'];
                    $data['content']="上传成功";
                    $this->ajaxReturn($data);
                }
            }
        }

        //模糊查询楼盘
        public function get_xiaoqu_txt(){
            $txt=I('txt');
            $listObj = M('xiaoqu');
            $where['_string']='(xiaoqum like "%'.$txt.'%") OR (pinyinjs like "%'.$txt.'%")';
            $where['gongsiid'] = session('gongsiid');
            $list = $listObj->where($where)->field('id,xiaoqum')->limit(10)->select();
            $data=array('status'=>1,'lists'=>$list);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        // 添加坐标
        public function addzb(){
            $zuobiao = I('zuobiao');
            if ($zuobiao) {
                $zb = $zuobiao;
                $this->isedit = 1;
            }else{
                $zb = $this->jcpz['dtzuobiao'];
            }
            $this->zb = $zb;
            $this->display();
        }

        //获取房源编号
        public function get_fybianhao_txt(){
            $txt = I('txt');
            $mmzl = I('mmzl');
            $fylx = I('fylx');
            $fylxs = "1,9,13,14";
            $fylxs1 = explode(",",$fylxs);
            if (in_array($fylx,$fylxs1)) {
                switch ($fylx) {
                    case '1':
                        $fyb = "__FANGYUAN__";
                        break;
                    case '9':
                        $fyb = "__YSFANGYUAN__";
                        break;
                    case '13':
                        $fyb = "__FYXIEZILOU__";
                        break;
                    case '14':
                        $fyb = "__FYSHANGPU__";
                        break;
                    default:
                        break;
                }
                if ($fylx == 9) {
                    $node = 262;
                } else {
                    $node = 41;
                }
                $condition = " where e.gongsiid=".session('gongsiid');
                $x = _rbac_lists_where(41,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";
                }
                if ($fylx == 9) {
                    $condition .= " and (ysbianhao like '%$txt%') ";
                    $cxzd="ysbianhao";
                } else {
                    $condition .= " and a.mmzl =".$mmzl." and (a.bianhao like '%$txt%') and a.zhuangtai not in (".get_options('chengjiao_options','fyfengpan_zhuangtai').")";
                    $cxzd="bianhao";
                }
                $list = "select a.id,a.".$cxzd." from ".$fyb." a join __YONGHU__ e on a.weihurenid=e.id {$condition} limit 10 ";
                $Model = new \Think\Model;
                $fylb = $Model->query($list);
                $data = array('status'=>1,'lists'=>$fylb);
            } else {
                $data = array('status'=>0,'info'=>"异常错误！");
            }
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        // 中介黑名单搜索框异步下拉
        public function get_zjhmd_ziyuan(){
            $txt=I('txt');
            $where['_string']='(sjh like "%'.$txt.'%")';
            $where['gongsiid'] = session('gongsiid');
            $list=M('Qtjjr')->where($where)->limit(10)->select();
            $data=array('district'=>$list);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        //获取配置新数据
        public function get_pzxsj(){
            $listObj = M('peizhi');
            $list = $listObj->order('id desc')->find();
            $data=array('pics'=>$list);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        //获取岗位新数据
        public function get_jsxsj(){
            $listObj = M('role');
            $list = $listObj->order('id desc')->find();
            $data=array('pics'=>$list);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        //关联
        public function guanlian(){
            $qxpdep = _rbac_btn_show(425,0) == true? '1': '0';
            $qxpddp = _rbac_btn_show(427,0) == true? '1': '0';
            $id=I('id');
            $where['gongsiid']=session('gongsiid');
            $where['id']=$id;
            $pzming=get_pzmzs($id,1);
            $leixing=get_peizhi($pzming,0,1);
            $data=array('status'=>0,'leixing'=>$leixing,'qxpdep'=>$qxpdep,'qxpddp'=>$qxpddp);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        // 异步获取打印模板新数据
        public function get_mobanxsj_ziyuan(){
            $listObj = M('fydayin');
            $list = $listObj->order('id desc')->find();
            $data=array('pics'=>$list);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        // 房源打印模板搜索框异步下拉
        public function get_moban_ziyuan(){
            $txt=I('txt');
            $listObj = M('fydayin');
            $where['_string']='(mbmingcheng like "%'.$txt.'%")';
            $where['gongsiid'] = session('gongsiid');
            $list = $listObj->where($where)->limit(10)->select();
            $data=array('status'=>0,'district'=>$list);
            header("Content-type: application/json");
            exit(json_encode($data));
        } 

        /**
            * [get_fy_img 异步返回图片列表]
            * @return [json] [图片相关的数组]
        */

        public function get_fy_img(){
            $fylx = I('fylx');
            $listObj = M('Photo');
            $where['p.fybh'] = I('fy_bh');
            $pzming = "";
            switch ($fylx) {
                case 1:
                    $fyb = M('fangyuan');
                    break;
                case 13:
                    $fyb = M('fyxiezilou');
                    $pzming = "xzltplx";
                    break;
                case 14:
                    $fyb = M('fyshangpu');
                    $pzming = "sptplx";
                    break;
                default:
                    $fyb = "";
                    break;
            }
            if ($fyb) {
                if ($fylx == 1) {
                    $fangyuan = $fyb->where(array('bianhao'=>I('fy_bh')))->field('zhuangtai,yongtu')->find();
                    switch ($fangyuan['yongtu']) {
                        case 1:
                            $pzming = "zztplx"; //住宅
                            break;
                        case 2:
                            $pzming = "bstplx"; //别墅
                            break;
                        case 3:
                            $pzming = "sptplx"; //商铺
                            break;    
                        case 4:
                            $pzming = "cktplx"; //仓库
                            break;
                        case 5:
                            $pzming = "chftplx"; //厂房
                            break;
                        case 6:
                            $pzming = "tdtplx"; //土地
                            break;
                        case 7:
                            $pzming = "chktplx"; //车库
                            break;
                        case 8:
                            $pzming = "xzltplx"; //写字楼
                            break;
                        default:
                            $pzming = "zztplx"; //住宅
                            break;
                    }
                } else {
                    $fangyuan = $fyb->where(array('bianhao'=>I('fy_bh')))->field('zhuangtai')->find();
                }
                foreach ($list as &$value) {
                    $temp = get_peizhi($pzming,$value['leixing'],3);
                    $value['lxming'] = $temp ? $temp : '未设置图片类型';
                    $value['fm'] = _rbac_btn_show(481,$fangyuan['zhuangtai'],$value['uid']) == true? '1': '0';
                    $value['del']  = _rbac_btn_show(50,$fangyuan['zhuangtai'],$value['uid']) == true? '1': '0';
                }
            } else {
                $list = "";
            }
            //返回查询结果到异步json
            $data=array('pics'=>$list);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        /**
            * [get_xq_img 异步返回图片列表]
            * @return [json] [图片相关的数组]
        */

        public function get_xq_img(){
            $listObj = M('xqphoto');
            $where['p.xqid'] = I('xq_id');
            $list = $listObj->alias('p')->join(' jjrxt_yonghu as y on p.uid=y.id ')->field('p.id,p.uid,p.leixing,p.leixing as lxming,p.fengmian,p.gongsiid,p.image,FROM_UNIXTIME(p.create_time,"%Y-%m-%d") AS create_time,p.xqid,y.ygmingcheng')->where($where)->order('p.id desc')->select();
            if ($list) {
                $pzming='xqtplx';
            }
            foreach ($list as &$value) {
                $temp = get_peizhi($pzming,$value['leixing'],3);
                $value['lxming'] = $value['leixing']?$temp:'未设置图片类型';
                $value['fm'] = _rbac_btn_show(485,0,$value['uid']) == true? '1': '0';
                $value['del']  = _rbac_btn_show(188,0,$value['uid']) == true? '1': '0';
            }
            //返回查询结果到异步json
            $data=array('pics'=>$list);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        //房源匹配客源
        public function fyppky(){
            $kylx = I('kylx');
            $kylxs = "2,15,16";
            $kylxs1 = explode(",",$kylxs);
            if (in_array($kylx,$kylxs1)) {
                switch ($kylx) {
                    case '1':
                        $shi = I('shi');
                        $niandai = I('niandai');
                        $chaoxiang = I('chaoxiang');
                        $fyb = "__KEYUAN__";
                        break;
                    case '15':
                        $fyb = "__KYXIEZILOU__";
                        break;
                    case '16':
                        $yetai = I('yetai');
                        $hangye = I('hangye');
                        $fenge = I('fenge');
                        $zuyue = I('zuyue');
                        $fyb = "__KYSHANGPU__";
                        break;
                    default:
                        break;
                }
                $mmzl=I('mmzl');
                if ($mmzl != 1 && $mmzl != 2) {
                    $data=array(
                        'info' => '房源买卖租赁类型异常！',
                        'status' => 0,
                    );
                    $this->ajaxReturn($data);
                }
                $leixing=I('leixing');
                $shoujia=I('shoujia');
                $zujia=I('zujia');
                $mianji=I('mianji');
                $louceng=I('louceng');
                $xiaoqum=I('xiaoqum');
                $zhuangxiu=I('zhuangxiu');
                if (!$leixing && !$shoujia && !$zujia && !$mianji && !$louceng && !$xiaoqum && !$zhuangxiu && !$shi && !$niandai && !$chaoxiang && !$yetai && !$hangye && !$fenge && !$zuyue) {
                    $data=array(
                        'info' => '请选择匹配条件！',
                        'status' => 0,
                    );
                    $this->ajaxReturn($data);
                }
                $condition = " a.gongsiid=".session('gongsiid')." and a.mmzl=".$mmzl." and a.zhuangtai not in (".get_options('chengjiao_options','fyfengpan_zhuangtai').")";
                if ($leixing) {
                    $condition.=" and a.xqleixing = ".$leixing;
                }
                if ($yetai) {
                    $condition.=" and a.yetai = ".$yetai;
                }
                if ($hangye) {
                    $condition.=" and a.hangye = ".$hangye;
                }
                if ($fenge) {
                    $condition.=" and (a.fenge = ".$fenge." or a.fenge = 3)";
                }
                if ($zuyue) {
                    $condition.=" and (a.zuyue = ".$zuyue." or a.zuyue = 3)";
                }
                if ($mmzl == 1) {
                    if ($shoujia) {
                        $condition.=" and a.xqjiage1 <= ".$shoujia." and (a.xqjiage2 >= ".$shoujia." or a.xqjiage2 < 1)";
                    }
                } elseif ($mmzl == 2) {
                    if ($zujia) {
                        $condition.=" and a.xqjiage1 <= ".$zujia." and (a.xqjiage2 >= ".$zujia." or a.xqjiage2 < 1)";
                    }
                }
                if ($mianji) {
                    $condition.=" and a.xqmianji1 <= ".$mianji." and (a.xqmianji2 >= ".$mianji." or a.xqmianji2 < 1)";
                }
                if ($shi) {
                    $condition.=" and a.xqhuxing1 <= ".$shi." and (a.xqhuxing2 >= ".$shi." or a.xqhuxing2 < 1)";
                }
                if ($louceng) {
                    $condition.=" and a.louceng1 <= ".$louceng." and (a.louceng2 >= ".$louceng." or a.louceng2 < 1)";
                }
                if ($niandai) {
                    $fyfl=date("Y",time())-$niandai+1;
                    $condition.=" and a.fangling1 <= ".$fyfl." and (a.fangling2 >= ".$fyfl." or a.fangling2 < 1)";
                }
                if ($xiaoqum) {
                    $condition.=" and a.xqxiaoqu like '%".$xiaoqum."%'";
                }
                if ($zhuangxiu) {
                    $condition.=" and a.zhuangxiu like '%".$zhuangxiu."%'";
                }
                if ($chaoxiang) {
                    $condition.=" and a.chaoxiang like '%".$chaoxiang."%'";
                }
                $Model = new \Think\Model;
                $xqky=$Model->query("select a.id,a.xqbianhao,a.zhuangtai,a.weihurenid,a.khxingming,a.kehulx,b.ygmingcheng,b.dianhua from __KEYUAN__ a join __YONGHU__ b on a.weihurenid=b.id where {$condition}");
                foreach ($xqky as $key => $value) {
                    $xqky[$key]['xqqxpb']=_rbac_btn_show(75,$value['zhuangtai'],$value['weihurenid']) == true? '1': '0';
                    $xqky[$key]['bmming']=yhbm($value['weihurenid'],3);
                    $xqky[$key]['yezhulx']=get_peizhi('yezhulx',$value['kehulx'],3);
                    $xqky[$key]['khzt']=get_peizhi('khzhuangtai',$value['zhuangtai'],3);
                }
                $data=array(
                    'info' => '匹配成功',
                    'status' => 1,
                    'data' => $xqky,
                );
                $this->ajaxReturn($data);
            } else {
                $data=array(
                    'info' => '房源写字楼商铺住宅类型异常！',
                    'status' => 0,
                );
                $this->ajaxReturn($data);
            }
        }

        //客源匹配房源
        public function kyppfy(){
            $fylx = I('fylx');
            $fylxs = "1,13,14";
            $fylxs1 = explode(",",$fylxs);
            if (in_array($fylx,$fylxs1)) {
                switch ($fylx) {
                    case '1':
                        $xqhuxing = I('xqhuxing');
                        $fangling = I('fangling');
                        $chaoxiang = I('chaoxiang');
                        $fyb = "__FANGYUAN__";
                        break;
                    case '13':
                        $fyb = "__FYXIEZILOU__";
                        break;
                    case '14':
                        $yetai = I('yetai');
                        $hangye = I('hangye');
                        $fenge = I('fenge');
                        $zuyue = I('zuyue');
                        $fyb = "__FYSHANGPU__";
                        break;
                    default:
                        break;
                }
                $mmzl = I('mmzl');
                if ($mmzl != 1 && $mmzl != 2) {
                    $data=array(
                        'info' => '客源求购求租类型异常！',
                        'status' => 0,
                    );
                    $this->ajaxReturn($data);
                }
                $xqxiaoqu = I('xqxiaoqu');
                $xqleixing = I('xqleixing');
                $xqjiage = I('xqjiage');
                $xqmianji = I('xqmianji');
                $louceng = I('louceng');
                $zhuangxiu = I('zhuangxiu');
                if (!$xqxiaoqu && !$xqleixing && !$yetai && !$hangye && !$xqjiage && !$xqmianji && !$xqhuxing && !$louceng && !$fangling && !$zhuangxiu && !$chaoxiang && !$fenge && !$zuyue) {
                    $data=array(
                        'info' => '请选择匹配条件！',
                        'status' => 0,
                    );
                    $this->ajaxReturn($data);
                }
                $condition = " a.gongsiid=".session('gongsiid')." and a.mmzl=".$mmzl." and a.zhuangtai not in (".get_options('chengjiao_options','fyfengpan_zhuangtai').")";
                if ($xqxiaoqu) {
                    $xqxiaoqud = explode("；", $xqxiaoqu);
                    $conditionxq="";
                    foreach ($xqxiaoqud as $key => $value) {
                        if ($value) {
                            if ($key == 0) {
                                $conditionxq.=" xiaoqum = '".$value."'";
                            } else {
                                $conditionxq.=" or xiaoqum = '".$value."'";
                            }
                        }
                    }
                    $condition.=" and (".$conditionxq.")";
                }
                if ($xqleixing) {
                    $condition.=" and leixing = ".$xqleixing;
                }
                if ($yetai) {
                    $condition.=" and yetai = ".$yetai;
                }
                if ($hangye) {
                    $condition.=" and hangye = ".$hangye;
                }
                if ($xqjiage) {
                    $xqjg1=explode(",", $xqjiage);
                    $xqjg2=explode(",", $xqjiage);
                    $xqjiage1=$xqjg1[0];
                    $xqjiage2=$xqjg2[1];
                }
                if ($mmzl == 1) {
                    if ($xqjiage1 && $xqjiage2) {
                        $condition.=" and shoujia >= ".$xqjiage1." and shoujia <= ".$xqjiage2;
                    }else if ($xqjiage1) {
                        $condition.=" and shoujia >= ".$xqjiage1;
                    }else if ($xqjiage2) {
                        $condition.=" and shoujia <= ".$xqjiage2;
                    }
                } elseif ($mmzl == 2) {
                    if ($xqjiage1 && $xqjiage2) {
                        $condition.=" and zujia >= ".$xqjiage1." and zujia <= ".$xqjiage2;
                    }else if ($xqjiage1) {
                        $condition.=" and zujia >= ".$xqjiage1;
                    }else if ($xqjiage2) {
                        $condition.=" and zujia <= ".$xqjiage2;
                    }
                }
                if ($xqmianji) {
                    $xqmj1=explode(",", $xqmianji);
                    $xqmj2=explode(",", $xqmianji);
                    $xqmianji1=$xqmj1[0];
                    $xqmianji2=$xqmj2[1];
                }
                if ($xqmianji1 && $xqmianji2) {
                    $condition.=" and mianji >= ".$xqmianji1." and mianji <= ".$xqmianji2;
                }else if ($xqmianji1) {
                    $condition.=" and mianji >= ".$xqmianji1;
                }else if ($xqmianji2) {
                    $condition.=" and mianji <= ".$xqmianji2;
                }
                if ($xqhuxing) {
                    $xqhx1=explode(",", $xqhuxing);
                    $xqhx2=explode(",", $xqhuxing);
                    $xqhuxing1=$xqhx1[0];
                    $xqhuxing2=$xqhx2[1];
                }
                if ($xqhuxing1 && $xqhuxing2) {
                    $condition.=" and shi >= ".$xqhuxing1." and shi <= ".$xqhuxing2;
                }else if ($xqhuxing1) {
                    $condition.=" and shi >= ".$xqhuxing1;
                }else if ($xqhuxing2) {
                    $condition.=" and shi <= ".$xqhuxing2;
                }
                if ($louceng) {
                    $xqlc1=explode(",", $louceng);
                    $xqlc2=explode(",", $louceng);
                    $louceng1=$xqlc1[0];
                    $louceng2=$xqlc2[1];
                }
                if ($louceng1 && $louceng2) {
                    $condition.=" and louceng >= ".$louceng1." and louceng <= ".$louceng2;
                }else if ($louceng1) {
                    $condition.=" and louceng >= ".$louceng1;
                }else if ($louceng2) {
                    $condition.=" and louceng <= ".$louceng2;
                }
                if ($fangling) {
                    $xqfl1=explode(",", $fangling);
                    $xqfl2=explode(",", $fangling);
                    $fangling1=$xqfl1[0];
                    $fangling2=$xqfl2[1];
                }
                if ($fangling1 && $fangling2) {
                    $fyfl1=date("Y",time())-$fangling1+1;
                    $fyfl2=date("Y",time())-$fangling2+1;
                    $condition.=" and niandai <= ".$fyfl1." and niandai >= ".$fyfl2;
                }else if ($fangling1) {
                    $fyfl1=date("Y",time())-$fangling1+1;
                    $condition.=" and niandai <= ".$fyfl1;
                }else if ($fangling2) {
                    $fyfl2=date("Y",time())-$fangling2+1;
                    $condition.=" and niandai >= ".$fyfl2;
                }
                if ($zhuangxiu) {
                    $condition.=" and zhuangxiu in (".$zhuangxiu.")";
                }
                if ($chaoxiang) {
                    $condition.=" and chaoxiang in (".$chaoxiang.")";
                }
                if ($fenge != 2) {
                    $condition.=" and fenge =".$fenge;
                }
                if ($zuyue != 2) {
                    $condition.=" and zuyue =".$zuyue;
                }
                $Model = new \Think\Model;
                $xqfy=$Model->query("select a.id,a.bianhao,a.zhuangtai,a.weihurenid,a.xiaoqu,b.ygmingcheng,b.dianhua from ".$fyb." a join __YONGHU__ b on a.weihurenid=b.id ");
                foreach ($xqfy as $key => $value) {
                    $xqfy[$key]['xqqxpb']=_rbac_btn_show(46,$value['zhuangtai'],$value['weihurenid']) == true? '1': '0';
                    $xqfy[$key]['xiaoqum']=zym($value['xiaoqu'],3);
                    $xqfy[$key]['bmming']=yhbm($value['weihurenid'],3);
                    $xqfy[$key]['fyzt']=get_peizhi('zhuangtai',$value['zhuangtai'],3);
                }
                $data=array(
                    'info' => '匹配成功',
                    'status' => 1,
                    'data' => $xqfy,
                );
                $this->ajaxReturn($data);
            } else {
                $data=array(
                    'info' => '客源写字楼商铺住宅类型异常！',
                    'status' => 0,
                );
                $this->ajaxReturn($data);
            }
        }

        //选择部门查询当前部门下的用户
        public function select_yuangong(){
            $id = I('id');
            $user = M('yonghu')->where(array('bumen'=>$id,'status'=>1))->field('id,ygmingcheng')->order('pinyinjs asc')->select();
            if($user){
                $this->ajaxReturn($user);
            }else{
                $this->ajaxReturn(array('status'=>0,'info'=>'当前部门下没有用户'));
            }
        }

        //查询当前行政区下的商圈
        public function select_pianqu(){
            $id = I('xzqid');
            $map .= " where gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(123,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');
            $condition.=" and ssxzq=".$id;
            $list="select a.id,a.pianqum from (select id,pianqum,whrid,pinyinjs from __PIANQU__ where {$condition}) a join (select id as e_id from __YONGHU__ {$map}) e on a.whrid=e.e_id order by a.pinyinjs asc ";
            $Model = new \Think\Model;
            $pianqu=$Model->query($list);
            if($pianqu){
                $this->ajaxReturn($pianqu);
            }else{
                $this->ajaxReturn(array('status'=>0,'info'=>'当前行政区下没有商圈'));
            }
        }

        //搜索行政区
        public function ss_xzq(){
            $map .= " where gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(372,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');
            $txt=I('txt');
            if ($txt) {
                $condition.=" and ((xzqming like '%$txt%') or (pinyinjs like '%$txt%'))";
            }
            $list="select a.id,a.xzqming from (select id,xzqming,whrid,pinyinjs from __XINGZHENGQU__ where {$condition}) a join (select id as e_id from __YONGHU__ {$map}) e on a.whrid=e.e_id order by a.pinyinjs asc limit 10 ";
            $Model = new \Think\Model;
            $xzq=$Model->query($list);
            $data=array('status'=>1,'lists'=>$xzq);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        //搜索商圈
        public function ss_pq(){
            $map .= " where gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(123,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');
            $txt=I('txt');
            if ($txt) {
                $condition.=" and ((pianqum like '%$txt%') or (pinyinjs like '%$txt%'))";
            }
            $list="select a.id,a.pianqum from (select id,pianqum,whrid,pinyinjs from __PIANQU__ where {$condition}) a join (select id as e_id from __YONGHU__ {$map}) e on a.whrid=e.e_id order by a.pinyinjs asc limit 10 ";
            $Model = new \Think\Model;
            $pq=$Model->query($list);
            $data=array('status'=>1,'lists'=>$pq);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        //搜索楼盘
        public function ss_xq(){
            $map .= " where gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(129,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');
            $txt=I('txt');
            if ($txt) {
                $condition.=" and ((xiaoqum like '%$txt%') or (pinyinjs like '%$txt%'))";
            }
            $list="select a.id,a.xiaoqum from (select id,xiaoqum,whrid,pinyinjs from __XIAOQU__ where {$condition}) a join (select id as e_id from __YONGHU__ {$map}) e on a.whrid=e.e_id order by a.pinyinjs asc limit 10 ";
            $Model = new \Think\Model;
            $xq=$Model->query($list);
            $data=array('status'=>1,'lists'=>$xq);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        //搜索学区
        public function ss_xx(){
            $map .= " where gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(135,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');
            $txt=I('txt');
            if ($txt) {
                $condition.=" and ((xuexiaom like '%$txt%') or (pinyinjs like '%$txt%'))";
            }
            $list="select a.id,a.xuexiaom from (select id,xuexiaom,whrid,pinyinjs from __XUEQU__ where {$condition}) a join (select id as e_id from __YONGHU__ {$map}) e on a.whrid=e.e_id order by a.pinyinjs asc limit 10 ";
            $Model = new \Think\Model;
            $xx=$Model->query($list);
            $data=array('status'=>1,'lists'=>$xx);
            header("Content-type: application/json");
            exit(json_encode($data));
        }

        //重复房源判断
        public function cffypd(){
            $fjid=I('fjid');
            $fylx=I('fylx');
            switch ($fylx) {
                case '1':
                    $fyb = M('fyxiezilou');
                    break;
                case '2':
                    $fyb = M('fyshangpu');
                    break;
                case '3':
                    $fyb = M('fangyuan');
                    break;
                default:
                    $fylx=1;
                    $fyb = M('fyxiezilou');
                    break;
            }
            if ($fjid) {
                $fangjian=M('fjxinxi')->where(array('id'=>$fjid))->find();
                $xiaoqu=$fangjian['xqid'];
                if ($fangjian['fjleixing'] != $fylx) {
                    $this->ajaxReturn(array('status'=>1,'info'=>'房间物业类型不匹配！'));
                }
            } else {
                $xiaoqu=I('xiaoqu');
            }
            if ($fylx != 1 && $fylx != 2) {
                $fanghao=$fangjian['fjmwdw'];
                $danyuan=M('dyxinxi')->where(array('id'=>$fangjian['dyid']))->getField('dymwdw');
                $zuodong=M('zdxinxi')->where(array('id'=>$fangjian['zdid']))->getField('zdmwdw');
            } else {
                $zuodong=I('zuodong');
                $danyuan=I('danyuan');
                $fanghao=I('fanghao');
            }
            $mmzl=I('mmzl');
            $condition['gongsiid']=session('gongsiid');
            $condition['zhuangtai']=array('neq',get_options('chengjiao_options','fychengjiao_zhuangtai'));
            $condition['isdel']=2;
            $condition['xiaoqu']=$xiaoqu;
            $condition['zuodong']=$zuodong;
            $condition['danyuan']=$danyuan;
            $condition['fanghao']=$fanghao;
            $condition['mmzl']=$mmzl;
            $cffy=$fyb->where($condition)->find();
            if($cffy){
                $this->ajaxReturn(array('status'=>1,'info'=>'房源已存在！'));
            }else{
                $this->ajaxReturn(array('status'=>2));
            }
        }

        /**
            * 获取递归数组深度
            * @param $id
            * @param $array
            * @param $i
        */

        public function get_level($id, $array = array(), $i = 0) {
            if ($array[$id]['parentid']==0 || empty($array[$array[$id]['parentid']]) || $array[$id]['parentid']==$id){
                return  $i;
            }else{
                $i++;
                return $this->get_level($array[$id]['parentid'],$array,$i);
            }
        }

    }