<?php

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

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

    public function glphotos(){
        $gltplx=I('gltplx');
        if ($gltplx == 5) {
            $this->kyhf=I('kyhf');
        }
        $this->gltplx=$gltplx;
        $this->display();
    }

    public function tixing(){
        $condition = "a.gongsiid=".session('gongsiid');
        $sousuo=I('sousuo');
        if ($sousuo == 0) {//搜索
            $condition.=" and a.txuid=".session('uid')." and a.txshijian<".time()." and a.isyidu=0";
        } elseif ($sousuo == 1) {
            $condition.=" and a.txuid=".session('uid')." and a.txshijian<".time()." and a.isyidu=1";
        } elseif ($sousuo == 2) {
            $condition.=" and a.txuid=".session('uid')." and a.isyidu=0";
        } elseif ($sousuo == 3) {
            $condition.=" and a.txuid=".session('uid')." and a.isyidu=1";
        } elseif ($sousuo == 4) {
            $condition.=" and a.txuid=".session('uid');
        }
        if (I('shijian1') and I('shijian2')){
            if (I('shijian1') > I('shijian2')){
                $this->error('开始日大于结束日!');
            }
            $condition.=" and a.txshijian>".strtotime(I('shijian1'))." and a.txshijian<".strtotime(I('shijian2'));
        } elseif (I('shijian1')) {
            $condition.=" and a.txshijian>".strtotime(I('shijian1'));
        } elseif (I('shijian2')) {
            $condition.=" and a.txshijian<".strtotime(I('shijian2'));
        }
        $count=M('kytixing')->query("select count(*) from __KYTIXING__ a where {$condition}");
        $Page=new \Think\Page($count['0']['count(*)'],20);
        $show  = $Page->show();
        $list="select a.*,b.ygmingcheng,b.dianhua from __KYTIXING__ a left join __YONGHU__ b on a.adduid = b.id where {$condition} order by a.txshijian DESC limit ".$Page->firstRow.','.$Page->listRows;
        $tixing=M('kytixing')->query($list);
        $this->assign('firstRow',$Page->firstRow);
        $this->assign('tixing',$tixing);
        $this->assign('count',$count);
        $this->assign('page',$show);
        $this->display();
    }

    public function tixingxq(){
        $id=I('id');
        $tixing=M('kytixing')->where(array('id'=>$id))->find();
        if ($tixing['txshijian'] < time()) {
            $isok=M('kytixing')->where(array('id'=>$id))->setField('isyidu',1);
        }
        $this->txren=M('yonghu')->where(array('id'=>$tixing['txuid']))->getField('ygmingcheng');
        $this->tjren=M('yonghu')->where(array('id'=>$tixing['adduid']))->getField('ygmingcheng');
        $this->tixing=$tixing;
        $this->display();
    }

    public function get_yuangong(){
        $listObj = M('yonghu');
        $txt=I('txt');
        $node=I('node');
        $map = "gongsiid=".session('gongsiid');
        $map .= " and status=1";
        $x = _rbac_lists_where($node,0);
        if ($x && $x != "all" && $x != "me") {
            $map .= " and bumen in ({$x})";
        }elseif ($x == "me") {
            $map .= " and id=".session('uid');
        }elseif ($x == false) {
            $map .= " and id=0";
        }
        if ($txt) {
            $map .= " and (pinyinjs like '%$txt%' or ygmingcheng like '%$txt%' or dianhua like '%$txt%')";
        }
        $list = $listObj->where($map)->limit(10)->select();
        $data=array('status'=>1,'district'=>$list);
        header("Content-type: application/json");
        exit(json_encode($data));
    }

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

    public function get_yonghu(){
        $txt=I('txt');
        $User = M("yonghu"); // 实例化User对象
        $map['status'] = 1;
        $map['lock1'] = 0;
        $map['_string']='(pinyinjs like "%'.$txt.'%") OR (ygmingcheng like "%'.$txt.'%") OR (dianhua like "%'.$txt.'%")';
        $list=$User->where($map)->limit(10)->order('pinyinjs asc')->select(); 
        $data=array('status'=>1,'district'=>$list);
        header("Content-type: application/json");
        exit(json_encode($data));
    }

    public function get_rsyonghu(){
        $txt=I('txt');
        $listObj = M('yonghu');
        $where['status'] = 1;
        $where['gongsiid'] = session('gongsiid');
        $where['_string']='(pinyinjs like "%'.$txt.'%") OR (ygmingcheng like "%'.$txt.'%") OR (dianhua like "%'.$txt.'%")';
        $list = $listObj->where($where)->limit(10)->order('pinyinjs asc')->select();
        $data=array('status'=>1,'district'=>$list);
        header("Content-type: application/json");
        exit(json_encode($data));
    }

    //获取经办人
    public function get_jingbanren(){
        $txt=I('txt');
        $map['status'] = 1;
        $map['lock1'] = 0;
        $map['_string']='(pinyinjs like "%'.$txt.'%") OR (ygmingcheng like "%'.$txt.'%") OR (dianhua like "%'.$txt.'%") AND (leixing=2 or leixing=3)';
        $list=M("yonghu")->where($map)->field('id,ygmingcheng')->limit(10)->order('pinyinjs asc')->select();
        $data=array('status'=>1,'district'=>$list);
        header("Content-type: application/json");
        exit(json_encode($data));
    }

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

    //搜索片区
    public function ss_pq(){
        $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";
        }
        $txt=I('txt');
        if ($txt) {
            $map.=" and ((a.pianqum like '%$txt%') or (a.pinyinjs like '%$txt%'))";
        }
        $list="select a.id,a.pianqum from __PIANQU__ a join __YONGHU__ e on a.adduid=e.id {$map} order by a.pinyinjs asc limit 10 ";
        $pq=M('pianqu')->query($list);
        $data=array('status'=>1,'lists'=>$pq);
        header("Content-type: application/json");
        exit(json_encode($data));
    }

    //搜索小区
    public function ss_xq(){
        $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";
        }
        $txt=I('txt');
        if ($txt) {//搜索
            $map.=" and ((a.xiaoqum like '%$txt%') or (a.pinyinjs like '%$txt%'))";
        }
        $list="select a.id,a.xiaoqum from __XIAOQU__ a join __YONGHU__ e on a.adduid=e.id {$map} order by a.pinyinjs asc limit 10 ";
        $xq=M('xiaoqu')->query($list);
        $data=array('status'=>1,'lists'=>$xq);
        header("Content-type: application/json");
        exit(json_encode($data));
    }

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

    //模糊查询小区
    public function get_xiaoqu_bdxs(){
        $xzqid=M('xingzhengqu')->where(array('gongsiid'=>session('gongsiid')))->order('id asc')->getField('id');
        $pqid=M('pianqu')->where(array('gongsiid'=>session('gongsiid'),'ssxzq'=>$xzqid))->order('id asc')->getField('id');
        $txt=I('txt');
        $sspq=I('sspq');
        $listObj = M('xiaoqu');
        $where['_string']='(xiaoqum like "%'.$txt.'%") OR (pinyinjs like "%'.$txt.'%")';
        $where['gongsiid'] = session('gongsiid');
        if ($sspq) {
            $where['sspianqu'] = $sspq;
        } else {
            $where['sspianqu'] = $pqid;
        }
        $list = $listObj->where($where)->limit(10)->select();
        $data=array('status'=>1,'lists'=>$list);
        header("Content-type: application/json");
        exit(json_encode($data));
    }
    //联动异步获取片区
    public function get_pianqu_ld(){
        $listObj = M('pianqu');
        $where['ssxzq'] = I('quyuxzq');
        $where['gongsiid'] = session('gongsiid');
        $list = $listObj->where($where)->select();
        $data=array('status'=>1,'lists'=>$list);
        header("Content-type: application/json");
        exit(json_encode($data));
    }

    // 添加坐标
    public function addzb(){
        $zuobiao = I('zuobiao');
        $jcpz=M('jcpz')->where(array('gongsiid'=>session('gongsiid')))->field('dtmiyao,dtzuobiao')->find();
        if ($zuobiao) {
            $zb = $zuobiao;
            $this->isedit = 1;
        }else{
            $zb = $jcpz['dtzuobiao'];
        }
        $this->zb = $zb;
        $this->jcpz = $jcpz;
        $this->display();
    }

    public function get_xsyhm(){
        $txt=I('txt');
        $xslx=I('xslx');
        $role=I('role');
        $map = "a.gongsiid=".session('gongsiid');
        if ($txt) {
            $map.=" and (a.dianhua like '%$txt%' or a.pinyinjs like '%$txt%' or a.ygmingcheng like '%$txt%' or a.ygbianhao like '%$txt%')";
        }
        if ($xslx == 1) {
            if ($role == 1) {
                $list="select a.ygmingcheng from __YONGHU__ a join (select xsid from __XQBDXS__ group by xsid) as b on a.id=b.xsid where {$map} order by a.pinyinjs asc limit 10 ";
            } else {
                $list="select a.ygmingcheng from __YONGHU__ a join (select uid from __XQBDXS__ group by uid) as b on a.id=b.uid where {$map} order by a.pinyinjs asc limit 10 ";
            }
        } else if ($xslx == 2) {
            $map .= " and a.lock1=0";
            $map .= " and a.status=1";
            if ($role == 1) {
                $map .= " and a.leixing=2";
            } else if ($role == 2) {
                $map .= " and a.leixing=3";
            } else {
                $map .= " and (a.leixing=2 or a.leixing=3)";
            }
            $list="select ygmingcheng from __YONGHU__ a where {$map} order by pinyinjs asc limit 10 ";
        }
        $Yonghu=M('yonghu')->query($list);
        $data=array('status'=>1,'district'=>$Yonghu);
        header("Content-type: application/json");
        exit(json_encode($data));
    }

    // 异步获取行政区新数据
    public function get_xzqxsj_ziyuan(){
        $listObj = M('xingzhengqu');
        $list = $listObj->order('id desc')->find();
        $data=array('pics'=>$list);
        header("Content-type: application/json");
        exit(json_encode($data));
    }

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

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

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

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

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

    //选择部门查询当前部门下的部门
    public function select_bumen(){
        $id = I('id');
        $bumen = M('bumen')->where(array('level'=>3,'parentid'=>$id))->field('id,bmming')->select();
        if($bumen){
            $this->ajaxReturn($bumen);
        }else{
            $this->ajaxReturn(array('status'=>0,'info'=>'当前部门下没有部门'));
        }
    }

    //查看客户回访图片
    public function ckkhhftp(){
        $kyhf=I('kyhf');
        $condition="b.gongsiid=".session('gongsiid')." and a.data_id=".$kyhf." and a.data_table='kygenjin'";
        $x = _rbac_lists_where(852,0);
        if ($x && $x != "all" && $x != "me") {
            $condition .= " and b.bumen in ({$x})";
        }elseif ($x == "me") {
            $condition .= " and b.id=".session('uid');
        }elseif ($x == false) {
            $condition .= " and b.id=0";
        }
        $list="select a.*,b.ygmingcheng from __PHOTOS__ a join __YONGHU__ b on a.adduid=b.id where {$condition} order by a.addtime DESC ";
        $photo=M('photos')->query($list);
        foreach ($photo as &$value) {
            $value['data_type'] = $value['data_type'] ? get_peizhi('hftplx',$value['data_type']) : '未设置图片类型';
        }
        $this->assign('photo',$photo);
        $this->display();
    }

    public function kydaochu() {
        $is_rbac = _rbac_node_show(931);
        if (!$is_rbac) {
            $this->redirect('Index/error');
        }
        $shijian = I('shijian');
        if ($shijian) {
            $time_s = explode(' - ',$shijian);
            $time_s1 = strtotime($time_s[0]);
            $time_s2 = strtotime($time_s[1])+86400;
            $condition = " where lurusj>=".$time_s1." and lurusj<".$time_s2;
        }
        $sql_count = "select count(*) as count from  __KEYUAN__ {$condition} ";
        $Model = new \Think\Model();
        $sqlCount = $Model->query($sql_count);
        $sqlCount = $sqlCount[0]['count'];
        if ($sqlCount == 0) {
            $this->error('无数据');
        }
        $xqbianhao = I('xqbianhao');
        if ($xqbianhao) {
            $dckhbh = 1;
        } else {
            $dckhbh = 0;
        }
        $zhuangtai = I('zhuangtai');
        if ($zhuangtai) {
            $dckhzt = 1;
        } else {
            $dckhzt = 0;
        }
        $kydengji = I('kydengji');
        if ($kydengji) {
            $dckhdj = 1;
        } else {
            $dckhdj = 0;
        }
        $khxingming = I('khxingming');
        if ($khxingming) {
            $dckhxm = 1;
        } else {
            $dckhxm = 0;
        }
        $xqxiaoqu = I('xqxiaoqu');
        if ($xqxiaoqu) {
            $dcxqxq = 1;
        } else {
            $dcxqxq = 0;
        }
        $dianhua = I('dianhua');
        if ($dianhua) {
            $dcdhyi = 1;
        } else {
            $dcdhyi = 0;
        }
        $dianhua2 = I('dianhua2');
        if ($dianhua2) {
            $dcdher = 1;
        } else {
            $dcdher = 0;
        }
        $mianji = I('mianji');
        if ($mianji) {
            $dcxqmj = 1;
        } else {
            $dcxqmj = 0;
        }
        $jiage = I('jiage');
        if ($jiage) {
            $dcxqjg = 1;
        } else {
            $dcxqjg = 0;
        }
        $huxing = I('huxing');
        if ($huxing) {
            $dcxqhx = 1;
        } else {
            $dcxqhx = 0;
        }
        $louceng = I('louceng');
        if ($louceng) {
            $dxxqlc = 1;
        } else {
            $dxxqlc = 0;
        }
        $kygjs = I('kygjs');
        if ($kygjs) {
            $dchfsl = 1;
        } else {
            $dchfsl = 0;
        }
        $zhgjsj = I('zhgjsj');
        if ($zhgjsj) {
            $dchfsj = 1;
        } else {
            $dchfsj = 0;
        }
        $kydks = I('kydks');
        if ($kydks) {
            $dclfsl = 1;
        } else {
            $dclfsl = 0;
        }
        $zhdkshijian = I('zhdkshijian');
        if ($zhdkshijian) {
            $dclfsj = 1;
        } else {
            $dclfsj = 0;
        }
        $lururen = I('lururen');
        if ($lururen) {
            $dclrrm = 1;
        } else {
            $dclrrm = 0;
        }
        $lurusj = I('lurusj');
        if ($lurusj) {
            $dclrsj = 1;
        } else {
            $dclrsj = 0;
        }
        $jingjiren = I('jingjiren');
        if ($jingjiren) {
            $dcjjrm = 1;
        } else {
            $dcjjrm = 0;
        }
        $weihuren = I('weihuren');
        if ($weihuren) {
            $dcjbrm = 1;
        } else {
            $dcjbrm = 0;
        }
        $khbeizhu = I('khbeizhu');
        if ($khbeizhu) {
            $dckhbz = 1;
        } else {
            $dckhbz = 0;
        }
        if ($dckhbh != 1 && $dckhzt != 1 && $dckhdj != 1 && $dckhxm != 1 && $dcxqxq != 1 && $dcdhyi != 1 && $dcdher != 1 && $dcxqmj != 1 && $dcxqjg != 1 && $dcxqhx != 1 && $dxxqlc != 1 && $dcxqzx != 1 && $dchfsl != 1 && $dchfsj != 1 && $dclfsl != 1 && $dclfsj != 1 && $dclrrm != 1 && $dclrsj != 1 && $dcjjrm != 1 && $dcjbrm != 1 && $dckhbz != 1) {
            $this->error('请选择至少一个导出字段！');
        }
        ini_set('max_execution_time', '0');
        $mark1 = './Upload/zip/'.time();
        if (!file_exists($mark1)){
            mkdir ($mark1,0777,true);
        }
        $mark = './Upload/zip/'.time().'/attack_ip_info';
        $fileName = "test.csv";
        set_time_limit(0);
        //p($sqlCount);die;
        //输出Excel文件头，可把user.csv换成你要的文件名
        header('Content-Type: application/vnd.ms-excel;charset=utf-8');
        header('Content-Disposition: attachment;filename="' . $fileName . '"');
        header('Cache-Control: max-age=0');
        $sqlLimit = 500;//每次只从数据库取100000条以防变量缓存太大
        //每隔$limit行，刷新一下输出buffer，不要太大，也不要太小
        $limit = 500;
        //buffer计数器
        $cnt = 0;
        $fileNameArr = array();
        //逐行取出数据，不浪费内存
        for ($i = 0; $i < ceil($sqlCount / $sqlLimit); $i++) {
            $fp = fopen($mark . '_' . $i . '.csv', 'w'); //生成临时文件
            //chmod('attack_ip_info_' . $i . '.csv',777);//修改可执行权限
            $fileNameArr[] = $mark . '_' .  $i . '.csv';
            //将数据通过fputcsv写到文件句柄
            //fputcsv($fp, $head);
            $l = $i * $sqlLimit;
            $sql = "select * from  __KEYUAN__  {$condition}  limit $l , $sqlLimit";
            $arr = $Model->query($sql);
            $dataArr = array();
            $data = array();
            if ($dckhbh == 1) {
                $data['xqbianhao'] = "编号";
            }
            if ($dckhzt == 1) {
                $data['zhuangtai'] = "状态";
            }
            if ($dckhdj == 1)  {
                $data['kydengji'] = "等级";
            }
            if ($dckhxm == 1) {
                $data['khxingming'] = "客户名称";
            }
            if ($dcxqxq == 1) {
                $data['xqxiaoqu'] = "需求小区";
            }
            if ($dcdhyi == 1) {
                $data['dianhua'] = "电话一";
            }
            if ($dcdher == 1) {
                $data['dianhua2'] = "电话二";
            }
            if ($dcxqmj == 1) {
                $data['mianji'] = "需求面积";
            }
            if ($dcxqjg == 1) {
                $data['jiage'] = "需求价格";
            }
            if ($dcxqhx == 1) {
                $data['huxing'] = "需求户型";
            }
            if ($dxxqlc == 1) {
                $data['louceng'] = "需求楼层";
            }
            if ($dchfsl == 1) {
                $data['kygjs'] = "回访数";
            }
            if ($dchfsj == 1) {
                $data['zhgjsj'] = "最后回访时间";
            }
            if ($dclfsl == 1) {
                $data['kydks'] = "来访数";
            }
            if ($dclfsj == 1) {
                $data['zhdkshijian'] = "最后来访时间";
            }
            if ($dclrrm == 1) {
                $data['lururen'] = "录入人";
            }
            if ($dclrsj == 1) {
                $data['lurusj'] = "录入时间";
            }
            if ($dcjjrm == 1) {
                $data['jingjiren'] = "经纪人";
            }
            if ($dcjbrm == 1) {
                $data['weihuren'] = "经办人";
            }
            if ($dckhbz == 1) {
                $data['beizhu'] = "备注";
            }
            $dataArr[] = $data;
            foreach ($arr as $key => $value) {
                $data = array();
                if ($dckhbh == 1) {
                    $data['xqbianhao'] = $value['xqbianhao'] ? $value['xqbianhao'] : "——";
                }
                if ($dckhzt == 1) {
                    $data['zhuangtai'] = $value['zhuangtai'] ? get_peizhi('khzhuangtai',$value['zhuangtai']) : "——";
                }
                if ($dckhdj == 1) {
                    $data['kydengji'] = $value['kydengji'] ? get_peizhi('kydengji',$value['kydengji']) : "——";
                }
                if ($dckhxm == 1) {
                    $data['khxingming'] = $value['khxingming'] ? $value['khxingming'].get_peizhi("yezhulx",$value["kehulx"]) : "——";
                }
                if ($dcxqxq == 1) {
                    $data['xqxiaoqu'] = $value['xqxiaoqu'] ? $value['xqxiaoqu'] : "——";
                }
                if ($dcdhyi == 1) {
                    $data['dianhua'] = $value['dianhua'] ? $value['dianhua'] : "——";
                }
                if ($dcdher == 1) {
                    $data['dianhua2'] = $value['dianhua2'] ? $value['dianhua2'] : "——";
                }
                if ($dcxqmj == 1) {
                    if ($value['xqmianji1'] && $value['xqmianji2']) {
                        $data['mianji'] = $value['xqmianji1'].'~'.$value['xqmianji2'].'m²';
                    } elseif ($value['xqmianji1']) {
                        $data['mianji'] = $value['xqmianji1'].'m²以上';
                    } elseif ($value['xqmianji2']) {
                        $data['mianji'] = $value['xqmianji2'].'m²以下';
                    } else {
                        $data['mianji'] = "——";
                    }
                }
                if ($dcxqjg == 1) {
                    if ($value['xqjiage1'] && $value['xqjiage2']) {
                        $data['jiage'] = $value['xqjiage1'].'-'.$value['xqjiage2'].'万';
                    } elseif ($value['xqjiage1']) {
                        $data['jiage'] = $value['xqjiage1'].'万以上';
                    } elseif ($value['xqjiage2']) {
                        $data['jiage'] = $value['xqjiage2'].'万以下';
                    } else {
                        $data['jiage'] = "——";
                    }
                }
                if ($dcxqhx == 1) {
                    if ($value['xqhuxing1'] && $value['xqhuxing2']) {
                        $data['huxing'] = $value['xqhuxing1'].'-'.$value['xqhuxing2'].'室';
                    } elseif ($value['xqhuxing1']) {
                        $data['huxing'] = $value['xqhuxing1'].'室以上';
                    } elseif ($value['xqhuxing2']) {
                        $data['huxing'] = $value['xqhuxing2'].'室以下';
                    } else {
                        $data['huxing'] = "——";
                    }
                }
                if ($dxxqlc == 1) {
                    if ($value['louceng1'] && $value['louceng2']) {
                        $data['louceng'] = $value['louceng1'].'-'.$value['louceng2'].'层';
                    } elseif ($value['louceng1']) {
                        $data['louceng'] = $value['louceng1'].'层以上';
                    } elseif ($value['louceng2']) {
                        $data['louceng'] = $value['louceng2'].'层以下';
                    } else {
                        $data['louceng'] = "——";
                    }
                }
                if ($dchfsl == 1) {
                    $kygjs=M('kygenjin')->where(array('kyid'=>$value['id']))->count();
                    $data['kygjs'] = $kygjs ? $kygjs : 0;
                }
                if ($dchfsj == 1) {
                    $data['zhgjsj'] = $value['zhgjsj'] ? date('Y-m-d H:i:s',$value['zhgjsj']) : "——";
                }
                if ($dclfsl == 1) {
                    $kydks=M('kydaikan')->where(array('kyid'=>$value['id']))->count();
                    $data['kydks'] = $kydks ? $kydks : 0;
                }
                if ($dclfsj == 1) {
                    $data['zhdkshijian'] = $value['zhdkshijian'] ? date('Y-m-d H:i:s',$value['zhdkshijian']) : "——";
                }
                if ($dclrrm == 1) {
                    $lururen = M('yonghu')->where(array('id'=>$value['lururenid']))->getField('ygmingcheng');
                    $data['lururen'] = $lururen ? $lururen : "——";
                }
                if ($dclrsj == 1) {
                    $data['lurusj'] = $value['lurusj'] ? date('Y-m-d H:i:s',$value['lurusj']) : "——";
                }
                if ($dcjjrm == 1) {
                    $data['jingjiren'] = $value['jingjiren'] ? $value['jingjiren'] : "——";
                }
                if ($dcjbrm == 1) {
                    $data['weihuren'] = $value['weihuren'] ? $value['weihuren'] : "——";
                }
                if ($dckhbz == 1) {
                    $data['beizhu'] = $value['beizhu'] ? $value['beizhu'] : "——";
                }
                $dataArr[] = $data;
            }
            foreach ($dataArr as $a) {
                $cnt++;
                if ($limit == $cnt) {
                  //刷新一下输出buffer，防止由于数据过多造成问题
                  ob_flush();
                  flush();
                  $cnt = 0;
                }
                fputcsv($fp, $a);
            }
            fclose($fp);//每生成一个文件关闭
        }
        //进行多个文件压缩
        $zip = new \ZipArchive();
        $filename = $mark . ".zip";
        $zip->open($filename, \ZipArchive::CREATE);//打开压缩包
        foreach ($fileNameArr as $file) {
           $zip->addFile($file, basename($file));//向压缩包中添加文件
        }
        $zip->close();//关闭压缩包
        foreach ($fileNameArr as $file) {
           unlink($file);//删除csv临时文件
        }
        //输出压缩文件提供下载
        header("Cache-Control: max-age=0");
        header("Content-Description: File Transfer");
        header('Content-disposition: attachment; filename=' . basename($filename));
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: binary");
        header('Content-Length: ' . filesize($filename));
        @readfile($filename);//输出文件;
        //unlink($filename); //删除压缩包临时文件
    }

}