<?php

	/***************************************************************\
	|																|
	| 开单大师（专业的房产ERP管理系统）								|
	|																|
	| © 2018 http://www.kaidandashi.com All rights reserved.		|
	|																|
	| 本系统由淮南市银泰科技软件有限公司提供技术支持				|
	|																|
	| QQ号：1264399051												|
	|																|
	\***************************************************************/

    namespace Admin\Controller;
    use Think\Controller;
    class KyneirongController extends CommonController {
        public function editKeyuan(){
            $id=I('id');
            $ky1=M('keyuan')->where(array('id'=>$id))->find();
            $kywhr=I('kywhr');
            if ($kywhr) {
                $is_rbac = _rbac_btn_show(79,0,$kywhr);
            } else {
                $is_rbac = _rbac_btn_show(79,0,$ky1['weihurenid']);
            }
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                if (session('gongsiid')=="") {
                    $this->error('请重新登录');
                }
                if ($ky1['zhuangtai'] == 3) {
                    $this->error('客源已成交！');
                }
                $iscfky = M('keyuan')->where(array('dianhua'=>I('dianhua')))->field('weihurenid,weihuren')->order('id asc')->find();
                $ygmingcheng=M('yonghu')->where(array('id'=>$ky1['weihurenid']))->getField('ygmingcheng');
                if ($ky1['weihurenid'] == session('uid')) {
                    $jdr="您";
                } else {
                    $jdr=$ygmingcheng;
                }
                if ($iscfky && ($iscfky['weihurenid'] != $ky1['weihurenid'])) {
                    $this->error(I('dianhua').'不是'.$jdr.'接待的客户！');
                }
                $dianhua=I('dianhua');
                $dianhua2=I('dianhua2');
                $xqhuxing1=I('xqhuxing1');
                $xqhuxing2=I('xqhuxing2');
                $xqjiage1=I('xqjiage1');
                $xqjiage2=I('xqjiage2');
                $xqmianji1=I('xqmianji1');
                $xqmianji2=I('xqmianji2');
                $louceng1=I('louceng1');
                $louceng2=I('louceng2');
                $xqxiaoqu=I('xqxiaoqu');
                $xqxiaoquid=I('xqxiaoquid');
                if(!preg_match("/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[8-9])\d{8}$|^0\d{2,3}-?\d{7,8}$/",$dianhua)){
                    $this->error('客户电话一请输入正确的手机号码或电话号码！');
                }
                if(!preg_match("/^$|^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[8-9])\d{8}$|^0\d{2,3}-?\d{7,8}$/",$dianhua2)){
                    $this->error('客户电话二请输入正确的手机号码或电话号码！');
                }
                if(!preg_match("/^$|^([1-9]{1})$|^(1[0]{1})$/",$xqhuxing1) || !preg_match("/^$|^([1-9]{1})$|^(1[0]{1})$/",$xqhuxing2)){
                    $this->error('户型请输入＞0≤10的整数！');
                }
                if(!preg_match("/^$|^([1-9][0-9]{0,4})$|^(1[0]{5})$/",$xqjiage1) || !preg_match("/^$|^([1-9][0-9]{0,4})$|^(1[0]{5})$/",$xqjiage2)){
                    $this->error('价格请输入＞0≤十亿的整数！');
                }
                if(!preg_match("/^$|^([1-9][0-9]{0,3})$|^(1[0]{4})$/",$xqmianji1) || !preg_match("/^$|^([1-9][0-9]{0,3})$|^(1[0]{4})$/",$xqmianji2)){
                    $this->error('面积请输入＞0≤一万的整数！');
                }
                if(!preg_match("/^$|^([1-9]{1})$|^([1-9][0-9]{1})$|^(1[0-9]{2})$|^(2[0]{2})$/",$louceng1) || !preg_match("/^$|^([1-9]{1})$|^([1-9][0-9]{1})$|^(1[0-9]{2})$|^(2[0]{2})$/",$louceng2)){
                    $this->error('楼层请输入＞0≤200的整数！');
                }
                $xqxiaoqus=array_unique(array_filter(explode("；",$xqxiaoqu)));
                $xqxiaoqums=implode(",",$xqxiaoqus);
                $xqxiaoquids=array_unique(array_filter(explode(",",$xqxiaoquid)));
                $xqxiaoqu=implode("；",$xqxiaoqus);
                $xqxiaoquid=implode(",",$xqxiaoquids);
                foreach ($xqxiaoqus as $key => $value) {
                    $xqxqid=M('xiaoqu')->where(array('xiaoqum'=>$value))->getField('id');
                    if (!in_array($xqxqid, $xqxiaoquids)) {
                        $this->error('需求小区异常错误！');
                    }
                }
                foreach ($xqxiaoquids as $key => $value) {
                    $xqxqm=M('xiaoqu')->where(array('id'=>$value))->getField('xiaoqum');
                    if (!in_array($xqxqm, $xqxiaoqus)) {
                        $this->error('需求小区异常错误！');
                    }
                }
                $user=M('yonghu')->where(array('id'=>$ky1['weihurenid']))->field('bumen,leixing')->find();
                if ($user['leixing'] != 2 and $user['leixing'] != 3) {
                    $this->error('客源经办人不是销售或者销售扩展！找不到指定销售！');
                }
                foreach ($xqxiaoquids as $key => $value) {
                    $xiaoqum=M('xiaoqu')->where(array('id'=>$value))->getField('xiaoqum');
                    $xqid1=M('xiaoqu')->where(array('bdbumen'=>$user['bumen']))->getField('id');
                    $xsid=M('xqbdxs')->where(array('uid'=>$ky1['weihurenid'],'xqid'=>$value))->getField('xsid');
                    if ((($user['leixing'] == 2) && ($value == $xqid1)) || $xsid) {
                    } else {
                        $this->error('未找到'.$xiaoqum.'的指定销售！');
                    }
                }
                if(I('jiqie')){
                    $jiqie=1;
                }else{
                    $jiqie=0;
                }
                $zx1=array_keys(I('zhuangxiu'));
                $zx2=implode(',',$zx1);
                $data['zhuangxiu']=$zx2;
                $data['jiqie']=$jiqie;
                $data['leixing']=I('leixing');
                $data['khxingming']=I('khxingming');
                $data['kehulx']=I('kehulx');
                $data['dianhua']=$dianhua;
                $data['dianhua2']=$dianhua2;
                $data['xqxiaoqu']=$xqxiaoqu;
                $data['xqxiaoquid']=$xqxiaoquid;
                $data['xqhuxing1']=$xqhuxing1;
                $data['xqhuxing2']=$xqhuxing2;
                $data['xqjiage1']=$xqjiage1;
                $data['xqjiage2']=$xqjiage2;
                $data['xqmianji1']=$xqmianji1;
                $data['xqmianji2']=$xqmianji2;
                $data['louceng1']=$louceng1;
                $data['louceng2']=$louceng2;
                $beizhu=preg_replace ( "/\s(?=\s)/","\\1", I('beizhu') );
                $data['beizhu']=$beizhu;
                $data['xiugaisj']=time();
                $data['jingjiren']=I('jingjiren');
                if ($ky1['jiqie']!=$jiqie && ($ky1['jiqie']==true || $jiqie==true)) {
                    $xin1=$ky1['xinzheng']?"是":"否";
                    $xin2=$jiqie?"是":"否";
                    $xiugai.="急切:[".$xin1."]=>[".$xin2."];";
                }
                if ($ky1['zhuangxiu']!=$zx2) {
                    if ($ky1['zhuangxiu']) {
                        $condition5="pzming = 'zhuangxiu'";
                        $condition5.=" and lxid in (".$ky1['zhuangxiu'].")";
                        $peitao5=M('peizhi')->query("select * from __PEIZHI__ where {$condition5}");
                        $goods5 = array();
                        foreach ($peitao5 AS $key => $val) {
                            $goods5[$key]['lxming'] = $val['lxming'];
                            $box5.= ",".$val['lxming'];
                        }
                    }
                    if ($zx2) {
                        $condition6="pzming = 'zhuangxiu'";
                        $condition6.=" and lxid in (".$zx2.")";
                        $peitao6=M('peizhi')->query("select * from __PEIZHI__ where {$condition6}");
                        $goods6 = array();
                        foreach ($peitao6 AS $key => $val) {
                            $goods6[$key]['lxming'] = $val['lxming'];
                            $box6.= $val['lxming'].",";
                        }
                    }
                    $xiugai.="装修:[".$box5."]=>[".$box6."];";
                }
                if ($ky1['khxingming']!=I('khxingming') && ($ky1['khxingming']==true || I('khxingming')==true)) {
                    $xiugai.="客户姓名:[".$ky1['khxingming']."]=>[".I('khxingming')."];";
                }
                if ($ky1['dianhua']!=$dianhua && ($ky1['dianhua']==true || $dianhua==true)) {
                    $xiugai.="客户电话一:[".$ky1['dianhua']."]=>[".$dianhua."];";
                }
                if ($ky1['dianhua2']!=$dianhua2 && ($ky1['dianhua2']==true || $dianhua2==true)) {
                    $xiugai.="客户电话二:[".$ky1['dianhua2']."]=>[".$dianhua2."];";
                }
                if (I('kehulx')!=$ky1['kehulx'] && ($ky1['kehulx']==true || I('kehulx')==true)) {
                    $xiugai.=get_xiugai($ky1['kehulx'],I('kehulx'),'yezhulx');
                }
                if ($ky1['jingjiren']!=I('jingjiren') && ($ky1['jingjiren']==true || I('jingjiren')==true)) {
                    $xiugai.="经纪人:[".$ky1['jingjiren']."]=>[".I('jingjiren')."];";
                }
                if ($ky1['xqxiaoqu']!=I('xqxiaoqu') && ($ky1['xqxiaoqu']==true || I('xqxiaoqu')==true)) {
                    $xiugai.="需求小区:[".$ky1['xqxiaoqu']."]=>[".I('xqxiaoqu')."];";
                }
                if ((($ky1['xqhuxing1']!=$xqhuxing1 && ($ky1['xqhuxing1']==true || $xqhuxing1==true))) || (($ky1['xqhuxing2']!=$xqhuxing2 && ($ky1['xqhuxing2']==true || $xqhuxing2==true)))){
                    $xiugai.="需求户型:[".$ky1['xqhuxing1']."~".$ky1['xqhuxing2']."]=>[".$xqhuxing1."~".$xqhuxing2."];";
                }
                if ((($ky1['xqjiage1']!=$xqjiage1 && ($ky1['xqjiage1']==true || $xqjiage1==true))) || (($ky1['xqjiage2']!=$xqjiage2 && ($ky1['xqjiage2']==true || $xqjiage2==true)))){
                    $xiugai.="需求价格:[".$ky1['xqjiage1']."~".$ky1['xqjiage2']."]=>[".$xqjiage1."~".$xqjiage2."];";
                }
                if ((($ky1['xqmianji1']!=$xqmianji1 && ($ky1['xqmianji1']==true || $xqmianji1==true))) || (($ky1['xqmianji2']!=$xqmianji2 && ($ky1['xqmianji2']==true || $xqmianji2==true)))){
                    $xiugai.="需求面积:[".$ky1['xqmianji1']."~".$ky1['xqmianji2']."]=>[".$xqmianji1."~".$xqmianji2."];";
                }
                if ((($ky1['louceng1']!=$louceng1 && ($ky1['louceng1']==true || $louceng1==true))) || (($ky1['louceng2']!=$louceng2 && ($ky1['louceng2']==true || $louceng2==true)))){
                    $xiugai.="需求楼层:[".$ky1['louceng1']."~".$ky1['louceng2']."]=>[".$louceng1."~".$louceng2."];";
                }
                if ($ky1['beizhu']!=I('beizhu') && ($ky1['beizhu']==true || I('beizhu')==true)) {
                    $xiugai.="备注:[".$ky1['beizhu']."]=>[".I('beizhu')."];";
                }
                $data_rizhi=array(
                    'zy_type'=>2,
                    'uid'=>session('uid'),
                    'addtime'=>time(),
                    'cz_type'=>2,
                    'zy_bianhao'=>$ky1['xqbianhao'],
                    'zy_id'=>$ky1['id'],
                    'neirong'=>$xiugai,
                );
                $isok=M("keyuan")->where(array('id'=>$id))->save($data);
                if ($isok){
                    M('fzky')->where(array('lzxqxq'=>1,'kyid'=>$id))->delete();
                    foreach ($xqxiaoquids as $key => $value) {
                        $xqid1=M('xiaoqu')->where(array('bdbumen'=>$user['bumen']))->getField('id');
                        $xsid=M('xqbdxs')->where(array('uid'=>$ky1['weihurenid'],'xqid'=>$value))->getField('xsid');
                        $fzkyid=M('fzky')->where(array('weihurenid'=>$xsid,'kyid'=>$id))->getField('id');
                        if (!$fzkyid) {
                            if (($user['leixing'] == 3 && $xsid) || (($user['leixing'] == 2) && ($value != $xqid1) && $xsid)) {
                                $data_keyuan['lzxqxq']=1;
                                $data_keyuan['kyid']=$id;
                                $data_keyuan['xqid']=$value;
                                $data_keyuan['weihurenid']=$xsid;
                                $data_keyuan['addtime']=time();
                                $data_keyuan['adduid']=session('uid');
                                $data_keyuan['gongsiid']=session('gongsiid');
                                M('fzky')->add($data_keyuan);
                            }
                        }
                    }
                    M('rizhi')->add($data_rizhi);
                    $this->success('更新成功');
                }else{
                    $this->error('更新失败');
                }
            }else{
                $keyuan=M('Keyuan')->where(array('id'=>$id,'gongsiid'=>session('gongsiid')))->find();
                $this->keyuan=$keyuan;
                $this->kehulx=M('Peizhi')->where(array('pzming'=>'yezhulx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->zhuangxiu=M('Peizhi')->where(array('pzming'=>'zhuangxiu','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $zhuangxiu1=M('keyuan')->where(array('id'=>$id,'gongsiid'=>session('gongsiid')))->getField('zhuangxiu');
                $this->zhuangxiu2=explode(",", $zhuangxiu1);
                $this->display();
            }
        }
        //设置意向房屋
        public function szyxfw(){
            $keyuan=M('keyuan')->where(array('id'=>I('kyid')))->find();
            $kywhr=I('kywhr');
            if ($kywhr) {
                $is_rbac = _rbac_btn_show(863,0,$kywhr);
            } else {
                $is_rbac = _rbac_btn_show(863,0,$keyuan['weihurenid']);
            }
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $user=M('yonghu')->where(array('id'=>$keyuan['weihurenid']))->field('bumen,leixing')->find();
                if ($user['leixing'] != 2 and $user['leixing'] != 3) {
                    $this->error('客源经办人不是销售或者销售扩展！找不到指定销售！');
                }
                $newstr = substr(I('yxfangwu'),0,strlen($str)-1);
                $aaa=explode(';',$newstr);
                $yxfw=array_unique($aaa);
                foreach ($yxfw as $key => $value) {
                    $xqid=M('fjxinxi')->where(array('bianhao'=>$value))->getField('xqid');
                    $xqid1=M('xiaoqu')->where(array('bdbumen'=>$user['bumen']))->getField('id');
                    $xsid=M('xqbdxs')->where(array('uid'=>$keyuan['weihurenid'],'xqid'=>$xqid))->getField('xsid');
                    $yxid=M('kyyxfw')->where(array('fwbh'=>$value,'kyid'=>$keyuan['id']))->getField('id');
                    if ((($user['leixing'] == 2) && ($xqid == $xqid1)) || $xsid) {
                    } else {
                        $this->error('未找到该小区（房屋'.$value.'）的指定销售！');
                    }
                    if ($yxid) {
                        $this->error($value.'已经是客源'.$keyuan['xqbianhao'].'的意向房屋！');
                    }
                }
                foreach ($yxfw as $key => $value) {
                    $fwid=M('fjxinxi')->where(array('bianhao'=>$value))->getField('id');
                    $xqid=M('fjxinxi')->where(array('bianhao'=>$value))->getField('xqid');
                    $xqid1=M('xiaoqu')->where(array('bdbumen'=>$user['bumen']))->getField('id');
                    $xsid=M('xqbdxs')->where(array('uid'=>$keyuan['weihurenid'],'xqid'=>$xqid))->getField('xsid');
                    $fzkyid=M('fzky')->where(array('weihurenid'=>$xsid,'kyid'=>$keyuan['id']))->getField('id');
                    if (!$fzkyid) {
                        if (($user['leixing'] == 3 && $xsid) || (($user['leixing'] == 2) && ($xqid != $xqid1) && $xsid)) {
                            $data_keyuan['kyid']=$keyuan['id'];
                            $data_keyuan['xqid']=$xqid;
                            $data_keyuan['weihurenid']=$xsid;
                            $data_keyuan['addtime']=time();
                            $data_keyuan['adduid']=session('uid');
                            $data_keyuan['gongsiid']=session('gongsiid');
                            $isok=M('fzky')->add($data_keyuan);
                        }
                    }
                    $data['kyid']=$keyuan['id'];
                    $data['fwid']=$fwid;
                    $data['fwbh']=$value;
                    $data['kybh']=$keyuan['xqbianhao'];
                    $data['addtime']=time();
                    $data['adduid']=session('uid');
                    $data['gongsiid']=session('gongsiid');
                    $isok=M('kyyxfw')->add($data);
                }
                if ($isok){
                    M('keyuan')->where(array('id'=>$keyuan['id']))->setField('xiugaisj',time());
                    $data_rizhi['zy_type']=2;
                    $data_rizhi['uid']=session("uid");
                    $data_rizhi['addtime']=time();
                    $data_rizhi['cz_type']=26;
                    $data_rizhi['zy_id']=$keyuan['id'];
                    $data_rizhi['zy_bianhao']=$keyuan['xqbianhao'];
                    $data_rizhi['neirong']=I('yxfangwu');
                    M('rizhi')->add($data_rizhi);
                }
                $this->success('添加成功');
            }else{
                $this->display();
            }
        }
        //写回访
        public function genjin(){
            $keyuan=M('keyuan')->where(array('id'=>I('kyid')))->find();
            $kywhr=I('kywhr');
            if ($kywhr) {
                $is_rbac = _rbac_btn_show(85,0,$kywhr);
            } else {
                $is_rbac = _rbac_btn_show(85,0,$keyuan['weihurenid']);
            }
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $data['kybh']=$keyuan['xqbianhao'];
                $data['kyid']=$keyuan['id'];
                $data['genjinfs']=I('genjinfs');
                $data['gjneirong']=I('gjneirong');
                $data['gjshijian']=strtotime(I('gjshijian'));
                $data['time']=time();
                $data['gjuid']=session('uid');
                $data['gongsiid']=session('gongsiid');
                $isok = M('kygenjin')->add($data);
                if ($isok) {
                    $data_kehu['genjin']=1;
                    $data_kehu['xiugaisj']=time();
                    M('keyuan')->where(array('id'=>$keyuan['id']))->save($data_kehu);
                    $data_rizhi['zy_type']=2;
                    $data_rizhi['uid']=session("uid");
                    $data_rizhi['addtime']=time();
                    $data_rizhi['cz_type']=5;
                    $data_rizhi['zy_id']=$keyuan['id'];
                    $data_rizhi['zy_bianhao']=$keyuan['xqbianhao'];
                    $neirong=I('gjneirong');
                    $data_rizhi['neirong']=$neirong;
                    M('rizhi')->add($data_rizhi);
                    $data_genjin['zhgjsj']=time();
                    $data_genjin['xiugaisj']=time();
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->genjinfs=M('Peizhi')->where(array('pzming'=>'genjinfs','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->display();
            }
        }
        //写提醒
        public function tixing(){
            $keyuan=M('keyuan')->where(array('id'=>I('kyid')))->find();
            $kywhr=I('kywhr');
            if ($kywhr) {
                $is_rbac = _rbac_btn_show(302,0,$kywhr);
            } else {
                $is_rbac = _rbac_btn_show(302,0,$keyuan['weihurenid']);
            }
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $data['kybh']=$keyuan['xqbianhao'];
                $data['kyid']=$keyuan['id'];
                $data['txuid']=I('txuid');
                $data['txshijian']=strtotime(I('txshijian'));
                $data['txneirong']=I('txneirong');
                $data['time']=time();
                $data['adduid']=session('uid');
                $data['gongsiid']=session('gongsiid');
                $isok=M('kytixing')->add($data);
                if ($isok) {
                    M('keyuan')->where(array('id'=>$keyuan['id']))->setField('xiugaisj',time());
                    $data_rizhi['zy_type']=2;
                    $data_rizhi['uid']=session("uid");
                    $data_rizhi['addtime']=time();
                    $data_rizhi['cz_type']=16;
                    $data_rizhi['zy_id']=$keyuan['id'];
                    $data_rizhi['zy_bianhao']=$keyuan['xqbianhao'];
                    $neirong=I('txneirong');
                    $data_rizhi['neirong']=$neirong;
                    M('rizhi')->add($data_rizhi);
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->ygmingcheng=M('Yonghu')->where(array('gongsiid'=>session('gongsiid')))->select();
                $this->display();
            }
        }
        //写带看
        public function daikan(){
            $id=I('kyid');
            $keyuan=M('keyuan')->where(array('id'=>$id))->find();
            $kywhr=I('kywhr');
            if ($kywhr) {
                $is_rbac = _rbac_btn_show(87,0,$kywhr);
            } else {
                $is_rbac = _rbac_btn_show(87,0,$keyuan['weihurenid']);
            }
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $newstr = substr(I('dkfangyuan'),0,strlen($str)-1);
                $aaa=explode(';',$newstr);
                $dkfw=array_unique($aaa);
                foreach ($dkfw as $key => $value) {
                    $fyid=M('fjxinxi')->where(array('bianhao'=>$value))->getField('id');
                    $data['kyid']=$id;
                    $data['fyid']=$fyid;
                    $data['fybh']=$value;
                    $data['kybh']=$keyuan['xqbianhao'];
                    $data['kehupj']=I('kehupj');
                    $data['peikanid']=I('peikanid');
                    $data['time']=time();
                    $data['dkuid']=session('uid');
                    $data['gongsiid']=session('gongsiid');
                    $data['dkshijian']=strtotime(I('dkshijian'));
                    M('kydaikan')->add($data);
                }
                $map['daikan']=1;
                $isok=M('Keyuan')->where(array('id'=>$id))->save($map);
                if ($isok){
                    M('keyuan')->where(array('id'=>$id))->setField('xiugaisj',time());
                    $data_rizhi['zy_type']=2;
                    $data_rizhi['uid']=session("uid");
                    $data_rizhi['addtime']=time();
                    $data_rizhi['cz_type']=17;
                    $data_rizhi['zy_id']=$id;
                    $data_rizhi['zy_bianhao']=$keyuan['xqbianhao'];
                    $neirong="客户评价：".I('kehupj');
                    $data_rizhi['neirong']=$neirong;
                    M('rizhi')->add($data_rizhi);
                }
                $this->success('添加成功');
            }else{
                $this->display();
            }
        }
        //变更维护人
        public function bgwhr(){
            $keyuan=M('keyuan')->where(array('id'=>I('id')))->find();
            $kywhr=I('kywhr');
            if ($kywhr) {
                $is_rbac = _rbac_btn_show(89,0,$kywhr);
            } else {
                $is_rbac = _rbac_btn_show(89,0,$keyuan['weihurenid']);
            }
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                if ($keyuan['zhuangtai'] == 3) {
                    $this->error('客源已成交！');
                }
                $keyuan1=M("keyuan")->where(array('id'=>I('id')))->find();
                $ysyr=M('yonghu')->where(array('id'=>$keyuan1['weihurenid']))->getField('ygmingcheng');
                $xsyr=M('yonghu')->where(array('id'=>I('weihurenid')))->getField('ygmingcheng');
                $data['weihurenid']=I('weihurenid');
                $data['weihuren']=$xsyr;
                $data['xiugaisj']=time();
                $isok=M("keyuan")->where(array('id'=>I('id')))->save($data);
                if ($isok){
                    $data_rizhi['zy_type']=2;
                    $data_rizhi['uid']=session("uid");
                    $data_rizhi['addtime']=time();
                    $data_rizhi['cz_type']=9;
                    $data_rizhi['zy_id']=I('id');
                    $data_rizhi['zy_bianhao']=$keyuan1['xqbianhao'];
                    $neirong="变更维护人:[".$ysyr."]=>[".$xsyr."]";
                    $data_rizhi['neirong']=$neirong;
                    M('rizhi')->add($data_rizhi);
                    $this->success('修改成功');
                }else{
                    $this->error('修改失败');
                }
            } else {
                $keyuan=M('Keyuan')->where(array('id'=>I('id'),'gongsiid'=>session('gongsiid')))->find();
                $this->assign('keyuan',$keyuan);
                $this->ygmingcheng=M('Yonghu')->where(array('gongsiid'=>session('gongsiid'),'lock1'=>0,'status'=>1,'leixing'=>2))->select();
                $this->weihu=M('Yonghu')->where(array('id'=>$keyuan['weihurenid']))->getField('ygmingcheng');
                $this->display();
            }
        }
        //修改客户详细信息
        public function xgkhxxxx(){
            $keyuan=M('keyuan')->where(array('id'=>I('id')))->find();
            $kywhr=I('kywhr');
            if ($kywhr) {
                $is_rbac = _rbac_btn_show(438,0,$kywhr);
            } else {
                $is_rbac = _rbac_btn_show(438,0,$keyuan['weihurenid']);
            }
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $keyuan=M("keyuan");
                $ah1=array_keys(I('khaihao'));
                $ah2=implode(',',$ah1);
                $data['khaihao']=$ah2;
                $ph1=array_keys(I('khjtzcph'));
                $ph2=implode(',',$ph1);
                $data['khjtzcph']=$ph2;
                $sx1=array_keys(I('khjqzdsx'));
                $sx2=implode(',',$sx1);
                $data['khjqzdsx']=$sx2;
                $data['khzhiwu']=I('khzhiwu');
                $data['khbeizhu']=I('khbeizhu');
                $data['khjiaoyu']=I('khjiaoyu');
                $data['khhunyin']=I('khhunyin');
                $data['khshouru']=I('khshouru');
                $data['khjzquyu']=I('khjzquyu');
                $data['khgzquyu']=I('khgzquyu');
                $data['khnianling']=I('khnianling');
                $data['khjzxiaoqu']=I('khjzxiaoqu');
                $data['khjzmianji']=I('khjzmianji');
                $data['khsjpinpai']=I('khsjpinpai');
                $data['khsshangye']=I('khsshangye');
                $data['khsfbeizhu']=I('khsfbeizhu');
                $data['khsjxinghao']=I('khsjxinghao');
                $data['khjtchengyuan']=I('khjtchengyuan');
                $data['xiugaisj']=time();
                $ky1=$keyuan->where(array('id'=>I('id')))->find();
                $isok=$keyuan->where(array('id'=>I('id')))->save($data);
                if ($ky1['khaihao']!=$ah2) {
                    if ($ky1['khaihao']) {
                        $condition1="pzming = 'khaihao'";
                        $condition1.=" and lxid in (".$ky1['khaihao'].")";
                        $peitao1=M('peizhi')->query("select * from __PEIZHI__ where {$condition1}");
                        $goods1 = array();
                        foreach ($peitao1 AS $key => $val)
                        {
                            $goods1[$key]['lxming'] = $val['lxming'];
                            $box1.= ",".$val['lxming'];
                        }
                    }
                    if ($ah2) {
                        $condition2="pzming = 'khaihao'";
                        $condition2.=" and lxid in (".$ah2.")";
                        $peitao2=M('peizhi')->query("select * from __PEIZHI__ where {$condition2}");
                        $goods2 = array();
                        foreach ($peitao2 AS $key => $val)
                        {
                            $goods2[$key]['lxming'] = $val['lxming'];
                            $box2.= $val['lxming'].",";
                        }
                    }
                    $xiugai.="个人爱好:[".$box1."]=>[".$box2."];";
                }
                if ($ky1['khjtzcph']!=$ph2) {
                    if ($ky1['khjtzcph']) {
                        $condition3="pzming = 'khjtzcph'";
                        $condition3.=" and lxid in (".$ky1['khjtzcph'].")";
                        $peitao3=M('peizhi')->query("select * from __PEIZHI__ where {$condition3}");
                        $goods3 = array();
                        foreach ($peitao3 AS $key => $val)
                        {
                            $goods3[$key]['lxming'] = $val['lxming'];
                            $box3.= ",".$val['lxming'];
                        }
                    }
                    if ($ph2) {
                        $condition4="pzming = 'khjtzcph'";
                        $condition4.=" and lxid in (".$ph2.")";
                        $peitao4=M('peizhi')->query("select * from __PEIZHI__ where {$condition4}");
                        $goods4 = array();
                        foreach ($peitao4 AS $key => $val)
                        {
                            $goods4[$key]['lxming'] = $val['lxming'];
                            $box4.= $val['lxming'].",";
                        }
                    }
                    $xiugai.="支出偏好:[".$box3."]=>[".$box4."];";
                }
                if ($ky1['khjqzdsx']!=$sx2) {
                    if ($ky1['khjqzdsx']) {
                        $condition5="pzming = 'khjqzdsx'";
                        $condition5.=" and lxid in (".$ky1['khjqzdsx'].")";
                        $peitao5=M('peizhi')->query("select * from __PEIZHI__ where {$condition5}");
                        $goods5 = array();
                        foreach ($peitao5 AS $key => $val)
                        {
                            $goods5[$key]['lxming'] = $val['lxming'];
                            $box5.= ",".$val['lxming'];
                        }
                    }
                    if ($sx2) {
                        $condition6="pzming = 'khjqzdsx'";
                        $condition6.=" and lxid in (".$sx2.")";
                        $peitao6=M('peizhi')->query("select * from __PEIZHI__ where {$condition6}");
                        $goods6 = array();
                        foreach ($peitao6 AS $key => $val)
                        {
                            $goods6[$key]['lxming'] = $val['lxming'];
                            $box6.= $val['lxming'].",";
                        }
                    }
                    $xiugai.="近期重大事项:[".$box5."]=>[".$box6."];";
                }
                if (I('khzhiwu')!=$ky1['khzhiwu'] && ($ky1['khzhiwu']==true || I('khzhiwu')==true)) {
                    $xiugai.=get_xiugai($ky1['khzhiwu'],I('khzhiwu'),'khzhiwu');
                }
                if ($ky1['khbeizhu']!=I('khbeizhu') && ($ky1['khbeizhu']==true || I('khbeizhu')==true)) {
                    $xiugai.="备注:[".$ky1['khbeizhu']."]=>[".I('khbeizhu')."];";
                }
                if (I('khjiaoyu')!=$ky1['khjiaoyu'] && ($ky1['khjiaoyu']==true || I('khjiaoyu')==true)) {
                    $xiugai.=get_xiugai($ky1['khjiaoyu'],I('khjiaoyu'),'khjiaoyu');
                }
                if (I('khhunyin')!=$ky1['khhunyin'] && ($ky1['khhunyin']==true || I('khhunyin')==true)) {
                    $xiugai.=get_xiugai($ky1['khhunyin'],I('khhunyin'),'khhunyin');
                }
                if (I('khshouru')!=$ky1['khshouru'] && ($ky1['khshouru']==true || I('khshouru')==true)) {
                    $xiugai.=get_xiugai($ky1['khshouru'],I('khshouru'),'khshouru');
                }
                if (I('khjzquyu')!=$ky1['khjzquyu'] && ($ky1['khjzquyu']==true || I('khjzquyu')==true)) {
                    $xiugai.=get_xiugai($ky1['khjzquyu'],I('khjzquyu'),'khjzquyu');
                }
                if (I('khgzquyu')!=$ky1['khgzquyu'] && ($ky1['khgzquyu']==true || I('khgzquyu')==true)) {
                    $xiugai.=get_xiugai($ky1['khgzquyu'],I('khgzquyu'),'khgzquyu');
                }
                if (I('khnianling')!=$ky1['khnianling'] && ($ky1['khnianling']==true || I('khgzquyu')==true)) {
                    $xiugai.=get_xiugai($ky1['khnianling'],I('khnianling'),'khnianling');
                }
                if ($ky1['khjzxiaoqu']!=I('khjzxiaoqu') && ($ky1['khjzxiaoqu']==true || I('khjzxiaoqu')==true)) {
                    $xiugai.="居住小区:[".$ky1['khjzxiaoqu']."]=>[".I('khjzxiaoqu')."];";
                }
                if (I('khjzmianji')!=$ky1['khjzmianji'] && ($ky1['khjzmianji']==true || I('khjzmianji')==true)) {
                    $xiugai.=get_xiugai($ky1['khjzmianji'],I('khjzmianji'),'khjzmianji');
                }
                if (I('khsjpinpai')!=$ky1['khsjpinpai'] && ($ky1['khsjpinpai']==true || I('khsjpinpai')==true)) {
                    $xiugai.=get_xiugai($ky1['khsjpinpai'],I('khsjpinpai'),'khsjpinpai');
                }
                if (I('khsshangye')!=$ky1['khsshangye'] && ($ky1['khsshangye']==true || I('khsshangye')==true)) {
                    $xiugai.=get_xiugai($ky1['khsshangye'],I('khsshangye'),'khsshangye');
                }
                if ($ky1['khsfbeizhu']!=I('khsfbeizhu') && ($ky1['khsfbeizhu']==true || I('khsfbeizhu')==true)) {
                    $xiugai.="身份备注:[".$ky1['khsfbeizhu']."]=>[".I('khsfbeizhu')."];";
                }
                if ($ky1['khsjxinghao']!=I('khsjxinghao') && ($ky1['khsjxinghao']==true || I('khsjxinghao')==true)) {
                    $xiugai.="手机型号:[".$ky1['khsjxinghao']."]=>[".I('khsjxinghao')."];";
                }
                if ($ky1['khjtchengyuan']!=I('khjtchengyuan') && ($ky1['khjtchengyuan']==true || I('khjtchengyuan')==true)) {
                    $xiugai.="家庭成员:[".$ky1['khjtchengyuan']."]=>[".I('khjtchengyuan')."];";
                }
                $data_rizhi=array(
                        'zy_type'=>2,
                        'uid'=>session('uid'),
                        'addtime'=>time(),
                        'cz_type'=>7,
                        'zy_bianhao'=>$ky1['xqbianhao'],
                        'zy_id'=>$ky1['id'],
                        'neirong'=>$xiugai,
                    );

                if ($isok){
                    M('rizhi')->add($data_rizhi);
                    $this->success('修改成功');
                }else{
                    $this->error('修改失败');
                }
            } else {
                $this->keyuan=M('keyuan')->where(array('id'=>I('id'),'gongsiid'=>session('gongsiid')))->find();
                $this->khaihao=M('peizhi')->where(array('pzming'=>'khaihao','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khzhiwu=M('peizhi')->where(array('pzming'=>'khzhiwu','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khjiaoyu=M('peizhi')->where(array('pzming'=>'khjiaoyu','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khhunyin=M('peizhi')->where(array('pzming'=>'khhunyin','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khshouru=M('peizhi')->where(array('pzming'=>'khshouru','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khjzquyu=M('peizhi')->where(array('pzming'=>'khjzquyu','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khgzquyu=M('peizhi')->where(array('pzming'=>'khgzquyu','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khjtzcph=M('peizhi')->where(array('pzming'=>'khjtzcph','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khjqzdsx=M('peizhi')->where(array('pzming'=>'khjqzdsx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khnianling=M('peizhi')->where(array('pzming'=>'khnianling','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khjzmianji=M('peizhi')->where(array('pzming'=>'khjzmianji','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khsjpinpai=M('peizhi')->where(array('pzming'=>'khsjpinpai','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->khsshangye=M('peizhi')->where(array('pzming'=>'khsshangye','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $id=I('id');
                $khaihao1=M('keyuan')->where(array('id'=>$id,'gongsiid'=>session('gongsiid')))->getField('khaihao');
                $this->khaihao2=explode(",", $khaihao1);
                $khjtzcph1=M('keyuan')->where(array('id'=>$id,'gongsiid'=>session('gongsiid')))->getField('khjtzcph');
                $this->khjtzcph2=explode(",", $khjtzcph1);
                $khjqzdsx1=M('keyuan')->where(array('id'=>$id,'gongsiid'=>session('gongsiid')))->getField('khjqzdsx');
                $this->khjqzdsx2=explode(",", $khjqzdsx1);
                $this->display();
            }
        }
        //查看客户详细信息
        public function ckkhxxxx(){
            $keyuan=M('keyuan')->where(array('id'=>I('id')))->find();
            $kywhr=I('kywhr');
            if ($kywhr) {
                $is_rbac = _rbac_btn_show(440,0,$kywhr);
            } else {
                $is_rbac = _rbac_btn_show(440,0,$keyuan['weihurenid']);
            }
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $id=I('id');
            $khaihao1=M('keyuan')->where(array('id'=>$id))->getField('khaihao');
            if ($khaihao1) {
                $condition="pzming = 'khaihao'";
                $condition.=" and lxid in ($khaihao1)";
                $this->khaihao=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
            }
            $khjtzcph1=M('keyuan')->where(array('id'=>$id))->getField('khjtzcph');
            if ($khjtzcph1) {
                $condition="pzming = 'khjtzcph'";
                $condition.=" and lxid in ($khjtzcph1)";
                $this->khjtzcph=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
            }
            $khjqzdsx1=M('keyuan')->where(array('id'=>$id))->getField('khjqzdsx');
            if ($khjqzdsx1) {
                $condition="pzming = 'khjqzdsx'";
                $condition.=" and lxid in ($khjqzdsx1)";
                $this->khjqzdsx=M('peizhi')->query("select * from __PEIZHI__ where {$condition}");
            }
            $keyuan=M('keyuan')->where(array('id'=>I('id'),'gongsiid'=>session('gongsiid')))->find();
            $this->keyuan=$keyuan;
            $this->khzhiwu=M('peizhi')->where(array('pzming'=>'khzhiwu','lxid'=>$keyuan['khzhiwu']))->getField('lxming');
            $this->khjiaoyu=M('peizhi')->where(array('pzming'=>'khjiaoyu','lxid'=>$keyuan['khjiaoyu']))->getField('lxming');
            $this->khhunyin=M('peizhi')->where(array('pzming'=>'khhunyin','lxid'=>$keyuan['khhunyin']))->getField('lxming');
            $this->khshouru=M('peizhi')->where(array('pzming'=>'khshouru','lxid'=>$keyuan['khshouru']))->getField('lxming');
            $this->khjzquyu=M('peizhi')->where(array('pzming'=>'khjzquyu','lxid'=>$keyuan['khjzquyu']))->getField('lxming');
            $this->khgzquyu=M('peizhi')->where(array('pzming'=>'khgzquyu','lxid'=>$keyuan['khgzquyu']))->getField('lxming');
            $this->khnianling=M('peizhi')->where(array('pzming'=>'khnianling','lxid'=>$keyuan['khnianling']))->getField('lxming');
            $this->khjzmianji=M('peizhi')->where(array('pzming'=>'khjzmianji','lxid'=>$keyuan['khjzmianji']))->getField('lxming');
            $this->khsjpinpai=M('peizhi')->where(array('pzming'=>'khsjpinpai','lxid'=>$keyuan['khsjpinpai']))->getField('lxming');
            $this->khsshangye=M('peizhi')->where(array('pzming'=>'khsshangye','lxid'=>$keyuan['khsshangye']))->getField('lxming');
            $this->display();
        }
        public function delkeyuan(){
            $id=I('id');
            $keyuan=M('keyuan')->where(array('id'=>$id))->find();
            $kywhr=I('kywhr');
            if ($kywhr) {
                $is_rbac = _rbac_btn_show(501,0,$kywhr);
            } else {
                $is_rbac = _rbac_btn_show(501,0,$keyuan['weihurenid']);
            }
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $id=I('id');
            $isok=M('keyuan')->where(array('id'=>$id))->setField('isdel',1);
            if($isok){
                M('keyuan')->where(array('id'=>$id))->setField('xiugaisj',time());
                $data_rizhi['zy_type']=2;
                $data_rizhi['uid']=session("uid");
                $data_rizhi['addtime']=time();
                $data_rizhi['cz_type']=8;
                $data_rizhi['zy_id']=$id;
                $data_rizhi['zy_bianhao']=$keyuan['xqbianhao'];
                $neirong="删除";
                $data_rizhi['neirong']=$neirong;
                M('rizhi')->add($data_rizhi);
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
        public function huifufy(){
            $id=I('id');
            $keyuan=M('keyuan')->where(array('id'=>$id))->find();
            $is_rbac = _rbac_btn_show(771,0,$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if($keyuan['isdel'] == 2){
                $this->error('客户已还原！');
            }
            $isok=M('keyuan')->where(array('id'=>$id))->setField('isdel',2);
            if ($isok) {
                M('keyuan')->where(array('id'=>$id))->setField('xiugaisj',time());
                $data_rizhi['zy_type']=2;
                $data_rizhi['uid']=session("uid");
                $data_rizhi['addtime']=time();
                $data_rizhi['cz_type']=8;
                $data_rizhi['zy_id']=$id;
                $data_rizhi['zy_bianhao']=$keyuan['xqbianhao'];
                $neirong="还原删除客户";
                $data_rizhi['neirong']=$neirong;
                M('rizhi')->add($data_rizhi);
                $this->success('还原成功');
            }else{
                $this->error('还原失败');
            }
        }
        public function yjdelfy(){
            $id=I('id');
            $keyuan=M('keyuan')->where(array('id'=>$id))->find();
            $is_rbac = _rbac_btn_show(772,0,$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $isok=M('keyuan')->where(array('id'=>$id))->delete();
            if ($isok) {
                $data_rizhi['zy_type']=2;
                $data_rizhi['uid']=session("uid");
                $data_rizhi['addtime']=time();
                $data_rizhi['cz_type']=8;
                $data_rizhi['zy_id']=$id;
                $data_rizhi['zy_bianhao']=$keyuan['xqbianhao'];
                $neirong="永久删除";
                $data_rizhi['neirong']=$neirong;
                M('rizhi')->add($data_rizhi);
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
        //上传回访图片
        public function schftp(){
            $kyhf=I('kyhf');
            $kygenjin=M('kygenjin')->where(array('id'=>$kyhf))->field('kybh,kyid,gjuid')->find();
            $is_rbac = _rbac_btn_show(851,0,$kygenjin['gjuid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $path = "./Upload/Genjin/".session('gongsiid')."/".$kygenjin['kybh'];
                if (!file_exists($path)){
                    mkdir($path);
                }
                $upload = new \Think\Upload();
                $upload->maxSize    =   5242880 ;
                $upload->exts       =   array('jpg','jpeg','png');
                $upload->rootPath   =   $path.'/';
                $upload->savePath   =   '';
                $upload->autoSub    =   false;
                $info = $upload->upload();
                if (!$info) {
                    $this->error($upload->getError());
                } else {
                    $data['gongsiid'] = session('gongsiid');
                    $data['adduid'] = session('uid');
                    $data['addtime'] = time();
                    $data['data_id'] = $kyhf;
                    $data['data_table'] = "kygenjin";
                    $data['img_path'] = '/Upload/Genjin/'.session('gongsiid').'/'.$kygenjin['kybh'].'/'.$info['file']['savename'];
                    $data['img_name'] = $info['file']['savename'];
                    $isok=M('photos')->add($data);
                    $xiugai.="上传了客户".$kygenjin['kybh']."的回访图片";
                    $data_rizhi['zy_type']=2;
                    $data_rizhi['uid']=session("uid");
                    $data_rizhi['addtime']=time();
                    $data_rizhi['cz_type']=12;
                    $data_rizhi['zy_id']=$kyhf;
                    $data_rizhi['neirong']=$xiugai;
                    M('rizhi')->add($data_rizhi);
                    M('keyuan')->where(array('id'=>$kygenjin['kyid']))->setField('xiugaisj',time());
                    $this->success('上传成功');
                }
            }else{
                $this->kyhf=$kyhf;
                $this->display();
            }
        }
        //查看回访图片
        public function ckhftp(){
            $kyhf=I('kyhf');
            $map="gongsiid=".session('gongsiid');
            $x = _rbac_lists_where(852,0);
            if ($x && $x != "all" && $x != "me") {
                $map .= " and bumen in ({$x})";
            }elseif ($x == "me") {
                $map .= " and id=".session('uid');
            }elseif ($x == false) {
                $map .= " and id=0";
            }
            $condition="gongsiid=".session('gongsiid');
            $condition.=" and data_id=$kyhf";
            $condition.=" and data_table='kygenjin'";
            $list="select * from (select * from __PHOTOS__ where {$condition}) a join (select id as yhid,ygmingcheng from __YONGHU__ where {$map}) b on a.adduid=b.yhid order by a.addtime DESC ";
            $photo=M('photos')->query($list);
            if ($photo) {
                $pzming='hftplx';
            }
            foreach ($photo as &$value) {
                if ($value['data_type']) {
                    $temp = get_peizhi($pzming,$value['data_type']);
                }
                $value['data_type'] = $temp?$temp:'未设置图片类型';
            }
            $this->assign('photo',$photo);
            $this->display();
        }
        //删除回访图片
        public function hftp_pic_del(){
            $id=I('hftp_pic_id');
            $hftp=M('photos')->where(array('id'=>$id))->field('adduid,img_path,data_id')->find();
            $kybh = M('kygenjin')->where(array('id'=>$hftp['data_id']))->getField('kybh');
            $is_rbac = _rbac_btn_show(853,0,$hftp['adduid']);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            if ($hftp){
                unlink('.'.$hftp['img_path']);
                M('photos')->where(array('id'=>$id))->delete();
                $xiugai.="删除了客户".$kybh."的回访图片";
                $data_rizhi['zy_type']=2;
                $data_rizhi['uid']=session("uid");
                $data_rizhi['addtime']=time();
                $data_rizhi['cz_type']=13;
                $data_rizhi['zy_id']=$hftp['data_id'];
                $data_rizhi['neirong']=$xiugai;
                M('rizhi')->add($data_rizhi);
                M('keyuan')->where(array('xqbianhao'=>$kybh))->setField('xiugaisj',time());
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
        //批量删除回访图片
        public function hftp_pic_dels(){
            $ids=I('hftp_pic_ids');
            foreach ($ids as $key => $value) {
                $hftp = M('photos')->where(array('id'=>$ids[$key]))->field('adduid,data_id')->find();
                $is_rbac = _rbac_btn_show(854,0,$hftp['adduid']);
                if (!$is_rbac) {
                    $this->error("您选择了权限范围之外的图片！");
                }
                $hfid = $hftp['data_id'];
                $kybh = M('kygenjin')->where(array('id'=>$hftp['data_id']))->getField('kybh');
            }
            if(is_array($ids)){
                $where = 'id in('.implode(',',$ids).')';
                $dp=M('photos');
                foreach ($ids as $value) {
                    $plist = $dp->where(array('id'=>$value))->find();
                    unlink('.'.$plist['img_path']);
                }
                $dp->where($where)->delete();
                $xiugai.="删除了客户".$kybh."的".count($ids)."张回访图片";
                $data_rizhi['zy_type']=2;
                $data_rizhi['uid']=session("uid");
                $data_rizhi['addtime']=time();
                $data_rizhi['cz_type']=13;
                $data_rizhi['zy_id']=$hfid;
                $data_rizhi['neirong']=$xiugai;
                M('rizhi')->add($data_rizhi);
                M('keyuan')->where(array('xqbianhao'=>$kybh))->setField('xiugaisj',time());
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }
        //批量下载回访图片
        public function hftp_pic_downloads(){
            $ids=I('hftp_pic_ids');
            foreach ($ids as $key => $value) {
                $hftp = M('photos')->where(array('id'=>$ids[$key]))->field('adduid,data_id')->find();
                $is_rbac = _rbac_btn_show(855,0,$hftp['adduid']);
                if (!$is_rbac) {
                    $this->error("您选择了权限范围之外的图片！");
                }
                $hfid = $hftp['data_id'];
                $kybh = M('kygenjin')->where(array('id'=>$hftp['data_id']))->getField('kybh');
            }
            $where = 'id in('.implode(',',$ids).')';
            $dp=M('photos');
            $plist = $dp->where($where)->field('img_path,img_name')->select();
            $zip = new \ZipArchive;
            if ($plist) {
                $filename = './Upload/Genjin/'.session('gongsiid').'/'.$kybh.'/'.md5(time()).'.zip';
                $downname = I("server.HTTP_HOST").'/Upload/Genjin/'.session('gongsiid').'/'.$kybh.'/'.md5(time()).'.zip';
            }
            $zip->open($filename,\ZipArchive::OVERWRITE | \ZipArchive::CREATE);
            if ($zip) {
                foreach ($plist as $value) {
                    $zip->addFile('.'.$value['img_path']);
                    $zip->renameName('.'.$value['img_path'],$value['img_name']);
                }
                $zip->close();
                $xiugai.="下载了客户".$kybh."的".count($ids)."张回访图片";
                $data_rizhi['zy_type']=2;
                $data_rizhi['uid']=session("uid");
                $data_rizhi['addtime']=time();
                $data_rizhi['cz_type']=20;
                $data_rizhi['zy_id']=$hfid;
                $data_rizhi['neirong']=$xiugai;
                M('rizhi')->add($data_rizhi);
                $this->success($downname);
            } else {
                $this->error('压缩失败'); 
            }
        }
        //设置图片类型
        public function szhftp(){
            $is_rbac = _rbac_node_show(856);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            if (IS_POST) {
                $ids=explode(",",I('hftp_pic_ids'));
                foreach ($ids as $key => $value) {
                    $hftp = M('photos')->where(array('id'=>$ids[$key]))->field('adduid,data_id')->find();
                    $is_rbac = _rbac_btn_show(856,0,$hftp['adduid']);
                    if (!$is_rbac) {
                        $this->error("您选择了权限范围之外的图片！");
                    }
                    $hfid = $hftp['data_id'];
                    $kybh = M('kygenjin')->where(array('id'=>$hftp['data_id']))->getField('kybh');
                }
                $ids1=I('hftp_pic_ids');
                $tpleixing = I('tpleixing');
                if ($tpleixing=="") {
                    $this->error("请选择图片类型");
                }
                $where = 'id in('.$ids1.')';
                $dp=M('photos');
                $plist = $dp->where($where)->setField(array('data_type' => $tpleixing ));
                if ($plist) {
                    $xiugai.="设置了客户".$kybh."的".count($ids1)."张回访图片类型";
                    $data_rizhi['zy_type']=2;
                    $data_rizhi['uid']=session("uid");
                    $data_rizhi['addtime']=time();
                    $data_rizhi['cz_type']=25;
                    $data_rizhi['zy_id']=$hfid;
                    $data_rizhi['neirong']=$xiugai;
                    M('rizhi')->add($data_rizhi);
                    M('keyuan')->where(array('xqbianhao'=>$kybh))->setField('xiugaisj',time());
                    $this->success("设置成功");
                }else{
                    $this->error("设置失败");
                }
            } else {
                $this->tpleixing = M('Peizhi')->where(array('pzming'=>'hftplx','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->display();// 输出模板
            }
        }
    }
?>