<?php
/*********************************************
*骑士猎头顾问列表
* *******************************************/
function tpl_function_qishi_hunter_list($params, &$smarty)
{
global $db,$_CFG;
$arrset=explode(',',$params['set']);
foreach($arrset as $str)
{
$a=explode(':',$str);
	switch ($a[0])
	{
	case "列表名":
		$aset['listname'] = $a[1];
		break;
	case "显示数目":
		$aset['row'] = $a[1];
		break;
	case "开始位置":
		$aset['start'] = $a[1];
		break;
	case "猎头名长度":
		$aset['hunterlen'] = $a[1];
		break;
	case "公司名长度":
		$aset['companynamelen'] = $a[1];
		break;
	case "擅长行业长度":
		$aset['goodtradelen'] = $a[1];
		break;
	case "擅长职能长度":
		$aset['goodcategorylen'] = $a[1];
		break;
	case "描述长度":
		$aset['brieflylen'] = $a[1];
		break;
	case "填补字符":
		$aset['dot'] = $a[1];
		break;
	case "地区分类":
		$aset['citycategory'] = $a[1];
		break;
	case "地区大类":
		$aset['district'] = $a[1];
		break;
	case "地区小类":
		$aset['sdistrict'] = $a[1];
		break;
	case "猎头头衔":
		$aset['rank'] = $a[1];
		break;
	case "日期范围":
		$aset['settr'] = $a[1];
		break;
	case "排序":
		$aset['displayorder'] = $a[1];
		break;
	case "分页显示":
		$aset['page'] = $a[1];
		break;
	case "会员UID":
		$aset['uid'] = $a[1];
		break;
	case "猎头页面":
		$aset['huntershow'] = $a[1];
		break;
	case "列表页":
		$aset['listpage'] = $a[1];
		break;
	case "关键字":
		$aset['key'] = $a[1];
		break;
	case "擅长行业":
		$aset['goodtrade'] = $a[1];
		break;
	case "擅长职能":
		$aset['goodcategory'] = $a[1];
		break;
	case "猎头等级":
		$aset['level_meal'] = $a[1];
		break;
	case "从业年限":
		$aset['experience'] = $a[1];
		break;
	case "性别":
		$aset['sex'] = $a[1];
		break;
	case "年龄要求":
		$aset['age'] = $a[1];
		break;
	case '认证':
		$aset['audit'] = $a[1];
		break;
	}
}
$aset=array_map("get_smarty_request",$aset);
$aset['listname']=isset($aset['listname'])?$aset['listname']:"list";
$aset['listpage']=isset($aset['listpage'])?$aset['listpage']:"QS_hunterlist";
$aset['row']=intval($aset['row'])>0?intval($aset['row']):10;
if ($aset['row']>30)$aset['row']=30;
$aset['start']=isset($aset['start'])?intval($aset['start']):0;
$aset['hunterlen']=isset($aset['hunterlen'])?intval($aset['hunterlen']):8;
$aset['companynamelen']=isset($aset['companynamelen'])?intval($aset['companynamelen']):15;
$aset['goodtradelen']=isset($aset['goodtradelen'])?intval($aset['goodtradelen']):13;
$aset['goodcategorylen']=isset($aset['goodcategorylen'])?intval($aset['goodcategorylen']):13;
$aset['rank']=isset($aset['rank'])?intval($aset['rank']):0;
$aset['brieflylen']=isset($aset['brieflylen'])?intval($aset['brieflylen']):0;
$aset['audit']=isset($aset['audit'])?intval($aset['audit']):1;

// $orderbysql=" ORDER BY refreshtime desc ";
$orderbysql = "";
if (isset($aset['displayorder']))
{
		$arr=explode('>',$aset['displayorder']);
		// 排序方式
		if($arr[1]=='desc'){
			$arr[1]="desc";
		}
		elseif($arr[1]=="asc")
		{
			$arr[1]="asc";
		}
		else
		{
			$arr[1]="desc";
		}
		if ($arr[0]=="rtime")
		{
		$orderbysql=" ORDER BY last_login_time {$arr[1]}";
		}
		elseif ($arr[0]=="atime")
		{
		$orderbysql=" ORDER BY addtime {$arr[1]}";
		}
		elseif ($arr[0]=="hot")
		{
		$orderbysql=" ORDER BY click {$arr[1]}";
		}
		elseif($arr[0]=="experience"){
			$orderbysql=" ORDER BY worktime asc";
		}
		else
		{
		$orderbysql=" ORDER BY level {$arr[1]}";
		}
}
else
{
$orderbysql=" ORDER BY last_login_time desc";
}
if (isset($aset['settr']) && $aset['settr']<>'')
{
	$settr=intval($aset['settr']);
	if ($settr>0)
	{
		$settr_val=intval(strtotime("-".$aset['settr']." day"));
		$wheresql.=" AND last_login_time>".$settr_val;
	}
}

if (isset($aset['uid'])  && $aset['uid']<>'')
{
	$wheresql.=" AND h.uid=".intval($aset['uid']);
}
if (isset($aset['rank']) && $aset['rank']<>'')
{
	$wheresql.=" AND h.rank=".intval($aset['rank'])." ";
}
if (isset($aset['level_meal']) && $aset['level_meal']<>'')
{
	$wheresql.=" AND h.level_meal=".intval($aset['level_meal'])." ";
}
//从业年限
if (isset($aset['experience']) && $aset['experience']<>'')
{
	$wheresql.=" AND h.worktime=".intval($aset['experience'])." ";
}
//性别要求  1->男   2->女   3->不限
if (isset($aset['sex']) && $aset['sex']<>'')
{
	if(intval($aset['sex'])>0 && intval($aset['sex'])<3){
		$wheresql.=" AND h.sex=".intval($aset['sex'])." ";
	}
}
//地区分类
if (!empty($aset['citycategory']))
{
	$dsql=$xsql="";
	$arr=explode(",",$aset['citycategory']);
	$arr=array_unique($arr);
	if (count($arr)>10) exit();
	foreach($arr as $sid)
	{
		$cat=explode(".",$sid);
		if (intval($cat[1])===0)
		{
		$dsql.= " OR h.district =".intval($cat[0]);
		}
		else
		{
		$xsql.= " OR h.sdistrict =".intval($cat[1]);
		}	
	}
	$wheresql.=" AND  (".ltrim(ltrim($dsql.$xsql),'OR').") ";
}
else
{
	if (isset($aset['district'])  && $aset['district']<>'')
	{
		if (strpos($aset['district'],"-"))
		{
			$or=$orsql="";
			$arr=explode("-",$aset['district']);
			$arr=array_unique($arr);
			if (count($arr)>20) exit();
			$sqlin=implode(",",$arr);
			if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
			{
				$wheresql.=" AND h.district IN  ({$sqlin}) ";
			}
		}
		else
		{
			$wheresql.=" AND h.district =".intval($aset['district']);
		}
	}
	if (isset($aset['sdistrict'])  && $aset['sdistrict']<>'')
	{
		if (strpos($aset['sdistrict'],"-"))
		{
			$or=$orsql="";
			$arr=explode("-",$aset['sdistrict']);
			$arr=array_unique($arr);
			if (count($arr)>10) exit();
			$sqlin=implode(",",$arr);
			if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
			{
				$wheresql.=" AND h.sdistrict IN  ({$sqlin}) ";
			}
		}
		else
		{
			$wheresql.=" AND h.sdistrict =".intval($aset['sdistrict']);
		}
	}	
}
//擅长行业
if (!empty($aset['goodtrade']))
{
	if (strpos($aset['goodtrade'],","))
	{
		$or=$orsql="";
		$arr=explode(",",$aset['goodtrade']);
		if (count($arr)>10) exit();
		$sqlin=implode(",",$arr);
		if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$sqlin))
		{
			$joinwheresql_trade.=" AND trade IN  ({$sqlin}) ";
		}
	}
	else
	{
	$joinwheresql_trade.=" AND trade=".intval($aset['goodtrade']);
	}
	
	if (!empty($joinwheresql_trade))
	{
	$joinwheresql_trade=" WHERE ".ltrim(ltrim($joinwheresql_trade),'AND');
	}
	$joinsql=$joinsql==""?"  INNER  JOIN  ( SELECT DISTINCT uid FROM ".table('hunter_trade')." {$joinwheresql_trade} )AS t ON  h.uid=t.uid ":$joinsql."  INNER  JOIN  ( SELECT DISTINCT uid FROM ".table('hunter_trade')." {$joinwheresql_trade} )AS t ON  h.uid=t.uid ";
}
//擅长职能
if (!empty($aset['goodcategory']))
{
	if (strpos($aset['goodcategory'],","))
	{
		$dsql=$xsql="";
		$arr=explode(",",$aset['goodcategory']);
		$arr=array_unique($arr);
		if (count($arr)>10) exit();
		foreach($arr as $sid)
		{
			$cat=explode(".",$sid);
			if (intval($cat[2])===0)
			{
				$dsql.= " OR category =".intval($cat[1]);
			}
			else
			{
				$xsql.= " OR subclass =".intval($cat[2]);
			}
		}
		$joinwheresql_category.=" AND  (".ltrim(ltrim($dsql.$xsql),'OR').") ";
	}
	else
	{
		$dsql=$xsql="";
		$cat=explode(".",$aset['goodcategory']);
		if (intval($cat[2])===0)
		{
			$dsql.= " OR category =".intval($cat[1]);
		}
		else
		{
			$xsql.= " OR subclass =".intval($cat[2]);
		}
		$joinwheresql_category.=" AND  (".ltrim(ltrim($dsql.$xsql),'OR').") ";
	}
	if (!empty($joinwheresql_category))
	{
	$joinwheresql_category=" WHERE ".ltrim(ltrim($joinwheresql_category),'AND');
	}
	$joinsql=$joinsql==""?"  INNER  JOIN  ( SELECT DISTINCT uid FROM ".table('hunter_category')." {$joinwheresql_category} )AS c ON  h.uid=c.uid ":$joinsql."  INNER  JOIN  ( SELECT DISTINCT uid FROM ".table('hunter_category')." {$joinwheresql_category} )AS c ON  h.uid=c.uid ";
}
// 年龄要求
if(isset($aset['age'])  && $aset['age']<>'')
{
	$Y=date("Y",time());
	$age_arr=explode(",",$aset['age']);
	$age_max=$Y-$age_arr[0]; 
	$age_min=$Y-$age_arr[1]; 
	$wheresql.=" AND  h.birthdate>='$age_min' AND h.birthdate<'$age_max' ";
}
// 猎头认证
if(isset($aset['audit'])  && $aset['audit']<>'')
{
	$audit=intval($aset['audit']);
	if($audit==1)
	{
		$wheresql.=" AND  h.belongcompany_audit=1 AND h.card_audit=1 AND h.avatars_audit=1 ";
	}
}
if (isset($aset['key']) && !empty($aset['key']))
{
	$key=trim($aset['key']);
	$wheresql.=" AND h.likekey LIKE '%{$key}%' ";
}
if (!empty($wheresql))
{
$wheresql=" WHERE ".ltrim(ltrim($wheresql),'AND');
}
if (isset($aset['page']))
{
	require_once(QISHI_ROOT_PATH.'include/page.class.php');
	$total_sql="SELECT COUNT(*) AS num FROM ".table('hunter_profile')." as h {$joinsql} {$wheresql}";
	$total_count=$db->get_total($total_sql);	
	$page = new page(array('total'=>$total_count, 'perpage'=>$aset['row']));
	$currenpage=$page->nowindex;
	$aset['start']=abs($currenpage-1)*$aset['row'];
	$countpage = $total_count%$aset['row']==0?$total_count/$aset['row']:intval($total_count/$aset['row'])+1;
	$smarty->assign('countpage',$countpage);
	
	$smarty->assign('page',$page->show(7));
	$smarty->assign('pagemin',$page->show(6));
	
	$smarty->assign('currenpage',$currenpage);
	$smarty->assign('total',$total_count);
}
	$limit=" LIMIT {$aset['start']} , {$aset['row']}";
	$list = $id = array();
	$result = $db->query("SELECT * FROM ".table('hunter_profile')." as h ".$joinsql.$wheresql.$orderbysql.$limit);
 	while($row = $db->fetch_array($result))
	{
		$row['refreshtime_cn']=daterange(time(),$row['refreshtime'],'Y-m-d',"#FF3300");
		$row['fullname']=cut_str($row['fullname'],$aset['hunterlen'],0,$aset['dot']);
		$row['companyname']=cut_str($row['companyname'],$aset['companynamelen'],0,$aset['dot']);
		$row['experience'] = date('Y')-$row['worktime'];
			if ($aset['brieflylen']>0)
			{
				$row['contents']=cut_str(strip_tags($row['contents']),$aset['brieflylen'],0,$aset['dot']);
			}
			else
			{
				$row['contents']=strip_tags($row['contents']);
			}
			if ($aset['goodtradelen']>0)
			{
				$row['goodtrade_cn']=cut_str(strip_tags($row['goodtrade_cn']),$aset['goodtradelen'],0,$aset['dot']);
			}
			else
			{
				$row['goodtrade_cn']=strip_tags($row['goodtrade_cn']);
			}
 			if ($aset['goodcategorylen']>0)
			{
				$row['goodcategory_cn']=cut_str(strip_tags($row['goodcategory_cn']),$aset['goodcategorylen'],0,$aset['dot']);
			}
			else
			{
				$row['goodcategory_cn']=strip_tags($row['goodcategory_cn']);
			}
			
		$row['contents']=strip_tags($row['contents']);
		$row['companyname_']=$row['companyname'];
		$row['huntername_']=$row['huntername'];
		$row['years']=date('Y')+1-$row['worktime_start'];
			if ($row['avatars'])
			{
			$row['photosrc']=$_CFG['site_dir']."data/hunter/photo_img/".$row['avatars'];
			}
			else
			{
			$row['photosrc']=$_CFG['site_dir']."data/hunter/photo_img/no_photo.gif";
			}
		$timenow=time();
		$wheresql1.="  AND audit=1 AND display=1 ";
		
		$row['countjobs'] = $db->get_total("select count(*) as num from ".table('jobs')." where uid=".$row['uid']." ".$wheresql1);
		
		foreach ($jobs as $key => $value) {
			$row['jobslist'][$key]['jobs_name'] = cut_str($value['jobs_name'],10,0,"..");
			$row['jobslist'][$key]['refreshtime'] = date("Y-m-d",$value['refreshtime']);
			if(intval($_CFG['subsite_id'])==0){
				$row['jobslist'][$key]['jobs_url'] = url_rewrite("QS_hunter_jobsshow",array("id"=>$value['id']),true,$value['subsite_id']);
			}else{
				$row['jobslist'][$key]['jobs_url'] = url_rewrite("QS_hunter_jobsshow",array("id"=>$value['id']),true);
			}
		}
		$list[] = $row;
		unset($row);
	}
	$smarty->assign('number',$total_count);
	$smarty->assign($aset['listname'],$list);
}
?>