<?php

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

    namespace Admin\Controller;
    use Think\Controller;
    class ZiyuanController extends CommonController {
        // 行政区列表
        public function xingzhengqu(){
            $map .= " where e.gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(372,0);
            if ($x && $x != "all" && $x != "me") {
                $map .= " and e.bumen in ({$x})";
            }elseif ($x == "me") {
                $map .= " and e.id=".session('uid');
            }elseif ($x == false) {
                $map .= " and e.id=0";
            }
            $count=M('xingzhengqu')->query("select count(*) from __XINGZHENGQU__ a join __YONGHU__ e on a.adduid=e.id {$map}");
            $Page=new \Think\Page($count['0']['count(*)'],30);
            $show=$Page->show();
            $list="select a.*,e.ygmingcheng,e.bumen,c.bmming from __XINGZHENGQU__ a join __YONGHU__ e on a.adduid=e.id left join __BUMEN__ c on a.bumen = c.id {$map} order by a.id DESC limit ".$Page->firstRow.','.$Page->listRows;
            $xingzhengqu=M('xingzhengqu')->query($list);
            $this->assign('firstRow',$Page->firstRow);
            $this->assign('xingzhengqu',$xingzhengqu);
            $this->assign('count',$count);
            $this->assign('page',$show);
            $this->display();
        }
        // 添加行政区
        public function addxzq(){
            $is_rbac = _rbac_btn_show(373,0);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $condition['gongsiid']=session('gongsiid');
                $xzqming=I('xzqming');
                $condition['xzqming']=$xzqming;
                $cfxzq=M('xingzhengqu')->where($condition)->find();
                if($cfxzq){
                    $this->error('行政区已存在');
                }
                $data['shijian']=time();
                $data['xzqming']=I('xzqming');
                $data['dituzb']=I('zuobiao');
                $data['adduid']=session('uid');
                $data['gongsiid']=session('gongsiid');
                $isok=M('xingzhengqu')->add($data);
                if ($isok) {
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            } else {
                $this->bumen=M('bumen')->where(array('level'=>2))->select();
                $this->display();
            }
        }
        // 更新行政区
        public function editxzq(){
            $id=I('id');
            $adduid=M('xingzhengqu')->where(array('id'=>$id))->getField('adduid');
            $is_rbac = _rbac_btn_show(375,0,$adduid);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $condition['gongsiid']=session('gongsiid');
                $condition['id']=array('neq',$id);
                $xzqming=I('xzqming');
                $condition['xzqming']=$xzqming;
                $cfxzq=M('xingzhengqu')->where($condition)->find();
                if($cfxzq){
                    $this->error('行政区已存在');
                }
                $data['dituzb']=I('zuobiao');
                $data['xzqming']=I('xzqming');
                $isok=M("xingzhengqu")->where(array('id'=>$id))->save($data);
                if ($isok){
                    $this->success('更新成功');
                }else{
                    $this->error('更新失败');
                }
            } else {
                $this->bumen=M('bumen')->where(array('level'=>2))->select();
                $this->xingzhengqu=M('xingzhengqu')->where(array('id'=>$id))->find();
                $this->display();
            }
        }
        // 删除行政区
        public function delxzq(){
            $id=I('id');
            $adduid=M('xingzhengqu')->where(array('id'=>$id))->getField('adduid');
            $is_rbac = _rbac_btn_show(377,0,$adduid);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $pqshu=M('pianqu')->where(array('ssxzq'=>$id))->count();
            $xqshu=M('xiaoqu')->where(array('ssxzq'=>$id))->count();
            if ($pqshu==0 && $xqshu==0){
                $isok=M('xingzhengqu')->where(array('id'=>$id))->delete();
            } else {
                $this->error('请先转移数据');
            }
            if ($isok){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
        // 片区列表
        public function Pianqu(){
            if (I('is_ajax')) {
                $field = I('field'); //排序字段名
                $order = I('order'); //排序方式
                $limit = I('limit',30,'int'); //每页条数
                $limit = $limit > 150 ? 150 : $limit;
                $map .= " where e.gongsiid=".session('gongsiid');
                $x = _rbac_lists_where(123,0);
                if ($x && $x != "all" && $x != "me") {
                    $map .= " and e.bumen in ({$x})";
                }elseif ($x == "me") {
                    $map .= " and e.id=".session('uid');
                }elseif ($x == false) {
                    $map .= " and e.id=0";
                }
                $sousuo=I('sousuo');
                if ($sousuo) {
                    $map.=" and ((a.pianqum like '%$sousuo%') or (a.pinyinjs like '%$sousuo%'))";
                }
                if ($field && $order) {
                    $pqpx.="order by a.".$field." ".$order;
                } else {
                    $pqpx.="order by a.id DESC";
                }
                $db = M('pianqu');
                $count = $db->query("select count(*) from __PIANQU__ a join __YONGHU__ e on a.adduid=e.id {$map}");
                $Page = new \Think\Page($count['0']['count(*)'],$limit);
                $list="select a.*,e.ygmingcheng,e.bumen from __PIANQU__ a join __YONGHU__ e on a.adduid=e.id {$map} ".$pqpx." limit ".$Page->firstRow.','.$Page->listRows;
                $data = $db->query($list);
                foreach ($data as $key => $value) {
                    $data[$key]['xzqm'] = M('xingzhengqu')->where(array('id'=>$value['ssxzq']))->getField('xzqming');
                    $data[$key]['tjbm'] = bumen($value['bumen']);
                    $is_rbacgx = _rbac_btn_show(126,0,$value['adduid']);
                    $is_rbaczy = _rbac_btn_show(533,0,$value['adduid']);
                    $is_rbacsc = _rbac_btn_show(128,0,$value['adduid']);
                    $data[$key]['rbacgx'] = $is_rbacgx ? 1 : 0;
                    $data[$key]['rbaczy'] = $is_rbaczy ? 1 : 0;
                    $data[$key]['rbacsc'] = $is_rbacsc ? 1 : 0;
                    $data[$key]['pinyinjs'] = $value['pinyinjs'] ? $value['pinyinjs'] : "---";
                    $data[$key]['dituzb'] = $value['dituzb'] ? $value['dituzb'] : "---";
                }
                $return_data = array(
                    'code'=>0,
                    'count'=>$count['0']['count(*)'],
                    'data'=>$data
                );
                $this->ajaxReturn($return_data);
            } else {
                $this->display();
            }
        }
        // 添加片区
        public function addPianqu(){
            $is_rbac = _rbac_btn_show(124,0);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $condition['gongsiid']=session('gongsiid');
                $ssxzq=I('ssxzq');
                $pianqum=I('pianqum');
                $condition['ssxzq']=$ssxzq;
                $condition['pianqum']=$pianqum;
                $cfpq=M('pianqu')->where($condition)->find();
                if($cfpq){
                    $this->error('片区已存在');
                }
                $bumen=M('yonghu')->where(array('id'=>session('uid')))->getField('bumen');
                $data['bumen']=$bumen;
                $data['shijian']=time();
                $data['pianqum']=I('pianqum');
                $data['pinyinjs']=I('pinyinjs');
                $data['ssxzq']=I('ssxzq');
                $data['dituzb']=I('zuobiao');
                $data['adduid']=session('uid');
                $data['gongsiid']=session('gongsiid');
                $isok=M('pianqu')->add($data);
                if ($isok) {
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            } else {
                $this->ssxzq=M('xingzhengqu')->where(array('gongsiid'=>session('gongsiid')))->select();
                $this->display();
            }
        }
        // 更新片区
        public function editPianqu(){
            $id=I('id');
            $adduid=M('pianqu')->where(array('id'=>$id))->getField('adduid');
            $is_rbac = _rbac_btn_show(126,0,$adduid);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $condition['gongsiid']=session('gongsiid');
                $condition['id']=array('neq',$id);
                $ssxzq=I('ssxzq');
                $pianqum=I('pianqum');
                $condition['ssxzq']=$ssxzq;
                $condition['pianqum']=$pianqum;
                $cfpq=M('pianqu')->where($condition)->find();
                if($cfpq){
                    $this->error('片区已存在');
                }
                $data['pianqum']=I('pianqum');
                $data['pinyinjs']=I('pinyinjs');
                $data['ssxzq']=I('ssxzq');
                $data['dituzb']=I('zuobiao');
                $isok=M("pianqu")->where(array('id'=>$id))->save($data);
                if ($isok){
                    $this->success('更新成功');
                }else{
                    $this->error('更新失败');
                }
            } else {
                $this->pianqu=M('pianqu')->where(array('id'=>$id))->find();
                $this->ssxzq=M('xingzhengqu')->where(array('gongsiid'=>session('gongsiid')))->select();
                $this->display();
            }
        }
        // 删除片区
        public function delPianqu(){
            $id=I('id');
            $adduid=M('pianqu')->where(array('id'=>$id))->getField('adduid');
            $is_rbac = _rbac_btn_show(128,0,$adduid);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $xqshu=M('xiaoqu')->where(array('sspianqu'=>$id))->count();
            if ($xqshu==0){
                $isok=M('pianqu')->where(array('id'=>$id))->delete();
            } else {
                $this->error('请先转移数据');
            }
            if ($isok){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
        // 小区列表
        public function Xiaoqu(){
            if (I('is_ajax')) {
                $field = I('field'); //排序字段名
                $order = I('order'); //排序方式
                $limit = I('limit',30,'int'); //每页条数
                $limit = $limit > 150 ? 150 : $limit;
                $map .= " where e.gongsiid=".session('gongsiid');
                $x = _rbac_lists_where(129,0);
                if ($x && $x != "all" && $x != "me") {
                    $map .= " and e.bumen in ({$x})";
                }elseif ($x == "me") {
                    $map .= " and e.id=".session('uid');
                }elseif ($x == false) {
                    $map .= " and e.id=0";
                }
                $is_rbac = _rbac_node_show(932);
                if (!$is_rbac) {
                    $map .= " and a.status=1";
                }
                $sousuo=I('sousuo');
                if ($sousuo) {//搜索
                    $map.=" and ((a.xiaoqum like '%$sousuo%') or (a.pinyinjs like '%$sousuo%'))";
                }
                if ($field && $order) {
                    $xqpx.="order by a.".$field." ".$order;
                } else {
                    $xqpx.="order by a.id DESC";
                }
                $db = M('xiaoqu');
                $count = $db->query("select count(*) from __XIAOQU__ a join __YONGHU__ e on a.adduid=e.id {$map}");
                $Page = new \Think\Page($count['0']['count(*)'],$limit);
                $list="select a.*,b.xzqming,c.pianqum,e.ygmingcheng,e.bumen from __XIAOQU__ a join __YONGHU__ e on a.adduid=e.id left join __XINGZHENGQU__ b on a.ssxzq=b.id left join __PIANQU__ c on a.sspianqu=c.id {$map} ".$xqpx." limit ".$Page->firstRow.','.$Page->listRows;
                $data = $db->query($list);
                foreach ($data as $key => $value) {
                    $data[$key]['tjbm'] = bumen($value['bumen']);
                    $is_rbacxq = _rbac_btn_show(175,0,$value['adduid']);
                    $is_rbacgx = _rbac_btn_show(132,0,$value['adduid']);
                    $is_rbaczy = _rbac_btn_show(533,0,$value['adduid']);
                    $is_rbacsc = _rbac_btn_show(134,0,$value['adduid']);
                    $data[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
                    $data[$key]['rbacgx'] = $is_rbacgx ? 1 : 0;
                    $data[$key]['rbaczy'] = $is_rbaczy ? 1 : 0;
                    $data[$key]['rbacsc'] = $is_rbacsc ? 1 : 0;
                    $data[$key]['pinyinjs'] = $value['pinyinjs'] ? $value['pinyinjs'] : "---";
                    $data[$key]['dituzb'] = $value['dituzb'] ? $value['dituzb'] : "---";
                }
                $return_data = array(
                    'code'=>0,
                    'count'=>$count['0']['count(*)'],
                    'data'=>$data
                );
                $this->ajaxReturn($return_data);
            } else {
                $this->display();
            }
        }
        // 添加小区
        public function addXiaoqu(){
            $is_rbac = _rbac_btn_show(130,0);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $is_rbaczt = _rbac_node_show(932);
            $qxpdzt = $is_rbaczt ? 1 : 0;
            if (IS_POST) {
                $condition['gongsiid']=session('gongsiid');
                $ssxzq=I('ssxzq');
                $sspianqu=I('sspianqu');
                $xiaoqum=I('xiaoqum');
                $condition['ssxzq']=$ssxzq;
                $condition['sspianqu']=$sspianqu;
                $condition['xiaoqum']=$xiaoqum;
                $cfxq=M('xiaoqu')->where($condition)->find();
                if($cfxq){
                    $this->error('小区已存在');
                }
                $zrbm=I('zrbm');
                $bdbm=I('bdbm');
                $zruid=I('zruid');
                $level=M('bumen')->where(array('id'=>$zrbm))->getField('level');
                if($level != 2){
                    $this->error('责任部门异常！');
                }
                $user=M('yonghu')->where(array('id'=>$zruid,'lock1'=>0,'status'=>1))->field('bumen,leixing')->find();
                if($user['leixing'] != 1){
                    $this->error('员工异常！');
                }
                if($user['bumen'] != $zrbm){
                    $this->error('数据异常！');
                }
                $bdbumen=M('bumen')->where(array('id'=>$bdbm))->field('level,parentid')->find();
                if($bdbumen['level'] != 3){
                    $this->error('绑定部门异常！');
                }
                if($bdbumen['parentid'] != $zrbm){
                    $this->error('数据异常！');
                }
                // $isbd=M('xiaoqu')->where(array('bdbumen'=>$bdbm))->getField('xiaoqum');
                // if($isbd){
                //     $this->error('该部门已被小区'.$isbd.'绑定！');
                // }
                if ($qxpdzt == 1) {
                    $status = I('status') ? 1 : 0;
                    $data['status'] = $status;
                }
                $data['bumen']=$zrbm;
                $data['bdbumen']=$bdbm;
                $data['adduid']=$zruid;
                $data['ssxzq']=$ssxzq;
                $data['dituzb']=I('dituzb');
                $data['jiedao']=I('jiedao');
                $data['jianjie']=I('jianjie');
                $data['xiaoqum']=$xiaoqum;
                if (I('csjunjia')) {
                    $data['csjunjia']=I('csjunjia');
                }
                $data['pinyinjs']=I('pinyinjs');
                $data['sspianqu']=$sspianqu;
                $data['xiaoqudz']=I('xiaoqudz');
                $data['youzheng']=I('youzheng');
                $data['shijian']=time();
                $data['luruuid']=session('uid');
                $data['gongsiid']=session('gongsiid');
                $isok=M("xiaoqu")->add($data);
                if ($isok) {
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            } else {
                $this->qxpdzt=$qxpdzt;
                $this->bumen=M('bumen')->where(array('level'=>2,'id > 2'))->select();
                $this->sspianqu=M('pianqu')->where(array('gongsiid'=>session('gongsiid')))->select();
                $this->ssxzq=M('xingzhengqu')->where(array('gongsiid'=>session('gongsiid')))->select();
                $this->display();
            }
        }
        // 更新小区
        public function editXiaoqu(){
            $id=I('id');
            $adduid=M('xiaoqu')->where(array('id'=>$id))->getField('adduid');
            $is_rbac = _rbac_btn_show(132,0,$adduid);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $is_rbaczt = _rbac_node_show(932);
            $qxpdzt = $is_rbaczt ? 1 : 0;
            if (IS_POST) {
                $condition['gongsiid']=session('gongsiid');
                $condition['id']=array('neq',$id);
                $ssxzq=I('ssxzq');
                $sspianqu=I('sspianqu');
                $xiaoqum=I('xiaoqum');
                $condition['ssxzq']=$ssxzq;
                $condition['sspianqu']=$sspianqu;
                $condition['xiaoqum']=$xiaoqum;
                $cfxq=M('xiaoqu')->where($condition)->find();
                if($cfxq){
                    $this->error('小区已存在');
                }
                $zrbm=I('zrbm');
                $bdbm=I('bdbm');
                $zruid=I('zruid');
                $level=M('bumen')->where(array('id'=>$zrbm))->getField('level');
                if($level != 2){
                    $this->error('部门异常！');
                }
                $user=M('yonghu')->where(array('id'=>$zruid,'lock1'=>0,'status'=>1))->field('bumen,leixing,dianhua,ygmingcheng')->find();
                if($user['leixing'] != 1){
                    $this->error('员工异常！');
                }
                if($user['bumen'] != $zrbm){
                    $this->error('数据异常！');
                }
                $bdbumen=M('bumen')->where(array('id'=>$bdbm))->field('level,parentid')->find();
                if($bdbumen['level'] != 3){
                    $this->error('绑定部门异常！');
                }
                if($bdbumen['parentid'] != $zrbm){
                    $this->error('数据异常！');
                }
                // $isbd=M('xiaoqu')->where(array('bdbumen'=>$bdbm,'id'=>array('neq',$id)))->getField('xiaoqum');
                // if($isbd){
                //     $this->error('该部门已被小区'.$isbd.'绑定！');
                // }
                if ($qxpdzt == 1) {
                    $status = I('status') ? 1 : 0;
                    $data['status'] = $status;
                }
                $data['bumen']=$zrbm;
                $data['bdbumen']=$bdbm;
                $data['adduid']=$zruid;
                $data['ssxzq']=$ssxzq;
                $data['dituzb']=I('dituzb');
                $data['jiedao']=I('jiedao');
                $data['jianjie']=I('jianjie');
                $data['xiaoqum']=$xiaoqum;
                if (I('csjunjia')) {
                    $data['csjunjia']=I('csjunjia');
                }
                $data['pinyinjs']=I('pinyinjs');
                $data['sspianqu']=$sspianqu;
                $data['xiaoqudz']=I('xiaoqudz');
                $data['youzheng']=I('youzheng');
                $isok=M("xiaoqu")->where(array('id'=>$id))->save($data);
                if ($isok){
                    $data_xqbdxs['fzr']=$user['ygmingcheng'];
                    $data_xqbdxs['fzdh']=$user['dianhua'];
                    $data_xqbdxs['bumen']=$zrbm;
                    $data_xqbdxs['xqtjr']=$zruid;
                    $data_xqbdxs['xiaoqum']=$xiaoqum;
                    M("xqbdxs")->where(array('id'=>$value['id']))->save($data_xqbdxs);
                    $this->success('更新成功');
                }else{
                    $this->error('更新失败');
                }
            } else {
                $this->qxpdzt=$qxpdzt;
                $xiaoqu=M('xiaoqu')->where(array('id'=>$id))->find();
                $this->xiaoqu=$xiaoqu;
                $this->bumen=M('bumen')->where(array('level'=>2,'leixing'=>0))->select();
                $this->ygmingcheng=M('Yonghu')->where("gongsiid = ".session('gongsiid')." and lock1 = 0 and status = 1 and leixing = 1 and bumen=".$xiaoqu['bumen'])->order('pinyinjs asc')->select();
                $this->bdbumen=M('bumen')->where(array('level'=>3,'parentid'=>$xiaoqu['bumen']))->select();
                $this->sspianqu=M('pianqu')->where(array('gongsiid'=>session('gongsiid')))->select();
                $this->ssxzq=M('xingzhengqu')->where(array('gongsiid'=>session('gongsiid')))->select();
                $this->display();
            }
        }
        // 删除小区
        public function delXiaoqu(){
            $id=I('id');
            $adduid=M('xiaoqu')->where(array('id'=>$id))->getField('adduid');
            $is_rbac = _rbac_btn_show(134,0,$adduid);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $zdshu=0;
            if ($zdshu==0){
                $isok=M('xiaoqu')->where(array('id'=>$id))->delete();
            } else {
                $this->error('请先转移数据');
            }
            if ($isok){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
        // 小区详情
        public function xiaoqurr(){
            $id=I('id');
            $xiaoqu=M('xiaoqu')->where(array('id'=>$id))->find();
            $is_rbac = _rbac_btn_show(175,0,$xiaoqu['adduid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $is_rbac1 = _rbac_node_show(932);
            if (!$is_rbac1 && $xiaoqu['status'] != 1) {
                $xiaoqu="";
            } else {
                $this->pianqu=M('pianqu')->where(array('id'=>$xiaoqu['sspianqu']))->getField('pianqum');
                $this->xingzhengqu=M('xingzhengqu')->where(array('id'=>$xiaoqu['ssxzq']))->getField('xzqming');
                $this->zdshu=0;
                $this->fjshu=0;
                $this->wuyelx=M('peizhi')->where(array('pzming'=>'wuyelx','lxid'=>$xiaoqu['wuyelx']))->getField('lxming');
                $this->wuyezz=M('peizhi')->where(array('pzming'=>'wuyezz','lxid'=>$xiaoqu['wuyezz']))->getField('lxming');
                $this->xmjibie=M('peizhi')->where(array('pzming'=>'xmjibie','lxid'=>$xiaoqu['xmjibie']))->getField('lxming');
                $this->gongqifs=M('peizhi')->where(array('pzming'=>'gongqifs','lxid'=>$xiaoqu['gongqifs']))->getField('lxming');
                $this->kangzhen=M('peizhi')->where(array('pzming'=>'kangzhen','lxid'=>$xiaoqu['kangzhen']))->getField('lxming');
                $this->ceweizldw=M('peizhi')->where(array('pzming'=>'ceweizldw','lxid'=>$xiaoqu['ceweizldw']))->getField('lxming');
                $this->gongdianlx=M('peizhi')->where(array('pzming'=>'gongdianlx','lxid'=>$xiaoqu['gongdianlx']))->getField('lxming');
                $this->gongshuilx=M('peizhi')->where(array('pzming'=>'gongshuilx','lxid'=>$xiaoqu['gongshuilx']))->getField('lxming');
                $this->spjzleixing=M('peizhi')->where(array('pzming'=>'spjzleixing','lxid'=>$xiaoqu['spjzleixing']))->getField('lxming');
                $jiegou1=M('xiaoqu')->where(array('id'=>$id))->getField('jiegou');
                if ($jiegou1) {
                    $condition="pzming = 'jiegou'";
                    $condition.=" and lxid in ($jiegou1)";
                    $this->jiegou=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
                }
                $yongtu1=M('xiaoqu')->where(array('id'=>$id))->getField('yongtu');
                if ($yongtu1) {
                    $condition="pzming = 'yongtu'";
                    $condition.=" and lxid in ($yongtu1)";
                    $this->yongtu=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
                }
                $dianshi1=M('xiaoqu')->where(array('id'=>$id))->getField('dianshi');
                if ($dianshi1) {
                    $condition="pzming = 'dianshi'";
                    $condition.=" and lxid in ($dianshi1)";
                    $this->dianshi=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
                }
                $tongxun1=M('xiaoqu')->where(array('id'=>$id))->getField('tongxun');
                if ($tongxun1) {
                    $condition="pzming = 'tongxun'";
                    $condition.=" and lxid in ($tongxun1)";
                    $this->tongxun=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
                }
                $chanquan1=M('xiaoqu')->where(array('id'=>$id))->getField('chanquan');
                if ($chanquan1) {
                    $condition="pzming = 'chanquan'";
                    $condition.=" and lxid in ($chanquan1)";
                    $this->chanquan=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
                }
                $sqanquan1=M('xiaoqu')->where(array('id'=>$id))->getField('sqanquan');
                if ($sqanquan1) {
                    $condition="pzming = 'sqanquan'";
                    $condition.=" and lxid in ($sqanquan1)";
                    $this->sqanquan=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
                }
                $cainuanfs1=M('xiaoqu')->where(array('id'=>$id))->getField('cainuanfs');
                if ($cainuanfs1) {
                    $condition="pzming = 'cainuanfs'";
                    $condition.=" and lxid in ($cainuanfs1)";
                    $this->cainuanfs=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
                }
                $fwleixing1=M('xiaoqu')->where(array('id'=>$id))->getField('fwleixing');
                if ($fwleixing1) {
                    $condition="pzming = 'fwleixing'";
                    $condition.=" and lxid in ($fwleixing1)";
                    $this->fwleixing=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
                }
                $wldibanlx1=M('xiaoqu')->where(array('id'=>$id))->getField('wldibanlx');
                if ($wldibanlx1) {
                    $condition="pzming = 'wldibanlx'";
                    $condition.=" and lxid in ($wldibanlx1)";
                    $this->wldibanlx=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
                }
                $gongshuifs1=M('xiaoqu')->where(array('id'=>$id))->getField('gongshuifs');
                if ($gongshuifs1) {
                    $condition="pzming = 'gongshuifs'";
                    $condition.=" and lxid in ($gongshuifs1)";
                    $this->gongshuifs=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
                }
            }
            $this->jcpz=M('jcpz')->where(array('gongsiid'=>session('gongsiid')))->field('dtmiyao,dtzuobiao')->find();
            $this->xiaoqu=$xiaoqu;
            $this->display();
        }
        // 修改小区详情
        public function xiugaixiaoqu(){
            $id=I('id');
            $adduid=M('xiaoqu')->where(array('id'=>$id))->getField('adduid');
            $is_rbac = _rbac_btn_show(176,0,$adduid);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $bjlpxq = I('bjlpxq');
            if (IS_POST) {
                switch ($bjlpxq) {
                    case '1':
                        $is_rbaczt = _rbac_node_show(932);
                        $qxpdzt = $is_rbaczt ? 1 : 0;
                        if ($qxpdzt == 1) {
                            $status = I('status') ? 1 : 0;
                            $data['status'] = $status;
                        }
                        if (I('chewei')) {
                            $data['chewei']=1;
                            $data['ceweizldw']=I('ceweizldw');
                            $data['dxiatcwsj']=I('dxiatcwsj');
                            $data['dxiatcwsl']=I('dxiatcwsl');
                            $data['dxiatcwzl']=I('dxiatcwzl');
                            $data['dshangtcwsj']=I('dshangtcwsj');
                            $data['dshangtcwsl']=I('dshangtcwsl');
                            $data['dshangtcwzl']=I('dshangtcwzl');
                        } else {
                            $data['chewei']=2;
                        }
                        if (I('dianti')) {
                            $data['dianti']=1;
                            $data['diantixq']=I('diantixq');
                        } else {
                            $data['dianti']=2;
                        }
                        $data['beizhu']=I('beizhu');
                        $data['jiedao']=I('jiedao');
                        $data['wuyedh']=I('wuyedh');
                        $data['wuyedz']=I('wuyedz');
                        $data['wuyemc']=I('wuyemc');
                        $data['jianjie']=I('jianjie');
                        $data['lvhualv']=I('lvhualv');
                        $data['wuyefei']=I('wuyefei');
                        $data['csjunjia']=I('csjunjia');
                        $data['gongqifs']=I('gongqifs');
                        $data['rongjilv']=I('rongjilv');
                        $data['xiaoqubm']=I('xiaoqubm');
                        $data['xiaoqudz']=I('xiaoqudz');
                        $data['youzheng']=I('youzheng');
                        $data['zhandimj']=I('zhandimj');
                        $data['jianzaond']=I('jianzaond');
                        $data['jianzhumj']=I('jianzhumj');
                        $data['gongdianlx']=I('gongdianlx');
                        $data['gongshuilx']=I('gongshuilx');
                        break;
                    case '2':
                        $data['ditielx']=I('ditielx');
                        $data['jinrongjg']=I('jinrongjg');
                        $data['paichusuo']=I('paichusuo');
                        $data['zhengfujg']=I('zhengfujg');
                        $data['gongjiaolx']=I('gongjiaolx');
                        $data['xiuxianyule']=I('xiuxianyule');
                        $data['yiliaojigou']=I('yiliaojigou');
                        $data['shangchangcs']=I('shangchangcs');
                        break;
                    case '3':
                        $jg1=array_keys(I('jiegou'));
                        $jg2=implode(',',$jg1);
                        $data['jiegou']=$jg2;
                        $yt1=array_keys(I('yongtu'));
                        $yt2=implode(',',$yt1);
                        $data['yongtu']=$yt2;
                        $cq1=array_keys(I('chanquan'));
                        $cq2=implode(',',$cq1);
                        $data['chanquan']=$cq2;
                        $fw1=array_keys(I('fwleixing'));
                        $fw2=implode(',',$fw1);
                        $data['fwleixing']=$fw2;
                        $data['lvhualv']=I('lvhualv');
                        $data['csjunjia']=I('csjunjia');
                        $data['nianxian']=I('nianxian');
                        $data['rongjilv']=I('rongjilv');
                        $data['zhandimj']=I('zhandimj');
                        $data['jianzaond']=I('jianzaond');
                        $data['jianzhumj']=I('jianzhumj');
                        $data['ycqdanwei']=I('ycqdanwei');
                        $data['kaifashang']=I('kaifashang');
                        $data['kpshijian']=strtotime(I('kpshijian'));
                        $data['rzshijian']=strtotime(I('rzshijian'));
                        break;
                    case '4':
                        if (I('chewei')) {
                            $data['chewei']=1;
                            $data['ceweizldw']=I('ceweizldw');
                            $data['dxiatcwsj']=I('dxiatcwsj');
                            $data['dxiatcwsl']=I('dxiatcwsl');
                            $data['dxiatcwzl']=I('dxiatcwzl');
                            $data['dshangtcwsj']=I('dshangtcwsj');
                            $data['dshangtcwsl']=I('dshangtcwsl');
                            $data['dshangtcwzl']=I('dshangtcwzl');
                        } else {
                            $data['chewei']=2;
                        }
                        if (I('dianti')) {
                            $data['dianti']=1;
                            $data['diantixq']=I('diantixq');
                        } else {
                            $data['dianti']=2;
                        }
                        $ds1=array_keys(I('dianshi'));
                        $ds2=implode(',',$ds1);
                        $data['dianshi']=$ds2;
                        $tx1=array_keys(I('tongxun'));
                        $tx2=implode(',',$tx1);
                        $data['tongxun']=$tx2;
                        $aq1=array_keys(I('sqanquan'));
                        $aq2=implode(',',$aq1);
                        $data['sqanquan']=$aq2;
                        $cn1=array_keys(I('cainuanfs'));
                        $cn2=implode(',',$cn1);
                        $data['cainuanfs']=$cn2;
                        $gs1=array_keys(I('gongshuifs'));
                        $gs2=implode(',',$gs1);
                        $data['gongshuifs']=$gs2;
                        $data['wuyedh']=I('wuyedh');
                        $data['wuyedz']=I('wuyedz');
                        $data['wuyelx']=I('wuyelx');
                        $data['wuyemc']=I('wuyemc');
                        $data['wuyezz']=I('wuyezz');
                        $data['dianfei']=I('dianfei');
                        $data['wuyefei']=I('wuyefei');
                        $data['gongqifs']=I('gongqifs');
                        $data['cainuanfy']=I('cainuanfy');
                        $data['gongdianlx']=I('gongdianlx');
                        $data['gongshuilx']=I('gongshuilx');
                        break;
                    default:
                        if (I('chewei')) {
                            $data['chewei']=1;
                            $data['ceweizldw']=I('ceweizldw');
                            $data['dxiatcwsj']=I('dxiatcwsj');
                            $data['dxiatcwsl']=I('dxiatcwsl');
                            $data['dxiatcwzl']=I('dxiatcwzl');
                            $data['dshangtcwsj']=I('dshangtcwsj');
                            $data['dshangtcwsl']=I('dshangtcwsl');
                            $data['dshangtcwzl']=I('dshangtcwzl');
                        } else {
                            $data['chewei']=2;
                        }
                        if (I('dianti')) {
                            $data['dianti']=1;
                            $data['diantixq']=I('diantixq');
                        } else {
                            $data['dianti']=2;
                        }
                        $data['beizhu']=I('beizhu');
                        $data['jiedao']=I('jiedao');
                        $data['wuyedh']=I('wuyedh');
                        $data['wuyedz']=I('wuyedz');
                        $data['wuyemc']=I('wuyemc');
                        $data['jianjie']=I('jianjie');
                        $data['lvhualv']=I('lvhualv');
                        $data['wuyefei']=I('wuyefei');
                        $data['csjunjia']=I('csjunjia');
                        $data['gongqifs']=I('gongqifs');
                        $data['rongjilv']=I('rongjilv');
                        $data['xiaoqubm']=I('xiaoqubm');
                        $data['xiaoqudz']=I('xiaoqudz');
                        $data['youzheng']=I('youzheng');
                        $data['zhandimj']=I('zhandimj');
                        $data['jianzaond']=I('jianzaond');
                        $data['jianzhumj']=I('jianzhumj');
                        $data['gongdianlx']=I('gongdianlx');
                        $data['gongshuilx']=I('gongshuilx');
                        break;
                }
                $isok=M('xiaoqu')->where(array('id'=>$id))->save($data);
                if ($isok){
                    $this->success('更新成功');
                }else{
                    $this->error('更新失败');
                }
            } else {
                switch ($bjlpxq) {
                    case '1':
                        $is_rbaczt = _rbac_node_show(932);
                        $qxpdzt = $is_rbaczt ? 1 : 0;
                        $this->qxpdzt=$qxpdzt;
                        $this->gongqifs=M('peizhi')->where(array('pzming'=>'gongqifs','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->ceweizldw=M('peizhi')->where(array('pzming'=>'ceweizldw','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->gongdianlx=M('peizhi')->where(array('pzming'=>'gongdianlx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->gongshuilx=M('peizhi')->where(array('pzming'=>'gongshuilx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        break;
                    case '2':
                        break;
                    case '3':
                        $this->jiegou=M('peizhi')->where(array('pzming'=>'jiegou','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->yongtu=M('peizhi')->where(array('pzming'=>'yongtu','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->chanquan=M('peizhi')->where(array('pzming'=>'chanquan','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->fwleixing=M('peizhi')->where(array('pzming'=>'fwleixing','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $jiegou1=M('xiaoqu')->where(array('id'=>$id))->getField('jiegou');
                        $this->jiegou2=explode(",", $jiegou1);
                        $yongtu1=M('xiaoqu')->where(array('id'=>$id))->getField('yongtu');
                        $this->yongtu2=explode(",", $yongtu1);
                        $chanquan1=M('xiaoqu')->where(array('id'=>$id))->getField('chanquan');
                        $this->chanquan2=explode(",", $chanquan1);
                        $fwleixing1=M('xiaoqu')->where(array('id'=>$id))->getField('fwleixing');
                        $this->fwleixing2=explode(",", $fwleixing1);
                        break;
                    case '4':
                        $this->wuyelx=M('peizhi')->where(array('pzming'=>'wuyelx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->wuyezz=M('peizhi')->where(array('pzming'=>'wuyezz','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->dianshi=M('peizhi')->where(array('pzming'=>'dianshi','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->tongxun=M('peizhi')->where(array('pzming'=>'tongxun','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->gongqifs=M('peizhi')->where(array('pzming'=>'gongqifs','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->sqanquan=M('peizhi')->where(array('pzming'=>'sqanquan','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->cainuanfs=M('peizhi')->where(array('pzming'=>'cainuanfs','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->ceweizldw=M('peizhi')->where(array('pzming'=>'ceweizldw','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->gongdianlx=M('peizhi')->where(array('pzming'=>'gongdianlx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->gongshuifs=M('peizhi')->where(array('pzming'=>'gongshuifs','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->gongshuilx=M('peizhi')->where(array('pzming'=>'gongshuilx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $dianshi1=M('xiaoqu')->where(array('id'=>$id))->getField('dianshi');
                        $this->dianshi2=explode(",", $dianshi1);
                        $tongxun1=M('xiaoqu')->where(array('id'=>$id))->getField('tongxun');
                        $this->tongxun2=explode(",", $tongxun1);
                        $sqanquan1=M('xiaoqu')->where(array('id'=>$id))->getField('sqanquan');
                        $this->sqanquan2=explode(",", $sqanquan1);
                        $cainuanfs1=M('xiaoqu')->where(array('id'=>$id))->getField('cainuanfs');
                        $this->cainuanfs2=explode(",", $cainuanfs1);
                        $gongshuifs1=M('xiaoqu')->where(array('id'=>$id))->getField('gongshuifs');
                        $this->gongshuifs2=explode(",", $gongshuifs1);
                        break;
                    default:
                        $bjlpxq = 1;
                        $this->qxpdzt=$qxpdzt;
                        $this->gongqifs=M('peizhi')->where(array('pzming'=>'gongqifs','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->ceweizldw=M('peizhi')->where(array('pzming'=>'ceweizldw','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->gongdianlx=M('peizhi')->where(array('pzming'=>'gongdianlx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        $this->gongshuilx=M('peizhi')->where(array('pzming'=>'gongshuilx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                        break;
                }
                $this->bjlpxq=$bjlpxq;
                $this->xiaoqu=M('xiaoqu')->where(array('id'=>$id))->find();
                $this->display();
            }
        }
        // 调整坐标
        public function tzdtzb(){
            $id=I('id');
            $adduid=M('xiaoqu')->where(array('id'=>$id))->getField('adduid');
            $is_rbac = _rbac_btn_show(180,0,$adduid);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $data['dituzb']=I('dituzb');
                $isok=M("xiaoqu")->where(array('id'=>$id))->save($data);
                if ($isok){
                    $this->success('更新成功');
                }else{
                    $this->error('更新失败');
                }
            } else {
                $this->xiaoqu=M('xiaoqu')->where(array('id'=>$id))->find();
                $this->jcpz=M('jcpz')->where(array('gongsiid'=>session('gongsiid')))->field('dtmiyao,dtzuobiao')->find();
                $this->display();
            }
        }
        /**
        * webuploader 图片上传
        */
        function scxqtp() {
            $xqid=I('xqid');
            $adduid=M('xiaoqu')->where(array('id'=>$xqid))->getField('adduid');
            $is_rbac = _rbac_btn_show(186,0,$adduid);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                // 根据自己的业务调整上传路径、允许的格式、文件大小
                $path = "/Upload/".session('gongsiid')."/xiaoqu/".$xqid;
                ajax_upload_xq($path);
            } else {
                $this->display();
            }
        }
        /**
        * [xq_pic_setfengmian 异步小区图片为封面]
        * @return [json] [description]
        */
        public function xq_pic_setfengmian(){
            $id=I('xq_pic_id');
            //根据图片id查找小区图片的小区id号xqid
            $xqid = M('xqphoto')->where(array('id'=>$id))->getField('xqid');
            $adduid=M('xqphoto')->where(array('id'=>$id))->getField('uid');
            $is_rbac = _rbac_btn_show(485,0,$adduid);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            M('xqphoto')->where(array('xqid'=>$xqid,'fengmian'=>1))->setField(array('fengmian'=>0));
            $r = M('xqphoto')->where(array('id'=>$id))->setField(array('fengmian'=>1));
            if ($r) {
                $this->success('设置成功');
            }else{
                $this->error('设置失败');
            }
        }
        /**
        * [xq_pic_del 异步删除图片]
        * @return [json] [description]
        */
        public function xq_pic_del(){
            $id=I('xq_pic_id');
            //根据图片id查看当前是否还有当前小区的图片
            $xqid = M('xqphoto')->where(array('id'=>$id))->getField('xqid');
            $adduid=M('xqphoto')->where(array('id'=>$id))->getField('uid');
            $is_rbac = _rbac_btn_show(188,0,$adduid);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $piccount = M('xqphoto')->where(array('xqid'=>$xqid))->count();
            $dp=M('xqphoto');
            if($id){    //删除当前图片
                $plist = $dp->where(array('id'=>$id))->find();
                if ($plist){
                    unlink('./Upload/'.$plist['gongsiid'].'/xiaoqu/'.$plist['xqid'].'/'.$plist['image']);
                    unlink('./Upload/'.$plist['gongsiid'].'/xiaoqu/'.$plist['xqid'].'/t_'.$plist['image']);
                    $dp->where(array('id'=>$id))->delete();
                    $data=array('pics'=>$plist);
                    //删除最后一个小区图片后给xiaoqu表的tupian字段置 0
                    if ($piccount==1) {
                        M('xiaoqu')->where(array('id'=>$xqid))->setField('tupian',0);
                    }
                }else{
                    $data=array('pics'=>$plist);
                }
            }
            //返回值
            header("Content-type: application/json");
            exit(json_encode($data));
        }
        //批量删除小区图片
        public function xq_pic_dels(){
            $is_rbac = _rbac_node_show(486);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $ids=I('xq_pic_ids');
            foreach ($ids as $key => $value) {
                $adduid=M('xqphoto')->where(array('id'=>$ids[$key]))->getField('uid');
                $is_rbac = _rbac_btn_show(486,0,$adduid);
                if (!$is_rbac) {
                    $this->error("您选择了权限范围之外的图片！");
                }
            }
            $xqid = M('xqphoto')->where(array('id'=>$ids[0]))->getField('xqid');
            if(is_array($ids)){
                $where = 'id in('.implode(',',$ids).')';//p($where);die;
                //获取当前图片的小区id
                $dp=M('xqphoto');
                foreach ($ids as $value) {//删除小区图片文件
                    $plist = $dp->where(array('id'=>$value))->find();
                    unlink('./Upload/'.$plist['gongsiid'].'/xiaoqu/'.$plist['xqid'].'/'.$plist['image']);
                    unlink('./Upload/'.$plist['gongsiid'].'/xiaoqu/'.$plist['xqid'].'/t_'.$plist['image']);
                }
                $dp->where($where)->delete();
                //删除最后一个小区图片后给xiaoqu表的tupian字段置 0
                $nopic = M('xqphoto')->where(array('xqid'=>$xqid))->count();
                if ($nopic<=0) {
                    M('xiaoqu')->where(array('id'=>$xqid))->setField('tupian',0);
                }
                $this->success('删除成功');
            }else{
                $this->error('删除失败'); 
            }
        }
        //批量下载小区图片
        public function xq_pic_downloads(){
            $is_rbac = _rbac_node_show(487);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $ids=I('xq_pic_ids');
            foreach ($ids as $key => $value) {
                $adduid=M('xqphoto')->where(array('id'=>$ids[$key]))->getField('uid');
                $is_rbac = _rbac_btn_show(487,0,$adduid);
                if (!$is_rbac) {
                    $this->error("您选择了权限范围之外的图片！");
                }
            }
            $where = 'id in('.implode(',',$ids).')';
            $dp=M('xqphoto');
            $plist = $dp->where($where)->field('id,gongsiid,xqid,image')->select();
            $zip = new \ZipArchive; 
            if ($plist) {
                //压缩文件名(临时文件)
                $filename = './Upload/'.$plist[0]['gongsiid'].'/xiaoqu/'.$plist[0]['xqid'].'/'.md5($plist[0]['xqid']).'.zip';
                $downname = I("server.HTTP_HOST").'/Upload/'.$plist[0]['gongsiid'].'/xiaoqu/'.$plist[0]['xqid'].'/'.md5($plist[0]['xqid']).'.zip';
                //p($downname);die;
            }
            //新建zip压缩包
            $zip->open($filename,\ZipArchive::OVERWRITE | \ZipArchive::CREATE);
            //把图片一张一张加进去压缩
            foreach ($plist as $value) {
                $zip->addFile('./Upload/'.$value['gongsiid'].'/xiaoqu/'.$value['xqid'].'/'.$value['image']);
                $zip->renameName('./Upload/'.$value['gongsiid'].'/xiaoqu/'.$value['xqid'].'/'.$value['image'],$value['image']);
            }
            //打包zip
            $zip->close();
            //可以直接重定向下载
            //header('Location:'.$downname);
            $this->success($downname);
        }
        //设置图片类型
        public function settupianleixing(){
            $is_rbac = _rbac_node_show(488);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $ids=explode(",",I('xq_pic_ids'));
                foreach ($ids as $key => $value) {
                    $adduid=M('xqphoto')->where(array('id'=>$ids[$key]))->getField('uid');
                    $is_rbac = _rbac_btn_show(488,0,$adduid);
                    if (!$is_rbac) {
                        $this->error("您选择了权限范围之外的图片！");
                    }
                }
                $ids1=I('xq_pic_ids');
                $tpleixing = I('tpleixing');
                if ($tpleixing=="") {
                    $this->error("请选择图片类型");
                }
                $where = 'id in('.$ids1.')';
                $dp=M('xqphoto');
                $plist = $dp->where($where)->setField(array('leixing' => $tpleixing ));
                if ($plist) {
                    $this->success("设置成功");
                }else{
                    $this->error("设置失败");
                }
            } else {
                $this->tpleixing = M('Peizhi')->where(array('pzming'=>'xqtplx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->display();// 输出模板
            }
        }
        //行政区、片区、小区信息转移
        public function xxzhuanyi(){
            $xzqid=I('xzqid');
            $pqid=I('pqid');
            $xqid=I('xqid');
            if ($xzqid) {
                $adduid=M('xingzhengqu')->where(array('id'=>$xzqid))->getField('adduid');
                $is_rbac = _rbac_btn_show(533,0,$adduid);
                if (!$is_rbac) {
                    $this->redirect('Index/error');
                }
            } elseif ($pqid) {
                $adduid=M('pianqu')->where(array('id'=>$pqid))->getField('adduid');
                $is_rbac = _rbac_btn_show(533,0,$adduid);
                if (!$is_rbac) {
                    $this->redirect('Index/error');
                }
            } elseif ($xqid) {
                $adduid=M('xiaoqu')->where(array('id'=>$xqid))->getField('adduid');
                $is_rbac = _rbac_btn_show(533,0,$adduid);
                if (!$is_rbac) {
                    $this->redirect('Index/error');
                }
            }
            if (IS_POST) {
                $xxzqid=I('xxzqid');
                $yxzqid=I('yxzqid');
                $xpqid=I('xpqid');
                $ypqid=I('ypqid');
                $xxqid=I('xxqid');
                $yxqid=I('yxqid');
                if ($xxzqid && $yxzqid) {
                    M('pianqu')->where(array('ssxzq'=>$yxzqid))->setField('ssxzq',$xxzqid);
                    M('xiaoqu')->where(array('ssxzq'=>$yxzqid))->setField('ssxzq',$xxzqid);
                } elseif ($xpqid && $ypqid) {
                    M('xiaoqu')->where(array('sspianqu'=>$ypqid))->setField('sspianqu',$xpqid);
                }
                $this->success('转移成功');
            }else{
                if ($xzqid) {
                    $this->pqshu=M('pianqu')->where(array('ssxzq'=>$xzqid))->count();
                    $this->xqshu=M('xiaoqu')->where(array('ssxzq'=>$xzqid))->count();
                    $this->xingzhengqu=M('xingzhengqu')->where(array('gongsiid'=>session('gongsiid')))->order('shijian asc')->select();
                } elseif ($pqid) {
                    $this->xqshu=M('xiaoqu')->where(array('sspianqu'=>$pqid))->count();
                    $ssxzq=M('pianqu')->where(array('gongsiid'=>session('gongsiid'),'id'=>$pqid))->getField('ssxzq');
                    $this->pianqu=M('pianqu')->where(array('gongsiid'=>session('gongsiid'),'ssxzq'=>$ssxzq))->order('ssxzq asc')->select();
                } elseif ($xqid) {
                    $this->zdshu=0;
                    $this->xiaoqu=M('xiaoqu')->where(array('gongsiid'=>session('gongsiid'),'sspianqu'=>$sspianqu))->order('ssxzq asc,sspianqu')->select();
                }
                $this->display();
            }
        }
        //小区视频
        public function shipin(){
            $id=I('id');
            $adduid=M('xiaoqu')->where(array('id'=>$id))->getField('adduid');
            $is_rbac = _rbac_btn_show(534,0,$adduid);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                if (preg_match_all('/src=&quot;(.*?)&quot;/', I('shipin_url'), $matches)) {
                    $s1 = $matches[1][0];
                    $shipin_url='<iframe src="'.$s1.'" frameborder="0" allowfullscreen="true" width="100%" height="100%"></iframe>';
                }elseif (preg_match_all('/src=\'(.*?)\'/', I('shipin_url'), $matches)) {
                    $s1 = $matches[1][0];
                    $shipin_url='<iframe src="'.$s1.'" frameborder="0" allowfullscreen="true" width="100%" height="100%"></iframe>';
                }else{
                    $this->error('修改失败!');
                }
                $isok=M('xiaoqu')->where(array('id'=>$id))->setField('shipin_url',$shipin_url);
                if ($isok) {
                    $this->success('修改成功!');
                }else{
                    $this->error('修改失败!');
                }
            }else{
                $this->shipin_url=M('xiaoqu')->where(array('id'=>$id))->getField('shipin_url');
                $this->display();
            }
        }
        //查看绑定销售
        public function ckbdxs(){
            $map = "a.gongsiid=".session('gongsiid')." and b.status=1 and b.lock1=0";
            $x = _rbac_lists_where(914,0);
            if ($x && $x != "all" && $x != "me") {
                $map .= " and a.bumen in ({$x})";
            }elseif ($x == "me") {
                $map .= " and a.xqtjr=".session('uid');
            }elseif ($x == false) {
                $map .= " and a.xqtjr=0";
            }
            $role=I('role');
            $bdxscx=I('bdxscx');
            $sousuo=I('sousuo');
            if ($role == 2) {
                $map .= " and b.leixing=2";
            } else if ($role == 3) {
                $map .= " and b.leixing=3";
            } else {
                $map .= " and (b.leixing=2 or b.leixing=3)";
            }
            if ($bdxscx) {
                $map.=" and (a.xsm like '%$bdxscx%')";
            }
            if ($sousuo) {
                $map.=" and (b.dianhua like '%$sousuo%' or b.pinyinjs like '%$sousuo%' or b.ygmingcheng like '%$sousuo%' or b.ygbianhao like '%$sousuo%')";
            }
            $count=M('xqbdxs')->query("select count(*) from __XQBDXS__ a join __YONGHU__ b on a.uid=b.id where {$map} ");
            $Page=new \Think\Page($count['0']['count(*)'],30);
            $show=$Page->show();
            $list="select a.*,b.ygmingcheng as bdyh,b.dianhua as bddh,d.remark as bdyhzw,e.ygmingcheng as tjr from __XQBDXS__ a join __YONGHU__ b on a.uid=b.id left join __ROLE_USER__ c on b.id=c.user_id left join __ROLE__ d on c.role_id=d.id left join __YONGHU__ e on a.adduid=e.id where {$map} order by b.pinyinjs asc limit ".$Page->firstRow.','.$Page->listRows;
            $xqbdxs=M('xqbdxs')->query($list);
            $this->assign('firstRow',$Page->firstRow);
            $this->assign('xqbdxs',$xqbdxs);
            $this->assign('count',$count);
            $this->assign('page',$show);
            $this->display();
        }
        //解除绑定
        public function jiechubd(){
            $id=I('id');
            $xqbdxs=M('xqbdxs')->where(array('id'=>$id))->getField('xqid');
            $adduid=M('xiaoqu')->where(array('id'=>$xqbdxs))->getField('adduid');
            $is_rbac = _rbac_btn_show(915,0,$adduid);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $isok=M('xqbdxs')->where(array('id'=>$id))->delete();
            if ($isok){
                $this->success('解绑成功');
            }else{
                $this->error('解绑失败');
            }
        }
    }