<?php
function tpl_function_qishi_resume_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['namelen'] = $a[1];
		break;
	case "特长描述长度":
		$aset['specialtylen'] = $a[1];
		break;
	case "意向职位长度":
		$aset['jobslen'] = $a[1];
		break;
	case "填补字符":
		$aset['dot'] = $a[1];
		break;
	case "职位分类":
		$aset['jobcategory'] = trim($a[1]);
		break;
	case "职位大类":
		$aset['category'] = trim($a[1]);
		break;
	case "职位小类":
		$aset['subclass'] = trim($a[1]);
		break;
	case "地区分类":
		$aset['citycategory'] = trim($a[1]);
		break;
	case "地区大类":
		$aset['district'] = $a[1];
		break;
	case "地区小类":
		$aset['sdistrict'] = $a[1];
		break;
	case "标签":
		$aset['tag'] = $a[1];
		break;
	case "学历":
		$aset['education'] = $a[1];
		break;
	case "工作经验":
		$aset['experience'] = $a[1];
		break;
	case "等级":
		$aset['talent'] = $a[1];
		break;
	case "性别":
		$aset['sex'] = $a[1];
		break;
	case "照片":
		$aset['photo'] = $a[1];
		break;
	case "关键字":
		$aset['keyword'] = $a[1];
		break;
	case "日期范围":
		$aset['settr'] = $a[1];
		break;
	case "排序":
		$aset['displayorder'] = $a[1];
		break;
	case "分页显示":
		$aset['paged'] = $a[1];
		break;
	case "页面":
		$aset['showname'] = $a[1];
		break;
	case "行业":
		$aset['trade'] = $a[1];
		break;
	case "目前状态":
		$aset['current'] = $a[1];
		break;
	case "年龄最小":
		$aset['min_age'] = $a[1];
		break;
	case "年龄最大":
		$aset['max_age'] = $a[1];
		break;
	case "年薪最小":
		$aset['min_wage'] = $a[1];
		break;
	case "年薪最大":
		$aset['max_wage'] = $a[1];
		break;
	case "编号":
		$aset['id'] = $a[1];
		break;
	}
}
if (is_array($aset)) $aset=array_map("get_smarty_request",$aset);
$aset['listname']=isset($aset['listname'])?$aset['listname']:"list";
$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['namelen']=isset($aset['namelen'])?intval($aset['namelen']):4;
$aset['specialtylen']=isset($aset['specialtylen'])?intval($aset['specialtylen']):0;
$aset['jobslen']=isset($aset['jobslen'])?intval($aset['jobslen']):0;
$aset['dot']=isset($aset['dot'])?$aset['dot']:null;
$aset['talent']=intval($aset['talent'])>0?$aset['talent']:1;
$aset['showname']=isset($aset['showname'])?$aset['showname']:'QS_resumeshow';
$resumetable=table('resume_search');
if (isset($aset['displayorder']))
{
	$arr=explode('>',$aset['displayorder']);
	$arr[1]=preg_match('/asc|desc/',$arr[1])?$arr[1]:"desc";
	if ($arr[0]=="rtime")
	{
		$orderbysql=" ORDER BY r.refreshtime {$arr[1]}";
	}
	elseif ($arr[0]=="experience")
	{
		$orderbysql=" ORDER BY r.experience {$arr[1]}";
	}
}else{
	$orderbysql=" ORDER BY r.refreshtime desc";
}
if (!empty($aset['jobcategory']))
{
	
	$dsql=$xsql="";
	$arr=explode(",",$aset['jobcategory']);
	$arr=array_unique($arr);
	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.=" WHERE ".ltrim(ltrim($dsql.$xsql),'OR');
	
	// $joinsql="  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('resume_jobs')." {$joinwheresql} )AS j ON  r.id=j.pid ";
	$joinsql=$joinsql==""?"  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('resume_jobs')." {$joinwheresql_category} )AS j ON  r.id=j.pid ":$joinsql."  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('resume_jobs')." {$joinwheresql_category} )AS j ON  r.id=j.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 district =".intval($cat[0]);
		}
		else
		{
		$xsql.= " OR sdistrict =".intval($cat[1]);
		}
	}
	$joinwheresql_district.=" WHERE ".ltrim(ltrim($dsql.$xsql),'OR');
	// $joinsql="  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('resume_district')." {$joinwheresql} )AS d ON  r.id=d.pid ";
	$joinsql=$joinsql==""?"  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('resume_district')." {$joinwheresql_district} )AS d ON  r.id=d.pid ":$joinsql."  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('resume_district')." {$joinwheresql_district} )AS d ON  r.id=d.pid ";
}
if (!empty($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('resume_trade')." {$joinwheresql_trade} )AS t ON  r.id=t.pid ":$joinsql."  INNER  JOIN  ( SELECT DISTINCT pid FROM ".table('resume_trade')." {$joinwheresql_trade} )AS t ON  r.id=t.pid ";
}
if (isset($aset['id']) && !empty($aset['id']))
{	
	$wheresql.=" AND r.id = ".intval($aset['id']);
}
if (isset($aset['min_age']) && !empty($aset['min_age']))
{
	$birthdate = date("Y")-intval($aset['min_age']);
	$wheresql.=" AND r.birthdate <= ".$birthdate;
}
if (isset($aset['max_age']) && !empty($aset['max_age']))
{
	$birthdate = date("Y")-intval($aset['max_age']);
	$wheresql.=" AND r.birthdate >= ".$birthdate;
}
if (isset($aset['min_wage']) && !empty($aset['min_wage']))
{
	$wage_hope = intval($aset['min_wage'])*10000;
	$wheresql.=" AND r.wage >= ".$wage_hope;
}
if (isset($aset['max_wage']) && !empty($aset['max_wage']))
{
	$wage_hope = intval($aset['max_wage'])*10000;
	$wheresql.=" AND r.wage <= ".$wage_hope;
}
if (isset($aset['settr']) && !empty($aset['settr']))
{
	$settr_val=intval(strtotime("-".intval($aset['settr'])." day"));
	$wheresql.=" AND r.refreshtime > ".$settr_val;
}
if (isset($aset['experience']) && !empty($aset['experience']))
{
	$wheresql.=" AND r.experience=".intval($aset['experience'])." ";
}
if (isset($aset['education']) && !empty($aset['education']))
{
	$wheresql.=" AND r.education=".intval($aset['education'])." ";
}
if (isset($aset['current']) && !empty($aset['current']))
{
	$wheresql.=" AND r.current=".intval($aset['current'])." ";
}
if (isset($aset['talent']) && !empty($aset['talent']))
{
	$wheresql.=" AND r.talent=".intval($aset['talent'])." ";
}
if (isset($aset['photo']) && !empty($aset['photo']))
{
	$wheresql.=" AND r.photo='".intval($aset['photo'])."' ";
}
if (isset($aset['sex'])  && !empty($aset['sex']))
{
	$wheresql.=" AND r.sex=".intval($aset['sex'])."";
}
if (isset($aset['keyword']) && !empty($aset['keyword']))
{
	$key=trim($aset['keyword']);
	$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 (r.`key`) AGAINST ('{$key}'{$mode}) ";
	$orderbysql="";
}
switch(intval($_SESSION['utype'])){
	case 1:
		$wheresql .= " AND (r.display=1 OR r.display=2) ";break;
	case 3:
		$wheresql .= " AND (r.display=1 OR r.display=3) ";break;
	default:
		$wheresql .= " AND r.display=1 ";break;
}
if(intval($_SESSION['parentuid'])>0){
	$shield_uid = array();
	$shield_company_all = $db->getall("select * from ".table('personal_shield_company'));
	$this_compnay_profile = $db->getone("select companyname from ".table('company_profile')." where uid=".intval($_SESSION['parentuid']));
	foreach ($shield_company_all as $key => $value) {
		if($value['comkeyword']==$this_compnay_profile['companyname']){
			$shield_uid[] = $value['uid'];
		}
	}
	if(!empty($shield_uid)){
		foreach ($shield_uid as $k => $v) {
			$wheresql .= " AND r.uid!=".$v." ";
		}
	}
}
if (!empty($wheresql))
{
$wheresql=" WHERE ".ltrim(ltrim($wheresql),'AND');
}
		if (isset($aset['paged']))
		{
			require_once(QISHI_ROOT_PATH.'include/page.class.php');
			$total_sql="SELECT  COUNT(*) AS num  FROM  {$resumetable} AS r ".$joinsql.$wheresql;
			$total_count=$db->get_total($total_sql);
			if (intval($_CFG['resume_list_max'])>0)
			{
				$total_count>intval($_CFG['resume_list_max']) && $total_count=intval($_CFG['resume_list_max']);
			}
			//echo $total_sql;
			//echo "SELECT  COUNT(DISTINCT r.id) AS num  FROM  ".table('resume')." AS r ".$wheresql;
			$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));
			$total_count = $total_count>3000?$total_count."+":$total_count;
			$smarty->assign('total',$total_count);
		}
	$limit=" LIMIT {$aset['start']} , {$aset['row']}";
	$list = $id = array();
	$idresult = $db->query("SELECT id FROM {$resumetable}  AS r".$joinsql.$wheresql.$orderbysql.$limit);
	//echo "SELECT id FROM {$resumetable}  AS r".$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('resume')."  AS r ".$wheresql.$orderbysql);
		while($row = $db->fetch_array($result))
		{
				
			$row['fullname_']=$row['fullname'];
			$row['fullname']=cut_str($row['fullname'],$aset['namelen'],0,$aset['dot']);
			$row['specialty_']=strip_tags($row['specialty']);
			if ($aset['specialtylen']>0)
			{
			$row['specialty']=cut_str(strip_tags($row['specialty']),$aset['specialtylen'],0,$aset['dot']);
			}
			if ($aset['jobslen']>0)
			{
			$row['intention_jobs']=cut_str(strip_tags($row['intention_jobs']),$aset['jobslen'],0,$aset['dot']);
			}
			$row['photosrc']=$row['photo']?$_CFG['resume_photo_dir_thumb'].$row['photo_img']:$_CFG['resume_photo_dir_thumb']."no_photo.gif";
			$row['resume_url']=url_rewrite($aset['showname'],array('id'=>$row['id']));
			// $row['resume_url']="?act=resume_show&id=".$row['id'];
			$row['refreshtime_cn']=daterange(time(),$row['refreshtime'],'Y-m-d',"#FF3300");
			$row['age']=date("Y")-$row['birthdate'];
			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();
			}
			$list[] = $row;
		}
	}
	else
	{
	$list=array();
	}
	$smarty->assign($aset['listname'], $list);
}
?>