<?php
/*********************************************
*骑士职位列表
* *******************************************/
function tpl_function_qishi_jobs_list($params, &$smarty)
{
global $db,$_CFG;
$arr=explode(',',$params['set']);
foreach($arr 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['jobslen'] = $a[1];
		break;
	case "企业名长度":
		$aset['companynamelen'] = $a[1];
		break;
	case "描述长度":
		$aset['brieflylen'] = $a[1];
		break;
	case "行业长度":
		$aset['tradelen'] = $a[1];
		break;
	case "填补字符":
		$aset['dot'] = $a[1];
		break;
	case "职位分类":
		$aset['jobcategory'] = $a[1];
		break;
	case "职位大类":
		$aset['category'] = $a[1];
		break;
	case "职位小类":
		$aset['subclass'] = $a[1];
		break;
	case "地区分类":
		$aset['citycategory'] = $a[1];
		break;
	case "地区大类":
		$aset['district'] = $a[1];
		break;
	case "地区小类":
		$aset['sdistrict'] = $a[1];
		break;
	case "道路":
		$aset['street'] = $a[1];
		break;
	case "写字楼":
		$aset['officebuilding'] = $a[1];
		break;
	case "标签":
		$aset['tag'] = $a[1];
		break;
	case "行业":
		$aset['trade'] = $a[1];
		break;
	case "学历":
		$aset['education'] = $a[1];
		break;
	case "工作经验":
		$aset['experience'] = $a[1];
		break;
	case "年薪范围":
		$aset['wage'] = $a[1];
		break;
	case "职位类型":
		$aset['nature'] = $a[1];
		break;
	case "公司性质":
		$aset['companynature'] = $a[1];
		break;
	case "公司规模":
		$aset['scale'] = $a[1];
		break;
	case "紧急招聘":
		$aset['emergency'] = $a[1];
		break;
	case "推荐":
		$aset['recommend'] = $a[1];
		break;
	case "关键字":
		$aset['key'] = $a[1];
		break;
	case "关键字类型":
		$aset['keytype'] = $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['jobs_type'] = $a[1];
		break;
	case "会员类型":
		$aset['utype'] = $a[1];
		break;
	}
}
//$aset数组里面存了 从URL中读到的各种参数
//get_smarty_request()->将参数中的$_GET参数替换成实际的内容
$aset=array_map("get_smarty_request",$aset);
$aset['listname']=isset($aset['listname'])?$aset['listname']:"list";
//row 显示条数
$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['tradelen']=isset($aset['tradelen'])?intval($aset['tradelen']):35;
$aset['jobslen']=isset($aset['jobslen'])?intval($aset['jobslen']):8;
$aset['companynamelen']=isset($aset['companynamelen'])?intval($aset['companynamelen']):15;
$aset['brieflylen']=isset($aset['brieflylen'])?intval($aset['brieflylen']):0;
$openorderby=false;
//排序
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 j.refreshtime {$arr[1]}";
		$jobstable=table('jobs_search_rtime');
		}
		elseif ($arr[0]=="recommend")
		{
		$orderbysql=" ORDER BY j.recommend {$arr[1]} , refreshtime {$arr[1]}";
		$jobstable=table('jobs_search_recommend');		
		}
		elseif ($arr[0]=="hot")
		{
		$orderbysql=" ORDER BY j.click {$arr[1]}";
		$jobstable=table('jobs_search_hot');		
		}
		elseif ($arr[0]=="scale")
		{
		$orderbysql=" ORDER BY j.scale {$arr[1]},refreshtime {$arr[1]}";
		$jobstable=table('jobs_search_scale');		
		}
		elseif ($arr[0]=="key")
		{
		$jobstable=table('jobs_search_key');
		}
		elseif ($arr[0]=="wage")
		{
		$orderbysql=" ORDER BY j.wage_min {$arr[1]},j.recommend {$arr[1]} ";	
		$jobstable=table('jobs_search_recommend');
		}
		elseif ($arr[0]=="null")
		{
		$orderbysql="";
		$jobstable=table('jobs_search_rtime');
		}
		else
		{
		$orderbysql=" ORDER BY j.recommend {$arr[1]} , j.refreshtime {$arr[1]}";
		$jobstable=table('jobs_search_recommend');	
		}
}
else
{
		$orderbysql=" ORDER BY j.recommend DESC , j.refreshtime DESC";
		$jobstable=table('jobs_search_recommend');
}
$orderbysql.=",id desc ";
// 职位类型
if (isset($aset['jobs_type'])  && $aset['jobs_type']<>'')
{
	$jobstype_arr = explode(".", $aset['jobs_type']);
	$where_or_sql = "";
	foreach($jobstype_arr as $jk){
		$where_or_sql .= " or j.jobs_type=".$jk." ";
	}
	$where_or_sql = "(".ltrim(trim($where_or_sql),"or").")";
	$wheresql.=" AND ".$where_or_sql;
}
if (isset($aset['settr']) && $aset['settr']<>'')
{
	$settr=intval($aset['settr']);
	if ($settr>0)
	{
	$settr_val=intval(strtotime("-".$aset['settr']." day"));
	$wheresql.=" AND j.addtime>".$settr_val;
	}
}
//某个会员的职位配合会员类型使用
if (isset($aset['uid'])  && $aset['uid']<>'')
{
	$utype=intval($aset['utype']);
	if($utype==3)
	{
		$wheresql.=" AND j.uid=".intval($aset['uid']);
	}else
	{
		$wheresql.=" AND j.parentuid=".intval($aset['uid']);
	}	
}
if (isset($aset['companynature'])  && $aset['companynature']<>'')
{
	$wheresql.=" AND j.companynature=".intval($aset['companynature']);
}
//职位性质
if (isset($aset['nature']) && $aset['nature']<>'')
{
	$aset['nature'] =  trim($aset['nature']);
	switch ($aset['nature']) {
		case '2':
			$wheresql.=" AND j.utype=1 ";
			break;
		case '3':
			$wheresql.=" AND j.utype=3 ";
			break;
		default:
			break;
	}
}
if (isset($aset['scale']) && $aset['scale']<>'')
{
	$wheresql.=" AND j.scale=".intval($aset['scale']);
}
if (isset($aset['education']) && $aset['education']<>'')
{
	$wheresql.=" AND j.education=".intval($aset['education']);
}
if (isset($aset['wage'])  && $aset['wage']<>'')
{
	$aset['wage'] = trim($aset['wage']);
	switch ($aset['wage']) {
		case '10':
			$wheresql.=" AND j.wage_max>= 5 AND j.wage_max<=10 ";
			break;
		case '15':
			$wheresql.=" AND j.wage_max>= 11 AND j.wage_max<=15 ";
			break;
		case '20':
			$wheresql.=" AND j.wage_max>= 16  AND j.wage_max<=20 ";
			break;
		case '30':
			$wheresql.=" AND j.wage_max>= 21 AND j.wage_max<=30 ";
			break;
		case '50':
			$wheresql.=" AND j.wage_max>= 31 AND j.wage_max<=50 ";
			break;
		case '100':
			$wheresql.=" AND j.wage_max>= 50 ";
			break;
		default:
			break;
	}
}
if (isset($aset['experience'])  && $aset['experience']<>'')
{
	$wheresql.=" AND j.experience=".intval($aset['experience']);
}
if (isset($aset['trade']) && $aset['trade']<>'')
{
	if (strpos($aset['trade'],","))
	{
		$or=$orsql="";
		$arr=explode(",",$aset['trade']);
		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['trade']);
	}
	
	if (!empty($joinwheresql_trade))
	{
	$joinwheresql_trade=" WHERE ".ltrim(ltrim($joinwheresql_trade),'AND');
	}
	$joinsql=$joinsql==""?"  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('jobs_trade')." {$joinwheresql_trade} )AS t ON  j.id=t.pid ":$joinsql."  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('jobs_trade')." {$joinwheresql_trade} )AS t ON  j.id=t.pid ";
}
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 j.district =".intval($cat[0]);
			}
			else
			{
			$xsql.= " OR j.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 j.district IN  ({$sqlin}) ";
			}
		}
		else
		{
			$wheresql.=" AND j.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 j.sdistrict IN  ({$sqlin}) ";
			}
		}
		else
		{
			$wheresql.=" AND j.sdistrict =".intval($aset['sdistrict']);
		}
	}	
}
if (!empty($aset['jobcategory']))
{
	$dsql=$xsql="";
	$arr=explode(",",$aset['jobcategory']);
	$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_cat.=" AND  (".ltrim(ltrim($dsql.$xsql),'OR').") ";
	if (!empty($joinwheresql_cat))
	{
	$joinwheresql_cat=" WHERE ".ltrim(ltrim($joinwheresql_cat),'AND');
	}
	$joinsql=$joinsql==""?"  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('jobs_category')." {$joinwheresql_cat} )AS c ON  j.id=c.pid ":$joinsql."  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('jobs_category')." {$joinwheresql_cat} )AS c ON  j.id=c.pid ";
}
if (isset($aset['key']) && !empty($aset['key']))
{
	if ($_CFG['jobsearch_purview']=='2')
	{
		if ($_SESSION['username']=='')
		{
		header("Location: ".url_rewrite('QS_index')."?url=".urlencode($_SERVER["REQUEST_URI"]));
		}
	}
	$key=trim($aset['key']);
	if ($_CFG['jobsearch_type']=='1')
	{
		$akey=explode(' ',$key);
		if (count($akey)>1)
		{
		$akey=array_filter($akey);
		$akey=array_slice($akey,0,2);
		$akey=array_map("fulltextpad",$akey);
		$key='+'.implode(' +',$akey);
		$mode=' IN BOOLEAN MODE';
		}
		else
		{
		$key=fulltextpad($key);
		$mode=' ';
		}
		$wheresql.=" AND  MATCH (j.`key`) AGAINST ('{$key}'{$mode}) ";
	}
	else
	{
		$wheresql.=" AND j.likekey LIKE '%{$key}%' ";
	}
	if ($_CFG['jobsearch_sort']=='1')
	{
	$orderbysql=" ORDER BY j.refreshtime DESC,id desc ";
	}
	else
	{
	$orderbysql=" ORDER BY j.refreshtime DESC,id desc ";
	}	
	$jobstable=table('jobs_search_key');
}
$time=time();
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 {$jobstable} as j {$joinsql} {$wheresql}";
	$total_count=$db->get_total($total_sql);	
	if ($_CFG['jobs_list_max']>0)
	{
		$total_count>intval($_CFG['jobs_list_max']) && $total_count=intval($_CFG['jobs_list_max']);
	}
	$page = new page(array('total'=>$total_count, 'perpage'=>$aset['row']));
	$currenpage=$page->nowindex;
	$aset['start']=abs($currenpage-1)*$aset['row'];
	$smarty->assign('page',$page->show(7));
	$smarty->assign('pagemin',$page->show(6));
	$smarty->assign('total',$total_count);
}
	$limit=" LIMIT {$aset['start']} , {$aset['row']}";
	$list = $id = array();
	$idresult = $db->query("SELECT id FROM {$jobstable} as j ".$joinsql.$wheresql.$orderbysql.$limit);
	while($row = $db->fetch_array($idresult))
	{
	$id[]=$row['id'];
	}
	if (!empty($id))
	{
	$wheresql=" WHERE id IN (".implode(',',$id).") ";
	//  职位显示条件
	$result = $db->query("SELECT * FROM ".table('jobs')." as j ".$wheresql.$orderbysql);	
		while($row = $db->fetch_array($result))
		{
		$row['jobs_name_']=$row['jobs_name'];
		$row['refreshtime_cn']=daterange(time(),$row['refreshtime'],'Y-m-d',"#FF3300");
		$row['jobs_name']=cut_str($row['jobs_name'],$aset['jobslen'],0,$aset['dot']);
			if (!empty($row['highlight']))
			{
			$row['jobs_name']="<span style=\"color:{$row['highlight']}\">{$row['jobs_name']}</span>";
			}
			if ($aset['brieflylen']>0)
			{
				$row['briefly']=cut_str(strip_tags($row['contents']),$aset['brieflylen'],0,$aset['dot']);
			}
			else
			{
				$row['briefly']=strip_tags($row['contents']);
			}
		$row['amount']=$row['amount']=="0"?'若干':$row['amount'];
		$row['trade_cn']=cut_str($row['trade_cn'],$aset['tradelen'],0,$aset['dot']);
		$row['briefly_']=strip_tags($row['contents']);
		$row['companyname_']=$row['companyname'];
		$row['companyname']=cut_str($row['companyname'],$aset['companynamelen'],0,$aset['dot']);
		if($row['utype']==3)
		{
			$row['jobs_url']=url_rewrite('QS_hunter_jobsshow',array('id'=>$row['id']));
			$row['company_url']=url_rewrite('QS_hunter_show',array('id'=>$row['company_id']));
		}
		elseif($row['utype']==1)
		{
			$row['jobs_url']=url_rewrite('QS_jobsshow',array('id'=>$row['id']));
			$row['company_url']=url_rewrite('QS_companyshow',array('id'=>$row['company_id']));
		}
		if ($row['tag'])
		{
			$tag=explode('|',$row['tag']);
			$taglist=array();
			if (!empty($tag) && is_array($tag))
			{
				foreach($tag as $t)
				{
				$tli=explode(',',$t);
				$taglist[]=array($tli[0],$tli[1]);
				}
			}
			$row['tag']=$taglist;
		}
		else
		{
		$row['tag']=array();
		}
		if($_SESSION['utype']==3)
		{
			$arr=get_entrust_hunter($row['id'],$_SESSION['uid']);
			if(!empty($arr))
			{
				$row['entrust_hunter']=1;//委托过
			}else
			{
				$row['entrust_hunter']=2;//未委托
			}
		}
		$list[] = $row;
		}
	}
	else
	{
	$list=array();
	}
	$smarty->assign($aset['listname'],$list);
}
// 是否委托过某个猎头
function get_entrust_hunter($jobs_id,$hunter_id)
{
	global $db;
	$jobs_id=intval($jobs_id);
	$hunter_id=intval($hunter_id);

	$sql = "select * from ".table('entrust_hunter')."  WHERE hunter_uid='{$hunter_id}' AND  jobs_id=$jobs_id AND state=1 LIMIT 1";
	return $db->getone($sql);
}
?>