<?php

	/*******************************************************************\
	|																	|
	|	房开源（综合房产ERP管理系统）									|
	|																	|
	|	© 2019-2023 http://www.kaidandashi.com/ All rights reserved.	|
	|																	|
	|	本系统由淮南市银泰软件科技有限公司提供技术支持					|
	|																	|
	|	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');
			}
			if ($ky1['isdel'] == 1) {
				$this->error('客源已删除！');
			}
			if ($ky1['shtype'] == 3) {
				$this->error('请等待审核！');
			}
			if (IS_POST) {
				$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 (!$xqhuxing1 && !$xqhuxing2) {
					$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 (!$xqjiage1 && !$xqjiage2) {
					$this->error('请输入价格！');
				}
				if ($ky1['mmzl'] == 1) {
					$jgdw = "亿";
				} elseif ($ky1['mmzl'] == 2) {
					$jgdw = "万";
				}
				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≤十'.$jgdw.'的整数！');
				}
				if (!$xqmianji1 && !$xqmianji2) {
					$this->error('请输入面积！');
				}
				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≤一万的整数！');
				}
				$cx1 = array_keys(I('chaoxiang'));
				$cx2 = implode(',',$cx1);
				if (!$cx2) {
					$this->error('请选择朝向！');
				}
				$zx1 = array_keys(I('zhuangxiu'));
				$zx2 = implode(',',$zx1);
				if (!$zx2) {
					$this->error('请选择装修！');
				}
				$pt1 = array_keys(I('peitao'));
				$pt2 = implode(',',$pt1);
				if (!$pt2) {
					$this->error('请选择配套！');
				}
				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;
				}
				$data['chaoxiang'] = $cx2;
				$data['zhuangxiu'] = $zx2;
				$data['peitao'] = $pt2;
				$data['jiqie'] = $jiqie;
				$data['fukuan'] = I('fukuan');
				$data['gongsiid'] = session('gongsiid');
				$data['khxingming'] = I('khxingming');
				$data['kehulx'] = I('kehulx');
				$data['xqleixing'] = I('xqleixing');
				$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();
				if ($ky1['khxingming'] != I('khxingming') && ($ky1['khxingming'] || I('khxingming'))) {
					$xiugai .= "客户姓名：[".$ky1['khxingming']."]=>[".I('khxingming')."]；";
				}
				if (I('kehulx') != $ky1['kehulx'] && ($ky1['kehulx'] || I('kehulx'))) {
					$xiugai .= get_xiugai($ky1['kehulx'],I('kehulx'),'yezhulx');
				}
				if ((($ky1['xqhuxing1'] != $xqhuxing1 && ($ky1['xqhuxing1'] || $xqhuxing1))) || (($ky1['xqhuxing2'] != $xqhuxing2 && ($ky1['xqhuxing2'] || $xqhuxing2)))) {
					if ($ky1['xqhuxing1'] && $ky1['xqhuxing2']) {
						$xgqhx = $ky1['xqhuxing1']."室~".$ky1['xqhuxing2']."室";
					} elseif ($ky1['xqhuxing1']) {
						$xgqhx = $ky1['xqhuxing1']."室以上";
					} elseif ($ky1['xqhuxing2']) {
						$xgqhx = $ky1['xqhuxing2']."室以下";
					}
					if ($xqhuxing1 && $xqhuxing2) {
						$xghhx = $xqhuxing1."室~".$xqhuxing2."室";
					} elseif ($xqhuxing1) {
						$xghhx = $xqhuxing1."室以上";
					} elseif ($xqhuxing2) {
						$xghhx = $xqhuxing2."室以下";
					}
					$xiugai .= "需求户型：[".$xgqhx."]=>[".$xghhx."]；";
				}
				if ((($ky1['xqjiage1'] != $xqjiage1 && ($ky1['xqjiage1'] || $xqjiage1))) || (($ky1['xqjiage2'] != $xqjiage2 && ($ky1['xqjiage2'] || $xqjiage2)))) {
					if ($ky1['mmzl'] == 1) {
						$xqjgdw = "万";
					} elseif ($ky1['mmzl'] == 2) {
						$xqjgdw = "元";
					}
					if ($ky1['xqjiage1'] && $ky1['xqjiage2']) {
						$xgqjg = $ky1['xqjiage1'].$xqjgdw."~".$ky1['xqjiage2'].$xqjgdw;
					} elseif ($ky1['xqjiage1']) {
						$xgqjg = $ky1['xqjiage1'].$xqjgdw."以上";
					} elseif ($ky1['xqjiage2']) {
						$xgqjg = $ky1['xqjiage2'].$xqjgdw."以下";
					}
					if ($xqjiage1 && $xqjiage2) {
						$xghjg = $xqjiage1.$xqjgdw."~".$xqjiage2.$xqjgdw;
					} elseif ($xqjiage1) {
						$xghjg = $xqjiage1.$xqjgdw."以上";
					} elseif ($xqjiage2) {
						$xghjg = $xqjiage2.$xqjgdw."以下";
					}
					$xiugai .= "需求价格：[".$xgqjg."]=>[".$xghjg."]；";
				}
				if ((($ky1['xqmianji1'] != $xqmianji1 && ($ky1['xqmianji1'] || $xqmianji1))) || (($ky1['xqmianji2'] != $xqmianji2 && ($ky1['xqmianji2'] || $xqmianji2)))) {
					if ($ky1['xqmianji1'] && $ky1['xqmianji2']) {
						$xgqmj = $ky1['xqmianji1']."m²~".$ky1['xqmianji2']."m²";
					} elseif ($ky1['xqmianji1']) {
						$xgqmj = $ky1['xqmianji1']."m²以上";
					} elseif ($ky1['xqmianji2']) {
						$xgqmj = $ky1['xqmianji2']."m²以下";
					}
					if ($xqmianji1 && $xqmianji2) {
						$xghmj = $xqmianji1."m²~".$xqmianji2."m²";
					} elseif ($xqmianji1) {
						$xghmj = $xqmianji1."m²以上";
					} elseif ($xqmianji2) {
						$xghmj = $xqmianji2."m²以下";
					}
					$xiugai .= "需求面积：[".$xgqmj."]=>[".$xghmj."]；";
				}
				if (I('xqleixing') != $ky1['xqleixing'] && ($ky1['xqleixing'] || I('xqleixing'))) {
					$xiugai .= get_xiugai($ky1['xqleixing'],I('xqleixing'),'fwleixing');
				}
				if (I('khlaiyuan') != $ky1['khlaiyuan'] && ($ky1['khlaiyuan'] || I('khlaiyuan'))) {
					$xiugai .= get_xiugai($ky1['khlaiyuan'],I('khlaiyuan'),'laiyuan');
				}
				if ($ky1['jiqie'] != $jiqie && ($ky1['jiqie'] || $jiqie)) {
					$xin1 = $ky1['xinzheng'] ? "是" : "否";
					$xin2 = $jiqie ? "是" : "否";
					$xiugai .= "急切：[".$xin1."]=>[".$xin2."]；";
				}
				if ($ky1['xqquyu'] != I('xqquyu') && ($ky1['xqquyu'] || I('xqquyu'))) {
					$xiugai .= "需求区域：[".$ky1['xqquyu']."]=>[".I('xqquyu')."]；";
				}
				if ($ky1['xqxiaoqu'] != $xqxiaoqu && ($ky1['xqxiaoqu'] || $xqxiaoqu)) {
					$xiugai .= "需求楼盘：[".$ky1['xqxiaoqu']."]=>[".$xqxiaoqu."]；";
				}
				$Model = new \Think\Model();
				if ($ky1['chaoxiang'] != $cx2) {
					if ($ky1['chaoxiang']) {
						$condition1 = "pzming = 'chaoxiang' and lxid in (".$ky1['chaoxiang'].")";
						$chaoxiang1 = $Model->query("select * from __PEIZHI__ where {$condition1} order by xsshunxu asc");
						$xqcxsz1 = array();
						foreach ($chaoxiang1 AS $key => $val) {
							$xqcxsz1[] = $val['lxming'];
						}
						$xqcxs1 = implode("；",$xqcxsz1);
					}
					if ($cx2) {
						$condition2 = "pzming = 'chaoxiang' and lxid in (".$cx2.")";
						$chaoxiang2 = $Model->query("select * from __PEIZHI__ where {$condition2} order by xsshunxu asc");
						$xqcxsz2 = array();
						foreach ($chaoxiang2 AS $key => $val) {
							$xqcxsz2[] = $val['lxming'];
						}
						$xqcxs2 = implode("；",$xqcxsz2);
					}
					$xiugai .= "朝向：[".$xqcxs1."]=>[".$xqcxs2."]；";
				}
				if ($ky1['zhuangxiu'] != $zx2) {
					if ($ky1['zhuangxiu']) {
						$condition3 = "pzming = 'zhuangxiu' and lxid in (".$ky1['zhuangxiu'].")";
						$zhuangxiu1 = $Model->query("select * from __PEIZHI__ where {$condition3} order by xsshunxu asc");
						$xqzxsz1 = array();
						foreach ($zhuangxiu1 AS $key => $val) {
							$xqzxsz1[] = $val['lxming'];
						}
						$xqzxs1 = implode("；",$xqzxsz1);
					}
					if ($zx2) {
						$condition4 = "pzming = 'zhuangxiu' and lxid in (".$zx2.")";
						$zhuangxiu2 = $Model->query("select * from __PEIZHI__ where {$condition4} order by xsshunxu asc");
						$xqzxsz2 = array();
						foreach ($zhuangxiu2 AS $key => $val) {
							$xqzxsz2[] = $val['lxming'];
						}
						$xqzxs2 = implode("；",$xqzxsz2);
					}
					$xiugai .= "装修：[".$xqzxs1."]=>[".$xqzxs2."]；";
				}
				if ($ky1['peitao'] != $pt2) {
					if ($ky1['peitao']) {
						$condition5 = "pzming = 'peitao' and lxid in (".$ky1['peitao'].")";
						$peitao1 = $Model->query("select * from __PEIZHI__ where {$condition5} order by xsshunxu asc");
						$xqptsz1 = array();
						foreach ($peitao1 AS $key => $val) {
							$xqptsz1[] = $val['lxming'];
						}
						$xqpts1 = implode("；",$xqptsz1);
					}
					if ($pt2) {
						$condition6 = "pzming = 'peitao' and lxid in (".$pt2.")";
						$peitao2 = $Model->query("select * from __PEIZHI__ where {$condition6} order by xsshunxu asc");
						$xqptsz2 = array();
						foreach ($peitao2 AS $key => $val) {
							$xqptsz2[] = $val['lxming'];
						}
						$xqpts2 = implode("；",$xqptsz2);
					}
					$xiugai .= "配套：[".$xqpts1."]=>[".$xqpts2."]；";
				}
				if ($ky1['xqyuanyin'] != I('xqyuanyin') && ($ky1['xqyuanyin'] || I('xqyuanyin'))) {
					$xiugai .= "需求原因：[".$ky1['xqyuanyin']."]=>[".I('xqyuanyin')."]；";
				}
				if ((($ky1['louceng1'] != $louceng1 && ($ky1['louceng1'] || $louceng1))) || (($ky1['louceng2'] != $louceng2 && ($ky1['louceng2'] || $louceng2)))) {
					if ($ky1['louceng1'] && $ky1['louceng2']) {
						$xgqlc = $ky1['louceng1']."层~".$ky1['louceng2']."层";
					} elseif ($ky1['louceng1']) {
						$xgqlc = $ky1['louceng1']."层以上";
					} elseif ($ky1['louceng2']) {
						$xgqlc = $ky1['louceng2']."层以下";
					}
					if ($louceng1 && $louceng2) {
						$xghlc = $louceng1."层~".$louceng2."层";
					} elseif ($louceng1) {
						$xghlc = $louceng1."层以上";
					} elseif ($louceng2) {
						$xghlc = $louceng2."层以下";
					}
					$xiugai .= "需求楼层：[".$xgqlc."]=>[".$xghlc."]；";
				}
				if ((($ky1['fangling1'] != $fangling1 && ($ky1['fangling1'] || $fangling1))) || (($ky1['fangling2'] != $fangling2 && ($ky1['fangling2'] || $fangling2)))) {
					if ($ky1['fangling1'] && $ky1['fangling2']) {
						$xgqfl = $ky1['fangling1']."年~".$ky1['fangling2']."年";
					} elseif ($ky1['fangling1']) {
						$xgqfl = $ky1['fangling1']."年以上";
					} elseif ($ky1['fangling2']) {
						$xgqfl = $ky1['fangling2']."年以下";
					}
					if ($fangling1 && $fangling2) {
						$xghfl = $fangling1."年~".$fangling2."年";
					} elseif ($fangling1) {
						$xghfl = $fangling1."年以上";
					} elseif ($fangling2) {
						$xghfl = $fangling2."年以下";
					}
					$xiugai .= "需求房龄：[".$xgqfl."]=>[".$xghfl."]；";
				}
				if (I('fukuan') != $ky1['fukuan'] && ($ky1['fukuan'] || I('fukuan'))) {
					if ($ky1['mmzl'] == 1) {
						$fuk = "fukuan";
					} elseif ($ky1['mmzl'] == 2) {
						$fuk = "zlfukuan";
					}
					$xiugai .= get_xiugai($ky1['fukuan'],I('fukuan'),$fuk);
				}
				if (I('gtjieduan') != $ky1['gtjieduan'] && ($ky1['gtjieduan'] || I('gtjieduan'))) {
					$xiugai .= get_xiugai($ky1['gtjieduan'],I('gtjieduan'),'gtjieduan');
				}
				if (I('guoji') != $ky1['guoji'] && ($ky1['guoji'] || I('guoji'))) {
					$xiugai .= get_xiugai($ky1['guoji'],I('guoji'),'guoji');
				}
				if (I('minzu') != $ky1['minzu'] && ($ky1['minzu'] || I('minzu'))) {
					$xiugai .= get_xiugai($ky1['minzu'],I('minzu'),'minzu');
				}
				if (I('xflinian') != $ky1['xflinian'] && ($ky1['xflinian'] || I('xflinian'))) {
					$xiugai .= get_xiugai($ky1['xflinian'],I('xflinian'),'xflinian');
				}
				if (I('kydengji') != $ky1['kydengji'] && ($ky1['kydengji'] || I('kydengji'))) {
					$xiugai .= get_xiugai($ky1['kydengji'],I('kydengji'),'kydengji');
				}
				if ($ky1['sfzheng'] != I('sfzheng') && ($ky1['sfzheng'] || I('sfzheng'))) {
					$xiugai .= "身份证：[".$ky1['sfzheng']."]=>[".I('sfzheng')."]；";
				}
				if ($ky1['qqhao'] != I('qqhao') && ($ky1['qqhao'] || I('qqhao'))) {
					$xiugai .= "QQ号：[".$ky1['qqhao']."]=>[".I('qqhao')."]；";
				}
				if ($ky1['youxiang'] != I('youxiang') && ($ky1['youxiang'] || I('youxiang'))) {
					$xiugai .= "邮箱：[".$ky1['youxiang']."]=>[".I('youxiang')."]；";
				}
				if ($ky1['weixin'] != I('weixin') && ($ky1['weixin'] || I('weixin'))) {
					$xiugai .= "微信：[".$ky1['weixin']."]=>[".I('weixin')."]；";
				}
				if (I('jtgongju') != $ky1['jtgongju'] && ($ky1['jtgongju'] || I('jtgongju'))) {
					$xiugai .= get_xiugai($ky1['jtgongju'],I('jtgongju'),'jtgongju');
				}
				if ($ky1['chexing'] != I('chexing') && ($ky1['chexing'] || I('chexing'))) {
					$xiugai .= "车型：[".$ky1['chexing']."]=>[".I('chexing')."]；";
				}
				if ($ky1['beizhu'] != I('beizhu') && ($ky1['beizhu'] || I('beizhu'))) {
					$xiugai .= "备注：[".$ky1['beizhu']."]=>[".I('beizhu')."]；";
				}
				$isok = M("keyuan")->where(array('id'=>$id))->save($data);
				if ($isok) {
					if ($ky1['mmzl'] == 1) {
						$zyzs = "住宅客源（求购）";
					} elseif ($ky1['mmzl'] == 2) {
						$zyzs = "住宅客源（求租）";
					}
					data_rizhi(2,2,$id,$ky1['xqbianhao'],$xiugai,U('keyuan1/neirong1',array('id'=>$id)),$zyzs);
					if ($ky1['shtype'] == 2) {
						M('keyuan')->where(array('id'=>$id))->setField('shtype',3);
						$this->success('已更新，请等待审核！');
					} elseif ($ky1['shtype'] == 1) {
						$this->success('更新成功');
					}
				} else {
					$this->error('更新失败！');
				}
			} else {
				if ($ky1['mmzl'] == 1) {
					$this->fukuan = get_peizhi('fukuan',0,1);
				} elseif ($ky1['mmzl'] == 2) {
					$this->fukuan = get_peizhi('zlfukuan',0,1);
				} else {
					$this->error('异常错误');
				}
				$this->keyuan = $ky1;
				$this->xqleixing = get_peizhi('fwleixing',0,1);
				$this->chaoxiang = get_peizhi('chaoxiang',0,1);
				$this->kehulx = get_peizhi('yezhulx',0,1);
				$this->zhuangxiu = get_peizhi('zhuangxiu',0,1);
				$this->peitao = get_peizhi('peitao',0,1);
				$this->laiyuan = get_peizhi('laiyuan',0,1);
				$this->xflinian = get_peizhi('xflinian',0,1);
				$this->guoji = get_peizhi('guoji',0,1);
				$this->minzu = get_peizhi('minzu',0,1);
				$this->kydengji = get_peizhi('kydengji',0,1);
				$this->jtgongju = get_peizhi('jtgongju',0,1);
				$this->gtjieduan = get_peizhi('gtjieduan',0,1);
				$this->chaoxiang2 = explode(",", $keyuan['chaoxiang']);
				$this->zhuangxiu2 = explode(",", $keyuan['zhuangxiu']);
				$this->peitao2 = explode(",", $keyuan['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');
			}
			if ($keyuan['isdel'] == 1) {
				$this->error('客源已删除！');
			}
			if ($keyuan['shtype'] == 3) {
				$this->error('请等待审核！');
			}
			if ($keyuan['shtype'] == 2) {
				$this->error('客源已驳回！');
			}
			$kycjzt = get_options('chengjiao_options','kychengjiao_zhuangtai');
			if ($keyuan['zhuangtai'] == $kycjzt){
				$this->error('客源已成交！');
			}
			if (IS_POST) {
				$data['zhuangtai'] = I('zhuangtai');
				$data['xiugaisj'] = time();
				$isok=M('keyuan')->where(array('id'=>I('id')))->save($data);
				if ($isok) {
					$xiugai = "变更".get_xiugai($keyuan['zhuangtai'],I('zhuangtai'),'khzhuangtai');
					if ($keyuan['mmzl'] == 1) {
						$zyzs = "住宅客源（求购）";
					} elseif ($keyuan['mmzl'] == 2) {
						$zyzs = "住宅客源（求租）";
					}
					data_rizhi(2,24,$keyuan['id'],$keyuan['xqbianhao'],$xiugai,U('keyuan1/neirong1',array('id'=>$keyuan['id'])),$zyzs);
					$this->success('变更成功');
				} else {
					$this->error('变更失败！');
				}
			} else {
				$this->kyzt = $keyuan['zhuangtai'];
				$this->zhuangtai = get_peizhi('khzhuangtai',0,1);
				$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->error("异常错误！");
			}
			if ($keyuan['isdel'] == 1) {
				$this->error('客源已删除！');
			}
			if ($keyuan['shtype'] == 3) {
				$this->error('请等待审核！');
			}
			if ($keyuan['shtype'] == 2) {
				$this->error('客源已驳回！');
			}
			$options_arr = get_options('chengjiao_options',0);
			if ($keyuan['zhuangtai'] == $options_arr['kychengjiao_zhuangtai']){
				$this->error('客源已成交！');
			}
			$fywhrs = M('fkywhr')->where(array('zylx'=>2,'id'=>I('id')))->select();
			foreach ($fywhrs as $key => $value) {
				if ($value['whrid'] == session('uid')) {
					$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) {
				$xiugai = session("ygmingcheng")."抓取客源".$keyuan['xqbianhao']."为私客";
				data_fkywhr(2,$keyuan['id'],session('uid'));
				if ($keyuan['mmzl'] == 1) {
					$zyzs = "住宅客源（求购）";
				} elseif ($keyuan['mmzl'] == 2) {
					$zyzs = "住宅客源（求租）";
				}
				data_rizhi(2,9,$keyuan['id'],$keyuan['xqbianhao'],$xiugai,U('keyuan1/neirong1',array('id'=>$keyuan['id'])),$zyzs);
				$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 ($keyuan['isdel'] == 1) {
				$this->error('客源已删除！');
			}
			if ($keyuan['shtype'] == 3) {
				$this->error('请等待审核！');
			}
			if ($keyuan['shtype'] == 2) {
				$this->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 ($keyuan['isdel'] == 1) {
				$this->error('客源已删除！');
			}
			if ($keyuan['shtype'] == 3) {
				$this->error('请等待审核！');
			}
			if ($keyuan['shtype'] == 2) {
				$this->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 = " where b.gongsiid=".session('gongsiid');
				$x = _rbac_lists_where(850,0);
				if ($x && $x != "all" && $x != "me") {
					$condition .= " and b.bumen in ({$x})";
				} elseif ($x == "me") {
					$condition .= " and b.id=".session('uid');
				} elseif ($x == false) {
					$condition .= " and b.id=0";
				}
				$type = I('type');
				$kylx = I('kylx');
				$fkplx = I('fkplx');
				$condition .= " and a.czlx=1";
				$kylxs = "2,15,16,17,18,19,20,21,22";
				$kylxs1 = explode(",",$kylxs);
				if (in_array($kylx,$kylxs1)) {
					$condition .= " and a.zylx = ".$kylx;
				} else {
					$condition .= " and a.zylx in (".$kylxs.")";
				}
				if ($type) {
					$condition .= " and a.type=".$type;
				}
				if ($fkplx) {
					$condition .= " and a.fkplx=".$fkplx;
				}
				$shijian1 = I('shijian1');
				$shijian2 = I('shijian2');
				if ($shijian1) {
					$shijian3 = strtotime($shijian1);
				}
				if ($shijian2) {
					$shijian4 = strtotime($shijian2)+86400;
				}
				if ($shijian3 and $shijian4) {
					if ($shijian3 > $shijian4) {
						$shijian3 = strtotime($shijian2);
						$shijian4 = strtotime($shijian1)+86400;
					}
					$condition .= " and a.addtime>".$shijian3." and a.addtime<".$shijian4;
				} elseif ($shijian3) {
					$condition .= " and a.addtime>".$shijian3;
				} elseif ($shijian4) {
					$condition .= " and a.addtime<".$shijian4;
				}
				$field = I('field'); //排序字段名
				$order = I('order'); //排序方式
				$limit = I('limit',30,'int'); //每页条数
				$limit = $limit > 150 ? 150 : $limit;
				if ($field && $order) {
					$gjpx = "order by a.".$field." ".$order;
				} else {
					$gjpx = "order by a.id desc";
				}
				$Model = new \Think\Model();
				$count = $Model->query("select count(*) from __FPKP__ a join __YONGHU__ b on a.uid = b.id {$condition}");
				$Page = new \Think\Page($count['0']['count(*)'],$limit);
				$list = "select a.*,b.ygmingcheng as fpren,b.dianhua as fpdh,b.bumen as fpbm from __FPKP__ a join __YONGHU__ b on a.uid = b.id {$condition} ".$fppx." limit ".$Page->firstRow.','.$Page->listRows;
				$data = $Model->query($list);
				$kylxs2 = "2,15,16";
				$kylxs3 = explode(",",$kylxs2);
				foreach ($data as $key => $value) {
					if (in_array($value['zylx'],$kylxs3)) {
						switch ($value['zylx']) {
							case '2':
								$kyb = M('keyuan');
								$kylxm = "住宅客源";
								$kyxqlj = U('keyuan1/neirong1',array('id'=>$value['zyid']));
								break;
							case '15':
								$kyb = M('kyxiezilou');
								$kylxm = "写字楼客源";
								$kyxqlj = U('kyxiezilou/neirong',array('id'=>$value['zyid']));
								break;
							case '16':
								$kyb = M('kyshangpu');
								$kylxm = "商铺客源";
								$kyxqlj = U('kyshangpu/neirong',array('id'=>$value['zyid']));
								break;
						}
						if ($kyb) {
							$kyxq = $kyb->where(array('id'=>$value['zyid']))->field('mmzl,zhuangtai,weihurenid')->find();
						}
						switch ($kyxq['mmzl']) {
							case '1':
								$mmzl = "（求购）";
								break;
							case '2':
								$mmzl = "（求租）";
								break;
							default:
								$mmzl = "";
								break;
						}
						$kylxm = $kylxm.$mmzl;
					} else {
						switch ($value['zylx']) {
							case '17':
								$kyb = M('kyxinfang');
								$kylxm = "新房客源";
								$kyxqlj = U('kyxinfang/neirong1',array('id'=>$value['zyid']));
								break;
							case '18':
								$kyb = M('kyjinrong');
								$kylxm = "金融客源";
								$kyxqlj = U('kyjinrong/neirong',array('id'=>$value['zyid']));
								break;
							case '19':
								$kyb = M('kyzhuangxiu');
								$kylxm = "装修客源";
								$kyxqlj = U('kyzhuangxiu/neirong',array('id'=>$value['zyid']));
								break;
							case '20':
								$kyb = M('kygongshang');
								$kylxm = "工商客源";
								$kyxqlj = U('kygongshang/neirong1',array('id'=>$value['zyid']));
								break;
							case '21':
								$kyb = M('kykuaiji');
								$kylxm = "会计客源";
								$kyxqlj = U('kykuaiji/neirong',array('id'=>$value['zyid']));
								break;
							case '22':
								$kyb = M('kyjiaju');
								$kylxm = "家具客源";
								$kyxqlj = U('kyjiaju/neirong',array('id'=>$value['zyid']));
								break;
							default:
								$kyb = "";
								$kylxm = "";
								$kyxqlj = "";
								break;
						}
						if ($kyb) {
							$kyxq = $kyb->where(array('id'=>$value['zyid']))->field('zhuangtai,weihurenid')->find();
						}
					}
					if ($kyxq) {
						$is_rbacxq = _rbac_btn_show(75,$kyxq['zhuangtai'],$kyxq['weihurenid']);
					} else {
						$is_rbacxq = "";
					}
					switch ($value['type']) {
						case '1':
							$type = "已通过";
							break;
						case '2':
							$type = "已驳回";
							break;
						case '3':
							$type = "审核中";
							break;
						default:
							$type = "";
							break;
					}
					switch ($value['fkplx']) {
						case '1':
							$fkplx = "封电话";
							break;
						case '5':
							$fkplx = "封查看";
							break;
						default:
							$fkplx = "";
							break;
					}
					if ($value['type'] == 3) {
						$is_rbacsh = _rbac_btn_show(851,$kyxq['zhuangtai'],$value['uid']);
						$data[$key]['rbacsh'] = $is_rbacsh ? 1 : 0;
					} else {
						$is_rbacrz = _rbac_btn_show(852,0,$value['uid']);
						$data[$key]['rbacrz'] = $is_rbacrz ? 1 : 0;
					}
					$data[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
					$data[$key]['kyxqlj'] = $kyxqlj;
					$data[$key]['kylxm'] = $kylxm;
					$data[$key]['fkplx'] = $fkplx;
					$data[$key]['type'] = $type;
					$data[$key]['fprxx'] = $value['fpren']."/".zym($value['fpbm'],6)."（".$value['fpdh']."）";
					$data[$key]['sqren'] = zym($value['adduid'],5)."（".zym($value['adduid'],14)."）";
					$data[$key]['endunix'] = $value['endunix'] ? date('Y-m-d',$value['endunix']) : "";
					$data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : "";
				}
				$return_data = array(
					'code'=>0,
					'count'=>$count['0']['count(*)'],
					'data'=>$data
				);
				$this->ajaxReturn($return_data);
			} else {
				$this->display();
			}
		}

		//封盘审核
		public function fpshenhe(){
			$id = I('id');
			$fpkp = M('fpkp')->where(array('id'=>$id,'czlx'=>1))->field('uid,zyid,zylx,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) {
				if (I('is_ajax')) {
					$condition = " where b.gongsiid=".session('gongsiid');
					$x = _rbac_lists_where(852,0);
					if ($x && $x != "all" && $x != "me") {
						$condition .= " and b.bumen in ({$x})";
					} elseif ($x == "me") {
						$condition .= " and b.id=".session('uid');
					} elseif ($x == false) {
						$condition .= " and b.id=0";
					}
					$kylx = I('kylx');
					$type = I('type');
					$fkplx = I('fkplx');
					$condition .= " and czlx=1";
					$kylxs = "2,15,16,17,18,19,20,21,22";
					$kylxs1 = explode(",",$kylxs);
					if (in_array($kylx,$kylxs1)) {
						$condition .= " and a.zylx = ".$kylx;
					} else {
						$condition .= " and a.zylx in (".$kylxs.")";
					}
					if ($type) {
						$condition .= " and type=".$type;
					}
					if ($fkplx) {
						$condition .= " and fkplx=".$fkplx;
					}
					$shijian1 = I('shijian1');
					$shijian2 = I('shijian2');
					if ($shijian1) {
						$shijian3 = strtotime($shijian1);
					}
					if ($shijian2) {
						$shijian4 = strtotime($shijian2)+86400;
					}
					if ($shijian3 and $shijian4) {
						if ($shijian3 > $shijian4) {
							$shijian3 = strtotime($shijian2);
							$shijian4 = strtotime($shijian1)+86400;
						}
						$condition .= " and a.addtime>".$shijian3." and a.addtime<".$shijian4;
					} elseif ($shijian3) {
						$condition .= " and a.addtime>".$shijian3;
					} elseif ($shijian4) {
						$condition .= " and a.addtime<".$shijian4;
					}
					$field = I('field'); //排序字段名
					$order = I('order'); //排序方式
					$limit = I('limit',30,'int'); //每页条数
					$limit = $limit > 150 ? 150 : $limit;
					if ($field && $order) {
						$gjpx = "order by a.".$field." ".$order;
					} else {
						$gjpx = "order by a.id desc";
					}
					$Model = new \Think\Model();
					$count = $Model->query("select count(*) from __FPKP__ a join __YONGHU__ b on a.uid = b.id {$condition}");
					$Page = new \Think\Page($count['0']['count(*)'],$limit);
					$list = "select a.*,b.ygmingcheng as fpren,b.dianhua as fpdh,b.bumen as fpbm from __FPKP__ a join __YONGHU__ b on a.uid = b.id {$condition} ".$fppx." limit ".$Page->firstRow.','.$Page->listRows;
					$data = $Model->query($list);
					$kylxs2 = "2,15,16";
					$kylxs3 = explode(",",$kylxs2);
					foreach ($data as $key => $value) {
						if (in_array($value['zylx'],$kylxs3)) {
							switch ($value['zylx']) {
								case '2':
									$kyb = M('keyuan');
									$kylxm = "住宅客源";
									$kyxqlj = U('keyuan1/neirong1',array('id'=>$value['zyid']));
									break;
								case '15':
									$kyb = M('kyxiezilou');
									$kylxm = "写字楼客源";
									$kyxqlj = U('kyxiezilou/neirong',array('id'=>$value['zyid']));
									break;
								case '16':
									$kyb = M('kyshangpu');
									$kylxm = "商铺客源";
									$kyxqlj = U('kyshangpu/neirong',array('id'=>$value['zyid']));
									break;
							}
							if ($kyb) {
								$kyxq = $kyb->where(array('id'=>$value['zyid']))->field('mmzl,zhuangtai,weihurenid')->find();
							}
							switch ($kyxq['mmzl']) {
								case '1':
									$mmzl = "（求购）";
									break;
								case '2':
									$mmzl = "（求租）";
									break;
								default:
									$mmzl = "";
									break;
							}
							$kylxm = $kylxm.$mmzl;
						} else {
							switch ($value['zylx']) {
								case '17':
									$kyb = M('kyxinfang');
									$kylxm = "新房客源";
									$kyxqlj = U('kyxinfang/neirong1',array('id'=>$value['zyid']));
									break;
								case '18':
									$kyb = M('kyjinrong');
									$kylxm = "金融客源";
									$kyxqlj = U('kyjinrong/neirong',array('id'=>$value['zyid']));
									break;
								case '19':
									$kyb = M('kyzhuangxiu');
									$kylxm = "装修客源";
									$kyxqlj = U('kyzhuangxiu/neirong',array('id'=>$value['zyid']));
									break;
								case '20':
									$kyb = M('kygongshang');
									$kylxm = "工商客源";
									$kyxqlj = U('kygongshang/neirong1',array('id'=>$value['zyid']));
									break;
								case '21':
									$kyb = M('kykuaiji');
									$kylxm = "会计客源";
									$kyxqlj = U('kykuaiji/neirong',array('id'=>$value['zyid']));
									break;
								case '22':
									$kyb = M('kyjiaju');
									$kylxm = "家具客源";
									$kyxqlj = U('kyjiaju/neirong',array('id'=>$value['zyid']));
									break;
								default:
									$kyb = "";
									$kylxm = "";
									$kyxqlj = "";
									break;
							}
							if ($kyb) {
								$kyxq = $kyb->where(array('id'=>$value['zyid']))->field('zhuangtai,weihurenid')->find();
							}
						}
						if ($kyxq) {
							$is_rbacxq = _rbac_btn_show(75,$kyxq['zhuangtai'],$kyxq['weihurenid']);
						} else {
							$is_rbacxq = "";
						}
						switch ($value['type']) {
							case '1':
								$type = "已通过";
								break;
							case '2':
								$type = "已驳回";
								break;
							case '3':
								$type = "审核中";
								break;
							default:
								$type = "";
								break;
						}
						switch ($value['fkplx']) {
							case '1':
								$fkplx = "封电话";
								break;
							case '5':
								$fkplx = "封查看";
								break;
							default:
								$fkplx = "";
								break;
						}
						$data[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
						$data[$key]['kyxqlj'] = $kyxqlj;
						$data[$key]['kylxm'] = $kylxm;
						$data[$key]['fkplx'] = $fkplx;
						$data[$key]['type'] = $type;
						$data[$key]['fprxx'] = $value['fpren']."/".zym($value['fpbm'],6)."（".$value['fpdh']."）";
						$data[$key]['sqren'] = zym($value['adduid'],5)."（".zym($value['adduid'],14)."）";
						$data[$key]['shren'] = $value['shuid'] ? zym($value['shuid'],5)."（".zym($value['shuid'],14)."）" : "——";
						$data[$key]['endunix'] = $value['endunix'] ? date('Y-m-d',$value['endunix']) : "";
						$data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : "";
						$data[$key]['shshijian'] = $value['shshijian'] ? date('Y-m-d H:i:s',$value['shshijian']) : "——";
						$data[$key]['remark'] = $value['remark'] ? $value['remark'] : "——";
					}
					$return_data = array(
						'code'=>0,
						'count'=>$count['0']['count(*)'],
						'data'=>$data
					);
					$this->ajaxReturn($return_data);
				} else {
					$this->display();
				}
			} elseif ($rzlx == 2) {
				$kyfp=M('fpkp')->where(array('id'=>I('id'),'czlx'=>1))->field('shuid,shshijian,remark,type')->find();
				$this->kyfp=$kyfp;
			}
			$this->rzlx=$rzlx;
			$this->display();
		}

		//开盘审核列表
		public function kpsh(){
			if (I('is_ajax')) {
				$condition = " where b.gongsiid=".session('gongsiid');
				$x = _rbac_lists_where(853,0);
				if ($x && $x != "all" && $x != "me") {
					$condition .= " and b.bumen in ({$x})";
				} elseif ($x == "me") {
					$condition .= " and b.id=".session('uid');
				} elseif ($x == false) {
					$condition .= " and b.id=0";
				}
				$type = I('type');
				$kylx = I('kylx');
				$fkplx = I('fkplx');
				$condition .= " and a.czlx=2";
				$kylxs = "2,15,16,17,18,19,20,21,22";
				$kylxs1 = explode(",",$kylxs);
				if (in_array($kylx,$kylxs1)) {
					$condition .= " and a.zylx = ".$kylx;
				} else {
					$condition .= " and a.zylx in (".$kylxs.")";
				}
				if ($type) {
					$condition .= " and a.type=".$type;
				}
				if ($fkplx) {
					$condition .= " and a.fkplx=".$fkplx;
				}
				$shijian1 = I('shijian1');
				$shijian2 = I('shijian2');
				if ($shijian1) {
					$shijian3 = strtotime($shijian1);
				}
				if ($shijian2) {
					$shijian4 = strtotime($shijian2)+86400;
				}
				if ($shijian3 and $shijian4) {
					if ($shijian3 > $shijian4) {
						$shijian3 = strtotime($shijian2);
						$shijian4 = strtotime($shijian1)+86400;
					}
					$condition .= " and a.addtime>".$shijian3." and a.addtime<".$shijian4;
				} elseif ($shijian3) {
					$condition .= " and a.addtime>".$shijian3;
				} elseif ($shijian4) {
					$condition .= " and a.addtime<".$shijian4;
				}
				$field = I('field'); //排序字段名
				$order = I('order'); //排序方式
				$limit = I('limit',30,'int'); //每页条数
				$limit = $limit > 150 ? 150 : $limit;
				if ($field && $order) {
					$gjpx = "order by a.".$field." ".$order;
				} else {
					$gjpx = "order by a.id desc";
				}
				$Model = new \Think\Model();
				$count = $Model->query("select count(*) from __FPKP__ a join __YONGHU__ b on a.uid = b.id {$condition}");
				$Page = new \Think\Page($count['0']['count(*)'],$limit);
				$list = "select a.*,b.ygmingcheng as kpren,b.dianhua as kpdh,b.bumen as kpbm from __FPKP__ a join __YONGHU__ b on a.uid = b.id {$condition} ".$fppx." limit ".$Page->firstRow.','.$Page->listRows;
				$data = $Model->query($list);
				$kylxs2 = "2,15,16";
				$kylxs3 = explode(",",$kylxs2);
				foreach ($data as $key => $value) {
					if (in_array($value['zylx'],$kylxs3)) {
						switch ($value['zylx']) {
							case '2':
								$kyb = M('keyuan');
								$kylxm = "住宅客源";
								$kyxqlj = U('keyuan1/neirong1',array('id'=>$value['zyid']));
								break;
							case '15':
								$kyb = M('kyxiezilou');
								$kylxm = "写字楼客源";
								$kyxqlj = U('kyxiezilou/neirong',array('id'=>$value['zyid']));
								break;
							case '16':
								$kyb = M('kyshangpu');
								$kylxm = "商铺客源";
								$kyxqlj = U('kyshangpu/neirong',array('id'=>$value['zyid']));
								break;
						}
						if ($kyb) {
							$kyxq = $kyb->where(array('id'=>$value['zyid']))->field('mmzl,zhuangtai,weihurenid')->find();
						}
						switch ($kyxq['mmzl']) {
							case '1':
								$mmzl = "（求购）";
								break;
							case '2':
								$mmzl = "（求租）";
								break;
							default:
								$mmzl = "";
								break;
						}
						$kylxm = $kylxm.$mmzl;
					} else {
						switch ($value['zylx']) {
							case '17':
								$kyb = M('kyxinfang');
								$kylxm = "新房客源";
								$kyxqlj = U('kyxinfang/neirong1',array('id'=>$value['zyid']));
								break;
							case '18':
								$kyb = M('kyjinrong');
								$kylxm = "金融客源";
								$kyxqlj = U('kyjinrong/neirong',array('id'=>$value['zyid']));
								break;
							case '19':
								$kyb = M('kyzhuangxiu');
								$kylxm = "装修客源";
								$kyxqlj = U('kyzhuangxiu/neirong',array('id'=>$value['zyid']));
								break;
							case '20':
								$kyb = M('kygongshang');
								$kylxm = "工商客源";
								$kyxqlj = U('kygongshang/neirong1',array('id'=>$value['zyid']));
								break;
							case '21':
								$kyb = M('kykuaiji');
								$kylxm = "会计客源";
								$kyxqlj = U('kykuaiji/neirong',array('id'=>$value['zyid']));
								break;
							case '22':
								$kyb = M('kyjiaju');
								$kylxm = "家具客源";
								$kyxqlj = U('kyjiaju/neirong',array('id'=>$value['zyid']));
								break;
							default:
								$kyb = "";
								$kylxm = "";
								$kyxqlj = "";
								break;
						}
						if ($kyb) {
							$kyxq = $kyb->where(array('id'=>$value['zyid']))->field('zhuangtai,weihurenid')->find();
						}
					}
					if ($kyxq) {
						$is_rbacxq = _rbac_btn_show(75,$kyxq['zhuangtai'],$kyxq['weihurenid']);
					} else {
						$is_rbacxq = "";
					}
					switch ($value['type']) {
						case '1':
							$type = "已通过";
							break;
						case '2':
							$type = "已驳回";
							break;
						case '3':
							$type = "审核中";
							break;
						default:
							$type = "";
							break;
					}
					switch ($value['fkplx']) {
						case '1':
							$fkplx = "开电话";
							break;
						case '5':
							$fkplx = "开查看";
							break;
						default:
							$fkplx = "";
							break;
					}
					if ($value['type'] == 3) {
						$is_rbacsh = _rbac_btn_show(854,$kyxq['zhuangtai'],$value['uid']);
						$data[$key]['rbacsh'] = $is_rbacsh ? 1 : 0;
					} else {
						$is_rbacrz = _rbac_btn_show(855,0,$value['uid']);
						$data[$key]['rbacrz'] = $is_rbacrz ? 1 : 0;
					}
					$data[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
					$data[$key]['kyxqlj'] = $kyxqlj;
					$data[$key]['kylxm'] = $kylxm;
					$data[$key]['fkplx'] = $fkplx;
					$data[$key]['type'] = $type;
					$data[$key]['kprxx'] = $value['kpren']."/".zym($value['kpbm'],6)."（".$value['kpdh']."）";
					$data[$key]['sqren'] = zym($value['adduid'],5)."（".zym($value['adduid'],14)."）";
					$data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : "";
				}
				$return_data = array(
					'code'=>0,
					'count'=>$count['0']['count(*)'],
					'data'=>$data
				);
				$this->ajaxReturn($return_data);
			} else {
				$this->display();
			}
		}

		//开盘审核
		public function kpshenhe(){
			$id = I('id');
			$fpkp = M('fpkp')->where(array('id'=>$id,'czlx'=>2))->field('uid,zyid,zylx,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) {
				if (I('is_ajax')) {
					$condition = " where b.gongsiid=".session('gongsiid');
					$x = _rbac_lists_where(855,0);
					if ($x && $x != "all" && $x != "me") {
						$condition .= " and b.bumen in ({$x})";
					} elseif ($x == "me") {
						$condition .= " and b.id=".session('uid');
					} elseif ($x == false) {
						$condition .= " and b.id=0";
					}
					$kylx = I('kylx');
					$type = I('type');
					$fkplx = I('fkplx');
					$condition .= " and czlx=2";
					$kylxs = "2,15,16,17,18,19,20,21,22";
					$kylxs1 = explode(",",$kylxs);
					if (in_array($kylx,$kylxs1)) {
						$condition .= " and a.zylx = ".$kylx;
					} else {
						$condition .= " and a.zylx in (".$kylxs.")";
					}
					if ($type) {
						$condition .= " and type=".$type;
					}
					if ($fkplx) {
						$condition .= " and fkplx=".$fkplx;
					}
					$shijian1 = I('shijian1');
					$shijian2 = I('shijian2');
					if ($shijian1) {
						$shijian3 = strtotime($shijian1);
					}
					if ($shijian2) {
						$shijian4 = strtotime($shijian2)+86400;
					}
					if ($shijian3 and $shijian4) {
						if ($shijian3 > $shijian4) {
							$shijian3 = strtotime($shijian2);
							$shijian4 = strtotime($shijian1)+86400;
						}
						$condition .= " and a.addtime>".$shijian3." and a.addtime<".$shijian4;
					} elseif ($shijian3) {
						$condition .= " and a.addtime>".$shijian3;
					} elseif ($shijian4) {
						$condition .= " and a.addtime<".$shijian4;
					}
					$field = I('field'); //排序字段名
					$order = I('order'); //排序方式
					$limit = I('limit',30,'int'); //每页条数
					$limit = $limit > 150 ? 150 : $limit;
					if ($field && $order) {
						$gjpx = "order by a.".$field." ".$order;
					} else {
						$gjpx = "order by a.id desc";
					}
					$Model = new \Think\Model();
					$count = $Model->query("select count(*) from __FPKP__ a join __YONGHU__ b on a.uid = b.id {$condition}");
					$Page = new \Think\Page($count['0']['count(*)'],$limit);
					$list = "select a.*,b.ygmingcheng as kpren,b.dianhua as kpdh,b.bumen as kpbm from __FPKP__ a join __YONGHU__ b on a.uid = b.id {$condition} ".$fppx." limit ".$Page->firstRow.','.$Page->listRows;
					$data = $Model->query($list);
					$kylxs2 = "2,15,16";
					$kylxs3 = explode(",",$kylxs2);
					foreach ($data as $key => $value) {
						if (in_array($value['zylx'],$kylxs3)) {
							switch ($value['zylx']) {
								case '2':
									$kyb = M('keyuan');
									$kylxm = "住宅客源";
									$kyxqlj = U('keyuan1/neirong1',array('id'=>$value['zyid']));
									break;
								case '15':
									$kyb = M('kyxiezilou');
									$kylxm = "写字楼客源";
									$kyxqlj = U('kyxiezilou/neirong',array('id'=>$value['zyid']));
									break;
								case '16':
									$kyb = M('kyshangpu');
									$kylxm = "商铺客源";
									$kyxqlj = U('kyshangpu/neirong',array('id'=>$value['zyid']));
									break;
							}
							if ($kyb) {
								$kyxq = $kyb->where(array('id'=>$value['zyid']))->field('mmzl,zhuangtai,weihurenid')->find();
							}
							switch ($kyxq['mmzl']) {
								case '1':
									$mmzl = "（求购）";
									break;
								case '2':
									$mmzl = "（求租）";
									break;
								default:
									$mmzl = "";
									break;
							}
							$kylxm = $kylxm.$mmzl;
						} else {
							switch ($value['zylx']) {
								case '17':
									$kyb = M('kyxinfang');
									$kylxm = "新房客源";
									$kyxqlj = U('kyxinfang/neirong1',array('id'=>$value['zyid']));
									break;
								case '18':
									$kyb = M('kyjinrong');
									$kylxm = "金融客源";
									$kyxqlj = U('kyjinrong/neirong',array('id'=>$value['zyid']));
									break;
								case '19':
									$kyb = M('kyzhuangxiu');
									$kylxm = "装修客源";
									$kyxqlj = U('kyzhuangxiu/neirong',array('id'=>$value['zyid']));
									break;
								case '20':
									$kyb = M('kygongshang');
									$kylxm = "工商客源";
									$kyxqlj = U('kygongshang/neirong1',array('id'=>$value['zyid']));
									break;
								case '21':
									$kyb = M('kykuaiji');
									$kylxm = "会计客源";
									$kyxqlj = U('kykuaiji/neirong',array('id'=>$value['zyid']));
									break;
								case '22':
									$kyb = M('kyjiaju');
									$kylxm = "家具客源";
									$kyxqlj = U('kyjiaju/neirong',array('id'=>$value['zyid']));
									break;
								default:
									$kyb = "";
									$kylxm = "";
									$kyxqlj = "";
									break;
							}
							if ($kyb) {
								$kyxq = $kyb->where(array('id'=>$value['zyid']))->field('zhuangtai,weihurenid')->find();
							}
						}
						if ($kyxq) {
							$is_rbacxq = _rbac_btn_show(75,$kyxq['zhuangtai'],$kyxq['weihurenid']);
						} else {
							$is_rbacxq = "";
						}
						switch ($value['type']) {
							case '1':
								$type = "已通过";
								break;
							case '2':
								$type = "已驳回";
								break;
							case '3':
								$type = "审核中";
								break;
							default:
								$type = "";
								break;
						}
						switch ($value['fkplx']) {
							case '1':
								$fkplx = "开电话";
								break;
							case '5':
								$fkplx = "开查看";
								break;
							default:
								$fkplx = "";
								break;
						}
						$data[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
						$data[$key]['kyxqlj'] = $kyxqlj;
						$data[$key]['kylxm'] = $kylxm;
						$data[$key]['fkplx'] = $fkplx;
						$data[$key]['type'] = $type;
						$data[$key]['kprxx'] = $value['kpren']."/".zym($value['kpbm'],6)."（".$value['kpdh']."）";
						$data[$key]['sqren'] = zym($value['adduid'],5)."（".zym($value['adduid'],14)."）";
						$data[$key]['shren'] = $value['shuid'] ? zym($value['shuid'],5)."（".zym($value['shuid'],14)."）" : "——";
						$data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : "";
						$data[$key]['shshijian'] = $value['shshijian'] ? date('Y-m-d H:i:s',$value['shshijian']) : "——";
						$data[$key]['remark'] = $value['remark'] ? $value['remark'] : "——";
					}
					$return_data = array(
						'code'=>0,
						'count'=>$count['0']['count(*)'],
						'data'=>$data
					);
					$this->ajaxReturn($return_data);
				} else {
					$this->display();
				}
			} elseif ($rzlx == 2) {
				$kykp=M('fpkp')->where(array('id'=>I('id'),'czlx'=>2))->field('shuid,shshijian,remark,type')->find();
				$this->kykp=$kykp;
			}
			$this->rzlx=$rzlx;
			$this->display();
		}

		//写跟进
		public function genjin(){
			$kyid = I('kyid');
			$keyuan = M('keyuan')->where(array('id'=>$kyid))->find();
			$is_rbac = _rbac_btn_show(85,$keyuan['zhuangtai'],$keyuan['weihurenid']);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			if ($keyuan['isdel'] == 1) {
				$this->error('客源已删除！');
			}
			if ($keyuan['shtype'] == 3) {
				$this->error('请等待审核！');
			}
			if ($keyuan['shtype'] == 2) {
				$this->error('客源已驳回！');
			}
			$gjid = I('gjid');
			$kybh = $keyuan['xqbianhao'];
			$gongsiid = session('gongsiid');
			if ($keyuan['mmzl'] == 1) {
				$zyzs = "住宅客源（求购）";
			} elseif ($keyuan['mmzl'] == 2) {
				$zyzs = "住宅客源（求租）";
			}
			//音频操作ajax（上传，删除）
			switch (I('gt')) {
				case '1':
					//语音上传
					$upload = new \Think\Upload();
					$upload->maxSize = 2097152;
					$upload->exts = ''; //array('mp3','ogg');
					$upload->rootPath = './Upload/';
					$upload->savePath = './Genjin/'.$gongsiid.'/keyuan/'.$kybh.'/';
					$upload->autoSub = false;
					$uplaod->replace = true; //存在同名文件是否是覆盖，默认为false
					$upload->saveName = 'gj'.$gjid;
					//开始上传音频文件
					$info = $upload->upload();
					if (!$info) {
						$this->error($upload->getError());
					} else {
						M('kygenjin')->where(array('kylx'=>2,'id'=>$gjid))->setField(array('gjvoice'=>1));
						data_rizhi(2,5,$gjid,$kybh,"上传跟进语音",U('keyuan1/neirong1',array('id'=>$kyid)),$zyzs);
						$this->success($info);
					}
					break;
				case '2':
					$isok = unlink('./Upload/Genjin/'.$gongsiid.'/keyuan/'.$kybh.'/gj'.$gjid.'.mp3');
					if ($isok) {
						M('kygenjin')->where(array('kylx'=>2,'id'=>$gjid))->setField(array('gjvoice'=>null)); 
					}
					data_rizhi(2,5,$gjid,$kybh,"删除跟进语音",U('keyuan1/neirong1',array('id'=>$kyid)),$zyzs);
					$this->success('删除语音成功！');
					break;
				default:
					break;
			}
			if (IS_POST) {
				$data['kylx'] = 2;
				$data['kyid'] = $kyid;
				$data['kybh'] = $kybh;
				$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(2,5,$isok,$kybh,I('gjneirong'),U('keyuan1/neirong1',array('id'=>$kyid)),$zyzs);
					$data_genjin['zhgjsj'] = time();
					$data_genjin['xiugaisj']=time();
					M('keyuan')->where(array('id'=>$kyid))->save($data_genjin);
					$this->success('添加成功');
				} else {
					$this->error('添加失败！');
				}
			} else {
				$this->kybh = $kybh;
				$this->genjinfs = get_peizhi('genjinfs',0,1);
				$this->display();
			}
		}

		//写提醒
		public function tixing(){
			$kyid = I('kyid');
			$keyuan = M('keyuan')->where(array('id'=>$kyid))->find();
			$is_rbac = _rbac_btn_show(302,$keyuan['zhuangtai'],$keyuan['weihurenid']);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			if ($keyuan['isdel'] == 1) {
				$this->error('客源已删除！');
			}
			if ($keyuan['shtype'] == 3) {
				$this->error('请等待审核！');
			}
			if ($keyuan['shtype'] == 2) {
				$this->error('客源已驳回！');
			}
			if (IS_POST) {
				$kybh = $keyuan['xqbianhao'];
				$data['kylx'] = 2;
				$data['kyid'] = $kyid;
				$data['kybh'] = $kybh;
				$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'=>$kyid))->setField('xiugaisj',time());
					if ($keyuan['mmzl'] == 1) {
						$zyzs = "住宅客源（求购）";
					} elseif ($keyuan['mmzl'] == 2) {
						$zyzs = "住宅客源（求租）";
					}
					data_rizhi(2,16,$isok,$kybh,I('txneirong'),U('keyuan1/neirong1',array('id'=>$kyid)),$zyzs);
					$this->success('添加成功');
				} else {
					$this->error('添加失败');
				}
			} else {
				$this->display();
			}
		}

		//写带看
		public function daikan(){
			$kyid = I('kyid');
			$keyuan = M('keyuan')->where(array('id'=>$kyid))->find();
			$is_rbac = _rbac_btn_show(87,$keyuan['zhuangtai'],$keyuan['weihurenid']);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			if ($keyuan['isdel'] == 1) {
				$this->error('客源已删除！');
			}
			if ($keyuan['shtype'] == 3) {
				$this->error('请等待审核！');
			}
			if ($keyuan['shtype'] == 2) {
				$this->error('客源已驳回！');
			}
			if (IS_POST) {
				$kybh = $keyuan['xqbianhao'];
				$peikanid = I('peikanid');
				$yh = _rbac_btn_show(88,0,$peikanid);
				if (!$yh || !$peikanid) {
					$this->error('异常错误！');
				}
				$newstr = substr(I('fybh'),0,strlen($str)-1);
				$aaa = array_unique(array_filter(explode(";",$newstr)));
				$s = count($aaa);
				$dkfy = array();
				for ($i=0; $i < $s; $i++) {
					$esfy = M('fangyuan')->where(array('bianhao'=>$aaa[$i]))->field('id,mmzl')->find();
					if ($esfy['mmzl'] == $keyuan['mmzl']) {
						$data['kylx'] = 2;
						$data['kyid'] = $kyid;
						$data['kybh'] = $kybh;
						$data['fyid'] = $esfy['id'];
						$data['fybh'] = $aaa[$i];
						$data['kehupj'] = I('kehupj');
						$data['peikanid'] = $peikanid;
						$data['time'] = time();
						$data['dkuid'] = session('uid');
						$data['gongsiid'] = session('gongsiid');
						$data['dkshijian'] = strtotime(I('dkshijian'));
						M('kydaikan')->add($data);
						$dkfy[] = $aaa[$i];
					}
				}
				$isok = M('Keyuan')->where(array('id'=>$kyid))->setField('daikan',1);
				if ($isok) {
					M('keyuan')->where(array('id'=>$kyid))->setField('xiugaisj',time());
					$dkfy1 = implode("；",$dkfy);
					$xiugai = "带看房源：".$dkfy1;
					if ($keyuan['mmzl'] == 1) {
						$zyzs = "住宅客源（求购）";
					} elseif ($keyuan['mmzl'] == 2) {
						$zyzs = "住宅客源（求租）";
					}
					data_rizhi(2,17,$kyid,$kybh,$xiugai,U('keyuan1/neirong1',array('id'=>$kyid)),$zyzs);
				}
				$this->success('添加成功');
			} else {
				$this->mmzl = $keyuan['mmzl'];
				$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 ($keyuan['isdel'] == 1) {
				$this->error('客源已删除！');
			}
			if ($keyuan['shtype'] == 3) {
				$this->error('请等待审核！');
			}
			if ($keyuan['shtype'] == 2) {
				$this->error('客源已驳回！');
			}
			if (IS_POST) {
				$ysyr = M('yonghu')->where(array('id'=>$keyuan['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'=>$keyuan['id']))->save($data);
				if ($isok) {
					$xiugai = "变更维护人：[".$ysyr."]=>[".$xsyr."]";
					data_fkywhr(2,$keyuan['id'],I('weihurenid'));
					if ($keyuan['mmzl'] == 1) {
						$zyzs = "住宅客源（求购）";
					} elseif ($keyuan['mmzl'] == 2) {
						$zyzs = "住宅客源（求租）";
					}
					data_rizhi(2,9,$keyuan['id'],$keyuan['xqbianhao'],$xiugai,U('keyuan1/neirong1',array('id'=>$keyuan['id'])),$zyzs);
					$this->success('修改成功');
				} else {
					$this->error('修改失败');
				}
			} else {
				$this->weihu = M('Yonghu')->where(array('id'=>$keyuan['weihurenid']))->getField('ygmingcheng');
				$this->display();
			}
		}

		//修改客户详细信息
		public function xgkhxxxx(){
			$ky1 = M("keyuan")->where(array('id'=>I('id')))->find();
			$is_rbac = _rbac_btn_show(438,$ky1['zhuangtai'],$ky1['weihurenid']);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			if ($keyuan['isdel'] == 1) {
				$this->error('客源已删除！');
			}
			if ($keyuan['shtype'] == 3) {
				$this->error('请等待审核！');
			}
			if ($keyuan['shtype'] == 2) {
				$this->error('客源已驳回！');
			}
			$is_rbacfdh = _rbac_node_show(864);
			if ($is_rbacfdh || ($ky1['fpdh'] == 2)) {
				$is_rbacfdh = 1;
				$this->is_rbacfdh = 1;
			}
			if (IS_POST) {
				if ($is_rbacfdh == 1) {
					$dianhua = I('dianhua');
					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('手机请输入正确的手机号码或电话号码！');
					}
					$data['dianhua'] = $dianhua;
				}
				$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();
				$isok = M("keyuan")->where(array('id'=>I('id')))->save($data);
				$Model = new \Think\Model();
				if ($is_rbacfdh == 1) {
					if ($ky1['dianhua'] != I('dianhua') && ($ky1['dianhua'] || I('dianhua'))) {
						$xiugai .= "客户电话：[".$ky1['dianhua']."]=>[".I('dianhua')."]；";
					}
				}
				if (I('khnianling') != $ky1['khnianling'] && ($ky1['khnianling'] || I('khgzquyu'))) {
					$xiugai .= get_xiugai($ky1['khnianling'],I('khnianling'),'khnianling');
				}
				if (I('khjiaoyu') != $ky1['khjiaoyu'] && ($ky1['khjiaoyu'] || I('khjiaoyu'))) {
					$xiugai .= get_xiugai($ky1['khjiaoyu'],I('khjiaoyu'),'khjiaoyu');
				}
				if (I('khhunyin') != $ky1['khhunyin'] && ($ky1['khhunyin'] || I('khhunyin'))) {
					$xiugai .= get_xiugai($ky1['khhunyin'],I('khhunyin'),'khhunyin');
				}
				if (I('khshouru') != $ky1['khshouru'] && ($ky1['khshouru'] || I('khshouru'))) {
					$xiugai .= get_xiugai($ky1['khshouru'],I('khshouru'),'khshouru');
				}
				if (I('khjzquyu') != $ky1['khjzquyu'] && ($ky1['khjzquyu'] || I('khjzquyu'))) {
					$xiugai .= get_xiugai($ky1['khjzquyu'],I('khjzquyu'),'khjzquyu');
				}
				if (I('khjzmianji') != $ky1['khjzmianji'] && ($ky1['khjzmianji'] || I('khjzmianji'))) {
					$xiugai .= get_xiugai($ky1['khjzmianji'],I('khjzmianji'),'khjzmianji');
				}
				if ($ky1['khjzxiaoqu'] != I('khjzxiaoqu') && ($ky1['khjzxiaoqu'] || I('khjzxiaoqu'))) {
					$xiugai .= "居住楼盘：[".$ky1['khjzxiaoqu']."]=>[".I('khjzxiaoqu')."]；";
				}
				if (I('khsjpinpai') != $ky1['khsjpinpai'] && ($ky1['khsjpinpai'] || I('khsjpinpai'))) {
					$xiugai .= get_xiugai($ky1['khsjpinpai'],I('khsjpinpai'),'khsjpinpai');
				}
				if ($ky1['khsjxinghao'] != I('khsjxinghao') && ($ky1['khsjxinghao'] || I('khsjxinghao'))) {
					$xiugai .= "手机型号：[".$ky1['khsjxinghao']."]=>[".I('khsjxinghao')."]；";
				}
				if (I('khgzquyu') != $ky1['khgzquyu'] && ($ky1['khgzquyu'] || I('khgzquyu'))) {
					$xiugai .= get_xiugai($ky1['khgzquyu'],I('khgzquyu'),'khgzquyu');
				}
				if (I('khsshangye') != $ky1['khsshangye'] && ($ky1['khsshangye'] || I('khsshangye'))) {
					$xiugai .= get_xiugai($ky1['khsshangye'],I('khsshangye'),'khsshangye');
				}
				if (I('khzhiwu') != $ky1['khzhiwu'] && ($ky1['khzhiwu'] || I('khzhiwu'))) {
					$xiugai .= get_xiugai($ky1['khzhiwu'],I('khzhiwu'),'khzhiwu');
				}
				if ($ky1['khsfbeizhu'] != I('khsfbeizhu') && ($ky1['khsfbeizhu'] || I('khsfbeizhu'))) {
					$xiugai .= "身份备注：[".$ky1['khsfbeizhu']."]=>[".I('khsfbeizhu')."]；";
				}
				if ($ky1['khjtchengyuan'] != I('khjtchengyuan') && ($ky1['khjtchengyuan'] || I('khjtchengyuan'))) {
					$xiugai .= "家庭成员：[".$ky1['khjtchengyuan']."]=>[".I('khjtchengyuan')."]；";
				}
				if ($ky1['khaihao'] != $ah2) {
					if ($ky1['khaihao']) {
						$condition1 = "pzming = 'khaihao' and lxid in (".$ky1['khaihao'].")";
						$khaihao1 = $Model->query("select * from __PEIZHI__ where {$condition1} order by xsshunxu asc");
						$khahsz1 = array();
						foreach ($khaihao1 AS $key => $val) {
							$khahsz1[] = $val['lxming'];
						}
						$khahs1 = implode("；",$khahsz1);
					}
					if ($ah2) {
						$condition2 = "pzming = 'khaihao' and lxid in (".$ah2.")";
						$khaihao2 = $Model->query("select * from __PEIZHI__ where {$condition2} order by xsshunxu asc");
						$khahsz2 = array();
						foreach ($khaihao2 AS $key => $val) {
							$khahsz2[] = $val['lxming'];
						}
						$khahs2 = implode("；",$khahsz2);
					}
					$xiugai .= "个人爱好：[".$khahs1."]=>[".$khahs2."]；";
				}
				if ($ky1['khjtzcph'] != $ph2) {
					if ($ky1['khjtzcph']) {
						$condition3 = "pzming = 'khjtzcph' and lxid in (".$ky1['khjtzcph'].")";
						$khjtzcph1 = $Model->query("select * from __PEIZHI__ where {$condition3} order by xsshunxu asc");
						$khzcsz1 = array();
						foreach ($khjtzcph1 AS $key => $val) {
							$khzcsz1[] = $val['lxming'];
						}
						$khzcs1 = implode("；",$khzcsz1);
					}
					if ($ph2) {
						$condition4 = "pzming = 'khjtzcph' and lxid in (".$ph2.")";
						$khjtzcph2 = $Model->query("select * from __PEIZHI__ where {$condition4} order by xsshunxu asc");
						$khzcsz2 = array();
						foreach ($khjtzcph2 AS $key => $val) {
							$khzcsz2[] = $val['lxming'];
						}
						$khzcs2 = implode("；",$khzcsz2);
					}
					$xiugai .= "支出偏好：[".$khzcs1."]=>[".$khzcs2."]；";
				}
				if ($ky1['khjqzdsx'] != $sx2) {
					if ($ky1['khjqzdsx']) {
						$condition5 = "pzming = 'khjqzdsx' and lxid in (".$ky1['khjqzdsx'].")";
						$khjqzdsx1 = $Model->query("select * from __PEIZHI__ where {$condition5} order by xsshunxu asc");
						$zdsxsz1 = array();
						foreach ($khjqzdsx1 AS $key => $val) {
							$zdsxsz1[] = $val['lxming'];
						}
						$zdsxs1 = implode("；",$zdsxsz1);
					}
					if ($sx2) {
						$condition6 = "pzming = 'khjqzdsx' and lxid in (".$sx2.")";
						$khjqzdsx2 = $Model->query("select * from __PEIZHI__ where {$condition6} order by xsshunxu asc");
						$zdsxsz2 = array();
						foreach ($khjqzdsx2 AS $key => $val) {
							$zdsxsz2[] = $val['lxming'];
						}
						$zdsxs2 = implode("；",$zdsxsz2);
					}
					$xiugai .= "近期重大事项：[".$zdsxs1."]=>[".$zdsxs2."]；";
				}
				if ($ky1['khbeizhu'] != I('khbeizhu') && ($ky1['khbeizhu'] || I('khbeizhu'))) {
					$xiugai .= "备注：[".$ky1['khbeizhu']."]=>[".I('khbeizhu')."]；";
				}
				if ($isok) {
					if ($ky1['mmzl'] == 1) {
						$zyzs = "住宅客源（求购）";
					} elseif ($ky1['mmzl'] == 2) {
						$zyzs = "住宅客源（求租）";
					}
					data_rizhi(2,7,$ky1['id'],$ky1['xqbianhao'],$xiugai,U('keyuan1/neirong1',array('id'=>$ky1['id'])),$zyzs);
					$this->success('修改成功');
				} else {
					$this->error('修改失败');
				}
			} else {
				$this->keyuan = $ky1;
				$this->khaihao = get_peizhi('khaihao',0,1);
				$this->khzhiwu = get_peizhi('khzhiwu',0,1);
				$this->khjiaoyu = get_peizhi('khjiaoyu',0,1);
				$this->khhunyin = get_peizhi('khhunyin',0,1);
				$this->khshouru = get_peizhi('khshouru',0,1);
				$this->khjzquyu = get_peizhi('khjzquyu',0,1);
				$this->khgzquyu = get_peizhi('khgzquyu',0,1);
				$this->khjtzcph = get_peizhi('khjtzcph',0,1);
				$this->khjqzdsx = get_peizhi('khjqzdsx',0,1);
				$this->khnianling = get_peizhi('khnianling',0,1);
				$this->khjzmianji = get_peizhi('khjzmianji',0,1);
				$this->khsjpinpai = get_peizhi('khsjpinpai',0,1);
				$this->khsshangye = get_peizhi('khsshangye',0,1);
				$this->khaihao2 = explode(",", $ky1['khaihao']);
				$this->khjtzcph2 = explode(",", $ky1['khjtzcph']);
				$this->khjqzdsx2 = explode(",", $ky1['khjqzdsx']);
				$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');
			$Model = new \Think\Model();
			if ($keyuan['khaihao']) {
				$condition1 = "pzming = 'khaihao' and lxid in (".$keyuan['khaihao'].")";
				$khaihao1 = $Model->query("select * from __PEIZHI__ where {$condition1} order by xsshunxu asc");
				$khaihao2 = array();
				foreach ($khaihao1 as $key => $value) {
					$khaihao2[$key] = $value['lxming'];
				}
				$this->khaihao = implode('；',$khaihao2);
			}
			if ($keyuan['khjtzcph']) {
				$condition2 = "pzming = 'khjtzcph' and lxid in (".$keyuan['khjtzcph'].")";
				$khjtzcph1 = $Model->query("select * from __PEIZHI__ where {$condition2} order by xsshunxu asc");
				$khjtzcph2 = array();
				foreach ($khjtzcph1 as $key => $value) {
					$khjtzcph2[$key] = $value['lxming'];
				}
				$this->khjtzcph = implode('；',$khjtzcph2);
			}
			if ($keyuan['khjqzdsx']) {
				$condition3 = "pzming = 'khjqzdsx' and lxid in (".$keyuan['khjqzdsx'].")";
				$khjqzdsx1 = $Model->query("select * from __PEIZHI__ where {$condition3} order by xsshunxu asc");
				$khjqzdsx2 = array();
				foreach ($khjqzdsx1 as $key => $value) {
					$khjqzdsx2[$key] = $value['lxming'];
				}
				$this->khjqzdsx = implode('；',$khjqzdsx2);
			}
			$keyuan = M('keyuan')->where(array('id'=>I('id'),'gongsiid'=>session('gongsiid')))->find();
			$this->keyuan = $keyuan;
			$this->khzhiwu = get_peizhi('khzhiwu',$keyuan['khzhiwu'],3);
			$this->khjiaoyu = get_peizhi('khjiaoyu',$keyuan['khjiaoyu'],3);
			$this->khhunyin = get_peizhi('khhunyin',$keyuan['khhunyin'],3);
			$this->khshouru = get_peizhi('khshouru',$keyuan['khshouru'],3);
			$this->khjzquyu = get_peizhi('khjzquyu',$keyuan['khjzquyu'],3);
			$this->khgzquyu = get_peizhi('khgzquyu',$keyuan['khgzquyu'],3);
			$this->khnianling = get_peizhi('khnianling',$keyuan['khnianling'],3);
			$this->khjzmianji = get_peizhi('khjzmianji',$keyuan['khjzmianji'],3);
			$this->khsjpinpai = get_peizhi('khsjpinpai',$keyuan['khsjpinpai'],3);
			$this->khsshangye = get_peizhi('khsshangye',$keyuan['khsshangye'],3);
			$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->error("异常错误！");
			}
			if ($keyuan['isdel'] == 2) {
				$this->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->error("异常错误！");
			}
			$this->error('请联系客服（19965783436）获取正式版功能！');
		}

		// 客源审核
		public function kyshenhe(){
			$id = I('id');
			$ky1 = M('keyuan')->where(array('id'=>$id))->field('weihurenid,isdel,shtype,mmzl')->find();
			$is_rbac = _rbac_btn_show(955,0,$ky1['weihurenid']);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			if ($ky1['isdel'] == 1) {
				$this->error('客源已删除！');
			}
			if (IS_POST) {
				$shtype = I('shtype');
				if ($ky1['shtype'] == 1 || $ky1['shtype'] == 2){
					$this->error('已审核！');
				}
				if ($shtype == ""){
					$this->error('请选择审核意见！');
				}
				$isok = M('keyuan')->where(array('id'=>$id))->setField('shtype',$shtype);
				if ($isok) {
					$kybh = M('keyuan')->where(array('id'=>$id))->getField('xqbianhao');
					if ($shtype == 1) {
						$xiugai = "通过了";
					}
					if ($shtype == 2) {
						$xiugai = "驳回了";
					}
					$xiugai .= $kybh."的添加。";
					if ($ky1['mmzl'] == 1) {
						$zyzs = "住宅客源（求购）";
					} elseif ($ky1['mmzl'] == 2) {
						$zyzs = "住宅客源（求租）";
					}
					data_rizhi(2,18,$id,$kybh,$xiugai,U('keyuan1/neirong1',array('id'=>$id)),$zyzs);
					$this->success('审核成功');
				} else {
					$this->error('审核失败');
				}
			} else {
				$this->display();
			}
		}

    }

?>