<?php

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

    namespace Admin\Controller;
    use Think\Controller;
    class FangyuanController extends CommonController {
        //二手房源列表
        public function Esfy(){
            if (I('is_ajax')) {
                $leixing = I('leixing',0,'int');
                $xingzhengqu = I('xingzhengqu',0,'int');
                $pianqu = I('pianqu',0,'int');
                $zhuangtai = I('zhuangtai',0,'int');
                $zhuangxiu = I('zhuangxiu',0,'int');
                $chaoxiang = I('chaoxiang',0,'int');
                $yongtu = I('yongtu',0,'int');
                $fengpan = I('fengpan',0,'int');
                $dshenhe = I('dshenhe',0,'int');
                $isdel = I('isdel',0,'int');
                $myfy = I('myfy',0,'int');
                $mysc = I('mysc',0,'int');
                $jrll = I('jrll',0,'int');
                $tupian = I('tupian',0,'int');
                $shipin = I('shipin',0,'int');
                $quanjing = I('quanjing',0,'int');
                $xinshang = I('xinshang',0,'int');
                $heyan = I('heyan',0,'int');
                $jishou = I('jishou',0,'int');
                $xuequ = I('xuequ',0,'int');
                $wushui = I('wushui',0,'int');
                $xinzheng = I('xinzheng',0,'int');
                $youdaikuan = I('youdaikuan',0,'int');
                $waiwangtb = I('waiwangtb',0,'int');
                $yaoshi = I('yaoshi',0,'int');
                $shijian = I('shijian');
                $zuodong = I('zuodong');
                $louceng1 = I('louceng1',0,'int');
                $louceng2 = I('louceng2',0,'int');
                $huxing1 = I('huxing1',0,'int');
                $huxing2 = I('huxing2',0,'int');
                $jiage1 = I('jiage1',0,'int');
                $jiage2 = I('jiage2',0,'int');
                $mianji1 = I('mianji1',0,'int');
                $mianji2 = I('mianji2',0,'int');
                $sousuo = sql_check_param(I('sousuo'));
                /*权限控制开始*/
                $condition = "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";
                }
                /*权限控制结束*/
                $options_arr = get_options('chengjiao_options','fyfengpan_zhuangtai');
                $is_rbacfck = _rbac_node_show(863);
                if (!$is_rbacfck) {
                    $condition .= " and a.fpck=2";
                }
                if ($leixing) {//类型
                    $condition .= " and a.leixing = '$leixing'";
                }
                if ($xingzhengqu) {//行政区
                    $condition .= " and a.xingzhengqu = '$xingzhengqu'";
                }
                if ($pianqu) {//片区
                    $condition .= " and a.pianqu = '$pianqu'";
                }
                if ($zhuangtai) {//状态
                    $condition .= " and a.zhuangtai = '$zhuangtai'";
                }
                if ($zhuangxiu) {//装修
                    $condition .= " and a.zhuangxiu = '$zhuangxiu'";
                }
                if ($chaoxiang) {//朝向
                    $condition .= " and a.chaoxiang = '$chaoxiang'";
                }
                if ($yongtu) {//用途
                    $condition .= " and a.yongtu = '$yongtu'";
                }
                if ($fengpan) {//无效房源
                    $condition .= " and a.zhuangtai in (".$options_arr.")";
                } else {
                    $condition .= " and a.zhuangtai not in (".$options_arr.")";
                }
                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 ($myfy) {//我的房源
                    $user_id = session('uid');
                    $condition .= " and a.weihurenid = '$user_id'";
                }
                if ($mysc) {//我的收藏
                    $sc = M('shoucang')->where(array('user_id'=>session('uid')))->select();
                    if ($sc) {
                        foreach ($sc as $key => $value) {
                            $cs_arr[] = $value['fyid'];
                        }
                        $in_sc = implode(',', $cs_arr);
                    } else {
                        $in_sc = 0;
                    }
                    $condition .= " and a.id in ($in_sc)";
                }
                if ($jrll) {//今日浏览
                    $today = "sj".date("m-d");
                    if (!cookie($today)) {
                        $history = 0;
                    } else {
                        $history = cookie($today);
                    }
                    $condition .= " and a.id in ($history)";
                }
                if ($tupian) {//图片
                    $condition .= " and a.tupian = 1";
                }
                if ($shipin) {//视频
                    $condition .= " and a.shipin_url <> ''";
                }
                if ($quanjing) {//全景
                    $condition .= " and a.vr_url <> ''";
                }
                if ($xinshang) {//新上
                    $xshang = time()-(86400*3);
                    $condition .= " and a.lurusj > '$xshang'";
                }
                if ($heyan) {//核验
                    $condition .= " and a.heyan = 1";
                }
                if ($jishou) {//急售
                    $condition .= " and a.jishou = 1";
                }
                if ($xuequ) {//学区
                    $condition .= " and c.id > 0";
                }
                if ($wushui) {//无税
                    $condition .= " and a.wushui = 1";
                }
                if ($xinzheng) {//新证
                    $condition .= " and a.xinzheng = 1";
                }
                if ($youdaikuan) {//有贷款
                    $condition .= " and a.youdaikuan = 1";
                }
                if ($waiwangtb) {//外网同步
                    $condition .= " and a.waiwangtb = 1";
                }
                if ($yaoshi) {//是否有钥匙
                    $condition .= " and (a.shouyaobm <> '' or a.shouyaoren <> '')";
                }
                if ($shijian) {//录入时间区间
                    $time_s = explode(' - ',$shijian);
                    $time_s1 = strtotime($time_s[0]);
                    $time_s2 = strtotime($time_s[1])+86400;
                    $condition .= " and a.lurusj>=".$time_s1." and a.lurusj<".$time_s2;
                }
                if ($zuodong) {//座栋
                    $condition .= " and a.zuodong = '$zuodong'";
                }
                $louceng3 = $louceng1;
                $louceng4 = $louceng2;
                if ($louceng1 && $louceng2 && ($louceng1 > $louceng2)) {
                    $louceng1 = $louceng4;
                    $louceng2 = $louceng3;
                }
                if ($louceng1 != "" && $louceng2 != "") {//楼层区间
                    $condition .= " and a.louceng >= '$louceng1' and a.louceng <= '$louceng2'";
                } else {
                    if ($louceng1) {
                        $condition .= " and a.louceng >= '$louceng1'";
                    } else {
                        if ($louceng2) {
                            $condition .= " and a.louceng <= '$louceng2'";
                        }
                    }
                }
                $huxing3 = $huxing1;
                $huxing4 = $huxing2;
                if ($huxing1 && $huxing2 && ($huxing1 > $huxing2)) {
                    $huxing1 = $huxing4;
                    $huxing2 = $huxing3;
                }
                if ($huxing1 != "" && $huxing2 != "") {//户型区间
                    $condition .= " and a.shi >= '$huxing1' and a.shi <= '$huxing2'";
                } else {
                    if ($huxing1) {
                        $condition .= " and a.shi >= '$huxing1'";
                    } else {
                        if ($huxing2) {
                            $condition .= " and a.shi <= '$huxing2'";
                        }
                    }
                }
                $jiage3 = $jiage1;
                $jiage4 = $jiage2;
                if ($jiage1 && $jiage2 && ($jiage1 > $jiage2)) {
                    $jiage1 = $jiage4;
                    $jiage2 = $jiage3;
                }
                switch ($leixing) {
                    case 1:
                        if ($jiage1 != "" && $jiage2 != "") {//售价区间
                            $condition .= " and a.shoujia >= '$jiage1' and a.shoujia <= '$jiage2'";
                        } else {
                            if ($jiage1) {
                                $condition .= " and a.shoujia >= '$jiage1'";
                            } else {
                                if ($jiage2) {
                                    $condition .= " and a.shoujia <= '$jiage2'";
                                }
                            }
                        }
                        break;
                    case 2:
                        if ($jiage1 != "" && $jiage2 != "") {//租价区间
                            $condition .= " and a.zujia >= '$jiage1' and a.zujia <= '$jiage2'";
                        } else {
                            if ($jiage1) {
                                $condition .= " and a.zujia >= '$jiage1'";
                            } else {
                                if ($jiage2) {
                                    $condition .= " and a.zujia <= '$jiage2'";
                                }
                            }
                        }
                        break;
                    default:
                        if ($jiage1 != "" && $jiage2 != "") {//价格区间
                            $condition .= " and ((a.shoujia >= '$jiage1' and a.shoujia <= '$jiage2') or (a.zujia >= '$jiage1' and a.zujia <= '$jiage2'))";
                        } else {
                            if ($jiage1) {
                                $condition .= " and (a.shoujia >= '$jiage1' or a.zujia >= '$jiage1')";
                            } else {
                                if ($jiage2) {
                                    $condition .= " and (a.shoujia <= '$jiage2' or a.zujia <= '$jiage2')";
                                }
                            }
                        }
                        break;
                }
                $mianji3 = $mianji1;
                $mianji4 = $mianji2;
                if ($mianji1 && $mianji2 && ($mianji1 > $mianji2)) {
                    $mianji1 = $mianji4;
                    $mianji2 = $mianji3;
                }
                if ($mianji1 != "" && $mianji2 != "") {//建筑面积区间
                    $condition .= " and a.mianji >= '$mianji1' and a.mianji <= '$mianji2'";
                } elseif ($mianji1) {
                    $condition .= " and a.mianji >= '$mianji1'";
                } elseif ($mianji2) {
                    $condition .= " and a.mianji <= '$mianji2'";
                } else {
                    if ($mianji1) {
                        $condition .= " and a.mianji >= '$mianji1'";
                    } else {
                        if ($mianji2) {
                            $condition .= " and a.mianji <= '$mianji2'";
                        }
                    }
                }
                if ($sousuo) {//搜索
                    $condition .= " and (a.fybiaoti like '%$sousuo%' or a.yezhu like '%$sousuo%' or a.bianhao like '%$sousuo%' or b.xiaoqum like '%$sousuo%' or b.xiaoqubm like '%$sousuo%' or b.pinyinjs like '%$sousuo%' or c.xuexiaom like '%$sousuo%' or c.pinyinjs 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) {
                    $fypx = "a.".$field." ".$order;
                } else {
                    $fypx = "a.id DESC";
                }
                $count = M('fangyuan')->alias('a')->join('jjrxt_yonghu e on a.weihurenid=e.id')->join('jjrxt_xiaoqu b on a.xiaoqu=b.id','LEFT')->join('jjrxt_xuequ c on b.ssxuexiao=c.id','LEFT')->where($condition)->count();
                $Page = I('p');
                $data = M('fangyuan')->alias('a')->join('jjrxt_yonghu e on a.weihurenid=e.id')->join('jjrxt_xiaoqu b on a.xiaoqu=b.id','LEFT')->join('jjrxt_xuequ c on b.ssxuexiao=c.id','LEFT')->field('a.*,b.xiaoqum as xqm,b.jianzaond as jznd,c.id as fyxqid,e.ygmingcheng,e.dianhua')->where($condition)->order($fypx)->page($Page,$limit)->select();
                foreach ($data as $key => $value) {
                    $data[$key]['yezhugx'] = "";
                    $data[$key]['yezhugx2'] = "";
                    $data[$key]['yezhugx3'] = "";
                    $data[$key]['yezhudianhua'] = "";
                    $data[$key]['yezhudianhua2'] = "";
                    $data[$key]['yezhudianhua3'] = "";
                    $data[$key]['zuodong'] = "";
                    $data[$key]['danyuan'] = "";
                    $data[$key]['fanghao'] = "";
                    $isshikan = M('shikan')->where(array('fybh'=>$value['bianhao']))->find();
                    $dqshu=M('xianzhi')->where(array('fyid'=>$value['id']))->count();
                    if ($isshikan) {
                        $data[$key]['isshikan'] = 1;
                    }
                    if ($value['shoujia'] > 0) {
                        $fyjg=floatval($value['shoujia'])."万元";
                    } else if ($value['zujia']) {
                        $fyjg=floatval($value['zujia']).get_peizhi('zujialx',$value['zujialx'],3);
                    }
                    $fyhx="";
                    if ($value['shi']) {
                        $fyhx.=$value['shi']."室";
                    }
                    if ($value['ting']) {
                        $fyhx.=$value['ting']."厅";
                    }
                    if ($value['wei']) {
                        $fyhx.=$value['wei']."卫";
                    }
                    if ($value['chu']) {
                        $fyhx.=$value['chu']."厨";
                    }
                    if ($value['yangtai']) {
                        $fyhx.=$value['yangtai']."阳台";
                    }
                    $fybh = explode("-", $value['bianhao']);
                    $data[$key]['fybh'] = $value['bianhao'];
                    $data[$key]['bianhao'] = $fybh[1];
                    $data[$key]['dqshu'] = $dqshu;
                    $data[$key]['jznd'] = $value['jznd'];
                    $data[$key]['img'] = get_s_img($value['bianhao']);
                    $data[$key]['mianji'] = floatval($value['mianji']);
                    $data[$key]['fyjg'] = $fyjg;
                    $data[$key]['danjia'] = $value['danjia'] ? floatval($value['danjia']) : 0;
                    $data[$key]['fyhx'] = $fyhx ? $fyhx : "——";
                    $data[$key]['fylc'] = $value['louceng']."/".$value['zlouceng'];
                    $data[$key]['fyyt'] = get_peizhi('yongtu',$value['yongtu'],3);
                    $data[$key]['fycx'] = get_peizhi('chaoxiang',$value['chaoxiang'],3);
                    $data[$key]['fyzt'] = get_peizhi('zhuangtai',$value['zhuangtai'],3);
                    $data[$key]['fyzx'] = get_peizhi('zhuangxiu',$value['zhuangxiu'],3);
                    $data[$key]['fydj'] = $value['fydengji'] ? get_peizhi('fydengji',$value['fydengji'],3) : "——";
                    $data[$key]['lurusj'] = $value['lurusj'] ? date('Y-m-d H:i:s',$value['lurusj']) : "——";
                    $data[$key]['whr'] = $value['ygmingcheng'];
                    $data[$key]['whrdh'] = $value['dianhua'];
                    $is_rbacxq = _rbac_btn_show(46,$value['zhuangtai'],$value['weihurenid']);
                    if ($value['isdel'] == 1) {
                        $is_rbachy = _rbac_btn_show(766,$value['zhuangtai'],$value['weihurenid']);
                        $is_rbacsc = _rbac_btn_show(767,$value['zhuangtai'],$value['weihurenid']);
                        $data[$key]['rbachy'] = $is_rbachy ? 1 : 0;
                        $data[$key]['rbacsc'] = $is_rbacsc ? 1 : 0;
                    } else if ($value['isdel'] == 2) {
                        $is_rbacdy = _rbac_btn_show(214,$value['zhuangtai'],$value['weihurenid']);
                        $data[$key]['rbacdy'] = $is_rbacdy ? 1 : 0;
                    }
                    $data[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
                    $photo[$key]=M('photo')->where(array('fybh'=>$value['bianhao'],'image'=>array('neq',$data[$key]['img'])))->field('image')->select();
                    $data[$key]['photo'] = $photo[$key];
                }
                $return_data = array(
                    'code'=>0,
                    'count'=>$count,
                    'data'=>$data
                );
                $this->ajaxReturn($return_data);
            } else {
                $this->zhuangtai = get_peizhi('zhuangtai',0,1);
                $this->chaoxiang = get_peizhi('chaoxiang',0,1);
                $this->zhuangxiu = get_peizhi('zhuangxiu',0,1);
                $this->yongtu = get_peizhi('yongtu',0,1);
                $this->xingzhengqu = M('xingzhengqu')->where(array('gongsiid'=>session('gongsiid')))->select();
                $this->display();// 输出模板
            }
        }
        //添加二手房源
        public function addEsfy(){
            $is_rbac = _rbac_node_show(42);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (session('gongsiid') == "") {
                $this->error('请重新登录！');
            }
            if (IS_POST) {
                if (I('xiaoqu')) {
                    $xiaoqu = I('xiaoqu',0,'int');
                    $xiaoqum = zym($xiaoqu,3);
                } else {
                    $this->error('请选择小区！');
                }
                $xingzhengqu = M('xiaoqu')->where(array('id'=>$xiaoqu))->getField('ssxzq');
                $pianqu = M('xiaoqu')->where(array('id'=>$xiaoqu))->getField('sspianqu');
                $isxq = M('xiaoqu')->where(array('id'=>$xiaoqu))->getField('ssxuexiao');
                if ($isxq) {
                    $xuequ = 1;
                } else {
                    $xuequ = 0;
                }
                if (I('yongtu')) {
                    $yongtu = I('yongtu',0,'int');
                } else {
                    $this->error('请选择用途！');
                }
                if (I('zhuangtai')) {
                    $zhuangtai = I('zhuangtai',0,'int');
                } else {
                    $this->error('请选择状态！');
                }
                $leixing = I('leixing',0,'int');
                switch ($leixing) {
                    case 1:
                        $chushoudj = I('chushoudj',0,'float');
                        if (!preg_match("/^([1-9][0-9]{0,4})(\.[0-9]{1,6})?$|^([0]\.[1-9][0-9]{0,5})$|^([0]\.[0][0-9]{0,4}[1-9]{1})$|^([0]\.[0]{2}[0-9]{0,3}[1-9]{1})$|^([0]\.[0]{3}[0-9]{0,2}[1-9]{1})$|^([0]\.[0]{4}[0-9]{0,1}[1-9]{1})$|^([0]\.[0]{5}[1-9]{1})$|^(1[0]{5})(\.[0][0]{0,5})?$/",$chushoudj)) {
                            $this->error('出售底价请输入＞0且≤十亿的数字（限六位小数）！');
                        }
                        $shoujia = I('shoujia',0,'float');
                        if (!preg_match("/^([1-9][0-9]{0,4})(\.[0-9]{1,6})?$|^([0]\.[1-9][0-9]{0,5})$|^([0]\.[0][0-9]{0,4}[1-9]{1})$|^([0]\.[0]{2}[0-9]{0,3}[1-9]{1})$|^([0]\.[0]{3}[0-9]{0,2}[1-9]{1})$|^([0]\.[0]{4}[0-9]{0,1}[1-9]{1})$|^([0]\.[0]{5}[1-9]{1})$|^(1[0]{5})(\.[0][0]{0,5})?$/",$shoujia)) {
                            $this->error('售价请输入＞0且≤十亿的数字（限六位小数）！');
                        }
                        break;
                    case 2:
                        $chuzudj = I('chuzudj',0,'float');
                        if (!preg_match("/^([1-9][0-9]{0,4})(\.[0-9]{1,2})?$|^([0]\.[1-9][0-9]{0,1})$|^([0]\.[0][1-9]{1})$|^(1[0]{5})(\.[0][0]{0,1})?$/",$chuzudj)) {
                            $this->error('出租底价请输入＞0且≤十万的数字（限两位小数）！');
                        }
                        $zujia = I('zujia',0,'float');
                        if (!preg_match("/^([1-9][0-9]{0,4})(\.[0-9]{1,2})?$|^([0]\.[1-9][0-9]{0,1})$|^([0]\.[0][1-9]{1})$|^(1[0]{5})(\.[0][0]{0,1})?$/",$zujia)) {
                            $this->error('租价请输入＞0且≤十万的数字（限两位小数）！');
                        }
                        $zujialx = I('zujialx',0,'int');
                        break;
                    default:
                        $this->error('类型异常！');
                        break;
                }
                if (I('zuodong')) {
                    $zuodong = I('zuodong');
                } else {
                    $this->error('请输入座栋！');
                }
                if (!preg_match("/^[a-zA-Z1-9][0-9]{0,6}$/",$zuodong)) {
                    $this->error('座栋首字符可以是非零整数或字母，除首字符外必须为非负整数（限六位整数）！');
                }
                if (I('danyuan')) {
                    $danyuan = I('danyuan');
                } else {
                    $this->error('请输入单元！');
                }
                if (!preg_match("/^[a-zA-Z1-9][0-9]{0,6}$/",$danyuan)) {
                    $this->error('单元首字符可以是非零整数或字母，除首字符外必须为非负整数（限六位整数）！');
                }
                if (I('fanghao')) {
                    $fanghao = I('fanghao');
                } else {
                    $this->error('请输入房号！');
                }
                if (!preg_match("/^[a-zA-Z1-9][0-9]{0,6}$/",$fanghao)) {
                    $this->error('房号首字符可以是非零整数或字母，除首字符外必须为非负整数（限六位整数）！');
                }
                $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['leixing'] = $leixing;
                $cffy = M('fangyuan')->where($condition)->find();
                if ($cffy) {
                    $this->error('房源已存在！');
                }
                if (I('shi')) {
                    $shi = I('shi',0,'int');
                } else {
                    $this->error('请输入卧室数！');
                }
                if (!preg_match("/^([1-9]{1})$|^(1[0]{1})$/",$shi)) {
                    $this->error('户型-室请输入＞0≤10的整数！');
                }
                if (I('ting')) {
                    $ting = I('ting',0,'int');
                } else {
                    $this->error('请输入客厅数！');
                }
                if (!preg_match("/^([1-9]{1})$|^(1[0]{1})$/",$ting)) {
                    $this->error('户型-厅请输入＞0≤10的整数！');
                }
                if (I('wei')) {
                    $wei = I('wei',0,'int');
                }
                if (!preg_match("/^$|^([0]{1})$|^([1-9]{1})$|^(1[0]{1})$/",$wei)) {
                    $this->error('户型-卫请输入＞0≤10的整数！');
                }
                if (I('chu')) {
                    $chu = I('chu',0,'int');
                }
                if (!preg_match("/^$|^([0]{1})$|^([1-9]{1})$|^(1[0]{1})$/",$chu)) {
                    $this->error('户型-厨请输入＞0≤10的整数！');
                }
                if (I('yangtai')) {
                    $yangtai = I('yangtai',0,'int');
                }
                if (!preg_match("/^$|^([0]{1})$|^([1-9]{1})$|^(1[0]{1})$/",$yangtai)) {
                    $this->error('户型-阳台请输入＞0≤10的整数！');
                }
                if (I('mianji')) {
                    $mianji = I('mianji',0,'float');
                } else {
                    $this->error('请输入建筑面积！');
                }
                if (!preg_match("/^([1-9][0-9]{0,3})(\.[0-9]{1,2})?$|^([0]\.[1-9][0-9]{0,1})$|^([0]\.[0][1-9]{1})$|^(1[0]{4})(\.[0][0]{0,1})?$/",$mianji)) {
                    $this->error('建筑面积请输入＞0且≤一万的数字（限两位小数）！');
                }
                if ($leixing == 1) {
                    if ($shoujia && $mianji) {
                        $num1 = $shoujia*10000;
                        $num2 = $mianji;
                        $num = $num1/$num2;
                        $danjia = number_format($num,2,".","");
                    }
                }
                if (I('symianji')) {
                    $symianji = I('symianji',0,'float');
                }
                if (!preg_match("/^$|^([0]{1})$|^([1-9][0-9]{0,3})(\.[0-9]{1,2})?$|^([0]\.[1-9][0-9]{0,1})$|^([0]\.[0][1-9]{1})$|^(1[0]{4})(\.[0][0]{0,1})?$/",$symianji)) {
                    $this->error('使用面积请输入＞0且≤一万的数字（限两位小数）！');
                }
                if (I('laiyuan')) {
                    $laiyuan = I('laiyuan',0,'int');
                } else {
                    $this->error('请选择来源！');
                }
                if (I('zhuangxiu')) {
                    $zhuangxiu = I('zhuangxiu',0,'int');
                } else {
                    $this->error('请选择装修！');
                }
                if (I('louceng')) {
                    $louceng = I('louceng');
                } else {
                    $this->error('请输入楼层！');
                }
                if (!preg_match("/^[a-zA-Z1-9][0-9]{0,6}$/",$louceng)) {
                    $this->error('楼层首字符可以是非零整数或字母，除首字符外必须为非负整数（限六位整数）！');
                }
                if (I('zlouceng')) {
                    $zlouceng = I('zlouceng',0,'int');
                } else {
                    $this->error('请输入总楼层！');
                }
                if (!preg_match("/^([1-9]{1})$|^([1-9][0-9]{1})$|^(1[0-9]{2})$|^(2[0]{2})$/",$zlouceng)) {
                    $this->error('总楼层请输入＞0≤200的整数！');
                }
                if (I('yezhu')) {
                    $yezhu = I('yezhu');
                } else {
                    $this->error('请输入业主姓名！');
                }
                if (I('yezhulx')) {
                    $yezhulx = I('yezhulx',0,'int');
                } else {
                    $this->error('请选择业主类型！');
                }
                if (I('yezhudianhua')) {
                    $yezhudianhua = I('yezhudianhua');
                } else {
                    $this->error('请输入业主电话！');
                }
                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}$/",$yezhudianhua)) {
                    $this->error('业主电话请输入正确的手机号码或电话号码！');
                }
                if (I('yezhugx')) {
                    $yezhugx = I('yezhugx',0,'int');
                } else {
                    $this->error('请选择业主电话关系！');
                }
                if (I('chaoxiang')) {
                    $chaoxiang = I('chaoxiang',0,'int');
                } else {
                    $this->error('请选择朝向！');
                }
                if (I('xianzhuang')) {
                    $xianzhuang = I('xianzhuang',0,'int');
                } else {
                    $this->error('请选择现状！');
                }
                if (I('fwleixing')){
                    $fwleixing = I('fwleixing',0,'int');
                } else {
                    $this->error('请选择房屋类型！');
                }
                $jiegou = I('jiegou',0,'int');
                if ($leixing == 1) {
                    if (I('chanquanzheng')) {
                        $chanquanzheng = I('chanquanzheng',0,'int');
                    } else {
                        $this->error('请选择产权证！');
                    }
                    if (I('kanfangfs')) {
                        $kanfangfs = I('kanfangfs',0,'int');
                    } else {
                        $this->error('请选择看房方式！');
                    }
                    if (I('budongchanzheng')) {
                        $budongchanzheng = I('budongchanzheng',0,'int');
                    } else {
                        $this->error('请选择不动产证！');
                    }
                    $tudizheng = I('tudizheng',0,'int');
                }
                $pt1 = array_keys(I('peitao'));
                $pt2 = implode(',',$pt1);
                $fukuanfs = I('fukuanfs',0,'int');
                $fydengji = I('fydengji',0,'int');
                if (I('wushui')) {
                    $wushui = 1;
                    $geshui = 0;
                    $zengshui = 0;
                } else {
                    $wushui = 0;
                    if (I('geshui')) {
                        $geshui = 1;
                    } else {
                        $geshui = 0;
                    }
                    if (I('zengshui')) {
                        $zengshui = 1;
                    } else {
                        $zengshui = 0;
                    }
                }
                if (I('waiwangtb')) {
                    $waiwangtb = 1;
                } else {
                    $waiwangtb = 0;
                }
                $ti = I('ti',0,'int');
                if (!preg_match("/^$|^([0]{1})$|^([1-9][0-9]{0,1})$|^(1[0]{2})$/",$ti)) {
                    $this->error('梯户请输入＞0≤100的整数！');
                }
                $hu = I('hu',0,'int');
                if (!preg_match("/^$|^([0]{1})$|^([1-9][0-9]{0,1})$|^(1[0]{2})$/",$hu)) {
                    $this->error('梯户请输入＞0≤100的整数！');
                }
                $niandai = I('niandai',0,'int');
                if ($leixing == 1) {
                    $czriqi = strtotime(I('czriqi'));
                    if (I('man5')) {
                        $man5 = 1;
                    } else {
                        $man5 = 0;
                    }
                    if (I('man2')) {
                        $man2 = 1;
                    } else {
                        $man2 = 0;
                    }
                    if (I('weiyi')) {
                        $weiyi = 1;
                    } else {
                        $weiyi = 0;
                    }
                    if (I('jishou')) {
                        $jishou = 1;
                    } else {
                        $jishou = 0;
                    }
                    $cqxingzhi = I('cqxingzhi',0,'int');
                    $cqnianxian = I('cqnianxian',0,'int');
                    if (!preg_match("/^$|^([0]{1})$|^([1-9][0-9]{0,2})$|^(1[0]{3})$/",$cqnianxian)) {
                        $this->error('产权年限请输入＞0≤1000的整数！');
                    }
                    if (I('heyan')) {
                        $heyan = 1;
                    } else {
                        $heyan = 0;
                    }
                    if (I('quankuan')) {
                        $quankuan = 1;
                    } else {
                        $quankuan = 0;
                    }
                    if (I('youdaikuan')) {
                        $youdaikuan = 1;
                    } else {
                        $youdaikuan = 0;
                    }
                    if (I('xinzheng')) {
                        $xinzheng = 1;
                    } else {
                        $xinzheng = 0;
                    }
                }
                $beizhu = preg_replace("/\s(?=\s)/","\\1",I('beizhu'));
                if (!preg_match("/^$|^[\s\S]{0,300}$/",$beizhu)) {
                    $this->error('备注只能输入300个字符！');
                }
                $bh = M('bianhao')->where(array('id'=>1))->setInc('bianhao');
                $bh1 = M('bianhao')->where(array('id'=>1))->getField('bianhao');
                $bm = M('yonghu')->where(array('id'=>session('uid')))->getField('bumen');
                $zhan_bumen = M('xingzhengqu')->where(array('id'=>$xingzhengqu))->getField('bumen');
                $data['gongsiid'] = session('gongsiid');
                $data['bumen'] = $bm;
                $data['zhan_bumen'] = $zhan_bumen;
                $data['leixing'] = $leixing;
                $data['yongtu'] = $yongtu;
                $data['zhuangtai'] = $zhuangtai;
                $data['xiaoqu'] = $xiaoqu;
                $data['xiaoqum'] = $xiaoqum;
                $data['xuequ'] = $xuequ;
                $data['pianqu'] = $pianqu;
                $data['xingzhengqu'] = $xingzhengqu;
                switch ($leixing) {
                    case 1:
                        $data['bianhao'] = "M-".$bh1;
                        $data['shoujia'] = $shoujia;
                        $data['chushoudj'] = $chushoudj;
                        $data['danjia'] = $danjia;
                        break;
                    case 2:
                        $data['bianhao'] = "Z-".$bh1;
                        $data['zujia'] = $zujia;
                        $data['chuzudj'] = $chuzudj;
                        $data['zujialx'] = $zujialx;
                        break;
                }
                $data['zuodong'] = $zuodong;
                $data['danyuan'] = $danyuan;
                $data['fanghao'] = $fanghao;
                $data['shi'] = $shi;
                $data['ting'] = $ting;
                $data['wei'] = $wei;
                $data['chu'] = $chu;
                $data['yangtai'] = $yangtai;
                $data['mianji'] = $mianji;
                $data['symianji'] = $symianji;
                $data['laiyuan'] = $laiyuan;
                $data['zhuangxiu'] = $zhuangxiu;
                $data['louceng'] = $louceng;
                $data['zlouceng'] = $zlouceng;
                $data['yezhu'] = $yezhu;
                $data['yezhulx'] = $yezhulx;
                $data['yezhudianhua'] = $yezhudianhua;
                $data['yezhugx'] = $yezhugx;
                $data['chaoxiang'] = $chaoxiang;
                $data['xianzhuang'] = $xianzhuang;
                $data['fwleixing'] = $fwleixing;
                $data['jiegou'] = $jiegou;
                if ($leixing == 1) {
                    $data['chanquanzheng'] = $chanquanzheng;
                    $data['kanfangfs'] = $kanfangfs;
                    $data['budongchanzheng'] = $budongchanzheng;
                    $data['tudizheng'] = $tudizheng;
                }
                $data['peitao'] = $pt2;
                $data['fukuanfs'] = $fukuanfs;
                $data['fydengji'] = $fydengji;
                $data['wushui'] = $wushui;
                $data['geshui'] = $geshui;
                $data['zengshui'] = $zengshui;
                $data['fybiaoti'] = I('fybiaoti');
                $data['waiwangtb'] = $waiwangtb;
                $data['ti'] = $ti;
                $data['hu'] = $hu;
                $data['niandai'] = $niandai;
                if ($leixing == 1) {
                    $data['czriqi'] = $czriqi;
                    $data['man5'] = $man5;
                    $data['man2'] = $man2;
                    $data['weiyi'] = $weiyi;
                    $data['jishou'] = $jishou;
                    $data['cqxingzhi'] = $cqxingzhi;
                    $data['cqnianxian'] = $cqnianxian;
                    $data['heyan'] = $heyan;
                    $data['quankuan'] = $quankuan;
                    $data['youdaikuan'] = $youdaikuan;
                    $data['xinzheng'] = $xinzheng;
                }
                $data['beizhu'] = $beizhu;
                $data['lururenid'] = session('uid');
                $data['weihurenid'] = session('uid');
                $data['weihuren'] = session('ygmingcheng');
                $data['lurusj'] = time();
                $data['zhgjsj'] = time();
                $data['xiugaisj'] = time();
                $isok = M('Fangyuan')->add($data);
                if ($isok) {
                    $data_rizhi['zy_type'] = 1;
                    $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 ($leixing) {
                        case 1:
                            $neirong = "新增出售房源";
                            break;
                        case 2:
                            $neirong = "新增出租房源";
                            break;
                    }
                    $data_rizhi['neirong'] = $neirong;
                    M('rizhi')->add($data_rizhi);
                    $this->success('添加成功！');
                } else {
                    $this->error('添加失败！');
                }
            } else {
                $jylx = I('jylx',0,'int');
                switch ($jylx) {
                    case 1:
                        $fukuan = "fukuan";
                        $this->kanfangfs = get_peizhi('kanfangfs',0,1);
                        $this->iszheng = get_peizhi('iszheng',0,1);
                        $this->chanquan = get_peizhi('chanquan',0,1);
                        break;
                    case 2:
                        $fukuan = "zlfukuan";
                        $this->zujialx = get_peizhi('zujialx',0,1);
                        break;
                    default:
                        $this->error('类型异常！');
                        break;
                }
                $this->yongtu = get_peizhi('yongtu',0,1);
                $this->zhuangtai = get_peizhi('zhuangtai',0,1);
                $this->laiyuan = get_peizhi('laiyuan',0,1);
                $this->zhuangxiu = get_peizhi('zhuangxiu',0,1);
                $this->yezhulx = get_peizhi('yezhulx',0,1);
                $this->yezhugx = get_peizhi('yezhugx',0,1);
                $this->chaoxiang = get_peizhi('chaoxiang',0,1);
                $this->xianzhuang = get_peizhi('xianzhuang',0,1);
                $this->fwleixing = get_peizhi('fwleixing',0,1);
                $this->jiegou = get_peizhi('jiegou',0,1);
                $this->peitao = get_peizhi('peitao',0,1);
                $this->fukuan = get_peizhi($fukuan,0,1);
                $this->fydengji = get_peizhi('fydengji',0,1);
                $this->display();
            }
        }
        //内容页
        public function neirong(){
            $id=I('id');
            $fybh = I('bianhao');
            if ($id) {
                $fangyuan=M('fangyuan')->where(array('id'=>$id))->find();
            } elseif($fybh) {
                $fangyuan=M('fangyuan')->where(array('bianhao'=>$fybh))->find();
            }
            if (!$fangyuan) {
                $this->error('异常错误!');
            }
            $is_rbacfck = _rbac_node_show(863);
            if (!$is_rbacfck && ($fangyuan['fpck'] != 2)) {
                $this->redirect('Index/error');
            }
            $is_rbac = _rbac_btn_show(46,$fangyuan['zhuangtai'],$fangyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $is_rbacfdh = _rbac_node_show(859);
            if ($is_rbacfdh || ($fangyuan['fpdh'] == 2)) {
                $this->is_rbacfdh=1;
            }
            $is_rbacfdz = _rbac_node_show(860);
            if ($is_rbacfdz || ($fangyuan['fpdz'] == 2)) {
                $this->is_rbacfdz=1;
            }
            $is_rbacfdj = _rbac_node_show(861);
            if ($is_rbacfdj || ($fangyuan['fpdj'] == 2)) {
                $this->is_rbacfdj=1;
            }
            $is_rbacfdq = _rbac_node_show(862);
            if ($is_rbacfdq || ($fangyuan['fpdq'] == 2)) {
                $this->is_rbacfdq=1;
            }
            $this->fangyuan=$fangyuan;
            $this->fydengji=M('Peizhi')->where(array('pzming'=>'fydengji','lxid'=>$fangyuan['fydengji']))->getField('lxming');
            $this->chanquanzheng=M('Peizhi')->where(array('pzming'=>'iszheng','lxid'=>$fangyuan['chanquanzheng']))->getField('lxming');
            $this->budongchanzheng=M('Peizhi')->where(array('pzming'=>'iszheng','lxid'=>$fangyuan['budongchanzheng']))->getField('lxming');
            $this->tudizheng=M('Peizhi')->where(array('pzming'=>'iszheng','lxid'=>$fangyuan['tudizheng']))->getField('lxming');
            $this->leixing=M('Peizhi')->where(array('pzming'=>'leixing','lxid'=>$fangyuan['leixing']))->getField('lxming');
            $this->yongtu=M('Peizhi')->where(array('pzming'=>'yongtu','lxid'=>$fangyuan['yongtu']))->getField('lxming');
            $this->zhuangtai=M('Peizhi')->where(array('pzming'=>'zhuangtai','lxid'=>$fangyuan['zhuangtai']))->getField('lxming');
            $this->zujialx=M('Peizhi')->where(array('pzming'=>'zujialx','lxid'=>$fangyuan['zujialx']))->getField('lxming');
            $this->chaoxiang=M('Peizhi')->where(array('pzming'=>'chaoxiang','lxid'=>$fangyuan['chaoxiang']))->getField('lxming');
            $this->zhuangxiu=M('Peizhi')->where(array('pzming'=>'zhuangxiu','lxid'=>$fangyuan['zhuangxiu']))->getField('lxming');
            $this->xianzhuang=M('Peizhi')->where(array('pzming'=>'xianzhuang','lxid'=>$fangyuan['xianzhuang']))->getField('lxming');
            $this->fwleixing=M('Peizhi')->where(array('pzming'=>'fwleixing','lxid'=>$fangyuan['fwleixing']))->getField('lxming');
            $this->jiegou=M('Peizhi')->where(array('pzming'=>'jiegou','lxid'=>$fangyuan['jiegou']))->getField('lxming');
            $this->cqxingzhi=M('Peizhi')->where(array('pzming'=>'chanquan','lxid'=>$fangyuan['cqxingzhi']))->getField('lxming');
            $this->laiyuan=M('Peizhi')->where(array('pzming'=>'laiyuan','lxid'=>$fangyuan['laiyuan']))->getField('lxming');
            $this->kanfangfs=M('Peizhi')->where(array('pzming'=>'kanfangfs','lxid'=>$fangyuan['kanfangfs']))->getField('lxming');
            $this->genjinfs=M('Peizhi')->where(array('pzming'=>'genjinfs'))->select();
            $this->jcpz=M('jcpz')->where(array('gongsiid'=>session('gongsiid')))->field('dtmiyao,dtzuobiao')->find();
            $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->isshoucang=M('shoucang')->where('fyid='.$fangyuan['id'].' and user_id='.session("uid"))->find();
            $peitao1=$fangyuan['peitao'];
            if ($peitao1) {
                $condition="pzming = 'peitao'";
                $condition.=" and lxid in ($peitao1)";
                $this->peitao=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
            }
            //房源上图人
            $bianhao=$fangyuan['bianhao'];
            $uid=M('photo')->where(array('gongsiid'=>session('gongsiid'),'fybh'=>$bianhao))->group('fybh')->getField('uid');
            $shangturen=M('yonghu')->where(array('id'=>$uid))->find();
            $this->assign('shangturen',$shangturen);
            //存钥部门
            $shouyaobmid=$fangyuan['shouyaobm'];
            $this->shouyaobm=M('bumen')->where(array('id'=>$shouyaobmid))->find();
            //收钥人
            $shouyaorenid=$fangyuan['shouyaoren'];
            $this->shouyaoren=M('yonghu')->where(array('id'=>$shouyaorenid))->find();
            //录入人
            $weihurenid=$fangyuan['weihurenid'];
            $this->weihuren=M('yonghu')->where(array('id'=>$weihurenid))->field('ygmingcheng,dianhua')->find();
            $this->fengpanren=M('yonghu')->where(array('id'=>$fangyuan['fengpanrenid']))->field('ygmingcheng,dianhua')->find();
            $weibanuid=M('weiban')->where(array('fybianhao'=>$fangyuan['bianhao']))->getField('uid');
            $this->weibanren=M('yonghu')->where(array('id'=>$weibanuid))->field('ygmingcheng,dianhua')->find();
            $shikanuid=M('shikan')->where(array('fybh'=>$fangyuan['bianhao']))->getField('uid');
            $this->shikanren=M('yonghu')->where(array('id'=>$shikanuid))->field('ygmingcheng,dianhua')->find();
            $Model = new \Think\Model;
            $this->fygenjin = $Model->table(array('jjrxt_fygenjin'=>'a','jjrxt_yonghu'=>'b','jjrxt_bumen'=>'c'))->field('a.neirong,a.shijian,c.bmming,b.ygmingcheng,a.genjinfs,a.zhuangtai,a.id,a.gjvoice')->where('a.uid=b.id and b.bumen=c.id and fyid='.$fangyuan['id'])->order('a.shijian desc')->select();
            $this->diaoqujl = $Model->table(array('jjrxt_xianzhi'=>'a','jjrxt_yonghu'=>'b','jjrxt_bumen'=>'c'))->field('a.shijian,c.bmming,b.ygmingcheng')->where('a.uid=b.id and b.bumen=c.id and fyid='.$fangyuan['id'])->order('a.shijian 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=1 and zy_id='.$fangyuan['id'])->order('a.id desc')->select();
            $this->xiaoqu=M('xiaoqu')->where(array('id'=>$fangyuan['xiaoqu']))->find();
            $this->xuexiao=M('xuequ')->where(array('id'=>$this->xiaoqu['ssxuexiao']))->getField('xuexiaom');
            $qitiannei = time()-604800;
            $this->daikanshu=M('kydaikan')->where('gongsiid ='.session('gongsiid').' and (fybh = "'.$fangyuan['bianhao'].'") and dkshijian >'.$qitiannei)->count();
            $dianhua=M('yonghu')->where(array('id'=>$fangyuan['weihurenid']))->getField('dianhua');
            $today="sj".date("m-d");
               if (!cookie($today)) {
                cookie($today,$fangyuan['id'],3600*24);
            }else{
                $value = cookie($today);
                $val=$value.','.$fangyuan['id'];
                cookie($today,$val,3600*24);
            }
            //钥匙列表
            $yoashi_sql="select a.*,b.ygmingcheng as ys_uid,c.ygmingcheng as adduid FROM  (select * from __YAOSHI__ where fybianhao='{$bianhao}' ) as a inner join (select * from __YONGHU__ ) as b on a.ys_uid=b.id left join __YONGHU__ as c on a.adduid=c.id  order by id DESC ";
            $this->yaoshi = $Model->query($yoashi_sql);
            $this->yaoshishu=M('yaoshi')->where(array('fybianhao'=>$bianhao))->count();
            //委办列表
            $weiban_sql="select a.*,b.ygmingcheng as uid,c.ygmingcheng as adduid FROM  (select * from __WEIBAN__ where fybianhao='{$bianhao}' ) as a inner join (select * from __YONGHU__ ) as b on a.uid=b.id left join __YONGHU__ as c on a.adduid=c.id  order by id DESC ";
            $this->weiban = $Model->query($weiban_sql);
            $this->display();
        }
        /**
        * webuploader 显示图片上传框
        */
        function scfytp() {
            $fybh=I('fybh');
            $fangyuan=M('fangyuan')->field('zhuangtai,weihurenid')->where(array('bianhao'=>$fybh))->find();
            $is_rbac = _rbac_btn_show(70,$fangyuan['zhuangtai'],$fangyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                // 根据自己的业务调整上传路径、允许的格式、文件大小
                $path = "/Upload/".session('gongsiid')."/".$fybh;
                ajax_upload($path);
            } else {
                $this->display();
            }
        }
        /**
            * [fy_pic_setfengmian 异步房源图片为封面]
            * @return [json] [description]
        */
        public function fy_pic_setfengmian(){
            $id=I('fy_pic_id');
            //根据图片id查找房源图片的房源编号fybh
            $fybh = M('Photo')->where(array('id'=>$id))->getField('fybh');
            $fyzhuangtai = M('fangyuan')->where(array('bianhao'=>$fybh))->getField('zhuangtai');
            $uid = M('photo')->where(array('id'=>$id))->getField('uid');
            $is_rbac = _rbac_btn_show(481,$fyzhuangtai,$uid);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            M('Photo')->where(array('fybh'=>$fybh,'fengmian'=>1))->setField(array('fengmian'=>0));
            $r = M('Photo')->where(array('id'=>$id))->setField(array('fengmian'=>1));
            if ($r) {
                $fyxgsj=M('fangyuan')->where(array('bianhao'=>$fybh))->setField('xiugaisj',time());
                $this->success('设置成功');
            }else{
                $this->error('设置失败');
            }
        }
        /**
            * [fy_pic_del 异步删除图片]
            * @return [json] [description]
        */
        public function fy_pic_del(){
            $id=I('fy_pic_id');
            //根据图片id查看当前是否还有当前房源的图片
            $fybh = M('Photo')->where(array('id'=>$id))->getField('fybh');
            $fyzhuangtai = M('fangyuan')->where(array('bianhao'=>$fybh))->getField('zhuangtai');
            $uid = M('photo')->where(array('id'=>$id))->getField('uid');
            $is_rbac = _rbac_btn_show(50,$fyzhuangtai,$uid);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $piccount = M('Photo')->where(array('fybh'=>$fybh))->count();
            $dp=M('Photo');
            //删除当前图片
            if($id){
                $plist = $dp->where(array('id'=>$id))->find();
                if ($plist){
                    unlink('./Upload/'.$plist['gongsiid'].'/'.$plist['fybh'].'/'.$plist['image']);
                    unlink('./Upload/'.$plist['gongsiid'].'/'.$plist['fybh'].'/t_'.$plist['image']);
                    $dp->where(array('id'=>$id))->delete();
                    $data_rizhi['zy_type']=1;
                    $data_rizhi['uid']=session("uid");
                    $data_rizhi['addtime']=time();
                    $data_rizhi['cz_type']=13;
                    $fyid=M('fangyuan')->where(array('bianhao'=>$fybh))->getField('id');
                    $data_rizhi['zy_id']=$fyid;
                    $data_rizhi['zy_bianhao']=$fybh;
                    $neirong="删除图片";
                    $data_rizhi['neirong']=$neirong;
                    M('rizhi')->add($data_rizhi);
                    $data=array('pics'=>$plist);
                    //删除最后一个房源图片后给fangyun表的tupian字段置 0
                    if ($piccount==1) {
                        M('Fangyuan')->where(array('bianhao'=>$fybh))->setField('tupian',0);
                    }
                    $fyxgsj=M('fangyuan')->where(array('bianhao'=>$fybh))->setField('xiugaisj',time());
                    $this->success('删除成功');
                }else{
                    $this->error('删除失败');
                }
            }
        }
        //批量删除房源图片
        public function fy_pic_dels(){
            $is_rbac = _rbac_node_show(482);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $ids=I('fy_pic_ids');
            $fybh = M('Photo')->where(array('id'=>$ids[0]))->getField('fybh');
            $fyzhuangtai = M('fangyuan')->where(array('bianhao'=>$fybh))->getField('zhuangtai');
            foreach ($ids as $key => $value) {
                $uid = M('photo')->where(array('id'=>$value))->getField('uid');
                $is_rbac = _rbac_btn_show(482,$fyzhuangtai,$uid);
                if (!$is_rbac) {
                    $this->error("您选择了权限范围之外的图片！");
                }
            }
            if(is_array($ids)){   
                $where = 'id in('.implode(',',$ids).')';
                $dp=M('Photo');
                //删除房源图片文件
                foreach ($ids as $value) {
                    $plist = $dp->where(array('id'=>$value))->find();
                    unlink('./Upload/'.$plist['gongsiid'].'/'.$plist['fybh'].'/'.$plist['image']);
                    unlink('./Upload/'.$plist['gongsiid'].'/'.$plist['fybh'].'/t_'.$plist['image']);
                }
                //删除图片记录
                $dp->where($where)->delete();
                //写入删除日志
                $data_rizhi['zy_type']=1;
                $data_rizhi['uid']=session("uid");
                $data_rizhi['addtime']=time();
                $data_rizhi['cz_type']=13;
                $fyid=M('fangyuan')->where(array('bianhao'=>$fybh))->getField('id');
                $data_rizhi['zy_id']=$fyid;
                $data_rizhi['zy_bianhao']=$fybh;
                $neirong="删除房源图片 ".count($ids)." 张";
                $data_rizhi['neirong']=$neirong;
                M('rizhi')->add($data_rizhi);
                //删除最后一个房源图片后给fangyun表的tupian字段置 0
                $nopic = M('Photo')->where(array('fybh'=>$fybh))->count();
                if ($nopic<=0) {
                    M('Fangyuan')->where(array('bianhao'=>$fybh))->setField('tupian',0);
                }
                $fyxgsj=M('fangyuan')->where(array('bianhao'=>$fybh))->setField('xiugaisj',time());
                $this->success('删除成功');
            }else{
                $this->error('删除失败'); 
            }
        }
        //批量下载房源图片
        public function fy_pic_downloads(){
            $is_rbac = _rbac_node_show(483);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $ids=I('fy_pic_ids');
            $fybh = M('Photo')->where(array('id'=>$ids[0]))->getField('fybh');
            $fyzhuangtai = M('fangyuan')->where(array('bianhao'=>$fybh))->getField('zhuangtai');
            foreach ($ids as $key => $value) {
                $uid = M('photo')->where(array('id'=>$value))->getField('uid');
                $is_rbac = _rbac_btn_show(483,$fyzhuangtai,$uid);
                if (!$is_rbac) {
                    $this->error("您选择了权限范围之外的图片！");
                }
            }
            $where = 'id in('.implode(',',$ids).')';
            $dp=M('Photo');
            $plist = $dp->where($where)->field('id,gongsiid,fybh,image')->select();
            $zip = new \ZipArchive;
            if ($plist) {
                //压缩文件名(临时文件)
                $filename = './Upload/'.$plist[0]['gongsiid'].'/'.$plist[0]['fybh'].'/'.md5($plist[0]['fybh']).'.zip';
                $downname = I("server.HTTP_HOST").'/Upload/'.$plist[0]['gongsiid'].'/'.$plist[0]['fybh'].'/'.md5($plist[0]['fybh']).'.zip';
            }
            //新建zip压缩包
            $zip->open($filename,\ZipArchive::OVERWRITE | \ZipArchive::CREATE);
            //把图片一张一张加进去压缩
            foreach ($plist as $value) {
                $zip->addFile('./Upload/'.$value['gongsiid'].'/'.$value['fybh'].'/'.$value['image']);
                $zip->renameName('./Upload/'.$value['gongsiid'].'/'.$value['fybh'].'/'.$value['image'],$value['image']);
            }
            //打包zip
            $zip->close();
            //可以直接重定向下载
            //header('Location:'.$downname);
            $this->success($downname);
        }
        //设置图片类型
        public function settupianleixing(){
            $is_rbac = _rbac_node_show(484);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $ids=explode(",",I('fy_pic_ids'));
                $fybh = M('Photo')->where(array('id'=>$ids[0]))->getField('fybh');
                $fyzhuangtai = M('fangyuan')->where(array('bianhao'=>$fybh))->getField('zhuangtai');
                foreach ($ids as $key => $value) {
                    $uid = M('photo')->where(array('id'=>$value))->getField('uid');
                    $is_rbac = _rbac_btn_show(484,$fyzhuangtai,$uid);
                    if (!$is_rbac) {
                        $this->error("您选择了权限范围之外的图片！");
                    }
                }
                $ids1=I('fy_pic_ids');
                $tpleixing = I('tpleixing');
                if ($tpleixing=="") {
                    $this->error("请选择图片类型");
                }
                $where = 'id in('.$ids1.')';
                $dp=M('Photo');
                $plist = $dp->where($where)->setField(array('leixing' => $tpleixing ));               
                if ($plist) {
                    $fyxgsj=M('fangyuan')->where(array('bianhao'=>$fybh))->setField('xiugaisj',time());
                    $this->success("设置成功");
                }else{
                    $this->error("设置失败");
                }
            } else {
                $this->yongtu = M('Peizhi')->where(array('pzming'=>'yongtu',lxid=>I('fy_yongtu'),'zhuangtai'=>1))->order('xsshunxu asc')->find();
                $pzming='';
                switch (I('fy_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;
                }
                $this->tpleixing = M('Peizhi')->where(array('pzming'=>$pzming,'zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->display();// 输出模板
            }
        }
        // 房源跟进列表
        public function fygenjin(){
            if (I('is_ajax')) {
                /*权限控制开始*/
                $condition = "e.gongsiid=".session('gongsiid');
                $x = _rbac_lists_where(253,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.shijian>=".$time_s1." and a.shijian<".$time_s2;
                }
                if ($sousuo) {
                    $condition.=" and ((e.pinyinjs like '%$sousuo%') or (e.ygmingcheng like '%$sousuo%') or (e.dianhua 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 __FYGENJIN__ a join __YONGHU__ e on a.uid=e.id where {$condition}");
                $Page = new \Think\Page($count['0']['count(*)'],$limit);
                $list = "select a.*,e.ygmingcheng,e.bumen from __FYGENJIN__ a join __YONGHU__ e on a.uid=e.id where {$condition} ".$dlpx." limit ".$Page->firstRow.','.$Page->listRows;
                $data = $Model->query($list);
                foreach ($data as $key => $value) {
                    $bianhao = explode("-", $value['fybh']);
                    $data[$key]['bianhao'] = $value['fybh'];
                    $data[$key]['fybh'] = $bianhao[1];
                    $data[$key]['gjbm'] = zym($value['bumen'],6);
                    $data[$key]['gjfs'] = get_peizhi('genjinfs',$value['genjinfs'],3);
                    $data[$key]['shijian'] = $value['shijian'] ? date('Y-m-d H:i:s',$value['shijian']) : "——";
                    $gjfy = M('fangyuan')->where(array('id'=>$value['id']))->field('zhuangtai,weihurenid')->find();
                    $is_rbacfy = _rbac_btn_show(46,$gjfy['zhuangtai'],$gjfy['weihurenid']);
                    $data[$key]['rbacfy'] = $is_rbacfy ? 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, 253);
                $this->assign("select_categorys", $select_categorys);
                $this->display();
            }
        }
    }