<?php

    /***************************************************************\
    |                                                               |
    |   开单大师（专业的房产ERP管理系统）                           |
    |                                                               |
    |   © 2017-2023 淮南市银泰软件科技有限公司 All rights reserved  |
    |                                                               |
    |   http://www.kaidandashi.com                                  |
    |                                                               |
    |   QQ号：984784483                                             |
    |                                                               |
    \***************************************************************/

    namespace Admin\Controller;
    use Think\Controller;
    class KyneirongController extends CommonController {
        public function editKeyuan(){
            $ky1 = M('keyuan')->where(array('id'=>I('id')))->find();
            $is_rbac = _rbac_btn_show(79,$ky1['zhuangtai'],$ky1['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $is_rbacfdh = _rbac_node_show(864);
            if ($is_rbacfdh || ($ky1['fpdh'] == 2)) {
                $is_rbacfdh = 1;
                $this->is_rbacfdh = 1;
            }
            if (IS_POST) {
                if (session('gongsiid') == "") {
                    $this->error('请重新登录');
                }
                if ($is_rbacfdh == 1) {
                    $dianhua = I('dianhua');
                }
                $xqhuxing1 = I('xqhuxing1');
                $xqhuxing2 = I('xqhuxing2');
                $xqjiage1 = I('xqjiage1');
                $xqjiage2 = I('xqjiage2');
                $xqmianji1 = I('xqmianji1');
                $xqmianji2 = I('xqmianji2');
                $louceng1 = I('louceng1');
                $louceng2 = I('louceng2');
                $fangling1 = I('fangling1');
                $fangling2 = I('fangling2');
                $qqhao = I('qqhao');
                $youxiang = I('youxiang');
                $sfzheng = I('sfzheng');
                $beizhu = preg_replace ( "/\s(?=\s)/","\\1", I('beizhu') );
                if ($is_rbacfdh == 1) {
                    if (!preg_match("/^(13[0-9]|14[0-1,4-9]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$|^0\d{2,3}-?\d{7,8}$/",$dianhua)) {
                        $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 ($ky1['mmzl'] == 2) {
                    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≤十万的整数！');
                    }
                } else {
                    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的整数！');
                }
                if (!preg_match("/^$|^([1-9][0-9]{0,2})$|^(1[0]{3})$/",$fangling1) || !preg_match("/^$|^([1-9][0-9]{0,2})$|^(1[0]{3})$/",$fangling2)) {
                    $this->error('房龄请输入＞0≤1000的整数！');
                }
                if (!preg_match("/^$|^[1-9][0-9]{4,}$/",$qqhao)) {
                    $this->error('QQ请输入正确的QQ号！');
                }
                if (!preg_match("/^$|^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/",$youxiang)) {
                    $this->error('邮箱请输入正确的邮箱！');
                }
                if (!preg_match("/^$|^$|^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$|^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/",$sfzheng)) {
                    $this->error('身份证请输入正确的身份证号！');
                }
                if (!preg_match("/^$|^[\s\S]{0,300}$/",$beizhu)) {
                    $this->error('备注只能输入300个字符！');
                }
                if (I('jiqie')) {
                    $jiqie = 1;
                } else {
                    $jiqie = 0;
                }
                $cx1 = array_keys(I('chaoxiang')); 
                $cx2 = implode(',',$cx1);
                $data['chaoxiang'] = $cx2;
                $zx1 = array_keys(I('zhuangxiu')); 
                $zx2 = implode(',',$zx1);
                $data['zhuangxiu'] = $zx2;
                $pt1 = array_keys(I('peitao')); 
                $pt2 = implode(',',$pt1);
                $data['peitao'] = $pt2;
                $data['jiqie'] = $jiqie;
                $data['fukuan'] = I('fukuan');
                if ($is_rbacfdh == 1) {
                    $data['khxingming'] = I('khxingming');
                    $data['kehulx'] = I('kehulx');
                    $data['dianhua'] = $dianhua;
                }
                $data['xqyongtu'] = I('xqyongtu');
                $data['xqquyu'] = I('xqquyu');
                $xqxiaoqus = array_unique(array_filter(explode("；",I('xqxiaoqu'))));
                $xqxiaoqu = implode("；",$xqxiaoqus);
                $data['xqxiaoqu'] = $xqxiaoqu;
                $data['xqhuxing1'] = $xqhuxing1;
                $data['xqhuxing2'] = $xqhuxing2;
                $data['xqjiage1'] = $xqjiage1;
                $data['xqjiage2'] = $xqjiage2;
                $data['xqmianji1'] = $xqmianji1;
                $data['xqmianji2'] = $xqmianji2;
                $data['louceng1'] = $louceng1;
                $data['louceng2'] = $louceng2;
                $data['fangling1'] = $fangling1;
                $data['fangling2'] = $fangling2;
                $data['xqyuanyin'] = I('xqyuanyin');
                $data['khlaiyuan'] = I('khlaiyuan');
                $data['sfzheng'] = $sfzheng;
                $data['qqhao'] = $qqhao;
                $data['guoji'] = I('guoji');
                $data['minzu'] = I('minzu');
                $data['youxiang'] = $youxiang;
                $data['weixin'] = I('weixin');
                $data['jtgongju'] = I('jtgongju');
                $data['chexing'] = I('chexing');
                $data['beizhu'] = $beizhu;
                $data['gtjieduan'] = I('gtjieduan');
                $data['xflinian'] = I('xflinian');
                $data['kydengji'] = I('kydengji');
                $data['xiugaisj'] = time();
                $xiugai = "";
                if ($ky1['jiqie'] != $jiqie && ($ky1['jiqie'] == true || $jiqie == true)) {
                    $xin1 = $ky1['xinzheng'] ? "是" : "否";
                    $xin2 = $jiqie ? "是" : "否";
                    $xiugai .= "急切：[".$xin1."]=>[".$xin2."]；";
                }
                if ($ky1['peitao'] != $pt2) {
                    if ($ky1['peitao']) {
                        $condition1 = " pzming = 'peitao' and lxid in (".$ky1['peitao'].")";
                        $peitao1 = M('peizhi')->where($condition1)->order('xsshunxu asc')->select();
                        foreach ($peitao1 AS $key => $val) {
                            $xqpt1 .= ",".$val['lxming'];
                        }
                    }
                    if ($pt2) {
                        $condition2 = " pzming = 'peitao' and lxid in (".$pt2.")";
                        $peitao2 = M('peizhi')->where($condition2)->order('xsshunxu asc')->select();
                        foreach ($peitao2 AS $key => $val) {
                            $xqpt2 .= $val['lxming'].",";
                        }
                    }
                    $xiugai .= "配套：[".$xqpt1."]=>[".$xqpt2."]；";
                }
                if ($ky1['chaoxiang'] != $cx2) {
                    if ($ky1['chaoxiang']) {
                        $condition3 = " pzming = 'chaoxiang' and lxid in (".$ky1['chaoxiang'].")";
                        $chaoxiang1 = M('peizhi')->where($condition3)->order('xsshunxu asc')->select();
                        foreach ($chaoxiang1 AS $key => $val) {
                            $xqcx1 .= ",".$val['lxming'];
                        }
                    }
                    if ($cx2) {
                        $condition4 = " pzming = 'chaoxiang' and lxid in (".$cx2.")";
                        $chaoxiang2 = M('peizhi')->where($condition4)->order('xsshunxu asc')->select();
                        foreach ($chaoxiang2 AS $key => $val) {
                            $xqcx2 .= $val['lxming'].",";
                        }
                    }
                    $xiugai .= "朝向：[".$xqcx1."]=>[".$xqcx2."]；";
                }
                if ($ky1['zhuangxiu'] != $zx2) {
                    if ($ky1['zhuangxiu']) {
                        $condition5 = " pzming = 'zhuangxiu' and lxid in (".$ky1['zhuangxiu'].")";
                        $zhuangxiu1 = M('peizhi')->where($condition5)->order('xsshunxu asc')->select();
                        foreach ($zhuangxiu1 AS $key => $val) {
                            $xqzx1 .= ",".$val['lxming'];
                        }
                    }
                    if ($zx2) {
                        $condition6 = "  pzming = 'zhuangxiu' and lxid in (".$zx2.")";
                        $zhuangxiu2 = M('peizhi')->where($condition6)->order('xsshunxu asc')->select();
                        foreach ($zhuangxiu2 AS $key => $val) {
                            $xqzx2 .= $val['lxming'].",";
                        }
                    }
                    $xiugai .= "装修：[".$xqzx1."]=>[".$xqzx2."]；";
                }
                if ($is_rbacfdh  ==  1) {
                    if ($ky1['khxingming'] != I('khxingming') && ($ky1['khxingming'] == true || I('khxingming') == true)) {
                        $xiugai .= "客户姓名：[".$ky1['khxingming']."]=>[".I('khxingming')."]；";
                    }
                    if (I('kehulx') != $ky1['kehulx'] && ($ky1['kehulx'] == true || I('kehulx') == true)) {
                        $xiugai .= get_xiugai($ky1['kehulx'],I('kehulx'),'yezhulx');
                    }
                    if ($ky1['dianhua'] != I('dianhua') && ($ky1['dianhua'] == true || I('dianhua') == true)) {
                        $xiugai .= "客户电话：[".$ky1['dianhua']."]=>[".I('dianhua')."]；";
                    }
                }
                if (I('xqyongtu') != $ky1['xqyongtu'] && ($ky1['xqyongtu'] == true || I('xqyongtu') == true)) {
                    $xiugai .= get_xiugai($ky1['xqyongtu'],I('xqyongtu'),'yongtu');
                }
                if ($ky1['xqquyu'] != I('xqquyu') && ($ky1['xqquyu'] == true || I('xqquyu') == true)) {
                    $xiugai .= "需求区域：[".$ky1['xqquyu']."]=>[".I('xqquyu')."]；";
                }
                if ($ky1['xqxiaoqu'] != $xqxiaoqu && ($ky1['xqxiaoqu'] == true || $xqxiaoqu == true)) {
                    $xiugai .= "需求小区：[".$ky1['xqxiaoqu']."]=>[".$xqxiaoqu."]；";
                }
                if ((($ky1['xqhuxing1'] != I('xqhuxing1') && ($ky1['xqhuxing1'] == true || I('xqhuxing1') == true))) || (($ky1['xqhuxing2'] != I('xqhuxing2') && ($ky1['xqhuxing2'] == true || I('xqhuxing2') == true)))){
                    $xiugai .= "需求户型：[".$ky1['xqhuxing1']."-".$ky1['xqhuxing2']."]=>[".I('xqhuxing1')."-".I('xqhuxing2')."]；";
                }
                if ((($ky1['xqjiage1'] != I('xqjiage1') && ($ky1['xqjiage1'] == true || I('xqjiage1') == true))) || (($ky1['xqjiage2'] != I('xqjiage2') && ($ky1['xqjiage2'] == true || I('xqjiage2') == true)))){
                    $xiugai .= "需求价格：[".$ky1['xqjiage1']."-".$ky1['xqjiage2']."]=>[".I('xqjiage1')."-".I('xqjiage2')."]；";
                }
                if ((($ky1['xqmianji1'] != I('xqmianji1') && ($ky1['xqmianji1'] == true || I('xqmianji1') == true))) || (($ky1['xqmianji2'] != I('xqmianji2') && ($ky1['xqmianji2'] == true || I('xqmianji2') == true)))){
                    $xiugai .= "需求面积：[".$ky1['xqmianji1']."-".$ky1['xqmianji2']."]=>[".I('xqmianji1')."-".I('xqmianji2')."]；";
                }
                if ((($ky1['louceng1'] != I('louceng1') && ($ky1['louceng1'] == true || I('louceng1') == true))) || (($ky1['louceng2'] != I('louceng2') && ($ky1['louceng2'] == true || I('louceng2') == true)))){
                    $xiugai .= "需求楼层：[".$ky1['louceng1']."-".$ky1['louceng2']."]=>[".I('louceng1')."-".I('louceng2')."]；";
                }
                if ((($ky1['fangling1'] != I('fangling1') && ($ky1['fangling1'] == true || I('fangling1') == true))) || (($ky1['fangling2'] != I('fangling2') && ($ky1['fangling2'] == true || I('fangling2') == true)))){
                    $xiugai .= "需求房龄：[".$ky1['fangling1']."-".$ky1['fangling2']."]=>[".I('fangling1')."-".I('fangling2')."]；";
                }
                if (I('fukuan') != $ky1['fukuan'] && ($ky1['fukuan'] == true || I('fukuan') == true)) {
                    if ($ky1['mmzl'] == 1) {
                        $fuk = "fukuan";
                    } elseif ($ky1['mmzl'] == 2) {
                        $fuk = "zlfukuan";
                    } elseif ($ky1['mmzl'] == 3) {
                        $fuk = "ysfukuan";
                    }
                    switch ($ky1['mmzl']) {
                        case 1:
                            $fuk = "fukuan";
                            break;
                        case 2:
                            $fuk = "zlfukuan";
                            break;
                        case 3:
                            $fuk = "ysfukuan";
                            break;
                        default:
                            $this->error('异常错误');
                            break;
                    }
                    $xiugai .= get_xiugai($ky1['fukuan'],I('fukuan'),$fuk);
                }
                if ($ky1['xqyuanyin'] != I('xqyuanyin') && ($ky1['xqyuanyin'] == true || I('xqyuanyin') == true)) {
                    $xiugai .= "需求原因：[".$ky1['xqyuanyin']."]=>[".I('xqyuanyin')."]；";
                }
                if (I('khlaiyuan') != $ky1['khlaiyuan'] && ($ky1['khlaiyuan'] == true || I('khlaiyuan') == true)) {
                    $xiugai .= get_xiugai($ky1['khlaiyuan'],I('khlaiyuan'),'laiyuan');
                }
                if ($ky1['sfzheng'] != I('sfzheng') && ($ky1['sfzheng'] == true || I('sfzheng') == true)) {
                    $xiugai .= "身份证：[".$ky1['sfzheng']."]=>[".I('sfzheng')."]；";
                }
                if ($ky1['qqhao'] != I('qqhao') && ($ky1['qqhao'] == true || I('qqhao') == true)) {
                    $xiugai .= "QQ号：[".$ky1['qqhao']."]=>[".I('qqhao')."]；";
                }
                if (I('guoji') != $ky1['guoji'] && ($ky1['guoji'] == true || I('guoji') == true)) {
                    $xiugai .= get_xiugai($ky1['guoji'],I('guoji'),'guoji');
                }
                if (I('minzu') != $ky1['minzu'] && ($ky1['minzu'] == true || I('minzu') == true)) {
                    $xiugai .= get_xiugai($ky1['minzu'],I('minzu'),'minzu');
                }
                if ($ky1['youxiang'] != I('youxiang') && ($ky1['youxiang'] == true || I('youxiang') == true)) {
                    $xiugai .= "邮箱：[".$ky1['youxiang']."]=>[".I('youxiang')."]；";
                }
                if ($ky1['weixin'] != I('weixin') && ($ky1['weixin'] == true || I('weixin') == true)) {
                    $xiugai .= "微信：[".$ky1['weixin']."]=>[".I('weixin')."]；";
                }
                if (I('jtgongju') != $ky1['jtgongju'] && ($ky1['jtgongju'] == true || I('jtgongju') == true)) {
                    $xiugai .= get_xiugai($ky1['jtgongju'],I('jtgongju'),'jtgongju');
                }
                if ($ky1['chexing'] != I('chexing') && ($ky1['chexing'] == true || I('chexing') == true)) {
                    $xiugai .= "车型：[".$ky1['chexing']."]=>[".I('chexing')."]；";
                }
                if ($ky1['beizhu'] != I('beizhu') && ($ky1['beizhu'] == true || I('beizhu') == true)) {
                    $xiugai .= "备注：[".$ky1['beizhu']."]=>[".I('beizhu')."]；";
                }
                if (I('gtjieduan') != $ky1['gtjieduan'] && ($ky1['gtjieduan'] == true || I('gtjieduan') == true)) {
                    $xiugai .= get_xiugai($ky1['gtjieduan'],I('gtjieduan'),'gtjieduan');
                }
                if (I('xflinian') != $ky1['xflinian'] && ($ky1['xflinian'] == true || I('xflinian') == true)) {
                    $xiugai .= get_xiugai($ky1['xflinian'],I('xflinian'),'xflinian');
                }
                if (I('kydengji') != $ky1['kydengji'] && ($ky1['kydengji'] == true || I('kydengji') == true)) {
                    $xiugai .= get_xiugai($ky1['kydengji'],I('kydengji'),'kydengji');
                }
                $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'=>I('id')))->save($data);
                if ($isok){
                    M('rizhi')->add($data_rizhi);
                    $this->success('更新成功',U('keyuan/neirong',array('id'=>$ky1['id'])));
                } else {
                    $this->error('更新失败');
                }
            } else {
                $this->keyuan = $ky1;
                switch ($ky1['mmzl']) {
                    case 1:
                        $this->fukuan = get_peizhi('fukuan',0,1);
                        break;
                    case 2:
                        $this->fukuan = get_peizhi('zlfukuan',0,1);
                        break;
                    default:
                        $this->error('异常错误');
                        break;
                }
                $this->guoji = get_peizhi('guoji',0,1);
                $this->minzu = get_peizhi('minzu',0,1);
                $this->peitao = get_peizhi('peitao',0,1);
                $this->yongtu = get_peizhi('yongtu',0,1);
                $this->kehulx = get_peizhi('yezhulx',0,1);
                $this->laiyuan = get_peizhi('laiyuan',0,1);
                $this->kydengji = get_peizhi('kydengji',0,1);
                $this->jtgongju = get_peizhi('jtgongju',0,1);
                $this->xflinian = get_peizhi('xflinian',0,1);
                $this->chaoxiang = get_peizhi('chaoxiang',0,1);
                $this->gtjieduan = get_peizhi('gtjieduan',0,1);
                $this->zhuangxiu = get_peizhi('zhuangxiu',0,1);
                $this->chaoxiang2 = explode(",", $ky1['chaoxiang']);
                $this->zhuangxiu2 = explode(",", $ky1['zhuangxiu']);
                $this->peitao2 = explode(",", $ky1['peitao']);
                $this->display();
            }
        }
        //变更状态
        public function bgkyzt(){
            $id=I('id');
            $keyuan=M('keyuan')->where(array('id'=>I('id')))->find();
            $is_rbac = _rbac_btn_show(697,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $chengjiao_options=M('options')->where(array('option_name'=>'chengjiao_options'))->find();
            $options_arr = json_decode($chengjiao_options['option_value'],true);
            if (IS_POST) {
                if ($keyuan['zhuangtai'] == $options_arr['kychengjiao_zhuangtai']){
                    $this->error('客源已成交');
                }
                if (C('IS_KEYUAN_SIKEXZ')==1) {
                    $keyuanshu=M('keyuan')->where(array('weihurenid'=>$keyuan['weihurenid'],'zhuangtai'=>$options_arr['kysike_zhuangtai']))->count();
                    $sikexzs=C('IS_KEYUAN_SIKES')-1;
                    if ((C('IS_KEYUAN_SIKEXZ')==1) && (I('zhuangtai')==$options_arr['kysike_zhuangtai']) && ($keyuanshu>$sikexzs)) {
                        $this->error($keyuan['weihuren'].'的私客已达上限！无法再添加私客！');
                    }
                }
                $data['zhuangtai']=I('zhuangtai');
                $data['xiugaisj']=time();
                $yzt=M('peizhi')->where(array('pzming'=>'khzhuangtai','lxid'=>$keyuan['zhuangtai']))->getField('lxming');
                $xzt=M('peizhi')->where(array('pzming'=>'khzhuangtai','lxid'=>I('zhuangtai')))->getField('lxming');
                $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']=24;
                    $data_rizhi['zy_id']=I('id');
                    $data_rizhi['zy_bianhao']=$keyuan['xqbianhao'];
                    $neirong="变更状态:[".$yzt."]=>[".$xzt."]";
                    $data_rizhi['neirong']=$neirong;
                    M('rizhi')->add($data_rizhi);
                    $this->success('变更成功');
                }else{
                    $this->error('变更失败');
                }
            }else{
                $this->kyzt=$keyuan['zhuangtai'];
                $this->zhuangtai=M('Peizhi')->where(array('pzming'=>'khzhuangtai','zhuangtai'=>1))->order('xsshunxu asc')->select();
                $this->display();
            }
        }
        public function sikeHandle(){
            $keyuan=M('keyuan')->where(array('id'=>I('id')))->find();
            $is_rbac = _rbac_btn_show(83,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $chengjiao_options=M('options')->where(array('option_name'=>'chengjiao_options'))->find();
            $options_arr = json_decode($chengjiao_options['option_value'],true);
            if ($keyuan['zhuangtai'] == $options_arr['kychengjiao_zhuangtai']){
                $this->error('客源已成交');
            }
            if (C('IS_KEYUAN_SIKEXZ')==1) {
                $keyuanshu=M('keyuan')->where(array('weihurenid'=>session('uid'),'zhuangtai'=>$options_arr['kysike_zhuangtai']))->count();
                $sikexzs=C('IS_KEYUAN_SIKES')-1;
                if ((C('IS_KEYUAN_SIKEXZ')==1) && ($keyuanshu>$sikexzs)) {
                    $this->error('您的私客已达上限！无法再添加私客！');
                }
            }
            $data['zhuangtai']=$options_arr['kysike_zhuangtai'];
            $data['weihurenid']=session("uid");
            $data['weihuren']=session("ygmingcheng");
            $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']=3;
                $data_rizhi['zy_id']=I('id');
                $data_rizhi['zy_bianhao']=$keyuan['xqbianhao'];
                $neirong="转为私客";
                $data_rizhi['neirong']=$neirong;
                M('rizhi')->add($data_rizhi);
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
        public function gongkeHandle(){
            $keyuan=M('keyuan')->where(array('id'=>I('id')))->find();
            $is_rbac = _rbac_btn_show(84,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $chengjiao_options=M('options')->where(array('option_name'=>'chengjiao_options'))->find();
            $options_arr = json_decode($chengjiao_options['option_value'],true);
            if ($keyuan['zhuangtai'] == $options_arr['kychengjiao_zhuangtai']){
                $this->error('客源已成交');
            }
            $data['zhuangtai']=$options_arr['kygongke_zhuangtai'];
            $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']=4;
                $data_rizhi['zy_id']=I('id');
                $data_rizhi['zy_bianhao']=$keyuan['xqbianhao'];
                $neirong="转为公客";
                $data_rizhi['neirong']=$neirong;
                M('rizhi')->add($data_rizhi);
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
        //封盘
        public function fengpan(){
            $keyuan=M('keyuan')->where(array('id'=>I('kyid')))->find();
            $is_rbac = _rbac_btn_show(171,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $this->error('请联系客服（19965783436）获取正式版功能！');
            }else{
                if ($keyuan['fpdh'] == 2) {
                    $this->fdh=1;
                }
                if ($keyuan['fpck'] == 2) {
                    $this->fck=1;
                }
                $this->kyid=I('kyid');
                $this->display();
            }
        }
        //开盘
        public function kaipan(){
            $keyuan=M('keyuan')->where(array('id'=>I('kyid')))->find();
            $is_rbac = _rbac_btn_show(173,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $this->error('请联系客服（19965783436）获取正式版功能！');
            }else{
                if ($keyuan['fpdh'] == 1) {
                    $this->kdh=1;
                }
                if ($keyuan['fpck'] == 1) {
                    $this->kck=1;
                }
                $this->kyid=I('kyid');
                $this->display();
            }
        }
        //封盘审核列表
        public function fpsh(){
            if (I('is_ajax')) {
                /*权限控制开始*/
                $condition = "e.gongsiid=".session('gongsiid')." and zylx=2 and czlx=1";
                $x = _rbac_lists_where(850,0);
                if ($x && $x != "all" && $x != "me") {
                    $condition .= " and e.bumen in ({$x})";
                } elseif ($x == "me") {
                    $condition .= " and e.id=".session('uid');
                } elseif ($x == false) {
                    $condition .= " and e.id=0";
                }
                /*权限控制结束*/
                $type = I('type');
                $fplx = I('fplx');
                $sousuo = I('sousuo');
                $shijian = I('shijian');
                if ($type) {
                    $condition .= " and a.type=".$type;
                }
                if ($fplx) {
                    $condition .= " and a.fkplx=".$fplx;
                }
                if ($sousuo) {
                    $condition .= " and (a.zybh like '%$sousuo%' or e.ygmingcheng like '%$sousuo%' or e.pinyinjs like '%$sousuo%' or e.dianhua like '%$sousuo%')";
                }
                if ($shijian) {
                    $time_s = explode(' - ',$shijian);
                    $time_s1 = strtotime($time_s[0]);
                    $time_s2 = strtotime($time_s[1])+86400;
                    $condition .= " and a.addtime>=".$time_s1." and a.addtime<".$time_s2;
                }
                $field = I('field'); //排序字段名
                $order = I('order'); //排序方式
                $limit = I('limit',30,'int'); //每页条数
                $limit = $limit > 150 ? 150 : $limit;
                if ($field && $order) {
                    $fppx = "a.".$field." ".$order;
                } else {
                    $fppx = "a.id desc";
                }
                $count = M('fpkp')->alias('a')->join('jjrxt_yonghu e on a.uid=e.id')->where($condition)->count();
                $Page = I('p');
                $data = M('fpkp')->alias('a')->join('jjrxt_yonghu e on a.uid=e.id')->field('a.*,e.ygmingcheng,e.dianhua,e.bumen')->where($condition)->order($fppx)->page($Page,$limit)->select();
                foreach ($data as $key => $value) {
                    $data[$key]['fprxx'] = $value['ygmingcheng']."/".zym($value['bumen'],6)."（".$value['dianhua']."）";
                    $data[$key]['tjrxx'] = zym($value['adduid'],5)."（".zym($value['adduid'],14)."）";
                    $data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : "——";
                    $data[$key]['endunix'] = $value['endunix'] ? date('Y-m-d',$value['endunix']) : "——";
                    switch ($value['type']) {
                        case '1':
                            $shzt = "已通过";
                            break;
                        case '2':
                            $shzt = "已驳回";
                            break;
                        case '3':
                            $shzt = "待审核";
                            break;
                        default:
                            break;
                    }
                    switch ($value['fkplx']) {
                        case '1':
                            $fpcz = "封电话";
                            break;
                        case '5':
                            $fpcz = "封查看";
                            break;
                        default:
                            break;
                    }
                    $data[$key]['shzt'] = $shzt ? $shzt : "——";
                    $data[$key]['fpcz'] = $fpcz ? $fpcz : "——";
                    $data[$key]['kyxqlj'] = U('keyuan1/neirong1',array('id'=>$value['zyid']));
                    $kyxq = "";
                    $is_rbacky = "";
                    $is_rbacsh = "";
                    $is_rbacrz = "";
                    $kyxq = M('keyuan')->where(array('id'=>$value['zyid']))->field('zhuangtai,weihurenid')->find();
                    if ($kyxq) {
                        $is_rbacky = _rbac_btn_show(75,$kyxq['zhuangtai'],$kyxq['weihurenid']);
                    }
                    if ($value['type'] == 3) {
                        if ($kyxq) {
                            $is_rbacsh = _rbac_btn_show(851,$kyxq['zhuangtai'],$value['uid']);
                        }
                    } else {
                        $is_rbacrz = _rbac_btn_show(852,0,$value['uid']);
                    }
                    $data[$key]['rbacky'] = $is_rbacky ? 1 : 0;
                    $data[$key]['rbacsh'] = $is_rbacsh ? 1 : 0;
                    $data[$key]['rbacrz'] = $is_rbacrz ? 1 : 0;
                }
                $return_data = array(
                    'code'=>0,
                    'count'=>$count,
                    'data'=>$data
                );
                $this->ajaxReturn($return_data);
            } else {
                $this->display();
            }
        }
        //封盘审核
        public function fpshenhe(){
            $id=I('id');
            $fpkp=M('fpkp')->where(array('id'=>$id,'zylx'=>2,'czlx'=>1))->field('uid,zyid,type,fkplx')->find();
            $kyzt=M('keyuan')->where(array('id'=>$fpkp['zyid']))->getField('zhuangtai');
            $is_rbac = _rbac_btn_show(851,$kyzt,$fpkp['uid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $this->error('请联系客服（19965783436）获取正式版功能！');
            }else{
                $this->display();
            }
        }
        //封盘日志
        public function fprizhi(){
            $rzlx = I('rzlx');
            if ($rzlx != 1 && $rzlx != 2) {
                $rzlx == 1;
            }
            switch ($rzlx) {
                case '1':
                    if (I('is_ajax')) {
                        /*权限控制开始*/
                        $condition = "e.gongsiid=".session('gongsiid')." and zylx=2 and czlx=1";
                        $x = _rbac_lists_where(852,0);
                        if ($x && $x != "all" && $x != "me") {
                            $condition .= " and e.bumen in ({$x})";
                        } elseif ($x == "me") {
                            $condition .= " and e.id=".session('uid');
                        } elseif ($x == false) {
                            $condition .= " and e.id=0";
                        }
                        /*权限控制结束*/
                        $type=I('type');
                        $fplx=I('fplx');
                        $sousuo = I('sousuo');
                        $shijian = I('shijian');
                        if ($type) {
                            $condition.=" and a.type=".$type;
                        }
                        if ($fplx) {
                            $condition.=" and a.fkplx=".$fplx;
                        }
                        if ($sousuo) {
                            $condition .= " and (a.zybh like '%$sousuo%' or e.ygmingcheng like '%$sousuo%' or e.pinyinjs like '%$sousuo%' or e.dianhua like '%$sousuo%')";
                        }
                        if ($shijian) {
                            $time_s = explode(' - ',$shijian);
                            $time_s1 = strtotime($time_s[0]);
                            $time_s2 = strtotime($time_s[1])+86400;
                            $condition .= " and a.addtime>=".$time_s1." and a.addtime<".$time_s2;
                        }
                        $field = I('field'); //排序字段名
                        $order = I('order'); //排序方式
                        $limit = I('limit',30,'int'); //每页条数
                        $limit = $limit > 150 ? 150 : $limit;
                        if ($field && $order) {
                            $rzpx = "a.".$field." ".$order;
                        } else {
                            $rzpx = "a.id desc";
                        }
                        $count = M('fpkp')->alias('a')->join('jjrxt_yonghu e on a.uid=e.id')->where($condition)->count();
                        $Page = I('p');
                        $data = M('fpkp')->alias('a')->join('jjrxt_yonghu e on a.uid=e.id')->field('a.*,e.ygmingcheng,e.dianhua,e.bumen')->where($condition)->order($rzpx)->page($Page,$limit)->select();
                        foreach ($data as $key => $value) {
                            $data[$key]['fprxx'] = $value['ygmingcheng']."/".zym($value['bumen'],6)."（".$value['dianhua']."）";
                            $data[$key]['tjrxx'] = zym($value['adduid'],5)."（".zym($value['adduid'],14)."）";
                            $data[$key]['shrxx'] = $value['shuid'] ? zym($value['shuid'],5)."（".zym($value['shuid'],14)."）" : "——";
                            $data[$key]['remark'] = $value['remark'] ? $value['remark'] : "——";
                            $data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : "——";
                            $data[$key]['endunix'] = $value['endunix'] ? date('Y-m-d',$value['endunix']) : "——";
                            $data[$key]['shshijian'] = $value['shshijian'] ? date('Y-m-d',$value['shshijian']) : "——";
                            switch ($value['type']) {
                                case '1':
                                    $shzt = "已通过";
                                    break;
                                case '2':
                                    $shzt = "已驳回";
                                    break;
                                case '3':
                                    $shzt = "待审核";
                                    break;
                                default:
                                    break;
                            }
                            switch ($value['fkplx']) {
                                case '1':
                                    $fpcz = "封电话";
                                    break;
                                case '5':
                                    $fpcz = "封查看";
                                    break;
                                default:
                                    break;
                            }
                            $data[$key]['shzt'] = $shzt ? $shzt : "——";
                            $data[$key]['fpcz'] = $fpcz ? $fpcz : "——";
                            $data[$key]['kyxqlj'] = U('keyuan1/neirong1',array('id'=>$value['zyid']));
                            $kyxq = "";
                            $is_rbacky = "";
                            $kyxq = M('keyuan')->where(array('id'=>$value['zyid']))->field('zhuangtai,weihurenid')->find();
                            if ($kyxq) {
                                $is_rbacky = _rbac_btn_show(75,$kyxq['zhuangtai'],$kyxq['weihurenid']);
                            }
                            $data[$key]['rbacky'] = $is_rbacky ? 1 : 0;
                        }
                        $return_data = array(
                            'code'=>0,
                            'count'=>$count,
                            'data'=>$data
                        );
                        $this->ajaxReturn($return_data);
                    } else {
                        $this->rzlx = 1;
                        $this->display();
                    }
                    break;
                case '2':
                    $kyfp=M('fpkp')->where(array('id'=>I('id'),'zylx'=>2,'czlx'=>1))->field('shuid,shshijian,remark,type')->find();
                    $shren = M('yonghu')->where(array('id'=>$kyfp['shuid']))->field('ygmingcheng,dianhua')->find();
                    switch ($kyfp['type']) {
                        case '1':
                            $shzt = "已通过";
                            break;
                        case '2':
                            $shzt = "已驳回";
                            break;
                        case '3':
                            $shzt = "待审核";
                            break;
                        default:
                            $shzt = "——";
                            break;
                    }
                    $this->shren = $shren ? $shren['ygmingcheng']."（".$shren['dianhua']."）" : "——";
                    $this->shsj = $kyfp['shshijian'] ? date('Y-m-d',$kyfp['shshijian']) : "——";
                    $this->remark = $kyfp['remark'] ? $kyfp['remark'] : "——";
                    $this->shzt = $shzt;
                    $this->rzlx = 2;
                    $this->display();
                    break;
            }
        }
        //开盘审核列表
        public function kpsh(){
            if (I('is_ajax')) {
                /*权限控制开始*/
                $condition = "e.gongsiid=".session('gongsiid')." and zylx=2 and czlx=2";
                $x = _rbac_lists_where(853,0);
                if ($x && $x != "all" && $x != "me") {
                    $condition .= " and e.bumen in ({$x})";
                } elseif ($x == "me") {
                    $condition .= " and e.id=".session('uid');
                } elseif ($x == false) {
                    $condition .= " and e.id=0";
                }
                /*权限控制结束*/
                $type = I('type');
                $kplx = I('kplx');
                $sousuo = I('sousuo');
                $shijian = I('shijian');
                if ($type) {
                    $condition .= " and a.type=".$type;
                }
                if ($kplx) {
                    $condition .= " and a.fkplx=".$kplx;
                }
                if ($sousuo) {
                    $condition .= " and (a.zybh like '%$sousuo%' or e.ygmingcheng like '%$sousuo%' or e.pinyinjs like '%$sousuo%' or e.dianhua like '%$sousuo%')";
                }
                if ($shijian) {
                    $time_s = explode(' - ',$shijian);
                    $time_s1 = strtotime($time_s[0]);
                    $time_s2 = strtotime($time_s[1])+86400;
                    $condition .= " and a.addtime>=".$time_s1." and a.addtime<".$time_s2;
                }
                $field = I('field'); //排序字段名
                $order = I('order'); //排序方式
                $limit = I('limit',30,'int'); //每页条数
                $limit = $limit > 150 ? 150 : $limit;
                if ($field && $order) {
                    $kppx = "a.".$field." ".$order;
                } else {
                    $kppx = "a.id desc";
                }
                $count = M('fpkp')->alias('a')->join('jjrxt_yonghu e on a.uid=e.id')->where($condition)->count();
                $Page = I('p');
                $data = M('fpkp')->alias('a')->join('jjrxt_yonghu e on a.uid=e.id')->field('a.*,e.ygmingcheng,e.dianhua,e.bumen')->where($condition)->order($kppx)->page($Page,$limit)->select();
                foreach ($data as $key => $value) {
                    $data[$key]['kprxx'] = $value['ygmingcheng']."/".zym($value['bumen'],6)."（".$value['dianhua']."）";
                    $data[$key]['tjrxx'] = zym($value['adduid'],5)."（".zym($value['adduid'],14)."）";
                    $data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : "——";
                    switch ($value['type']) {
                        case '1':
                            $shzt = "已通过";
                            break;
                        case '2':
                            $shzt = "已驳回";
                            break;
                        case '3':
                            $shzt = "待审核";
                            break;
                        default:
                            break;
                    }
                    switch ($value['fkplx']) {
                        case '1':
                            $kpcz = "开电话";
                            break;
                        case '5':
                            $kpcz = "开查看";
                            break;
                        default:
                            break;
                    }
                    $data[$key]['shzt'] = $shzt ? $shzt : "——";
                    $data[$key]['kpcz'] = $kpcz ? $kpcz : "——";
                    $data[$key]['kyxqlj'] = U('keyuan1/neirong1',array('id'=>$value['zyid']));
                    $kyxq = "";
                    $is_rbacky = "";
                    $is_rbacsh = "";
                    $is_rbacrz = "";
                    $kyxq = M('keyuan')->where(array('id'=>$value['zyid']))->field('zhuangtai,weihurenid')->find();
                    if ($kyxq) {
                        $is_rbacky = _rbac_btn_show(75,$kyxq['zhuangtai'],$kyxq['weihurenid']);
                    }
                    if ($value['type'] == 3) {
                        if ($kyxq) {
                            $is_rbacsh = _rbac_btn_show(854,$kyxq['zhuangtai'],$value['uid']);
                        }
                    } else {
                        $is_rbacrz = _rbac_btn_show(855,0,$value['uid']);
                    }
                    $data[$key]['rbacky'] = $is_rbacky ? 1 : 0;
                    $data[$key]['rbacsh'] = $is_rbacsh ? 1 : 0;
                    $data[$key]['rbacrz'] = $is_rbacrz ? 1 : 0;
                }
                $return_data = array(
                    'code'=>0,
                    'count'=>$count,
                    'data'=>$data
                );
                $this->ajaxReturn($return_data);
            } else {
                $this->display();
            }
        }
        //开盘审核
        public function kpshenhe(){
            $id=I('id');
            $fpkp=M('fpkp')->where(array('id'=>$id,'zylx'=>2,'czlx'=>2))->field('uid,zyid,type,fkplx')->find();
            $kyzt=M('keyuan')->where(array('id'=>$fpkp['zyid']))->getField('zhuangtai');
            $is_rbac = _rbac_btn_show(854,$kyzt,$fpkp['uid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $this->error('请联系客服（19965783436）获取正式版功能！');
            }else{
                $this->display();
            }
        }
        //开盘日志
        public function kprizhi(){
            $rzlx = I('rzlx');
            if ($rzlx != 1 && $rzlx != 2) {
                $rzlx == 1;
            }
            switch ($rzlx) {
                case '1':
                    if (I('is_ajax')) {
                        /*权限控制开始*/
                        $condition = "e.gongsiid=".session('gongsiid')." and zylx=2 and czlx=2";
                        $x = _rbac_lists_where(855,0);
                        if ($x && $x != "all" && $x != "me") {
                            $condition .= " and e.bumen in ({$x})";
                        } elseif ($x == "me") {
                            $condition .= " and e.id=".session('uid');
                        } elseif ($x == false) {
                            $condition .= " and e.id=0";
                        }
                        /*权限控制结束*/
                        $type=I('type');
                        $kplx=I('kplx');
                        $sousuo = I('sousuo');
                        $shijian = I('shijian');
                        if ($type) {
                            $condition.=" and a.type=".$type;
                        }
                        if ($kplx) {
                            $condition.=" and a.fkplx=".$kplx;
                        }
                        if ($sousuo) {
                            $condition .= " and (a.zybh like '%$sousuo%' or e.ygmingcheng like '%$sousuo%' or e.pinyinjs like '%$sousuo%' or e.dianhua like '%$sousuo%')";
                        }
                        if ($shijian) {
                            $time_s = explode(' - ',$shijian);
                            $time_s1 = strtotime($time_s[0]);
                            $time_s2 = strtotime($time_s[1])+86400;
                            $condition .= " and a.addtime>=".$time_s1." and a.addtime<".$time_s2;
                        }
                        $field = I('field'); //排序字段名
                        $order = I('order'); //排序方式
                        $limit = I('limit',30,'int'); //每页条数
                        $limit = $limit > 150 ? 150 : $limit;
                        if ($field && $order) {
                            $rzpx = "a.".$field." ".$order;
                        } else {
                            $rzpx = "a.id desc";
                        }
                        $count = M('fpkp')->alias('a')->join('jjrxt_yonghu e on a.uid=e.id')->where($condition)->count();
                        $Page = I('p');
                        $data = M('fpkp')->alias('a')->join('jjrxt_yonghu e on a.uid=e.id')->field('a.*,e.ygmingcheng,e.dianhua,e.bumen')->where($condition)->order($rzpx)->page($Page,$limit)->select();
                        foreach ($data as $key => $value) {
                            $data[$key]['kprxx'] = $value['ygmingcheng']."/".zym($value['bumen'],6)."（".$value['dianhua']."）";
                            $data[$key]['tjrxx'] = zym($value['adduid'],5)."（".zym($value['adduid'],14)."）";
                            $data[$key]['shrxx'] = $value['shuid'] ? zym($value['shuid'],5)."（".zym($value['shuid'],14)."）" : "——";
                            $data[$key]['remark'] = $value['remark'] ? $value['remark'] : "——";
                            $data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : "——";
                            $data[$key]['endunix'] = $value['endunix'] ? date('Y-m-d',$value['endunix']) : "——";
                            $data[$key]['shshijian'] = $value['shshijian'] ? date('Y-m-d',$value['shshijian']) : "——";
                            switch ($value['type']) {
                                case '1':
                                    $shzt = "已通过";
                                    break;
                                case '2':
                                    $shzt = "已驳回";
                                    break;
                                case '3':
                                    $shzt = "待审核";
                                    break;
                                default:
                                    break;
                            }
                            switch ($value['fkplx']) {
                                case '1':
                                    $kpcz = "开电话";
                                    break;
                                case '5':
                                    $kpcz = "开查看";
                                    break;
                                default:
                                    break;
                            }
                            $data[$key]['shzt'] = $shzt ? $shzt : "——";
                            $data[$key]['kpcz'] = $kpcz ? $kpcz : "——";
                            $data[$key]['kyxqlj'] = U('keyuan1/neirong1',array('id'=>$value['zyid']));
                            $kyxq = "";
                            $is_rbacky = "";
                            $kyxq = M('keyuan')->where(array('id'=>$value['zyid']))->field('zhuangtai,weihurenid')->find();
                            if ($kyxq) {
                                $is_rbacky = _rbac_btn_show(75,$kyxq['zhuangtai'],$kyxq['weihurenid']);
                            }
                            $data[$key]['rbacky'] = $is_rbacky ? 1 : 0;
                        }
                        $return_data = array(
                            'code'=>0,
                            'count'=>$count,
                            'data'=>$data
                        );
                        $this->ajaxReturn($return_data);
                    } else {
                        $this->rzlx = 1;
                        $this->display();
                    }
                    break;
                case '2':
                    $kykp=M('fpkp')->where(array('id'=>I('id'),'zylx'=>2,'czlx'=>2))->field('shuid,shshijian,remark,type')->find();
                    $shren = M('yonghu')->where(array('id'=>$kykp['shuid']))->field('ygmingcheng,dianhua')->find();
                    switch ($kykp['type']) {
                        case '1':
                            $shzt = "已通过";
                            break;
                        case '2':
                            $shzt = "已驳回";
                            break;
                        case '3':
                            $shzt = "待审核";
                            break;
                        default:
                            $shzt = "——";
                            break;
                    }
                    $this->shren = $shren ? $shren['ygmingcheng']."（".$shren['dianhua']."）" : "——";
                    $this->shsj = $kykp['shshijian'] ? date('Y-m-d',$kykp['shshijian']) : "——";
                    $this->remark = $kykp['remark'] ? $kykp['remark'] : "——";
                    $this->shzt = $shzt;
                    $this->rzlx = 2;
                    $this->display();
                    break;
            }
        }
        //写跟进
        public function genjin(){
            $keyuan=M('keyuan')->where(array('id'=>I('kyid')))->find();
            $is_rbac = _rbac_btn_show(85,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $gjid = I('gjid');
            $gongsiid = session('gongsiid');
            //音频操作ajax（上传，删除）
            switch (I('gt')) {
                case '1':
                    //语音上传
                    $upload = new \Think\Upload();
                    $upload->maxSize = 2097152;
                    $upload->exts = array('mp3'); //array('mp3','ogg');
                    $upload->rootPath = './Upload/';
                    $upload->savePath = './Genjin/'.$gongsiid.'/'.$keyuan['xqbianhao'].'/';
                    $upload->autoSub = false;
                    $uplaod->replace = true;    //存在同名文件是否是覆盖，默认为false
                    $upload->saveName = $gjid;
                    //开始上传音频文件
                    $info = $upload->upload();
                    if (!$info) {
                        $this->error($upload->getError());
                    }else{
                        M('kygenjin')->where(array('id'=>$gjid))->setField(array('gjvoice'=>1));
                        $this->success($info);
                    }
                    break;
                case '2':
                    $isok = unlink('./Upload/Genjin/'.$gongsiid.'/'.$keyuan['xqbianhao'].'/'.$gjid.'.mp3');
                    if ($isok) {
                        M('kygenjin')->where(array('id'=>$gjid))->setField(array('gjvoice'=>null)); 
                    }
                    $this->success('删除语音成功！');
                    break;
                default:
                    break;
            }
            if (IS_POST) {
                $data['kybh']=I('kybh');
                $data['kyid']=I('kyid');
                $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_rizhi['zy_type']=2;
                    $data_rizhi['uid']=session("uid");
                    $data_rizhi['addtime']=time();
                    $data_rizhi['cz_type']=5;
                    $data_rizhi['zy_id']=I('kyid');
                    $data_rizhi['zy_bianhao']=I('kybh');
                    $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();
            $is_rbac = _rbac_btn_show(302,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $data['kybh']=I('kybh');
                $data['kyid']=I('kyid');
                $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'=>I('kyid')))->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']=I('kyid');
                    $data_rizhi['zy_bianhao']=I('kybh');
                    $neirong=I('txneirong');
                    $data_rizhi['neirong']=$neirong;
                    M('rizhi')->add($data_rizhi);
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->display();
            }
        }
        //写带看
        public function daikan(){
            $id=I('kyid');
            $keyuan=M('keyuan')->where(array('id'=>$id))->find();
            $is_rbac = _rbac_btn_show(87,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $mmzl=I('mmzl');
                $newstr = substr(I('dkfangyuan'),0,strlen($str)-1);
                $aaa=array_unique(array_filter(explode(";",$newstr)));
                $s=count($aaa);
                for ($i=0; $i < $s; $i++) {
                    $fyid=M('fangyuan')->where(array('bianhao'=>$aaa[$i]))->getField('id');
                    if (!$fyid) {
                        $this->error('未找到房源'.$aaa[$i].'！');
                    }
                }
                for ($i=0; $i < $s; $i++) {
                    $fyid=M('fangyuan')->where(array('bianhao'=>$aaa[$i]))->getField('id');
                    $data['kyid']=$id;
                    $data['fyid']=$fyid;
                    $data['fybh']=$aaa[$i];
                    $data['kybh']=I('kybh');
                    $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('gongsiid'=>session('gongsiid'),'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']=I('kybh');
                    $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();
            $is_rbac = _rbac_btn_show(89,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            if (IS_POST) {
                $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 {
                $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();
            $is_rbac = _rbac_btn_show(438,$keyuan['zhuangtai'],$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();
            $is_rbac = _rbac_btn_show(440,$keyuan['zhuangtai'],$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();
            $is_rbac = _rbac_btn_show(501,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->error('异常错误！');
            }
            $this->error('请联系客服（19965783436）获取正式版功能！');
        }
        public function huifuky(){
            $id=I('id');
            $keyuan=M('keyuan')->where(array('id'=>$id))->find();
            $is_rbac = _rbac_btn_show(771,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $this->error('请联系客服（19965783436）获取正式版功能！');
        }
        public function yjdelky(){
            $id=I('id');
            $keyuan=M('keyuan')->where(array('id'=>$id))->find();
            $is_rbac = _rbac_btn_show(772,$keyuan['zhuangtai'],$keyuan['weihurenid']);
            if (!$is_rbac) {
                $this->redirect('Index/error');
            }
            $this->error('请联系客服（19965783436）获取正式版功能！');
        }
    }
?>