<?php

	/*******************************************************************\
	|																	|
	|	房开源（综合房产ERP管理系统）									|
	|																	|
	|	© 2019-2023 http://www.kaidandashi.com/ All rights reserved.	|
	|																	|
	|	本系统由淮南市银泰软件科技有限公司提供技术支持					|
	|																	|
	|	QQ号：984784483													|
	|																	|
	\*******************************************************************/

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

		//住宅客源
		public function index(){
			if (I('is_ajax')) {
				$sousuo = I('sousuo');
				$mmzl = I('mmzl');
				$zhuangtai = I('zhuangtai');
				$jiage = I('jiage');
				$mianji = I('mianji');
				$jrll = I('jrll');
				$jiqie = I('jiqie');
				$daikan = I('daikan');
				$myky = I('myky');
				$dshenhe = I('dshenhe');
				$bohui = I('bohui');
				$wuxiao = I('wuxiao');
				$isdel = I('isdel');
				$condition = "e.gongsiid=".session('gongsiid');
				$x = _rbac_lists_where(66,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";
				}
				$is_rbacfck = _rbac_node_show(865);
				if (!$is_rbacfck) {
					$condition .= " and a.fpck=2";
				}
				if ($isdel) {
					$condition .= " and a.isdel=1";
				} else {
					$condition .= " and a.isdel=2";
				}
				if ($mmzl) {
					$condition .= " and a.mmzl=".$mmzl;
				}
				$kywxzt = get_options('chengjiao_options','kyfengpan_zhuangtai');
				if ($wuxiao) {
					$condition .= " and a.zhuangtai in (".$kywxzt.")";
				} else {
					$condition .= " and a.zhuangtai not in (".$kywxzt.")";
				}
				if ($zhuangtai) {
					$condition .= " and a.zhuangtai=".$zhuangtai;
				}
				if ($dshenhe) {
					$condition .= " and a.shtype=3";
				}
				if ($bohui) {
					$condition .= " and a.shtype=2";
				}
				if (!$dshenhe && !$bohui) {
					$condition .= " and a.shtype=1";
				}
				if ($myky) {
					$condition .= " and a.weihurenid=".session('uid');
				}
				if ($jrll) {
					$today = "kyzz".date("m-d");
					if (!cookie($today)) {
						$history = 0;
					} else {
						$history = cookie($today);
					}
					$condition .= " and a.id in ($history)";
				}
				if ($jiqie) {
					$condition .= " and a.jiqie=1";
				}
				if ($daikan) {
					$condition .= " and a.daikan=1";
				}
				if ($jiage) {
					$condition .= " and a.xqjiage1<=".$jiage." and a.xqjiage2>=".$jiage;
				}
				if ($mianji) {
					$condition .= " and a.xqmianji1<=".$mianji." and a.xqmianji2>=".$mianji;
				}
				if ($sousuo) {
					$condition .= " and (a.xqbianhao like '%$sousuo%' or a.xqquyu like '%$sousuo%' or a.xqxiaoqu like '%$sousuo%' or a.khxingming like '%$sousuo%' or a.dianhua like '%$sousuo%' or a.weihuren like '%$sousuo%')";
				}
				$field = I('field'); //排序字段名
				$order = I('order'); //排序方式
				$limit = I('limit',30,'int'); //每页条数
				$limit = $limit > 150 ? 150 : $limit;
				if ($field && $order) {
					$kypx = "order by a.".$field." ".$order;
				} else {
					$kypx = "order by a.id desc";
				}
				$Model = new \Think\Model();
				$count = $Model->query("select count(*) from __KEYUAN__ a join __YONGHU__ e on a.weihurenid=e.id where {$condition}");
				$Page = new \Think\Page($count['0']['count(*)'],$limit);
				$list = "select a.* from __KEYUAN__ a join __YONGHU__ e on a.weihurenid=e.id where {$condition} ".$kypx." limit ".$Page->firstRow.','.$Page->listRows;
				$data = $Model->query($list);
				foreach ($data as $key => $value) {
					switch ($value['mmzl']) {
						case '1':
							$jgdw = "万";
							break;
						case '2':
							$jgdw = "元";
							break;
						default:
							$jgdw = "";
							break;
					}
					if ($value['xqmianji1'] && $value['xqmianji2']) {
						$xqmj = $value['xqmianji1']."m²~".$value['xqmianji2']."m²";
					} else if ($value['xqmianji1']) {
						$xqmj = $value['xqmianji1']."m²以上";
					} else if ($value['xqmianji2']) {
						$xqmj = $value['xqmianji2']."m²以下";
					} else {
						$xqmj = "";
					}
					if ($value['xqjiage1'] && $value['xqjiage2']) {
						$xqjg = $value['xqjiage1'].$jgdw."~".$value['xqjiage2'].$jgdw;
					} else if ($value['xqjiage1']) {
						$xqjg = $value['xqjiage1'].$jgdw."以上";
					} else if ($value['xqjiage2']) {
						$xqjg = $value['xqjiage2'].$jgdw."以下";
					} else {
						$xqjg = "";
					}
					if ($value['xqhuxing1'] && $value['xqhuxing2']) {
						$xqhx = $value['xqhuxing1']."室~".$value['xqhuxing2']."室";
					} else if ($value['xqhuxing1']) {
						$xqhx = $value['xqhuxing1']."室以上";
					} else if ($value['xqhuxing2']) {
						$xqhx = $value['xqhuxing2']."室以下";
					} else {
						$xqhx = "";
					}
					$data[$key]['xqmj'] = $xqmj ? $xqmj : "——";
					$data[$key]['xqjg'] = $xqjg ? $xqjg : "——";
					$data[$key]['xqhx'] = $xqhx ? $xqhx : "——";
					$data[$key]['xqlx'] = get_peizhi('fwleixing',$value['xqleixing'],3);
					$data[$key]['khly'] = get_peizhi('laiyuan',$value['khlaiyuan'],3);
					$data[$key]['kyzt'] = get_peizhi('khzhuangtai',$value['zhuangtai'],3);
					$data[$key]['khxm'] = $value['khxingming'].get_peizhi('yezhulx',$value['kehulx'],3);
					$data[$key]['gtjd'] = $value['gtjieduan'] ? get_peizhi('gtjieduan',$value['gtjieduan'],3) : "——";
					$data[$key]['lurusj'] = $value['lurusj'] ? date('Y-m-d H:i:s',$value['lurusj']) : "——";
					$data[$key]['whr'] = zym($value['weihurenid'],5);
					$data[$key]['whrdh'] = zym($value['weihurenid'],14);
					$is_rbacxq = _rbac_btn_show(75,$value['zhuangtai'],$value['weihurenid']);
					$is_rbacsh = _rbac_btn_show(955,0,$value['weihurenid']);
					if ($value['isdel'] == 1) {
						$is_rbachy = _rbac_btn_show(771,$value['zhuangtai'],$value['weihurenid']);
						$is_rbacsc = _rbac_btn_show(772,$value['zhuangtai'],$value['weihurenid']);
						$data[$key]['rbachy'] = $is_rbachy ? 1 : 0;
						$data[$key]['rbacsc'] = $is_rbacsc ? 1 : 0;
					}
					$data[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
					$data[$key]['rbacsh'] = $is_rbacsh ? 1 : 0;
				}
				$return_data = array(
					'code'=>0,
					'count'=>$count['0']['count(*)'],
					'data'=>$data
				);
				$this->ajaxReturn($return_data);
			} else {
				$this->kyzhuangtai = get_peizhi('khzhuangtai',0,1);
				$this->display();
			}
		}

		//添加客源
		public function addKeyuan(){
			$is_rbac = _rbac_btn_show(68,0);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			$mmzl = I('mmzl');
			if (IS_POST) {
				if ($mmzl == 1) {
					$bhao = 'MK';
					$zyzs = "住宅客源（求购）";
				} elseif ($mmzl == 2) {
					$bhao = 'ZK';
					$zyzs = "住宅客源（求租）";
				} else {
					$this->error('异常错误！');
				}
				$dianhua = I('dianhua');
				$cfky = M('keyuan')->where(array('dianhua'=>$dianhua))->getField('dianhua');
				if ($cfky) {
					$this->error('客源已存在');
				}
				$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 (!preg_match("/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[8-9])\d{8}$|^0\d{2,3}-?\d{7,8}$/",$dianhua)) {
					$this->error('手机请输入正确的手机号码或电话号码！');
				}
				if (!$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 ($mmzl == 1) {
					$jgdw = "亿";
				} elseif ($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≤一万的整数！');
				}
				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个字符！');
				}
				$bh = M('bianhao')->where(array('id'=>2))->setInc('bianhao');
				$bh1 = M('bianhao')->where(array('id'=>2))->getField('bianhao');
				$xqxiaoqus = array_unique(array_filter(explode("；",I('xqxiaoqu'))));
				$xqxiaoqu = implode("；",$xqxiaoqus);
				$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 (I('jiqie')) {
					$jiqie = 1;
				} else {
					$jiqie = 0;
				}
				$data['gongsiid'] = session('gongsiid');
				$data['mmzl'] = $mmzl;
				$data['zhuangtai'] = get_options('chengjiao_options','kysike_zhuangtai');
				$data['kydengji'] = I('kydengji');
				$data['xqbianhao'] = $bhao.$bh1;
				$data['xqquyu'] = I('xqquyu');
				$data['xqxiaoqu'] = $xqxiaoqu;
				$data['xqmianji1'] = $xqmianji1;
				$data['xqmianji2'] = $xqmianji2;
				$data['xqjiage1'] = $xqjiage1;
				$data['xqjiage2'] = $xqjiage2;
				$data['xqhuxing1'] = $xqhuxing1;
				$data['xqhuxing2'] = $xqhuxing2;
				$data['louceng1'] = $louceng1;
				$data['louceng2'] = $louceng2;
				$data['fangling1'] = $fangling1;
				$data['fangling2'] = $fangling2;
				$data['xqleixing'] = I('xqleixing');
				$data['fukuan'] = I('fukuan');
				$data['peitao'] = $pt2;
				$data['zhuangxiu'] = $zx2;
				$data['chaoxiang'] = $cx2;
				$data['xqyuanyin'] = I('xqyuanyin');
				$data['beizhu'] = $beizhu;
				$data['jiqie'] = $jiqie;
				$data['gtjieduan'] = I('gtjieduan');
				$data['khlaiyuan'] = I('khlaiyuan');
				$data['khxingming'] = I('khxingming');
				$data['dianhua'] = $dianhua;
				$data['sfzheng'] = $sfzheng;
				$data['qqhao'] = $qqhao;
				$data['youxiang'] = $youxiang;
				$data['weixin'] = I('weixin');
				$data['chexing'] = I('chexing');
				$data['jtgongju'] = I('jtgongju');
				$data['guoji'] = I('guoji');
				$data['minzu'] = I('minzu');
				$data['xflinian'] = I('xflinian');
				$data['kehulx'] = I('kehulx');
				$data['weihuren'] = session('ygmingcheng');
				$data['weihurenid'] = session('uid');
				$data['lururenid'] = session('uid');
				$data['lurusj'] = time();
				$data['xiugaisj'] = time();
				$data['zhgjsj'] = time();
				$isok = M('Keyuan')->add($data);
				if ($isok) {
					if (I('khxqid')) {
						$xiugai = "客户需求转入商机中心（住宅客源）。";
						$data_khxq['zybh'] = $data['xqbianhao'];
						$data_khxq['zylj'] = U('keyuan1/neirong1',array('id'=>$isok));
						$data_khxq['zrsjzx'] = "已被".zym(session('uid'),5)."转入商机中心（住宅客源）。";
						M('fabufy')->where(array("id"=>I('khxqid')))->save($data_khxq);
						data_rizhi(5,1,$isok,$data['xqbianhao'],$xiugai,U('keyuan1/neirong1',array('id'=>$isok)),"外网客户");
					} else {
						$xiugai = "添加了一条客源数据。";
						data_fkywhr(2,$isok,session('uid'));
					}
					data_rizhi(2,1,$isok,$data['xqbianhao'],$xiugai,U('keyuan1/neirong1',array('id'=>$isok)),$zyzs);
					$this->success('已添加，请等待审核！');
				} else {
					$this->error('添加失败！');
				}
			} else {
				if ($mmzl == 1) {
					$this->fukuan = get_peizhi('fukuan',0,1);
				} elseif ($mmzl == 2){
					$this->fukuan = get_peizhi('zlfukuan',0,1);
				} else {
					$this->error('异常错误');
				}
				$this->xqleixing = get_peizhi('fwleixing',0,1);
				$this->chaoxiang = get_peizhi('chaoxiang',0,1);
				$this->kehulx = get_peizhi('yezhulx',0,1);
				$this->peitao = get_peizhi('peitao',0,1);
				$this->laiyuan = get_peizhi('laiyuan',0,1);
				$this->zhuangxiu = get_peizhi('zhuangxiu',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->khzhuangtai = get_peizhi('khzhuangtai',0,1);
				$this->jtgongju = get_peizhi('jtgongju',0,1);
				$this->gtjieduan = get_peizhi('gtjieduan',0,1);
				$this->display();
			}
		}

		//客源内容页
		public function neirong1(){
			$id = I('id');
			$kybh = I('xqbianhao');
			if ($id) {
				$keyuan = M('keyuan')->where(array('id'=>$id))->find();
			} elseif ($kybh) {
				$keyuan = M('keyuan')->where(array('xqbianhao'=>$kybh))->find();
			}
			if (!$keyuan) {
				$this->error('未找到客源！');
			}
			$is_rbac = _rbac_btn_show(75,$keyuan['zhuangtai'],$keyuan['weihurenid']);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			$is_rbacfck = _rbac_node_show(865);
			if (!$is_rbacfck && ($keyuan['fpck'] != 2)) {
				$this->redirect('Index/error');
			}
			$options_arr = get_options('chengjiao_options',0);
			$this->options_arr = $options_arr;
			$this->assign('keyuan',$keyuan);
			$this->lrdh = zym($keyuan['lururenid'],14);
			$this->whdh = zym($keyuan['weihurenid'],14);
			$this->fpdh = zym($keyuan['fengpanrenid'],14);
			$id = I('id');
			$qitiannei = time()-604800;
			$this->daikanshu = M('kydaikan')->where(array('kylx'=>2,'kyid'=>$id,'dkshijian'=>array('gt',$qitiannei)))->count();
			$iskzq = 1;
			$kywxzt = get_options('chengjiao_options','kyfengpan_zhuangtai');
			if (!in_array($keyuan['zhuangtai'],$kywxzt)) {
				$kywhrs = M('fkywhr')->where(array('zylx'=>2,'id'=>$keyuan['id']))->select();
				foreach ($kywhrs as $key => $value) {
					if ($value['whrid'] == session('uid')) {
						$iskzq = 2;
					}
				}
			}
			$this->iskzq = $iskzq;
			if ($keyuan['mmzl'] == 1) {
				$this->fukuan = get_peizhi('fukuan',$keyuan['fukuan'],3);
				$this->xqjgdw = "万";
			} elseif ($keyuan['mmzl'] == 2) {
				$this->fukuan = get_peizhi('zlfukuan',$keyuan['fukuan'],3);
				$this->xqjgdw = "元";
			}
			$this->guoji = get_peizhi('guoji',$keyuan['guoji'],3);
			$this->minzu = get_peizhi('minzu',$keyuan['minzu'],3);
			$this->kehulx = get_peizhi('yezhulx',$keyuan['kehulx'],3);
			$this->jtgongju = get_peizhi('jtgongju',$keyuan['jtgongju'],3);
			$this->kydengji = get_peizhi('kydengji',$keyuan['kydengji'],3);
			$this->xflinian = get_peizhi('xflinian',$keyuan['xflinian'],3);
			$this->khlaiyuan = get_peizhi('laiyuan',$keyuan['khlaiyuan'],3);
			$this->gtjieduan = get_peizhi('gtjieduan',$keyuan['gtjieduan'],3);
			$this->kygenjin = M('kygenjin')->where(array('kylx'=>2,'kyid'=>$keyuan['id']))->order('gjshijian desc')->select();
			$this->kydaikan = M('kydaikan')->where(array('kylx'=>2,'kyid'=>$keyuan['id']))->order('dkshijian desc')->select();
			$this->kytixing = M('kytixing')->where(array('kylx'=>2,'kyid'=>$keyuan['id']))->order('txshijian desc')->select();
			$this->volist = M('rizhi')->where(array('zy_type'=>2,'zy_bianhao'=>$keyuan['xqbianhao']))->order('id desc')->select();
			$today = "kyzz".date("m-d");
			if (!cookie($today)) {
				cookie($today,$id,3600*24);
			} else {
				$value = cookie($today);
				$val = $value.','.$id;
				cookie($today,$val,3600*24);
			}
			$Model = new \Think\Model();
			if ($keyuan['peitao']) {
				$condition1 = "pzming = 'peitao' and lxid in (".$keyuan['peitao'].")";
				$peitao1 = $Model->query("select * from __PEIZHI__ where {$condition1} order by xsshunxu asc");
				$peitao2 = array();
				foreach ($peitao1 as $key => $value) {
					$peitao2[$key] = $value['lxming'];
				}
				$this->peitao = implode('；',$peitao2);
			}
			if ($keyuan['chaoxiang']) {
				$condition2 = "pzming = 'chaoxiang' and lxid in (".$keyuan['chaoxiang'].")";
				$chaoxiang1 = $Model->query("select * from __PEIZHI__ where {$condition2} order by xsshunxu asc");
				$chaoxiang2 = array();
				foreach ($chaoxiang1 as $key => $value) {
					$chaoxiang2[$key] = $value['lxming'];
				}
				$this->chaoxiang = implode('；',$chaoxiang2);
			}
			if ($keyuan['zhuangxiu']) {
				$condition3 = "pzming = 'zhuangxiu' and lxid in (".$keyuan['zhuangxiu'].")";
				$zhuangxiu1 = $Model->query("select * from __PEIZHI__ where {$condition3} order by xsshunxu asc");
				$zhuangxiu2 = array();
				foreach ($zhuangxiu1 as $key => $value) {
					$zhuangxiu2[$key] = $value['lxming'];
				}
				$this->zhuangxiu = implode('；',$zhuangxiu2);
			}
			$this->display();
		}

		//客源跟进列表
		public function kygenjin(){
			if (I('is_ajax')) {
				$bumen = I("get.bumen",0,'intval');
				$sousuo = I('sousuo');
				$kylx = I('kylx');
				$condition = "e.gongsiid=".session('gongsiid');
				$x = _rbac_lists_where(237,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";
				}
				if ($bumen) {
					$string = get_bm_sz($bumen,2);
					$condition .= " and e.bumen in ($string)";
				}
				$kylxs = "2,15,16,17,18,19,20,21,22";
				$kylxs1 = explode(",",$kylxs);
				if (in_array($kylx,$kylxs1)) {
					$condition .= " and a.kylx = ".$kylx;
				} else {
					$condition .= " and a.kylx in (".$kylxs.")";
				}
				$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.gjshijian>=".$shijian3." and a.gjshijian<".$shijian4;
				} elseif ($shijian3) {
					$condition .= " and a.gjshijian>=".$shijian3;
				} elseif ($shijian4) {
					$condition .= " and a.gjshijian<".$shijian4;
				}
				if ($sousuo) {
					$condition .= " and ((e.pinyinjs like '%$sousuo%') or (e.ygmingcheng like '%$sousuo%') or (e.dianhua like '%$sousuo%') or (a.kybh like '%$sousuo%'))";
				}
				$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 __KYGENJIN__ a join __YONGHU__ e on a.gjuid=e.id where {$condition}");
				$Page = new \Think\Page($count['0']['count(*)'],$limit);
				$list = "select a.*,e.ygmingcheng,e.bumen from __KYGENJIN__ a join __YONGHU__ e on a.gjuid=e.id where {$condition} ".$gjpx." limit ".$Page->firstRow.','.$Page->listRows;
				$data = $Model->query($list);
				foreach ($data as $key => $value) {
					switch ($value['kylx']) {
						case '2':
							$kyb = M('keyuan');
							$kylxm = "住宅客源";
							$kyxqlj = U('keyuan1/neirong1',array('id'=>$value['kyid']));
							break;
						case '15':
							$kyb = M('kyxiezilou');
							$kylxm = "写字楼客源";
							$kyxqlj = U('kyxiezilou/neirong',array('id'=>$value['kyid']));
							break;
						case '16':
							$kyb = M('kyshangpu');
							$kylxm = "商铺客源";
							$kyxqlj = U('kyshangpu/neirong',array('id'=>$value['kyid']));
							break;
						case '17':
							$kyb2 = M('kyxinfang');
							$kylxm = "新房客源";
							$kyxqlj = U('kyxinfang/neirong',array('id'=>$value['kyid']));
							break;
						case '18':
							$kyb2 = M('kyjinrong');
							$kylxm = "金融客源";
							$kyxqlj = U('kyjinrong/neirong',array('id'=>$value['kyid']));
							break;
						case '19':
							$kyb2 = M('kyzhuangxiu');
							$kylxm = "装修客源";
							$kyxqlj = U('kyzhuangxiu/neirong',array('id'=>$value['kyid']));
							break;
						case '20':
							$kyb2 = M('kygongshang');
							$kylxm = "工商客源";
							$kyxqlj = U('kygongshang/neirong',array('id'=>$value['kyid']));
							break;
						case '21':
							$kyb2 = M('kykuaiji');
							$kylxm = "会计客源";
							$kyxqlj = U('kykuaiji/neirong',array('id'=>$value['kyid']));
							break;
						case '22':
							$kyb2 = M('kyjiaju');
							$kylxm = "家具客源";
							$kyxqlj = U('kyjiaju/neirong',array('id'=>$value['kyid']));
							break;
						default:
							$kylxm = "";
							$kyxqlj = "";
							break;
					}
					if ($kyb) {
						$kyxq = $kyb->where(array('id'=>$value['kyid']))->field('khxingming,kehulx,mmzl,zhuangtai,weihurenid')->find();
						switch ($kyxq['mmzl']) {
							case '1':
								$mmzl = "（求购）";
								break;
							case '2':
								$mmzl = "（求租）";
								break;
							default:
								$mmzl = "";
								break;
						}
						$kylxmc = $kylxm.$mmzl;
					}
					if ($kyb2) {
						$kyxq = $kyb2->where(array('id'=>$value['kyid']))->field('khxingming,kehulx,zhuangtai,weihurenid')->find();
						$kylxmc = $kylxm;
					}
					if ($kyxq) {
						$is_rbacxq = _rbac_btn_show(75,$kyxq['zhuangtai'],$kyxq['weihurenid']);
					} else {
						$is_rbacxq = "";
					}
					$data[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
					$data[$key]['bumen'] = zym($value['bumen'],6);
					$data[$key]['gjshijian'] = $value['gjshijian'] ? date('Y-m-d H:i:s',$value['gjshijian']) : "";
					$data[$key]['genjinfs'] = get_peizhi("genjinfs",$value['genjinfs'],3);
					$data[$key]['khxingming'] = $kyxq['khxingming'].get_peizhi('yezhulx',$kyxq['kehulx'],3);
					$data[$key]['kylxmc'] = $kylxmc;
					$data[$key]['kyxqlj'] = $kyxqlj;
					$data[$key]['weihurenid'] = $kyxq['weihurenid'];
				}
				$return_data = array(
					'code'=>0,
					'count'=>$count['0']['count(*)'],
					'data'=>$data
				);
				$this->ajaxReturn($return_data);
			} else {
				$select_categorys = bumen_option( $bumen, 237);
				$this->assign("select_categorys", $select_categorys);
				$this->display();
			}
		}

		//客源带看列表
		public function kydaikan(){
			if (I('is_ajax')) {
				$bumen = I("get.bumen",0,'intval');
				$sousuo = I('sousuo');
				$kylx = I('kylx');
				$condition = "e.gongsiid=".session('gongsiid');
				$x = _rbac_lists_where(238,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";
				}
				if ($bumen) {
					$string = get_bm_sz($bumen,2);
					$condition .= " and e.bumen in ($string)";
				}
				$kylxs = "2,15,16,17";
				$kylxs1 = explode(",",$kylxs);
				if (in_array($kylx,$kylxs1)) {
					$condition .= " and a.kylx = ".$kylx;
				} else {
					$condition .= " and a.kylx in (".$kylxs.")";
				}
				$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.dkshijian>=".$shijian3." and a.dkshijian<".$shijian4;
				} elseif ($shijian3) {
					$condition .= " and a.dkshijian>=".$shijian3;
				} elseif ($shijian4) {
					$condition .= " and a.dkshijian<".$shijian4;
				}
				if ($sousuo) {//搜索
					$condition .= " and ((e.pinyinjs like '%$sousuo%') or (e.ygmingcheng like '%$sousuo%') or (e.dianhua like '%$sousuo%') or (a.kybh like '%$sousuo%') or (a.fybh like '%$sousuo%'))";
				}
				$field = I('field'); //排序字段名
				$order = I('order'); //排序方式
				$limit = I('limit',30,'int'); //每页条数
				$limit = $limit > 150 ? 150 : $limit;
				if ($field && $order) {
					$dkpx = "order by a.".$field." ".$order;
				} else {
					$dkpx = "order by a.id desc";
				}
				$Model = new \Think\Model();
				$count = $Model->query("select count(*) from __KYDAIKAN__ a join __YONGHU__ e on a.dkuid=e.id where {$condition}");
				$Page = new \Think\Page($count['0']['count(*)'],$limit);
				$list = "select a.*,e.ygmingcheng,e.bumen from __KYDAIKAN__ a join __YONGHU__ e on a.dkuid=e.id where {$condition} ".$dkpx." limit ".$Page->firstRow.','.$Page->listRows;
				$data = $Model->query($list);
				foreach ($data as $key => $value) {
					if ($value['kylx'] == 17) {
						$kyxq = M('kyxinfang')->where(array('id'=>$value['kyid']))->field('khxingming,kehulx,zhuangtai,weihurenid')->find();
						$kylxm = "新房客源";
						$kyxqlj = U('kyxinfang/neirong',array('id'=>$value['kyid']));
						$fywhr = M('ysfangyuan')->where(array('ysbianhao'=>$dk['fybh']))->getField('weihurenid');
						$fyxqlj = U('fyxinloupan/ysxiangqing',array('ysbianhao'=>$value['fybh']));
						$is_rbacfy = _rbac_btn_show(272,0,$fywhr);
					} else {
						switch ($value['kylx']) {
							case '2':
								$kyb = M('keyuan');
								$kylxm = "住宅客源";
								$kyxqlj = U('keyuan1/neirong1',array('id'=>$value['kyid']));
								$fyb = M('fangyuan');
								$fyxqlj = U('fangyuan/neirong',array('bianhao'=>$value['fybh']));
								break;
							case '15':
								$kyb = M('kyxiezilou');
								$kylxm = "写字楼客源";
								$kyxqlj = U('kyxiezilou/neirong',array('id'=>$value['kyid']));
								$fyb = M('fyxiezilou');
								$fyxqlj = U('fyxiezilou/neirong',array('bianhao'=>$value['fybh']));
								break;
							case '16':
								$kyb = M('kyshangpu');
								$kylxm = "商铺客源";
								$kyxqlj = U('kyshangpu/neirong',array('id'=>$value['kyid']));
								$fyb = M('fyshangpu');
								$fyxqlj = U('fyshangpu/neirong',array('bianhao'=>$value['fybh']));
								break;
							default:
								$kyb = "";
								$kylxm = "";
								$kyxqlj = "";
								$fyb = "";
								$fyxqlj = "";
								break;
						}
						if ($kyb) {
							$kyxq = $kyb->where(array('id'=>$value['kyid']))->field('khxingming,kehulx,mmzl,zhuangtai,weihurenid')->find();
						}
						if ($fyb) {
							$fyxq = $fyb->where(array('bianhao'=>$value['fybh']))->field('zhuangtai,weihurenid')->find();
						}
						switch ($kyxq['mmzl']) {
							case '1':
								$mmzl = "（求购）";
								break;
							case '2':
								$mmzl = "（求租）";
								break;
							default:
								$mmzl = "";
								break;
						}
						$kylxmc = $kylxm.$mmzl;
						if ($fyxq) {
							$is_rbacfy = _rbac_btn_show(46,$fyxq['zhuangtai'],$fyxq['weihurenid']);
						} else {
							$is_rbacfy = "";
						}
					}
					if ($kyxq) {
						$is_rbacky = _rbac_btn_show(75,$kyxq['zhuangtai'],$kyxq['weihurenid']);
					} else {
						$is_rbacky = "";
					}
					$data[$key]['rbacky'] = $is_rbacky ? 1 : 0;
					$data[$key]['rbacfy'] = $is_rbacfy ? 1 : 0;
					$data[$key]['bumen'] = zym($value['bumen'],6);
					$data[$key]['dkshijian'] = $value['dkshijian'] ? date('Y-m-d H:i:s',$value['dkshijian']) : "";
					$data[$key]['peikanren'] = $value['peikanid'] ? zym($value['peikanid'],5) : "——";
					$data[$key]['khxingming'] = $kyxq['khxingming'].get_peizhi('yezhulx',$kyxq['kehulx'],3);
					$data[$key]['kylxmc'] = $kylxmc;
					$data[$key]['kyxqlj'] = $kyxqlj;
					$data[$key]['fyxqlj'] = $fyxqlj;
					$data[$key]['weihurenid'] = $kyxq['weihurenid'];
				}
				$return_data = array(
					'code'=>0,
					'count'=>$count['0']['count(*)'],
					'data'=>$data
				);
				$this->ajaxReturn($return_data);
			} else {
				$select_categorys = bumen_option( $bumen, 238);
				$this->assign("select_categorys", $select_categorys);
				$this->display();
			}
		}

		//客源提醒列表
		public function kytixing(){
			if (I('is_ajax')) {
				$bumen = I("get.bumen",0,'intval');
				$sousuo = I('sousuo');
				$kylx = I('kylx');
				$condition = "e.gongsiid=".session('gongsiid');
				$x = _rbac_lists_where(858,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";
				}
				if ($bumen) {
					$string = get_bm_sz($bumen,2);
					$condition .= " and e.bumen in ($string)";
				}
				$kylxs = "2,15,16,17,18,19,20,21,22";
				$kylxs1 = explode(",",$kylxs);
				if (in_array($kylx,$kylxs1)) {
					$condition .= " and a.kylx = ".$kylx;
				} else {
					$condition .= " and a.kylx in (".$kylxs.")";
				}
				$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.time>=".$shijian3." and a.time<".$shijian4;
				} elseif ($shijian3) {
					$condition .= " and a.time>=".$shijian3;
				} elseif ($shijian4) {
					$condition .= " and a.time<".$shijian4;
				}
				if ($sousuo) {
					$condition .= " and ((e.pinyinjs like '%$sousuo%') or (e.ygmingcheng like '%$sousuo%') or (e.dianhua like '%$sousuo%') or (a.kybh like '%$sousuo%'))";
				}
				$field = I('field'); //排序字段名
				$order = I('order'); //排序方式
				$limit = I('limit',30,'int'); //每页条数
				$limit = $limit > 150 ? 150 : $limit;
				if ($field && $order) {
					$txpx = "order by a.".$field." ".$order;
				} else {
					$txpx = "order by a.id desc";
				}
				$Model = new \Think\Model();
				$count = $Model->query("select count(*) from __KYTIXING__ a join __YONGHU__ e on a.txuid=e.id where {$condition}");
				$Page = new \Think\Page($count['0']['count(*)'],$limit);
				$list = "select a.*,e.ygmingcheng,e.bumen,e.dianhua from __KYTIXING__ a join __YONGHU__ e on a.txuid=e.id where {$condition} ".$txpx." limit ".$Page->firstRow.','.$Page->listRows;
				$data = $Model->query($list);
				foreach ($data as $key => $value) {
					switch ($value['kylx']) {
						case '2':
							$kyb = M('keyuan');
							$kylxm = "住宅客源";
							$kyxqlj = U('keyuan1/neirong1',array('id'=>$value['kyid']));
							break;
						case '15':
							$kyb = M('kyxiezilou');
							$kylxm = "写字楼客源";
							$kyxqlj = U('kyxiezilou/neirong',array('id'=>$value['kyid']));
							break;
						case '16':
							$kyb = M('kyshangpu');
							$kylxm = "商铺客源";
							$kyxqlj = U('kyshangpu/neirong',array('id'=>$value['kyid']));
							break;
						case '17':
							$kyb2 = M('kyxinfang');
							$kylxm = "新房客源";
							$kyxqlj = U('kyxinfang/neirong',array('id'=>$value['kyid']));
							break;
						case '18':
							$kyb2 = M('kyjinrong');
							$kylxm = "金融客源";
							$kyxqlj = U('kyjinrong/neirong',array('id'=>$value['kyid']));
							break;
						case '19':
							$kyb2 = M('kyzhuangxiu');
							$kylxm = "装修客源";
							$kyxqlj = U('kyzhuangxiu/neirong',array('id'=>$value['kyid']));
							break;
						case '20':
							$kyb2 = M('kygongshang');
							$kylxm = "工商客源";
							$kyxqlj = U('kygongshang/neirong',array('id'=>$value['kyid']));
							break;
						case '21':
							$kyb2 = M('kykuaiji');
							$kylxm = "会计客源";
							$kyxqlj = U('kykuaiji/neirong',array('id'=>$value['kyid']));
							break;
						case '22':
							$kyb2 = M('kyjiaju');
							$kylxm = "家具客源";
							$kyxqlj = U('kyjiaju/neirong',array('id'=>$value['kyid']));
							break;
						default:
							$kyb = "";
							$kylxm = "";
							$kyxqlj = "";
							break;
					}
					if ($kyb) {
						$kyxq = $kyb->where(array('id'=>$value['kyid']))->field('khxingming,kehulx,mmzl,zhuangtai,weihurenid')->find();
						switch ($kyxq['mmzl']) {
							case '1':
								$mmzl = "（求购）";
								break;
							case '2':
								$mmzl = "（求租）";
								break;
							default:
								$mmzl = "";
								break;
						}
						$kylxmc = $kylxm.$mmzl;
					}
					if ($kyb2) {
						$kyxq = $kyb->where(array('id'=>$value['kyid']))->field('khxingming,kehulx,zhuangtai,weihurenid')->find();
						$kylxmc = $kylxm;
					}
					if ($kyxq) {
						$is_rbacxq = _rbac_btn_show(75,$kyxq['zhuangtai'],$kyxq['weihurenid']);
					} else {
						$is_rbacxq = "";
					}
					$data[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
					$data[$key]['bumen'] = zym($value['bumen'],6);
					$data[$key]['txshijian'] = $value['txshijian'] ? date('Y-m-d H:i:s',$value['txshijian']) : "";
					$data[$key]['khxingming'] = $kyxq['khxingming'].get_peizhi('yezhulx',$kyxq['kehulx'],3);
					$data[$key]['kylxmc'] = $kylxmc;
					$data[$key]['kyxqlj'] = $kyxqlj;
					$data[$key]['weihurenid'] = $kyxq['weihurenid'];
					$data[$key]['tjren'] = zym($value['adduid'],5);
					$data[$key]['tjrdh'] = zym($value['adduid'],14);
					$data[$key]['time'] = $value['time'] ? date('Y-m-d H:i:s',$value['time']) : "";
				}
				$return_data = array(
					'code'=>0,
					'count'=>$count['0']['count(*)'],
					'data'=>$data
				);
				$this->ajaxReturn($return_data);
			} else {
				$select_categorys = bumen_option( $bumen, 858);
				$this->assign("select_categorys", $select_categorys);
				$this->display();
			}
		}

		//客源日志
		public function rizhi(){
			if (I('is_ajax')) {
				/*权限控制开始*/
				$condition = "e.gongsiid=".session('gongsiid');
				$x = _rbac_lists_where(254,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";
				}
				$is_rbac = _rbac_node_show(256);
				if (!$is_rbac) {
					$condition .= " and a.cz_type <> 7";
				}
				/*权限控制结束*/
				$sousuo = I('sousuo');
				$zy_type = I('zy_type');
				$zylxs = "2,15,16,17,18,19,20,21,22";
				$zylxs1 = explode(",",$zylxs);
				if (in_array($zy_type,$zylxs1)) {
					$condition .= " and a.zy_type = ".$zy_type;
				} else {
					$condition .= " and a.zy_type in (".$zylxs.")";
				}
				$shijian = I('shijian');
				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;
				}
				if ($sousuo) {
					$condition .= " and (e.pinyinjs like '%$sousuo%' or e.ygmingcheng like '%$sousuo%' or a.zy_bianhao like '%$sousuo%' or a.zy_remark like '%$sousuo%')";
				}
				$field = I('field'); //排序字段名
				$order = I('order'); //排序方式
				$limit = I('limit',30,'int'); //每页条数
				$limit = $limit > 150 ? 150 : $limit;
				if ($field && $order) {
					$rzpx = "order by a.".$field." ".$order;
				} else {
					$rzpx = "order by a.addtime desc";
				}
				$Model = new \Think\Model();
				$count = $Model->query("select count(*) from __RIZHI__ a join __YONGHU__ e on a.uid=e.id where {$condition}");
				$Page = new \Think\Page($count['0']['count(*)'],$limit);
				$list = "select a.*,e.ygmingcheng,e.bumen from __RIZHI__ a join __YONGHU__ e on a.uid=e.id where {$condition} ".$rzpx." limit ".$Page->firstRow.','.$Page->listRows;
				$data=$Model->query($list);
				foreach ($data as $key => $value) {
					switch ($value['zy_type']) {
						case '2':
							$kyb = M('keyuan');
							break;
						case '15':
							$kyb = M('kyxiezilou');
							break;
						case '16':
							$kyb = M('kyshangpu');
							break;
						case '17':
							$kyb = M('kyxinfang');
							break;
						case '18':
							$kyb = M('kyjinrong');
							break;
						case '19':
							$kyb = M('kyzhuangxiu');
							break;
						case '20':
							$kyb = M('kygongshang');
							break;
						case '21':
							$kyb = M('kykuaiji');
							break;
						case '22':
							$kyb = M('kyjiaju');
							break;
						default:
							$kyb = "";
							break;
					}
					if ($kyb) {
						$ky = $kyb->where(array('bianhao'=>$value['zy_bianhao']))->field('zhuangtai,weihurenid')->find();
					}
					$data[$key]['kyxqqx'] = _rbac_btn_show(75,$ky['zhuangtai'],$ky['weihurenid']) == true ? '1' : '0';
					switch ($value['cz_type']) {
						case '1':
							$data[$key]['cz_type'] = "新增";
							break;
						case '2':
							$data[$key]['cz_type'] = "基础修改";
							break;
						case '3':
							$data[$key]['cz_type'] = "抓取";
							break;
						case '5':
							$data[$key]['cz_type'] = "写跟进";
							break;
						case '7':
							$data[$key]['cz_type'] = "核心修改";
							break;
						case '8':
							$data[$key]['cz_type'] = "删除";
							break;
						case '9':
							$data[$key]['cz_type'] = "变更维护人";
							break;
						case '14':
							$data[$key]['cz_type'] = "封开盘";
							break;
						case '16':
							$data[$key]['cz_type'] = "写提醒";
							break;
						case '17':
							$data[$key]['cz_type'] = "写带看";
							break;
						case '18':
							$data[$key]['cz_type'] = "审核";
							break;
						case '24':
							$data[$key]['cz_type'] = "变更状态";
							break;
						default:
							$data[$key]['cz_type'] = "——";
							break;
					}
					$data[$key]['bumen'] = zym($value['bumen'],6);
					$data[$key]['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();// 输出模板
			}
		}

	}

?>