<?php
function tpl_function_qishi_resume_show($params, &$smarty)
{
global $db,$_CFG;
$arr=explode(',',$params['set']);
foreach($arr as $str)
{
$a=explode(':',$str);
	switch ($a[0])
	{
	case "简历ID":
		$aset['id'] = $a[1];
		break;
	case "列表名":
		$aset['listname'] = $a[1];
		break;
	}
}
$aset=array_map("get_smarty_request",$aset);
$utype = intval($_SESSION['utype']);
$smarty->assign("utype",$utype);
$aset['id']=$aset['id']?intval($aset['id']):0;
$aset['listname']=$aset['listname']?$aset['listname']:"list";
$wheresql=" WHERE  id=".$aset['id']."";
$val=$db->getone("select * from ".table('resume').$wheresql." LIMIT  1");

if ($val)
{	
		if(intval($_SESSION['admin_id'])==0 && intval($_SESSION['uid'])!=$val['uid'] && !($val['display']==1 || ($val['display']==2 && intval($_SESSION['utype'])==1) || ($val['display']==3 && intval($_SESSION['utype'])==3))){
			showmsg("您没有查看该简历的权限！",1);
		}
		//重载个别信息
		$val['_wage_hope'] = round($val['wage_hope']/10000,2).'万';
		$val['_wage_current'] = round($val['wage_current']/10000,2).'万';
		$val['fullname_'] = $val['fullname'];
		// 浏览过的简历
		if($_SESSION['utype']==1 || $_SESSION['utype']==3)
		{
			if(!check_view_log(intval($_SESSION['uid']),$val['id']))
			{
				add_view_log(intval($_SESSION['uid']),$_SESSION['utype'],$val['id'],$val['talent']);
			} 
		}
		//个人自己预览
		if($_SESSION['utype'] == '2' && $_SESSION['uid'] == $val['uid'] ){
			
			$tip = '预览：'.$val['title'];
			$val['email'] = $val['email'];
		}
		//企业会员   查看 简历
		if($_SESSION['utype']=="1"){
			//首先判断是否下载过此简历
			$sql = "select did,down_addtime from ".table('company_down_resume')." WHERE company_uid = {$_SESSION['parentuid']} AND resume_id='{$aset['id']}' LIMIT 1";
			$info=$db->getone($sql);
			if (!empty($info))
			{
				$tip = "主动下载 | ".date("Y-m-d h:i",$info['down_addtime']);
			}
			//没下载过(则须判断套餐是否允许查看)
			else{
				$setmeal=get_user_setmeal(intval($_SESSION['parentuid']));
				$setmeal['setmeal_name'] = cut_str($setmeal['setmeal_name'],3,0);
				$smarty->assign('setmeal',$setmeal);
				$count_today = count_view_resume_times_today($_SESSION['parentuid'],$val['talent']);
				$see_num = $val['talent']=="1"?$setmeal['see_ordinary_resume']:$setmeal['see_senior_resume'];
				if($count_today>=$see_num){
					showmsg("已经超出今天可查看简历数！",1);
				}
				//套餐允许
				else{
					//查看简历是否主动应聘
					$apply = $db->getone("select jobs_name,apply_addtime from ".table('personal_jobs_apply')." where belong_uid=".$_SESSION['parentuid']." and resume_id=".$val['id']." limit 1");
					if(!empty($apply)){
						$tip = "主动应聘职位".$apply['jobs_name']." | ".date("Y-m-d H:i",$apply['apply_addtime']);
					}
					//查看简历是否是猎头推荐
					else{
						//当未下载和不是主动应聘的(姓名,电话,邮件全为******)
						$val['fullname'] = "*****";
						$val['telephone'] = "*****";
						$val['email'] = "*****";
						$recommend = $db->getone("select jobs_name,recommend_addtime from ".table('hunter_recommend')." where company_uid=".$_SESSION['parentuid']." and resume_id=".$val['id']." limit 1");
						if(!empty($recommend)){
							$tip = "猎头为职位".$recommend['jobs_name']."推荐 | ".date("Y-m-d H:i",$recommend['recommend_addtime']);
							$smarty->assign('is_hun_recom','1');
						}else{
							$tip = $_CFG['site_name']."真诚为您推荐 ! 若该简历符合您的要求 , 记得下载";
						}
					}
				}
			}
		}
		//猎头会员  查看 简历
		if($_SESSION['utype']=="3"){
			$favorites = $db->getone("select * from ".table('hunter_favorites')." where hunter_uid=".$_SESSION['uid']." and resume_id=".$val['id']." limit 1");
			if(!empty($favorites)){
				$tip = "该简历来自于人才库 | ".date("Y-m-d H:i",$favorites['favorites_addtime']);
			}
			else{
				$tip = $_CFG['site_name']."真诚为您推荐 ! 若该简历符合您的要求 , 记得下载";
				$val['fullname'] = "*****";
				$val['telephone'] = "*****";
				$val['email'] = "*****";
			}
		} 
		$smarty->assign("tip",$tip);
		
		//教育
		$val['user_info'] = $db->getone("select email_audit,mobile_audit,last_login_time from ".table('members')." where uid=".$val['uid']);
		$val['education_list']=get_resume_education($val['uid'],$val['id']);
		foreach ($val['education_list'] as $key => $value) { 
			if($value['today'] == '1'){ 
				$val['education_list'][$key]['duration'] = timediff(date("Y-m-d",$value['starttime']),date("Y-m-d",time())); 
			}else{ 
				$val['education_list'][$key]['duration'] = timediff(date("Y-m-d",$value['starttime']),date("Y-m-d",$value['endtime'])); 
			}
		}
		//工作列表
		$val['work_list']=get_resume_work($val['uid'],$val['id']);
		foreach ($val['work_list'] as $key => $value) { 
			if($value['today'] == '1'){ 
				$val['work_list'][$key]['duration'] = timediff(date("Y-m-d",$value['starttime']),date("Y-m-d",time())); 
			}else{ 
				$val['work_list'][$key]['duration'] = timediff(date("Y-m-d",$value['starttime']),date("Y-m-d",$value['endtime'])); 
			}
		}
		//项目经验
		$val['project_list']=get_resume_project($val['uid'],$val['id']);
		foreach ($val['project_list'] as $key => $value) { 
			if($value['today'] == '1'){ 
				$val['project_list'][$key]['duration'] = timediff(date("Y-m-d",$value['starttime']),date("Y-m-d",time())); 
			}else{ 
				$val['project_list'][$key]['duration'] = timediff(date("Y-m-d",$value['starttime']),date("Y-m-d",$value['endtime'])); 
			}
		}
		//语言
		$val['language_list']=get_resume_language($val['uid'],$val['id']);
		$val['age']=date("Y")-$val['birthdate'];
		if ($val['photo_img'])
		{
			$val['photosrc']=$_CFG['resume_photo_dir'].$val['photo_img'];
		}
		else
		{
			$val['photosrc']=$_CFG['resume_photo_dir_thumb']."no_photo.gif";
		}
		$val['tagcn']=preg_replace("/\d+/", '',$val['tag']);
		$val['tagcn']=preg_replace('/\,/','',$val['tagcn']);
		$val['tagcn']=preg_replace('/\|/','&nbsp;&nbsp;&nbsp;',$val['tagcn']);	
}
else
{
	header("HTTP/1.1 404 Not Found"); 
	$smarty->display("404.htm");
	exit();
}
$smarty->assign($aset['listname'],$val);
}
function get_resume_education($uid,$pid)
{
	global $db;
	$sql = "SELECT * FROM ".table('resume_education')." WHERE uid='".intval($uid)."' AND pid='".intval($pid)."' ";
	return $db->getall($sql);
}
function get_resume_work($uid,$pid)
{
	global $db;
	$sql = "select * from ".table('resume_work')." where uid=".intval($uid)." AND pid='".$pid."' " ;
	return $db->getall($sql);
}
function get_resume_project($uid,$pid)
{
	global $db;
	$sql = "select * from ".table('resume_project')." where uid='".intval($uid)."' AND pid='".intval($pid)."'";
	return $db->getall($sql);
}
function get_resume_language($uid,$pid)
{
	global $db;
	$sql = "select * from ".table('resume_language')." where pid='".intval($pid)."' AND  uid='".intval($uid)."' ";
	return $db->getall($sql);
}
function get_user_setmeal($uid)
{
	global $db;
	$uid=intval($uid);
	$sql = "select * from ".table('members_setmeal')."  WHERE uid='{$uid}' AND  effective=1 LIMIT 1";
	return $db->getone($sql);
}
function count_view_resume_times_today($uid,$talent){
	global $db;
	$starttime = strtotime(date("Y-m-d"));
	$sql = "select count(*) as num from ".table('view_resume')." where uid=".$uid." and talent=".$talent." and addtime>=".$starttime." and uid!=".$uid;
	return $db->get_total($sql);
}
function timediff($date1,$date2){
	$date1_stamp=strtotime($date1);
	$date2_stamp=strtotime($date2);
	list($date_1['y'],$date_1['m'])=explode("-",date('Y-m',$date1_stamp));
	list($date_2['y'],$date_2['m'])=explode("-",date('Y-m',$date2_stamp));
	$month =  abs(($date_2['y']-$date_1['y'])*12 +$date_2['m']-$date_1['m']);
	$year =  floor($month/12);
	if($year == '0'){
		$time = $month.'月';
	}else{
		$sy_month = $month-($year*12);
		if($sy_month == '0'){
			$time = $year.'年';
		}else{
			$time = $year.'年'.$sy_month.'月';
		}
	}
	return $time;
} 
function check_view_log($uid,$resumeid){
	global $db;
	$result = $db->getone("select * from ".table("view_resume")." where `uid`=".$uid." and `resumeid`=".$resumeid);
	return $result;
}
function add_view_log($uid,$utype,$resumeid,$talent){
	$setsqlarr['uid'] = $uid;
	$setsqlarr['utype'] = $utype;
	$setsqlarr['resumeid'] = $resumeid;
	$setsqlarr['talent'] = $talent;
	$setsqlarr['addtime'] = time();
	inserttable(table("view_resume"),$setsqlarr);
}
?>